aboutsummaryrefslogtreecommitdiff
path: root/src/panic_wait.rs
blob: a8d09625d0d1d2f257f793fa8e13e2c8cd19cd1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! # Panic module
//!
//! A panic handler that infinitely waits

use crate::{cpu, serial_println};
use core::panic::PanicInfo;

/// # Prevent Double Faulting
///
/// An atomic operation is used to mark that
/// a fault has occurred. If it detects that
/// there was already a fault, it spins to
/// prevent a recursive faulting cycle.
fn panic_prevent_reenter() {
    use core::sync::atomic::{AtomicBool, Ordering};

    static PANIC_IN_PROGRESS: AtomicBool = AtomicBool::new(false);

    if !PANIC_IN_PROGRESS.load(Ordering::Relaxed) {
        PANIC_IN_PROGRESS.store(true, Ordering::Relaxed);
        return;
    }

    cpu::wait_forever()
}

/// # Panic handler
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
    panic_prevent_reenter();

    let (location, line, column) = match info.location() {
        Some(loc) => (loc.file(), loc.line(), loc.column()),
        _ => ("???", 0, 0),
    };

    serial_println!(
        "Kernel panic!\n\nPanic Location:\n\tFile: '{}', line {}, column {}\n\n{}",
        location,
        line,
        column,
        info.message().unwrap_or(&format_args!("")),
    );
    cpu::wait_forever()
}