mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
kernel: implement gdb server
The gdb server runs on an additional serial port and provides process-level debugging to the kernel. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
3469c292ec
commit
7c2ffbb34e
12 changed files with 6604 additions and 81 deletions
|
|
@ -24,7 +24,7 @@ xous-kernel = { package = "xous", version = "0.9.37", features = [
|
|||
|
||||
[target.'cfg(target_arch = "riscv32")'.dependencies]
|
||||
riscv = { version = "0.5.6", package = "xous-riscv" } # source is in "../imports/riscv-0.5.6"
|
||||
gdbstub = { version = "0.6.4", optional = true, default-features = false }
|
||||
gdbstub = { version = "0.6.5", optional = true, default-features = false }
|
||||
gdbstub_arch = { version = "0.2.4", optional = true, default-features = false }
|
||||
|
||||
[target.'cfg(target_arch = "arm")'.dependencies]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use gdbstub::common::Signal;
|
||||
use gdbstub::common::{Signal, Tid};
|
||||
use gdbstub::conn::Connection;
|
||||
use gdbstub::stub::state_machine::GdbStubStateMachine;
|
||||
use gdbstub::stub::{GdbStubBuilder, GdbStubError, MultiThreadStopReason};
|
||||
|
|
@ -8,19 +8,23 @@ use crate::io::SerialRead;
|
|||
use crate::platform::precursor::gdbuart::GdbUart;
|
||||
|
||||
mod breakpoints;
|
||||
mod current_active_pid;
|
||||
mod extended_mode;
|
||||
mod monitor;
|
||||
mod multi_thread_base;
|
||||
mod multi_thread_resume;
|
||||
mod multi_thread_single_step;
|
||||
mod single_register_access;
|
||||
mod target;
|
||||
|
||||
#[cfg(target_arch = "riscv32")]
|
||||
#[path = "gdb/riscv.rs"]
|
||||
mod cpu;
|
||||
|
||||
pub struct XousTarget {
|
||||
pid: Option<xous_kernel::PID>,
|
||||
|
||||
/// When doing a `stepi` we patch the instruction with an illegal instruction
|
||||
/// and store the previous value here.
|
||||
patched_instruction: usize,
|
||||
inner: cpu::XousTargetInner,
|
||||
}
|
||||
|
||||
pub struct XousDebugState<'a> {
|
||||
|
|
@ -58,7 +62,7 @@ impl XousTarget {
|
|||
pub fn new() -> XousTarget {
|
||||
XousTarget {
|
||||
pid: None,
|
||||
patched_instruction: 0,
|
||||
inner: cpu::XousTargetInner::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -85,9 +89,10 @@ fn ensure_can_accept_characters_inner<'a, T: Target + ProcessPid, C: Connection>
|
|||
GdbStubStateMachine::Idle(_) | GdbStubStateMachine::Running(_) => Some(machine),
|
||||
GdbStubStateMachine::CtrlCInterrupt(gdb_stm_inner) => {
|
||||
if let Some(pid) = target.pid() {
|
||||
// println!("Starting debug on process {:?}", pid);
|
||||
crate::services::SystemServices::with_mut(|system_services| {
|
||||
system_services.pause_process_for_debug(pid).unwrap()
|
||||
if let Err(e) = system_services.pause_process_for_debug(pid) {
|
||||
println!("Unable to pause process {:?} for debug: {:?}", pid, e);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
println!("No process specified! Not debugging");
|
||||
|
|
@ -175,6 +180,11 @@ fn process_character(byte: u8) {
|
|||
}
|
||||
};
|
||||
|
||||
// If the user just hit Ctrl-C, then remove the pending interrupt that may or may not exist.
|
||||
if let GdbStubStateMachine::CtrlCInterrupt(_) = &new_server {
|
||||
target.unpatch_stepi(Tid::new(1).unwrap()).ok();
|
||||
}
|
||||
|
||||
let Some(server) = ensure_can_accept_characters(new_server, &mut target) else {
|
||||
println!("Couldn't convert GDB into a state that accepts characters");
|
||||
return;
|
||||
|
|
@ -183,8 +193,42 @@ fn process_character(byte: u8) {
|
|||
unsafe { GDB_STATE = Some(XousDebugState { target, server }) };
|
||||
}
|
||||
|
||||
pub fn report_terminated(pid: xous_kernel::PID) {
|
||||
println!("Reporting that process {:?} has terminated!", pid);
|
||||
pub fn report_stop(_pid: xous_kernel::PID, tid: xous_kernel::TID, _pc: usize) {
|
||||
let Some(XousDebugState {
|
||||
mut target,
|
||||
server: gdb,
|
||||
}) = (unsafe { GDB_STATE.take() }) else {
|
||||
println!("No GDB!");
|
||||
return;
|
||||
};
|
||||
|
||||
target.unpatch_stepi(Tid::new(tid).unwrap()).ok();
|
||||
|
||||
let GdbStubStateMachine::Running(inner) = gdb else {
|
||||
println!("GDB state machine was in an invalid state");
|
||||
return;
|
||||
};
|
||||
|
||||
let Ok(new_gdb) = inner.report_stop(
|
||||
&mut target,
|
||||
MultiThreadStopReason::SignalWithThread {
|
||||
signal: Signal::EXC_BREAKPOINT,
|
||||
tid: Tid::new(tid).unwrap(),
|
||||
}
|
||||
) else {
|
||||
println!("Unable to report stop");
|
||||
return;
|
||||
};
|
||||
|
||||
unsafe {
|
||||
GDB_STATE = Some(XousDebugState {
|
||||
target,
|
||||
server: new_gdb,
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
pub fn report_terminated(_pid: xous_kernel::PID) {
|
||||
let Some(XousDebugState {
|
||||
mut target,
|
||||
server: gdb,
|
||||
|
|
|
|||
|
|
@ -1,29 +1,24 @@
|
|||
use gdbstub::target::ext::breakpoints::Breakpoints;
|
||||
use gdbstub::target::TargetResult;
|
||||
use gdbstub::target::ext::breakpoints::{Breakpoints, SwBreakpoint, SwBreakpointOps};
|
||||
|
||||
use super::XousTarget;
|
||||
|
||||
impl Breakpoints for XousTarget {
|
||||
// fn support_sw_breakpoint(
|
||||
// &mut self,
|
||||
// ) -> Option<target::ext::breakpoints::SwBreakpointOps<'_, Self>> {
|
||||
// Some(self)
|
||||
// }
|
||||
/// Indicate that we do not support software breakpoints. If we ever decide
|
||||
/// to add breakpoint support, we will need to implement this ourselves. However,
|
||||
/// by reporting `None` here we allow GDB to manage breakpoints for us.
|
||||
fn support_sw_breakpoint(&mut self) -> Option<SwBreakpointOps<'_, Self>> {
|
||||
// Some(self)
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// impl target::ext::breakpoints::SwBreakpoint for XousTarget {
|
||||
// fn add_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||
// println!(
|
||||
// "GDB asked us to add a software breakpoint at {:08x} of kind {:?}",
|
||||
// _addr, _kind
|
||||
// );
|
||||
// Ok(true)
|
||||
// }
|
||||
impl SwBreakpoint for XousTarget {
|
||||
fn add_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||
Ok(false)
|
||||
}
|
||||
|
||||
// fn remove_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||
// println!(
|
||||
// "GDB asked us to remove a software breakpoint at {:08x} of kind {:?}",
|
||||
// _addr, _kind
|
||||
// );
|
||||
// Ok(true)
|
||||
// }
|
||||
// }
|
||||
fn remove_sw_breakpoint(&mut self, _addr: u32, _kind: usize) -> TargetResult<bool, Self> {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
29
kernel/src/debug/gdb/current_active_pid.rs
Normal file
29
kernel/src/debug/gdb/current_active_pid.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use gdbstub::common::Pid;
|
||||
use gdbstub::target::ext::extended_mode::CurrentActivePid;
|
||||
|
||||
use super::XousTarget;
|
||||
|
||||
// https://users.rust-lang.org/t/compile-time-const-unwrapping/51619/7
|
||||
//
|
||||
// This works from Rust 1.46.0 onwards, which stabilized branching and looping
|
||||
// in const contexts.
|
||||
macro_rules! unwrap {
|
||||
($e:expr $(,)*) => {
|
||||
match $e {
|
||||
::core::option::Option::Some(x) => x,
|
||||
::core::option::Option::None => {
|
||||
["tried to unwrap a None"][99];
|
||||
loop {}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// (Internal) The fake Pid reported to GDB when running in multi-threaded mode.
|
||||
const FAKE_PID: Pid = unwrap!(Pid::new(1));
|
||||
|
||||
impl CurrentActivePid for XousTarget {
|
||||
fn current_active_pid(&mut self) -> Result<Pid, Self::Error> {
|
||||
Ok(self.pid.map(|v| v.into()).unwrap_or(FAKE_PID))
|
||||
}
|
||||
}
|
||||
6081
kernel/src/debug/gdb/disasm.rs
Normal file
6081
kernel/src/debug/gdb/disasm.rs
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -1,13 +1,15 @@
|
|||
use gdbstub::common::{Pid, Tid};
|
||||
use gdbstub::target;
|
||||
use gdbstub::target::ext::extended_mode::{AttachKind, ShouldTerminate};
|
||||
use gdbstub::target::ext::extended_mode::{AttachKind, ExtendedMode, ShouldTerminate};
|
||||
use gdbstub::target::{TargetError, TargetResult};
|
||||
|
||||
use super::XousTarget;
|
||||
use core::convert::TryInto;
|
||||
|
||||
impl target::ext::extended_mode::ExtendedMode for XousTarget {
|
||||
fn attach(&mut self, new_pid: gdbstub::common::Pid) -> TargetResult<(), Self> {
|
||||
impl ExtendedMode for XousTarget {
|
||||
fn attach(&mut self, new_pid: Pid) -> TargetResult<(), Self> {
|
||||
if let Some(previous_pid) = self.pid.take() {
|
||||
self.unpatch_stepi(Tid::new(1).unwrap()).ok();
|
||||
crate::services::SystemServices::with_mut(|system_services| {
|
||||
system_services
|
||||
.resume_process_from_debug(previous_pid)
|
||||
|
|
@ -19,7 +21,7 @@ impl target::ext::extended_mode::ExtendedMode for XousTarget {
|
|||
if new_pid.get() == 1 {
|
||||
println!("Kernel cannot debug itself");
|
||||
self.pid = None;
|
||||
return Ok(());
|
||||
return Err(TargetError::NonFatal);
|
||||
}
|
||||
|
||||
self.pid = new_pid.try_into().map(|v| Some(v)).unwrap_or(None);
|
||||
|
|
@ -43,7 +45,7 @@ impl target::ext::extended_mode::ExtendedMode for XousTarget {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn kill(&mut self, pid: Option<gdbstub::common::Pid>) -> TargetResult<ShouldTerminate, Self> {
|
||||
fn kill(&mut self, pid: Option<Pid>) -> TargetResult<ShouldTerminate, Self> {
|
||||
println!("GDB sent a kill request for pid {:?}", pid);
|
||||
Ok(ShouldTerminate::No)
|
||||
}
|
||||
|
|
@ -53,11 +55,7 @@ impl target::ext::extended_mode::ExtendedMode for XousTarget {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn query_if_attached(
|
||||
&mut self,
|
||||
_pid: gdbstub::common::Pid,
|
||||
) -> TargetResult<target::ext::extended_mode::AttachKind, Self> {
|
||||
println!("Querying if attached");
|
||||
fn query_if_attached(&mut self, _pid: Pid) -> TargetResult<AttachKind, Self> {
|
||||
Ok(AttachKind::Attach)
|
||||
}
|
||||
|
||||
|
|
@ -65,8 +63,15 @@ impl target::ext::extended_mode::ExtendedMode for XousTarget {
|
|||
&mut self,
|
||||
_filename: Option<&[u8]>,
|
||||
_args: target::ext::extended_mode::Args<'_, '_>,
|
||||
) -> TargetResult<gdbstub::common::Pid, Self> {
|
||||
) -> TargetResult<Pid, Self> {
|
||||
println!("Trying to run command (?!)");
|
||||
Err(TargetError::NonFatal)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn support_current_active_pid(
|
||||
&mut self,
|
||||
) -> Option<target::ext::extended_mode::CurrentActivePidOps<'_, Self>> {
|
||||
Some(self)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ impl MultiThreadBase for XousTarget {
|
|||
data: &mut [u8],
|
||||
_tid: Tid, // same address space for each core
|
||||
) -> TargetResult<(), Self> {
|
||||
let mut current_addr = start_addr;
|
||||
let current_addr = start_addr as usize;
|
||||
let Some(pid) = self.pid else {
|
||||
for entry in data.iter_mut() { *entry = 0 };
|
||||
return Ok(());
|
||||
|
|
@ -107,10 +107,11 @@ impl MultiThreadBase for XousTarget {
|
|||
.unwrap()
|
||||
.activate()
|
||||
.unwrap();
|
||||
data.iter_mut().for_each(|b| {
|
||||
*b = crate::arch::mem::peek_memory(current_addr as *mut u8).unwrap_or(0xff);
|
||||
current_addr += 1;
|
||||
});
|
||||
for (offset, b) in data.iter_mut().enumerate() {
|
||||
*b = crate::arch::mem::peek_memory((current_addr + offset) as *mut u8)
|
||||
.unwrap_or(0xff);
|
||||
// println!("<< Peek {:02x} @ {:08x}", *b, current_addr);
|
||||
}
|
||||
|
||||
// Restore the previous PID
|
||||
system_services
|
||||
|
|
@ -126,10 +127,11 @@ impl MultiThreadBase for XousTarget {
|
|||
&mut self,
|
||||
start_addr: u32,
|
||||
data: &[u8],
|
||||
_tid: Tid, // same address space for each core
|
||||
_tid: Tid, // all threads share the same process memory space
|
||||
) -> TargetResult<(), Self> {
|
||||
let mut current_addr = start_addr;
|
||||
let Some(pid) = self.pid else {
|
||||
println!("Couldn't poke memory: no current process!");
|
||||
return Ok(());
|
||||
};
|
||||
crate::services::SystemServices::with(|system_services| {
|
||||
|
|
@ -145,9 +147,9 @@ impl MultiThreadBase for XousTarget {
|
|||
.unwrap();
|
||||
data.iter().for_each(|b| {
|
||||
if let Err(_e) = crate::arch::mem::poke_memory(current_addr as *mut u8, *b) {
|
||||
panic!("couldn't poke memory: {:?}", _e);
|
||||
// gprintln!("Error writing to {:08x}: {:?}", current_addr, e);
|
||||
// panic!("couldn't poke memory: {:?}", _e);
|
||||
}
|
||||
// println!("Poked {:02x} @ {:08x}", *b, current_addr);
|
||||
current_addr += 1;
|
||||
});
|
||||
|
||||
|
|
@ -161,13 +163,12 @@ impl MultiThreadBase for XousTarget {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn list_active_threads(
|
||||
&mut self,
|
||||
register_thread: &mut dyn FnMut(Tid),
|
||||
) -> Result<(), Self::Error> {
|
||||
let Some(pid) = self.pid else {
|
||||
// Register a fake thread
|
||||
register_thread(Tid::new(1).unwrap());
|
||||
return Ok(());
|
||||
};
|
||||
crate::services::SystemServices::with(|system_services| {
|
||||
|
|
|
|||
|
|
@ -1,19 +1,10 @@
|
|||
use gdbstub::common::{Signal, Tid};
|
||||
use gdbstub::target::ext::base::multithread::MultiThreadResume;
|
||||
use gdbstub::target::ext::base::multithread::{MultiThreadResume, MultiThreadSingleStepOps};
|
||||
|
||||
use super::XousTarget;
|
||||
|
||||
impl MultiThreadResume for XousTarget {
|
||||
fn resume(&mut self) -> Result<(), Self::Error> {
|
||||
// println!("Resuming process {:?}", self.pid);
|
||||
// unsafe { HALTED = false };
|
||||
// match default_resume_action {
|
||||
// ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
||||
// return Err("single-stepping not supported")
|
||||
// }
|
||||
// _ => (),
|
||||
// }
|
||||
|
||||
if let Some(pid) = self.pid {
|
||||
crate::services::SystemServices::with_mut(|system_services| {
|
||||
system_services.resume_process_from_debug(pid).unwrap()
|
||||
|
|
@ -22,26 +13,19 @@ impl MultiThreadResume for XousTarget {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn support_single_step(&mut self) -> Option<MultiThreadSingleStepOps<'_, Self>> {
|
||||
Some(self)
|
||||
}
|
||||
|
||||
fn clear_resume_actions(&mut self) -> Result<(), Self::Error> {
|
||||
// println!("Clearing resume actions");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_resume_action_continue(
|
||||
&mut self,
|
||||
tid: Tid,
|
||||
signal: Option<Signal>,
|
||||
_tid: Tid,
|
||||
_signal: Option<Signal>,
|
||||
) -> Result<(), Self::Error> {
|
||||
println!(
|
||||
"Setting resume action continue on process {:?} thread {:?} with signal: {:?}",
|
||||
self.pid, tid, signal
|
||||
);
|
||||
// match action {
|
||||
// ResumeAction::Step | ResumeAction::StepWithSignal(_) => {
|
||||
// Err("single-stepping resume action not supported")
|
||||
// }
|
||||
// ResumeAction::Continue | ResumeAction::ContinueWithSignal(_) => Ok(()),
|
||||
// }
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
21
kernel/src/debug/gdb/multi_thread_single_step.rs
Normal file
21
kernel/src/debug/gdb/multi_thread_single_step.rs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
use super::XousTarget;
|
||||
use gdbstub::common::{Signal, Tid};
|
||||
use gdbstub::target::ext::base::multithread::MultiThreadSingleStep;
|
||||
|
||||
impl MultiThreadSingleStep for XousTarget {
|
||||
fn set_resume_action_step(
|
||||
&mut self,
|
||||
tid: Tid,
|
||||
_signal: Option<Signal>,
|
||||
) -> Result<(), Self::Error> {
|
||||
// if signal.is_some() {
|
||||
// return Err("no support for stepping with signal");
|
||||
// }
|
||||
// println!(
|
||||
// "Performing a single step -- Setting resume action {:?} for tid {:?}",
|
||||
// _signal, tid
|
||||
// );
|
||||
self.patch_stepi(tid)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,362 @@
|
|||
use core::convert::TryInto;
|
||||
use core::hint::unreachable_unchecked;
|
||||
|
||||
// mod disasm;
|
||||
|
||||
use super::XousTarget;
|
||||
use gdbstub::common::Tid;
|
||||
use gdbstub::target::ext::base::multithread::MultiThreadBase;
|
||||
use gdbstub::target::ext::base::single_register_access::SingleRegisterAccess;
|
||||
use gdbstub::target::Target;
|
||||
use gdbstub_arch::riscv::reg::id::RiscvRegId;
|
||||
|
||||
enum Opcode {
|
||||
Opcode16(u16),
|
||||
Opcode32(u32),
|
||||
}
|
||||
|
||||
impl core::fmt::LowerHex for Opcode {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
Opcode::Opcode16(val) => write!(f, "{:04x}", val),
|
||||
Opcode::Opcode32(val) => write!(f, "{:08x}", val),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct PatchedInstruction {
|
||||
/// The address that was patched
|
||||
pc: u32,
|
||||
|
||||
/// The value of the region before it was patched
|
||||
previous: Opcode,
|
||||
}
|
||||
|
||||
pub(crate) struct XousTargetInner {
|
||||
/// When doing a `stepi` we patch the instruction with an illegal instruction
|
||||
/// and store the previous value here.
|
||||
step_patch: Option<PatchedInstruction>,
|
||||
}
|
||||
|
||||
impl Default for XousTargetInner {
|
||||
fn default() -> Self {
|
||||
XousTargetInner { step_patch: None }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum OpcodeType {
|
||||
Rv16,
|
||||
Rv32,
|
||||
}
|
||||
|
||||
/// GDB is broken and will send `vCont;s` to targets even if they
|
||||
/// report that they don't support single-stepping. Therefore,
|
||||
/// we have to include a partial disassembler in the kernel in
|
||||
/// order to work around this bug.
|
||||
|
||||
/// Patch the program at the given process and thread such that
|
||||
/// the next instruction it executes is `c.break`.
|
||||
///
|
||||
/// Return the previous memory value so that it can be saved.
|
||||
impl XousTarget {
|
||||
pub fn patch_stepi(&mut self, tid: Tid) -> Result<(), <XousTarget as Target>::Error> {
|
||||
if self.inner.step_patch.is_some() {
|
||||
self.unpatch_stepi(tid)?;
|
||||
}
|
||||
|
||||
let mut pc = [0u8; core::mem::size_of::<u32>()];
|
||||
self.read_register(tid, RiscvRegId::Pc, &mut pc)
|
||||
.or(Err("unable to read register"))?;
|
||||
let pc = u32::from_le_bytes(pc);
|
||||
|
||||
let mut opcode = [0u8; core::mem::size_of::<u32>()];
|
||||
self.read_addrs(pc, &mut opcode, tid)
|
||||
.or(Err("unable to read memory"))?;
|
||||
let current = u32::from_le_bytes(opcode);
|
||||
|
||||
let current_opcode_type = match current & 0b11 {
|
||||
0b00 | 0b01 | 0b10 => OpcodeType::Rv16,
|
||||
0b11 => OpcodeType::Rv32,
|
||||
_ => unsafe { unreachable_unchecked() },
|
||||
};
|
||||
|
||||
let new_pc = match current_opcode_type {
|
||||
OpcodeType::Rv16 => self.next_pc_16(pc, (current & 0xffff).try_into().unwrap(), tid)?,
|
||||
OpcodeType::Rv32 => self.next_pc_32(pc, current, tid)?,
|
||||
};
|
||||
|
||||
let mut opcode = [0u8; core::mem::size_of::<u32>()];
|
||||
self.read_addrs(new_pc, &mut opcode, tid)
|
||||
.or(Err("unable to read memory"))?;
|
||||
let existing = u32::from_le_bytes(opcode);
|
||||
|
||||
let existing_opcode_type = match existing & 0b11 {
|
||||
0b00 | 0b01 | 0b10 => OpcodeType::Rv16,
|
||||
0b11 => OpcodeType::Rv32,
|
||||
_ => unsafe { unreachable_unchecked() },
|
||||
};
|
||||
|
||||
match existing_opcode_type {
|
||||
OpcodeType::Rv16 => {
|
||||
self.next_pc_16(new_pc, (existing & 0xffff).try_into().unwrap(), tid)?
|
||||
}
|
||||
OpcodeType::Rv32 => self.next_pc_32(new_pc, existing, tid)?,
|
||||
};
|
||||
|
||||
let (existing, new_opcode) = match existing_opcode_type {
|
||||
OpcodeType::Rv16 => (Opcode::Opcode16(existing as u16), Opcode::Opcode16(0x9002)), // c.ebreak
|
||||
OpcodeType::Rv32 => (Opcode::Opcode32(existing), Opcode::Opcode32(0x0010_0073)), // c.ebreak c.ebreak
|
||||
};
|
||||
|
||||
match new_opcode {
|
||||
Opcode::Opcode16(val) => self
|
||||
.write_addrs(new_pc, &val.to_le_bytes(), tid)
|
||||
.or(Err("unable to write memory"))?,
|
||||
Opcode::Opcode32(val) => self
|
||||
.write_addrs(new_pc, &val.to_le_bytes(), tid)
|
||||
.or(Err("unable to write memory"))?,
|
||||
}
|
||||
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"
|
||||
fence.i
|
||||
fence
|
||||
"
|
||||
)
|
||||
};
|
||||
|
||||
assert!(self.inner.step_patch.is_none());
|
||||
self.inner.step_patch = Some(PatchedInstruction {
|
||||
pc: new_pc,
|
||||
previous: existing,
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unpatch_stepi(&mut self, tid: Tid) -> Result<(), <XousTarget as Target>::Error> {
|
||||
let Some(step_patch) = self.inner.step_patch.take() else {
|
||||
return Ok(());
|
||||
};
|
||||
crate::services::SystemServices::with(|system_services| {
|
||||
let current_pid = system_services.current_pid();
|
||||
|
||||
// Actiavte the debugging process and iterate through it,
|
||||
// noting down each active thread.
|
||||
let debugging_pid = self.pid.unwrap();
|
||||
system_services
|
||||
.get_process(debugging_pid)
|
||||
.unwrap()
|
||||
.activate()
|
||||
.unwrap();
|
||||
|
||||
match step_patch.previous {
|
||||
Opcode::Opcode16(val) => self
|
||||
.write_addrs(step_patch.pc, &val.to_le_bytes(), tid)
|
||||
.or(Err("unable to undo patch"))?,
|
||||
Opcode::Opcode32(val) => self
|
||||
.write_addrs(step_patch.pc, &val.to_le_bytes(), tid)
|
||||
.or(Err("unable to undo patch"))?,
|
||||
}
|
||||
// Restore the previous PID
|
||||
system_services
|
||||
.get_process(current_pid)
|
||||
.unwrap()
|
||||
.activate()
|
||||
.unwrap();
|
||||
Ok(())
|
||||
})?;
|
||||
unsafe {
|
||||
core::arch::asm!(
|
||||
"
|
||||
fence.i
|
||||
fence
|
||||
"
|
||||
)
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn next_pc_16(
|
||||
&mut self,
|
||||
pc: u32,
|
||||
opcode: u16,
|
||||
tid: Tid,
|
||||
) -> Result<u32, <XousTarget as Target>::Error> {
|
||||
let opcode = opcode as u32;
|
||||
if opcode & 0b1110_00000_1111111 == 0b1000_00000_0000010 {
|
||||
// c.jr or c.jalr
|
||||
let rs1 = (opcode >> 7) & 0b11111;
|
||||
if rs1 == 0 {
|
||||
return Ok(pc + 2);
|
||||
}
|
||||
let mut rs = [0u8; 4];
|
||||
self.read_register(tid, RiscvRegId::Gpr(rs1 as u8), &mut rs)
|
||||
.or(Err("unable to read register"))?;
|
||||
let rs1_val = u32::from_le_bytes(rs);
|
||||
Ok(rs1_val)
|
||||
} else if opcode & 0b011_0000000000011 == 0b001_00000000000_01 {
|
||||
// c.j or c.jal
|
||||
// [11|4|9:8|10|6|7|3:1|5]
|
||||
let mut imm = (((opcode >> (3 - 1)) & 0b00000001110)
|
||||
| (opcode >> (11 - 4)) & 0b00000010000
|
||||
| (opcode << (5 - 2)) & 0b00000100000
|
||||
| (opcode >> (7 - 6)) & 0b00001000000
|
||||
| (opcode << (7 - 6)) & 0b00010000000
|
||||
| (opcode >> (9 - 8)) & 0b01100000000
|
||||
| (opcode << (10 - 8)) & 0b10000000000) as u32;
|
||||
// Sign extend
|
||||
if opcode & 0b0001_0000_0000_0000 != 0 {
|
||||
imm |= 0xffff_f800;
|
||||
}
|
||||
|
||||
Ok(pc.wrapping_add(imm))
|
||||
} else if opcode & 0b110_00000000000_11 == 0b110_00000000000_01 {
|
||||
// c.bnez or c.beqz
|
||||
let rs1 = ((opcode >> 7) & 0b111) | 0b1000;
|
||||
let mut rs1_val = [0u8; 4];
|
||||
self.read_register(tid, RiscvRegId::Gpr(rs1 as u8), &mut rs1_val)
|
||||
.or(Err("unable to read register"))?;
|
||||
let rs1_val = u32::from_le_bytes(rs1_val);
|
||||
|
||||
let mut imm = (((opcode >> 2) & 0b00_0_00_11_0)
|
||||
| (opcode >> (10 - 3)) & 0b00_0_11_00_0
|
||||
| (opcode << (5 - 2)) & 0b00_1_00_00_0
|
||||
| (opcode << (7 - 6)) & 0b11_0_00_00_0) as u32;
|
||||
if opcode & (1 << 12) != 0 {
|
||||
imm |= 0xffff_ff00;
|
||||
}
|
||||
|
||||
if opcode & 0b001_00000000000_00 == 0b001_00000000000_00 {
|
||||
let target = if rs1_val != 0 {
|
||||
pc.wrapping_add(imm)
|
||||
} else {
|
||||
pc + 2
|
||||
};
|
||||
Ok(target)
|
||||
} else {
|
||||
let target = if rs1_val == 0 {
|
||||
pc.wrapping_add(imm)
|
||||
} else {
|
||||
pc + 2
|
||||
};
|
||||
Ok(target)
|
||||
}
|
||||
} else {
|
||||
Ok(pc.wrapping_add(2))
|
||||
}
|
||||
}
|
||||
|
||||
fn next_pc_32(
|
||||
&mut self,
|
||||
pc: u32,
|
||||
opcode: u32,
|
||||
tid: Tid,
|
||||
) -> Result<u32, <XousTarget as Target>::Error> {
|
||||
// We probably also ought to look for an LR sequence,
|
||||
// but that seems complicated.
|
||||
|
||||
// jal: xxxxxxxxxxxxxxxxxxxxxxxxx1101111
|
||||
if opcode & 0b1111111 == 0b110_1111 {
|
||||
let mut imm = ((opcode >> 20) & 0b0_1111111111_0)
|
||||
| ((opcode >> 10) & 0b1_0000000000_0)
|
||||
| (opcode & 0b11111111_0000000000_0);
|
||||
if opcode & 0x80000000 != 0 {
|
||||
imm |= 0xfff8_0000;
|
||||
}
|
||||
Ok(pc.wrapping_add(imm))
|
||||
}
|
||||
// jalr: xxxxxxxxxxxxxxxxx000xxxxx1100111
|
||||
else if opcode & 0b000000000000_00000_111_00000_1111111
|
||||
== 0b000000000000_00000_000_00000_1100111
|
||||
{
|
||||
let mut imm = (opcode >> 20) & 0b111_1111_1111;
|
||||
if opcode & 0x80000000 != 0 {
|
||||
imm |= 0xffff_f800;
|
||||
}
|
||||
|
||||
let rs1 = (opcode >> 15) & 0b11111;
|
||||
let mut rs1_val = [0u8; 4];
|
||||
self.read_register(tid, RiscvRegId::Gpr(rs1 as u8), &mut rs1_val)
|
||||
.or(Err("unable to read register"))?;
|
||||
let rs1_val = u32::from_le_bytes(rs1_val);
|
||||
|
||||
Ok(rs1_val.wrapping_add(imm))
|
||||
} else if opcode & 0b1111111 == 0b1100011 {
|
||||
let mut imm = ((opcode >> 7) & 0b0_000000_1111_0)
|
||||
| (opcode >> 20) & 0b0_111111_0000_0
|
||||
| (opcode << 4) & 0b1_000000_0000_0;
|
||||
if opcode & 0x80000000 != 0 {
|
||||
imm |= 0xffff_f000;
|
||||
}
|
||||
|
||||
let rs1 = (opcode >> 15) & 0b11111;
|
||||
let mut rs = [0u8; 4];
|
||||
self.read_register(tid, RiscvRegId::Gpr(rs1 as u8), &mut rs)
|
||||
.or(Err("unable to read register"))?;
|
||||
let rs1_val = i32::from_le_bytes(rs);
|
||||
|
||||
let rs2 = (opcode >> 20) & 0b11111;
|
||||
let mut rs = [0u8; 4];
|
||||
self.read_register(tid, RiscvRegId::Gpr(rs2 as u8), &mut rs)
|
||||
.or(Err("unable to read register"))?;
|
||||
let rs2_val = i32::from_le_bytes(rs);
|
||||
|
||||
Ok(pc.wrapping_add(match (opcode >> 12) & 0b111 {
|
||||
// beq
|
||||
0b000 => {
|
||||
if rs1_val == rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
// bne
|
||||
0b001 => {
|
||||
if rs1_val != rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
// blt
|
||||
0b100 => {
|
||||
if rs1_val < rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
// bge
|
||||
0b101 => {
|
||||
if rs1_val >= rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
// bltu
|
||||
0b110 => {
|
||||
if rs1_val < rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
// bgeu
|
||||
0b111 => {
|
||||
if rs1_val >= rs2_val {
|
||||
imm
|
||||
} else {
|
||||
4
|
||||
}
|
||||
}
|
||||
_ => pc + 4,
|
||||
}))
|
||||
} else {
|
||||
Ok(pc.wrapping_add(4))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -25,7 +25,7 @@ impl SingleRegisterAccess<Tid> for XousTarget {
|
|||
return Ok(buf.len());
|
||||
}
|
||||
RiscvRegId::Gpr(x) => x as usize - 1,
|
||||
RiscvRegId::Pc => 32,
|
||||
RiscvRegId::Pc => 31,
|
||||
_ => {
|
||||
return Err(TargetError::Fatal("register out of range"));
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ impl SingleRegisterAccess<Tid> for XousTarget {
|
|||
.unwrap();
|
||||
let process = crate::arch::process::Process::current();
|
||||
let thread = process.thread(tid.get());
|
||||
let reg = if reg_id == 32 {
|
||||
let reg = if reg_id == 31 {
|
||||
&thread.sepc
|
||||
} else {
|
||||
match thread.registers.get(reg_id) {
|
||||
|
|
@ -84,7 +84,7 @@ impl SingleRegisterAccess<Tid> for XousTarget {
|
|||
return Ok(());
|
||||
}
|
||||
RiscvRegId::Gpr(x) => x as usize - 1,
|
||||
RiscvRegId::Pc => 32,
|
||||
RiscvRegId::Pc => 31,
|
||||
_ => {
|
||||
return Err(TargetError::Fatal("register out of range"));
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ impl SingleRegisterAccess<Tid> for XousTarget {
|
|||
.unwrap();
|
||||
let mut process = crate::arch::process::Process::current();
|
||||
let thread = process.thread_mut(tid.get());
|
||||
let reg = if reg_id == 32 {
|
||||
let reg = if reg_id == 31 {
|
||||
&mut thread.sepc
|
||||
} else {
|
||||
match thread.registers.get_mut(reg_id) {
|
||||
|
|
|
|||
|
|
@ -19,7 +19,8 @@ impl Target for XousTarget {
|
|||
}
|
||||
|
||||
/// Opt in to having GDB handle breakpoints for us. This allows for an unlimited number
|
||||
/// of breakpoints without having us keep track of the breakpoints ourselves.
|
||||
/// of breakpoints without having us keep track of the breakpoints ourselves, but
|
||||
/// doesn't work with XIP programs.
|
||||
fn guard_rail_implicit_sw_breakpoints(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue