kernel: riscv: don't crash when mapping invalid memory

Previously, the kernel would dutifully try to map memory that was in the
kernel space. It did this by investigating the kernel memory offset,
which immediately causes a double-fault.

Check to make sure a userspace process is not trying to map kernel
memory before checking the valid page.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-06-30 19:45:08 +08:00
parent dcc964c3c1
commit 426fe4b91f

View file

@ -259,6 +259,10 @@ impl MemoryMapping {
PID::new((self.satp >> 22 & ((1 << 9) - 1)) as _).unwrap()
}
pub fn is_kernel(&self) -> bool {
self.get_pid().get() == 1
}
/// Set this mapping as the systemwide mapping.
/// **Note:** This should only be called from an interrupt in the
/// kernel, which should be mapped into every possible address space.
@ -288,7 +292,7 @@ impl MemoryMapping {
);
// Page 1023 is only available to PID1
if i == 1023 && self.get_pid().get() != 1 {
if i == 1023 && !self.is_kernel() {
println!(" <unavailable>");
continue;
}
@ -872,6 +876,10 @@ pub fn virt_to_phys(virt: usize) -> Result<usize, xous_kernel::Error> {
}
pub fn ensure_page_exists_inner(address: usize) -> Result<usize, xous_kernel::Error> {
// Disallow mapping memory outside of user land
if ! MemoryMapping::current().is_kernel() && address >= USER_AREA_END {
return Err(xous_kernel::Error::OutOfMemory);
}
let virt = address & !0xfff;
let entry = crate::arch::mem::pagetable_entry(virt).or(Err(xous_kernel::Error::BadAddress))?;
// let entry = crate::arch::mem::pagetable_entry(virt).or_else(|e| {