Skip to content

Commit 9f7cf2c

Browse files
committed
pci: clearer intersection code
1 parent 603f9dd commit 9f7cf2c

2 files changed

Lines changed: 8 additions & 8 deletions

File tree

src/arch/x86_64/kernel/pci.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ pub(crate) fn init() {
9494
}
9595

9696
// For Hermit, we currently limit scanning to the first 32 buses.
97-
const PCI_MAX_BUS_NUMBER: u8 = 32;
9897
scan_bus(
9998
0..PCI_MAX_BUS_NUMBER,
10099
PciConfigRegion::Pci(LegacyPciConfigRegion::new()),
@@ -126,7 +125,7 @@ fn scan_bus(bus_range: impl IntoIterator<Item = u8> + Debug, pci_config: PciConf
126125
#[cfg(feature = "acpi")]
127126
mod pcie {
128127
use core::{ptr, slice};
129-
128+
use core::ops::{Bound, IntoBounds, RangeBounds};
130129
use memory_addresses::{PhysAddr, VirtAddr};
131130
use pci_types::{ConfigRegionAccess, PciAddress};
132131

@@ -196,6 +195,7 @@ mod pcie {
196195
// We go through `pci_config_space_address`, which makes sure the physical addresses are valid
197196
let pci_start = self.pci_config_space_address(0, 0, 0).as_u64();
198197
let pci_end = self.pci_config_space_address(PCI_MAX_BUS_NUMBER, u8::MAX, u8::MAX).as_u64() + 0x1000;
198+
let pci_range = pci_start..pci_end;
199199

200200
let all_memory_regions = fdt
201201
.find_all_nodes("/memory")
@@ -204,13 +204,11 @@ mod pcie {
204204
for mem_region in all_memory_regions {
205205
let region_start = u64::try_from(mem_region.starting_address.addr()).unwrap();
206206
let region_end = region_start + u64::try_from(mem_region.size.expect("found a memory region with no declared size")).unwrap();
207+
let region_range = region_start..region_end;
207208

208-
if
209-
(pci_start >= region_start && pci_start <= region_end) // PCI region starts within the memory region
210-
|| (pci_end >= region_start && pci_end <= region_end) // PCI region ends within the memory region
211-
|| (pci_start <= region_start && pci_end >= region_end) // PCI region contains the memory region
212-
{
213-
error!("The declared PCI region {pci_start:x}-{pci_end:x} may overlap with physical memory region {region_start:x}-{region_end:x}");
209+
let intersection = region_range.intersect(pci_range);
210+
if !intersection.is_empty() {
211+
error!("The declared PCI region {pci_start:x}-{pci_end:x} overlaps with physical memory region {region_start:x}-{region_end:x}. Intersection: {intersection:?}");
214212
return false;
215213
}
216214
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
),
6767
feature(thread_local)
6868
)]
69+
#![cfg_attr(feature = "acpi", feature(range_bounds_is_empty))]
70+
#![cfg_attr(feature = "acpi", feature(range_into_bounds))]
6971
#![cfg_attr(target_os = "none", no_std)]
7072
#![cfg_attr(target_os = "none", feature(custom_test_frameworks))]
7173
#![cfg_attr(all(target_os = "none", test), test_runner(crate::test_runner))]

0 commit comments

Comments
 (0)