diff --git a/kernel/src/svsm.rs b/kernel/src/svsm.rs index 02a042208a..1af50f9059 100755 --- a/kernel/src/svsm.rs +++ b/kernel/src/svsm.rs @@ -57,7 +57,7 @@ use svsm::svsm_paging::{ use svsm::task::{KernelThreadStartInfo, schedule_init, start_kernel_task}; use svsm::types::PAGE_SIZE; use svsm::utils::{MemoryRegion, ScopedRef, round_to_pages}; -#[cfg(feature = "virtio-drivers")] +#[cfg(all(feature = "virtio-drivers", feature = "block"))] use svsm::virtio::probe_mmio_slots; #[cfg(all(feature = "vtpm", not(test)))] use svsm::vtpm::vtpm_init; @@ -162,12 +162,12 @@ fn mapping_info_init(launch_info: &KernelLaunchInfo) { /// Returns Ok if initialization is successful or no virtio devices are found /// Returns an error when a virtio device is found but its driver initialization fails. #[cfg(feature = "virtio-drivers")] -fn initialize_virtio_mmio() -> Result<(), SvsmError> { - let mut slots = probe_mmio_slots(); - +fn initialize_virtio_mmio(_config: &SvsmConfig<'_>) -> Result<(), SvsmError> { #[cfg(feature = "block")] { use svsm::block::virtio_blk::initialize_block; + + let mut slots = probe_mmio_slots(_config); initialize_block(&mut slots)?; } @@ -435,11 +435,7 @@ fn svsm_init(launch_info: &KernelLaunchInfo) { virt_log_usage(); #[cfg(feature = "virtio-drivers")] - if config.has_fw_cfg_port() { - // Virtio cannot exist if there is no fw_cfg, so do not bother to - // attempt initialization if it is not present. - initialize_virtio_mmio().expect("Failed to initialize virtio-mmio drivers"); - } + initialize_virtio_mmio(&config).expect("Failed to initialize virtio-mmio drivers"); if let Err(e) = SVSM_PLATFORM.launch_fw(&config) { panic!("Failed to launch FW: {e:?}"); diff --git a/kernel/src/virtio/mmio.rs b/kernel/src/virtio/mmio.rs index f1cb2e9acd..11d5a0ae0f 100644 --- a/kernel/src/virtio/mmio.rs +++ b/kernel/src/virtio/mmio.rs @@ -12,6 +12,7 @@ use virtio_drivers::transport::{DeviceType, Transport, mmio::MmioTransport}; use crate::{ address::PhysAddr, + config::SvsmConfig, fw_cfg::FwCfg, mm::{GlobalRangeGuard, map_global_range_4k_shared, pagetable::PTEntryFlags}, platform::SVSM_PLATFORM, @@ -25,7 +26,7 @@ pub struct MmioSlot { pub transport: MmioTransport, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct MmioSlots { slots: Vec, } @@ -47,14 +48,18 @@ pub struct MmioSlots { /// Returns an [`MmioSlots`] collection containing all discovered virtio-MMIO devices. /// Returns an empty collection if no devices are found or if the fw_cfg interface /// is unavailable. -pub fn probe_mmio_slots() -> MmioSlots { +pub fn probe_mmio_slots(config: &SvsmConfig<'_>) -> MmioSlots { + // Virtio MMIO addresses are discovered via fw_cfg, so skip probing + // if it is not present. + if !config.has_fw_cfg_port() { + return MmioSlots::default(); + } + virtio_init(); let cfg = FwCfg::new(SVSM_PLATFORM.get_io_port()); let Ok(dev) = cfg.get_virtio_mmio_addresses() else { - return MmioSlots { - slots: Vec::::new(), - }; + return MmioSlots::default(); }; let mut slots = Vec::with_capacity(dev.len());