Skip to content

Commit

Permalink
Tidy mem ranges
Browse files Browse the repository at this point in the history
Signed-off-by: Graham MacDonald <[email protected]>
  • Loading branch information
gmacd committed Mar 29, 2024
1 parent 3e587e3 commit 615f45b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 38 deletions.
80 changes: 72 additions & 8 deletions aarch64/src/kmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down Expand Up @@ -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())
}

Expand Down
40 changes: 12 additions & 28 deletions aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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());
}
}

Expand Down
4 changes: 2 additions & 2 deletions aarch64/src/vmalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use port::{
mcslock::{Lock, LockNode},
};

use crate::kmem::kernel_heap_virtrange;
use crate::kmem::heap_virtrange;

static VMALLOC: Lock<Option<&'static mut VmAlloc>> = Lock::new("vmalloc", None);

Expand All @@ -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(
Expand Down

0 comments on commit 615f45b

Please sign in to comment.