Skip to content

Commit 9728d3c

Browse files
committed
Don't die after panics
Let's leave the chance to attach a debugger.
1 parent d924a0d commit 9728d3c

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ target = "riscv64gc-unknown-none-elf"
77
[target.riscv64gc-unknown-none-elf]
88
runner = """
99
qemu-system-riscv64 \
10+
-s \
1011
-machine virt \
1112
-cpu rv64 \
1213
-smp 1 \

justfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ test:
2828
miri: build-cargo
2929
MIRIFLAGS="-Zmiri-permissive-provenance -Zmiri-env-forward=RUST_BACKTRACE" RUST_BACKTRACE=1 cargo miri test --target riscv64gc-unknown-linux-gnu
3030

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

kernel/src/cpu.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ pub fn memory_fence() {
4949
}
5050
}
5151

52-
pub fn disable_gloabl_interrupts() {
52+
pub unsafe fn disable_global_interrupts() {
5353
unsafe {
5454
asm!(
55-
"csrc sstatus, {}",
55+
"csrc sstatus, {}", // Disable global interrupt flag
56+
"csrw sie, x0", // Clear any local enabled interrupts otherwise wfi just goes to the current pending interrupt
5657
in(reg) 0b10);
5758
}
5859
}

kernel/src/panic.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#![cfg_attr(miri, allow(unused_imports))]
2-
use crate::{io::uart::QEMU_UART, println, test::qemu_exit};
2+
use crate::{
3+
io::uart::QEMU_UART, memory::page_tables::KERNEL_PAGE_TABLES, println,
4+
test::qemu_exit::wait_for_the_end,
5+
};
36
use core::{panic::PanicInfo, sync::atomic::AtomicU8};
47

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

710
#[cfg(not(miri))]
811
#[panic_handler]
912
fn panic(info: &PanicInfo) -> ! {
10-
use crate::memory::page_tables::KERNEL_PAGE_TABLES;
11-
12-
crate::cpu::disable_gloabl_interrupts();
13+
unsafe {
14+
crate::cpu::disable_global_interrupts();
15+
}
1316

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

33-
qemu_exit::exit_failure(1);
36+
println!("Time to attach gdb ;) use 'just attach'");
37+
wait_for_the_end();
3438
}
3539

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

3943
if current >= 1 {
4044
println!("Panic in panic! ABORTING!");
41-
qemu_exit::exit_failure(1);
45+
println!("Time to attach gdb ;) use 'just attach'");
46+
wait_for_the_end();
4247
}
4348
}

kernel/src/test/qemu_exit.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ pub fn exit_reset() -> ! {
2727
wait_for_the_end();
2828
}
2929

30-
fn wait_for_the_end() -> ! {
31-
cpu::disable_gloabl_interrupts();
32-
cpu::wait_for_interrupt();
33-
#[allow(clippy::empty_loop)]
34-
loop {}
30+
pub fn wait_for_the_end() -> ! {
31+
unsafe {
32+
cpu::disable_global_interrupts();
33+
}
34+
loop {
35+
cpu::wait_for_interrupt();
36+
}
3537
}

0 commit comments

Comments
 (0)