From afedb33dcd406fcf20fa02b9f01ea18c74d45610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 7 Feb 2024 16:31:47 +0100 Subject: [PATCH 1/3] fix: remove redundant lint config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 8270d8c1..2ec1cb8f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ #![no_std] #![no_main] #![warn(rust_2018_idioms)] -#![cfg_attr(target_arch = "riscv64", allow(unstable_name_collisions))] #![allow(clippy::missing_safety_doc)] #![allow(unstable_name_collisions)] From 98f7112809d50014b390d859c15eaab7713ae839 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 7 Feb 2024 16:32:08 +0100 Subject: [PATCH 2/3] fix: reorder lint config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2ec1cb8f..85fba20e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ #![no_std] #![no_main] #![warn(rust_2018_idioms)] -#![allow(clippy::missing_safety_doc)] #![allow(unstable_name_collisions)] +#![allow(clippy::missing_safety_doc)] #[macro_use] mod macros; From b9276de4f7fcf38fd2f83fe6af7fb4407a0fd2a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 7 Feb 2024 16:33:53 +0100 Subject: [PATCH 3/3] fix: enable unsafe_op_in_unsafe_fn lint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/riscv64/mod.rs | 33 +++++++++++-------- src/arch/x86_64/mod.rs | 73 +++++++++++++++++++++++------------------ src/main.rs | 1 + src/none.rs | 21 +++++++----- 4 files changed, 75 insertions(+), 53 deletions(-) diff --git a/src/arch/riscv64/mod.rs b/src/arch/riscv64/mod.rs index bdf12265..b272dd1b 100644 --- a/src/arch/riscv64/mod.rs +++ b/src/arch/riscv64/mod.rs @@ -64,7 +64,8 @@ pub unsafe fn get_memory(memory_size: u64) -> u64 { let initrd = AddressRange::try_from(find_kernel().as_ptr_range()).unwrap(); let fdt = { let start = start::get_fdt_ptr(); - AddressRange::try_from(start..start.add(start::get_fdt().total_size())).unwrap() + let end = unsafe { start.add(start::get_fdt().total_size()) }; + AddressRange::try_from(start..end).unwrap() }; info!("initrd = {initrd}"); @@ -101,7 +102,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { static mut BOOT_INFO: Option = None; - BOOT_INFO = { + let boot_info = { let phys_addr_range = { let memory = fdt.memory(); let mut regions = memory.regions(); @@ -134,25 +135,31 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { info!("boot_info = {boot_info:#?}"); - Some(RawBootInfo::from(boot_info)) + RawBootInfo::from(boot_info) }; + unsafe { + BOOT_INFO = Some(boot_info); + } + // Check expected signature of entry function let entry: Entry = { let entry: unsafe extern "C" fn(hart_id: usize, boot_info: &'static RawBootInfo) -> ! = - core::mem::transmute(entry_point); + unsafe { core::mem::transmute(entry_point) }; entry }; info!("Jumping into kernel at {entry:p}"); - asm!( - "mv sp, {stack}", - "jr {entry}", - entry = in(reg) entry, - stack = in(reg) start::get_stack_ptr(), - in("a0") start::get_hart_id(), - in("a1") BOOT_INFO.as_ref().unwrap(), - options(noreturn) - ) + unsafe { + asm!( + "mv sp, {stack}", + "jr {entry}", + entry = in(reg) entry, + stack = in(reg) start::get_stack_ptr(), + in("a0") start::get_hart_id(), + in("a1") BOOT_INFO.as_ref().unwrap(), + options(noreturn) + ) + } } diff --git a/src/arch/x86_64/mod.rs b/src/arch/x86_64/mod.rs index 59d5f774..ab9d3dd1 100644 --- a/src/arch/x86_64/mod.rs +++ b/src/arch/x86_64/mod.rs @@ -56,7 +56,7 @@ static mut MEM: Mem = Mem; impl MemoryManagement for Mem { unsafe fn paddr_to_slice<'a>(&self, p: PAddr, sz: usize) -> Option<&'static [u8]> { let ptr = sptr::from_exposed_addr(p as usize); - Some(slice::from_raw_parts(ptr, sz)) + unsafe { Some(slice::from_raw_parts(ptr, sz)) } } // If you only want to read fields, you can simply return `None`. @@ -174,18 +174,20 @@ pub unsafe fn find_kernel() -> &'static [u8] { } #[cfg(all(target_os = "none", not(feature = "fc")))] -pub unsafe fn find_kernel() -> &'static [u8] { +pub fn find_kernel() -> &'static [u8] { use core::cmp; paging::clean_up(); // Identity-map the Multiboot information. - assert!(mb_info > 0, "Could not find Multiboot information"); - info!("Found Multiboot information at {:#x}", mb_info); - let page_address = mb_info.align_down(Size4KiB::SIZE as usize); + unsafe { + assert!(mb_info > 0, "Could not find Multiboot information"); + info!("Found Multiboot information at {:#x}", mb_info); + } + let page_address = unsafe { mb_info.align_down(Size4KiB::SIZE as usize) }; paging::map::(page_address, page_address, 1, PageTableFlags::empty()); // Load the Multiboot information and identity-map the modules information. - let multiboot = Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap(); + let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap() }; let modules_address = multiboot .modules() .expect("Could not find a memory map in the Multiboot information") @@ -245,7 +247,7 @@ pub unsafe fn find_kernel() -> &'static [u8] { paging::map::(address, address, counter, PageTableFlags::empty()); } - slice::from_raw_parts(sptr::from_exposed_addr(elf_start), elf_len) + unsafe { slice::from_raw_parts(sptr::from_exposed_addr(elf_start), elf_len) } } #[cfg(all(target_os = "none", feature = "fc"))] @@ -401,14 +403,15 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { entry_point, } = kernel_info; - let multiboot = Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap(); + let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut MEM).unwrap() }; // determine boot stack address - let mut new_stack = (&kernel_end as *const u8 as usize).align_up(Size4KiB::SIZE as usize); + let mut new_stack = + (unsafe { &kernel_end } as *const u8 as usize).align_up(Size4KiB::SIZE as usize); - if new_stack + KERNEL_STACK_SIZE as usize > mb_info { - new_stack = - (mb_info + mem::size_of::>()).align_up(Size4KiB::SIZE as usize); + if new_stack + KERNEL_STACK_SIZE as usize > unsafe { mb_info } { + new_stack = (unsafe { mb_info } + mem::size_of::>()) + .align_up(Size4KiB::SIZE as usize); } let command_line = multiboot.command_line(); @@ -432,15 +435,17 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { ); // clear stack - write_bytes( - sptr::from_exposed_addr_mut::(new_stack), - 0, - KERNEL_STACK_SIZE.try_into().unwrap(), - ); + unsafe { + write_bytes( + sptr::from_exposed_addr_mut::(new_stack), + 0, + KERNEL_STACK_SIZE.try_into().unwrap(), + ); + } static mut BOOT_INFO: Option = None; - BOOT_INFO = { + let boot_info = { let boot_info = BootInfo { hardware_info: HardwareInfo { phys_addr_range: 0..0, @@ -450,13 +455,15 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { load_info, platform_info: PlatformInfo::Multiboot { command_line, - multiboot_info_addr: (mb_info as u64).try_into().unwrap(), + multiboot_info_addr: (unsafe { mb_info } as u64).try_into().unwrap(), }, }; - Some(RawBootInfo::from(boot_info)) + RawBootInfo::from(boot_info) }; - - info!("BootInfo located at {:p}", &BOOT_INFO); + unsafe { + BOOT_INFO = Some(boot_info); + info!("BootInfo located at {:p}", &BOOT_INFO); + } // Jump to the kernel entry point and provide the Multiboot information to it. info!( @@ -475,15 +482,17 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { entry_signature }; - asm!( - "mov rsp, {stack_address}", - "jmp {entry}", - stack_address = in(reg) current_stack_address, - entry = in(reg) entry_point, - in("rdi") BOOT_INFO.as_ref().unwrap(), - in("rsi") 0, - options(noreturn) - ) + unsafe { + asm!( + "mov rsp, {stack_address}", + "jmp {entry}", + stack_address = in(reg) current_stack_address, + entry = in(reg) entry_point, + in("rdi") BOOT_INFO.as_ref().unwrap(), + in("rsi") 0, + options(noreturn) + ) + } } unsafe fn map_memory(address: usize, memory_size: usize) -> usize { @@ -497,5 +506,5 @@ unsafe fn map_memory(address: usize, memory_size: usize) -> usize { pub unsafe fn get_memory(memory_size: u64) -> u64 { let address = PhysAlloc::allocate((memory_size as usize).align_up(Size2MiB::SIZE as usize)); - map_memory(address, memory_size as usize) as u64 + unsafe { map_memory(address, memory_size as usize) as u64 } } diff --git a/src/main.rs b/src/main.rs index 85fba20e..0f1585e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #![no_std] #![no_main] #![warn(rust_2018_idioms)] +#![warn(unsafe_op_in_unsafe_fn)] #![allow(unstable_name_collisions)] #![allow(clippy::missing_safety_doc)] diff --git a/src/none.rs b/src/none.rs index f66b2d49..f7051cd4 100644 --- a/src/none.rs +++ b/src/none.rs @@ -19,21 +19,26 @@ pub(crate) unsafe extern "C" fn loader_main() -> ! { arch::message_output_init(); crate::log::init(); - info!("Loader: [{:p} - {:p}]", &kernel_start, &kernel_end); + unsafe { + info!("Loader: [{:p} - {:p}]", &kernel_start, &kernel_end); + } - let kernel = KernelObject::parse(arch::find_kernel()).unwrap(); + let kernel = arch::find_kernel(); + let kernel = KernelObject::parse(kernel).unwrap(); let mem_size = kernel.mem_size(); - let kernel_addr = arch::get_memory(mem_size as u64); + let kernel_addr = unsafe { arch::get_memory(mem_size as u64) }; let kernel_addr = kernel.start_addr().unwrap_or(kernel_addr); - let memory = slice::from_raw_parts_mut( - sptr::from_exposed_addr_mut::>(kernel_addr as usize), - mem_size, - ); + let memory = unsafe { + slice::from_raw_parts_mut( + sptr::from_exposed_addr_mut::>(kernel_addr as usize), + mem_size, + ) + }; let kernel_info = kernel.load_kernel(memory, memory.as_ptr() as u64); - arch::boot_kernel(kernel_info) + unsafe { arch::boot_kernel(kernel_info) } } #[panic_handler]