From 89d345978572005aa3998310d40fae768fc87883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:40:04 +0100 Subject: [PATCH 1/7] fix: allow building with all features --- src/arch/riscv64/kernel/devicetree.rs | 6 +-- src/drivers/mod.rs | 16 +++--- src/drivers/net/mod.rs | 4 +- src/drivers/pci.rs | 73 +++++++++++++++++++++------ src/drivers/virtio/mod.rs | 12 +++-- src/drivers/virtio/transport/pci.rs | 15 ++++-- 6 files changed, 92 insertions(+), 34 deletions(-) diff --git a/src/arch/riscv64/kernel/devicetree.rs b/src/arch/riscv64/kernel/devicetree.rs index 09d36351b3..484baf102f 100644 --- a/src/arch/riscv64/kernel/devicetree.rs +++ b/src/arch/riscv64/kernel/devicetree.rs @@ -2,7 +2,7 @@ use core::ptr::NonNull; use fdt::Fdt; -#[cfg(all(feature = "tcp", feature = "gem-net"))] +#[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))] use memory_addresses::VirtAddr; use memory_addresses::{AddrRange, PhysAddr}; #[cfg(all(feature = "tcp", not(feature = "pci")))] @@ -15,7 +15,7 @@ use crate::arch::riscv64::kernel::interrupts::init_plic; #[cfg(all(feature = "tcp", not(feature = "pci")))] use crate::arch::riscv64::kernel::mmio::MmioDriver; use crate::arch::riscv64::mm::paging; -#[cfg(feature = "gem-net")] +#[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))] use crate::drivers::net::gem; #[cfg(all(feature = "tcp", not(feature = "pci"), not(feature = "gem-net")))] use crate::drivers::virtio::transport::mmio::{self as mmio_virtio, VirtioDriver}; @@ -104,7 +104,7 @@ pub fn init_drivers() { } // Init GEM - #[cfg(all(feature = "tcp", feature = "gem-net"))] + #[cfg(all(feature = "tcp", feature = "gem-net", not(feature = "pci")))] if let Some(gem_node) = fdt.find_compatible(&["sifive,fu540-c000-gem"]) { debug!("Found Ethernet controller"); diff --git a/src/drivers/mod.rs b/src/drivers/mod.rs index 0ce3719401..e1a07f1d79 100644 --- a/src/drivers/mod.rs +++ b/src/drivers/mod.rs @@ -32,9 +32,9 @@ pub(crate) type InterruptHandlerQueue = VecDeque; pub mod error { use core::fmt; - #[cfg(feature = "gem-net")] + #[cfg(all(target_arch = "riscv64", feature = "gem-net"))] use crate::drivers::net::gem::GEMError; - #[cfg(feature = "rtl8139")] + #[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] use crate::drivers::net::rtl8139::RTL8139Error; #[cfg(any( all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")), @@ -51,9 +51,9 @@ pub mod error { feature = "vsock" ))] InitVirtioDevFail(VirtioError), - #[cfg(feature = "rtl8139")] + #[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] InitRTL8139DevFail(RTL8139Error), - #[cfg(feature = "gem-net")] + #[cfg(all(target_arch = "riscv64", feature = "gem-net"))] InitGEMDevFail(GEMError), } @@ -68,14 +68,14 @@ pub mod error { } } - #[cfg(feature = "rtl8139")] + #[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] impl From for DriverError { fn from(err: RTL8139Error) -> Self { DriverError::InitRTL8139DevFail(err) } } - #[cfg(feature = "gem-net")] + #[cfg(all(target_arch = "riscv64", feature = "gem-net"))] impl From for DriverError { fn from(err: GEMError) -> Self { DriverError::InitGEMDevFail(err) @@ -94,11 +94,11 @@ pub mod error { DriverError::InitVirtioDevFail(ref err) => { write!(f, "Virtio driver failed: {err:?}") } - #[cfg(feature = "rtl8139")] + #[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] DriverError::InitRTL8139DevFail(ref err) => { write!(f, "RTL8139 driver failed: {err:?}") } - #[cfg(feature = "gem-net")] + #[cfg(all(target_arch = "riscv64", feature = "gem-net"))] DriverError::InitGEMDevFail(ref err) => { write!(f, "GEM driver failed: {err:?}") } diff --git a/src/drivers/net/mod.rs b/src/drivers/net/mod.rs index 0cff2854af..2a051cf658 100644 --- a/src/drivers/net/mod.rs +++ b/src/drivers/net/mod.rs @@ -1,8 +1,8 @@ #[cfg(all(target_arch = "riscv64", feature = "gem-net"))] pub mod gem; -#[cfg(feature = "rtl8139")] +#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] pub mod rtl8139; -#[cfg(not(feature = "rtl8139"))] +#[cfg(not(all(target_arch = "x86_64", feature = "rtl8139")))] pub mod virtio; use smoltcp::phy::ChecksumCapabilities; diff --git a/src/drivers/pci.rs b/src/drivers/pci.rs index 52621efba7..c5bb6d2011 100644 --- a/src/drivers/pci.rs +++ b/src/drivers/pci.rs @@ -19,20 +19,29 @@ use pci_types::{ use crate::arch::pci::PciConfigRegion; #[cfg(feature = "fuse")] use crate::drivers::fs::virtio_fs::VirtioFsDriver; -#[cfg(feature = "rtl8139")] +#[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] use crate::drivers::net::rtl8139::{self, RTL8139Driver}; -#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] +#[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") +))] use crate::drivers::net::virtio::VirtioNetDriver; #[cfg(any(feature = "tcp", feature = "udp"))] use crate::drivers::net::NetworkDriver; #[cfg(any( - all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")), + all( + any(feature = "tcp", feature = "udp"), + not(all(target_arch = "x86_64", feature = "rtl8139")) + ), feature = "fuse", feature = "vsock" ))] use crate::drivers::virtio::transport::pci as pci_virtio; #[cfg(any( - all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")), + all( + any(feature = "tcp", feature = "udp"), + not(all(target_arch = "x86_64", feature = "rtl8139")) + ), feature = "fuse", feature = "vsock" ))] @@ -321,14 +330,24 @@ pub(crate) enum PciDriver { VirtioFs(InterruptTicketMutex), #[cfg(feature = "vsock")] VirtioVsock(InterruptTicketMutex), - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] VirtioNet(InterruptTicketMutex), - #[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))] + #[cfg(all( + target_arch = "x86_64", + feature = "rtl8139", + any(feature = "tcp", feature = "udp") + ))] RTL8139Net(InterruptTicketMutex), } impl PciDriver { - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] fn get_network_driver(&self) -> Option<&InterruptTicketMutex> { #[allow(unreachable_patterns)] match self { @@ -337,7 +356,11 @@ impl PciDriver { } } - #[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))] + #[cfg(all( + target_arch = "x86_64", + feature = "rtl8139", + any(feature = "tcp", feature = "udp") + ))] fn get_network_driver(&self) -> Option<&InterruptTicketMutex> { #[allow(unreachable_patterns)] match self { @@ -379,7 +402,11 @@ impl PciDriver { (irq_number, vsock_handler) } - #[cfg(all(feature = "rtl8139", any(feature = "tcp", feature = "udp")))] + #[cfg(all( + target_arch = "x86_64", + feature = "rtl8139", + any(feature = "tcp", feature = "udp") + ))] Self::RTL8139Net(drv) => { fn rtl8139_handler() { if let Some(driver) = get_network_driver() { @@ -391,7 +418,10 @@ impl PciDriver { (irq_number, rtl8139_handler) } - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] Self::VirtioNet(drv) => { fn network_handler() { if let Some(driver) = get_network_driver() { @@ -440,7 +470,10 @@ pub(crate) fn get_interrupt_handlers() -> HashMap Option<&'static InterruptTicketMutex> { PCI_DRIVERS .get()? @@ -448,7 +481,11 @@ pub(crate) fn get_network_driver() -> Option<&'static InterruptTicketMutex Option<&'static InterruptTicketMutex> { PCI_DRIVERS .get()? @@ -485,12 +522,18 @@ pub(crate) fn init() { ); #[cfg(any( - all(any(feature = "tcp", feature = "udp"), not(feature = "rtl8139")), + all( + any(feature = "tcp", feature = "udp"), + not(all(target_arch = "x86_64", feature = "rtl8139")) + ), feature = "fuse", feature = "vsock" ))] match pci_virtio::init_device(adapter) { - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] Ok(VirtioDriver::Network(drv)) => { register_driver(PciDriver::VirtioNet(InterruptTicketMutex::new(drv))); } @@ -507,7 +550,7 @@ pub(crate) fn init() { } // Searching for Realtek RTL8139, which is supported by Qemu - #[cfg(feature = "rtl8139")] + #[cfg(all(target_arch = "x86_64", feature = "rtl8139"))] for adapter in PCI_DEVICES.finalize().iter().filter(|x| { let (vendor_id, device_id) = x.id(); vendor_id == 0x10ec && (0x8138..=0x8139).contains(&device_id) diff --git a/src/drivers/virtio/mod.rs b/src/drivers/virtio/mod.rs index 1f9e6e6d00..8e94a63f4c 100644 --- a/src/drivers/virtio/mod.rs +++ b/src/drivers/virtio/mod.rs @@ -10,7 +10,10 @@ pub mod error { #[cfg(feature = "fuse")] pub use crate::drivers::fs::virtio_fs::error::VirtioFsError; - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] pub use crate::drivers::net::virtio::error::VirtioNetError; #[cfg(feature = "pci")] use crate::drivers::pci::error::PciError; @@ -29,7 +32,10 @@ pub mod error { #[cfg(feature = "pci")] NoNotifCfg(u16), DevNotSupported(u16), - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] NetDriver(VirtioNetError), #[cfg(feature = "fuse")] FsDriver(VirtioFsError), @@ -58,7 +64,7 @@ pub mod error { #[cfg(feature = "pci")] VirtioError::NoNotifCfg(id) => write!(f, "Virtio driver failed, for device {id:x}, due to a missing or malformed notification config!"), VirtioError::DevNotSupported(id) => write!(f, "Device with id {id:#x} not supported."), - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all(not(all(target_arch = "x86_64", feature = "rtl8139")), any(feature = "tcp", feature = "udp")))] VirtioError::NetDriver(net_error) => match net_error { #[cfg(feature = "pci")] VirtioNetError::NoDevCfg(id) => write!(f, "Virtio network driver failed, for device {id:x}, due to a missing or malformed device config!"), diff --git a/src/drivers/virtio/transport/pci.rs b/src/drivers/virtio/transport/pci.rs index c9634e8d10..65439af16d 100644 --- a/src/drivers/virtio/transport/pci.rs +++ b/src/drivers/virtio/transport/pci.rs @@ -22,7 +22,10 @@ use crate::arch::pci::PciConfigRegion; use crate::drivers::error::DriverError; #[cfg(feature = "fuse")] use crate::drivers::fs::virtio_fs::VirtioFsDriver; -#[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] +#[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") +))] use crate::drivers::net::virtio::VirtioNetDriver; use crate::drivers::pci::error::PciError; use crate::drivers::pci::PciDevice; @@ -800,7 +803,10 @@ pub(crate) fn init_device( let id = virtio::Id::from(u8::try_from(device_id - 0x1040).unwrap()); match id { - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] virtio::Id::Net => match VirtioNetDriver::init(device) { Ok(virt_net_drv) => { info!("Virtio network driver initialized."); @@ -868,7 +874,10 @@ pub(crate) fn init_device( } pub(crate) enum VirtioDriver { - #[cfg(all(not(feature = "rtl8139"), any(feature = "tcp", feature = "udp")))] + #[cfg(all( + not(all(target_arch = "x86_64", feature = "rtl8139")), + any(feature = "tcp", feature = "udp") + ))] Network(VirtioNetDriver), #[cfg(feature = "vsock")] Vsock(VirtioVsockDriver), From 7c0b8b3a7da93f6b808738c7554a4ef5ac1ff856 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:28:08 +0100 Subject: [PATCH 2/7] fix(interrupts): `unused_imports` --- src/arch/x86_64/kernel/interrupts.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/arch/x86_64/kernel/interrupts.rs b/src/arch/x86_64/kernel/interrupts.rs index aa8047bf3a..4158506192 100644 --- a/src/arch/x86_64/kernel/interrupts.rs +++ b/src/arch/x86_64/kernel/interrupts.rs @@ -1,6 +1,5 @@ use alloc::collections::BTreeMap; use core::arch::asm; -use core::ptr; use core::sync::atomic::{AtomicU64, Ordering}; use ahash::RandomState; @@ -53,7 +52,7 @@ pub(crate) fn enable_and_wait() { #[cfg(not(feature = "idle-poll"))] if crate::processor::supports_mwait() { - let addr = ptr::from_ref(core_scheduler().get_priority_bitmap()).cast::(); + let addr = core::ptr::from_ref(core_scheduler().get_priority_bitmap()).cast::(); unsafe { if crate::processor::supports_clflush() { From 7e4b1ccca0cdf29e39e4f152eae5e284b50b7027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:28:56 +0100 Subject: [PATCH 3/7] fix(scheduler): `dead_code` --- src/scheduler/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scheduler/mod.rs b/src/scheduler/mod.rs index cebaea04c8..1cc50c4f5e 100644 --- a/src/scheduler/mod.rs +++ b/src/scheduler/mod.rs @@ -912,7 +912,7 @@ pub(crate) fn add_current_core() { } #[inline] -#[cfg(all(target_arch = "x86_64", feature = "smp"))] +#[cfg(all(target_arch = "x86_64", feature = "smp", not(feature = "idle-poll")))] pub(crate) fn take_core_hlt_state(core_id: CoreId) -> bool { CORE_HLT_STATE.lock()[usize::try_from(core_id).unwrap()].swap(false, Ordering::Acquire) } From 628f041e6bc3bec983e9b9e884fe86fd01739b1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:30:33 +0100 Subject: [PATCH 4/7] fix(rtl8139): `clippy::if-not-else` --- src/drivers/net/rtl8139.rs | 58 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/drivers/net/rtl8139.rs b/src/drivers/net/rtl8139.rs index 8fb6c69eb1..435f1647f2 100644 --- a/src/drivers/net/rtl8139.rs +++ b/src/drivers/net/rtl8139.rs @@ -270,38 +270,38 @@ impl NetworkDriver for RTL8139Driver { fn receive_packet(&mut self) -> Option<(RxToken, TxToken)> { let cmd = unsafe { inb(self.iobase + CR) }; - if (cmd & CR_BUFE) != CR_BUFE { - let header = self.rx_peek_u16(); - self.advance_rxpos(mem::size_of::()); + if (cmd & CR_BUFE) == CR_BUFE { + return None; + } - if header & ISR_ROK == ISR_ROK { - let length = self.rx_peek_u16() - 4; // copy packet (but not the CRC) - let pos = (self.rxpos + mem::size_of::()) % RX_BUF_LEN; - - // do we reach the end of the receive buffers? - // in this case, we contact the two slices to one vec - let vec_data = if pos + length as usize > RX_BUF_LEN { - let first = &self.rxbuffer[pos..RX_BUF_LEN]; - let second = &self.rxbuffer[..length as usize - first.len()]; - [first, second].concat() - } else { - (self.rxbuffer[pos..][..length.into()]).to_vec() - }; - - self.consume_current_buffer(); - - Some((RxToken::new(vec_data), TxToken::new())) - } else { - warn!( - "RTL8192: invalid header {:#x}, rx_pos {}\n", - header, self.rxpos - ); + let header = self.rx_peek_u16(); + self.advance_rxpos(mem::size_of::()); - None - } - } else { - None + if header & ISR_ROK != ISR_ROK { + warn!( + "RTL8192: invalid header {:#x}, rx_pos {}\n", + header, self.rxpos + ); + + return None; } + + let length = self.rx_peek_u16() - 4; // copy packet (but not the CRC) + let pos = (self.rxpos + mem::size_of::()) % RX_BUF_LEN; + + // do we reach the end of the receive buffers? + // in this case, we contact the two slices to one vec + let vec_data = if pos + length as usize > RX_BUF_LEN { + let first = &self.rxbuffer[pos..RX_BUF_LEN]; + let second = &self.rxbuffer[..length as usize - first.len()]; + [first, second].concat() + } else { + (self.rxbuffer[pos..][..length.into()]).to_vec() + }; + + self.consume_current_buffer(); + + Some((RxToken::new(vec_data), TxToken::new())) } fn set_polling_mode(&mut self, value: bool) { From 9a38118c671d75b5f8772fdc84d96a04f1119c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:31:09 +0100 Subject: [PATCH 5/7] fix: `clippy::semicolon-if-nothing-returned` --- src/drivers/net/rtl8139.rs | 2 +- src/drivers/pci.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/drivers/net/rtl8139.rs b/src/drivers/net/rtl8139.rs index 435f1647f2..222fb63c24 100644 --- a/src/drivers/net/rtl8139.rs +++ b/src/drivers/net/rtl8139.rs @@ -431,7 +431,7 @@ pub(crate) fn init_device( for i in 0..MAX_BARS { if let Some(Bar::Io { port }) = device.get_bar(i.try_into().unwrap()) { - iobase = Some(port) + iobase = Some(port); } } diff --git a/src/drivers/pci.rs b/src/drivers/pci.rs index c5bb6d2011..c625533c5d 100644 --- a/src/drivers/pci.rs +++ b/src/drivers/pci.rs @@ -561,7 +561,7 @@ pub(crate) fn init() { ); if let Ok(drv) = rtl8139::init_device(adapter) { - register_driver(PciDriver::RTL8139Net(InterruptTicketMutex::new(drv))) + register_driver(PciDriver::RTL8139Net(InterruptTicketMutex::new(drv))); } } }); From f8b0b86bf529dea1782bd7cc32a33320c105d2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:31:49 +0100 Subject: [PATCH 6/7] fix(udp): `clippy::ignored-unit-patterns` --- src/fd/socket/udp.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fd/socket/udp.rs b/src/fd/socket/udp.rs index d0df7c7937..86792c97bd 100644 --- a/src/fd/socket/udp.rs +++ b/src/fd/socket/udp.rs @@ -52,7 +52,7 @@ impl Socket { Poll::Ready( socket .send_slice(buffer, *meta) - .map(|_| buffer.len()) + .map(|()| buffer.len()) .map_err(|_| io::Error::EIO), ) } else { From 0f81173312d7e08be300b83e14d7b00e12f1ed45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Thu, 2 Jan 2025 11:41:00 +0100 Subject: [PATCH 7/7] fix(xtask): clippy all features --- xtask/src/clippy.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/xtask/src/clippy.rs b/xtask/src/clippy.rs index 72f37e5b4f..6919403d2a 100644 --- a/xtask/src/clippy.rs +++ b/xtask/src/clippy.rs @@ -24,6 +24,9 @@ impl Clippy { cmd!(sh, "cargo clippy --target={triple}") .arg("--no-default-features") .run()?; + cmd!(sh, "cargo clippy --target={triple}") + .arg("--all-features") + .run()?; cmd!(sh, "cargo clippy --target={triple}") .arg("--no-default-features") .arg("--features=tcp")