Skip to content

Commit

Permalink
Don't die after panics
Browse files Browse the repository at this point in the history
Let's leave the chance to attach a debugger.
  • Loading branch information
sysheap committed Dec 9, 2024
1 parent a1f4807 commit fc57d86
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target = "riscv64gc-unknown-none-elf"
[target.riscv64gc-unknown-none-elf]
runner = """
qemu-system-riscv64 \
-s \
-machine virt \
-cpu rv64 \
-smp 1 \
Expand Down
3 changes: 3 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ test:
miri: build-cargo
MIRIFLAGS="-Zmiri-permissive-provenance -Zmiri-env-forward=RUST_BACKTRACE" RUST_BACKTRACE=1 cargo miri test --target riscv64gc-unknown-linux-gnu

attach:
gdb-multiarch $(pwd)/target/riscv64gc-unknown-none-elf/release/kernel -ex "target remote :1234"

debug: build
tmux new-session -d '{{debugReleaseCommand}}' \; split-window -v 'gdb-multiarch $(pwd)/target/riscv64gc-unknown-none-elf/release/kernel -ex "target remote :1234"' \; attach

Expand Down
5 changes: 3 additions & 2 deletions kernel/src/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ pub fn memory_fence() {
}
}

pub fn disable_gloabl_interrupts() {
pub unsafe fn disable_global_interrupts() {
unsafe {
asm!(
"csrc sstatus, {}",
"csrc sstatus, {}", // Disable global interrupt flag
"csrw sie, x0", // Clear any local enabled interrupts otherwise wfi just goes to the current pending interrupt
in(reg) 0b10);
}
}
Expand Down
17 changes: 11 additions & 6 deletions kernel/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#![cfg_attr(miri, allow(unused_imports))]
use crate::{io::uart::QEMU_UART, println, test::qemu_exit};
use crate::{
io::uart::QEMU_UART, memory::page_tables::KERNEL_PAGE_TABLES, println,
test::qemu_exit::wait_for_the_end,
};
use core::{panic::PanicInfo, sync::atomic::AtomicU8};

static PANIC_COUNTER: AtomicU8 = AtomicU8::new(0);

#[cfg(not(miri))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
use crate::memory::page_tables::KERNEL_PAGE_TABLES;

crate::cpu::disable_gloabl_interrupts();
unsafe {
crate::cpu::disable_global_interrupts();
}

// SAFTEY: The worst what happen is scrambled output
// Disable the stdout mutex in case it was locked before
Expand All @@ -30,14 +33,16 @@ fn panic(info: &PanicInfo) -> ! {
crate::debugging::backtrace::print();
crate::debugging::dump_current_state();

qemu_exit::exit_failure(1);
println!("Time to attach gdb ;) use 'just attach'");
wait_for_the_end();
}

fn abort_if_double_panic() {
let current = PANIC_COUNTER.fetch_add(1, core::sync::atomic::Ordering::SeqCst);

if current >= 1 {
println!("Panic in panic! ABORTING!");
qemu_exit::exit_failure(1);
println!("Time to attach gdb ;) use 'just attach'");
wait_for_the_end();
}
}
12 changes: 7 additions & 5 deletions kernel/src/test/qemu_exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ pub fn exit_reset() -> ! {
wait_for_the_end();
}

fn wait_for_the_end() -> ! {
cpu::disable_gloabl_interrupts();
cpu::wait_for_interrupt();
#[allow(clippy::empty_loop)]
loop {}
pub fn wait_for_the_end() -> ! {
unsafe {
cpu::disable_global_interrupts();
}
loop {
cpu::wait_for_interrupt();
}
}

0 comments on commit fc57d86

Please sign in to comment.