From a40f60f45b7f16b71b2a6f3e31ee0bad3fb9a39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 1 May 2024 22:30:20 +0200 Subject: [PATCH 1/4] fix(x86_64/mmio): remove redundant, inactive print statement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/x86_64/kernel/mmio.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/arch/x86_64/kernel/mmio.rs b/src/arch/x86_64/kernel/mmio.rs index 566c29cc9f..9a75836b79 100644 --- a/src/arch/x86_64/kernel/mmio.rs +++ b/src/arch/x86_64/kernel/mmio.rs @@ -182,8 +182,6 @@ fn guess_device() -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> BasePageSize::SIZE as usize, ); - //mmio.print_information(); - return Ok((mmio, IRQ_NUMBER)); } From 117b7746ccdafb62a10109646894ad17cc1d5dbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 1 May 2024 22:31:18 +0200 Subject: [PATCH 2/4] fix(x86_64/mmio): display initialization error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/x86_64/kernel/mmio.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/arch/x86_64/kernel/mmio.rs b/src/arch/x86_64/kernel/mmio.rs index 9a75836b79..db2eb6b100 100644 --- a/src/arch/x86_64/kernel/mmio.rs +++ b/src/arch/x86_64/kernel/mmio.rs @@ -221,8 +221,11 @@ pub(crate) fn init_drivers() { "Found MMIO device, but we guess the interrupt number {}!", irq ); - if let Ok(VirtioDriver::Network(drv)) = mmio_virtio::init_device(mmio, irq) { - register_driver(MmioDriver::VirtioNet(InterruptTicketMutex::new(drv))) + match mmio_virtio::init_device(mmio, irq) { + Ok(VirtioDriver::Network(drv)) => { + register_driver(MmioDriver::VirtioNet(InterruptTicketMutex::new(drv))) + } + Err(err) => error!("Could not initialize virtio-mmio device: {err}"), } } else { warn!("Unable to find mmio device"); From 8077510837b828df8cba42b51c7419b56754b9dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 1 May 2024 22:32:44 +0200 Subject: [PATCH 3/4] fix(x86_64/mmio): update memory range and IRQ number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These are valid on QEMU 5.2.0 (Debian 11) up to QEMU 9.0.0 Signed-off-by: Martin Kröning --- src/arch/x86_64/kernel/mmio.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/arch/x86_64/kernel/mmio.rs b/src/arch/x86_64/kernel/mmio.rs index db2eb6b100..30438e8338 100644 --- a/src/arch/x86_64/kernel/mmio.rs +++ b/src/arch/x86_64/kernel/mmio.rs @@ -16,9 +16,9 @@ use crate::env; pub const MAGIC_VALUE: u32 = 0x74726976; -pub const MMIO_START: usize = 0x00000000c0000000; -pub const MMIO_END: usize = 0x00000000c0000fff; -const IRQ_NUMBER: u8 = 12; +pub const MMIO_START: usize = 0x00000000feb00000; +pub const MMIO_END: usize = 0x00000000feb0ffff; +const IRQ_NUMBER: u8 = 23; static mut MMIO_DRIVERS: Vec = Vec::new(); From 681bbe05cf67d93a03d81573c444b9726a2e6189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 1 May 2024 22:55:23 +0200 Subject: [PATCH 4/4] Revert "refactor(virtio/mmio): make `device_status` an u8" This reverts commit 00b617cdb4eea8e2f529552d92c465e527f201bf. The spec says: "The driver MUST only use 32 bit wide and aligned reads and writes to access the control registers." --- src/drivers/virtio/transport/mmio.rs | 48 ++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 200d630259..fc6c062290 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -142,13 +142,16 @@ impl ComCfg { /// Returns the device status field. pub fn dev_status(&self) -> u8 { - unsafe { read_volatile(&self.com_cfg.status).bits() } + unsafe { read_volatile(&self.com_cfg.status).try_into().unwrap() } } /// Resets the device status field to zero. pub fn reset_dev(&mut self) { unsafe { - write_volatile(&mut self.com_cfg.status, DeviceStatus::empty()); + write_volatile( + &mut self.com_cfg.status, + DeviceStatus::empty().bits().into(), + ); } } @@ -157,7 +160,7 @@ impl ComCfg { /// A driver MAY use the device again after a proper reset of the device. pub fn set_failed(&mut self) { unsafe { - write_volatile(&mut self.com_cfg.status, DeviceStatus::FAILED); + write_volatile(&mut self.com_cfg.status, DeviceStatus::FAILED.bits().into()); } } @@ -165,8 +168,12 @@ impl ComCfg { /// OS has notived the device pub fn ack_dev(&mut self) { unsafe { - let status = read_volatile(&self.com_cfg.status); - write_volatile(&mut self.com_cfg.status, status | DeviceStatus::ACKNOWLEDGE); + let status = + DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); + write_volatile( + &mut self.com_cfg.status, + (status | DeviceStatus::ACKNOWLEDGE).bits().into(), + ); } } @@ -174,8 +181,12 @@ impl ComCfg { /// know how to run this device. pub fn set_drv(&mut self) { unsafe { - let status = read_volatile(&self.com_cfg.status); - write_volatile(&mut self.com_cfg.status, status | DeviceStatus::DRIVER); + let status = + DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); + write_volatile( + &mut self.com_cfg.status, + (status | DeviceStatus::DRIVER).bits().into(), + ); } } @@ -184,8 +195,12 @@ impl ComCfg { /// Drivers MUST NOT accept new features after this step. pub fn features_ok(&mut self) { unsafe { - let status = read_volatile(&self.com_cfg.status); - write_volatile(&mut self.com_cfg.status, status | DeviceStatus::FEATURES_OK); + let status = + DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); + write_volatile( + &mut self.com_cfg.status, + (status | DeviceStatus::FEATURES_OK).bits().into(), + ); } } @@ -197,7 +212,8 @@ impl ComCfg { /// otherwise, the device does not support our subset of features and the device is unusable. pub fn check_features(&self) -> bool { unsafe { - let status = read_volatile(&self.com_cfg.status); + let status = + DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); status.contains(DeviceStatus::FEATURES_OK) } } @@ -207,8 +223,12 @@ impl ComCfg { /// After this call, the device is "live"! pub fn drv_ok(&mut self) { unsafe { - let status = read_volatile(&self.com_cfg.status); - write_volatile(&mut self.com_cfg.status, status | DeviceStatus::DRIVER_OK); + let status = + DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); + write_volatile( + &mut self.com_cfg.status, + (status | DeviceStatus::DRIVER_OK).bits().into(), + ); } } @@ -428,8 +448,8 @@ pub struct MmioRegisterLayout { interrupt_ack: u32, _reserved4: [u32; 2], - status: DeviceStatus, - _reserved5: [u8; 15], + status: u32, + _reserved5: [u32; 3], queue_desc_low: u32, // non-legacy only queue_desc_high: u32, // non-legacy only