From 615f45b65606999ef8256a6265521261e4e12fff Mon Sep 17 00:00:00 2001 From: Graham MacDonald Date: Fri, 29 Mar 2024 20:32:30 +0000 Subject: [PATCH] Tidy mem ranges Signed-off-by: Graham MacDonald --- aarch64/src/kmem.rs | 80 +++++++++++++++++++++++++++++++++++++----- aarch64/src/main.rs | 40 +++++++-------------- aarch64/src/vmalloc.rs | 4 +-- 3 files changed, 86 insertions(+), 38 deletions(-) diff --git a/aarch64/src/kmem.rs b/aarch64/src/kmem.rs index f8bfa14..9c9efbe 100644 --- a/aarch64/src/kmem.rs +++ b/aarch64/src/kmem.rs @@ -3,37 +3,69 @@ use port::mem::{PhysAddr, PhysRange, VirtRange}; // These map to definitions in kernel.ld extern "C" { + static boottext: [u64; 0]; + static eboottext: [u64; 0]; + static text: [u64; 0]; static etext: [u64; 0]; + static rodata: [u64; 0]; static erodata: [u64; 0]; + static data: [u64; 0]; + static edata: [u64; 0]; + static bss: [u64; 0]; static ebss: [u64; 0]; static early_pagetables: [u64; 0]; static eearly_pagetables: [u64; 0]; static heap: [u64; 0]; static eheap: [u64; 0]; + static end: [u64; 0]; +} + +fn start_addr() -> usize { + unsafe { boottext.as_ptr().addr() } +} + +fn end_addr() -> usize { + unsafe { end.as_ptr().addr() } +} + +fn boottext_addr() -> usize { + unsafe { boottext.as_ptr().addr() } +} + +fn eboottext_addr() -> usize { + unsafe { eboottext.as_ptr().addr() } } fn text_addr() -> usize { - 0xffff_8000_0000_0000 + unsafe { text.as_ptr().addr() } } fn etext_addr() -> usize { unsafe { etext.as_ptr().addr() } } +fn rodata_addr() -> usize { + unsafe { rodata.as_ptr().addr() } +} + fn erodata_addr() -> usize { unsafe { erodata.as_ptr().addr() } } -fn ebss_addr() -> usize { - unsafe { ebss.as_ptr().addr() } +fn data_addr() -> usize { + unsafe { data.as_ptr().addr() } } -fn early_pagetables_addr() -> usize { - unsafe { early_pagetables.as_ptr().addr() } +fn edata_addr() -> usize { + unsafe { edata.as_ptr().addr() } } -fn eearly_pagetables_addr() -> usize { - unsafe { eearly_pagetables.as_ptr().addr() } +fn bss_addr() -> usize { + unsafe { bss.as_ptr().addr() } +} + +fn ebss_addr() -> usize { + unsafe { ebss.as_ptr().addr() } } fn heap_addr() -> usize { @@ -44,6 +76,14 @@ fn eheap_addr() -> usize { unsafe { eheap.as_ptr().addr() } } +fn early_pagetables_addr() -> usize { + unsafe { early_pagetables.as_ptr().addr() } +} + +fn eearly_pagetables_addr() -> usize { + unsafe { eearly_pagetables.as_ptr().addr() } +} + pub const fn physaddr_as_virt(pa: PhysAddr) -> usize { (pa.addr() as usize).wrapping_add(KZERO) } @@ -76,7 +116,31 @@ pub fn kernel_heap_physrange() -> PhysRange { PhysRange::with_len(from_virt_to_physaddr(heap_addr()).addr(), eheap_addr() - heap_addr()) } -pub fn kernel_heap_virtrange() -> VirtRange { +pub fn total_virtrange() -> VirtRange { + VirtRange(start_addr()..end_addr()) +} + +pub fn boottext_virtrange() -> VirtRange { + VirtRange(boottext_addr()..eboottext_addr()) +} + +pub fn text_virtrange() -> VirtRange { + VirtRange(text_addr()..etext_addr()) +} + +pub fn rodata_virtrange() -> VirtRange { + VirtRange(rodata_addr()..erodata_addr()) +} + +pub fn data_virtrange() -> VirtRange { + VirtRange(data_addr()..edata_addr()) +} + +pub fn bss_virtrange() -> VirtRange { + VirtRange(bss_addr()..ebss_addr()) +} + +pub fn heap_virtrange() -> VirtRange { VirtRange::with_len(heap_addr(), eheap_addr() - heap_addr()) } diff --git a/aarch64/src/main.rs b/aarch64/src/main.rs index 8fc8007..c7a8a58 100644 --- a/aarch64/src/main.rs +++ b/aarch64/src/main.rs @@ -25,10 +25,9 @@ mod vmalloc; use crate::kmem::from_virt_to_physaddr; use crate::vm::kernel_root; -use core::ffi::c_void; use core::ptr; use port::fdt::DeviceTree; -use port::mem::PhysRange; +use port::mem::{PhysRange, VirtRange}; use port::println; use vm::PageTable; @@ -37,38 +36,23 @@ core::arch::global_asm!(include_str!("l.S")); static mut KPGTBL: PageTable = PageTable::empty(); -unsafe fn print_memory_range(name: &str, start: &*const c_void, end: &*const c_void) { - let start = start as *const _ as u64; - let end = end as *const _ as u64; - let size = end - start; +unsafe fn print_memory_range(name: &str, range: VirtRange) { + let start = range.start(); + let end = range.end(); + let size = range.size(); println!(" {name}{start:#x}..{end:#x} ({size:#x})"); } fn print_binary_sections() { - extern "C" { - static boottext: *const c_void; - static eboottext: *const c_void; - static text: *const c_void; - static etext: *const c_void; - static rodata: *const c_void; - static erodata: *const c_void; - static data: *const c_void; - static edata: *const c_void; - static bss: *const c_void; - static end: *const c_void; - static heap: *const c_void; - static eheap: *const c_void; - } - println!("Binary sections:"); unsafe { - print_memory_range("boottext:\t", &boottext, &eboottext); - print_memory_range("text:\t\t", &text, &etext); - print_memory_range("rodata:\t", &rodata, &erodata); - print_memory_range("data:\t\t", &data, &edata); - print_memory_range("bss:\t\t", &bss, &end); - print_memory_range("heap:\t\t", &heap, &eheap); - print_memory_range("total:\t", &boottext, &end); + print_memory_range("boottext:\t", kmem::boottext_virtrange()); + print_memory_range("text:\t\t", kmem::text_virtrange()); + print_memory_range("rodata:\t", kmem::rodata_virtrange()); + print_memory_range("data:\t\t", kmem::data_virtrange()); + print_memory_range("bss:\t\t", kmem::bss_virtrange()); + print_memory_range("heap:\t\t", kmem::heap_virtrange()); + print_memory_range("total:\t", kmem::total_virtrange()); } } diff --git a/aarch64/src/vmalloc.rs b/aarch64/src/vmalloc.rs index 7630a28..0e0ecb7 100644 --- a/aarch64/src/vmalloc.rs +++ b/aarch64/src/vmalloc.rs @@ -5,7 +5,7 @@ use port::{ mcslock::{Lock, LockNode}, }; -use crate::kmem::kernel_heap_virtrange; +use crate::kmem::heap_virtrange; static VMALLOC: Lock> = Lock::new("vmalloc", None); @@ -15,7 +15,7 @@ struct VmAlloc { impl VmAlloc { fn new() -> Self { - let heap_range = kernel_heap_virtrange(); + let heap_range = heap_virtrange(); let quantum = 4096; Self { _heap_arena: Arena::new_with_static_range(