Skip to content

Commit

Permalink
cleanup: apply clippy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
n1tram1 committed Dec 2, 2023
1 parent 67f2691 commit 346b9cb
Show file tree
Hide file tree
Showing 18 changed files with 38 additions and 96 deletions.
9 changes: 5 additions & 4 deletions hal_aarch64/src/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
}
}

Expand Down
2 changes: 1 addition & 1 deletion hal_aarch64/src/mm/pgt48.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };

Expand Down
1 change: 0 additions & 1 deletion hal_core/src/mm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ pub fn prefill_pagetable<P: PageMap + 'static>(
) -> 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)?;
Expand Down
7 changes: 3 additions & 4 deletions hal_riscv64/src/irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)();
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions hal_riscv64/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ mod registers;

use core::arch::asm;

pub fn panic_info() -> () {
()
}
pub fn panic_info() {}

#[naked]
#[no_mangle]
Expand Down
2 changes: 1 addition & 1 deletion hal_riscv64/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 1 addition & 5 deletions hal_riscv64/src/mm/sv39.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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() };

Expand Down
4 changes: 1 addition & 3 deletions kernel/src/device_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
5 changes: 1 addition & 4 deletions kernel/src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ pub struct Matcher<T: ?Sized> {

impl<T: ?Sized> Matcher<T> {
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<dyn Console + Send + Sync>;
Expand Down
2 changes: 0 additions & 2 deletions kernel/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use super::mm;

#[derive(Debug)]
pub enum Error {
DeviceNotFound(&'static str),
Expand Down
9 changes: 3 additions & 6 deletions kernel/src/executable/elf.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use core::iter::Iterator;

use crate::globals;
use crate::mm;
use crate::Error;

use goblin;
Expand Down Expand Up @@ -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) };
Expand All @@ -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
};
Expand All @@ -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,
)
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/generic_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn generic_main<const LAUNCH_TESTS: bool>(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
Expand Down
2 changes: 0 additions & 2 deletions kernel/src/globals.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use crate::lock::Lock;

use crate::mm;

pub static PHYSICAL_MEMORY_MANAGER: mm::PhysicalMemoryManager = mm::PhysicalMemoryManager::new();
Expand Down
1 change: 0 additions & 1 deletion kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
27 changes: 0 additions & 27 deletions kernel/src/lock.rs

This file was deleted.

2 changes: 1 addition & 1 deletion kernel/src/mm/binary_buddy_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 10 additions & 11 deletions kernel/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -142,14 +142,13 @@ pub fn map_address_space<'a, I: Iterator<Item = &'a &'a dyn Driver>>(
// 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();
Expand Down
28 changes: 9 additions & 19 deletions kernel/src/mm/physical_memory_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -253,14 +252,6 @@ impl PhysicalMemoryManager {
Ok(())
}

fn metadata_pages(&self) -> impl core::iter::Iterator<Item = usize> {
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<usize, AllocatorError> {
let mut consecutive_pages: usize = 0;
let mut first_page_index: usize = 0;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -307,21 +296,22 @@ impl PageAlloc for PhysicalMemoryManager {
fn alloc(&self, page_count: usize) -> Result<usize, AllocatorError> {
// 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> {
Expand Down

0 comments on commit 346b9cb

Please sign in to comment.