From 5c008bfc0446e4631dbab64be61159af04f78dd1 Mon Sep 17 00:00:00 2001 From: taitep Date: Sat, 27 Dec 2025 21:33:39 +0100 Subject: Add exception values (what will go in mtval/stval) --- src/core.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'src/core.rs') diff --git a/src/core.rs b/src/core.rs index 8d51947..eb0549f 100644 --- a/src/core.rs +++ b/src/core.rs @@ -10,7 +10,7 @@ use crate::{ consts::{Addr, RegId, RegValue}, core::commands::CoreCmd, decode::Instruction, - exceptions::ExceptionType, + exceptions::{Exception, ExceptionType, MemoryException}, gdb::{self, DebugCommand, DebugStream, StopReason}, instructions::find_and_exec, mem::MemConfig, @@ -86,7 +86,8 @@ impl Core { } => { let data = (0..len) .map(|offset| self.mem.read_byte(addr + offset)) - .collect(); + .collect::>() + .map_err(Into::into); responder.send(data)?; } @@ -101,7 +102,7 @@ impl Core { Ok(_) => gdb::StopReason::Step, Err(e) => { self.throw_exception(e); - gdb::StopReason::Exception(e) + gdb::StopReason::Exception(e.into()) } })?; } @@ -132,14 +133,17 @@ impl Core { if let Err(e) = self.step() { self.throw_exception(e); - return StopReason::Exception(e); + return StopReason::Exception(e.into()); } } } - pub(crate) fn step(&mut self) -> Result<(), ExceptionType> { + pub(crate) fn step(&mut self) -> Result<(), Exception> { if !self.pc.is_multiple_of(4) { - self.throw_exception(ExceptionType::InstructionAddressMisaligned); + self.throw_exception(Exception { + type_: ExceptionType::InstructionAddressMisaligned, + value: self.pc, + }); } let instr = match self.mem.read_word(self.pc) { @@ -149,13 +153,13 @@ impl Core { } }; - if instr == 0 { - return Err(ExceptionType::IllegalInstruction); - } - if instr & 3 != 3 { // Compressed instruction - (currently) unsupported - return Err(ExceptionType::IllegalInstruction); + // Could also be zero instruction, that also matches this + return Err(Exception { + type_: ExceptionType::IllegalInstruction, + value: instr as u64, + }); } let instr = Instruction(instr); @@ -168,8 +172,8 @@ impl Core { Ok(()) } - fn throw_exception(&mut self, exception_type: ExceptionType) { - eprintln!("Exception: {exception_type:?}"); + fn throw_exception(&mut self, exception: Exception) { + eprintln!("Exception: {exception:?}"); dbg!(self.pc, self.x_regs); } -- cgit v1.2.3