kernel: add gdb support to riscv

This adds various primitives that can be used to attach a debugger to
the kernel.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-06-21 14:13:01 +08:00
parent 0332959068
commit 57b502e2c6
2 changed files with 107 additions and 0 deletions

View file

@ -308,6 +308,99 @@ pub fn hand_page_to_user(virt: *mut u8) -> Result<(), xous_kernel::Error> {
Ok(())
}
pub fn peek_memory<T>(addr: *mut T) -> Result<T, xous_kernel::Error> {
let virt = addr as usize;
let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1);
let vpo = (virt >> 0) & ((1 << 12) - 1);
assert!(vpn1 < 1024);
assert!(vpn0 < 1024);
assert!(vpo < 4096);
// The root (l1) pagetable is defined to be mapped into our virtual
// address space at this address.
let l1_pt = unsafe { &mut (*(PAGE_TABLE_ROOT_OFFSET as *mut RootPageTable)) };
let ref mut l1_pt = l1_pt.entries;
// Subsequent pagetables are defined as being mapped starting at
// PAGE_TABLE_OFFSET
let l0pt_virt = PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE;
let ref mut l0_pt = unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) };
// If the level 1 pagetable doesn't exist, then this address isn't valid.
if l1_pt[vpn1] & MMUFlags::VALID.bits() == 0 {
return Err(xous_kernel::Error::BadAddress);
}
// Ensure the entry hasn't already been mapped.
if l0_pt.entries[vpn0] & 1 == 0 {
return Err(xous_kernel::Error::BadAddress);
}
// Strip the USER flag to the entry so we can read it
l0_pt.entries[vpn0] &= !MMUFlags::USER.bits();
unsafe { flush_mmu() };
// Perform the read
let val = unsafe { addr.read_volatile() };
// Add the USER flag back to the entry
l0_pt.entries[vpn0] |= MMUFlags::USER.bits();
unsafe { flush_mmu() };
Ok(val)
}
pub fn poke_memory<T>(addr: *mut T, val: T) -> Result<(), xous_kernel::Error> {
let virt = addr as usize;
let vpn1 = (virt >> 22) & ((1 << 10) - 1);
let vpn0 = (virt >> 12) & ((1 << 10) - 1);
let vpo = (virt >> 0) & ((1 << 12) - 1);
assert!(vpn1 < 1024);
assert!(vpn0 < 1024);
assert!(vpo < 4096);
// The root (l1) pagetable is defined to be mapped into our virtual
// address space at this address.
let l1_pt = unsafe { &mut (*(PAGE_TABLE_ROOT_OFFSET as *mut RootPageTable)) };
let ref mut l1_pt = l1_pt.entries;
// Subsequent pagetables are defined as being mapped starting at
// PAGE_TABLE_OFFSET
let l0pt_virt = PAGE_TABLE_OFFSET + vpn1 * PAGE_SIZE;
let ref mut l0_pt = unsafe { &mut (*(l0pt_virt as *mut LeafPageTable)) };
// If the level 1 pagetable doesn't exist, then this address isn't valid.
if l1_pt[vpn1] & MMUFlags::VALID.bits() == 0 {
Err(xous_kernel::Error::BadAddress)?;
}
// Ensure the entry hasn't already been mapped.
if l0_pt.entries[vpn0] & 1 == 0 {
Err(xous_kernel::Error::BadAddress)?;
}
// Ensure we're allowed to read it.
if l0_pt.entries[vpn0] & MMUFlags::W.bits() == 0 {
Err(xous_kernel::Error::AccessDenied)?;
}
// Strip the USER flag to the entry so we can read it
l0_pt.entries[vpn0] &= !MMUFlags::USER.bits();
unsafe { flush_mmu() };
// Perform the write
unsafe { addr.write_volatile(val) };
// Add the USER flag back to the entry
l0_pt.entries[vpn0] |= MMUFlags::USER.bits();
unsafe { flush_mmu() };
Ok(())
}
/// Map the given page to the specified process table. If necessary,
/// allocate a new page.
///

View file

@ -225,6 +225,20 @@ impl Process {
&process.threads[thread]
}
pub fn for_each_thread_mut<F>(&self, mut op: F)
where
F: FnMut(TID, &Thread),
{
let process = unsafe { &mut *PROCESS };
for (idx, thread) in process.threads.iter_mut().enumerate() {
// Ignore threads that have no PC, and ignore the ISR thread
if thread.sepc == 0 || idx == IRQ_TID {
continue;
}
op(idx, thread);
}
}
pub fn find_free_thread(&self) -> Option<TID> {
let process = unsafe { &mut *PROCESS };
let start_tid = process.last_tid_allocated as usize;