2020-01-21 09:04:09 +00:00
|
|
|
use crate::definitions::{MemoryAddress, XousError, XousPid};
|
2019-12-25 12:11:15 +00:00
|
|
|
use crate::mem::MemoryManager;
|
2020-01-21 09:04:09 +00:00
|
|
|
use crate::{filled_array, print, println};
|
2020-01-21 10:14:13 +00:00
|
|
|
use vexriscv::register::{mstatus, satp};
|
2019-12-25 12:11:15 +00:00
|
|
|
|
|
|
|
const MAX_PROCESS_COUNT: usize = 256;
|
|
|
|
|
2020-01-21 09:04:09 +00:00
|
|
|
pub struct Process {
|
|
|
|
pub satp: usize,
|
2019-12-25 12:11:15 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 06:43:11 +00:00
|
|
|
impl core::fmt::Debug for Process {
|
|
|
|
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
|
2020-01-21 09:04:09 +00:00
|
|
|
write!(
|
|
|
|
fmt,
|
|
|
|
"Process (satp: 0x{:08x}, mode: {}, ASID: {}, PPN: {:08x})",
|
|
|
|
self.satp,
|
|
|
|
self.satp >> 31,
|
|
|
|
self.satp >> 22 & ((1 << 9) - 1),
|
|
|
|
(self.satp >> 0 & ((1 << 22) - 1)) << 9,
|
|
|
|
)
|
2020-01-20 06:43:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 12:11:15 +00:00
|
|
|
pub struct ProcessTable {
|
|
|
|
processes: [Process; MAX_PROCESS_COUNT],
|
|
|
|
}
|
|
|
|
|
2020-01-21 10:14:13 +00:00
|
|
|
extern "Rust" {
|
|
|
|
fn start_kmain(
|
|
|
|
kmain: extern "Rust" fn(MemoryManager, ProcessTable) -> !,
|
|
|
|
mm: MemoryManager,
|
|
|
|
pt: ProcessTable,
|
|
|
|
) -> !;
|
|
|
|
}
|
2020-01-21 12:05:39 +00:00
|
|
|
extern "C" {
|
|
|
|
fn read_satp() -> usize;
|
|
|
|
}
|
2020-01-21 10:14:13 +00:00
|
|
|
|
2019-12-25 12:11:15 +00:00
|
|
|
impl ProcessTable {
|
2020-01-21 10:14:13 +00:00
|
|
|
pub fn new(mut mm: MemoryManager, kmain: fn(MemoryManager, ProcessTable) -> !) -> ! {
|
2019-12-25 12:11:15 +00:00
|
|
|
let mut pt = ProcessTable {
|
2020-01-21 09:04:09 +00:00
|
|
|
processes: filled_array![Process { satp: 0 }; 256],
|
2019-12-25 12:11:15 +00:00
|
|
|
};
|
|
|
|
|
2020-01-21 09:04:09 +00:00
|
|
|
// Allocate a root page table for PID 1. Also mark the "ASID" as "1"
|
|
|
|
// for "PID 1"
|
|
|
|
let root_page = mm.alloc_page(1).unwrap().get();
|
2020-01-21 12:05:39 +00:00
|
|
|
pt.processes[1].satp = (root_page >> 12) | (1 << 22);
|
2020-01-21 10:14:13 +00:00
|
|
|
mm.create_identity(&pt.processes[1])
|
2020-01-21 09:04:09 +00:00
|
|
|
.expect("Unable to create identity mapping");
|
|
|
|
println!("PID 1: {:?} root page @ {:08x}", pt.processes[1], root_page);
|
|
|
|
println!("Enabling MMU...");
|
|
|
|
unsafe {
|
|
|
|
// Set the MMU pointer to our identity mapping
|
|
|
|
satp::set(
|
|
|
|
satp::Mode::Sv32,
|
|
|
|
(pt.processes[1].satp >> 22) & ((1 << 9) - 1),
|
|
|
|
(pt.processes[1].satp >> 0) & ((1 << 22) - 1),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Switch to Supervisor mode
|
|
|
|
mstatus::set_mpp(mstatus::MPP::Supervisor);
|
|
|
|
};
|
2020-01-21 10:14:13 +00:00
|
|
|
println!("MMU enabled, jumping to kmain");
|
2020-01-21 12:05:39 +00:00
|
|
|
println!("SATP: {:08x}", unsafe { read_satp() });
|
2020-01-21 10:14:13 +00:00
|
|
|
unsafe { start_kmain(kmain, mm, pt) }
|
2019-12-25 12:11:15 +00:00
|
|
|
}
|
|
|
|
}
|