Skip to content

Commit

Permalink
debug/gdbstub,cpu/idt: Report page faults as SIGSEGV in gdbstub
Browse files Browse the repository at this point in the history
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 <rhopkins@suse.de>
  • Loading branch information
roy-hopkins committed Jul 20, 2023
1 parent a66e4bb commit c0ee673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/cpu/idt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::vc::handle_vc_exception;
use super::{X86GeneralRegs, X86InterruptFrame};
use crate::address::{Address, VirtAddr};
use crate::cpu::extable::handle_exception_table;
use crate::debug::gdbstub::svsm_gdbstub::handle_bp_exception;
use crate::debug::gdbstub::svsm_gdbstub::handle_debug_exception;
use crate::types::SVSM_CS;
use core::arch::{asm, global_asm};
use core::mem;
Expand Down Expand Up @@ -186,6 +186,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
let err = ctx.error_code;

if !handle_exception_table(ctx) {
handle_debug_exception(ctx, ctx.vector);
panic!(
"Unhandled Page-Fault at RIP {:#018x} CR2: {:#018x} error code: {:#018x}",
rip, cr2, err
Expand All @@ -194,7 +195,7 @@ fn generic_idt_handler(ctx: &mut X86ExceptionContext) {
} else if ctx.vector == VC_VECTOR {
handle_vc_exception(ctx);
} else if ctx.vector == BP_VECTOR {
handle_bp_exception(ctx);
handle_debug_exception(ctx, ctx.vector);
} else {
let err = ctx.error_code;
let vec = ctx.vector;
Expand Down
18 changes: 14 additions & 4 deletions src/debug/gdbstub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod svsm_gdbstub {

use crate::address::{Address, VirtAddr};
use crate::cpu::control_regs::read_cr3;
use crate::cpu::idt::X86ExceptionContext;
use crate::cpu::idt::{X86ExceptionContext, BP_VECTOR};
use crate::cpu::percpu::{this_cpu, this_cpu_mut};
use crate::cpu::X86GeneralRegs;
use crate::error::SvsmError;
Expand Down Expand Up @@ -66,10 +66,15 @@ pub mod svsm_gdbstub {
enum ExceptionType {
Debug,
SwBreakpoint,
PageFault,
}

pub fn handle_bp_exception(ctx: &mut X86ExceptionContext) {
handle_exception(ctx, ExceptionType::SwBreakpoint);
pub fn handle_debug_exception(ctx: &mut X86ExceptionContext, exception: usize) {
let tp = match exception {
BP_VECTOR => ExceptionType::SwBreakpoint,
_ => ExceptionType::PageFault,
};
handle_exception(ctx, tp);
}

pub fn handle_db_exception(ctx: &mut X86ExceptionContext) {
Expand Down Expand Up @@ -260,6 +265,11 @@ pub mod svsm_gdbstub {
tid,
signal: Signal::SIGINT,
}
} else if exception_type == ExceptionType::PageFault {
MultiThreadStopReason::SignalWithThread {
tid,
signal: Signal::SIGSEGV,
}
} else {
MultiThreadStopReason::SwBreak(tid)
};
Expand Down Expand Up @@ -707,7 +717,7 @@ pub mod svsm_gdbstub {
Ok(())
}

pub fn handle_bp_exception(_ctx: &mut X86ExceptionContext) {}
pub fn handle_debug_exception(_ctx: &mut X86ExceptionContext, _exception: usize) {}

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

Expand Down

0 comments on commit c0ee673

Please sign in to comment.