2019-11-25 18:54:05 +00:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
2019-10-08 19:50:48 +00:00
|
|
|
//
|
2022-01-15 20:50:11 +00:00
|
|
|
// Copyright (c) 2018-2022 Andre Richter <andre.o.richter@gmail.com>
|
2019-10-08 19:50:48 +00:00
|
|
|
|
|
|
|
//! A panic handler that infinitely waits.
|
|
|
|
|
2020-03-28 12:27:14 +00:00
|
|
|
use crate::{cpu, println};
|
2019-10-08 19:50:48 +00:00
|
|
|
use core::panic::PanicInfo;
|
|
|
|
|
2022-04-08 20:24:12 +00:00
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
// Private Code
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/// Stop immediately if called a second time.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// Using atomics here relieves us from needing to use `unsafe` for the static variable.
|
|
|
|
///
|
|
|
|
/// On `AArch64`, which is the only implemented architecture at the time of writing this,
|
|
|
|
/// [`AtomicBool::load`] and [`AtomicBool::store`] are lowered to ordinary load and store
|
|
|
|
/// instructions. They are therefore safe to use even with MMU + caching deactivated.
|
|
|
|
///
|
|
|
|
/// [`AtomicBool::load`]: core::sync::atomic::AtomicBool::load
|
|
|
|
/// [`AtomicBool::store`]: core::sync::atomic::AtomicBool::store
|
|
|
|
fn panic_prevent_reenter() {
|
|
|
|
use core::sync::atomic::{AtomicBool, Ordering};
|
|
|
|
|
|
|
|
#[cfg(not(target_arch = "aarch64"))]
|
|
|
|
compile_error!("Add the target_arch to above's check if the following code is safe to use");
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:50:48 +00:00
|
|
|
#[panic_handler]
|
|
|
|
fn panic(info: &PanicInfo) -> ! {
|
2022-04-08 20:24:12 +00:00
|
|
|
// Protect against panic infinite loops if any of the following code panics itself.
|
|
|
|
panic_prevent_reenter();
|
|
|
|
|
2022-04-08 20:24:56 +00:00
|
|
|
let (location, line, column) = match info.location() {
|
|
|
|
Some(loc) => (loc.file(), loc.line(), loc.column()),
|
|
|
|
_ => ("???", 0, 0),
|
|
|
|
};
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Kernel panic!\n\n\
|
|
|
|
Panic location:\n File '{}', line {}, column {}\n\n\
|
|
|
|
{}",
|
|
|
|
location,
|
|
|
|
line,
|
|
|
|
column,
|
|
|
|
info.message().unwrap_or(&format_args!("")),
|
|
|
|
);
|
2019-10-08 19:50:48 +00:00
|
|
|
|
2020-03-28 12:27:14 +00:00
|
|
|
cpu::wait_forever()
|
2019-10-08 19:50:48 +00:00
|
|
|
}
|