microsoft/mu_feature_ffa
Publicmirrored from https://github.com/microsoft/mu_feature_ffaAvailable
FfaFeaturePkg/SecurePartitions/MsSecurePartitionRust/src/baremetal/panic.rs
58lines · modeblame
aa115865kuqin1210 months ago | 1 | //! A panic handler that infinitely waits. |
| 2 | use core::panic::PanicInfo; | |
| 3 | | |
| 4 | use aarch64_cpu::asm; | |
| 5 | use log::error; | |
| 6 | | |
| 7 | /// Stop immediately if called a second time. | |
| 8 | /// | |
| 9 | /// # Note | |
| 10 | /// | |
| 11 | /// Using atomics here relieves us from needing to use `unsafe` for the static variable. | |
| 12 | /// | |
| 13 | /// On `AArch64`, which is the only implemented architecture at the time of writing this, | |
| 14 | /// [`AtomicBool::load`] and [`AtomicBool::store`] are lowered to ordinary load and store | |
| 15 | /// instructions. They are therefore safe to use even with MMU + caching deactivated. | |
| 16 | /// | |
| 17 | /// [`AtomicBool::load`]: core::sync::atomic::AtomicBool::load | |
| 18 | /// [`AtomicBool::store`]: core::sync::atomic::AtomicBool::store | |
| 19 | fn panic_prevent_reenter() { | |
| 20 | use core::sync::atomic::{AtomicBool, Ordering}; | |
| 21 | | |
| 22 | static PANIC_IN_PROGRESS: AtomicBool = AtomicBool::new(false); | |
| 23 | | |
| 24 | if !PANIC_IN_PROGRESS.load(Ordering::Relaxed) { | |
| 25 | PANIC_IN_PROGRESS.store(true, Ordering::Relaxed); | |
| 26 | | |
| 27 | return; | |
| 28 | } | |
| 29 | | |
| 30 | loop { | |
| 31 | asm::wfe() | |
| 32 | } | |
| 33 | } | |
| 34 | | |
| 35 | #[panic_handler] | |
| 36 | fn panic(info: &PanicInfo) -> ! { | |
| 37 | // Protect against panic infinite loops if any of the following code panics itself. | |
| 38 | panic_prevent_reenter(); | |
| 39 | | |
| 40 | let (location, line, column) = match info.location() { | |
| 41 | Some(loc) => (loc.file(), loc.line(), loc.column()), | |
| 42 | _ => ("???", 0, 0), | |
| 43 | }; | |
| 44 | | |
| 45 | error!( | |
| 46 | "Kernel panic!\n\n\ | |
| 47 | Panic location:\n File '{}', line {}, column {}\n\n\ | |
| 48 | {}", | |
| 49 | location, | |
| 50 | line, | |
| 51 | column, | |
| 52 | info.message(), | |
| 53 | ); | |
| 54 | | |
| 55 | loop { | |
| 56 | asm::wfe() | |
| 57 | } | |
| 58 | } |