|
|
|
@ -4,13 +4,14 @@ use super::riscv::{addr::*, register::sstatus};
|
|
|
|
|
use ucore_memory::PAGE_SIZE;
|
|
|
|
|
|
|
|
|
|
pub fn init() {
|
|
|
|
|
#[repr(align(4096))]
|
|
|
|
|
#[repr(align(4096))] // align the PageData struct to 4096 bytes
|
|
|
|
|
struct PageData([u8; PAGE_SIZE]);
|
|
|
|
|
static PAGE_TABLE_ROOT: PageData = PageData([0; PAGE_SIZE]);
|
|
|
|
|
|
|
|
|
|
unsafe { sstatus::set_sum(); } // Allow user memory access
|
|
|
|
|
let frame = Frame::of_addr(PhysAddr::new(&PAGE_TABLE_ROOT as *const _ as u32));
|
|
|
|
|
super::paging::setup_page_table(frame);
|
|
|
|
|
let frame = Frame::of_addr(PhysAddr::new(&PAGE_TABLE_ROOT as *const _ as u32));
|
|
|
|
|
super::paging::setup_page_table(frame); // set up page table
|
|
|
|
|
// initialize heap and Frame allocator
|
|
|
|
|
init_frame_allocator();
|
|
|
|
|
remap_the_kernel();
|
|
|
|
|
init_heap();
|
|
|
|
@ -23,9 +24,19 @@ fn init_frame_allocator() {
|
|
|
|
|
|
|
|
|
|
let mut ba = FRAME_ALLOCATOR.lock();
|
|
|
|
|
use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE};
|
|
|
|
|
// keep memory for the kernel heap and set other physical memory available in BitAlloc
|
|
|
|
|
ba.insert(to_range(KERNEL_HEAP_OFFSET + KERNEL_HEAP_SIZE, MEMORY_END));
|
|
|
|
|
info!("FrameAllocator init end");
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* @param:
|
|
|
|
|
* start: start address
|
|
|
|
|
* end: end address
|
|
|
|
|
* @brief:
|
|
|
|
|
* transform the memory address to the page number
|
|
|
|
|
* @retval:
|
|
|
|
|
* the page number range from start address to end address
|
|
|
|
|
*/
|
|
|
|
|
fn to_range(start: usize, end: usize) -> Range<usize> {
|
|
|
|
|
let page_start = (start - MEMORY_OFFSET) / PAGE_SIZE;
|
|
|
|
|
let page_end = (end - MEMORY_OFFSET - 1) / PAGE_SIZE + 1;
|
|
|
|
@ -35,6 +46,7 @@ fn init_frame_allocator() {
|
|
|
|
|
|
|
|
|
|
fn remap_the_kernel() {
|
|
|
|
|
use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE};
|
|
|
|
|
// set up kernel stack
|
|
|
|
|
let kstack = Stack {
|
|
|
|
|
top: bootstacktop as usize,
|
|
|
|
|
bottom: bootstack as usize + PAGE_SIZE,
|
|
|
|
|