From 7016cd0bace5044bda9c19a85d41a25d39955d58 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 11 Nov 2026 13:25:36 -0500 Subject: [PATCH] feat(boot): print the guest's panic region log when a reset comes without a panic on the console, as when a PE parked in SPTM keeps XNU from printing it --- crates/stela-boot-core/src/lib.rs | 1 + crates/stela-boot-core/src/panic_region.rs | 137 ++++++++++++++++++ crates/stela-boot/src/engine.rs | 71 +++++++++ crates/stela-boot/src/engine/bundle.rs | 1 + .../src/machine/engine/data_access.rs | 7 + 5 files changed, 217 insertions(+) create mode 100644 crates/stela-boot-core/src/panic_region.rs diff --git a/crates/stela-boot-core/src/lib.rs b/crates/stela-boot-core/src/lib.rs index e374008680..e4fa8c9daa 100644 --- a/crates/stela-boot-core/src/lib.rs +++ b/crates/stela-boot-core/src/lib.rs @@ -54,6 +54,7 @@ pub mod native_resources; pub mod nofault_read; pub mod options; pub mod page_window; +pub mod panic_region; pub mod paging_controls; pub mod passthrough; pub mod pe; diff --git a/crates/stela-boot-core/src/panic_region.rs b/crates/stela-boot-core/src/panic_region.rs new file mode 100644 index 0000000000..384a42d0aa --- /dev/null +++ b/crates/stela-boot-core/src/panic_region.rs @@ -0,0 +1,137 @@ +//! The guest kernel's panic region (`/pram reg`, carveout 98, which the +//! loader publishes), read without the guest's help when it went down +//! without printing a panic: XNU writes its panic log there before the +//! console sees it, and a CPU that never answers the debugger cross-call +//! (one parked in SPTM, say) keeps the console from ever seeing it. +//! +//! Layout: XNU `osfmk/kern/debug.h` `struct embedded_panic_header`, packed: +//! `eph_magic` u32 at 0 (`EMBEDDED_PANIC_MAGIC`, 'CNUF'), `eph_crc` at 4, +//! `eph_version` at 8, `eph_panic_flags` u64 at 12, `eph_panic_log_offset` +//! u32 at 20 and `eph_panic_log_len` u32 at 24. `PE_init_panicheader` sets +//! the magic and the log offset (past the header) when a CPU starts to +//! panic; the length is written only when the log is flushed, so the text +//! is read up to its first NUL instead. + +use alloc::string::String; + +/// `EMBEDDED_PANIC_MAGIC`. +pub const MAGIC: u32 = 0x4655_4e43; +/// Bytes of the header that this reader needs. +pub const HEADER_BYTES: usize = 28; +/// The most text one report shows. +pub const TEXT_LIMIT: usize = 2048; + +/// What the panic region held. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Contents { + /// No panic header: the kernel has not begun a panic (or never mapped + /// the region). + NoHeader, + /// A header whose log offset lies outside the region. + BadOffset(u32), + /// A header with no log text yet. + Empty, + /// The log text, with its unprintable bytes shown as `.`, cut at + /// [`TEXT_LIMIT`]; `truncated` says whether more followed. + Text { text: String, truncated: bool }, +} + +/// The panic log `region` (the start of the panic region) holds. +pub fn read(region: &[u8]) -> Contents { + let word = |at: usize| { + region + .get(at..at + 4) + .map(|bytes| u32::from_le_bytes(bytes.try_into().expect("4 bytes"))) + }; + if word(0) != Some(MAGIC) { + return Contents::NoHeader; + } + let Some(offset) = word(20) else { + return Contents::NoHeader; + }; + let start = offset as usize; + if start < HEADER_BYTES || start >= region.len() { + return Contents::BadOffset(offset); + } + let log = ®ion[start..]; + let end = log.iter().position(|&byte| byte == 0).unwrap_or(log.len()); + let shown = end.min(TEXT_LIMIT); + if shown == 0 { + return Contents::Empty; + } + let text = log[..shown] + .iter() + .map(|&byte| { + if byte.is_ascii_graphic() || b" \t\n".contains(&byte) { + char::from(byte) + } else { + '.' + } + }) + .collect(); + Contents::Text { + text, + truncated: end > shown, + } +} + +#[cfg(test)] +mod tests { + use super::*; + use alloc::{vec, vec::Vec}; + + /// A region with the header `PE_init_panicheader` writes and the log + /// 0x331 bytes in, where 26A434 XNU's `debug_log_init` puts it. + fn region(log: &[u8]) -> Vec { + let mut region = vec![0u8; 0x1000]; + region[..4].copy_from_slice(&MAGIC.to_le_bytes()); + region[20..24].copy_from_slice(&0x331u32.to_le_bytes()); + region[0x331..0x331 + log.len()].copy_from_slice(log); + region + } + + #[test] + fn the_log_after_the_header_is_read_to_its_first_nul() { + let contents = read(®ion( + b"panic(cpu 2 caller 0xfffffe0037a1d94): cannot rewrap ek 12, 3\n\0stale", + )); + assert_eq!( + contents, + Contents::Text { + text: "panic(cpu 2 caller 0xfffffe0037a1d94): cannot rewrap ek 12, 3\n".into(), + truncated: false, + } + ); + } + + #[test] + fn a_region_without_the_magic_has_no_panic() { + let mut bytes = region(b"panic(cpu 0 caller 0x1): old\n"); + bytes[0] = 0; + assert_eq!(read(&bytes), Contents::NoHeader); + assert_eq!(read(&[]), Contents::NoHeader); + assert_eq!(read(&MAGIC.to_le_bytes()), Contents::NoHeader); + } + + #[test] + fn a_header_without_text_or_with_a_wild_offset_says_so() { + assert_eq!(read(®ion(b"")), Contents::Empty); + let mut bytes = region(b"x"); + bytes[20..24].copy_from_slice(&0x2000u32.to_le_bytes()); + assert_eq!(read(&bytes), Contents::BadOffset(0x2000)); + bytes[20..24].copy_from_slice(&4u32.to_le_bytes()); + assert_eq!(read(&bytes), Contents::BadOffset(4)); + } + + #[test] + fn long_or_binary_text_is_cut_and_masked() { + let mut log = vec![b'a'; TEXT_LIMIT + 10]; + log[1] = 0x07; + let Contents::Text { text, truncated } = read(®ion(&log)) else { + panic!("text"); + }; + assert!(truncated); + assert_eq!(text.len(), TEXT_LIMIT); + assert!(text.starts_with("a.a")); + } +} diff --git a/crates/stela-boot/src/engine.rs b/crates/stela-boot/src/engine.rs index 29ee0d1823..144ec01c65 100644 --- a/crates/stela-boot/src/engine.rs +++ b/crates/stela-boot/src/engine.rs @@ -797,6 +797,72 @@ fn guest_panic() -> Option { .and_then(|panic| panic.message().map(String::from)) } +/// The panic region's guest-physical base and size, from the booted +/// DeviceTree's `/pram reg` (the loader publishes it; zero when absent). +static PANIC_REGION: [AtomicU64; 2] = [AtomicU64::new(0), AtomicU64::new(0)]; + +/// Keep the panic region the booted DeviceTree names, for +/// [`panic_region_report`]. +pub(crate) fn remember_panic_region(tree: &DeviceTree) { + let Some(reg) = tree.property(stela_loader::carveouts::PRAM, "reg") else { + return; + }; + let Some((base, bytes)) = reg.value.get(..16).map(|pair| { + let (base, bytes) = pair.split_at(8); + ( + u64::from_le_bytes(base.try_into().expect("8 bytes")), + u64::from_le_bytes(bytes.try_into().expect("8 bytes")), + ) + }) else { + return; + }; + PANIC_REGION[0].store(base, Ordering::Relaxed); + PANIC_REGION[1].store(bytes, Ordering::Relaxed); +} + +/// When the guest went down without a panic on its console, what its panic +/// region holds: XNU logs a panic there before it stops the other CPUs' +/// work, and a CPU that never answers (parked in SPTM, say) keeps the +/// console from ever printing it, until a watchdog resets the machine. +fn panic_region_report(machine: &mut Machine) { + use stela_boot_core::panic_region::{Contents, HEADER_BYTES, TEXT_LIMIT, read}; + let base = PANIC_REGION[0].load(Ordering::Relaxed); + let bytes = PANIC_REGION[1].load(Ordering::Relaxed); + if base == 0 || bytes < HEADER_BYTES as u64 { + output::arguments(format_args!("GUEST PANIC region=none\n")); + return; + } + // The header, the log offset XNU's header gives (0x331 bytes in) and the + // text limit, read a page at a time so a short region stays in bounds. + let wanted = bytes.min(0x1000 + TEXT_LIMIT as u64) as usize; + let mut region = alloc::vec![0u8; wanted]; + for (index, chunk) in region.chunks_mut(0x1000).enumerate() { + if !machine.read_physical(base + index as u64 * 0x1000, chunk) { + output::arguments(format_args!( + "GUEST PANIC region={base:#x} unreadable at {:#x}\n", + base + index as u64 * 0x1000 + )); + return; + } + } + match read(®ion) { + Contents::NoHeader => output::arguments(format_args!( + "GUEST PANIC region={base:#x} no panic header: the kernel did not begin a panic log\n" + )), + Contents::BadOffset(offset) => output::arguments(format_args!( + "GUEST PANIC region={base:#x} header log offset {offset:#x} lies outside it\n" + )), + Contents::Empty => output::arguments(format_args!( + "GUEST PANIC region={base:#x} header present, no log text yet\n" + )), + Contents::Text { text, truncated } => { + output::arguments(format_args!( + "GUEST PANIC region={base:#x} truncated={truncated} text={text:?}\n" + )); + } + } +} + /// How the guest went down: the first panic, else `otherwise`. fn guest_down(otherwise: String) -> String { match guest_panic() { @@ -1034,6 +1100,11 @@ fn finish( output::arguments(format_args!("STOP macOS crashed: {message}\n")); output::arguments(format_args!("RESET after panic: {reset}\n")); } else { + // Before the report: a watchdog's reset leaves RAM as the + // panicking PE left it. + if !orderly(&reset) { + panic_region_report(machine); + } output::arguments(format_args!("STOP reset: {reset}\n")); } // The run is over: its blocks' memory goes to proving the new diff --git a/crates/stela-boot/src/engine/bundle.rs b/crates/stela-boot/src/engine/bundle.rs index 671fe26662..3eea2db2a9 100644 --- a/crates/stela-boot/src/engine/bundle.rs +++ b/crates/stela-boot/src/engine/bundle.rs @@ -663,6 +663,7 @@ fn load_named<'a>( .ok_or(refuse!("DeviceTree range is out of bounds"))?; let tree = DeviceTree::parse(bytes) .map_err(|error| refuse!("boot trace tree: {error}", %error))?; + super::remember_panic_region(&tree); let trace = boot_trace::Region::from_tree(&tree) .map_err(|error| refuse!("boot trace region: {error}", %error))?; if let Some(trace) = trace { diff --git a/crates/stela-engine/src/machine/engine/data_access.rs b/crates/stela-engine/src/machine/engine/data_access.rs index b4e9680347..499faf99fb 100644 --- a/crates/stela-engine/src/machine/engine/data_access.rs +++ b/crates/stela-engine/src/machine/engine/data_access.rs @@ -774,4 +774,11 @@ impl Machine { _ => false, } } + + /// Read `bytes.len()` bytes of guest RAM at physical `address`, for + /// diagnostics after a stop (the panic region): `false` when it reaches + /// no RAM; nothing is faulted or changed. + pub fn read_physical(&mut self, address: u64, bytes: &mut [u8]) -> bool { + self.host.read_ram(address, bytes) + } }