diff --git a/src/arch/aarch64/mod.rs b/src/arch/aarch64/mod.rs index 188eb80b..6ebdfb9c 100644 --- a/src/arch/aarch64/mod.rs +++ b/src/arch/aarch64/mod.rs @@ -22,7 +22,6 @@ use crate::arch::paging::*; use crate::os::CONSOLE; unsafe extern "C" { - static mut _end: u8; static mut l0_pgtable: u64; static mut l1_pgtable: u64; static mut l2_pgtable: u64; @@ -48,7 +47,8 @@ const PT_MEM_CD: u64 = 0x70F; const PT_SELF: u64 = 1 << 55; pub unsafe fn get_memory(_memory_size: u64) -> u64 { - (ptr::addr_of_mut!(_end).expose_provenance() as u64).align_up(LargePageSize::SIZE as u64) + let loader_end = crate::os::executable_end().as_ptr(); + (loader_end.expose_provenance() as u64).align_up(LargePageSize::SIZE as u64) } pub fn find_kernel() -> &'static [u8] { diff --git a/src/arch/x86_64/platform/linux/entry.s b/src/arch/x86_64/platform/linux/entry.s index 756a6c97..102999be 100644 --- a/src/arch/x86_64/platform/linux/entry.s +++ b/src/arch/x86_64/platform/linux/entry.s @@ -3,9 +3,6 @@ .code64 -.extern __executable_start # defined in linker script -.extern _end - # Move entry point at the beginning of the elf file .section .mboot, "a" .align 8 diff --git a/src/arch/x86_64/platform/linux/mod.rs b/src/arch/x86_64/platform/linux/mod.rs index b5463213..b0bf04ab 100644 --- a/src/arch/x86_64/platform/linux/mod.rs +++ b/src/arch/x86_64/platform/linux/mod.rs @@ -18,10 +18,6 @@ use crate::arch::x86_64::physicalmem::PhysAlloc; use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT, page_tables}; use crate::fdt::Fdt; -unsafe extern "C" { - static mut _end: u8; -} - mod entry { core::arch::global_asm!( include_str!("entry.s"), @@ -41,7 +37,8 @@ unsafe extern "C" fn rust_start(boot_params: *mut BootParams) -> ! { crate::log::init(); BOOT_PARAMS.store(boot_params, Ordering::Relaxed); - let free_addr = ptr::addr_of!(_end).addr().align_up(Size2MiB::SIZE as usize); + let loader_end = crate::os::executable_end().as_ptr(); + let free_addr = loader_end.addr().align_up(Size2MiB::SIZE as usize); // Memory after the highest end address is unused and available for the physical memory manager. info!("Intializing PhysAlloc with {free_addr:#x}"); PhysAlloc::init(free_addr); @@ -83,9 +80,9 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { let boot_params_ref = unsafe { BootParams::get() }; // determine boot stack address - let stack = - (ptr::addr_of!(_end).addr() + Size4KiB::SIZE as usize).align_up(Size4KiB::SIZE as usize); - let stack = ptr::addr_of_mut!(_end).with_addr(stack); + let loader_end = crate::os::executable_end().as_ptr(); + let stack = (loader_end.addr() + Size4KiB::SIZE as usize).align_up(Size4KiB::SIZE as usize); + let stack = loader_end.with_addr(stack).cast::(); // clear stack unsafe { write_bytes(stack, 0, KERNEL_STACK_SIZE.try_into().unwrap()); diff --git a/src/arch/x86_64/platform/multiboot/entry.s b/src/arch/x86_64/platform/multiboot/entry.s index 3fcab471..1cf64a67 100644 --- a/src/arch/x86_64/platform/multiboot/entry.s +++ b/src/arch/x86_64/platform/multiboot/entry.s @@ -5,9 +5,6 @@ .code32 -.extern __executable_start # defined in linker script -.extern _end - # We use a special name to map this section at the begin of our kernel # => Multiboot expects its magic number at the beginning of the kernel. .section .mboot, "a" diff --git a/src/arch/x86_64/platform/multiboot/mod.rs b/src/arch/x86_64/platform/multiboot/mod.rs index b570a0d9..aa0ae607 100644 --- a/src/arch/x86_64/platform/multiboot/mod.rs +++ b/src/arch/x86_64/platform/multiboot/mod.rs @@ -17,10 +17,7 @@ use crate::BootInfoExt; use crate::arch::x86_64::physicalmem::PhysAlloc; use crate::arch::x86_64::{KERNEL_STACK_SIZE, SERIAL_IO_PORT, page_tables}; use crate::fdt::Fdt; - -unsafe extern "C" { - static mut _end: u8; -} +use crate::os::executable_end; #[allow(bad_asm_style)] mod entry { @@ -148,7 +145,8 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { let multiboot = unsafe { Multiboot::from_ptr(mb_info as u64, &mut mem).unwrap() }; // determine boot stack address - let mut new_stack = ptr::addr_of!(_end).addr().align_up(Size4KiB::SIZE as usize); + let loader_end = executable_end().as_ptr(); + let mut new_stack = loader_end.addr().align_up(Size4KiB::SIZE as usize); if new_stack + KERNEL_STACK_SIZE as usize > mb_info.addr() { new_stack = (mb_info.addr() + mem::size_of::>()) @@ -164,7 +162,7 @@ pub unsafe fn boot_kernel(kernel_info: LoadedKernel) -> ! { } } - let stack = ptr::addr_of_mut!(_end).with_addr(new_stack); + let stack = loader_end.with_addr(new_stack).cast::(); // clear stack unsafe { diff --git a/src/os/none/mod.rs b/src/os/none/mod.rs index 2eda78fd..d75eaa51 100644 --- a/src/os/none/mod.rs +++ b/src/os/none/mod.rs @@ -3,6 +3,7 @@ mod console; use core::fmt::Write; use core::mem::MaybeUninit; +use core::ptr::NonNull; use core::{ptr, slice}; use hermit_entry::elf::KernelObject; @@ -11,17 +12,32 @@ use log::info; pub use self::console::CONSOLE; use crate::arch; -unsafe extern "C" { - static _end: u8; - static __executable_start: u8; +pub fn executable_start() -> NonNull<()> { + unsafe extern "C" { + static mut __executable_start: u8; + } + + let ptr = &raw mut __executable_start; + let ptr = ptr.cast::<()>(); + NonNull::new(ptr).unwrap() +} + +pub fn executable_end() -> NonNull<()> { + unsafe extern "C" { + static mut _end: u8; + } + + let ptr = &raw mut _end; + let ptr = ptr.cast::<()>(); + NonNull::new(ptr).unwrap() } /// Entry Point of the BIOS Loader /// (called from entry.asm or entry.rs) pub(crate) unsafe extern "C" fn loader_main() -> ! { - unsafe { - info!("Loader: [{:p} - {:p}]", &__executable_start, &_end); - } + let loader_start = executable_start(); + let loader_end = executable_end(); + info!("Loader: [{loader_start:p} - {loader_end:p}]"); let kernel = arch::find_kernel(); let kernel = KernelObject::parse(kernel).unwrap();