Merge branch 'main' of github.com:betrusted-io/xous-core into main

This commit is contained in:
bunnie 2023-05-17 04:12:46 +08:00
commit b3542d2add
2 changed files with 74 additions and 40 deletions

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(())
} }

View file

@ -5,6 +5,7 @@ use gdbstub::target::ext::base::single_register_access::SingleRegisterAccessOps;
use gdbstub::target::TargetResult; use gdbstub::target::TargetResult;
use super::XousTarget; use super::XousTarget;
use core::convert::TryInto;
impl MultiThreadBase for XousTarget { impl MultiThreadBase for XousTarget {
fn read_registers( fn read_registers(
@ -107,10 +108,33 @@ impl MultiThreadBase for XousTarget {
.unwrap() .unwrap()
.activate() .activate()
.unwrap(); .unwrap();
if data.len() == 2 && (start_addr & 1) == 0 {
let val = crate::arch::mem::peek_memory(start_addr as *mut u16).unwrap_or(0);
for (dest, src) in data.iter_mut().zip(val.to_le_bytes()) {
*dest = src;
}
} else if data.len() == 4 && (start_addr & 3) == 0 {
let val = crate::arch::mem::peek_memory(start_addr as *mut u32).unwrap_or(0);
for (dest, src) in data.iter_mut().zip(val.to_le_bytes()) {
*dest = src;
}
} else if (data.len() & 3) == 0 && (start_addr & 3) == 0 {
let mut current_addr = current_addr;
for word in data.chunks_mut(4) {
let bytes = crate::arch::mem::peek_memory(current_addr as *mut u32)
.unwrap_or(0)
.to_le_bytes();
for (dest, src) in word.iter_mut().zip(bytes) {
*dest = src;
}
current_addr += 4;
}
} else {
for (offset, b) in data.iter_mut().enumerate() { for (offset, b) in data.iter_mut().enumerate() {
*b = crate::arch::mem::peek_memory((current_addr + offset) as *mut u8) *b = crate::arch::mem::peek_memory((current_addr + offset) as *mut u8)
.unwrap_or(0xff); .unwrap_or(0xff);
// println!("<< Peek {:02x} @ {:08x}", *b, current_addr); }
} }
// Restore the previous PID // Restore the previous PID
@ -145,13 +169,21 @@ impl MultiThreadBase for XousTarget {
.unwrap() .unwrap()
.activate() .activate()
.unwrap(); .unwrap();
if data.len() == 2 && (start_addr & 1) == 0 {
let val = u16::from_le_bytes(data.try_into().unwrap());
crate::arch::mem::poke_memory(start_addr as *mut u16, val).ok();
} else if data.len() == 4 && (start_addr & 3) == 0 {
let val = u32::from_le_bytes(data.try_into().unwrap());
crate::arch::mem::poke_memory(start_addr as *mut u32, val).ok();
} else {
data.iter().for_each(|b| { data.iter().for_each(|b| {
if let Err(_e) = crate::arch::mem::poke_memory(current_addr as *mut u8, *b) { if let Err(_e) = crate::arch::mem::poke_memory(current_addr as *mut u8, *b) {
// panic!("couldn't poke memory: {:?}", _e); // panic!("couldn't poke memory: {:?}", _e);
} }
// println!("Poked {:02x} @ {:08x}", *b, current_addr);
current_addr += 1; current_addr += 1;
}); });
}
// Restore the previous PID // Restore the previous PID
system_services system_services