From d2e10c1875ffbcd1e8272d570b0dee143efce8b6 Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Wed, 11 Nov 2026 15:30:45 -0500 Subject: [PATCH] feat(boot): report watchdog mechanism arms and disarms with the writing PE, and the format strings each PE's stack points to at a stop --- crates/stela-boot/src/engine.rs | 8 +- crates/stela-boot/src/engine/reports.rs | 83 +++++++++++++++++++++ crates/stela-boot/src/engine/run.rs | 1 + crates/stela-soc/src/machine.rs | 3 + crates/stela-soc/src/machine/timekeeping.rs | 17 +++++ crates/stela-soc/src/wdt.rs | 80 ++++++++++++++++++++ 6 files changed, 190 insertions(+), 2 deletions(-) diff --git a/crates/stela-boot/src/engine.rs b/crates/stela-boot/src/engine.rs index 7fde77e379..98cd951d3f 100644 --- a/crates/stela-boot/src/engine.rs +++ b/crates/stela-boot/src/engine.rs @@ -1177,6 +1177,7 @@ fn finish( host.guard_faults, EVENTS.load(Ordering::Relaxed), )); + watchdog_report(machine, drained.watchdog); for line in drained.sep { output::arguments(format_args!("SEP OBSERVE {line}\n")); } @@ -1339,12 +1340,15 @@ fn machine_report(machine: &mut Machine) { .filter(|&pe| pe != machine.processor()) .filter_map(|pe| Some((pe, hub.source_observation(pe)?.source))) .filter(|(_, source)| source.el > 0) - .map(|(pe, source)| (pe, source.fp)) + .map(|(pe, source)| (pe, source.fp, source.sp)) .collect::>() }; - for (pe, frame) in sources { + for &(pe, frame, _) in &sources { frames(machine, &alloc::format!("PE {pe} SOURCE FRAME"), frame); } + for (pe, _, sp) in sources { + stack_text_report(machine, pe, sp); + } let cpu = &machine.cpu; for (entry, reads, writes) in cpu.apple.impdef.used() { output::arguments(format_args!( diff --git a/crates/stela-boot/src/engine/reports.rs b/crates/stela-boot/src/engine/reports.rs index 49fdd89219..26d65bed3f 100644 --- a/crates/stela-boot/src/engine/reports.rs +++ b/crates/stela-boot/src/engine/reports.rs @@ -514,6 +514,56 @@ pub(super) fn stack_report(machine: &mut Machine) { text_report(machine); } +/// The format strings another PE's stack points to, from its last +/// materialized SP up: a PE inside `panic` keeps the panic's format and +/// arguments in its frames even when the console never prints them (a PE +/// that never answers the debugger cross-call holds it back). Only words +/// that point to at least 12 printable characters with a `%` are shown, +/// three per PE from 128 words. +pub(super) fn stack_text_report( + machine: &mut Machine, + pe: usize, + sp: u64, +) { + const WORDS: u64 = 128; + const SHOWN: usize = 3; + const LIMIT: usize = 160; + let mut shown = 0; + for word in 0..WORDS { + let mut value = [0u8; 8]; + if !machine.read_virtual(sp.wrapping_add(word * 8), &mut value) { + break; + } + let address = u64::from_le_bytes(value); + if address >> 48 != 0xffff { + continue; + } + let mut text = Vec::new(); + 'read: while text.len() < LIMIT { + let mut byte = [0u8; 1]; + if !machine.read_virtual(address.wrapping_add(text.len() as u64), &mut byte) { + break; + } + let byte = byte[0]; + if byte == 0 || !(byte.is_ascii_graphic() || b" \t\n".contains(&byte)) { + break 'read; + } + text.push(byte); + } + if text.len() >= 12 && text.contains(&b'%') { + let text = core::str::from_utf8(&text).expect("ASCII"); + output::arguments(format_args!( + "PE {pe} STACK TEXT sp+{:#x} {address:#x}: {text:?}\n", + word * 8 + )); + shown += 1; + if shown == SHOWN { + break; + } + } + } +} + /// The text a general-purpose register points to, when it reads as at least /// eight printable characters: a guest's panic message often sits in one at /// a stop, while the console that would print it is off. @@ -557,6 +607,9 @@ pub(super) struct Drained { pub(super) smc: Vec, pub(super) display: Vec, pub(super) power: Vec, + /// The watchdog mechanism's arms and disarms, and how many more were + /// not kept. + pub(super) watchdog: (Vec, u32), } impl Drained { @@ -589,10 +642,40 @@ impl Drained { smc: core::iter::from_fn(|| devices.take_smc_log()).collect(), display: core::iter::from_fn(|| devices.take_display_log()).collect(), power: core::iter::from_fn(|| devices.take_power_log()).collect(), + watchdog: devices.take_watchdog_mechanism_log(), } } } +/// One line per watchdog mechanism write: XNU arms it on its halt, restart +/// and panic path, so the arm dates the start of a reset that follows. The +/// writing PE's pc and lr are its last materialized ones, close to the write +/// but not exact. +pub(super) fn watchdog_report( + machine: &Machine, + (events, dropped): (Vec, u32), +) { + let hub = machine.devices.hub(); + for event in events { + let (pc, lr) = hub + .source_observation(event.cpu) + .map_or((0, 0), |observed| (observed.source.pc, observed.source.lr)); + match event.deadline { + Some(seconds) => output::arguments(format_args!( + "WDT MECHANISM armed deadline_s={seconds} counter={} pe={} source_pc={pc:#x} source_lr={lr:#x}\n", + event.at, event.cpu + )), + None => output::arguments(format_args!( + "WDT MECHANISM disarmed counter={} pe={} source_pc={pc:#x} source_lr={lr:#x}\n", + event.at, event.cpu + )), + } + } + if dropped > 0 { + output::arguments(format_args!("WDT MECHANISM writes_not_kept={dropped}\n")); + } +} + /// The distinct unsupported operations `UNMODELED` lines have reported. pub(super) static UNMODELED_REPORTED: AtomicU64 = AtomicU64::new(0); diff --git a/crates/stela-boot/src/engine/run.rs b/crates/stela-boot/src/engine/run.rs index 7680f2fc99..547c494bd2 100644 --- a/crates/stela-boot/src/engine/run.rs +++ b/crates/stela-boot/src/engine/run.rs @@ -596,6 +596,7 @@ pub(super) fn report_drained( crate::status::stream(lines.iter().map(String::as_str)); crate::status::tick(); unmodeled_report(machine, drained.unmodeled); + watchdog_report(machine, drained.watchdog); for line in drained.sep { output::arguments(format_args!("SEP OBSERVE {line}\n")); } diff --git a/crates/stela-soc/src/machine.rs b/crates/stela-soc/src/machine.rs index 36e0fd05cb..965b096967 100644 --- a/crates/stela-soc/src/machine.rs +++ b/crates/stela-soc/src/machine.rs @@ -221,6 +221,8 @@ pub struct AppleDevices { /// by PE number. core_counters: Vec>, pc_samplers: Vec<(usize, u64, DeviceId)>, + /// The SoC watchdogs, whose mechanism writes the reports show. + watchdogs: Vec, starts: Vec, selects: Vec, /// The AP GPIO bank's base and the board nets on its input pins. @@ -657,6 +659,7 @@ impl AppleDevices { reset_vectors: Vec::new(), core_counters: Vec::new(), pc_samplers: Vec::new(), + watchdogs: Vec::new(), starts: Vec::new(), selects: Vec::new(), gpio: None, diff --git a/crates/stela-soc/src/machine/timekeeping.rs b/crates/stela-soc/src/machine/timekeeping.rs index b43c4ce1c3..ecaa3e119b 100644 --- a/crates/stela-soc/src/machine/timekeeping.rs +++ b/crates/stela-soc/src/machine/timekeeping.rs @@ -87,9 +87,25 @@ impl AppleDevices { now: u64, ) -> Result<(), MapError> { let device = self.bus.add(Box::new(AppleWdt::new(irq, now))); + self.watchdogs.push(device); self.map(device, ranges) } + /// Every watchdog's mechanism writes since the last call, and how many + /// more were not kept. + pub fn take_watchdog_mechanism_log(&mut self) -> (Vec, u32) { + let mut events = Vec::new(); + let mut dropped = 0u32; + for &device in &self.watchdogs { + if let Some(watchdog) = self.bus.device_mut::(device) { + let (log, more) = watchdog.take_mechanism_log(); + events.extend(log); + dropped = dropped.saturating_add(more); + } + } + (events, dropped) + } + /// The main watchdog and its independently DT-placed optional side words. pub fn add_watchdog_with_side( &mut self, @@ -162,6 +178,7 @@ impl AppleDevices { model = model.with_shared_panic_flags(); } let device = self.bus.add(Box::new(model)); + self.watchdogs.push(device); self.map(device, &[range]) .map_err(|error| refuse!("watchdog placement: {error}", ?error))?; let offsets = [ diff --git a/crates/stela-soc/src/wdt.rs b/crates/stela-soc/src/wdt.rs index 21628b27f9..a085175615 100644 --- a/crates/stela-soc/src/wdt.rs +++ b/crates/stela-soc/src/wdt.rs @@ -73,6 +73,22 @@ pub const MECHANISM: u64 = 0x4004; pub const CONFIGURATION: u64 = 0x4008; pub const POWER_OFF: u64 = 0x400c; +/// One write to the watchdog mechanism: XNU arms it on its halt, restart +/// and panic path, so the record dates the start of a reset that follows. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct MechanismEvent { + /// The deadline armed, in system counter seconds; `None` disarms. + pub deadline: Option, + /// The system counter at the write. + pub at: u64, + /// The PE that wrote it. + pub cpu: usize, +} + +/// The most mechanism writes kept between two reports; later ones are +/// counted, not kept. +const MECHANISM_LOG: usize = 8; + enum PowerOff { Separate(u32), /// Both DT views name one physical flags word, not a copied value. @@ -117,6 +133,10 @@ pub struct AppleWdt { panic_save: u32, /// The armed mechanism deadline, in seconds of the system counter. mechanism: Option, + /// Mechanism writes since the last [`AppleWdt::take_mechanism_log`]. + mechanism_log: alloc::vec::Vec, + /// Writes not kept since then, past [`MECHANISM_LOG`]. + mechanism_dropped: u32, configuration: u32, power_off: PowerOff, } @@ -138,10 +158,21 @@ impl AppleWdt { side: false, panic_save: 0, mechanism: None, + mechanism_log: alloc::vec::Vec::new(), + mechanism_dropped: 0, configuration: 0, power_off: PowerOff::Separate(0), } } + /// The mechanism writes since the last call, and how many more were + /// not kept. + pub fn take_mechanism_log(&mut self) -> (alloc::vec::Vec, u32) { + ( + core::mem::take(&mut self.mechanism_log), + core::mem::take(&mut self.mechanism_dropped), + ) + } + /// The optional DT side controls, separate from the timer window. pub fn with_side_controls(mut self) -> Self { self.side = true; @@ -292,6 +323,15 @@ impl MmioDevice for AppleWdt { } if offset == MECHANISM { self.mechanism = (value != 0).then_some(value as u32); + if self.mechanism_log.len() < MECHANISM_LOG { + self.mechanism_log.push(MechanismEvent { + deadline: self.mechanism, + at: access.now, + cpu: access.cpu, + }); + } else { + self.mechanism_dropped = self.mechanism_dropped.saturating_add(1); + } self.evaluate(access); return Ok(()); } @@ -443,6 +483,24 @@ mod tests { assert_eq!(w.mechanism, Some(10)); w.write(MECHANISM, Width::Word, 0, &mut a).unwrap(); assert_eq!(w.mechanism, None); + let (log, dropped) = w.take_mechanism_log(); + assert_eq!(dropped, 0); + assert_eq!( + log, + [ + MechanismEvent { + deadline: Some(10), + at: 0, + cpu: 0 + }, + MechanismEvent { + deadline: None, + at: 0, + cpu: 0 + }, + ] + ); + assert_eq!(w.take_mechanism_log(), (alloc::vec::Vec::new(), 0)); assert!(w.read(MECHANISM, Width::Word, &mut a).is_err()); assert!(w.read(PANIC_SAVE, Width::Double, &mut a).is_err()); assert!(w.read(PANIC_SAVE + 0x10, Width::Word, &mut a).is_err()); @@ -481,6 +539,28 @@ mod tests { assert_eq!(trigger.read(0, Width::Word, &mut a), Ok(1)); } + #[test] + fn mechanism_writes_are_kept_up_to_the_log_bound_then_counted() { + let mut sink = InterruptLatch::new(); + let mut w = AppleWdt::new(None, 0).with_side_controls(); + for second in 1..=10u64 { + let mut a = Access::new(3, second * REFERENCE_HZ, &mut sink); + w.write(MECHANISM, Width::Word, second + 10, &mut a) + .unwrap(); + } + let (log, dropped) = w.take_mechanism_log(); + assert_eq!(log.len(), MECHANISM_LOG); + assert_eq!(dropped, 2); + assert_eq!( + log[0], + MechanismEvent { + deadline: Some(11), + at: REFERENCE_HZ, + cpu: 3 + } + ); + } + #[test] fn an_armed_mechanism_resets_at_its_deadline_unless_rearmed() { let mut latch = InterruptLatch::new();