vmm: move BAR mapping registration from PciBus to DeviceManager

`PciBus::register_mapping()` operates on `mmio_bus` and `io_bus`
which are passed in as external parameters and have nothing to do
with PciBus internal state. Move this logic into
`DeviceManager::register_bar_mapping()` where it belongs, and move
the `PioInsert`/`MmioInsert` error variants from `PciRootError` to
`DeviceManagerError` accordingly.

Signed-off-by: wuxinyue <wuxinyue.wxy@antgroup.com>
Assisted-by: Claude:Opus-4.6
This commit is contained in:
wuxinyue
2026-06-11 10:54:42 +08:00
committed by Rob Bradford
parent bf4b856dd2
commit f787bd7a17
2 changed files with 41 additions and 50 deletions

View File

@@ -13,12 +13,9 @@ use std::sync::{Arc, Barrier, Mutex};
use byteorder::{ByteOrder, LittleEndian}; use byteorder::{ByteOrder, LittleEndian};
use log::warn; use log::warn;
use thiserror::Error; use thiserror::Error;
use vm_device::{Bus, BusDevice, BusDeviceSync}; use vm_device::BusDevice;
use crate::PciBarConfiguration; use crate::configuration::{PciBridgeSubclass, PciClassCode, PciConfiguration, PciHeaderType};
use crate::configuration::{
PciBarRegionType, PciBridgeSubclass, PciClassCode, PciConfiguration, PciHeaderType,
};
use crate::device::{BarReprogrammingParams, DeviceRelocation, Error as PciDeviceError, PciDevice}; use crate::device::{BarReprogrammingParams, DeviceRelocation, Error as PciDeviceError, PciDevice};
/// Denotes the PCI device ID of a bus' root bridge device. /// Denotes the PCI device ID of a bus' root bridge device.
@@ -38,12 +35,6 @@ pub enum PciRootError {
/// Could not allocate an IRQ number. /// Could not allocate an IRQ number.
#[error("Could not allocate an IRQ number")] #[error("Could not allocate an IRQ number")]
AllocateIrq, AllocateIrq,
/// Could not add a device to the port io bus.
#[error("Could not add a device to the port io bus")]
PioInsert(#[source] vm_device::BusError),
/// Could not add a device to the mmio bus.
#[error("Could not add a device to the mmio bus")]
MmioInsert(#[source] vm_device::BusError),
/// Could not find an available device slot on the PCI bus. /// Could not find an available device slot on the PCI bus.
#[error("Could not find an available device slot on the PCI bus")] #[error("Could not find an available device slot on the PCI bus")]
NoPciDeviceSlotAvailable, NoPciDeviceSlotAvailable,
@@ -145,31 +136,6 @@ impl PciBus {
} }
} }
#[expect(clippy::needless_pass_by_value)]
pub fn register_mapping(
&self,
dev: Arc<dyn BusDeviceSync>,
io_bus: &Bus,
mmio_bus: &Bus,
bars: Vec<PciBarConfiguration>,
) -> Result<()> {
for bar in bars {
match bar.region_type() {
PciBarRegionType::IoRegion => {
io_bus
.insert(dev.clone(), bar.addr(), bar.size())
.map_err(PciRootError::PioInsert)?;
}
PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => {
mmio_bus
.insert(dev.clone(), bar.addr(), bar.size())
.map_err(PciRootError::MmioInsert)?;
}
}
}
Ok(())
}
pub fn add_device(&mut self, device_id: u8, device: Arc<Mutex<dyn PciDevice>>) -> Result<()> { pub fn add_device(&mut self, device_id: u8, device: Arc<Mutex<dyn PciDevice>>) -> Result<()> {
self.devices.insert(device_id, device); self.devices.insert(device_id, device);
Ok(()) Ok(())
@@ -559,6 +525,7 @@ mod unit_tests {
use std::result::Result; use std::result::Result;
use super::*; use super::*;
use crate::configuration::PciBarRegionType;
#[derive(Debug)] #[derive(Debug)]
/// Helper struct that mocks the implementation of DeviceRelocation /// Helper struct that mocks the implementation of DeviceRelocation

View File

@@ -74,8 +74,8 @@ use libc::{
}; };
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use pci::{ use pci::{
DeviceRelocation, MmioRegion, PciBarRegionType, PciBdf, PciDevice, VfioDmaMapping, DeviceRelocation, MmioRegion, PciBarConfiguration, PciBarRegionType, PciBdf, PciDevice,
VfioPciDevice, VfioUserDmaMapping, VfioUserPciDevice, VfioUserPciDeviceError, VfioDmaMapping, VfioPciDevice, VfioUserDmaMapping, VfioUserPciDevice, VfioUserPciDeviceError,
}; };
use rate_limiter::group::RateLimiterGroup; use rate_limiter::group::RateLimiterGroup;
use seccompiler::SeccompAction; use seccompiler::SeccompAction;
@@ -306,6 +306,14 @@ pub enum DeviceManagerError {
#[error("Cannot add PCI device")] #[error("Cannot add PCI device")]
AddPciDevice(#[source] pci::PciRootError), AddPciDevice(#[source] pci::PciRootError),
/// Could not add a device to the port io bus
#[error("Could not add a device to the port io bus")]
PioInsert(#[source] vm_device::BusError),
/// Could not add a device to the mmio bus
#[error("Could not add a device to the mmio bus")]
MmioInsert(#[source] vm_device::BusError),
/// Cannot open persistent memory file /// Cannot open persistent memory file
#[error("Cannot open persistent memory file")] #[error("Cannot open persistent memory file")]
PmemFileOpen(#[source] io::Error), PmemFileOpen(#[source] io::Error),
@@ -4120,25 +4128,16 @@ impl DeviceManager {
) )
.map_err(DeviceManagerError::AllocateBars)?; .map_err(DeviceManagerError::AllocateBars)?;
let mut pci_bus = self.pci_segments[segment_id as usize] self.pci_segments[segment_id as usize]
.pci_bus .pci_bus
.lock() .lock()
.unwrap(); .unwrap()
pci_bus
.add_device(bdf.device(), pci_device) .add_device(bdf.device(), pci_device)
.map_err(DeviceManagerError::AddPciDevice)?; .map_err(DeviceManagerError::AddPciDevice)?;
self.bus_devices.push(Arc::clone(&bus_device)); self.bus_devices.push(Arc::clone(&bus_device));
pci_bus self.register_bar_mapping(bus_device, &bars)?;
.register_mapping(
bus_device,
self.address_manager.io_bus.as_ref(),
self.address_manager.mmio_bus.as_ref(),
bars.clone(),
)
.map_err(DeviceManagerError::AddPciDevice)?;
let mut new_resources = Vec::new(); let mut new_resources = Vec::new();
for bar in bars { for bar in bars {
@@ -4154,6 +4153,31 @@ impl DeviceManager {
Ok(new_resources) Ok(new_resources)
} }
#[expect(clippy::needless_pass_by_value)]
fn register_bar_mapping(
&mut self,
bus_device: Arc<dyn BusDeviceSync>,
bars: &[PciBarConfiguration],
) -> DeviceManagerResult<()> {
for bar in bars {
match bar.region_type() {
PciBarRegionType::IoRegion => {
self.address_manager
.io_bus
.insert(bus_device.clone(), bar.addr(), bar.size())
.map_err(DeviceManagerError::PioInsert)?;
}
PciBarRegionType::Memory32BitRegion | PciBarRegionType::Memory64BitRegion => {
self.address_manager
.mmio_bus
.insert(bus_device.clone(), bar.addr(), bar.size())
.map_err(DeviceManagerError::MmioInsert)?;
}
}
}
Ok(())
}
fn add_vfio_devices( fn add_vfio_devices(
&mut self, &mut self,
snapshot: Option<&Snapshot>, snapshot: Option<&Snapshot>,