Browse Source

fix(virtio):修复了特定virtio设备环境下中断号重复错误,以及开机内核panic的bug (#881)

曾俊 6 months ago
parent
commit
886ce28516
2 changed files with 15 additions and 4 deletions
  1. 6 2
      kernel/src/driver/virtio/transport_pci.rs
  2. 9 2
      tools/run-qemu.sh

+ 6 - 2
kernel/src/driver/virtio/transport_pci.rs

@@ -115,6 +115,7 @@ impl PciTransport {
     /// - `device` - The PCI device structure for the VirtIO device.
     /// - `irq_handler` - An optional handler for the device's interrupt. If `None`, a default
     ///     handler `DefaultVirtioIrqHandler` will be used.
+    /// - `irq_number_offset` - Currently, this parameter is just simple make a offset to the irq number, cause it's not be allowed to have the same irq number within different device
     #[allow(clippy::extra_unused_type_parameters)]
     pub fn new<H: Hal>(
         device: &mut PciDeviceStructureGeneralDevice,
@@ -140,8 +141,8 @@ impl PciTransport {
         let irq_vector = standard_device.irq_vector_mut().unwrap();
         irq_vector.push(irq);
         standard_device
-            .irq_init(IRQ::PCI_IRQ_MSIX)
-            .expect("IRQ init failed");
+            .irq_init(IRQ::PCI_IRQ_MSIX | IRQ::PCI_IRQ_MSI)
+            .ok_or(VirtioPciError::UnableToInitIrq)?;
         // 中断相关信息
         let msg = PciIrqMsg {
             irq_common_message: IrqCommonMsg::init_from(
@@ -445,6 +446,8 @@ pub enum VirtioPciError {
     /// `VIRTIO_PCI_CAP_NOTIFY_CFG` capability has a `notify_off_multiplier` that is not a multiple
     /// of 2.
     InvalidNotifyOffMultiplier(u32),
+    /// Unable to find capability such as MSIX or MSI.
+    UnableToInitIrq,
     /// No valid `VIRTIO_PCI_CAP_ISR_CFG` capability was found.
     MissingIsrConfig,
     /// An IO BAR was provided rather than a memory BAR.
@@ -474,6 +477,7 @@ impl Display for VirtioPciError {
                 "PCI device vender ID {:#06x} was not the VirtIO vendor ID {:#06x}.",
                 vendor_id, VIRTIO_VENDOR_ID
             ),
+            Self::UnableToInitIrq => write!(f, "Unable to find capability such as MSIX or MSI."),
             Self::MissingCommonConfig => write!(
                 f,
                 "No valid `VIRTIO_PCI_CAP_COMMON_CFG` capability was found."

+ 9 - 2
tools/run-qemu.sh

@@ -77,7 +77,8 @@ QEMU_DRIVE="id=disk,file=${QEMU_DISK_IMAGE},if=none"
 QEMU_ACCELARATE=""
 QEMU_ARGUMENT=""
 QEMU_DEVICES=""
-
+#这个变量为true则使用virtio磁盘
+VIRTIO_BLK_DEVICE=false
 # 如果qemu_accel不为空
 if [ -n "${qemu_accel}" ]; then
     QEMU_ACCELARATE="-machine accel=${qemu_accel} -enable-kvm "
@@ -87,7 +88,13 @@ if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
     QEMU_MACHINE=" -machine q35,memory-backend=${QEMU_MEMORY_BACKEND} "
     QEMU_CPU_FEATURES+="-cpu IvyBridge,apic,x2apic,+fpu,check,+vmx,${allflags}"
     QEMU_RTC_CLOCK+=" -rtc clock=host,base=localtime"
-    QEMU_DEVICES_DISK="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 "
+    if [ ${VIRTIO_BLK_DEVICE} == false ]; then
+      QEMU_DEVICES_DISK+="-device ahci,id=ahci -device ide-hd,drive=disk,bus=ahci.0 "
+    else
+      QEMU_DEVICES_DISK="-device virtio-blk-pci,drive=disk -device pci-bridge,chassis_nr=1,id=pci.1 -device pcie-root-port "
+    fi
+    
+    
 else
     QEMU_MACHINE=" -machine virt,memory-backend=${QEMU_MEMORY_BACKEND} -cpu sifive-u54 "
     QEMU_DEVICES_DISK="-device virtio-blk-device,drive=disk "