Skip to content

Commit

Permalink
feat(panic): resolve deadlock when panicking while printing
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroening committed Dec 12, 2024
1 parent b9ff719 commit 7ea8d12
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/arch/x86_64/kernel/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,17 @@ impl IrqStatistics {
}

pub(crate) fn print_statistics() {
println!("Number of interrupts");
panic_println!("Number of interrupts");
for (core_id, irg_statistics) in IRQ_COUNTERS.lock().iter() {
for (i, counter) in irg_statistics.counters.iter().enumerate() {
let counter = counter.load(Ordering::Relaxed);
if counter > 0 {
match get_irq_name(i.try_into().unwrap()) {
Some(name) => {
println!("[{core_id}][{name}]: {counter}");
panic_println!("[{core_id}][{name}]: {counter}");
}
_ => {
println!("[{core_id}][{i}]: {counter}");
panic_println!("[{core_id}][{i}]: {counter}");
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/console.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::fmt;
use core::{fmt, mem};

use hermit_sync::{InterruptTicketMutex, Lazy};

Expand Down Expand Up @@ -44,6 +44,14 @@ pub fn _print(args: fmt::Arguments<'_>) {
CONSOLE.lock().write_fmt(args).unwrap();
}

#[doc(hidden)]
pub fn _panic_print(args: fmt::Arguments<'_>) {
use fmt::Write;
let mut console = unsafe { CONSOLE.make_guard_unchecked() };
console.write_fmt(args).ok();
mem::forget(console);
}

#[cfg(all(test, not(target_os = "none")))]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ fn application_processor_main() -> ! {
#[panic_handler]
fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
let core_id = crate::arch::core_local::core_id();
println!("[{core_id}][PANIC] {info}");
panic_println!("[{core_id}][PANIC] {info}\n");

crate::scheduler::shutdown(1);
}
12 changes: 12 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ macro_rules! println {
}};
}

/// Emergency output.
#[cfg(target_os = "none")]
#[macro_export]
macro_rules! panic_println {
() => {
$crate::console::_panic_print(::core::format_args!("\n"));
};
($($arg:tt)*) => {{
$crate::console::_panic_print(::core::format_args!("{}\n", format_args!($($arg)*)));
}};
}

/// Prints and returns the value of a given expression for quick and dirty
/// debugging.
// Copied from std/macros.rs
Expand Down
2 changes: 1 addition & 1 deletion src/syscalls/interfaces/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait SyscallInterface: Send + Sync {

fn shutdown(&self, error_code: i32) -> ! {
// This is a stable message used for detecting exit codes for different hypervisors.
println!("exit status {error_code}");
panic_println!("exit status {error_code}");

arch::processor::shutdown(error_code)
}
Expand Down

0 comments on commit 7ea8d12

Please sign in to comment.