From d572b2c297f25111fc4cb3530be4b300b92fcdfa Mon Sep 17 00:00:00 2001 From: Berkus Decker Date: Tue, 20 Oct 2020 17:54:49 +0300 Subject: [PATCH] Move panics to separate module Add special panic code for QEMU tests. --- nucleus/src/main.rs | 6 +----- nucleus/src/panic.rs | 13 +++++++++++++ nucleus/src/qemu.rs | 14 ++++++++++++-- 3 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 nucleus/src/panic.rs diff --git a/nucleus/src/main.rs b/nucleus/src/main.rs index c1187c6..cf4b4ae 100644 --- a/nucleus/src/main.rs +++ b/nucleus/src/main.rs @@ -32,6 +32,7 @@ pub mod arch; pub use arch::*; mod macros; mod mm; +mod panic; mod platform; #[cfg(feature = "qemu")] mod qemu; @@ -100,11 +101,6 @@ pub fn kmain() -> ! { endless_sleep() } -#[panic_handler] -fn panicked(_info: &core::panic::PanicInfo) -> ! { - endless_sleep() -} - #[cfg(test)] mod main_tests { use super::*; diff --git a/nucleus/src/panic.rs b/nucleus/src/panic.rs new file mode 100644 index 0000000..c4750d0 --- /dev/null +++ b/nucleus/src/panic.rs @@ -0,0 +1,13 @@ +#[cfg(not(test))] +#[panic_handler] +fn panicked(info: &core::panic::PanicInfo) -> ! { + crate::println!("{}", info); + crate::endless_sleep() +} + +#[cfg(test)] +#[panic_handler] +fn panicked(info: &core::panic::PanicInfo) -> ! { + crate::println!("[failed]\nError: {}\n", info); + crate::qemu::semihosting::exit_failure() +} diff --git a/nucleus/src/qemu.rs b/nucleus/src/qemu.rs index 97771a2..a972b41 100644 --- a/nucleus/src/qemu.rs +++ b/nucleus/src/qemu.rs @@ -4,13 +4,23 @@ */ pub mod semihosting { #[cfg(test)] - pub fn exit_success() { + pub fn exit_success() -> ! { use qemu_exit::QEMUExit; #[cfg(target_arch = "aarch64")] let qemu_exit_handle = qemu_exit::AArch64::new(); - qemu_exit_handle.exit_success(); + qemu_exit_handle.exit_success() + } + + #[cfg(test)] + pub fn exit_failure() -> ! { + use qemu_exit::QEMUExit; + + #[cfg(target_arch = "aarch64")] + let qemu_exit_handle = qemu_exit::AArch64::new(); + + qemu_exit_handle.exit_failure() } #[cfg(test)]