From d60e2e2a6bbb3aab4319ccc9fbb4645ae89022e2 Mon Sep 17 00:00:00 2001 From: Yuke Peng Date: Fri, 25 Jul 2025 15:55:47 +0800 Subject: [PATCH] Align cap_ptr in PCI --- kernel/comps/pci/src/capability/mod.rs | 9 ++++----- ostd/src/bus/pci.rs | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/kernel/comps/pci/src/capability/mod.rs b/kernel/comps/pci/src/capability/mod.rs index 93d549d0b..89b68f397 100644 --- a/kernel/comps/pci/src/capability/mod.rs +++ b/kernel/comps/pci/src/capability/mod.rs @@ -92,16 +92,15 @@ impl Capability { return Vec::new(); } let mut capabilities = Vec::new(); - let mut cap_ptr = + let mut cap_ptr = PciDeviceLocation::align_ptr( dev.location() - .read8(PciDeviceCommonCfgOffset::CapabilitiesPointer as u16) as u16 - & PciDeviceLocation::BIT32_ALIGN_MASK; + .read8(PciDeviceCommonCfgOffset::CapabilitiesPointer as u16) as u16, + ); let mut cap_ptr_vec = Vec::new(); // read all cap_ptr so that it is easy for us to get the length. while cap_ptr > 0 { cap_ptr_vec.push(cap_ptr); - cap_ptr = - dev.location().read8(cap_ptr + 1) as u16 & PciDeviceLocation::BIT32_ALIGN_MASK; + cap_ptr = PciDeviceLocation::align_ptr(dev.location().read8(cap_ptr + 1) as u16); } cap_ptr_vec.sort(); // Push here so that we can calculate the length of the last capability. diff --git a/ostd/src/bus/pci.rs b/ostd/src/bus/pci.rs index 2387fc3fe..e7f95d172 100644 --- a/ostd/src/bus/pci.rs +++ b/ostd/src/bus/pci.rs @@ -77,6 +77,11 @@ impl PciDeviceLocation { impl PciDeviceLocation { const BIT32_ALIGN_MASK: u16 = 0xFFFC; + /// Aligns the given pointer to a 32-bit boundary. + pub const fn align_ptr(ptr: u16) -> u16 { + ptr & Self::BIT32_ALIGN_MASK + } + /// Reads a 8-bit value from the PCI configuration space at the specified offset. pub fn read8(&self, offset: u16) -> u8 { let val = self.read32(offset & Self::BIT32_ALIGN_MASK);