Skip to content

Commit

Permalink
Working on vmem allocation
Browse files Browse the repository at this point in the history
Signed-off-by: Graham MacDonald <[email protected]>
  • Loading branch information
gmacd committed Jun 11, 2024
1 parent 48f8e54 commit 9e28700
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 99 deletions.
9 changes: 8 additions & 1 deletion aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn print_memory_info() {
let vc_mem = mailbox::get_vc_memory();
println!(" Video:\t{vc_mem} ({:#x})", vc_mem.size());

println!("Memory usage::");
println!("Memory usage:");
let (used, total) = pagealloc::usage_bytes();
println!(" Used:\t\t{used:#016x}");
println!(" Total:\t{total:#016x}");
Expand Down Expand Up @@ -128,6 +128,13 @@ pub extern "C" fn main9(dtb_va: usize) {

println!("looping now");

{
let test = vmalloc::alloc(1024);
println!("test alloc: {:p}", test);
let test2 = vmalloc::alloc(1024);
println!("test alloc: {:p}", test2);
}

#[allow(clippy::empty_loop)]
loop {}
}
Expand Down
6 changes: 3 additions & 3 deletions aarch64/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::{
kmem::{
from_ptr_to_physaddr, kernel_bss_physrange, kernel_data_physrange, kernel_heap_physrange,
kernel_text_physrange, physaddr_as_ptr_mut, physaddr_as_virt,
from_ptr_to_physaddr, heap_virtrange, kernel_bss_physrange, kernel_data_physrange,
kernel_heap_physrange, kernel_text_physrange, physaddr_as_ptr_mut, physaddr_as_virt,
},
pagealloc,
registers::rpi_mmio,
Expand Down Expand Up @@ -469,7 +469,7 @@ fn print_pte(indent: usize, i: usize, level: Level, pte: Entry) {

pub unsafe fn init(kpage_table: &mut PageTable, dtb_range: PhysRange, available_mem: PhysRange) {
pagealloc::init_page_allocator();
vmalloc::init();
vmalloc::init(heap_virtrange());

// We use recursive page tables, but we have to be careful in the init call,
// since the kpage_table is not currently pointed to by ttbr1_el1. Any
Expand Down
32 changes: 23 additions & 9 deletions aarch64/src/vmalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,54 @@ use core::mem::MaybeUninit;
use port::{
boundarytag::Arena,
mcslock::{Lock, LockNode},
mem::VirtRange,
};

use crate::kmem::heap_virtrange;

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

static mut EARLY_TAGS_PAGE: [u8; 4096] = [0; 4096];

struct VmAlloc {
_heap_arena: Arena,
heap_arena: Arena,
}

impl VmAlloc {
fn new() -> Self {
let heap_range = heap_virtrange();
fn new(heap_range: VirtRange) -> Self {
let quantum = 4096;

let early_tags_ptr = unsafe { &EARLY_TAGS_PAGE as *const _ as usize };
let early_tags_size = unsafe { EARLY_TAGS_PAGE.len() };
let early_tags_range = VirtRange::with_len(early_tags_ptr, early_tags_size);

Self {
_heap_arena: Arena::new_with_static_range(
heap_arena: Arena::new_with_static_range(
"heap",
heap_range.start(),
heap_range.size(),
quantum,
heap_range,
early_tags_range,
),
}
}
}

pub fn init() {
pub fn init(heap_range: VirtRange) {
let node = LockNode::new();
let mut vmalloc = VMALLOC.lock(&node);
*vmalloc = Some({
static mut MAYBE_VMALLOC: MaybeUninit<VmAlloc> = MaybeUninit::uninit();
unsafe {
MAYBE_VMALLOC.write(VmAlloc::new());
MAYBE_VMALLOC.write(VmAlloc::new(heap_range));
MAYBE_VMALLOC.assume_init_mut()
}
});
}

// TODO Add VmFlag (BestFit, InstantFit, NextFit)

pub fn alloc(size: usize) -> *mut u8 {
let node = LockNode::new();
let mut lock = VMALLOC.lock(&node);
let vmalloc = lock.as_deref_mut().unwrap();
vmalloc.heap_arena.alloc(size)
}
Loading

0 comments on commit 9e28700

Please sign in to comment.