From 49eb21ce0e9ddb2cdc31258811aeb28a52719f72 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 2 Apr 2026 19:55:07 +0200 Subject: [PATCH] Use KERNEL_OFFSET from linker script --- src/allocator/linked_list.rs | 2 +- src/allocator/mod.rs | 2 +- src/arch/aarch64/consts.rs | 8 ++++---- src/arch/riscv64/consts.rs | 8 ++++---- src/arch/x86/consts.rs | 8 ++++---- src/arch/x86_64/consts.rs | 7 +++++-- src/main.rs | 4 +++- src/panic.rs | 2 +- src/startup/memory.rs | 6 ++++-- 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/allocator/linked_list.rs b/src/allocator/linked_list.rs index 116aba5251..f17ef4abdf 100644 --- a/src/allocator/linked_list.rs +++ b/src/allocator/linked_list.rs @@ -28,7 +28,7 @@ unsafe impl GlobalAlloc for Allocator { let size = heap.size(); super::map_heap( &mut KernelMapper::lock_rw(), - crate::KERNEL_HEAP_OFFSET + size, + crate::kernel_heap_offset() + size, super::KERNEL_HEAP_SIZE, ); heap.extend(super::KERNEL_HEAP_SIZE); diff --git a/src/allocator/mod.rs b/src/allocator/mod.rs index cccbb8454f..ef239afe99 100644 --- a/src/allocator/mod.rs +++ b/src/allocator/mod.rs @@ -39,7 +39,7 @@ unsafe fn map_heap(mapper: &mut KernelMapper, offset: usize, size: usize) pub unsafe fn init() { unsafe { - let offset = crate::KERNEL_HEAP_OFFSET; + let offset = crate::kernel_heap_offset(); let size = KERNEL_HEAP_SIZE; // Map heap pages diff --git a/src/arch/aarch64/consts.rs b/src/arch/aarch64/consts.rs index 145db36cdb..ea0a640149 100644 --- a/src/arch/aarch64/consts.rs +++ b/src/arch/aarch64/consts.rs @@ -5,11 +5,11 @@ /// The size of a single PML4 pub const PML4_SIZE: usize = 0x0000_0080_0000_0000; -/// Offset of kernel -pub const KERNEL_OFFSET: usize = (2 * PML4_SIZE).wrapping_neg(); - /// Offset to kernel heap -pub const KERNEL_HEAP_OFFSET: usize = KERNEL_OFFSET - PML4_SIZE; +#[inline(always)] +pub fn kernel_heap_offset() -> usize { + crate::kernel_executable_offsets::KERNEL_OFFSET() - PML4_SIZE +} /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 256 * PML4_SIZE; diff --git a/src/arch/riscv64/consts.rs b/src/arch/riscv64/consts.rs index 39867f9029..1470ae0e28 100644 --- a/src/arch/riscv64/consts.rs +++ b/src/arch/riscv64/consts.rs @@ -6,11 +6,11 @@ const PML4_SHIFT: usize = (CurrentRmmArch::PAGE_LEVELS - 1) * CurrentRmmArch::PA /// The size of a single PML4 pub const PML4_SIZE: usize = 1_usize << PML4_SHIFT; -/// Offset of kernel -pub const KERNEL_OFFSET: usize = (2 * PML4_SIZE).wrapping_neg(); - /// Offset to kernel heap -pub const KERNEL_HEAP_OFFSET: usize = KERNEL_OFFSET - PML4_SIZE; +#[inline(always)] +pub fn kernel_heap_offset() -> usize { + crate::kernel_executable_offsets::KERNEL_OFFSET() - PML4_SIZE +} /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 1_usize << (CurrentRmmArch::PAGE_ADDRESS_SHIFT - 1); diff --git a/src/arch/x86/consts.rs b/src/arch/x86/consts.rs index 224896f5dd..28c5073f5a 100644 --- a/src/arch/x86/consts.rs +++ b/src/arch/x86/consts.rs @@ -3,9 +3,6 @@ // Each PML4 entry references up to 512 GB of memory // The second from the top (510) PML4 is reserved for the kernel -/// Offset of kernel (256 MiB max) -pub const KERNEL_OFFSET: usize = 0xC000_0000; - // Framebuffer mapped by bootloader to 0xD000_0000 (128 MiB max) // Offset to APIC mappings (optional) @@ -14,7 +11,10 @@ pub const IOAPIC_OFFSET: usize = LAPIC_OFFSET + 4096; pub const HPET_OFFSET: usize = IOAPIC_OFFSET + 4096; /// Offset to kernel heap (256 MiB max) -pub const KERNEL_HEAP_OFFSET: usize = 0xE000_0000; +#[inline(always)] +pub fn kernel_heap_offset() -> usize { + 0xE000_0000 +} /// End offset of the user image, i.e. kernel start pub const USER_END_OFFSET: usize = 0x8000_0000; diff --git a/src/arch/x86_64/consts.rs b/src/arch/x86_64/consts.rs index 6a744dbc2b..547b9bfadb 100644 --- a/src/arch/x86_64/consts.rs +++ b/src/arch/x86_64/consts.rs @@ -12,10 +12,13 @@ pub const PML4_SIZE: usize = 0x0000_0080_0000_0000; /// Offset of kernel -pub const KERNEL_OFFSET: usize = (1_usize << 31).wrapping_neg(); +const KERNEL_OFFSET: usize = (1_usize << 31).wrapping_neg(); /// Offset to kernel heap -pub const KERNEL_HEAP_OFFSET: usize = KERNEL_OFFSET - PML4_SIZE; +#[inline(always)] +pub fn kernel_heap_offset() -> usize { + crate::kernel_executable_offsets::KERNEL_OFFSET() - PML4_SIZE +} /// End offset of the user image, i.e. kernel start // TODO: Make this offset at least PAGE_SIZE less? There are known hardware bugs on some arches, diff --git a/src/main.rs b/src/main.rs index 5c47f91397..4397beab45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -223,7 +223,8 @@ fn run_userspace(token: &mut CleanLockToken) -> ! { macro_rules! linker_offsets( ($($name:ident),*) => { $( - #[inline] + #[inline(always)] + #[allow(non_snake_case)] pub fn $name() -> usize { unsafe extern "C" { // TODO: UnsafeCell? @@ -236,6 +237,7 @@ macro_rules! linker_offsets( ); mod kernel_executable_offsets { linker_offsets!( + KERNEL_OFFSET, __text_start, __text_end, __rodata_start, diff --git a/src/panic.rs b/src/panic.rs index 4e03ed4fc5..c10fa16c72 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -79,7 +79,7 @@ pub unsafe fn stack_trace() { unsafe { let mapper = KernelMapper::lock_ro(); - let kernel_ptr = crate::KERNEL_OFFSET as *const u8; + let kernel_ptr = crate::kernel_executable_offsets::KERNEL_OFFSET() as *const u8; let elf_header: &FileHeader = object::pod::from_bytes(slice::from_raw_parts( kernel_ptr, size_of::>(), diff --git a/src/startup/memory.rs b/src/startup/memory.rs index 603d9a7cad..d52e263b27 100644 --- a/src/startup/memory.rs +++ b/src/startup/memory.rs @@ -1,5 +1,5 @@ use crate::{ - arch::{consts::KERNEL_OFFSET, CurrentRmmArch}, + arch::CurrentRmmArch, memory::PAGE_SIZE, startup::{memory::BootloaderMemoryKind::Null, KernelArgs}, }; @@ -331,7 +331,9 @@ unsafe fn map_memory(areas: &[MemoryArea], mut bump_allocator: &mut Bum // Map kernel at KERNEL_OFFSET and identity map too for i in 0..kernel_size / A::PAGE_SIZE { let phys = PhysicalAddress::new(kernel_base + i * PAGE_SIZE); - let virt = VirtualAddress::new(KERNEL_OFFSET + i * PAGE_SIZE); + let virt = VirtualAddress::new( + crate::kernel_executable_offsets::KERNEL_OFFSET() + i * PAGE_SIZE, + ); let flags = kernel_page_flags::(virt); let flush = mapper .map_phys(virt, phys, flags)