feat(boot): parse every DMAR unit and reserved region and decide which units to translate from the present PCI functions

This commit is contained in:
Tyler Smith committed 2026-10-28 11:11:53 +00:00
1 parent 5a8edb74ea
commit c5731330d6
3 files changed
+591

No files matched your search

+205
View File
@@ -2,10 +2,12 @@
//! include-all unit or a completely matched set of direct endpoint scopes can
//! identify its remapper; neither proves device quiescence. Bridges, aliases,
//! multi-hop scopes, reserved regions, ATS and unknown structures are refused.
use crate::remapping::Scope;
use alloc::vec::Vec;
const HEADER: usize = 48;
const DRHD: u16 = 0;
const RMRR: u16 = 1;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DmarError {
@@ -34,6 +36,163 @@ fn word(bytes: &[u8], offset: usize) -> u16 {
u16::from_le_bytes(bytes[offset..offset + 2].try_into().unwrap())
}
fn quad(bytes: &[u8], offset: usize) -> u64 {
u64::from_le_bytes(bytes[offset..offset + 8].try_into().unwrap())
}
/// A remapping hardware unit (DRHD): its registers, segment and the PCI
/// requesters it translates, or every requester on the segment no other
/// unit lists when `include_all`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Drhd {
pub base: u64,
pub bytes: usize,
pub segment: u16,
pub include_all: bool,
pub scopes: Vec<Scope>,
}
/// A reserved memory region (RMRR): memory the firmware keeps DMA-ing to
/// for the requesters in its scopes, which a translating unit must map one
/// to one.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Rmrr {
pub segment: u16,
pub base: u64,
pub limit: u64,
pub scopes: Vec<Scope>,
}
/// A parsed DMAR table: the host address width and the units and reserved
/// regions. ATSR, RHSA, ANDD, SATC and later structures describe ATS,
/// proximity and namespace devices, none of which the mount enables, and
/// are skipped.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Remapping {
pub physical_bits: u8,
pub units: Vec<Drhd>,
pub reserved: Vec<Rmrr>,
}
/// The PCI scopes of a structure starting at `from`. IOAPIC, HPET and ACPI
/// namespace scopes name interrupt sources, which matter only to interrupt
/// remapping.
fn scopes(entry: &[u8], from: usize) -> Result<Vec<Scope>, DmarError> {
let mut found = Vec::new();
let mut scope = from;
while scope < entry.len() {
let header = entry
.get(scope..scope + 6)
.ok_or(DmarError::InvalidStructure)?;
let bytes = usize::from(header[1]);
if bytes < 6 || !bytes.is_multiple_of(2) {
return Err(DmarError::InvalidStructure);
}
let end = scope
.checked_add(bytes)
.ok_or(DmarError::InvalidStructure)?;
let path = entry
.get(scope + 6..end)
.ok_or(DmarError::InvalidStructure)?
.as_chunks::<2>()
.0;
if path.iter().any(|hop| hop[0] > 31 || hop[1] > 7) {
return Err(DmarError::InvalidStructure);
}
if matches!(header[0], 1 | 2) {
if path.is_empty() {
return Err(DmarError::InvalidStructure);
}
found.push(Scope {
bridge: header[0] == 2,
bus: header[5],
path: path.iter().map(|hop| (hop[0], hop[1])).collect(),
});
}
scope = end;
}
Ok(found)
}
/// Parse a DMAR table, validating its checksum, width and every DRHD and
/// RMRR it holds.
pub fn parse(table: &[u8]) -> Result<Remapping, DmarError> {
if table.len() < HEADER
|| &table[..4] != b"DMAR"
|| u32::from_le_bytes(table[4..8].try_into().unwrap()) as usize != table.len()
|| table.iter().fold(0u8, |sum, byte| sum.wrapping_add(*byte)) != 0
{
return Err(DmarError::InvalidTable);
}
let physical_bits = table[36]
.checked_add(1)
.filter(|bits| (32..=64).contains(bits))
.ok_or(DmarError::UnsupportedWidth)?;
let mut remapping = Remapping {
physical_bits,
units: Vec::new(),
reserved: Vec::new(),
};
let mut offset = HEADER;
while offset < table.len() {
let header = table
.get(offset..offset + 4)
.ok_or(DmarError::InvalidStructure)?;
let kind = word(header, 0);
let length = usize::from(word(header, 2));
let end = offset
.checked_add(length)
.filter(|_| length >= 4)
.ok_or(DmarError::InvalidStructure)?;
let entry = table.get(offset..end).ok_or(DmarError::InvalidStructure)?;
match kind {
DRHD => {
if length < 16 {
return Err(DmarError::InvalidStructure);
}
let base = quad(entry, 8);
let bytes = 1usize
.checked_shl(12 + u32::from(entry[5] & 0xf))
.ok_or(DmarError::InvalidStructure)?;
if base == 0
|| !base.is_multiple_of(4096)
|| base.checked_add(bytes as u64).is_none()
{
return Err(DmarError::InvalidStructure);
}
remapping.units.push(Drhd {
base,
bytes,
segment: word(entry, 6),
include_all: entry[4] & 1 != 0,
scopes: scopes(entry, 16)?,
});
}
RMRR => {
if length < 24 {
return Err(DmarError::InvalidStructure);
}
let (base, limit) = (quad(entry, 8), quad(entry, 16));
if !base.is_multiple_of(4096) || limit < base || limit & 0xfff != 0xfff {
return Err(DmarError::InvalidStructure);
}
remapping.reserved.push(Rmrr {
segment: word(entry, 6),
base,
limit,
scopes: scopes(entry, 24)?,
});
}
_ => {}
}
offset = end;
}
if remapping.units.is_empty() {
return Err(DmarError::MissingUnit);
}
Ok(remapping)
}
/// Validate the complete table before returning one owner of the live flat bus. No
/// unknown/RMRR structure is skipped in order to manufacture a usable domain.
pub fn single_flat_owner(
@@ -199,6 +358,52 @@ mod tests {
table[50..52].copy_from_slice(&length.to_le_bytes());
seal(table)
}
#[test]
fn parses_units_reserved_regions_and_bridge_paths() {
let mut table = fixture();
// A second unit with an endpoint behind a bridge and a bridge scope.
let mut unit = vec![0, 0, 0, 0, 0, 0, 0, 0];
unit.extend(0xfed91000u64.to_le_bytes());
unit.extend([1, 10, 0, 0, 0, 0, 0x1c, 0, 0, 0]);
unit.extend([2, 8, 0, 0, 0, 0, 0x1d, 0]);
unit.extend([3, 8, 0, 0, 2, 0xf0, 0x1f, 0]);
let length = unit.len() as u16;
unit[2..4].copy_from_slice(&length.to_le_bytes());
table.extend(unit);
let mut rmrr = vec![1, 0, 32, 0, 0, 0, 0, 0];
rmrr.extend(0x7d000000u64.to_le_bytes());
rmrr.extend(0x7d01ffffu64.to_le_bytes());
rmrr.extend([1, 8, 0, 0, 0, 0, 0x14, 0]);
table.extend(rmrr);
table.extend([3, 0, 4, 0]);
let parsed = parse(&seal(table)).unwrap();
assert_eq!(parsed.physical_bits, 48);
assert_eq!(parsed.units.len(), 2);
assert!(parsed.units[0].include_all);
assert_eq!(
parsed.units[1].scopes,
[
Scope {
bridge: false,
bus: 0,
path: vec![(0x1c, 0), (0, 0)]
},
Scope {
bridge: true,
bus: 0,
path: vec![(0x1d, 0)]
}
]
);
assert_eq!(parsed.reserved[0].limit, 0x7d01ffff);
assert_eq!(parsed.reserved[0].scopes[0].path, [(0x14, 0)]);
let mut broken = fixture();
broken.extend([1, 0, 24, 0, 0, 0, 0, 0]);
broken.extend(0x1000u64.to_le_bytes());
broken.extend(0x1000u64.to_le_bytes());
assert_eq!(parse(&seal(broken)), Err(DmarError::InvalidStructure));
}
#[test]
fn requires_exact_live_endpoint_coverage_and_valid_direct_paths() {
let one = [1, 8, 0, 0, 0, 0, 1, 0];
+1
View File
@@ -15,6 +15,7 @@ pub mod passthrough;
pub mod pe;
pub mod ram;
pub mod record;
pub mod remapping;
pub mod storage;
pub mod topology;
pub mod worker_bindings;
+385
View File
@@ -0,0 +1,385 @@
//! Which DMA remapping units the mount takes over, and which requesters
//! each one isolates: the PCI functions present behind the root buses, the
//! units' device scopes resolved against them, and the policy that leaves a
//! unit untranslated when taking it would cut off DMA nothing re-maps.
//!
//! A unit is left untranslated when its scope holds a display controller no
//! driver claims (an integrated GPU's scanout reaches its stolen memory
//! through the unit, with the firmware's reserved regions mapped one to
//! one), or when a reserved region (DMAR RMRR, IVRS IVMD) names a requester
//! other than a USB host controller, whose reserved region serves only the
//! firmware's legacy USB emulation until the xHCI BIOS handoff (Linux's
//! `device_rmrr_is_relaxable`). A display driver that maps those regions
//! itself passes its function in `claimed_displays`, and the unit is taken.
use alloc::vec::Vec;
/// One present PCI function: its requester ID, class code and, for a
/// PCI-to-PCI bridge, its secondary and subordinate buses.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Present {
pub rid: u16,
/// Base class, subclass and programming interface.
pub class: [u8; 3],
pub bridge: Option<(u8, u8)>,
}
impl Present {
pub fn display(&self) -> bool {
self.class[0] == 0x03
}
pub fn usb(&self) -> bool {
self.class[0] == 0x0c && self.class[1] == 0x03
}
}
const VENDOR: u16 = 0x00;
const CLASS: u16 = 0x08;
const HEADER: u16 = 0x0c;
const BUSES: u16 = 0x18;
/// Every function behind `roots`, found as Linux's `pci_scan_child_bus`
/// finds them: each root bus, then each bus a bridge leads to. `read32`
/// reads a configuration dword of a requester, `None` where nothing
/// answers.
pub fn enumerate(roots: &[u8], mut read32: impl FnMut(u16, u16) -> Option<u32>) -> Vec<Present> {
let mut found = Vec::new();
let mut seen = [false; 256];
let mut pending: Vec<u8> = roots.to_vec();
while let Some(bus) = pending.pop() {
if core::mem::replace(&mut seen[usize::from(bus)], true) {
continue;
}
for device in 0..32u16 {
let first = u16::from(bus) << 8 | device << 3;
let Some(header) = read32(first, VENDOR)
.filter(|id| *id as u16 != 0xffff)
.and_then(|_| read32(first, HEADER))
else {
continue;
};
let functions = if header >> 16 & 0x80 != 0 { 8 } else { 1 };
for function in 0..functions {
let rid = first | function;
if read32(rid, VENDOR).is_none_or(|id| id as u16 == 0xffff) {
continue;
}
let class = read32(rid, CLASS).unwrap_or(0);
let kind = read32(rid, HEADER).unwrap_or(0) >> 16 & 0x7f;
let bridge = (kind == 1)
.then(|| read32(rid, BUSES))
.flatten()
.map(|buses| ((buses >> 8) as u8, (buses >> 16) as u8))
.filter(|&(secondary, subordinate)| {
secondary > bus && subordinate >= secondary
});
if let Some((secondary, _)) = bridge {
pending.push(secondary);
}
found.push(Present {
rid,
class: [(class >> 24) as u8, (class >> 16) as u8, (class >> 8) as u8],
bridge,
});
}
}
}
found.sort_unstable_by_key(|function| function.rid);
found
}
/// A device scope: an endpoint, or a bridge and every function behind it,
/// named by its start bus and (device, function) path.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Scope {
pub bridge: bool,
pub bus: u8,
pub path: Vec<(u8, u8)>,
}
impl Scope {
/// The present requesters the scope names: none when its path does not
/// lead to a present function.
pub fn resolve(&self, present: &[Present]) -> Vec<u16> {
let find = |rid: u16| present.iter().find(|function| function.rid == rid);
let mut bus = self.bus;
let mut target = None;
for (index, &(device, function)) in self.path.iter().enumerate() {
let rid = u16::from(bus) << 8 | u16::from(device) << 3 | u16::from(function);
let Some(found) = find(rid) else {
return Vec::new();
};
if index + 1 == self.path.len() {
target = Some(found);
} else if let Some((secondary, _)) = found.bridge {
bus = secondary;
} else {
return Vec::new();
}
}
match target {
None => Vec::new(),
Some(found) if !self.bridge => alloc::vec![found.rid],
Some(found) => {
let Some((secondary, subordinate)) = found.bridge else {
return alloc::vec![found.rid];
};
present
.iter()
.filter(|function| {
function.rid == found.rid
|| (secondary..=subordinate).contains(&((function.rid >> 8) as u8))
})
.map(|function| function.rid)
.collect()
}
}
}
}
/// Why a unit is left untranslated.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Leave {
/// No present requester sits in its scope.
Empty,
/// This display controller sits in its scope, and no driver maps the
/// unit's reserved regions for it.
Display(u16),
/// A reserved region names this requester, which is not a USB host
/// controller.
Reserved(u16),
}
/// The requesters a unit covers, sorted, when the policy takes it.
pub fn decide(
covered: &[u16],
reserved: &[u16],
present: &[Present],
claimed_displays: &[u16],
) -> Result<Vec<u16>, Leave> {
let mut covered = covered.to_vec();
covered.sort_unstable();
covered.dedup();
if covered.is_empty() {
return Err(Leave::Empty);
}
let function = |rid: u16| present.iter().find(|function| function.rid == rid);
if let Some(&display) = covered.iter().find(|&&rid| {
function(rid).is_some_and(Present::display) && !claimed_displays.contains(&rid)
}) {
return Err(Leave::Display(display));
}
if let Some(&held) = reserved
.iter()
.find(|&&rid| covered.contains(&rid) && !function(rid).is_some_and(Present::usb))
{
return Err(Leave::Reserved(held));
}
Ok(covered)
}
/// The present requesters an include-all unit covers: those no other unit
/// on its segment lists.
pub fn remainder(present: &[Present], listed: &[u16]) -> Vec<u16> {
present
.iter()
.map(|function| function.rid)
.filter(|rid| !listed.contains(rid))
.collect()
}
/// What the policy does with each unit of `remapping` on `segment`, by its
/// index in `remapping.units`: listed units first, then include-all units
/// over what the listed ones leave.
pub fn plan(
remapping: &crate::dmar::Remapping,
segment: u16,
present: &[Present],
claimed_displays: &[u16],
) -> Vec<(usize, Result<Vec<u16>, Leave>)> {
let reserved: Vec<u16> = remapping
.reserved
.iter()
.filter(|region| region.segment == segment)
.flat_map(|region| {
region
.scopes
.iter()
.flat_map(|scope| scope.resolve(present))
})
.collect();
let on_segment = || {
remapping
.units
.iter()
.enumerate()
.filter(|(_, unit)| unit.segment == segment)
};
let mut listed = Vec::new();
let mut decisions = Vec::new();
for (index, unit) in on_segment().filter(|(_, unit)| !unit.include_all) {
let covered: Vec<u16> = unit
.scopes
.iter()
.flat_map(|scope| scope.resolve(present))
.collect();
listed.extend_from_slice(&covered);
decisions.push((
index,
decide(&covered, &reserved, present, claimed_displays),
));
}
for (index, _) in on_segment().filter(|(_, unit)| unit.include_all) {
let covered = remainder(present, &listed);
decisions.push((
index,
decide(&covered, &reserved, present, claimed_displays),
));
}
decisions
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::{collections::BTreeMap, vec};
/// A function's requester, class, multifunction bit and bridge buses.
type Function = (u16, [u8; 3], bool, Option<(u8, u8)>);
/// A config space of `(rid, offset) -> dword`.
fn space(functions: &[Function]) -> BTreeMap<(u16, u16), u32> {
let mut space = BTreeMap::new();
for &(rid, class, multi, bridge) in functions {
space.insert((rid, VENDOR), 0x1234_8086);
space.insert(
(rid, CLASS),
u32::from(class[0]) << 24 | u32::from(class[1]) << 16 | u32::from(class[2]) << 8,
);
let kind = u32::from(bridge.is_some()) | if multi { 0x80 } else { 0 };
space.insert((rid, HEADER), kind << 16);
if let Some((secondary, subordinate)) = bridge {
space.insert(
(rid, BUSES),
u32::from(secondary) << 8 | u32::from(subordinate) << 16,
);
}
}
space
}
fn inventory() -> Vec<Present> {
let space = space(&[
(0x0000, [6, 0, 0], false, None),
(0x0010, [3, 0, 0], false, None),
(0x00a0, [0x0c, 3, 0x30], true, None),
(0x00a1, [5, 0, 0], true, None),
(0x00e0, [6, 4, 0], false, Some((1, 2))),
(0x0100, [6, 4, 0], false, Some((2, 2))),
(0x0200, [1, 8, 2], false, None),
]);
enumerate(&[0], |rid, offset| space.get(&(rid, offset)).copied())
}
#[test]
fn enumeration_follows_bridges_and_multifunction_devices() {
let found = inventory();
let rids: Vec<u16> = found.iter().map(|function| function.rid).collect();
assert_eq!(
rids,
[0x0000, 0x0010, 0x00a0, 0x00a1, 0x00e0, 0x0100, 0x0200]
);
assert!(found[1].display());
assert!(found[2].usb());
assert_eq!(found[4].bridge, Some((1, 2)));
}
#[test]
fn scopes_resolve_endpoints_paths_and_bridge_subtrees() {
let present = inventory();
let endpoint = Scope {
bridge: false,
bus: 0,
path: vec![(0x1c, 0), (0, 0), (0, 0)],
};
assert_eq!(endpoint.resolve(&present), [0x0200]);
let bridge = Scope {
bridge: true,
bus: 0,
path: vec![(0x1c, 0)],
};
assert_eq!(bridge.resolve(&present), [0x00e0, 0x0100, 0x0200]);
let absent = Scope {
bridge: false,
bus: 0,
path: vec![(5, 0)],
};
assert!(absent.resolve(&present).is_empty());
}
#[test]
fn an_include_all_unit_covers_what_listed_units_leave() {
use crate::dmar::{Drhd, Remapping, Rmrr};
let present = inventory();
let unit = |include_all, scopes| Drhd {
base: 0xfed9_0000,
bytes: 4096,
segment: 0,
include_all,
scopes,
};
let graphics = Scope {
bridge: false,
bus: 0,
path: vec![(2, 0)],
};
let mut remapping = Remapping {
physical_bits: 39,
units: vec![unit(true, vec![]), unit(false, vec![graphics])],
reserved: vec![Rmrr {
segment: 0,
base: 0x7d00_0000,
limit: 0x7d01_ffff,
scopes: vec![Scope {
bridge: false,
bus: 0,
path: vec![(0x14, 0)],
}],
}],
};
let decisions = plan(&remapping, 0, &present, &[]);
assert_eq!(decisions[0], (1, Err(Leave::Display(0x0010))));
assert_eq!(
decisions[1],
(0, Ok(vec![0x0000, 0x00a0, 0x00a1, 0x00e0, 0x0100, 0x0200]))
);
assert!(plan(&remapping, 1, &present, &[]).is_empty());
remapping.reserved[0].scopes[0].path = vec![(0x1c, 0), (0, 0), (0, 0)];
assert_eq!(
plan(&remapping, 0, &present, &[])[1],
(0, Err(Leave::Reserved(0x0200)))
);
}
#[test]
fn displays_and_non_usb_reservations_leave_a_unit_untranslated() {
let present = inventory();
let all = remainder(&present, &[]);
assert_eq!(
decide(&all, &[], &present, &[]),
Err(Leave::Display(0x0010))
);
assert_eq!(decide(&all, &[], &present, &[0x0010]), Ok(all.clone()));
let rest = remainder(&present, &[0x0010]);
assert_eq!(decide(&rest, &[0x00a0], &present, &[]), Ok(rest.clone()));
assert_eq!(
decide(&rest, &[0x0200], &present, &[]),
Err(Leave::Reserved(0x0200))
);
// A reservation for a requester another unit covers does not count.
assert_eq!(
decide(&[0x0200], &[0x0010], &present, &[]),
Ok(vec![0x0200])
);
assert_eq!(decide(&[], &[], &present, &[]), Err(Leave::Empty));
}
}