kernel: riscv: use SUM bit for debug visibility

Instead of remapping the page to allow the kernel to access a process'
memory, simply set the SUM bit. This makes it no longer an error for the
kernel to access userspace.

Keep setting the `W` bit when we poke, to allow for updating program
memory with breakpoints.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-05-11 15:11:17 +08:00
parent 874bb655df
commit 55d27cbf1b

View file

@ -1,10 +1,10 @@
// SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io> // SPDX-FileCopyrightText: 2020 Sean Cross <sean@xobs.io>
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use crate::mem::MemoryManager;
use crate::arch::process::InitialProcess; use crate::arch::process::InitialProcess;
use crate::mem::MemoryManager;
use core::fmt; use core::fmt;
use riscv::register::satp; use riscv::register::{satp, sstatus};
use xous_kernel::{MemoryFlags, PID}; use xous_kernel::{MemoryFlags, PID};
// pub const DEFAULT_STACK_TOP: usize = 0x8000_0000; // pub const DEFAULT_STACK_TOP: usize = 0x8000_0000;
@ -32,10 +32,7 @@ extern "C" {
} }
unsafe fn zeropage(s: *mut u32) { unsafe fn zeropage(s: *mut u32) {
let page = core::slice::from_raw_parts_mut( let page = core::slice::from_raw_parts_mut(s, PAGE_SIZE / core::mem::size_of::<u32>());
s,
PAGE_SIZE / core::mem::size_of::<u32>()
);
page.fill(0); page.fill(0);
} }
@ -356,8 +353,7 @@ impl MemoryMapping {
// Zero-out the new page // Zero-out the new page
let page_addr = l0pt_virt as *mut usize; let page_addr = l0pt_virt as *mut usize;
unsafe { zeropage(page_addr as *mut u32) unsafe { zeropage(page_addr as *mut u32) };
};
} }
let l0_pt = &mut unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) }; let l0_pt = &mut unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) };
@ -483,21 +479,22 @@ pub fn peek_memory<T>(addr: *mut T) -> Result<T, xous_kernel::Error> {
return Err(xous_kernel::Error::BadAddress); return Err(xous_kernel::Error::BadAddress);
} }
// Ensure the entry hasn't already been mapped. // Ensure the entry has already been mapped, and that we're allowed
if l0_pt.entries[vpn0] & 1 == 0 { // to read it.
if l0_pt.entries[vpn0] & (MMUFlags::R | MMUFlags::VALID).bits()
!= (MMUFlags::R | MMUFlags::VALID).bits()
{
return Err(xous_kernel::Error::BadAddress); return Err(xous_kernel::Error::BadAddress);
} }
// Strip the USER flag to the entry so we can read it // Enable supervisor access to user mode
l0_pt.entries[vpn0] &= !MMUFlags::USER.bits(); unsafe { sstatus::set_sum() };
unsafe { flush_mmu() };
// Perform the read // Perform the read
let val = unsafe { addr.read_volatile() }; let val = unsafe { addr.read_volatile() };
// Add the USER flag back to the entry // Remove supervisor access to user mode
l0_pt.entries[vpn0] |= MMUFlags::USER.bits(); unsafe { sstatus::clear_sum() };
unsafe { flush_mmu() };
Ok(val) Ok(val)
} }
@ -528,30 +525,35 @@ pub fn poke_memory<T>(addr: *mut T, val: T) -> Result<(), xous_kernel::Error> {
return Err(xous_kernel::Error::BadAddress); return Err(xous_kernel::Error::BadAddress);
} }
// Ensure the entry hasn't already been mapped. // Ensure the entry has been mapped.
if l0_pt.entries[vpn0] & 1 == 0 { if l0_pt.entries[vpn0] & MMUFlags::VALID.bits() == 0 {
return Err(xous_kernel::Error::BadAddress); return Err(xous_kernel::Error::BadAddress);
} }
// Ensure we're allowed to read it. // Ensure we're allowed to read it.
let was_writable = l0_pt.entries[vpn0] & MMUFlags::W.bits() != 0; let was_writable = l0_pt.entries[vpn0] & MMUFlags::W.bits() != 0;
// Strip the USER flag to the entry so we can read it // Add the WRITE bit, which allows us to patch things like
l0_pt.entries[vpn0] &= !MMUFlags::USER.bits(); // program code.
if !was_writable {
l0_pt.entries[vpn0] |= MMUFlags::W.bits(); l0_pt.entries[vpn0] |= MMUFlags::W.bits();
unsafe { flush_mmu() }; unsafe { flush_mmu() };
}
// Enable supervisor access to user mode
unsafe { sstatus::set_sum() };
// Perform the write // Perform the write
unsafe { addr.write_volatile(val) }; unsafe { addr.write_volatile(val) };
// Add the USER flag back to the entry // Remove supervisor access to user mode
l0_pt.entries[vpn0] |= MMUFlags::USER.bits(); unsafe { sstatus::clear_sum() };
// Strip the "writable" bit if it wasn't set before // Remove the WRITE bit if it wasn't previously set
if !was_writable { if !was_writable {
l0_pt.entries[vpn0] &= !MMUFlags::W.bits(); l0_pt.entries[vpn0] &= !MMUFlags::W.bits();
}
unsafe { flush_mmu() }; unsafe { flush_mmu() };
}
Ok(()) Ok(())
} }