Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: enable unsafe_op_in_unsafe_fn lint #288

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/arch/riscv64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down Expand Up @@ -101,7 +102,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {

static mut BOOT_INFO: Option<RawBootInfo> = None;

BOOT_INFO = {
let boot_info = {
let phys_addr_range = {
let memory = fdt.memory();
let mut regions = memory.regions();
Expand Down Expand Up @@ -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)
)
}
}
73 changes: 41 additions & 32 deletions src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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::<Size4KiB>(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")
Expand Down Expand Up @@ -245,7 +247,7 @@ pub unsafe fn find_kernel() -> &'static [u8] {
paging::map::<Size2MiB>(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"))]
Expand Down Expand Up @@ -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::<Multiboot<'_, '_>>()).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::<Multiboot<'_, '_>>())
.align_up(Size4KiB::SIZE as usize);
}

let command_line = multiboot.command_line();
Expand All @@ -432,15 +435,17 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! {
);

// clear stack
write_bytes(
sptr::from_exposed_addr_mut::<u8>(new_stack),
0,
KERNEL_STACK_SIZE.try_into().unwrap(),
);
unsafe {
write_bytes(
sptr::from_exposed_addr_mut::<u8>(new_stack),
0,
KERNEL_STACK_SIZE.try_into().unwrap(),
);
}

static mut BOOT_INFO: Option<RawBootInfo> = None;

BOOT_INFO = {
let boot_info = {
let boot_info = BootInfo {
hardware_info: HardwareInfo {
phys_addr_range: 0..0,
Expand All @@ -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!(
Expand All @@ -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 {
Expand All @@ -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 }
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![no_std]
#![no_main]
#![warn(rust_2018_idioms)]
#![cfg_attr(target_arch = "riscv64", allow(unstable_name_collisions))]
#![allow(clippy::missing_safety_doc)]
#![warn(unsafe_op_in_unsafe_fn)]
#![allow(unstable_name_collisions)]
#![allow(clippy::missing_safety_doc)]

#[macro_use]
mod macros;
Expand Down
21 changes: 13 additions & 8 deletions src/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MaybeUninit<u8>>(kernel_addr as usize),
mem_size,
);
let memory = unsafe {
slice::from_raw_parts_mut(
sptr::from_exposed_addr_mut::<MaybeUninit<u8>>(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]
Expand Down