From e8e2705384107b6ab30ef6eb74f9db999aab6108 Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Fri, 11 Oct 2024 15:44:14 +0300 Subject: [PATCH] refactor: resolve clippy warnings --- kernel/arch/aarch64/src/lib.rs | 8 ++++++++ kernel/arch/i686/src/lib.rs | 8 ++++++++ kernel/arch/i686/src/mem/mod.rs | 13 +++++++++---- kernel/arch/interface/src/lib.rs | 2 ++ kernel/arch/interface/src/mem/mod.rs | 6 ++++++ kernel/arch/x86_64/src/lib.rs | 8 ++++++++ kernel/arch/x86_64/src/mem/table.rs | 5 +++++ kernel/driver/block/ahci/src/port.rs | 14 +++++--------- kernel/driver/fs/ext2/src/lib.rs | 6 +++--- kernel/libk/libk-util/src/lru_hash_table.rs | 9 ++++----- kernel/libk/src/vfs/block/cache.rs | 6 ++++-- kernel/src/arch/i686/exception.rs | 9 +++++++-- kernel/src/arch/i686/mod.rs | 3 ++- kernel/src/arch/x86/gdt.rs | 4 ++-- kernel/src/arch/x86/peripherals/i8259.rs | 2 +- 15 files changed, 74 insertions(+), 29 deletions(-) diff --git a/kernel/arch/aarch64/src/lib.rs b/kernel/arch/aarch64/src/lib.rs index 86c92d77..7b32ab27 100644 --- a/kernel/arch/aarch64/src/lib.rs +++ b/kernel/arch/aarch64/src/lib.rs @@ -10,6 +10,7 @@ use alloc::{boxed::Box, vec::Vec}; use device_api::interrupt::{LocalInterruptController, MessageInterruptController}; use kernel_arch_interface::{ cpu::{CpuImpl, IpiQueue}, + guard::IrqGuard, task::Scheduler, util::OneTimeInit, Architecture, @@ -71,6 +72,13 @@ impl Architecture for ArchitectureImpl { aarch64_cpu::asm::wfi(); } + fn halt() -> ! { + let _irq = IrqGuard::::acquire(); + loop { + aarch64_cpu::asm::wfi(); + } + } + unsafe fn set_local_cpu(cpu: *mut ()) { TPIDR_EL1.set(cpu as _); } diff --git a/kernel/arch/i686/src/lib.rs b/kernel/arch/i686/src/lib.rs index b0cc383d..bd1d6f3e 100644 --- a/kernel/arch/i686/src/lib.rs +++ b/kernel/arch/i686/src/lib.rs @@ -110,4 +110,12 @@ impl Architecture for ArchitectureImpl { fn idle_task() -> extern "C" fn(usize) -> ! { idle_task } + + fn halt() -> ! { + loop { + unsafe { + core::arch::asm!("cli; hlt"); + } + } + } } diff --git a/kernel/arch/i686/src/mem/mod.rs b/kernel/arch/i686/src/mem/mod.rs index ee4adc42..512518dc 100644 --- a/kernel/arch/i686/src/mem/mod.rs +++ b/kernel/arch/i686/src/mem/mod.rs @@ -5,10 +5,10 @@ use kernel_arch_interface::{ }; use libk_mm_interface::{ address::{AsPhysicalAddress, PhysicalAddress}, - table::page_count, + table::{page_count, EntryLevel}, KernelImageObject, }; -use table::{PageAttributes, PageEntry, L3}; +use table::{PageAttributes, PageEntry, L0, L3}; use yggdrasil_abi::error::Error; pub mod fixed; @@ -66,6 +66,11 @@ impl KernelTableManager for KernelTableManagerImpl { } } +/// Sets up fixed MMU translation tables. +/// +/// # Safety +/// +/// Only meant to be called once during early OS init. pub unsafe fn init_fixed_tables() { // Unmap lower stuff for (i, entry) in KERNEL_TABLES.l0.lower.iter_mut().enumerate() { @@ -75,8 +80,8 @@ pub unsafe fn init_fixed_tables() { // Map the rest of fixed translation for (i, entry) in KERNEL_TABLES.l0.kernel.iter_mut().enumerate() { - let virt = KERNEL_VIRT_OFFSET + i << 22; - let phys = (i << 22) as u32; + let virt = KERNEL_VIRT_OFFSET + (i << L0::SHIFT); + let phys = (i << L0::SHIFT) as u32; *entry = PageEntry::block(PhysicalAddress::from_u32(phys), PageAttributes::WRITABLE); flush_tlb_entry(virt); } diff --git a/kernel/arch/interface/src/lib.rs b/kernel/arch/interface/src/lib.rs index 5053c3b2..8ea101de 100644 --- a/kernel/arch/interface/src/lib.rs +++ b/kernel/arch/interface/src/lib.rs @@ -54,6 +54,8 @@ pub trait Architecture: Sized { unsafe fn set_interrupt_mask(mask: bool) -> bool; fn wait_for_interrupt(); + fn halt() -> !; + // Architectural devices fn local_interrupt_controller() -> &'static dyn LocalInterruptController { unimplemented!() diff --git a/kernel/arch/interface/src/mem/mod.rs b/kernel/arch/interface/src/mem/mod.rs index 94484b50..84f5130d 100644 --- a/kernel/arch/interface/src/mem/mod.rs +++ b/kernel/arch/interface/src/mem/mod.rs @@ -65,6 +65,12 @@ pub trait KernelTableManager: Sized + fmt::Debug { /// Only meant to be called from "safer" wrappers like [RawDeviceMemoryMapping]. unsafe fn unmap_device_pages(mapping: &RawDeviceMemoryMapping); + /// Releases a "virtualized" physical address. + /// + /// # Safety + /// + /// The address will/may become invalid after the call. + /// Caller must guarantee the address will no longer be used. #[allow(unused)] unsafe fn unmap_physical_address(virt: usize) {} } diff --git a/kernel/arch/x86_64/src/lib.rs b/kernel/arch/x86_64/src/lib.rs index 7941de4a..08fb27fd 100644 --- a/kernel/arch/x86_64/src/lib.rs +++ b/kernel/arch/x86_64/src/lib.rs @@ -155,6 +155,14 @@ impl Architecture for ArchitectureImpl { } } + fn halt() -> ! { + loop { + unsafe { + core::arch::asm!("cli; hlt"); + } + } + } + fn local_interrupt_controller() -> &'static dyn LocalInterruptController { let local = Self::local_cpu_data().unwrap(); local.local_apic diff --git a/kernel/arch/x86_64/src/mem/table.rs b/kernel/arch/x86_64/src/mem/table.rs index efa21217..fbf6c3ac 100644 --- a/kernel/arch/x86_64/src/mem/table.rs +++ b/kernel/arch/x86_64/src/mem/table.rs @@ -213,6 +213,11 @@ impl PageTable { Ok(table) } + /// Recursively clears and deallocates the translation table. + /// + /// # Safety + /// + /// The caller must ensure the table is no longer in use and is not referenced anymore. pub unsafe fn free(this: PhysicalRefMut) { let physical = this.as_physical_address(); TA::free_page_table(physical); diff --git a/kernel/driver/block/ahci/src/port.rs b/kernel/driver/block/ahci/src/port.rs index 86eb8016..6dc8bb31 100644 --- a/kernel/driver/block/ahci/src/port.rs +++ b/kernel/driver/block/ahci/src/port.rs @@ -274,15 +274,11 @@ impl AhciPort { let ci = inner.regs.CI.get(); for i in 0..MAX_COMMANDS { - if ci & (1 << i) == 0 { - if self.command_completion[i] - .1 - .swap(status.into(), Ordering::Release) - == CMD_PENDING - { - log::info!("port{}: completion on slot {}", self.index, i); - self.command_completion[i].0.wake(); - } + if ci & (1 << i) == 0 + && self.command_completion[i].1.swap(status, Ordering::Release) == CMD_PENDING + { + log::info!("port{}: completion on slot {}", self.index, i); + self.command_completion[i].0.wake(); } } diff --git a/kernel/driver/fs/ext2/src/lib.rs b/kernel/driver/fs/ext2/src/lib.rs index 8906a479..282d49c4 100644 --- a/kernel/driver/fs/ext2/src/lib.rs +++ b/kernel/driver/fs/ext2/src/lib.rs @@ -1,4 +1,5 @@ #![cfg_attr(not(test), no_std)] +#![allow(clippy::new_ret_no_self)] extern crate alloc; @@ -45,9 +46,8 @@ pub struct Ext2Fs { impl Ext2Fs { pub async fn create(device: &'static dyn BlockDevice) -> Result { - let fs = Self::create_fs(device).await.map_err(|e| { + let fs = Self::create_fs(device).await.inspect_err(|e| { log::error!("Ext2 init error: {:?}", e); - e })?; let fs = Arc::new(fs); let root = fs.load_node(data::ROOT_INODE).await?; @@ -177,7 +177,7 @@ impl Ext2Fs { core::slice::from_raw_parts(&block[0] as *const _ as *const _, self.pointers_per_block) }; - return Ok(indirect[index]); + Ok(indirect[index]) } async fn inode_block_index(&self, inode: &Inode, index: u32) -> Result { diff --git a/kernel/libk/libk-util/src/lru_hash_table.rs b/kernel/libk/libk-util/src/lru_hash_table.rs index 1432ca85..4c710f1e 100644 --- a/kernel/libk/libk-util/src/lru_hash_table.rs +++ b/kernel/libk/libk-util/src/lru_hash_table.rs @@ -1,3 +1,4 @@ +#![allow(clippy::type_complexity)] use core::{ hash::{BuildHasher, Hash}, ops::AsyncFnOnce, @@ -63,8 +64,6 @@ impl LruCacheBucket { // Safety: just checked it's not None above, so safe let (key, value) = unsafe { cursor.remove_current().unwrap_unchecked() }; - drop(cursor); - self.data.push_front((key, value)); // Safety: just pushed here, so safe @@ -138,11 +137,11 @@ impl LruCache { &mut self.buckets[h as usize % bucket_count] } - pub async fn try_get_or_insert_with_async<'a, F, E>( - &'a mut self, + pub async fn try_get_or_insert_with_async( + &mut self, key: K, supplier: F, - ) -> Result<(&'a mut V, Option<(K, V)>), E> + ) -> Result<(&mut V, Option<(K, V)>), E> where F: AsyncFnOnce() -> Result, { diff --git a/kernel/libk/src/vfs/block/cache.rs b/kernel/libk/src/vfs/block/cache.rs index e7a54c65..ba13c350 100644 --- a/kernel/libk/src/vfs/block/cache.rs +++ b/kernel/libk/src/vfs/block/cache.rs @@ -1,3 +1,4 @@ +#![allow(clippy::missing_transmute_annotations)] use core::ops::{Deref, DerefMut}; use alloc::sync::Arc; @@ -75,6 +76,7 @@ impl BlockCache { }))) } + #[allow(clippy::needless_lifetimes)] async fn entry<'a>( &'a self, address: u64, @@ -105,13 +107,13 @@ impl BlockCache { .await } - pub async fn get<'a>(&'a self, address: u64) -> Result { + pub async fn get(&self, address: u64) -> Result { self.entry(address) .await .map(|e| CachedBlockRef::new(e.deref())) } - pub async fn get_mut<'a>(&'a self, address: u64) -> Result { + pub async fn get_mut(&self, address: u64) -> Result { self.entry(address) .await .map(|mut e| CachedBlockMut::new(e.deref_mut())) diff --git a/kernel/src/arch/i686/exception.rs b/kernel/src/arch/i686/exception.rs index e2f00b37..546c1645 100644 --- a/kernel/src/arch/i686/exception.rs +++ b/kernel/src/arch/i686/exception.rs @@ -242,6 +242,11 @@ extern "C" fn __i686_syscall_handler(frame: *mut SyscallFrame) { } } +/// Initializes interrupt descriptor table with exception, interrupt and system call vectors. +/// +/// # Safety +/// +/// Only meant to be called once during OS init. pub unsafe fn init_exceptions() { extern "C" { static __i686_exception_vectors: [usize; 32]; @@ -254,8 +259,8 @@ pub unsafe fn init_exceptions() { IDT[i] = Entry::new(entry, 0x08, Entry::PRESENT | Entry::INT32); } - for i in 32..256 { - IDT[i] = Entry::new( + for entry in IDT.iter_mut().skip(32) { + *entry = Entry::new( __i686_dummy_vector as usize, 0x08, Entry::PRESENT | Entry::INT32 | Entry::DPL3, diff --git a/kernel/src/arch/i686/mod.rs b/kernel/src/arch/i686/mod.rs index 061d1481..7a84e21b 100644 --- a/kernel/src/arch/i686/mod.rs +++ b/kernel/src/arch/i686/mod.rs @@ -6,6 +6,7 @@ use device_api::{ Device, ResetDevice, }; use git_version::git_version; +use kernel_arch::{Architecture, ArchitectureImpl}; use kernel_arch_i686::{mem::table::L3, PerCpuData}; use kernel_fs::devfs::{self, CharDeviceType}; use libk::{ @@ -83,7 +84,7 @@ impl Platform for I686 { type L3 = L3; unsafe fn reset(&self) -> ! { - loop {} + ArchitectureImpl::halt(); } unsafe fn send_ipi(&self, _target: IpiDeliveryTarget, _msg: IpiMessage) -> Result<(), Error> { diff --git a/kernel/src/arch/x86/gdt.rs b/kernel/src/arch/x86/gdt.rs index 39281674..aadff7af 100644 --- a/kernel/src/arch/x86/gdt.rs +++ b/kernel/src/arch/x86/gdt.rs @@ -117,7 +117,7 @@ mod imp { pub(super) unsafe fn load_gdt(gdt: &'static [Entry]) { let gdt_addr = gdt.as_ptr().addr(); let gdtr = Pointer { - limit: (gdt.len() * size_of::()) as u16 - 1, + limit: size_of_val(gdt) as u16 - 1, offset: gdt_addr, }; @@ -253,7 +253,7 @@ mod imp { pub(super) unsafe fn load_gdt(gdt: &'static [Entry]) { let gdt_addr = gdt.as_ptr().addr(); let gdtr = Pointer { - limit: (gdt.len() * size_of::()) as u16 - 1, + limit: size_of_val(gdt) as u16 - 1, offset: gdt_addr, }; diff --git a/kernel/src/arch/x86/peripherals/i8259.rs b/kernel/src/arch/x86/peripherals/i8259.rs index a43e1b28..b1a95902 100644 --- a/kernel/src/arch/x86/peripherals/i8259.rs +++ b/kernel/src/arch/x86/peripherals/i8259.rs @@ -154,7 +154,7 @@ impl I8259 { inner.slave_data.write(ICW4_8086); // Mask everything (except IRQ#2 on master, used for cascading) - inner.master_data.write(0xFF & !(1 << 2)); + inner.master_data.write(!(1 << 2)); inner.slave_data.write(0xFF); inner.master_cmd.write(0x20);