Skip to content

Commit a8db262

Browse files
committed
debug/gdbstub,cpu/idt: Report page faults as SIGSEGV in gdbstub
The gdbstub currently triggers a breakpoint on a panic, which allows the debugger to directly probe the state of the kernel when a panic occurs. However, some of the context is lost before the panic handler is invoked. Therefore this patch introduces direct handling of unhandled page faults within the gdbstub to provide that additional context. If the gdbstub feature is not enabled then these changes have no effect. Signed-off-by: Roy Hopkins <[email protected]>
1 parent 79f367c commit a8db262

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

src/cpu/idt.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use super::vc::handle_vc_exception;
1010
use super::{X86GeneralRegs, X86InterruptFrame};
1111
use crate::address::{Address, VirtAddr};
1212
use crate::cpu::extable::handle_exception_table;
13-
use crate::debug::gdbstub::svsm_gdbstub::handle_bp_exception;
13+
use crate::debug::gdbstub::svsm_gdbstub::handle_debug_exception;
1414
use crate::types::SVSM_CS;
1515
use core::arch::{asm, global_asm};
1616
use core::mem;
@@ -199,6 +199,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
199199
let err = ctx.error_code;
200200

201201
if !handle_exception_table(ctx) {
202+
handle_debug_exception(ctx, ctx.vector);
202203
panic!(
203204
"Unhandled Page-Fault at RIP {:#018x} CR2: {:#018x} error code: {:#018x}",
204205
rip, cr2, err
@@ -207,7 +208,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
207208
} else if ctx.vector == VC_VECTOR {
208209
handle_vc_exception(ctx);
209210
} else if ctx.vector == BP_VECTOR {
210-
handle_bp_exception(ctx);
211+
handle_debug_exception(ctx, ctx.vector);
211212
} else {
212213
let err = ctx.error_code;
213214
let vec = ctx.vector;

src/debug/gdbstub.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod svsm_gdbstub {
1515

1616
use crate::address::{Address, VirtAddr};
1717
use crate::cpu::control_regs::read_cr3;
18-
use crate::cpu::idt::X86ExceptionContext;
18+
use crate::cpu::idt::{X86ExceptionContext, BP_VECTOR};
1919
use crate::cpu::percpu::{this_cpu, this_cpu_mut};
2020
use crate::cpu::X86GeneralRegs;
2121
use crate::error::SvsmError;
@@ -66,10 +66,15 @@ pub mod svsm_gdbstub {
6666
enum ExceptionType {
6767
Debug,
6868
SwBreakpoint,
69+
PageFault,
6970
}
7071

71-
pub fn handle_bp_exception(ctx: &mut X86ExceptionContext) {
72-
handle_exception(ctx, ExceptionType::SwBreakpoint);
72+
pub fn handle_debug_exception(ctx: &mut X86ExceptionContext, exception: usize) {
73+
let tp = match exception {
74+
BP_VECTOR => ExceptionType::SwBreakpoint,
75+
_ => ExceptionType::PageFault,
76+
};
77+
handle_exception(ctx, tp);
7378
}
7479

7580
pub fn handle_db_exception(ctx: &mut X86ExceptionContext) {
@@ -260,6 +265,11 @@ pub mod svsm_gdbstub {
260265
tid,
261266
signal: Signal::SIGINT,
262267
}
268+
} else if exception_type == ExceptionType::PageFault {
269+
MultiThreadStopReason::SignalWithThread {
270+
tid,
271+
signal: Signal::SIGSEGV,
272+
}
263273
} else {
264274
MultiThreadStopReason::SwBreak(tid)
265275
};
@@ -707,7 +717,7 @@ pub mod svsm_gdbstub {
707717
Ok(())
708718
}
709719

710-
pub fn handle_bp_exception(_ctx: &mut X86ExceptionContext) {}
720+
pub fn handle_debug_exception(_ctx: &mut X86ExceptionContext, _exception: usize) {}
711721

712722
pub fn handle_db_exception(_ctx: &mut X86ExceptionContext) {}
713723

0 commit comments

Comments
 (0)