From 545596f296f86680239f02441c09bb7faa80c00e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 22 Jan 2024 17:54:38 +0100 Subject: [PATCH] Don't try to fetch BARs unless there is a matching driver Fetching BARs will temporarily invalidate them, which could cause crashes if a driver happens to access it at the same time. As we don't yet use a single pcid instance, we don't know which devices have a driver attached or not. As such approximate this as the set of devices for which the current pcid instance would load a driver. --- pcid/src/main.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/pcid/src/main.rs b/pcid/src/main.rs index 5789d21263..de7bbe0508 100644 --- a/pcid/src/main.rs +++ b/pcid/src/main.rs @@ -207,9 +207,8 @@ pub struct State { } fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, header: PciHeader) { - let raw_class: u8 = header.class().into(); let mut string = format!("PCI {} {:>04X}:{:>04X} {:>02X}.{:>02X}.{:>02X}.{:>02X} {:?}", - addr, header.vendor_id(), header.device_id(), raw_class, + addr, header.vendor_id(), header.device_id(), header.class(), header.subclass(), header.interface(), header.revision(), header.class()); let device_type = DeviceType::from((header.class(), header.subclass())); match device_type { @@ -229,17 +228,6 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he }, _ => (), } - - let bars = header.bars(&state.pcie); - for (i, bar) in bars.iter().enumerate() { - match bar { - PciBar::None => {}, - PciBar::Memory32{addr,..} => string.push_str(&format!(" {i}={addr:08X}")), - PciBar::Memory64{addr,..} => string.push_str(&format!(" {i}={addr:016X}")), - PciBar::Port(port) => string.push_str(&format!(" {i}=P{port:04X}")), - } - } - info!("{}", string); for driver in config.drivers.iter() { @@ -251,6 +239,21 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he continue; }; + let mut string = String::new(); + let bars = header.bars(&state.pcie); + for (i, bar) in bars.iter().enumerate() { + match bar { + PciBar::None => {}, + PciBar::Memory32 { addr, .. } => string.push_str(&format!(" {i}={addr:08X}")), + PciBar::Memory64 { addr, .. } => string.push_str(&format!(" {i}={addr:016X}")), + PciBar::Port(port) => string.push_str(&format!(" {i}=P{port:04X}")), + } + } + + if !string.is_empty() { + info!(" BAR{}", string); + } + // Enable bus mastering, memory space, and I/O space unsafe { let mut data = state.pcie.read(addr, 0x04);