rust-raspberrypi-OS-tutorials/10_privilege_level/src/panic_wait.rs

40 lines
1.1 KiB
Rust
Raw Normal View History

2019-11-25 18:54:05 +00:00
// SPDX-License-Identifier: MIT OR Apache-2.0
2019-11-01 22:03:29 +00:00
//
2021-01-01 10:28:32 +00:00
// Copyright (c) 2018-2021 Andre Richter <andre.o.richter@gmail.com>
2019-11-01 22:03:29 +00:00
//! A panic handler that infinitely waits.
2020-03-28 12:25:18 +00:00
use crate::{bsp, cpu};
use core::{fmt, panic::PanicInfo};
2020-03-28 12:25:18 +00:00
//--------------------------------------------------------------------------------------------------
// Private Code
//--------------------------------------------------------------------------------------------------
fn _panic_print(args: fmt::Arguments) {
use fmt::Write;
2020-03-28 12:25:18 +00:00
unsafe { bsp::console::panic_console_out().write_fmt(args).unwrap() };
}
/// Prints with a newline - only use from the panic handler.
///
2021-01-29 21:30:02 +00:00
/// Carbon copy from <https://doc.rust-lang.org/src/std/macros.rs.html>
#[macro_export]
macro_rules! panic_println {
($($arg:tt)*) => ({
_panic_print(format_args_nl!($($arg)*));
})
}
2019-11-01 22:03:29 +00:00
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
if let Some(args) = info.message() {
2020-01-03 23:59:37 +00:00
panic_println!("\nKernel panic: {}", args);
2019-11-01 22:03:29 +00:00
} else {
2020-01-03 23:59:37 +00:00
panic_println!("\nKernel panic!");
2019-11-01 22:03:29 +00:00
}
2020-03-28 12:25:18 +00:00
cpu::wait_forever()
2019-11-01 22:03:29 +00:00
}