aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / pci / configuration.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Pci root bus resource configuration descriptor parsing. |
| 4 | |
| 5 | #[cfg(feature = "alloc")] |
| 6 | use alloc::vec::Vec; |
| 7 | #[cfg(feature = "alloc")] |
| 8 | use core::ffi::c_void; |
| 9 | #[cfg(feature = "alloc")] |
| 10 | use core::ptr; |
| 11 | |
| 12 | /// Represents the type of resource described by a QWORD Address Space Descriptor. |
| 13 | /// This corresponds to the `resource_type` field at offset 0x03 in the descriptor. |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 15 | #[repr(u8)] |
| 16 | pub enum ResourceRangeType { |
| 17 | /// Memory Range (value = 0) |
| 18 | /// Indicates that the descriptor describes a memory-mapped address range. |
| 19 | /// Commonly used for MMIO regions decoded by the PCI root bridge. |
| 20 | Memory = 0, |
| 21 | |
| 22 | /// I/O Range (value = 1) |
| 23 | /// Indicates that the descriptor describes a legacy I/O port range. |
| 24 | /// Used for devices that communicate via port-mapped I/O. |
| 25 | Io = 1, |
| 26 | |
| 27 | /// Bus Number Range (value = 2) |
| 28 | /// Indicates that the descriptor describes a range of PCI bus numbers. |
| 29 | /// Used to define the bus hierarchy behind a PCI root bridge. |
| 30 | Bus = 2, |
| 31 | |
| 32 | /// Unknown or vendor-specific resource type. |
| 33 | /// Captures any unrecognized value for forward compatibility. |
| 34 | Unknown(u8), |
| 35 | } |
| 36 | impl From<u8> for ResourceRangeType { |
| 37 | fn from(value: u8) -> Self { |
| 38 | match value { |
| 39 | 0 => Self::Memory, |
| 40 | 1 => Self::Io, |
| 41 | 2 => Self::Bus, |
| 42 | other => Self::Unknown(other), |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /// Represents a parsed QWORD Address Space Descriptor from UEFI. |
| 48 | /// This structure describes a decoded resource range for a PCI root bridge. |
| 49 | #[derive(Clone, Debug)] |
| 50 | pub struct QwordAddressSpaceDescriptor { |
| 51 | /// Type of resource: Memory, I/O, Bus, or Unknown. |
| 52 | pub resource_range_type: ResourceRangeType, |
| 53 | /// General flags that describe decode behavior (e.g., positive decode). |
| 54 | pub general_flags: u8, |
| 55 | /// Type-specific flags (e.g., cacheability for memory). |
| 56 | pub type_specific_flags: u8, |
| 57 | /// Granularity of the address space (typically 32 or 64). |
| 58 | /// Indicates whether the range is 32-bit or 64-bit. |
| 59 | pub granularity: u64, |
| 60 | /// Minimum address of the range (inclusive). |
| 61 | pub address_min: u64, |
| 62 | /// Maximum address of the range (inclusive). |
| 63 | pub address_max: u64, |
| 64 | /// Translation offset to convert host address to PCI address. |
| 65 | /// Usually zero unless the bridge remaps addresses. |
| 66 | pub translation_offset: u64, |
| 67 | /// Length of the address range (in bytes or bus numbers). |
| 68 | pub address_length: u64, |
| 69 | } |
| 70 | |
| 71 | /// Parses a list of QWORD Address Space Descriptors from a raw memory region. |
| 72 | /// Stops when it encounters an End Tag descriptor (type 0x79). |
| 73 | /// |
| 74 | /// # Safety |
| 75 | /// |
| 76 | /// `base` must be non-null and point to a descriptor list that is terminated |
| 77 | /// by an End Tag descriptor. |
| 78 | #[cfg(feature = "alloc")] |
| 79 | pub(crate) unsafe fn parse(base: *const c_void) -> Vec<QwordAddressSpaceDescriptor> { |
| 80 | use alloc::slice; |
| 81 | const PCI_RESTBL_QWORDADDRSPEC_TAG: u8 = 0x8a; |
| 82 | const PCI_RESTBL_END_TAG: u8 = 0x79; |
| 83 | |
| 84 | let base: *const u8 = base.cast(); |
| 85 | |
| 86 | // Phase 1: determine total length |
| 87 | let mut offset = 0; |
| 88 | loop { |
| 89 | // SAFETY: The memory is valid. |
| 90 | let tag = unsafe { ptr::read(base.add(offset)) }; |
| 91 | offset += match tag { |
| 92 | PCI_RESTBL_QWORDADDRSPEC_TAG => 3 + 0x2B, |
| 93 | PCI_RESTBL_END_TAG => break, |
| 94 | _ => panic!("{tag}"), // Unknown tag - bailing |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | // Phase 2: parse descriptors from resource table |
| 99 | // SAFETY: The pointer is valid for the requested slice length. |
| 100 | let mut bfr: &[u8] = unsafe { slice::from_raw_parts(base, offset) }; |
| 101 | let mut descriptors = Vec::new(); |
| 102 | while !bfr.is_empty() { |
| 103 | match bfr[0] { |
| 104 | PCI_RESTBL_QWORDADDRSPEC_TAG => { |
| 105 | let descriptor = QwordAddressSpaceDescriptor { |
| 106 | resource_range_type: ResourceRangeType::from(bfr[0x03]), |
| 107 | general_flags: bfr[0x04], |
| 108 | type_specific_flags: bfr[0x05], |
| 109 | granularity: u64::from_le_bytes(bfr[0x06..0x06 + 8].try_into().unwrap()), |
| 110 | address_min: u64::from_le_bytes(bfr[0x0E..0x0E + 8].try_into().unwrap()), |
| 111 | address_max: u64::from_le_bytes(bfr[0x16..0x16 + 8].try_into().unwrap()), |
| 112 | translation_offset: u64::from_le_bytes(bfr[0x1E..0x1E + 8].try_into().unwrap()), |
| 113 | address_length: u64::from_le_bytes(bfr[0x26..0x26 + 8].try_into().unwrap()), |
| 114 | }; |
| 115 | descriptors.push(descriptor); |
| 116 | |
| 117 | bfr = &bfr[3 + 0x2B..]; |
| 118 | } |
| 119 | _ => break, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | descriptors |
| 124 | } |
| 125 | |
| 126 | #[cfg(test)] |
| 127 | mod tests { |
| 128 | use crate::proto::pci::configuration::ResourceRangeType; |
| 129 | |
| 130 | #[test] |
| 131 | fn parse() { |
| 132 | // example acpi pci qword configuration table export from a qemu vm |
| 133 | const BFR: &[u8] = &[ |
| 134 | 138, 43, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 0, 0, 0, 0, 0, 0, 255, 111, 0, 0, |
| 135 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 138, 43, 0, 0, 0, 0, 32, |
| 136 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 255, 255, 15, 129, 0, 0, 0, 0, 0, 0, 0, |
| 137 | 0, 0, 0, 0, 0, 0, 0, 16, 1, 0, 0, 0, 0, 138, 43, 0, 0, 0, 0, 64, 0, 0, 0, 0, 0, 0, 0, |
| 138 | 0, 0, 0, 0, 192, 0, 0, 0, 255, 255, 15, 0, 192, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 139 | 16, 0, 0, 0, 0, 0, 138, 43, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 140 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 121, 0, |
| 141 | ]; |
| 142 | // SAFETY: The test buffer ends with an End Tag descriptor. |
| 143 | let configuration = unsafe { super::parse(BFR.as_ptr().cast()) }; |
| 144 | assert_eq!(configuration.len(), 4); |
| 145 | let (mut cnt_mem, mut cnt_io, mut cnt_bus) = (0, 0, 0); |
| 146 | for entry in &configuration { |
| 147 | match entry.resource_range_type { |
| 148 | ResourceRangeType::Memory => cnt_mem += 1, |
| 149 | ResourceRangeType::Io => cnt_io += 1, |
| 150 | ResourceRangeType::Bus => cnt_bus += 1, |
| 151 | _ => unreachable!(), |
| 152 | } |
| 153 | } |
| 154 | assert_eq!(cnt_mem, 2); |
| 155 | assert_eq!(cnt_io, 1); |
| 156 | assert_eq!(cnt_bus, 1); |
| 157 | } |
| 158 | } |