changed page allocation to happen on page fault for performance reasons

This commit is contained in:
2025-02-27 23:57:23 +00:00
parent 2915d0c879
commit 192100be7a
9 changed files with 144 additions and 61 deletions
+48 -20
View File
@@ -2,10 +2,16 @@ use linked_list_allocator::LockedHeap;
use x86_64::{
VirtAddr,
structures::paging::{
FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB, mapper::MapToError,
FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
mapper::{MapToError, MapperFlushAll},
},
};
use crate::{
drivers::memory::{FRAME_ALLOCATOR, OFFSET_PAGE_TABLE},
serial_print, serial_println,
};
/// We are currently using a linked list heap allocator which uses our underlying page allocator.
pub type FoundryAllocator = LockedHeap;
@@ -14,28 +20,50 @@ pub type FoundryAllocator = LockedHeap;
static ALLOCATOR: FoundryAllocator = FoundryAllocator::empty();
pub const HEAP_START: usize = 0x4444_4444_0000;
pub const HEAP_SIZE: usize = 1000 * 1024;
pub const HEAP_SIZE: usize = 1024 * 1024 * 1024;
/// Sets up the heap using the backing page frame allocator.
pub fn init_heap(
mapper: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
let range = {
let heap_start = VirtAddr::new(HEAP_START as u64);
let heap_end = heap_start + HEAP_SIZE as u64 - 1u64;
let heap_start_page = Page::<Size4KiB>::containing_address(heap_start);
let heap_end_page = Page::<Size4KiB>::containing_address(heap_end);
Page::range_inclusive(heap_start_page, heap_end_page)
};
pub fn init_heap() -> Result<(), MapToError<Size4KiB>> {
// let mut frame_allocator = if let Some(f) = FRAME_ALLOCATOR.get() {
// f.lock()
// } else {
// return Err(MapToError::FrameAllocationFailed);
// };
for page in range {
let frame = frame_allocator
.allocate_frame()
.ok_or(MapToError::FrameAllocationFailed)?;
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
unsafe { mapper.map_to(page, frame, flags, frame_allocator)?.flush() };
}
// let mut mapper = if let Some(m) = OFFSET_PAGE_TABLE.get() {
// m.lock()
// } else {
// return Err(MapToError::FrameAllocationFailed);
// };
// let range = {
// let heap_start = VirtAddr::new(HEAP_START as u64);
// let heap_end = heap_start + HEAP_SIZE as u64 - 1u64;
// let heap_start_page = Page::<Size4KiB>::containing_address(heap_start);
// let heap_end_page = Page::<Size4KiB>::containing_address(heap_end);
// Page::range_inclusive(heap_start_page, heap_end_page)
// };
// let usable_frames = frame_allocator.count_usable_frames();
// serial_println!("usable frames: {}", usable_frames);
// let mut i = 0;
// for page in range {
// i += 1;
// if i % 128 == 0 {
// serial_println!("allocated {} pages", i);
// }
// let frame = frame_allocator
// .allocate_frame()
// .ok_or(MapToError::FrameAllocationFailed)?;
// let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
// unsafe {
// // IMPORTANT: make sure to flush the mapper!!!!
// let _ = mapper.map_to(page, frame, flags, &mut *frame_allocator)?;
// }
// }
// MapperFlushAll::new().flush_all();
unsafe {
ALLOCATOR.lock().init(HEAP_START as *mut u8, HEAP_SIZE);