aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / helpers / panic_handler.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | use core::panic::PanicInfo; |
| 4 | use core::ptr; |
| 5 | use core::time::Duration; |
| 6 | |
| 7 | use crate::{boot, println}; |
| 8 | use cfg_if::cfg_if; |
| 9 | |
| 10 | #[panic_handler] |
| 11 | fn panic_handler(info: &PanicInfo) -> ! { |
| 12 | println!("[PANIC]: {}", info); |
| 13 | |
| 14 | // Give the user some time to read the message |
| 15 | if boot::are_boot_services_active() { |
| 16 | boot::stall(Duration::from_secs(10)); |
| 17 | } else { |
| 18 | let mut dummy = 0u64; |
| 19 | // FIXME: May need different counter values in debug & release builds |
| 20 | for i in 0..300_000_000 { |
| 21 | // SAFETY: we own the memory. |
| 22 | unsafe { |
| 23 | ptr::write_volatile(&mut dummy, i); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | cfg_if! { |
| 29 | if #[cfg(all(target_arch = "x86_64", feature = "qemu"))] { |
| 30 | // If running in QEMU, use the f4 exit port to signal the error and exit |
| 31 | use qemu_exit::QEMUExit; |
| 32 | let custom_exit_success = 3; |
| 33 | // SAFETY: the I/O port matches the one of the QEMU environment. |
| 34 | let qemu_exit_handle = unsafe { qemu_exit::X86::new(0xF4, custom_exit_success) }; |
| 35 | qemu_exit_handle.exit_failure(); |
| 36 | } else { |
| 37 | // If the system table is available, use UEFI's standard shutdown mechanism |
| 38 | if let Some(st) = crate::table::system_table_raw() { |
| 39 | // SAFETY: The handle is known to point to live storage here. |
| 40 | if !unsafe { st.as_ref().runtime_services }.is_null() { |
| 41 | crate::runtime::reset(crate::runtime::ResetType::SHUTDOWN, crate::Status::ABORTED, None); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // If we don't have any shutdown mechanism handy, the best we can do is loop |
| 46 | log::error!("Could not shut down, please power off the system manually..."); |
| 47 | |
| 48 | cfg_if! { |
| 49 | if #[cfg(target_arch = "x86_64")] { |
| 50 | loop { |
| 51 | // SAFETY: No side-effects on memory. |
| 52 | unsafe { |
| 53 | // Try to at least keep CPU from running at 100% |
| 54 | core::arch::asm!("hlt", options(nomem, nostack)); |
| 55 | } |
| 56 | } |
| 57 | } else if #[cfg(target_arch = "aarch64")] { |
| 58 | loop { |
| 59 | // SAFETY: No side-effects on memory. |
| 60 | unsafe { |
| 61 | // Try to at least keep CPU from running at 100% |
| 62 | core::arch::asm!("hlt 420", options(nomem, nostack)); |
| 63 | } |
| 64 | } |
| 65 | } else { |
| 66 | loop { |
| 67 | // just run forever dammit how do you return never anyway |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |