minimal_rt/
enlightened_panic.rs1use crate::arch::write_crash_reg;
12use arrayvec::ArrayString;
13use core::fmt::Write;
14use core::sync::atomic::AtomicBool;
15use core::sync::atomic::Ordering::Relaxed;
16use hvdef::GuestCrashCtl;
17
18const PRE_OS_ID: u8 = 5;
20
21static CAN_REPORT_MSG: AtomicBool = AtomicBool::new(false);
22
23fn report_raw(tag: [u8; 8], msg: &[u8], msg_pa: Option<usize>) {
24 let crash_ctl = GuestCrashCtl::new()
29 .with_pre_os_id(PRE_OS_ID)
30 .with_no_crash_dump(true)
31 .with_crash_message(!msg.is_empty())
32 .with_crash_notify(true);
33
34 unsafe {
36 write_crash_reg(0, u64::from_le_bytes(*b"BOOTSHIM"));
37 write_crash_reg(1, u64::from_be_bytes(tag));
38 write_crash_reg(2, u64::MAX);
39
40 match (msg, msg_pa) {
41 (msg @ [_, ..], Some(msg_pa)) => {
42 write_crash_reg(3, msg_pa as u64);
44 write_crash_reg(4, msg.len() as u64);
45 }
46 _ => {
47 write_crash_reg(3, 0);
48 write_crash_reg(4, 0);
49 }
50 }
51
52 write_crash_reg(5, crash_ctl.into());
54 }
55}
56
57pub fn report(
61 tag: [u8; 8],
62 panic: &core::panic::PanicInfo<'_>,
63 mut stack_va_to_pa: impl FnMut(*const ()) -> Option<usize>,
64) {
65 let mut panic_buffer = ArrayString::<512>::new();
66 if CAN_REPORT_MSG.load(Relaxed) {
67 let _ = write!(panic_buffer, "{}", panic);
68 }
69 report_raw(
70 tag,
71 panic_buffer.as_bytes(),
72 stack_va_to_pa(panic_buffer.as_bytes().as_ptr().cast()),
73 );
74}
75
76pub fn enable_enlightened_panic() {
81 CAN_REPORT_MSG.store(true, Relaxed);
82}