Skip to content

Commit 6bf06c2

Browse files
committed
debug: implement From<usize> for ExceptionType
Implement From<usize> for ExceptionType and add a unit test. The UT is only run if `--features enable-gdb` is passed to cargo test. handle_exception body has been moved to handle_debug_exception. Signed-off-by: Thomas Leroy <[email protected]>
1 parent 6282e42 commit 6bf06c2

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

src/debug/gdbstub.rs

+37-9
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,25 @@ pub mod svsm_gdbstub {
6262
Ok(())
6363
}
6464

65-
#[derive(PartialEq, Eq)]
65+
#[derive(PartialEq, Eq, Debug)]
6666
enum ExceptionType {
6767
Debug,
6868
SwBreakpoint,
6969
PageFault,
7070
}
7171

72-
pub fn handle_debug_exception(ctx: &mut X86ExceptionContext, exception: usize) {
73-
let tp = match exception {
74-
BP_VECTOR => ExceptionType::SwBreakpoint,
75-
VC_VECTOR => ExceptionType::Debug,
76-
_ => ExceptionType::PageFault,
77-
};
78-
handle_exception(ctx, tp);
72+
impl From<usize> for ExceptionType {
73+
fn from(value: usize) -> Self {
74+
match value {
75+
BP_VECTOR => ExceptionType::SwBreakpoint,
76+
VC_VECTOR => ExceptionType::Debug,
77+
_ => ExceptionType::PageFault,
78+
}
79+
}
7980
}
8081

81-
fn handle_exception(ctx: &mut X86ExceptionContext, exception_type: ExceptionType) {
82+
pub fn handle_debug_exception(ctx: &mut X86ExceptionContext, exception: usize) {
83+
let exception_type = ExceptionType::from(exception);
8284
let id = this_cpu().runqueue().lock_read().current_task_id();
8385
let mut task_ctx = TaskContext {
8486
regs: X86GeneralRegs {
@@ -704,6 +706,32 @@ pub mod svsm_gdbstub {
704706
Ok(true)
705707
}
706708
}
709+
710+
#[cfg(test)]
711+
pub mod tests {
712+
extern crate alloc;
713+
714+
use super::ExceptionType;
715+
use crate::cpu::idt::common::{BP_VECTOR, VC_VECTOR};
716+
use alloc::vec;
717+
use alloc::vec::Vec;
718+
719+
#[test]
720+
fn exception_type_from() {
721+
let exceptions: Vec<ExceptionType> = [VC_VECTOR, BP_VECTOR, 0]
722+
.iter()
723+
.map(|e| ExceptionType::from(*e))
724+
.collect();
725+
assert_eq!(
726+
exceptions,
727+
vec![
728+
ExceptionType::Debug,
729+
ExceptionType::SwBreakpoint,
730+
ExceptionType::PageFault
731+
]
732+
);
733+
}
734+
}
707735
}
708736

709737
#[cfg(not(feature = "enable-gdb"))]

0 commit comments

Comments
 (0)