Next Generation WASM Microkernel Operating System
at trap_handler 46 lines 1.5 kB view raw
1// Copyright 2025 Jonas Kruckenberg 2// 3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or 4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or 5// http://opensource.org/licenses/MIT>, at your option. This file may not be 6// copied, modified, or distributed except according to those terms. 7 8use crate::mem::bootstrap_alloc::BootstrapAllocator; 9use crate::{INITIAL_HEAP_SIZE_PAGES, arch}; 10use core::alloc::Layout; 11use core::range::Range; 12use loader_api::BootInfo; 13use talc::{ErrOnOom, Span, Talc, Talck}; 14 15#[global_allocator] 16static KERNEL_ALLOCATOR: Talck<spin::Mutex<()>, ErrOnOom> = Talc::new(ErrOnOom).lock(); 17 18pub fn init(boot_alloc: &mut BootstrapAllocator, boot_info: &BootInfo) { 19 let layout = 20 Layout::from_size_align(INITIAL_HEAP_SIZE_PAGES * arch::PAGE_SIZE, arch::PAGE_SIZE) 21 .unwrap(); 22 23 let phys = boot_alloc.allocate_contiguous(layout).unwrap(); 24 25 let virt = { 26 let start = boot_info 27 .physical_address_offset 28 .checked_add(phys.get()) 29 .unwrap(); 30 31 Range::from(start..start.checked_add(layout.size()).unwrap()) 32 }; 33 tracing::debug!("Kernel Heap: {virt:#x?}"); 34 35 let mut alloc = KERNEL_ALLOCATOR.lock(); 36 let span = Span::from_base_size( 37 virt.start as *mut u8, 38 virt.end.checked_sub(virt.start).unwrap(), 39 ); 40 41 // Safety: just allocated the memory region 42 unsafe { 43 let old_heap = alloc.claim(span).unwrap(); 44 alloc.extend(old_heap, span); 45 } 46}