2019-11-14 14:26:37 +01:00
|
|
|
use polling_serial::serial_println;
|
|
|
|
use x86_64::{
|
|
|
|
structures::paging::{
|
|
|
|
mapper::MapToError, FrameAllocator, Mapper, Page, PageTableFlags, Size4KiB,
|
|
|
|
},
|
|
|
|
VirtAddr,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const HEAP_START: usize = 0x4444_4444_0000;
|
|
|
|
pub const HEAP_SIZE: usize = 0x1 << 20; // 1MB Heap
|
|
|
|
|
|
|
|
pub fn init_heap(
|
|
|
|
mapper: &mut impl Mapper<Size4KiB>,
|
|
|
|
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
2020-02-19 15:08:24 +01:00
|
|
|
) -> Result<(), MapToError<Size4KiB>> {
|
2019-11-14 14:26:37 +01:00
|
|
|
let page_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::containing_address(heap_start);
|
|
|
|
let heap_end_page = Page::containing_address(heap_end);
|
|
|
|
Page::range_inclusive(heap_start_page, heap_end_page)
|
|
|
|
};
|
|
|
|
|
2020-02-04 12:00:03 +01:00
|
|
|
for (i, page) in page_range.enumerate() {
|
2019-11-14 14:26:37 +01:00
|
|
|
let frame = frame_allocator
|
|
|
|
.allocate_frame()
|
|
|
|
.ok_or(MapToError::FrameAllocationFailed)?;
|
|
|
|
let flags = PageTableFlags::PRESENT | PageTableFlags::WRITABLE;
|
2020-02-19 15:08:24 +01:00
|
|
|
mapper.map_to(page, frame, flags, frame_allocator)?.flush();
|
2019-11-14 14:26:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
super::ALLOCATOR.lock().init(HEAP_START, HEAP_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|