Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/crates/aienos-kernel/src/acpi.rs # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / crates / aienos-kernel / src / acpi.rs` 660 lines ยท 25387 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 //! Minimal ACPI table parsing for early CPU topology. 17 //! 18 //! Pure functions over byte slices so they are testable on the host; the boot 19 //! image copies no memory and only hands the firmware's tables in as slices. 20 //! 21 //! The MADT lists one GIC CPU interface (GICC) entry per core. Since ACPI 6.0 22 //! each entry carries a Processor Power Efficiency Class; lower numbers are more 23 //! power-efficient. On heterogeneous parts such as the GB10 (Cortex-A725 and 24 //! Cortex-X925) this is how firmware tells the OS which cores are which, 25 //! without starting the secondary cores. 26 27 pub const SDT_HEADER_LEN: usize = 36; 28 const MADT_ENTRIES_OFFSET: usize = 44; 29 const GICC_TYPE: u8 = 0x0B; 30 const GICD_TYPE: u8 = 0x0C; 31 const GICR_TYPE: u8 = 0x0E; 32 const GICC_FLAGS: usize = 12; 33 const GICC_MPIDR: usize = 68; 34 const GICC_EFFICIENCY_CLASS: usize = 76; 35 /// GICC entries shorter than this predate the efficiency class field. 36 const GICC_LEN_WITH_CLASS: usize = 77; 37 const GICC_ENABLED: u32 = 1; 38 const GICC_ONLINE_CAPABLE: u32 = 1 << 3; 39 40 /// Distinct efficiency classes tracked; real parts have two or three. 41 pub const MAX_CLASSES: usize = 8; 42 43 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 44 pub enum AcpiError { 45 Truncated, 46 BadSignature, 47 BadChecksum, 48 BadEntry, 49 } 50 51 /// GICv3 physical register regions advertised by the MADT. 52 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 53 pub struct GicBases { 54 pub distributor: Option, 55 pub redistributor: Option<(u64, u32)>, 56 } 57 58 /// Reads the first GIC distributor and redistributor regions from a MADT. 59 pub fn madt_gic_bases(madt: &[u8]) -> Result { 60 let madt = checked_table(madt, b"APIC")?; 61 let mut bases = GicBases::default(); 62 let mut at = MADT_ENTRIES_OFFSET; 63 while at < madt.len() { 64 let kind = madt[at]; 65 let len = *madt.get(at + 1).ok_or(AcpiError::Truncated)? as usize; 66 if len < 2 || at + len > madt.len() { 67 return Err(AcpiError::BadEntry); 68 } 69 let entry = &madt[at..at + len]; 70 if kind == GICD_TYPE { 71 if len < 24 { 72 return Err(AcpiError::BadEntry); 73 } 74 if bases.distributor.is_none() { 75 bases.distributor = u64_at(entry, 8); 76 } 77 } else if kind == GICR_TYPE { 78 if len < 16 { 79 return Err(AcpiError::BadEntry); 80 } 81 if bases.redistributor.is_none() { 82 bases.redistributor = Some(( 83 u64_at(entry, 4).ok_or(AcpiError::BadEntry)?, 84 u32_at(entry, 12).ok_or(AcpiError::BadEntry)?, 85 )); 86 } 87 } 88 at += len; 89 } 90 Ok(bases) 91 } 92 93 fn u32_at(b: &[u8], at: usize) -> Option { 94 Some(u32::from_le_bytes(b.get(at..at + 4)?.try_into().ok()?)) 95 } 96 97 fn u64_at(b: &[u8], at: usize) -> Option { 98 Some(u64::from_le_bytes(b.get(at..at + 8)?.try_into().ok()?)) 99 } 100 101 /// Length field of a system description table header. 102 pub fn sdt_length(header: &[u8]) -> Option { 103 u32_at(header, 4).map(|l| l as usize) 104 } 105 106 /// Validates signature, declared length and checksum; returns the table. 107 pub fn checked_table<'a>(bytes: &'a [u8], signature: &[u8; 4]) -> Result<&'a [u8], AcpiError> { 108 let len = sdt_length(bytes).ok_or(AcpiError::Truncated)?; 109 if len < SDT_HEADER_LEN || len > bytes.len() { 110 return Err(AcpiError::Truncated); 111 } 112 let table = &bytes[..len]; 113 if &table[..4] != signature { 114 return Err(AcpiError::BadSignature); 115 } 116 if table.iter().fold(0u8, |sum, b| sum.wrapping_add(*b)) != 0 { 117 return Err(AcpiError::BadChecksum); 118 } 119 Ok(table) 120 } 121 122 /// Physical addresses listed in an XSDT. 123 pub fn xsdt_entries(xsdt: &[u8]) -> impl Iterator + '_ { 124 xsdt.get(SDT_HEADER_LEN..) 125 .unwrap_or(&[]) 126 .as_chunks::<8>() 127 .0 128 .iter() 129 .map(|c| u64::from_le_bytes(*c)) 130 } 131 132 #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] 133 pub struct CpuTopology { 134 /// Enabled or online-capable cores listed in the MADT. 135 pub cores: u32, 136 /// `(efficiency_class, core_count)`, sorted by class; unused slots are zero. 137 pub classes: [(u8, u32); MAX_CLASSES], 138 pub distinct_classes: usize, 139 /// Cores whose GICC entry is too old to carry an efficiency class. 140 pub unknown_class: u32, 141 /// Affinity (MPIDR) of the first listed core, for cross-checking the boot core. 142 pub first_mpidr: Option, 143 /// The boot core (matched by MPIDR affinity) appears in the MADT. 144 pub boot_core_listed: bool, 145 /// Efficiency class of the boot core, if its entry carries one. 146 pub boot_class: Option, 147 } 148 149 impl CpuTopology { 150 fn add(&mut self, class: u8) { 151 if let Some(slot) = self.classes[..self.distinct_classes] 152 .iter_mut() 153 .find(|(c, _)| *c == class) 154 { 155 slot.1 += 1; 156 } else if self.distinct_classes < MAX_CLASSES { 157 self.classes[self.distinct_classes] = (class, 1); 158 self.distinct_classes += 1; 159 self.classes[..self.distinct_classes].sort_unstable_by_key(|(c, _)| *c); 160 } else { 161 self.unknown_class += 1; 162 } 163 } 164 165 pub fn classes(&self) -> &[(u8, u32)] { 166 &self.classes[..self.distinct_classes] 167 } 168 } 169 170 /// Serial console described by the ACPI SPCR table. 171 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 172 pub struct SpcrConsole { 173 /// SPCR interface type (0x00 16550, 0x03 PL011, 0x0e SBSA, 0x12 16550 with GAS). 174 pub interface_type: u8, 175 /// Generic Address Structure: 0 = system memory. 176 pub address_space: u8, 177 pub register_bit_width: u8, 178 /// GAS access size: 1 byte, 2 word, 3 dword, 4 qword. 179 pub access_size: u8, 180 pub base: u64, 181 } 182 183 /// Which early UART driver fits an SPCR console. 184 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 185 pub enum UartKind { 186 /// 16550-compatible with 32-bit registers (4-byte stride), as on the DGX Spark. 187 Ns16550Mmio32(u64), 188 /// ARM PL011 or SBSA generic UART (PL011 register subset). 189 Pl011(u64), 190 /// Present but not drivable by the early console. 191 Unsupported, 192 } 193 194 impl SpcrConsole { 195 pub fn kind(&self) -> UartKind { 196 if self.address_space != 0 || self.base == 0 { 197 return UartKind::Unsupported; 198 } 199 match self.interface_type { 200 0x00 | 0x12 if self.access_size == 3 || self.register_bit_width == 32 => { 201 UartKind::Ns16550Mmio32(self.base) 202 } 203 0x03 | 0x0d | 0x0e => UartKind::Pl011(self.base), 204 _ => UartKind::Unsupported, 205 } 206 } 207 } 208 209 const SPCR_INTERFACE_TYPE: usize = 36; 210 const SPCR_BASE_GAS: usize = 40; 211 212 /// Reads the console from a validated SPCR table. 213 pub fn spcr_console(spcr: &[u8]) -> Result { 214 let spcr = checked_table(spcr, b"SPCR")?; 215 let gas = spcr 216 .get(SPCR_BASE_GAS..SPCR_BASE_GAS + 12) 217 .ok_or(AcpiError::Truncated)?; 218 Ok(SpcrConsole { 219 interface_type: spcr[SPCR_INTERFACE_TYPE], 220 address_space: gas[0], 221 register_bit_width: gas[1], 222 access_size: gas[3], 223 base: u64_at(gas, 4).ok_or(AcpiError::Truncated)?, 224 }) 225 } 226 227 /// MPIDR affinity fields (Aff3, Aff2, Aff1, Aff0); other bits are flags. 228 pub const MPIDR_AFFINITY_MASK: u64 = 0xff_00ff_ffff; 229 230 /// Summarises the cores in a validated MADT ("APIC") table. 231 pub fn madt_cpu_topology(madt: &[u8]) -> Result { 232 madt_cpu_topology_for(madt, None) 233 } 234 235 /// Like `madt_cpu_topology`, also locating the core whose MPIDR is `boot_mpidr`. 236 pub fn madt_cpu_topology_for( 237 madt: &[u8], 238 boot_mpidr: Option, 239 ) -> Result { 240 let madt = checked_table(madt, b"APIC")?; 241 let mut topo = CpuTopology::default(); 242 let mut at = MADT_ENTRIES_OFFSET; 243 while at < madt.len() { 244 let kind = madt[at]; 245 let len = *madt.get(at + 1).ok_or(AcpiError::Truncated)? as usize; 246 if len < 2 || at + len > madt.len() { 247 return Err(AcpiError::BadEntry); 248 } 249 let entry = &madt[at..at + len]; 250 if kind == GICC_TYPE { 251 let flags = u32_at(entry, GICC_FLAGS).ok_or(AcpiError::BadEntry)?; 252 if flags & (GICC_ENABLED | GICC_ONLINE_CAPABLE) != 0 { 253 topo.cores += 1; 254 if topo.first_mpidr.is_none() { 255 topo.first_mpidr = u64_at(entry, GICC_MPIDR); 256 } 257 let mpidr = u64_at(entry, GICC_MPIDR).unwrap_or(u64::MAX); 258 if boot_mpidr 259 .is_some_and(|b| b & MPIDR_AFFINITY_MASK == mpidr & MPIDR_AFFINITY_MASK) 260 { 261 topo.boot_core_listed = true; 262 topo.boot_class = 263 (len >= GICC_LEN_WITH_CLASS).then(|| entry[GICC_EFFICIENCY_CLASS]); 264 } 265 if len >= GICC_LEN_WITH_CLASS { 266 topo.add(entry[GICC_EFFICIENCY_CLASS]); 267 } else { 268 topo.unknown_class += 1; 269 } 270 } 271 } 272 at += len; 273 } 274 Ok(topo) 275 } 276 277 /// MCFG allocations start after the header and 8 reserved bytes. 278 const MCFG_ENTRIES_OFFSET: usize = 44; 279 const MCFG_ENTRY_LEN: usize = 16; 280 const IORT_NODE_HEADER_LEN: usize = 16; 281 const IORT_NODE_SMMUV3: u8 = 4; 282 const IORT_NODE_ROOT_COMPLEX: u8 = 2; 283 284 /// PCI requester ID range routed through a SMMUv3 node. 285 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 286 pub struct IortStreamMapping { 287 pub input_base: u32, 288 pub id_count: u32, 289 pub output_base: u32, 290 } 291 292 /// SMMUv3 base and the PCI requester ID mappings that target it. 293 #[derive(Clone, Debug, PartialEq, Eq)] 294 pub struct IortSmmu { 295 pub base: u64, 296 pub pci_streams: alloc::vec::Vec, 297 } 298 299 impl IortSmmu { 300 /// Resolve a PCI requester ID through the first matching root-complex map. 301 pub fn stream_id_for(&self, requester_id: u32) -> Option { 302 self.pci_streams.iter().find_map(|mapping| { 303 let relative = requester_id.checked_sub(mapping.input_base)?; 304 (relative <= mapping.id_count) 305 .then(|| mapping.output_base.checked_add(relative)) 306 .flatten() 307 }) 308 } 309 } 310 311 /// Reads the first SMMUv3 node and PCI root-complex mappings to it from IORT. 312 pub fn iort_smmuv3(iort: &[u8]) -> Result, AcpiError> { 313 let iort = checked_table(iort, b"IORT")?; 314 let count = u32_at(iort, 36).ok_or(AcpiError::Truncated)? as usize; 315 let mut at = u32_at(iort, 40).ok_or(AcpiError::Truncated)? as usize; 316 let mut smmu = None; 317 let mut roots = alloc::vec::Vec::new(); 318 for _ in 0..count { 319 let hdr = iort 320 .get(at..at + IORT_NODE_HEADER_LEN) 321 .ok_or(AcpiError::BadEntry)?; 322 let kind = hdr[0]; 323 let len = u16::from_le_bytes([hdr[1], hdr[2]]) as usize; 324 if len < IORT_NODE_HEADER_LEN || at.checked_add(len).is_none_or(|end| end > iort.len()) { 325 return Err(AcpiError::BadEntry); 326 } 327 let node = &iort[at..at + len]; 328 let map_count = u32_at(node, 8).ok_or(AcpiError::BadEntry)? as usize; 329 let map_offset = u32_at(node, 12).ok_or(AcpiError::BadEntry)? as usize; 330 let map_bytes = map_count.checked_mul(20).ok_or(AcpiError::BadEntry)?; 331 if map_count != 0 332 && (map_offset < IORT_NODE_HEADER_LEN 333 || map_offset 334 .checked_add(map_bytes) 335 .is_none_or(|end| end > len)) 336 { 337 return Err(AcpiError::BadEntry); 338 } 339 if kind == IORT_NODE_SMMUV3 && smmu.is_none() { 340 // SMMUv3 node's first field after the common header is Base Address. 341 let base = u64_at(node, 16).ok_or(AcpiError::BadEntry)?; 342 if base == 0 { 343 return Err(AcpiError::BadEntry); 344 } 345 smmu = Some((at as u32, base)); 346 } 347 if kind == IORT_NODE_ROOT_COMPLEX { 348 roots.push((node.to_vec(), map_offset, map_count)); 349 } 350 at += len; 351 } 352 let Some((smmu_offset, base)) = smmu else { 353 return Ok(None); 354 }; 355 let mut pci_streams = alloc::vec::Vec::new(); 356 for (node, map_offset, map_count) in roots { 357 for i in 0..map_count { 358 let m = &node[map_offset + i * 20..map_offset + (i + 1) * 20]; 359 if u32_at(m, 12) == Some(smmu_offset) { 360 pci_streams.push(IortStreamMapping { 361 input_base: u32_at(m, 0).ok_or(AcpiError::BadEntry)?, 362 id_count: u32_at(m, 4).ok_or(AcpiError::BadEntry)?, 363 output_base: u32_at(m, 8).ok_or(AcpiError::BadEntry)?, 364 }); 365 } 366 } 367 } 368 Ok(Some(IortSmmu { base, pci_streams })) 369 } 370 371 /// One PCI Express enhanced configuration (ECAM) window from the MCFG table. 372 #[derive(Clone, Copy, Debug, PartialEq, Eq)] 373 pub struct EcamWindow { 374 pub base: u64, 375 pub segment: u16, 376 pub start_bus: u8, 377 pub end_bus: u8, 378 } 379 380 impl EcamWindow { 381 /// Address of config register `offset` of `bus:device.function`, or 382 /// `None` outside this window or the 4 KiB function space. 383 pub fn config_address(&self, bus: u8, device: u8, function: u8, offset: u16) -> Option { 384 let in_window = (self.start_bus..=self.end_bus).contains(&bus); 385 if !in_window || device > 31 || function > 7 || offset > 0xfff { 386 return None; 387 } 388 let bus = u64::from(bus - self.start_bus); 389 let function_offset = u64::from(device) << 15 | u64::from(function) << 12; 390 Some(self.base + (bus << 20 | function_offset | u64::from(offset))) 391 } 392 } 393 394 /// The ECAM window of a validated MCFG table covering `segment` and `bus`. 395 pub fn mcfg_window(mcfg: &[u8], segment: u16, bus: u8) -> Result, AcpiError> { 396 let mcfg = checked_table(mcfg, b"MCFG")?; 397 let entries = mcfg 398 .get(MCFG_ENTRIES_OFFSET..) 399 .ok_or(AcpiError::Truncated)?; 400 Ok(entries 401 .as_chunks::() 402 .0 403 .iter() 404 .map(|e| EcamWindow { 405 base: u64_at(e, 0).unwrap_or(0), 406 segment: u16::from_le_bytes([e[8], e[9]]), 407 start_bus: e[10], 408 end_bus: e[11], 409 }) 410 .find(|w| w.segment == segment && (w.start_bus..=w.end_bus).contains(&bus))) 411 } 412 413 #[cfg(test)] 414 mod tests { 415 use super::*; 416 use std::vec::Vec; 417 418 fn gicc(mpidr: u64, flags: u32, class: u8, len: usize) -> Vec { 419 let mut e = std::vec![0u8; len]; 420 e[0] = GICC_TYPE; 421 e[1] = len as u8; 422 e[GICC_FLAGS..GICC_FLAGS + 4].copy_from_slice(&flags.to_le_bytes()); 423 e[GICC_MPIDR..GICC_MPIDR + 8].copy_from_slice(&mpidr.to_le_bytes()); 424 if len > GICC_EFFICIENCY_CLASS { 425 e[GICC_EFFICIENCY_CLASS] = class; 426 } 427 e 428 } 429 430 fn madt(entries: &[Vec]) -> Vec { 431 let mut t = std::vec![0u8; MADT_ENTRIES_OFFSET]; 432 t[..4].copy_from_slice(b"APIC"); 433 for e in entries { 434 t.extend_from_slice(e); 435 } 436 let len = t.len() as u32; 437 t[4..8].copy_from_slice(&len.to_le_bytes()); 438 let sum = t.iter().fold(0u8, |s, b| s.wrapping_add(*b)); 439 t[9] = 0u8.wrapping_sub(sum); 440 t 441 } 442 443 #[test] 444 fn parses_iort_smmuv3_base_and_pci_stream_mapping() { 445 let mut table = std::vec![0u8; 48 + 36 + 36]; 446 table[..4].copy_from_slice(b"IORT"); 447 table[36..40].copy_from_slice(&2u32.to_le_bytes()); 448 table[40..44].copy_from_slice(&48u32.to_le_bytes()); 449 let smmu = 48; 450 table[smmu] = IORT_NODE_SMMUV3; 451 table[smmu + 1..smmu + 3].copy_from_slice(&36u16.to_le_bytes()); 452 table[smmu + 16..smmu + 24].copy_from_slice(&0x0900_0000u64.to_le_bytes()); 453 let root = smmu + 36; 454 table[root] = IORT_NODE_ROOT_COMPLEX; 455 table[root + 1..root + 3].copy_from_slice(&36u16.to_le_bytes()); 456 table[root + 8..root + 12].copy_from_slice(&1u32.to_le_bytes()); 457 table[root + 12..root + 16].copy_from_slice(&16u32.to_le_bytes()); 458 table[root + 16..root + 20].copy_from_slice(&0x400u32.to_le_bytes()); 459 table[root + 20..root + 24].copy_from_slice(&0xffu32.to_le_bytes()); 460 table[root + 24..root + 28].copy_from_slice(&0x800u32.to_le_bytes()); 461 table[root + 28..root + 32].copy_from_slice(&(smmu as u32).to_le_bytes()); 462 let len = table.len() as u32; 463 table[4..8].copy_from_slice(&len.to_le_bytes()); 464 let sum = table.iter().fold(0u8, |s, b| s.wrapping_add(*b)); 465 table[9] = 0u8.wrapping_sub(sum); 466 assert_eq!( 467 iort_smmuv3(&table).unwrap(), 468 Some(IortSmmu { 469 base: 0x0900_0000, 470 pci_streams: std::vec![IortStreamMapping { 471 input_base: 0x400, 472 id_count: 0xff, 473 output_base: 0x800, 474 }], 475 }) 476 ); 477 let smmu = iort_smmuv3(&table).unwrap().unwrap(); 478 assert_eq!(smmu.stream_id_for(0x400), Some(0x800)); 479 assert_eq!(smmu.stream_id_for(0x4ff), Some(0x8ff)); 480 assert_eq!(smmu.stream_id_for(0x500), None); 481 } 482 483 #[test] 484 fn extracts_gicd_and_gicr_bases() { 485 let mut dist = std::vec![0u8; 24]; 486 dist[0] = GICD_TYPE; 487 dist[1] = 24; 488 dist[8..16].copy_from_slice(&0x0800_0000u64.to_le_bytes()); 489 let mut redist = std::vec![0u8; 16]; 490 redist[0] = GICR_TYPE; 491 redist[1] = 16; 492 redist[4..12].copy_from_slice(&0x080a_0000u64.to_le_bytes()); 493 redist[12..16].copy_from_slice(&0x20_0000u32.to_le_bytes()); 494 let table = madt(&[dist, redist]); 495 assert_eq!( 496 madt_gic_bases(&table).unwrap(), 497 GicBases { 498 distributor: Some(0x0800_0000), 499 redistributor: Some((0x080a_0000, 0x20_0000)), 500 } 501 ); 502 } 503 504 #[test] 505 fn counts_gb10_style_heterogeneous_cores_by_efficiency_class() { 506 // Ten efficient cores (class 0) then ten performance cores (class 1). 507 let entries: Vec> = (0..20u64) 508 .map(|i| gicc(0x8100_0000 + (i << 8), GICC_ENABLED, u8::from(i >= 10), 82)) 509 .collect(); 510 let topo = madt_cpu_topology(&madt(&entries)).unwrap(); 511 assert_eq!(topo.cores, 20); 512 assert_eq!(topo.classes(), &[(0, 10), (1, 10)]); 513 assert_eq!(topo.unknown_class, 0); 514 assert_eq!(topo.first_mpidr, Some(0x8100_0000)); 515 } 516 517 #[test] 518 fn finds_the_boot_core_class_ignoring_mpidr_flag_bits() { 519 let entries: Vec> = (0..20u64) 520 .map(|i| gicc(0x8100_0000 + (i << 8), GICC_ENABLED, u8::from(i >= 10), 82)) 521 .collect(); 522 let table = madt(&entries); 523 // MPIDR_EL1 bit 31 is RES1 and absent from the MADT copy. 524 let boot = (1 << 31) | 0x8100_0000 | (15 << 8); 525 let topo = madt_cpu_topology_for(&table, Some(boot)).unwrap(); 526 assert!(topo.boot_core_listed); 527 assert_eq!(topo.boot_class, Some(1)); 528 529 let missing = madt_cpu_topology_for(&table, Some(0x42)).unwrap(); 530 assert!(!missing.boot_core_listed); 531 assert_eq!(missing.boot_class, None); 532 } 533 534 #[test] 535 fn skips_disabled_cores_and_counts_old_entries_as_unknown() { 536 let entries = [ 537 gicc(0, GICC_ENABLED, 0, 80), 538 gicc(1, 0, 1, 80), // disabled, not online capable 539 gicc(2, GICC_ONLINE_CAPABLE, 1, 80), // hot-pluggable, counted 540 gicc(3, GICC_ENABLED, 0, 76), // ACPI 5.x entry, no class byte 541 ]; 542 let topo = madt_cpu_topology(&madt(&entries)).unwrap(); 543 assert_eq!(topo.cores, 3); 544 assert_eq!(topo.classes(), &[(0, 1), (1, 1)]); 545 assert_eq!(topo.unknown_class, 1); 546 } 547 548 #[test] 549 fn rejects_bad_checksum_signature_and_lengths() { 550 let good = madt(&[gicc(0, GICC_ENABLED, 0, 80)]); 551 let mut bad_sum = good.clone(); 552 bad_sum[MADT_ENTRIES_OFFSET + 30] ^= 1; 553 assert_eq!(madt_cpu_topology(&bad_sum), Err(AcpiError::BadChecksum)); 554 555 let mut wrong_sig = good.clone(); 556 wrong_sig[0] = b'X'; 557 wrong_sig[9] = wrong_sig[9].wrapping_sub(b'X' - b'A'); 558 assert_eq!(madt_cpu_topology(&wrong_sig), Err(AcpiError::BadSignature)); 559 560 assert_eq!( 561 madt_cpu_topology(&good[..good.len() - 1]), 562 Err(AcpiError::Truncated) 563 ); 564 565 let mut zero_len = madt(&[]); 566 zero_len.extend_from_slice(&[GICC_TYPE, 0]); 567 let len = zero_len.len() as u32; 568 zero_len[4..8].copy_from_slice(&len.to_le_bytes()); 569 zero_len[9] = 0; 570 let sum = zero_len.iter().fold(0u8, |s, b| s.wrapping_add(*b)); 571 zero_len[9] = 0u8.wrapping_sub(sum); 572 assert_eq!(madt_cpu_topology(&zero_len), Err(AcpiError::BadEntry)); 573 } 574 575 fn spcr(interface_type: u8, space: u8, bit_width: u8, access: u8, base: u64) -> Vec { 576 let mut t = std::vec![0u8; 80]; 577 t[..4].copy_from_slice(b"SPCR"); 578 t[4..8].copy_from_slice(&80u32.to_le_bytes()); 579 t[SPCR_INTERFACE_TYPE] = interface_type; 580 t[SPCR_BASE_GAS] = space; 581 t[SPCR_BASE_GAS + 1] = bit_width; 582 t[SPCR_BASE_GAS + 3] = access; 583 t[SPCR_BASE_GAS + 4..SPCR_BASE_GAS + 12].copy_from_slice(&base.to_le_bytes()); 584 let sum = t.iter().fold(0u8, |s, b| s.wrapping_add(*b)); 585 t[9] = 0u8.wrapping_sub(sum); 586 t 587 } 588 589 #[test] 590 fn spcr_selects_the_early_uart_driver() { 591 // DGX Spark style: 16550-compatible, 32-bit registers at 0x16A00000. 592 let spark = spcr_console(&spcr(0x12, 0, 32, 3, 0x16A0_0000)).unwrap(); 593 assert_eq!(spark.kind(), UartKind::Ns16550Mmio32(0x16A0_0000)); 594 // QEMU virt: ARM PL011 at 0x09000000. 595 let qemu = spcr_console(&spcr(0x03, 0, 32, 3, 0x0900_0000)).unwrap(); 596 assert_eq!(qemu.kind(), UartKind::Pl011(0x0900_0000)); 597 // Byte-wide 16550, I/O port space and unknown types are not drivable. 598 assert_eq!( 599 spcr_console(&spcr(0x00, 0, 8, 1, 0x3f8)).unwrap().kind(), 600 UartKind::Unsupported 601 ); 602 assert_eq!( 603 spcr_console(&spcr(0x12, 1, 32, 3, 0x3f8)).unwrap().kind(), 604 UartKind::Unsupported 605 ); 606 assert_eq!( 607 spcr_console(&spcr(0x20, 0, 32, 3, 0x1000)).unwrap().kind(), 608 UartKind::Unsupported 609 ); 610 // A corrupted table is rejected. 611 let mut bad = spcr(0x03, 0, 32, 3, 0x0900_0000); 612 bad[50] ^= 1; 613 assert_eq!(spcr_console(&bad), Err(AcpiError::BadChecksum)); 614 } 615 616 fn mcfg(windows: &[(u64, u16, u8, u8)]) -> Vec { 617 let mut t = std::vec![0u8; MCFG_ENTRIES_OFFSET]; 618 t[..4].copy_from_slice(b"MCFG"); 619 for (base, segment, start, end) in windows { 620 t.extend_from_slice(&base.to_le_bytes()); 621 t.extend_from_slice(&segment.to_le_bytes()); 622 t.extend_from_slice(&[*start, *end, 0, 0, 0, 0]); 623 } 624 let len = t.len() as u32; 625 t[4..8].copy_from_slice(&len.to_le_bytes()); 626 let sum = t.iter().fold(0u8, |s, b| s.wrapping_add(*b)); 627 t[9] = 0u8.wrapping_sub(sum); 628 t 629 } 630 631 #[test] 632 fn mcfg_finds_the_ecam_window_for_a_segment_and_bus() { 633 // QEMU virt high ECAM, plus a second segment starting at bus 0x80. 634 let table = mcfg(&[(0x40_1000_0000, 0, 0, 0xff), (0x6000_0000, 1, 0x80, 0x8f)]); 635 let qemu = mcfg_window(&table, 0, 0).unwrap().unwrap(); 636 assert_eq!(qemu.base, 0x40_1000_0000); 637 assert_eq!(qemu.config_address(0, 2, 0, 0x04), Some(0x40_1001_0004)); 638 let second = mcfg_window(&table, 1, 0x81).unwrap().unwrap(); 639 // Bus numbers count from the window's start bus. 640 assert_eq!(second.config_address(0x81, 1, 3, 0x10), Some(0x6010_b010)); 641 assert_eq!(second.config_address(0x90, 0, 0, 0), None, "past end bus"); 642 assert_eq!(second.config_address(0x80, 32, 0, 0), None); 643 assert_eq!(second.config_address(0x80, 0, 8, 0), None); 644 assert_eq!(second.config_address(0x80, 0, 0, 0x1000), None); 645 assert_eq!(mcfg_window(&table, 1, 0x7f).unwrap(), None); 646 assert_eq!(mcfg_window(&table, 2, 0).unwrap(), None); 647 let mut bad = table.clone(); 648 bad[50] ^= 1; 649 assert_eq!(mcfg_window(&bad, 0, 0), Err(AcpiError::BadChecksum)); 650 } 651 652 #[test] 653 fn reads_xsdt_entry_addresses() { 654 let mut xsdt = std::vec![0u8; SDT_HEADER_LEN]; 655 xsdt.extend_from_slice(&0x1234_5678u64.to_le_bytes()); 656 xsdt.extend_from_slice(&0x9abc_def0u64.to_le_bytes()); 657 let addrs: Vec = xsdt_entries(&xsdt).collect(); 658 assert_eq!(addrs, [0x1234_5678, 0x9abc_def0]); 659 } 660 }