From edef28a3e742d9e692f42ee6530526242dfc6ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Mon, 27 May 2024 18:53:45 +0200 Subject: [PATCH 01/17] fix(virtio/mmio): remove `MmioRegisterLayout::get_config` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 4915dd7f4d..63ffe18ce4 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -584,24 +584,6 @@ impl MmioRegisterLayout { } } - pub fn get_config(&mut self) -> [u32; 3] { - // see Virtio specification v1.1 - 2.4.1 - unsafe { - loop { - let before = read_volatile(&self.config_generation); - fence(Ordering::SeqCst); - let config = read_volatile(&self.config); - fence(Ordering::SeqCst); - let after = read_volatile(&self.config_generation); - fence(Ordering::SeqCst); - - if before == after { - return config; - } - } - } - } - pub fn print_information(&mut self) { infoheader!(" MMIO RREGISTER LAYOUT INFORMATION "); @@ -619,7 +601,6 @@ impl MmioRegisterLayout { infoentry!("Device status", "{:#X}", unsafe { read_volatile(&self.status) }); - infoentry!("Configuration space", "{:#X?}", self.get_config()); infofooter!(); } From ba12f13bfba632937bbfa553d5fbbb237b9bc99a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Mon, 27 May 2024 22:36:46 +0200 Subject: [PATCH 02/17] refactor(virtio/mmio): reuse `ComCfg::get_max_queue_size` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 63ffe18ce4..05e706f645 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -122,7 +122,7 @@ impl ComCfg { /// /// INFO: The queue size is automatically bounded by constant `src::config:VIRTIO_MAX_QUEUE_SIZE`. pub fn select_vq(&mut self, index: u16) -> Option> { - if self.com_cfg.get_max_queue_size(u32::from(index)) == 0 { + if self.get_max_queue_size(index) == 0 { None } else { Some(VqCfgHandler { @@ -132,8 +132,11 @@ impl ComCfg { } } - pub fn get_max_queue_size(&mut self, sel: u32) -> u32 { - self.com_cfg.get_max_queue_size(sel) + pub fn get_max_queue_size(&mut self, sel: u16) -> u16 { + self.com_cfg + .get_max_queue_size(u32::from(sel)) + .try_into() + .unwrap() } pub fn get_queue_ready(&mut self, sel: u32) -> bool { From e81b638d340043e2ed12ad8e45e6b5ac5a968541 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 17:08:51 +0200 Subject: [PATCH 03/17] refactor(virtio-spec): specify access level for wide volatile pointers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/pci.rs | 3 +++ virtio-spec/src/volatile.rs | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/virtio-spec/src/pci.rs b/virtio-spec/src/pci.rs index f73d67ab23..8f9d580910 100644 --- a/virtio-spec/src/pci.rs +++ b/virtio-spec/src/pci.rs @@ -127,12 +127,15 @@ impl_wide_field_access! { /// Common configuration structure pub trait CommonCfgVolatileWideFieldAccess<'a, A>: CommonCfg { /// The driver writes the physical address of Device Area here. See section _Basic Facilities of a Virtio Device / Virtqueues_. + #[access(ReadWrite)] queue_desc: queue_desc_low, queue_desc_high; /// The driver writes the physical address of Device Area here. See section _Basic Facilities of a Virtio Device / Virtqueues_. + #[access(ReadWrite)] queue_driver: queue_driver_low, queue_driver_high; /// The driver writes the physical address of Device Area here. See section _Basic Facilities of a Virtio Device / Virtqueues_. + #[access(ReadWrite)] queue_device: queue_device_low, queue_device_high; } } diff --git a/virtio-spec/src/volatile.rs b/virtio-spec/src/volatile.rs index 025aeda6b7..f1730ac302 100644 --- a/virtio-spec/src/volatile.rs +++ b/virtio-spec/src/volatile.rs @@ -78,7 +78,8 @@ macro_rules! impl_wide_field_access { $(#[$outer:meta])* $vis:vis trait $Trait:ident<'a, A>: $T:ty { $( - $(#[$inner:meta])* + $(#[doc = $doc:literal])* + #[access($Access:ty)] $field:ident: $field_low:ident, $field_high:ident; )* } @@ -86,10 +87,10 @@ macro_rules! impl_wide_field_access { $(#[$outer])* $vis trait $Trait<'a, A> { $( - $(#[$inner])* + $(#[doc = $doc])* fn $field(self) -> WideVolatilePtr<'a, le32, A::Restricted> where - A: RestrictAccess; + A: RestrictAccess<$Access>; )* } @@ -97,7 +98,7 @@ macro_rules! impl_wide_field_access { $( fn $field(self) -> WideVolatilePtr<'a, le32, A::Restricted> where - A: RestrictAccess, + A: RestrictAccess<$Access>, { WideVolatilePtr::from_low_high(self.$field_low(), self.$field_high()) } From fa0b41c0116566943d3cdc2ea69caedc73c2ff62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 17:24:30 +0200 Subject: [PATCH 04/17] refactor(x86_64/mmio): extract `chec_ptr` 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 | 100 ++++++++++++++------------------- 1 file changed, 41 insertions(+), 59 deletions(-) diff --git a/src/arch/x86_64/kernel/mmio.rs b/src/arch/x86_64/kernel/mmio.rs index 70f8681d29..c9a721339c 100644 --- a/src/arch/x86_64/kernel/mmio.rs +++ b/src/arch/x86_64/kernel/mmio.rs @@ -36,6 +36,37 @@ impl MmioDriver { } } +unsafe fn check_ptr(ptr: *mut u8) -> Option<&'static mut MmioRegisterLayout> { + // Verify the first register value to find out if this is really an MMIO magic-value. + let mmio = unsafe { ptr.cast::().as_mut().unwrap() }; + + let magic = mmio.get_magic_value(); + let version = mmio.get_version(); + + if magic != MAGIC_VALUE { + trace!("It's not a MMIO-device at {mmio:p}"); + return None; + } + + if version != 2 { + trace!("Found a legacy device, which isn't supported"); + return None; + } + + // We found a MMIO-device (whose 512-bit address in this structure). + trace!("Found a MMIO-device at {mmio:p}"); + + // Verify the device-ID to find the network card + let id = mmio.get_device_id(); + + if id != DevId::VIRTIO_DEV_ID_NET { + trace!("It's not a network card at {mmio:p}"); + return None; + } + + Some(mmio) +} + fn check_linux_args( linux_mmio: &'static [String], ) -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> { @@ -66,37 +97,12 @@ fn check_linux_args( flags, ); - // Verify the first register value to find out if this is really an MMIO magic-value. - let mmio = unsafe { - &mut *(ptr::with_exposed_provenance_mut::( - virtual_address.as_usize() - | (current_address & (BasePageSize::SIZE as usize - 1)), - )) - }; - - let magic = mmio.get_magic_value(); - let version = mmio.get_version(); - - if magic != MAGIC_VALUE { - trace!("It's not a MMIO-device at {mmio:p}"); - continue; - } - - if version != 2 { - trace!("Found a legacy device, which isn't supported"); - continue; - } - - // We found a MMIO-device (whose 512-bit address in this structure). - trace!("Found a MMIO-device at {mmio:p}"); - - // Verify the device-ID to find the network card - let id = mmio.get_device_id(); - - if id != DevId::VIRTIO_DEV_ID_NET { - trace!("It's not a network card at {mmio:p}"); + let addr = virtual_address.as_usize() + | (current_address & (BasePageSize::SIZE as usize - 1)); + let ptr = ptr::with_exposed_provenance_mut(addr); + let Some(mmio) = (unsafe { check_ptr(ptr) }) else { continue; - } + }; crate::arch::mm::physicalmem::reserve( PhysAddr::from(current_address.align_down(BasePageSize::SIZE as usize)), @@ -144,36 +150,12 @@ fn guess_device() -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> current_page = current_address / BasePageSize::SIZE as usize; } - // Verify the first register value to find out if this is really an MMIO magic-value. - let mmio = unsafe { - &mut *(ptr::with_exposed_provenance_mut::( - virtual_address.as_usize() | (current_address & (BasePageSize::SIZE as usize - 1)), - )) - }; - - let magic = mmio.get_magic_value(); - let version = mmio.get_version(); - - if magic != MAGIC_VALUE { - trace!("It's not a MMIO-device at {mmio:p}"); + let addr = + virtual_address.as_usize() | (current_address & (BasePageSize::SIZE as usize - 1)); + let ptr = ptr::with_exposed_provenance_mut(addr); + let Some(mmio) = (unsafe { check_ptr(ptr) }) else { continue; - } - - if version != 2 { - trace!("Found a legacy device, which isn't supported"); - continue; - } - - // We found a MMIO-device (whose 512-bit address in this structure). - trace!("Found a MMIO-device at {mmio:p}"); - - // Verify the device-ID to find the network card - let id = mmio.get_device_id(); - - if id != DevId::VIRTIO_DEV_ID_NET { - trace!("It's not a network card at {mmio:p}"); - continue; - } + }; info!("Found network card at {mmio:p}"); From ec148ace4ff910b2e62acd445801629736049b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 17:46:06 +0200 Subject: [PATCH 05/17] refactor(virtio/mmio): move logic out of `MmioRegisterLayout` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 243 +++++++++++---------------- 1 file changed, 102 insertions(+), 141 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 05e706f645..648526b608 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -69,22 +69,49 @@ impl<'a> VqCfgHandler<'a> { /// /// Returns the set size in form of a `u16`. pub fn set_vq_size(&mut self, size: u16) -> u16 { - self.raw - .set_queue_size(self.vq_index, size as u32) - .try_into() - .unwrap() + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + + let num_max = read_volatile(&self.raw.queue_num_max); + + if num_max >= u32::from(size) { + write_volatile(&mut self.raw.queue_num, u32::from(size)); + size + } else { + write_volatile(&mut self.raw.queue_num, num_max); + num_max.try_into().unwrap() + } + } } pub fn set_ring_addr(&mut self, addr: PhysAddr) { - self.raw.set_ring_addr(self.vq_index, addr); + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + write_volatile(&mut self.raw.queue_desc_low, addr.as_u64() as u32); + write_volatile(&mut self.raw.queue_desc_high, (addr.as_u64() >> 32) as u32); + } } pub fn set_drv_ctrl_addr(&mut self, addr: PhysAddr) { - self.raw.set_drv_ctrl_addr(self.vq_index, addr); + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + write_volatile(&mut self.raw.queue_driver_low, addr.as_u64() as u32); + write_volatile( + &mut self.raw.queue_driver_high, + (addr.as_u64() >> 32) as u32, + ); + } } pub fn set_dev_ctrl_addr(&mut self, addr: PhysAddr) { - self.raw.set_dev_ctrl_addr(self.vq_index, addr); + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + write_volatile(&mut self.raw.queue_device_low, addr.as_u64() as u32); + write_volatile( + &mut self.raw.queue_device_high, + (addr.as_u64() >> 32) as u32, + ); + } } pub fn notif_off(&mut self) -> u16 { @@ -93,7 +120,10 @@ impl<'a> VqCfgHandler<'a> { } pub fn enable_queue(&mut self) { - self.raw.enable_queue(self.vq_index); + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + write_volatile(&mut self.raw.queue_ready, 1u32); + } } } @@ -133,14 +163,19 @@ impl ComCfg { } pub fn get_max_queue_size(&mut self, sel: u16) -> u16 { - self.com_cfg - .get_max_queue_size(u32::from(sel)) - .try_into() - .unwrap() + unsafe { + write_volatile(&mut self.com_cfg.queue_sel, u32::from(sel)); + read_volatile(&self.com_cfg.queue_num_max) + .try_into() + .unwrap() + } } pub fn get_queue_ready(&mut self, sel: u32) -> bool { - self.com_cfg.get_queue_ready(sel) + unsafe { + write_volatile(&mut self.com_cfg.queue_sel, sel); + read_volatile(&self.com_cfg.queue_ready) != 0 + } } /// Returns the device status field. @@ -237,16 +272,67 @@ impl ComCfg { /// Returns the features offered by the device. pub fn dev_features(&mut self) -> virtio_spec::F { - self.com_cfg.dev_features() + // Indicate device to show high 32 bits in device_feature field. + // See Virtio specification v1.1. - 4.1.4.3 + unsafe { + write_volatile(&mut self.com_cfg.device_features_sel, 1u32); + + // read high 32 bits of device features + let mut device_features = u64::from(read_volatile(&self.com_cfg.device_features)) << 32; + + // Indicate device to show low 32 bits in device_feature field. + // See Virtio specification v1.1. - 4.1.4.3 + write_volatile(&mut self.com_cfg.device_features_sel, 0u32); + + // read low 32 bits of device features + device_features |= u64::from(read_volatile(&self.com_cfg.device_features)); + + virtio_spec::F::from_bits_retain(u128::from(device_features).into()) + } } /// Write selected features into driver_select field. pub fn set_drv_features(&mut self, features: virtio_spec::F) { - self.com_cfg.set_drv_features(features); + let features = features.bits().to_ne() as u64; + let high: u32 = (features >> 32) as u32; + let low: u32 = features as u32; + + unsafe { + // Indicate to device that driver_features field shows low 32 bits. + // See Virtio specification v1.1. - 4.1.4.3 + write_volatile(&mut self.com_cfg.driver_features_sel, 0u32); + + // write low 32 bits of device features + write_volatile(&mut self.com_cfg.driver_features, low); + + // Indicate to device that driver_features field shows high 32 bits. + // See Virtio specification v1.1. - 4.1.4.3 + write_volatile(&mut self.com_cfg.driver_features_sel, 1u32); + + // write high 32 bits of device features + write_volatile(&mut self.com_cfg.driver_features, high); + } } pub fn print_information(&mut self) { - self.com_cfg.print_information(); + infoheader!(" MMIO RREGISTER LAYOUT INFORMATION "); + + infoentry!("Device version", "{:#X}", self.com_cfg.get_version()); + infoentry!("Device ID", "{:?}", unsafe { + read_volatile(&self.com_cfg.device_id) + }); + infoentry!("Vendor ID", "{:#X}", unsafe { + read_volatile(&self.com_cfg.vendor_id) + }); + infoentry!("Device Features", "{:#X}", self.dev_features()); + infoentry!("Interrupt status", "{:#X}", unsafe { + read_volatile(&self.com_cfg.interrupt_status) + }); + infoentry!("Device status", "{:#X}", unsafe { + read_volatile(&self.com_cfg.status) + }); + + infofooter!(); } } @@ -482,129 +568,4 @@ impl MmioRegisterLayout { pub fn get_device_id(&self) -> DevId { unsafe { read_volatile(&self.device_id) } } - - pub fn enable_queue(&mut self, sel: u32) { - unsafe { - write_volatile(&mut self.queue_sel, sel); - write_volatile(&mut self.queue_ready, 1u32); - } - } - - pub fn get_max_queue_size(&mut self, sel: u32) -> u32 { - unsafe { - write_volatile(&mut self.queue_sel, sel); - read_volatile(&self.queue_num_max) - } - } - - pub fn set_queue_size(&mut self, sel: u32, size: u32) -> u32 { - unsafe { - write_volatile(&mut self.queue_sel, sel); - - let num_max = read_volatile(&self.queue_num_max); - - if num_max >= size { - write_volatile(&mut self.queue_num, size); - size - } else { - write_volatile(&mut self.queue_num, num_max); - num_max - } - } - } - - pub fn set_ring_addr(&mut self, sel: u32, addr: PhysAddr) { - unsafe { - write_volatile(&mut self.queue_sel, sel); - write_volatile(&mut self.queue_desc_low, addr.as_u64() as u32); - write_volatile(&mut self.queue_desc_high, (addr.as_u64() >> 32) as u32); - } - } - - pub fn set_drv_ctrl_addr(&mut self, sel: u32, addr: PhysAddr) { - unsafe { - write_volatile(&mut self.queue_sel, sel); - write_volatile(&mut self.queue_driver_low, addr.as_u64() as u32); - write_volatile(&mut self.queue_driver_high, (addr.as_u64() >> 32) as u32); - } - } - - pub fn set_dev_ctrl_addr(&mut self, sel: u32, addr: PhysAddr) { - unsafe { - write_volatile(&mut self.queue_sel, sel); - write_volatile(&mut self.queue_device_low, addr.as_u64() as u32); - write_volatile(&mut self.queue_device_high, (addr.as_u64() >> 32) as u32); - } - } - - pub fn get_queue_ready(&mut self, sel: u32) -> bool { - unsafe { - write_volatile(&mut self.queue_sel, sel); - read_volatile(&self.queue_ready) != 0 - } - } - - pub fn dev_features(&mut self) -> virtio_spec::F { - // Indicate device to show high 32 bits in device_feature field. - // See Virtio specification v1.1. - 4.1.4.3 - unsafe { - write_volatile(&mut self.device_features_sel, 1u32); - - // read high 32 bits of device features - let mut device_features = u64::from(read_volatile(&self.device_features)) << 32; - - // Indicate device to show low 32 bits in device_feature field. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.device_features_sel, 0u32); - - // read low 32 bits of device features - device_features |= u64::from(read_volatile(&self.device_features)); - - virtio_spec::F::from_bits_retain(u128::from(device_features).into()) - } - } - - /// Write selected features into driver_select field. - pub fn set_drv_features(&mut self, features: virtio_spec::F) { - let features = features.bits().to_ne() as u64; - let high: u32 = (features >> 32) as u32; - let low: u32 = features as u32; - - unsafe { - // Indicate to device that driver_features field shows low 32 bits. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.driver_features_sel, 0u32); - - // write low 32 bits of device features - write_volatile(&mut self.driver_features, low); - - // Indicate to device that driver_features field shows high 32 bits. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.driver_features_sel, 1u32); - - // write high 32 bits of device features - write_volatile(&mut self.driver_features, high); - } - } - - pub fn print_information(&mut self) { - infoheader!(" MMIO RREGISTER LAYOUT INFORMATION "); - - infoentry!("Device version", "{:#X}", self.get_version()); - infoentry!("Device ID", "{:?}", unsafe { - read_volatile(&self.device_id) - }); - infoentry!("Vendor ID", "{:#X}", unsafe { - read_volatile(&self.vendor_id) - }); - infoentry!("Device Features", "{:#X}", self.dev_features()); - infoentry!("Interrupt status", "{:#X}", unsafe { - read_volatile(&self.interrupt_status) - }); - infoentry!("Device status", "{:#X}", unsafe { - read_volatile(&self.status) - }); - - infofooter!(); - } } From 6a3a416502ef6b8d39f37ad9b9bd6bcb16dc8e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 18:16:35 +0200 Subject: [PATCH 06/17] refactor(virtio/mmio): extract `VqCfgHandler::select_queue` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 648526b608..b453685d3d 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -64,14 +64,21 @@ pub struct VqCfgHandler<'a> { } impl<'a> VqCfgHandler<'a> { + // TODO: Create type for queue selected invariant to get rid of `self.select_queue()` everywhere. + fn select_queue(&mut self) { + unsafe { + write_volatile(&mut self.raw.queue_sel, self.vq_index); + } + } + /// Sets the size of a given virtqueue. In case the provided size exceeds the maximum allowed /// size, the size is set to this maximum instead. Else size is set to the provided value. /// /// Returns the set size in form of a `u16`. pub fn set_vq_size(&mut self, size: u16) -> u16 { - unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); + self.select_queue(); + unsafe { let num_max = read_volatile(&self.raw.queue_num_max); if num_max >= u32::from(size) { @@ -85,16 +92,18 @@ impl<'a> VqCfgHandler<'a> { } pub fn set_ring_addr(&mut self, addr: PhysAddr) { + self.select_queue(); + unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); write_volatile(&mut self.raw.queue_desc_low, addr.as_u64() as u32); write_volatile(&mut self.raw.queue_desc_high, (addr.as_u64() >> 32) as u32); } } pub fn set_drv_ctrl_addr(&mut self, addr: PhysAddr) { + self.select_queue(); + unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); write_volatile(&mut self.raw.queue_driver_low, addr.as_u64() as u32); write_volatile( &mut self.raw.queue_driver_high, @@ -104,8 +113,9 @@ impl<'a> VqCfgHandler<'a> { } pub fn set_dev_ctrl_addr(&mut self, addr: PhysAddr) { + self.select_queue(); + unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); write_volatile(&mut self.raw.queue_device_low, addr.as_u64() as u32); write_volatile( &mut self.raw.queue_device_high, @@ -120,8 +130,9 @@ impl<'a> VqCfgHandler<'a> { } pub fn enable_queue(&mut self) { + self.select_queue(); + unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); write_volatile(&mut self.raw.queue_ready, 1u32); } } From 9f65c106d0a1916456f941f175f05c1fad017501 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 18:23:15 +0200 Subject: [PATCH 07/17] refactor(virtio/mmio): make `VqCfgHandler.vq_index` `u16` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index b453685d3d..b31b29dfc8 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -59,7 +59,7 @@ impl From for DevId { } pub struct VqCfgHandler<'a> { - vq_index: u32, + vq_index: u16, raw: &'a mut MmioRegisterLayout, } @@ -67,7 +67,7 @@ impl<'a> VqCfgHandler<'a> { // TODO: Create type for queue selected invariant to get rid of `self.select_queue()` everywhere. fn select_queue(&mut self) { unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index); + write_volatile(&mut self.raw.queue_sel, self.vq_index.into()); } } @@ -167,7 +167,7 @@ impl ComCfg { None } else { Some(VqCfgHandler { - vq_index: index as u32, + vq_index: index, raw: self.com_cfg, }) } From fc1c16b25537db98720bd7bf00fcc7f56822fff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 18:29:11 +0200 Subject: [PATCH 08/17] refactor(virtio/mmio): inline `device_id` format argument MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index b31b29dfc8..155e530e0d 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -506,11 +506,8 @@ pub(crate) fn init_device( } } } - _ => { - error!( - "Device with id {:?} is currently not supported!", - registers.device_id - ); + device_id => { + error!("Device with id {device_id:?} is currently not supported!"); // Return Driver error inidacting device is not supported Err(DriverError::InitVirtioDevFail( VirtioError::DevNotSupported(dev_id), From 316bdf77d0aee7b45f679011b861b4efd07f4cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 19:04:59 +0200 Subject: [PATCH 09/17] refactor(virtio/mmio): simplify `VqCfgHandler::set_vq_size` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/drivers/virtio/transport/mmio.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 155e530e0d..1706c904d7 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -79,15 +79,10 @@ impl<'a> VqCfgHandler<'a> { self.select_queue(); unsafe { - let num_max = read_volatile(&self.raw.queue_num_max); - - if num_max >= u32::from(size) { - write_volatile(&mut self.raw.queue_num, u32::from(size)); - size - } else { - write_volatile(&mut self.raw.queue_num, num_max); - num_max.try_into().unwrap() - } + let num_max = u16::try_from(read_volatile(&self.raw.queue_num_max)).unwrap(); + let size = size.min(num_max); + write_volatile(&mut self.raw.queue_num, size.into()); + size } } From c51ca8cb02d45d299859337a50876c6056068097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 19:19:20 +0200 Subject: [PATCH 10/17] refactor(riscv64/devicetree): format `mmio` using `fmt::Pointer` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/riscv64/kernel/devicetree.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/arch/riscv64/kernel/devicetree.rs b/src/arch/riscv64/kernel/devicetree.rs index 45380af9da..6e51c66f05 100644 --- a/src/arch/riscv64/kernel/devicetree.rs +++ b/src/arch/riscv64/kernel/devicetree.rs @@ -191,25 +191,22 @@ pub fn init_drivers() { const MMIO_MAGIC_VALUE: u32 = 0x74726976; if magic != MMIO_MAGIC_VALUE { - error!("It's not a MMIO-device at {:#X}", mmio as *const _ as usize); + error!("It's not a MMIO-device at {mmio:p}"); } if version != 2 { warn!("Found a leagacy device, which isn't supported"); } else { // We found a MMIO-device (whose 512-bit address in this structure). - trace!("Found a MMIO-device at {:#X}", mmio as *const _ as usize); + trace!("Found a MMIO-device at {mmio:p}"); // Verify the device-ID to find the network card let id = mmio.get_device_id(); if id != DevId::VIRTIO_DEV_ID_NET { - debug!( - "It's not a network card at {:#X}", - mmio as *const _ as usize - ); + debug!("It's not a network card at {mmio:p}"); } else { - info!("Found network card at {:#X}", mmio as *const _ as usize); + info!("Found network card at {mmio:p}"); // crate::arch::mm::physicalmem::reserve( // PhysAddr::from(current_address.align_down(BasePageSize::SIZE as usize)), From 23ed52681c8878093c060b7bbea60a256c69c2e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 10:47:40 +0200 Subject: [PATCH 11/17] feat(virtio-spec): make volatile module public MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/lib.rs | 3 +-- virtio-spec/src/pci.rs | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/virtio-spec/src/lib.rs b/virtio-spec/src/lib.rs index cea4366df1..953779ec23 100644 --- a/virtio-spec/src/lib.rs +++ b/virtio-spec/src/lib.rs @@ -8,7 +8,7 @@ #[macro_use] mod bitflags; #[macro_use] -mod volatile; +pub mod volatile; mod features; pub mod net; pub mod pci; @@ -16,7 +16,6 @@ pub mod pci; pub use endian_num::{be128, be16, be32, be64, le128, le16, le32, le64}; pub use self::features::{FeatureBits, F}; -pub use self::volatile::WideVolatilePtr; pub mod fs { //! File System Device diff --git a/virtio-spec/src/pci.rs b/virtio-spec/src/pci.rs index 8f9d580910..f361bee7ff 100644 --- a/virtio-spec/src/pci.rs +++ b/virtio-spec/src/pci.rs @@ -3,7 +3,8 @@ use volatile::access::{ReadOnly, ReadWrite, RestrictAccess}; use volatile::{VolatileFieldAccess, VolatilePtr}; -use crate::{le16, le32, DeviceStatus, WideVolatilePtr}; +use crate::volatile::WideVolatilePtr; +use crate::{le16, le32, DeviceStatus}; /// Common configuration structure /// From 5cd998abd838af09768258a2df50d9be756bbabb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 11:03:22 +0200 Subject: [PATCH 12/17] feat(virtio-spec): `impl Copy for WideVolatilePtr` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/volatile.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/virtio-spec/src/volatile.rs b/virtio-spec/src/volatile.rs index f1730ac302..ea79fbcbe9 100644 --- a/virtio-spec/src/volatile.rs +++ b/virtio-spec/src/volatile.rs @@ -9,11 +9,25 @@ use crate::{be32, be64, le32, le64}; /// /// In virtio, 64-bit fields are to be treated as two 32-bit fields, with low 32 bit part followed by the high 32 bit part. /// This type mimics [`VolatilePtr`], and allows easy access to 64-bit fields. -pub struct WideVolatilePtr<'a, T, A> { +pub struct WideVolatilePtr<'a, T, A> +where + T: ?Sized, +{ low: VolatilePtr<'a, T, A>, high: VolatilePtr<'a, T, A>, } +impl<'a, T, A> Copy for WideVolatilePtr<'a, T, A> where T: ?Sized {} + +impl<'a, T, A> Clone for WideVolatilePtr<'a, T, A> +where + T: ?Sized, +{ + fn clone(&self) -> Self { + *self + } +} + impl<'a, T, A> WideVolatilePtr<'a, T, A> { /// Creates a new wide volatile pointer from pointers to the low and to the high part. pub fn from_low_high(low: VolatilePtr<'a, T, A>, high: VolatilePtr<'a, T, A>) -> Self { From 0a2df9ed0eacd3ef48ad740abb8a69f67df27c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 12:54:07 +0200 Subject: [PATCH 13/17] feat(virtio-spec): add `WideVolatilePtr::update` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/volatile.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/virtio-spec/src/volatile.rs b/virtio-spec/src/volatile.rs index ea79fbcbe9..8f671c549c 100644 --- a/virtio-spec/src/volatile.rs +++ b/virtio-spec/src/volatile.rs @@ -59,6 +59,17 @@ impl<'a, A> WideVolatilePtr<'a, le32, A> { self.low.write(low); self.high.write(high); } + + /// Updates the contained value using the given closure and volatile instructions. + /// + /// See [`VolatilePtr::update`]. + pub fn update(self, f: impl FnOnce(le64) -> le64) + where + A: Readable + Writable, + { + let new = f(self.read()); + self.write(new); + } } impl<'a, A> WideVolatilePtr<'a, be32, A> { @@ -85,6 +96,17 @@ impl<'a, A> WideVolatilePtr<'a, be32, A> { self.low.write(low); self.high.write(high); } + + /// Updates the contained value using the given closure and volatile instructions. + /// + /// See [`VolatilePtr::update`]. + pub fn update(self, f: impl FnOnce(be64) -> be64) + where + A: Readable + Writable, + { + let new = f(self.read()); + self.write(new); + } } macro_rules! impl_wide_field_access { From d4b3d4371c13d51ba345acc448bc90334216fc45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 13:19:13 +0200 Subject: [PATCH 14/17] feat(virtio-spec): add `OveralignedVolatilePtr` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/volatile.rs | 140 +++++++++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 1 deletion(-) diff --git a/virtio-spec/src/volatile.rs b/virtio-spec/src/volatile.rs index 8f671c549c..e6e13a6b27 100644 --- a/virtio-spec/src/volatile.rs +++ b/virtio-spec/src/volatile.rs @@ -1,9 +1,11 @@ //! Volatile Pointer Types. +use core::marker::PhantomData; + use volatile::access::{Readable, Writable}; use volatile::VolatilePtr; -use crate::{be32, be64, le32, le64}; +use crate::{be32, be64, le16, le32, le64, DeviceStatus}; /// A wide volatile pointer for 64-bit fields. /// @@ -142,3 +144,139 @@ macro_rules! impl_wide_field_access { } }; } + +/// An overaligned volatile pointer for fields that require wider access operations. +/// +/// In virtio, some fields require wider access operations than their type indicate, such as for [`mmio::DeviceRegisters`]. +/// +/// [`mmio::DeviceRegisters`]: crate::mmio::DeviceRegisters +pub struct OveralignedVolatilePtr<'a, T, F, A> +where + T: ?Sized, + F: ?Sized, +{ + ptr: VolatilePtr<'a, F, A>, + ty: PhantomData>, +} + +impl<'a, T, F, A> Copy for OveralignedVolatilePtr<'a, T, F, A> +where + T: ?Sized, + F: ?Sized, +{ +} + +impl<'a, T, F, A> Clone for OveralignedVolatilePtr<'a, T, F, A> +where + T: ?Sized, + F: ?Sized, +{ + fn clone(&self) -> Self { + *self + } +} + +impl<'a, T, F, A> OveralignedVolatilePtr<'a, T, F, A> +where + T: OveralignedField, + F: Copy, +{ + /// Creates a new overaligned volatile pointer. + pub fn new(ptr: VolatilePtr<'a, F, A>) -> Self { + Self { + ptr, + ty: PhantomData, + } + } + + /// Performs a volatile read of the contained value. + /// + /// See [`VolatilePtr::read`]. + pub fn read(self) -> T + where + A: Readable, + { + T::from_field(self.ptr.read()) + } + + /// Performs a volatile write, setting the contained value to the given `value`. + /// + /// See [`VolatilePtr::write`]. + pub fn write(self, value: T) + where + A: Writable, + { + self.ptr.write(value.into_field()) + } + + /// Updates the contained value using the given closure and volatile instructions. + /// + /// See [`VolatilePtr::update`]. + pub fn update(self, f: impl FnOnce(T) -> T) + where + A: Readable + Writable, + { + let new = f(self.read()); + self.write(new); + } +} + +/// A trait for fields that can be accessed via [`OveralignedVolatilePtr`]. +pub trait OveralignedField: private::Sealed { + /// Converts to this type from the overaligned field. + fn from_field(field: F) -> Self; + + /// Converts this type into the overaligned field. + fn into_field(self) -> F; +} + +impl OveralignedField for le16 { + fn from_field(field: le32) -> Self { + field.try_into().unwrap() + } + + fn into_field(self) -> le32 { + self.into() + } +} + +impl OveralignedField for bool { + fn from_field(field: le32) -> Self { + field.to_ne() == 1 + } + + fn into_field(self) -> le32 { + le32::from_ne(self as u32) + } +} + +impl OveralignedField for u8 { + fn from_field(field: le32) -> Self { + field.to_ne().try_into().unwrap() + } + + fn into_field(self) -> le32 { + le32::from_ne(self.into()) + } +} + +impl OveralignedField for DeviceStatus { + fn from_field(field: le32) -> Self { + Self::from_bits_retain(u8::from_field(field)) + } + + fn into_field(self) -> le32 { + self.bits().into_field() + } +} + +mod private { + use crate::{le16, le32, DeviceStatus}; + + pub trait Sealed {} + + impl Sealed for bool {} + impl Sealed for u8 {} + impl Sealed for le16 {} + impl Sealed for DeviceStatus {} +} From ce9cf38bc27b8174e9a8a5f77fff38c410e470f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 15:17:45 +0200 Subject: [PATCH 15/17] fix(virtio-spec): support doc alias in `impl_wide_field_access` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/volatile.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/virtio-spec/src/volatile.rs b/virtio-spec/src/volatile.rs index e6e13a6b27..d5d9757d32 100644 --- a/virtio-spec/src/volatile.rs +++ b/virtio-spec/src/volatile.rs @@ -117,6 +117,7 @@ macro_rules! impl_wide_field_access { $vis:vis trait $Trait:ident<'a, A>: $T:ty { $( $(#[doc = $doc:literal])* + $(#[doc(alias = $alias:literal)])? #[access($Access:ty)] $field:ident: $field_low:ident, $field_high:ident; )* @@ -126,6 +127,7 @@ macro_rules! impl_wide_field_access { $vis trait $Trait<'a, A> { $( $(#[doc = $doc])* + $(#[doc(alias = $alias)])? fn $field(self) -> WideVolatilePtr<'a, le32, A::Restricted> where A: RestrictAccess<$Access>; From 2669df88435ed245e9bd76bb123d7112cf8c0f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 29 May 2024 15:21:24 +0200 Subject: [PATCH 16/17] feat(virtio-spec): add `mmio::DeviceRegisters` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- virtio-spec/src/lib.rs | 1 + virtio-spec/src/mmio.rs | 530 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 531 insertions(+) create mode 100644 virtio-spec/src/mmio.rs diff --git a/virtio-spec/src/lib.rs b/virtio-spec/src/lib.rs index 953779ec23..c0704fadac 100644 --- a/virtio-spec/src/lib.rs +++ b/virtio-spec/src/lib.rs @@ -10,6 +10,7 @@ mod bitflags; #[macro_use] pub mod volatile; mod features; +pub mod mmio; pub mod net; pub mod pci; diff --git a/virtio-spec/src/mmio.rs b/virtio-spec/src/mmio.rs new file mode 100644 index 0000000000..52138f977b --- /dev/null +++ b/virtio-spec/src/mmio.rs @@ -0,0 +1,530 @@ +//! Definitions for Virtio over MMIO. + +use core::mem; + +use endian_num::{le16, le32}; +use volatile::access::{ReadOnly, ReadWrite, RestrictAccess, WriteOnly}; +use volatile::VolatilePtr; + +use crate::volatile::{OveralignedVolatilePtr, WideVolatilePtr}; +use crate::DeviceStatus; + +/// MMIO Device Registers +#[repr(transparent)] +pub struct DeviceRegisters([le32; 0x100 / mem::size_of::()]); + +macro_rules! field_fn { + ( + $(#[doc = $doc:literal])* + #[doc(alias = $alias:literal)] + #[access($Access:ty)] + $field:ident: le32, + ) => { + $(#[doc = $doc])* + #[doc(alias = $alias)] + fn $field(self) -> VolatilePtr<'a, le32, A::Restricted> + where + A: RestrictAccess<$Access>; + }; + ( + $(#[doc = $doc:literal])* + #[doc(alias = $alias:literal)] + #[access($Access:ty)] + $field:ident: $T:ty, + ) => { + $(#[doc = $doc])* + #[doc(alias = $alias)] + fn $field(self) -> OveralignedVolatilePtr<'a, $T, le32, A::Restricted> + where + A: RestrictAccess<$Access>; + }; +} + +macro_rules! field_impl { + ( + #[offset($offset:literal)] + #[access($Access:ty)] + $field:ident: le32, + ) => { + fn $field(self) -> VolatilePtr<'a, le32, A::Restricted> + where + A: RestrictAccess<$Access>, + { + unsafe { + self.map(|ptr| ptr.cast::().byte_add($offset)) + .restrict() + } + } + }; + ( + #[offset($offset:literal)] + #[access($Access:ty)] + $field:ident: $T:ty, + ) => { + fn $field(self) -> OveralignedVolatilePtr<'a, $T, le32, A::Restricted> + where + A: RestrictAccess<$Access>, + { + let ptr = unsafe { self.map(|ptr| ptr.cast::().byte_add($offset)) }; + OveralignedVolatilePtr::new(ptr.restrict()) + } + }; +} + +macro_rules! device_register_impl { + ( + $(#[doc = $outer_doc:literal])* + pub struct DeviceRegisters { + $( + $(#[doc = $doc:literal])* + #[doc(alias = $alias:literal)] + #[offset($offset:literal)] + #[access($Access:ty)] + $field:ident: $T:ident, + )* + } + ) => { + $(#[doc = $outer_doc])* + pub trait DeviceRegisterVolatileFieldAccess<'a, A> { + $( + field_fn! { + $(#[doc = $doc])* + #[doc(alias = $alias)] + #[access($Access)] + $field: $T, + } + )* + } + + impl<'a, A> DeviceRegisterVolatileFieldAccess<'a, A> for VolatilePtr<'a, DeviceRegisters, A> { + $( + field_impl! { + #[offset($offset)] + #[access($Access)] + $field: $T, + } + )* + } + }; +} + +device_register_impl! { + /// MMIO Device Registers + pub struct DeviceRegisters { + /// Magic Value + /// + /// 0x74726976 + /// (a Little Endian equivalent of the “virt” string). + #[doc(alias = "MagicValue")] + #[offset(0x000)] + #[access(ReadOnly)] + magic_value: le32, + + /// Device version number + /// + /// 0x2. + /// + ///
+ /// + /// Legacy devices (see _Virtio Transport Options / Virtio Over MMIO / Legacy interface_) used 0x1. + /// + ///
+ #[doc(alias = "Version")] + #[offset(0x004)] + #[access(ReadOnly)] + version: le32, + + /// Virtio Subsystem Device ID + /// + /// See _Device Types_ for possible values. + /// Value zero (0x0) is used to + /// define a system memory map with placeholder devices at static, + /// well known addresses, assigning functions to them depending + /// on user's needs. + #[doc(alias = "DeviceID")] + #[offset(0x008)] + #[access(ReadOnly)] + device_id: le32, + + /// Virtio Subsystem Vendor ID + #[doc(alias = "VendorID")] + #[offset(0x00c)] + #[access(ReadOnly)] + vendor_id: le32, + + /// Flags representing features the device supports + /// + /// Reading from this register returns 32 consecutive flag bits, + /// the least significant bit depending on the last value written to + /// `DeviceFeaturesSel`. Access to this register returns + /// bits `DeviceFeaturesSel`*32 to (`DeviceFeaturesSel`*32)+31, eg. + /// feature bits 0 to 31 if `DeviceFeaturesSel` is set to 0 and + /// features bits 32 to 63 if `DeviceFeaturesSel` is set to 1. + /// Also see _Basic Facilities of a Virtio Device / Feature Bits_. + #[doc(alias = "DeviceFeatures")] + #[offset(0x010)] + #[access(ReadOnly)] + device_features: le32, + + /// Device (host) features word selection. + /// + /// Writing to this register selects a set of 32 device feature bits + /// accessible by reading from `DeviceFeatures`. + #[doc(alias = "DeviceFeaturesSel")] + #[offset(0x014)] + #[access(WriteOnly)] + device_features_sel: le32, + + /// Flags representing device features understood and activated by the driver + /// + /// Writing to this register sets 32 consecutive flag bits, the least significant + /// bit depending on the last value written to `DriverFeaturesSel`. + /// Access to this register sets bits `DriverFeaturesSel`*32 + /// to (`DriverFeaturesSel`*32)+31, eg. feature bits 0 to 31 if + /// `DriverFeaturesSel` is set to 0 and features bits 32 to 63 if + /// `DriverFeaturesSel` is set to 1. Also see _Basic Facilities of a Virtio Device / Feature Bits_. + #[doc(alias = "DriverFeatures")] + #[offset(0x020)] + #[access(WriteOnly)] + driver_features: le32, + + /// Activated (guest) features word selection + /// + /// Writing to this register selects a set of 32 activated feature + /// bits accessible by writing to `DriverFeatures`. + #[doc(alias = "DriverFeaturesSel")] + #[offset(0x024)] + #[access(WriteOnly)] + driver_features_sel: le32, + + /// Virtual queue index + /// + /// Writing to this register selects the virtual queue that the + /// following operations on `QueueNumMax`, `QueueNum`, `QueueReady`, + /// `QueueDescLow`, `QueueDescHigh`, `QueueDriverlLow`, `QueueDriverHigh`, + /// `QueueDeviceLow`, `QueueDeviceHigh` and `QueueReset` apply to. The index + /// number of the first queue is zero (0x0). + #[doc(alias = "QueueSel")] + #[offset(0x030)] + #[access(WriteOnly)] + queue_sel: le16, + + /// Maximum virtual queue size + /// + /// Reading from the register returns the maximum size (number of + /// elements) of the queue the device is ready to process or + /// zero (0x0) if the queue is not available. This applies to the + /// queue selected by writing to `QueueSel`. + #[doc(alias = "QueueNumMax")] + #[offset(0x034)] + #[access(ReadOnly)] + queue_num_max: le16, + + /// Virtual queue size + /// + /// Queue size is the number of elements in the queue. + /// Writing to this register notifies the device what size of the + /// queue the driver will use. This applies to the queue selected by + /// writing to `QueueSel`. + #[doc(alias = "QueueNum")] + #[offset(0x038)] + #[access(WriteOnly)] + queue_num: le16, + + /// Virtual queue ready bit + /// + /// Writing one (0x1) to this register notifies the device that it can + /// execute requests from this virtual queue. Reading from this register + /// returns the last value written to it. Both read and write + /// accesses apply to the queue selected by writing to `QueueSel`. + #[doc(alias = "QueueReady")] + #[offset(0x044)] + #[access(ReadWrite)] + queue_ready: bool, + + /// Queue notifier + /// + /// Writing a value to this register notifies the device that + /// there are new buffers to process in a queue. + /// + /// When VIRTIO_F_NOTIFICATION_DATA has not been negotiated, + /// the value written is the queue index. + /// + /// When VIRTIO_F_NOTIFICATION_DATA has been negotiated, + /// the `Notification data` value has the following format: + /// + /// ```c + /// le32 { + /// vqn : 16; + /// next_off : 15; + /// next_wrap : 1; + /// }; + /// ``` + /// + /// See _Virtqueues / Driver notifications_ + /// for the definition of the components. + #[doc(alias = "QueueNotify")] + #[offset(0x050)] + #[access(WriteOnly)] + queue_notify: le32, + + /// Interrupt status + /// + /// Reading from this register returns a bit mask of events that + /// caused the device interrupt to be asserted. + /// The following events are possible: + /// + /// - Used Buffer Notification + /// - bit 0 - the interrupt was asserted + /// because the device has used a buffer + /// in at least one of the active virtual queues. + /// - Configuration Change Notification + /// - bit 1 - the interrupt was + /// asserted because the configuration of the device has changed. + #[doc(alias = "InterruptStatus")] + #[offset(0x060)] + #[access(ReadOnly)] + interrupt_status: le32, + + /// Interrupt acknowledge + /// + /// Writing a value with bits set as defined in `InterruptStatus` + /// to this register notifies the device that events causing + /// the interrupt have been handled. + #[doc(alias = "InterruptACK")] + #[offset(0x064)] + #[access(WriteOnly)] + interrupt_ack: le32, + + /// Device status + /// + /// Reading from this register returns the current device status + /// flags. + /// Writing non-zero values to this register sets the status flags, + /// indicating the driver progress. Writing zero (0x0) to this + /// register triggers a device reset. + /// See also p. _Virtio Transport Options / Virtio Over MMIO / MMIO-specific Initialization And Device Operation / Device Initialization_. + #[doc(alias = "Status")] + #[offset(0x070)] + #[access(ReadWrite)] + status: DeviceStatus, + + /// Virtual queue's Descriptor Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDescLow`, higher 32 bits to `QueueDescHigh`) notifies + /// the device about location of the Descriptor Area of the queue + /// selected by writing to `QueueSel` register. + #[doc(alias = "QueueDescLow")] + #[offset(0x080)] + #[access(WriteOnly)] + queue_desc_low: le32, + + /// Virtual queue's Descriptor Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDescLow`, higher 32 bits to `QueueDescHigh`) notifies + /// the device about location of the Descriptor Area of the queue + /// selected by writing to `QueueSel` register. + #[doc(alias = "QueueDescHigh")] + #[offset(0x084)] + #[access(WriteOnly)] + queue_desc_high: le32, + + /// Virtual queue's Driver Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDriverLow`, higher 32 bits to `QueueDriverHigh`) notifies + /// the device about location of the Driver Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDriverLow")] + #[offset(0x090)] + #[access(WriteOnly)] + queue_driver_low: le32, + + /// Virtual queue's Driver Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDriverLow`, higher 32 bits to `QueueDriverHigh`) notifies + /// the device about location of the Driver Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDriverHigh")] + #[offset(0x094)] + #[access(WriteOnly)] + queue_driver_high: le32, + + /// Virtual queue's Device Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDeviceLow`, higher 32 bits to `QueueDeviceHigh`) notifies + /// the device about location of the Device Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDeviceLow")] + #[offset(0x0a0)] + #[access(WriteOnly)] + queue_device_low: le32, + + /// Virtual queue's Device Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDeviceLow`, higher 32 bits to `QueueDeviceHigh`) notifies + /// the device about location of the Device Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDeviceHigh")] + #[offset(0x0a4)] + #[access(WriteOnly)] + queue_device_high: le32, + + /// Shared memory id + /// + /// Writing to this register selects the shared memory region _Basic Facilities of a Virtio Device / Shared Memory Regions_ + /// following operations on `SHMLenLow`, `SHMLenHigh`, + /// `SHMBaseLow` and `SHMBaseHigh` apply to. + #[doc(alias = "SHMSel")] + #[offset(0x0ac)] + #[access(WriteOnly)] + shm_sel: le32, + + /// Shared memory region 64 bit long length + /// + /// These registers return the length of the shared memory + /// region in bytes, as defined by the device for the region selected by + /// the `SHMSel` register. The lower 32 bits of the length + /// are read from `SHMLenLow` and the higher 32 bits from + /// `SHMLenHigh`. Reading from a non-existent + /// region (i.e. where the ID written to `SHMSel` is unused) + /// results in a length of -1. + #[doc(alias = "SHMLenLow")] + #[offset(0x0b0)] + #[access(ReadOnly)] + shm_len_low: le32, + + /// Shared memory region 64 bit long length + /// + /// These registers return the length of the shared memory + /// region in bytes, as defined by the device for the region selected by + /// the `SHMSel` register. The lower 32 bits of the length + /// are read from `SHMLenLow` and the higher 32 bits from + /// `SHMLenHigh`. Reading from a non-existent + /// region (i.e. where the ID written to `SHMSel` is unused) + /// results in a length of -1. + #[doc(alias = "SHMLenHigh")] + #[offset(0x0b4)] + #[access(ReadOnly)] + shm_len_high: le32, + + /// Shared memory region 64 bit long physical address + /// + /// The driver reads these registers to discover the base address + /// of the region in physical address space. This address is + /// chosen by the device (or other part of the VMM). + /// The lower 32 bits of the address are read from `SHMBaseLow` + /// with the higher 32 bits from `SHMBaseHigh`. Reading + /// from a non-existent region (i.e. where the ID written to + /// `SHMSel` is unused) results in a base address of + /// 0xffffffffffffffff. + #[doc(alias = "SHMBaseLow")] + #[offset(0x0b8)] + #[access(ReadOnly)] + shm_base_low: le32, + + /// Shared memory region 64 bit long physical address + /// + /// The driver reads these registers to discover the base address + /// of the region in physical address space. This address is + /// chosen by the device (or other part of the VMM). + /// The lower 32 bits of the address are read from `SHMBaseLow` + /// with the higher 32 bits from `SHMBaseHigh`. Reading + /// from a non-existent region (i.e. where the ID written to + /// `SHMSel` is unused) results in a base address of + /// 0xffffffffffffffff. + #[doc(alias = "SHMBaseHigh")] + #[offset(0x0bc)] + #[access(ReadOnly)] + shm_base_high: le32, + + /// Virtual queue reset bit + /// + /// If VIRTIO_F_RING_RESET has been negotiated, writing one (0x1) to this + /// register selectively resets the queue. Both read and write accesses + /// apply to the queue selected by writing to `QueueSel`. + #[doc(alias = "QueueReset")] + #[offset(0x0c0)] + #[access(ReadWrite)] + queue_reset: le32, + + /// Configuration atomicity value + /// + /// Reading from this register returns a value describing a version of the device-specific configuration space (see `Config`). + /// The driver can then access the configuration space and, when finished, read `ConfigGeneration` again. + /// If no part of the configuration space has changed between these two `ConfigGeneration` reads, the returned values are identical. + /// If the values are different, the configuration space accesses were not atomic and the driver has to perform the operations again. + /// See also _Basic Facilities of a Virtio Device / Device Configuration Space_. + #[doc(alias = "ConfigGeneration")] + #[offset(0x0fc)] + #[access(ReadOnly)] + config_generation: le32, + } +} + +impl_wide_field_access! { + /// MMIO Device Registers + pub trait DeviceRegisterVolatileWideFieldAccess<'a, A>: DeviceRegisters { + /// Virtual queue's Descriptor Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDescLow`, higher 32 bits to `QueueDescHigh`) notifies + /// the device about location of the Descriptor Area of the queue + /// selected by writing to `QueueSel` register. + #[doc(alias = "QueueDesc")] + #[access(WriteOnly)] + queue_desc: queue_desc_low, queue_desc_high; + + /// Virtual queue's Driver Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDriverLow`, higher 32 bits to `QueueDriverHigh`) notifies + /// the device about location of the Driver Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDriver")] + #[access(WriteOnly)] + queue_driver: queue_driver_low, queue_driver_high; + + /// Virtual queue's Device Area 64 bit long physical address + /// + /// Writing to these two registers (lower 32 bits of the address + /// to `QueueDeviceLow`, higher 32 bits to `QueueDeviceHigh`) notifies + /// the device about location of the Device Area of the queue + /// selected by writing to `QueueSel`. + #[doc(alias = "QueueDevice")] + #[access(WriteOnly)] + queue_device: queue_device_low, queue_device_high; + + /// Shared memory region 64 bit long length + /// + /// These registers return the length of the shared memory + /// region in bytes, as defined by the device for the region selected by + /// the `SHMSel` register. The lower 32 bits of the length + /// are read from `SHMLenLow` and the higher 32 bits from + /// `SHMLenHigh`. Reading from a non-existent + /// region (i.e. where the ID written to `SHMSel` is unused) + /// results in a length of -1. + #[doc(alias = "SHMLen")] + #[access(ReadOnly)] + shm_len: shm_len_low, shm_len_high; + + /// Shared memory region 64 bit long physical address + /// + /// The driver reads these registers to discover the base address + /// of the region in physical address space. This address is + /// chosen by the device (or other part of the VMM). + /// The lower 32 bits of the address are read from `SHMBaseLow` + /// with the higher 32 bits from `SHMBaseHigh`. Reading + /// from a non-existent region (i.e. where the ID written to + /// `SHMSel` is unused) results in a base address of + /// 0xffffffffffffffff. + #[doc(alias = "SHMBase")] + #[access(ReadOnly)] + shm_base: shm_base_low, shm_base_high; + } +} From edbf1dfb21fe76407e5c1c88a117a1a961dea899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Tue, 28 May 2024 19:18:49 +0200 Subject: [PATCH 17/17] refactor(virtio/mmio): migrate `MmioRegisterLayout` to `virtio-spec` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/riscv64/kernel/devicetree.rs | 20 +- src/arch/x86_64/kernel/mmio.rs | 21 +- src/drivers/net/virtio_mmio.rs | 24 +- src/drivers/virtio/transport/mmio.rs | 306 +++++++++----------------- 4 files changed, 148 insertions(+), 223 deletions(-) diff --git a/src/arch/riscv64/kernel/devicetree.rs b/src/arch/riscv64/kernel/devicetree.rs index 6e51c66f05..71b00378a6 100644 --- a/src/arch/riscv64/kernel/devicetree.rs +++ b/src/arch/riscv64/kernel/devicetree.rs @@ -1,4 +1,11 @@ +#[cfg(all(feature = "tcp", not(feature = "pci")))] +use core::ptr::NonNull; + use fdt::Fdt; +#[cfg(all(feature = "tcp", not(feature = "pci")))] +use virtio_spec::mmio::{DeviceRegisterVolatileFieldAccess, DeviceRegisters}; +#[cfg(all(feature = "tcp", not(feature = "pci")))] +use volatile::VolatileRef; #[cfg(feature = "gem-net")] use crate::arch::mm::VirtAddr; @@ -9,11 +16,11 @@ use crate::arch::riscv64::kernel::mmio::MmioDriver; use crate::arch::riscv64::mm::{paging, PhysAddr}; #[cfg(feature = "gem-net")] use crate::drivers::net::gem; +#[cfg(all(feature = "tcp", not(feature = "pci")))] +use crate::drivers::virtio::transport::mmio::DevId; #[cfg(all(feature = "tcp", not(feature = "pci"), not(feature = "gem-net")))] use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver}; #[cfg(all(feature = "tcp", not(feature = "pci")))] -use crate::drivers::virtio::transport::mmio::{DevId, MmioRegisterLayout}; -#[cfg(all(feature = "tcp", not(feature = "pci")))] use crate::kernel::mmio::register_driver; static mut PLATFORM_MODEL: Model = Model::Unknown; @@ -184,10 +191,11 @@ pub fn init_drivers() { ); // Verify the first register value to find out if this is really an MMIO magic-value. - let mmio = &mut *(virtio_region.starting_address as *mut MmioRegisterLayout); + let ptr = virtio_region.starting_address as *mut DeviceRegisters; + let mmio = VolatileRef::new(NonNull::new(ptr).unwrap()); - let magic = mmio.get_magic_value(); - let version = mmio.get_version(); + let magic = mmio.as_ptr().magic_value().read().to_ne(); + let version = mmio.as_ptr().version().read().to_ne(); const MMIO_MAGIC_VALUE: u32 = 0x74726976; if magic != MMIO_MAGIC_VALUE { @@ -201,7 +209,7 @@ pub fn init_drivers() { trace!("Found a MMIO-device at {mmio:p}"); // Verify the device-ID to find the network card - let id = mmio.get_device_id(); + let id = DevId::from(mmio.as_ptr().device_id().read().to_ne()); if id != DevId::VIRTIO_DEV_ID_NET { debug!("It's not a network card at {mmio:p}"); diff --git a/src/arch/x86_64/kernel/mmio.rs b/src/arch/x86_64/kernel/mmio.rs index c9a721339c..95329b6177 100644 --- a/src/arch/x86_64/kernel/mmio.rs +++ b/src/arch/x86_64/kernel/mmio.rs @@ -1,9 +1,12 @@ use alloc::string::String; use alloc::vec::Vec; +use core::ptr::NonNull; use core::{ptr, str}; use align_address::Align; use hermit_sync::{without_interrupts, InterruptTicketMutex}; +use virtio_spec::mmio::{DeviceRegisterVolatileFieldAccess, DeviceRegisters}; +use volatile::VolatileRef; use crate::arch::x86_64::mm::paging::{ BasePageSize, PageSize, PageTableEntryFlags, PageTableEntryFlagsExt, @@ -11,7 +14,7 @@ use crate::arch::x86_64::mm::paging::{ use crate::arch::x86_64::mm::{paging, PhysAddr}; use crate::drivers::net::virtio_net::VirtioNetDriver; use crate::drivers::virtio::transport::mmio as mmio_virtio; -use crate::drivers::virtio::transport::mmio::{DevId, MmioRegisterLayout, VirtioDriver}; +use crate::drivers::virtio::transport::mmio::{DevId, VirtioDriver}; use crate::env; pub const MAGIC_VALUE: u32 = 0x74726976; @@ -36,12 +39,12 @@ impl MmioDriver { } } -unsafe fn check_ptr(ptr: *mut u8) -> Option<&'static mut MmioRegisterLayout> { +unsafe fn check_ptr(ptr: *mut u8) -> Option> { // Verify the first register value to find out if this is really an MMIO magic-value. - let mmio = unsafe { ptr.cast::().as_mut().unwrap() }; + let mmio = unsafe { VolatileRef::new(NonNull::new(ptr.cast::()).unwrap()) }; - let magic = mmio.get_magic_value(); - let version = mmio.get_version(); + let magic = mmio.as_ptr().magic_value().read().to_ne(); + let version = mmio.as_ptr().version().read().to_ne(); if magic != MAGIC_VALUE { trace!("It's not a MMIO-device at {mmio:p}"); @@ -57,7 +60,7 @@ unsafe fn check_ptr(ptr: *mut u8) -> Option<&'static mut MmioRegisterLayout> { trace!("Found a MMIO-device at {mmio:p}"); // Verify the device-ID to find the network card - let id = mmio.get_device_id(); + let id = DevId::from(mmio.as_ptr().device_id().read().to_ne()); if id != DevId::VIRTIO_DEV_ID_NET { trace!("It's not a network card at {mmio:p}"); @@ -69,7 +72,7 @@ unsafe fn check_ptr(ptr: *mut u8) -> Option<&'static mut MmioRegisterLayout> { fn check_linux_args( linux_mmio: &'static [String], -) -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> { +) -> Result<(VolatileRef<'static, DeviceRegisters>, u8), &'static str> { let virtual_address = crate::arch::mm::virtualmem::allocate(BasePageSize::SIZE as usize).unwrap(); @@ -123,7 +126,7 @@ fn check_linux_args( Err("Network card not found!") } -fn guess_device() -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> { +fn guess_device() -> Result<(VolatileRef<'static, DeviceRegisters>, u8), &'static str> { // Trigger page mapping in the first iteration! let mut current_page = 0; let virtual_address = @@ -175,7 +178,7 @@ fn guess_device() -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> /// Tries to find the network device within the specified address range. /// Returns a reference to it within the Ok() if successful or an Err() on failure. -fn detect_network() -> Result<(&'static mut MmioRegisterLayout, u8), &'static str> { +fn detect_network() -> Result<(VolatileRef<'static, DeviceRegisters>, u8), &'static str> { let linux_mmio = env::mmio(); if !linux_mmio.is_empty() { diff --git a/src/drivers/net/virtio_mmio.rs b/src/drivers/net/virtio_mmio.rs index f99e33a7dd..674e434372 100644 --- a/src/drivers/net/virtio_mmio.rs +++ b/src/drivers/net/virtio_mmio.rs @@ -4,17 +4,18 @@ use alloc::rc::Rc; use alloc::vec::Vec; -use core::ptr; use core::ptr::read_volatile; use core::str::FromStr; use core::sync::atomic::{fence, Ordering}; use smoltcp::phy::ChecksumCapabilities; +use virtio_spec::mmio::{DeviceRegisterVolatileFieldAccess, DeviceRegisters}; +use volatile::VolatileRef; use crate::drivers::net::virtio_net::constants::Status; use crate::drivers::net::virtio_net::{CtrlQueue, NetDevCfg, RxQueues, TxQueues, VirtioNetDriver}; use crate::drivers::virtio::error::{VirtioError, VirtioNetError}; -use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, MmioRegisterLayout, NotifCfg}; +use crate::drivers::virtio::transport::mmio::{ComCfg, IsrStatus, NotifCfg}; use crate::drivers::virtio::virtqueue::Virtq; /// Virtio's network device configuration structure. @@ -110,18 +111,25 @@ impl NetDevCfgRaw { impl VirtioNetDriver { pub fn new( dev_id: u16, - registers: &'static mut MmioRegisterLayout, + mut registers: VolatileRef<'static, DeviceRegisters>, irq: u8, ) -> Result { - let dev_cfg_raw: &'static NetDevCfgRaw = - unsafe { &*(ptr::with_exposed_provenance(ptr::from_ref(registers).addr() + 0xFC)) }; + let dev_cfg_raw: &'static NetDevCfgRaw = unsafe { + &*registers + .borrow_mut() + .as_mut_ptr() + .config_generation() + .as_raw_ptr() + .cast::() + .as_ptr() + }; let dev_cfg = NetDevCfg { raw: dev_cfg_raw, dev_id, features: virtio_spec::net::F::empty(), }; - let isr_stat = IsrStatus::new(registers); - let notif_cfg = NotifCfg::new(registers); + let isr_stat = IsrStatus::new(registers.borrow_mut()); + let notif_cfg = NotifCfg::new(registers.borrow_mut()); let mtu = if let Some(my_mtu) = hermit_var!("HERMIT_MTU") { u16::from_str(&my_mtu).unwrap() @@ -159,7 +167,7 @@ impl VirtioNetDriver { /// [VirtioNetDriver](structs.virtionetdriver.html) or an [VirtioError](enums.virtioerror.html). pub fn init( dev_id: u16, - registers: &'static mut MmioRegisterLayout, + registers: VolatileRef<'static, DeviceRegisters>, irq_no: u8, ) -> Result { if let Ok(mut drv) = VirtioNetDriver::new(dev_id, registers, irq_no) { diff --git a/src/drivers/virtio/transport/mmio.rs b/src/drivers/virtio/transport/mmio.rs index 1706c904d7..9a75eeb8ad 100644 --- a/src/drivers/virtio/transport/mmio.rs +++ b/src/drivers/virtio/transport/mmio.rs @@ -3,11 +3,14 @@ //! The module contains ... #![allow(dead_code)] -use core::ptr; use core::ptr::{read_volatile, write_volatile}; use core::sync::atomic::{fence, Ordering}; +use virtio_spec::mmio::{ + DeviceRegisterVolatileFieldAccess, DeviceRegisterVolatileWideFieldAccess, DeviceRegisters, +}; use virtio_spec::DeviceStatus; +use volatile::VolatileRef; #[cfg(any(feature = "tcp", feature = "udp"))] use crate::arch::kernel::interrupts::*; @@ -60,15 +63,16 @@ impl From for DevId { pub struct VqCfgHandler<'a> { vq_index: u16, - raw: &'a mut MmioRegisterLayout, + raw: VolatileRef<'a, DeviceRegisters>, } impl<'a> VqCfgHandler<'a> { // TODO: Create type for queue selected invariant to get rid of `self.select_queue()` everywhere. fn select_queue(&mut self) { - unsafe { - write_volatile(&mut self.raw.queue_sel, self.vq_index.into()); - } + self.raw + .as_mut_ptr() + .queue_sel() + .write(self.vq_index.into()) } /// Sets the size of a given virtqueue. In case the provided size exceeds the maximum allowed @@ -77,46 +81,30 @@ impl<'a> VqCfgHandler<'a> { /// Returns the set size in form of a `u16`. pub fn set_vq_size(&mut self, size: u16) -> u16 { self.select_queue(); + let ptr = self.raw.as_mut_ptr(); - unsafe { - let num_max = u16::try_from(read_volatile(&self.raw.queue_num_max)).unwrap(); - let size = size.min(num_max); - write_volatile(&mut self.raw.queue_num, size.into()); - size - } + let num_max = ptr.queue_num_max().read().to_ne(); + let size = size.min(num_max); + ptr.queue_num().write(size.into()); + size } pub fn set_ring_addr(&mut self, addr: PhysAddr) { self.select_queue(); - unsafe { - write_volatile(&mut self.raw.queue_desc_low, addr.as_u64() as u32); - write_volatile(&mut self.raw.queue_desc_high, (addr.as_u64() >> 32) as u32); - } + self.raw.as_mut_ptr().queue_desc().write(addr.0.into()); } pub fn set_drv_ctrl_addr(&mut self, addr: PhysAddr) { self.select_queue(); - unsafe { - write_volatile(&mut self.raw.queue_driver_low, addr.as_u64() as u32); - write_volatile( - &mut self.raw.queue_driver_high, - (addr.as_u64() >> 32) as u32, - ); - } + self.raw.as_mut_ptr().queue_driver().write(addr.0.into()); } pub fn set_dev_ctrl_addr(&mut self, addr: PhysAddr) { self.select_queue(); - unsafe { - write_volatile(&mut self.raw.queue_device_low, addr.as_u64() as u32); - write_volatile( - &mut self.raw.queue_device_high, - (addr.as_u64() >> 32) as u32, - ); - } + self.raw.as_mut_ptr().queue_device().write(addr.0.into()); } pub fn notif_off(&mut self) -> u16 { @@ -127,9 +115,7 @@ impl<'a> VqCfgHandler<'a> { pub fn enable_queue(&mut self) { self.select_queue(); - unsafe { - write_volatile(&mut self.raw.queue_ready, 1u32); - } + self.raw.as_mut_ptr().queue_ready().write(true); } } @@ -139,9 +125,8 @@ impl<'a> VqCfgHandler<'a> { /// Provides a safe API for the raw structure and allows interaction with the device via /// the structure. pub struct ComCfg { - // References the raw structure in PCI memory space. Is static as - // long as the device is present, which is mandatory in order to let this code work. - com_cfg: &'static mut MmioRegisterLayout, + // FIXME: remove 'static lifetime + com_cfg: VolatileRef<'static, DeviceRegisters>, /// Preferences of the device for this config. From 1 (highest) to 2^7-1 (lowest) rank: u8, @@ -149,7 +134,7 @@ pub struct ComCfg { // Public Interface of ComCfg impl ComCfg { - pub fn new(raw: &'static mut MmioRegisterLayout, rank: u8) -> Self { + pub fn new(raw: VolatileRef<'static, DeviceRegisters>, rank: u8) -> Self { ComCfg { com_cfg: raw, rank } } @@ -163,89 +148,72 @@ impl ComCfg { } else { Some(VqCfgHandler { vq_index: index, - raw: self.com_cfg, + raw: self.com_cfg.borrow_mut(), }) } } pub fn get_max_queue_size(&mut self, sel: u16) -> u16 { - unsafe { - write_volatile(&mut self.com_cfg.queue_sel, u32::from(sel)); - read_volatile(&self.com_cfg.queue_num_max) - .try_into() - .unwrap() - } + let ptr = self.com_cfg.as_mut_ptr(); + ptr.queue_sel().write(sel.into()); + ptr.queue_num_max().read().to_ne() } - pub fn get_queue_ready(&mut self, sel: u32) -> bool { - unsafe { - write_volatile(&mut self.com_cfg.queue_sel, sel); - read_volatile(&self.com_cfg.queue_ready) != 0 - } + pub fn get_queue_ready(&mut self, sel: u16) -> bool { + let ptr = self.com_cfg.as_mut_ptr(); + ptr.queue_sel().write(sel.into()); + ptr.queue_ready().read() } /// Returns the device status field. pub fn dev_status(&self) -> u8 { - unsafe { read_volatile(&self.com_cfg.status).try_into().unwrap() } + self.com_cfg.as_ptr().status().read().bits() } /// Resets the device status field to zero. pub fn reset_dev(&mut self) { - unsafe { - write_volatile( - &mut self.com_cfg.status, - DeviceStatus::empty().bits().into(), - ); - } + self.com_cfg + .as_mut_ptr() + .status() + .write(DeviceStatus::empty()); } /// Sets the device status field to FAILED. /// A driver MUST NOT initialize and use the device any further after this. /// 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.bits().into()); - } + self.com_cfg + .as_mut_ptr() + .status() + .write(DeviceStatus::FAILED); } /// Sets the ACKNOWLEDGE bit in the device status field. This indicates, the /// OS has notived the device pub fn ack_dev(&mut self) { - unsafe { - 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(), - ); - } + self.com_cfg + .as_mut_ptr() + .status() + .update(|status| status | DeviceStatus::ACKNOWLEDGE); } /// Sets the DRIVER bit in the device status field. This indicates, the OS /// know how to run this device. pub fn set_drv(&mut self) { - unsafe { - 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(), - ); - } + self.com_cfg + .as_mut_ptr() + .status() + .update(|status| status | DeviceStatus::DRIVER); } /// Sets the FEATURES_OK bit in the device status field. /// /// Drivers MUST NOT accept new features after this step. pub fn features_ok(&mut self) { - unsafe { - 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(), - ); - } + self.com_cfg + .as_mut_ptr() + .status() + .update(|status| status | DeviceStatus::FEATURES_OK); } /// In order to correctly check feature negotiaten, this function @@ -255,88 +223,79 @@ impl ComCfg { /// Re-reads device status to ensure the FEATURES_OK bit is still set: /// otherwise, the device does not support our subset of features and the device is unusable. pub fn check_features(&self) -> bool { - unsafe { - let status = - DeviceStatus::from_bits(read_volatile(&self.com_cfg.status) as u8).unwrap(); - status.contains(DeviceStatus::FEATURES_OK) - } + self.com_cfg + .as_ptr() + .status() + .read() + .contains(DeviceStatus::FEATURES_OK) } /// Sets the DRIVER_OK bit in the device status field. /// /// After this call, the device is "live"! pub fn drv_ok(&mut self) { - unsafe { - 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(), - ); - } + self.com_cfg + .as_mut_ptr() + .status() + .update(|status| status | DeviceStatus::DRIVER_OK); } /// Returns the features offered by the device. pub fn dev_features(&mut self) -> virtio_spec::F { + let ptr = self.com_cfg.as_mut_ptr(); + // Indicate device to show high 32 bits in device_feature field. // See Virtio specification v1.1. - 4.1.4.3 - unsafe { - write_volatile(&mut self.com_cfg.device_features_sel, 1u32); + ptr.device_features_sel().write(1.into()); - // read high 32 bits of device features - let mut device_features = u64::from(read_volatile(&self.com_cfg.device_features)) << 32; + // read high 32 bits of device features + let mut device_features = u64::from(ptr.device_features().read().to_ne()) << 32; - // Indicate device to show low 32 bits in device_feature field. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.com_cfg.device_features_sel, 0u32); + // Indicate device to show low 32 bits in device_feature field. + // See Virtio specification v1.1. - 4.1.4.3 + ptr.device_features_sel().write(0.into()); - // read low 32 bits of device features - device_features |= u64::from(read_volatile(&self.com_cfg.device_features)); + // read low 32 bits of device features + device_features |= u64::from(ptr.device_features().read().to_ne()); - virtio_spec::F::from_bits_retain(u128::from(device_features).into()) - } + virtio_spec::F::from_bits_retain(u128::from(device_features).into()) } /// Write selected features into driver_select field. pub fn set_drv_features(&mut self, features: virtio_spec::F) { + let ptr = self.com_cfg.as_mut_ptr(); + let features = features.bits().to_ne() as u64; let high: u32 = (features >> 32) as u32; let low: u32 = features as u32; - unsafe { - // Indicate to device that driver_features field shows low 32 bits. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.com_cfg.driver_features_sel, 0u32); + // Indicate to device that driver_features field shows low 32 bits. + // See Virtio specification v1.1. - 4.1.4.3 + ptr.driver_features_sel().write(0.into()); - // write low 32 bits of device features - write_volatile(&mut self.com_cfg.driver_features, low); + // write low 32 bits of device features + ptr.driver_features().write(low.into()); - // Indicate to device that driver_features field shows high 32 bits. - // See Virtio specification v1.1. - 4.1.4.3 - write_volatile(&mut self.com_cfg.driver_features_sel, 1u32); + // Indicate to device that driver_features field shows high 32 bits. + // See Virtio specification v1.1. - 4.1.4.3 + ptr.driver_features_sel().write(1.into()); - // write high 32 bits of device features - write_volatile(&mut self.com_cfg.driver_features, high); - } + // write high 32 bits of device features + ptr.driver_features().write(high.into()); } pub fn print_information(&mut self) { + let ptr = self.com_cfg.as_ptr(); + infoheader!(" MMIO RREGISTER LAYOUT INFORMATION "); - infoentry!("Device version", "{:#X}", self.com_cfg.get_version()); - infoentry!("Device ID", "{:?}", unsafe { - read_volatile(&self.com_cfg.device_id) - }); - infoentry!("Vendor ID", "{:#X}", unsafe { - read_volatile(&self.com_cfg.vendor_id) - }); + infoentry!("Device version", "{:#X}", ptr.version().read()); + infoentry!("Device ID", "{:?}", ptr.device_id().read()); + infoentry!("Vendor ID", "{:#X}", ptr.vendor_id().read()); infoentry!("Device Features", "{:#X}", self.dev_features()); - infoentry!("Interrupt status", "{:#X}", unsafe { - read_volatile(&self.com_cfg.interrupt_status) - }); - infoentry!("Device status", "{:#X}", unsafe { - read_volatile(&self.com_cfg.status) - }); + let ptr = self.com_cfg.as_ptr(); + infoentry!("Interrupt status", "{:#X}", ptr.interrupt_status().read()); + infoentry!("Device status", "{:#X}", ptr.status().read()); infofooter!(); } @@ -350,8 +309,13 @@ pub struct NotifCfg { } impl NotifCfg { - pub fn new(registers: &mut MmioRegisterLayout) -> Self { - let raw = ptr::from_mut(&mut registers.queue_notify); + pub fn new(mut registers: VolatileRef<'_, DeviceRegisters>) -> Self { + let raw = registers + .as_mut_ptr() + .queue_notify() + .as_raw_ptr() + .as_ptr() + .cast(); NotifCfg { queue_notify: raw } } @@ -427,8 +391,12 @@ pub struct IsrStatus { } impl IsrStatus { - pub fn new(registers: &mut MmioRegisterLayout) -> Self { - let ptr = ptr::from_mut(&mut registers.interrupt_status); + pub fn new(mut registers: VolatileRef<'_, DeviceRegisters>) -> Self { + let ptr = registers + .as_mut_ptr() + .interrupt_status() + .as_raw_ptr() + .as_ptr(); let raw: &'static mut IsrStatusRaw = unsafe { &mut *(ptr as *mut IsrStatusRaw) }; IsrStatus { raw } @@ -469,12 +437,12 @@ pub(crate) enum VirtioDriver { #[allow(unused_variables)] pub(crate) fn init_device( - registers: &'static mut MmioRegisterLayout, + registers: VolatileRef<'static, DeviceRegisters>, irq_no: u8, ) -> Result { let dev_id: u16 = 0; - if registers.version == 0x1 { + if registers.as_ptr().version().read().to_ne() == 0x1 { error!("Legacy interface isn't supported!"); return Err(DriverError::InitVirtioDevFail( VirtioError::DevNotSupported(dev_id), @@ -482,7 +450,7 @@ pub(crate) fn init_device( } // Verify the device-ID to find the network card - match registers.device_id { + match registers.as_ptr().device_id().read().to_ne().into() { #[cfg(any(feature = "tcp", feature = "udp"))] DevId::VIRTIO_DEV_ID_NET => { match VirtioNetDriver::init(dev_id, registers, irq_no) { @@ -510,65 +478,3 @@ pub(crate) fn init_device( } } } - -/// The Layout of MMIO Device -#[repr(C, align(4))] -pub struct MmioRegisterLayout { - magic_value: u32, - version: u32, - device_id: DevId, - vendor_id: u32, - - device_features: u32, - device_features_sel: u32, - _reserved0: [u32; 2], - driver_features: u32, - driver_features_sel: u32, - - guest_page_size: u32, // legacy only - _reserved1: u32, - - queue_sel: u32, - queue_num_max: u32, - queue_num: u32, - queue_align: u32, // legacy only - queue_pfn: u32, // legacy only - queue_ready: u32, // non-legacy only - _reserved2: [u32; 2], - queue_notify: u32, - _reserved3: [u32; 3], - - interrupt_status: u32, - interrupt_ack: u32, - _reserved4: [u32; 2], - - status: u32, - _reserved5: [u32; 3], - - queue_desc_low: u32, // non-legacy only - queue_desc_high: u32, // non-legacy only - _reserved6: [u32; 2], - queue_driver_low: u32, // non-legacy only - queue_driver_high: u32, // non-legacy only - _reserved7: [u32; 2], - queue_device_low: u32, // non-legacy only - queue_device_high: u32, // non-legacy only - _reserved8: [u32; 21], - - config_generation: u32, // non-legacy only - config: [u32; 3], -} - -impl MmioRegisterLayout { - pub fn get_magic_value(&self) -> u32 { - unsafe { read_volatile(&self.magic_value) } - } - - pub fn get_version(&self) -> u32 { - unsafe { read_volatile(&self.version) } - } - - pub fn get_device_id(&self) -> DevId { - unsafe { read_volatile(&self.device_id) } - } -}