From 82fe14960fdac65268d1c5e9c5b01a9ff029da76 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Wed, 8 Nov 2023 09:54:27 -0700 Subject: [PATCH] Fix build on x86 --- virtio-core/src/arch/x86.rs | 46 +++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/virtio-core/src/arch/x86.rs b/virtio-core/src/arch/x86.rs index 9806acea4f..08483e9346 100644 --- a/virtio-core/src/arch/x86.rs +++ b/virtio-core/src/arch/x86.rs @@ -1,3 +1,14 @@ +use crate::{ + legacy_transport::LegacyTransport, + reinit, + transport::Error, + utils::VolatileCell, + Device, +}; +use std::fs::File; + +use pcid_interface::*; + pub fn enable_msix(pcid_handle: &mut PcidServerHandle) -> Result { panic!("virtio-core: x86 doesn't support enable_msix") } @@ -6,5 +17,36 @@ pub fn probe_legacy_port_transport<'a>( pci_header: &PciHeader, pcid_handle: &mut PcidServerHandle, ) -> Result, Error> { - crate::x86_64::probe_legacy_port_transport(pci_header, pcid_handle) -} \ No newline at end of file + if let PciBar::Port(port) = pci_header.get_bar(0) { + unsafe { syscall::iopl(3).expect("virtio: failed to set I/O privilege level") }; + log::warn!("virtio: using legacy transport"); + + static SHIM: VolatileCell = VolatileCell::new(0); + + let transport = LegacyTransport::new(port); + + // Setup interrupts. + let all_pci_features = pcid_handle.fetch_all_features()?; + let has_msix = all_pci_features + .iter() + .any(|(feature, _)| feature.is_msix()); + + // According to the virtio specification, the device REQUIRED to support MSI-X. + assert!(has_msix, "virtio: device does not support MSI-X"); + let irq_handle = enable_msix(pcid_handle)?; + + let device = Device { + transport, + irq_handle, + isr: &SHIM, + device_space: core::ptr::null_mut(), + }; + + device.transport.reset(); + reinit(&device)?; + + Ok(device) + } else { + unreachable!("virtio: legacy transport with non-port IO?") + } +}