From 72db5f424de9c3a2722ded05fb9e3d7ba836ded2 Mon Sep 17 00:00:00 2001 From: Martin Schmidt Date: Fri, 1 Dec 2023 18:37:34 +0100 Subject: [PATCH] cleanup: apply clippy fixes --- hal_aarch64/src/irq.rs | 9 ++++---- hal_aarch64/src/mm/pgt48.rs | 2 +- hal_core/src/mm.rs | 1 - hal_riscv64/src/irq.rs | 7 +++--- hal_riscv64/src/lib.rs | 4 +--- hal_riscv64/src/mm/mod.rs | 2 +- hal_riscv64/src/mm/sv39.rs | 6 +---- kernel/src/device_tree.rs | 4 +--- kernel/src/drivers/mod.rs | 5 +---- kernel/src/error.rs | 2 -- kernel/src/executable/elf.rs | 9 +++----- kernel/src/generic_main.rs | 2 +- kernel/src/globals.rs | 2 -- kernel/src/lib.rs | 1 - kernel/src/lock.rs | 27 ----------------------- kernel/src/mm/binary_buddy_allocator.rs | 2 +- kernel/src/mm/mod.rs | 21 +++++++++--------- kernel/src/mm/physical_memory_manager.rs | 28 ++++++++---------------- 18 files changed, 38 insertions(+), 96 deletions(-) delete mode 100644 kernel/src/lock.rs diff --git a/hal_aarch64/src/irq.rs b/hal_aarch64/src/irq.rs index 412024f8..10a99a17 100644 --- a/hal_aarch64/src/irq.rs +++ b/hal_aarch64/src/irq.rs @@ -103,11 +103,12 @@ extern "C" fn irq_current_el_sp0() { // Clear the timer in order to EOI it. cpu::clear_physical_timer(); - let timer_ptr = TIMER_CALLBACK.load(Ordering::Relaxed); - if !timer_ptr.is_null() { + let timer_cb = TIMER_CALLBACK.load(Ordering::Relaxed); + if !timer_cb.is_null() { unsafe { - let timer: fn() = core::mem::transmute::<_, fn()>(timer_ptr); - timer(); + // Cannot simply dereference TIMER_CALLBACK here. + // We are using an AtomicPtr and TIMER_CALLBACK already holds the fn(). + core::mem::transmute::<_, fn()>(timer_cb)(); } } diff --git a/hal_aarch64/src/mm/pgt48.rs b/hal_aarch64/src/mm/pgt48.rs index ae94d892..eb437297 100644 --- a/hal_aarch64/src/mm/pgt48.rs +++ b/hal_aarch64/src/mm/pgt48.rs @@ -226,7 +226,7 @@ impl PageMap for PageTable { fn new(allocator: &impl PageAlloc) -> Result<&'static mut Self, Error> { let page = allocator.alloc(1)?; - let page_table = unsafe { page as *mut PageTable }; + let page_table = page as *mut PageTable; // Safety: the PMM gave us the memory, it should be a valid pointer. let page_table: &mut PageTable = unsafe { page_table.as_mut().unwrap() }; diff --git a/hal_core/src/mm.rs b/hal_core/src/mm.rs index 3d9d8760..f8cb6d8c 100644 --- a/hal_core/src/mm.rs +++ b/hal_core/src/mm.rs @@ -179,7 +179,6 @@ pub fn prefill_pagetable( ) -> Result<&'static mut P, Error> { trace!("hal_core::mm::prefill_pagetable"); let pt: &'static mut P = P::new(allocator)?; - let page_size = P::PAGE_SIZE; for range in pre_allocated { pt.add_invalid_entries(range, allocator)?; diff --git a/hal_riscv64/src/irq.rs b/hal_riscv64/src/irq.rs index 37a6dc99..9e2a4e66 100644 --- a/hal_riscv64/src/irq.rs +++ b/hal_riscv64/src/irq.rs @@ -202,11 +202,10 @@ extern "C" fn undefined_handler() { } extern "C" fn timer_handler() { - let timer_ptr = TIMER_CALLBACK.load(Ordering::Relaxed); - if !timer_ptr.is_null() { + let timer_cb = TIMER_CALLBACK.load(Ordering::Relaxed); + if !timer_cb.is_null() { unsafe { - let timer: fn() = core::mem::transmute::<_, fn()>(timer_ptr); - timer(); + core::mem::transmute::<_, fn()>(timer_cb)(); } } } diff --git a/hal_riscv64/src/lib.rs b/hal_riscv64/src/lib.rs index 84dfa306..b83db9c7 100644 --- a/hal_riscv64/src/lib.rs +++ b/hal_riscv64/src/lib.rs @@ -10,9 +10,7 @@ mod registers; use core::arch::asm; -pub fn panic_info() -> () { - () -} +pub fn panic_info() {} #[naked] #[no_mangle] diff --git a/hal_riscv64/src/mm/mod.rs b/hal_riscv64/src/mm/mod.rs index dee6d330..0da77294 100644 --- a/hal_riscv64/src/mm/mod.rs +++ b/hal_riscv64/src/mm/mod.rs @@ -13,7 +13,7 @@ pub const PAGE_SIZE: usize = PageTable::PAGE_SIZE; static mut GPT: OnceCell<&'static mut PageTable> = OnceCell::new(); pub fn current() -> &'static mut PageTable { - unsafe { *GPT.get_mut().unwrap() } + unsafe { GPT.get_mut().unwrap() } } pub fn prefill_pagetable( diff --git a/hal_riscv64/src/mm/sv39.rs b/hal_riscv64/src/mm/sv39.rs index f6cb9a2f..65aaeb5b 100644 --- a/hal_riscv64/src/mm/sv39.rs +++ b/hal_riscv64/src/mm/sv39.rs @@ -115,10 +115,6 @@ pub struct PageTableEntry { } impl PageTableEntry { - fn clear(&mut self) { - *self = PageTableEntry::from_bytes([0u8; 8]) - } - fn is_valid(&self) -> bool { self.v() == 1 } @@ -169,7 +165,7 @@ impl PageMap for PageTable { fn new(allocator: &impl PageAlloc) -> Result<&'static mut Self, Error> { let page = allocator.alloc(1)?; - let page_table = unsafe { page as *mut PageTable }; + let page_table = page as *mut PageTable; // Safety: the PMM gave us the memory, it should be a valid pointer. let page_table: &mut PageTable = unsafe { page_table.as_mut().unwrap() }; diff --git a/kernel/src/device_tree.rs b/kernel/src/device_tree.rs index df0515e5..994999ab 100644 --- a/kernel/src/device_tree.rs +++ b/kernel/src/device_tree.rs @@ -76,8 +76,6 @@ impl DeviceTree { // The heuristic, the root irq chip doesn't have a reg property. // Works on aarch64 and riscv64. - let interrupt_controller = interrupt_controllers.find(|intc| intc.reg().is_some()); - - interrupt_controller + interrupt_controllers.find(|intc| intc.reg().is_some()) } } diff --git a/kernel/src/drivers/mod.rs b/kernel/src/drivers/mod.rs index 4bd19452..4de8f6ee 100644 --- a/kernel/src/drivers/mod.rs +++ b/kernel/src/drivers/mod.rs @@ -26,10 +26,7 @@ pub struct Matcher { impl Matcher { pub fn matches(&self, compatible: &str) -> bool { - self.compatibles - .iter() - .find(|&s| s == &compatible) - .is_some() + self.compatibles.iter().any(|s| s == &compatible) } } type ConsoleMatcher = Matcher; diff --git a/kernel/src/error.rs b/kernel/src/error.rs index 4037b086..5efa7e95 100644 --- a/kernel/src/error.rs +++ b/kernel/src/error.rs @@ -1,5 +1,3 @@ -use super::mm; - #[derive(Debug)] pub enum Error { DeviceNotFound(&'static str), diff --git a/kernel/src/executable/elf.rs b/kernel/src/executable/elf.rs index e93eb92f..890a9496 100644 --- a/kernel/src/executable/elf.rs +++ b/kernel/src/executable/elf.rs @@ -1,7 +1,6 @@ use core::iter::Iterator; use crate::globals; -use crate::mm; use crate::Error; use goblin; @@ -86,7 +85,7 @@ impl<'a> Elf<'a> { (virtual_pages as usize) - align_down(virtual_pages as usize, page_size); let segment_data_src_addr = ((self.data.as_ptr() as usize) + p_offset) as *const u8; - let segment_data_dst_addr = (usize::from(physical_pages) + offset_in_page) as *mut u8; + let segment_data_dst_addr = (physical_pages + offset_in_page) as *mut u8; let segment_data_src: &[u8] = unsafe { core::slice::from_raw_parts(segment_data_src_addr, p_filesz) }; @@ -95,9 +94,7 @@ impl<'a> Elf<'a> { unsafe { core::slice::from_raw_parts_mut(segment_data_dst_addr, p_memsz) }; // Zeroing uninitialized data - for i in p_filesz..p_memsz { - dst[i as usize] = 0u8; - } + dst[p_filesz..p_memsz].iter_mut().for_each(|e| *e = 0u8); dst }; @@ -112,7 +109,7 @@ impl<'a> Elf<'a> { hal::mm::current() .map( VAddr::new(align_down(virtual_pages as usize, page_size) + page_offset), - PAddr::new(usize::from(physical_pages) + page_offset), + PAddr::new(physical_pages + page_offset), perms, &globals::PHYSICAL_MEMORY_MANAGER, ) diff --git a/kernel/src/generic_main.rs b/kernel/src/generic_main.rs index d410e6f2..807445c7 100644 --- a/kernel/src/generic_main.rs +++ b/kernel/src/generic_main.rs @@ -15,7 +15,7 @@ pub fn generic_main(dt: DeviceTree, hacky_devices: &[& let qemu_exit = QemuExit::new(); let qemu_exit_slice = [&qemu_exit as &dyn Driver]; - let devices = hacky_devices.into_iter().chain(&qemu_exit_slice); + let devices = hacky_devices.iter().chain(&qemu_exit_slice); // Memory init globals::PHYSICAL_MEMORY_MANAGER diff --git a/kernel/src/globals.rs b/kernel/src/globals.rs index f2363e29..89d8e213 100644 --- a/kernel/src/globals.rs +++ b/kernel/src/globals.rs @@ -1,5 +1,3 @@ -use crate::lock::Lock; - use crate::mm; pub static PHYSICAL_MEMORY_MANAGER: mm::PhysicalMemoryManager = mm::PhysicalMemoryManager::new(); diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 8a04c99d..6b217d6d 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -24,7 +24,6 @@ pub mod executable; pub mod generic_main; pub mod globals; pub mod kernel_console; -mod lock; pub mod mm; mod panic; mod tests; diff --git a/kernel/src/lock.rs b/kernel/src/lock.rs deleted file mode 100644 index 92e3d15d..00000000 --- a/kernel/src/lock.rs +++ /dev/null @@ -1,27 +0,0 @@ -use core::cell::UnsafeCell; - -pub struct Lock { - data: UnsafeCell, -} - -impl Lock { - pub const fn new(data: T) -> Self { - Self { - data: UnsafeCell::new(data), - } - } - - pub fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut T) -> R) -> R { - // TODO: actually lock something... - let data = unsafe { &mut *self.data.get() }; - - f(data) - } - - pub fn get(&self) -> &'static mut T { - unsafe { &mut *self.data.get() } - } -} - -unsafe impl Send for Lock where T: Sized + Send {} -unsafe impl Sync for Lock where T: Sized + Send {} diff --git a/kernel/src/mm/binary_buddy_allocator.rs b/kernel/src/mm/binary_buddy_allocator.rs index c31da797..bb17cadb 100644 --- a/kernel/src/mm/binary_buddy_allocator.rs +++ b/kernel/src/mm/binary_buddy_allocator.rs @@ -31,7 +31,7 @@ unsafe impl GlobalAlloc for BinaryBuddyAllocator { }; globals::PHYSICAL_MEMORY_MANAGER .alloc(page_count) - .unwrap_or(0usize.into()) as *mut u8 + .unwrap_or(0usize) as *mut u8 } unsafe fn dealloc(&self, _: *mut u8, _: Layout) { diff --git a/kernel/src/mm/mod.rs b/kernel/src/mm/mod.rs index 05536677..581fe94f 100644 --- a/kernel/src/mm/mod.rs +++ b/kernel/src/mm/mod.rs @@ -6,16 +6,16 @@ mod binary_buddy_allocator; use crate::device_tree::DeviceTree; use crate::globals; -use crate::hal::{self, mm::PAGE_SIZE}; +use crate::hal; use crate::Error; -use hal_core::mm::{align_up, NullPageAllocator, PageAlloc, PageMap, Permissions, VAddr}; +use hal_core::mm::{NullPageAllocator, PageAlloc, PageMap, Permissions, VAddr}; use hal_core::AddressRange; use crate::drivers; use drivers::Driver; use arrayvec::ArrayVec; -use core::{iter, slice}; +use core::iter; use log::debug; @@ -142,14 +142,13 @@ pub fn map_address_space<'a, I: Iterator>( // the pre_allocated_entries). // Therefore no allocations will be made, pass the NullPageAllocator. globals::PHYSICAL_MEMORY_MANAGER.used_pages(|page| { - log::debug!("pushing rw allocated page 0x{:X}", page); - // let range = AddressRange::new(page..page + PAGE_SIZE); - // rw_entries.try_push(range); - hal::mm::current().identity_map( - VAddr::new(page), - Permissions::READ | Permissions::WRITE, - &mut NullPageAllocator, - ); + hal::mm::current() + .identity_map( + VAddr::new(page), + Permissions::READ | Permissions::WRITE, + &NullPageAllocator, + ) + .unwrap(); }); hal::mm::enable_paging(); diff --git a/kernel/src/mm/physical_memory_manager.rs b/kernel/src/mm/physical_memory_manager.rs index 0329de8e..f7a7e08a 100644 --- a/kernel/src/mm/physical_memory_manager.rs +++ b/kernel/src/mm/physical_memory_manager.rs @@ -2,7 +2,6 @@ use crate::device_tree::DeviceTree; use crate::globals; use crate::hal; use crate::mm; -use crate::Error; use core::mem; use hal_core::{ mm::{AllocatorError, NullPageAllocator, PageAlloc, PageMap, Permissions, VAddr}, @@ -253,14 +252,6 @@ impl PhysicalMemoryManager { Ok(()) } - fn metadata_pages(&self) -> impl core::iter::Iterator { - let metadata = self.metadata.lock(); - let metadata_start = (&metadata[0] as *const PhysicalPage) as usize; - let metadata_last = (&metadata[metadata.len() - 1] as *const PhysicalPage) as usize; - - (metadata_start..=metadata_last).step_by(PAGE_SIZE) - } - pub fn alloc_pages(&self, page_count: usize) -> Result { let mut consecutive_pages: usize = 0; let mut first_page_index: usize = 0; @@ -293,8 +284,6 @@ impl PhysicalMemoryManager { .for_each(|page| page.set_allocated()); metadata[i].set_last(); - let addr = metadata[first_page_index].base; - return Ok(metadata[first_page_index].base); } } @@ -307,21 +296,22 @@ impl PageAlloc for PhysicalMemoryManager { fn alloc(&self, page_count: usize) -> Result { // If there is a kernel pagetable, identity map the pages. let first_page = self.alloc_pages(page_count)?; - let addr: usize = first_page.into(); if unsafe { globals::STATE.is_mmu_enabled() } { // The mmu is enabled, therefore we already mapped all DRAM into the kernel's pagetable // as invalid entries. // Pagetable must only modify existing entries and not allocate. - hal::mm::current().identity_map_range( - VAddr::new(addr), - page_count, - Permissions::READ | Permissions::WRITE, - &mut NullPageAllocator, - ); + hal::mm::current() + .identity_map_range( + VAddr::new(first_page), + page_count, + Permissions::READ | Permissions::WRITE, + &NullPageAllocator, + ) + .unwrap(); } - Ok(addr) + Ok(first_page) } fn dealloc(&self, _base: usize, _page_count: usize) -> Result<(), AllocatorError> {