forked from AbleOS/ableos
47 lines
1.3 KiB
Rust
47 lines
1.3 KiB
Rust
use linked_list_allocator::LockedHeap;
|
|
use x86_64::{
|
|
structures::paging::{
|
|
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
|
|
},
|
|
VirtAddr,
|
|
};
|
|
|
|
use crate::allocator::{HEAP_SIZE, HEAP_START};
|
|
|
|
#[global_allocator]
|
|
static ALLOCATOR: LockedHeap = LockedHeap::empty();
|
|
|
|
pub unsafe fn init_alloc() -> Result<(), MapToError<Size4KiB>> {
|
|
log::info!("Initialising kernel heap allocator");
|
|
let page_range = Page::range_inclusive(
|
|
Page::containing_address(VirtAddr::new(HEAP_START as u64)),
|
|
Page::containing_address(VirtAddr::new(HEAP_START as u64) + HEAP_SIZE - 1u64),
|
|
);
|
|
|
|
let mut frame_allocator = super::memory::FRAME_ALLOC
|
|
.get()
|
|
.expect("frame allocator is not initialised")
|
|
.lock();
|
|
|
|
let mut mapper = super::memory::PAGE_TABLE
|
|
.get()
|
|
.expect("page table is not initialised")
|
|
.lock();
|
|
|
|
for page in page_range {
|
|
let frame = frame_allocator
|
|
.allocate_frame()
|
|
.ok_or(MapToError::FrameAllocationFailed)?;
|
|
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
|
|
mapper
|
|
.map_to(page, frame, flags, &mut *frame_allocator)?
|
|
.flush();
|
|
}
|
|
|
|
ALLOCATOR
|
|
.lock()
|
|
.init(crate::allocator::HEAP_START, crate::allocator::HEAP_SIZE);
|
|
|
|
Ok(())
|
|
}
|