rust-raspberrypi-OS-tutorials/13_integrated_testing/src/panic_wait.rs

68 lines
2.1 KiB
Rust
Raw Normal View History

2019-12-30 23:04:13 +00:00
// SPDX-License-Identifier: MIT OR Apache-2.0
//
2020-01-01 23:41:03 +00:00
// Copyright (c) 2018-2020 Andre Richter <andre.o.richter@gmail.com>
2019-12-30 23:04:13 +00:00
//! A panic handler that infinitely waits.
2020-03-28 12:23:22 +00:00
use crate::{bsp, cpu};
2019-12-30 23:04:13 +00:00
use core::{fmt, panic::PanicInfo};
2020-03-28 12:23:22 +00:00
//--------------------------------------------------------------------------------------------------
// Private Code
//--------------------------------------------------------------------------------------------------
2019-12-30 23:04:13 +00:00
fn _panic_print(args: fmt::Arguments) {
use fmt::Write;
2020-03-28 12:23:22 +00:00
unsafe { bsp::console::panic_console_out().write_fmt(args).unwrap() };
2019-12-30 23:04:13 +00:00
}
/// The point of exit for the "standard" (non-testing) `libkernel`.
///
/// This code will be used by the release kernel binary and the `integration tests`. It is linked
/// weakly, so that the integration tests can overload it to exit `QEMU` instead of spinning
/// forever.
///
/// This is one possible approach to solve the problem that `cargo` can not know who the consumer of
/// the library will be:
2020-03-28 12:23:22 +00:00
/// - The release kernel binary that should safely park the paniced core,
/// - or an `integration test` that is executed in QEMU, which should just exit QEMU.
2019-12-30 23:04:13 +00:00
#[cfg(not(test))]
#[linkage = "weak"]
#[no_mangle]
fn _panic_exit() -> ! {
2020-03-28 12:23:22 +00:00
cpu::wait_forever()
}
/// Prints with a newline - only use from the panic handler.
///
/// 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-12-30 23:04:13 +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-12-30 23:04:13 +00:00
} else {
2020-01-03 23:59:37 +00:00
panic_println!("\nKernel panic!");
2019-12-30 23:04:13 +00:00
}
_panic_exit()
}
//--------------------------------------------------------------------------------------------------
// Testing
//--------------------------------------------------------------------------------------------------
/// The point of exit when the library is compiled for testing.
#[cfg(test)]
#[no_mangle]
fn _panic_exit() -> ! {
2020-03-28 12:23:22 +00:00
cpu::qemu_exit_failure()
2019-12-30 23:04:13 +00:00
}