diff --git a/build/bootlog.rs b/build/bootlog.rs index 85675f4a843..625e255cc59 100644 --- a/build/bootlog.rs +++ b/build/bootlog.rs @@ -41,7 +41,7 @@ pub const DEADLINE_ARMED: &str = "boot deadline: "; /// What the `wedge-before-reset` actuator says before it stops every CPU, in /// `kernel/src/deadline.rs`. The witness that a deadline ended a wedge and not /// a boot merely slower than its bound, which is what makes that control one. -pub const WEDGE_STAGED: &str = "wedge: staged, and only the boot deadline ends this machine"; +pub const WEDGE_STAGED: &str = "wedge: staged, and only a bound this machine runs under ends it"; /// What each CPU the wedge takes says about the state it arrived in, also in /// `kernel/src/deadline.rs`: [`WEDGE_AWAKE`] with interrupts open, and @@ -385,6 +385,14 @@ pub fn foreign_done(loader: &str) -> Result<(), Unfit> { } } +/// Whether the pass after the reset found the page as the loader armed it: a +/// kernel that sealed nothing, its panel census among it. What a reset from +/// outside the kernel, such as the chipset watchdog's, leaves. +pub fn sealed_nothing(loader: &str) -> bool { + let armed = format!("{PREVIOUS_PANIC} the page still reads {}", toyos_blackbox::State::Armed.named()); + after_the_reset(loader).is_some_and(|after| after.contains(&armed)) +} + /// `loader.log` from [`SEPARATOR`] on: the pass that read what this boot left, /// or `None` where the chain did not go round. pub fn after_the_reset(loader: &str) -> Option<&str> { @@ -653,6 +661,15 @@ mod tests { assert!(source.contains(&format), "{} formats no {format:?}", path.display()); } + /// Only the pass after the reset speaks of this boot's page. + #[test] + fn a_page_no_kernel_sealed_is_read_off_the_pass_after_the_reset() { + let armed = format!("{PREVIOUS_PANIC} the page still reads {}, so that kernel died\n", toyos_blackbox::State::Armed.named()); + assert!(sealed_nothing(&format!("first pass\n{SEPARATOR}\n{armed}"))); + assert!(!sealed_nothing(&format!("{armed}{SEPARATOR}\n{PREVIOUS_PANIC} the last boot read WEDGED\n"))); + assert!(!sealed_nothing(&armed), "a loader.log with no pass after the reset"); + } + #[test] fn the_loaders_file_is_told_from_logkeepers_however_a_driver_spelled_it() { let listing = "2026-09-06-084003.log\nloader.log\nunknown-00.log\nnotes.txt\n"; diff --git a/issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md b/issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md index 1bfa99b0691..255271b4b1a 100644 --- a/issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md +++ b/issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md @@ -7,7 +7,7 @@ opened: 2026-09-30 # A frozen ToyOS waits for a hand on the power button No shipped image arms a hardware watchdog or the hard-lockup detector: the -kernel arms the chipset's TCO only on the `watchdog` parameter +kernel arms the chipset's watchdog only on the `watchdog` parameter (`arch::watchdog::init`), feeds it from the scheduler pass (`kernel/src/sched/driver.rs:512`), and arms the detector only through `boot-deadline=` (`kernel/src/deadline.rs:168-183`). @@ -64,7 +64,8 @@ fails to build. `Armed`'s `(0, _)` arm, which nothing reaches, is gone. **Then, once loader stage 7 has taken Ubuntu**, whose `iTCO_wdt_probe` clears `SECOND_TO_STS` (Linux 6.12, `drivers/watchdog/iTCO_wdt.c:545-560`), out of the loop: **the reading**, `watchdog_resets`' metal row in -`issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md`. The +`issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md`, which +judges the AMD bench's FCH and has no TCO judge yet. The owner's ruling (2026-10-03): one T14 boot may stop feeding the chipset's watchdog on purpose, "only if its quick i dont want tests doing nothing for a long time". So the row's wait is bounded by the watchdog's own bound and fails diff --git a/issues/no-watchdog-arms-on-an-amd-machine.md b/issues/no-watchdog-arms-on-an-amd-machine.md deleted file mode 100644 index c9942e10096..00000000000 --- a/issues/no-watchdog-arms-on-an-amd-machine.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -status: open -kind: defect -opened: 2026-10-12 ---- - -# No watchdog arms on an AMD machine - -The loader and the kernel arm only an Intel PCH's TCO timer -(`kernel/src/arch/x86_64/watchdog.rs`, `toyos_tco`). On the AMD bench both say -there is none — `watchdog: no function on bus 0 carries a TCO block this -loader knows` and `watchdog: no PCI function here carries a TCO block this -kernel knows — not armed` (PR #874's whole-suite run on that bench) — so a -frozen boot there waits for a hand on the power button, and the -`loader_watchdog_arms` metal row is red there. AMD's fusion controller hub -carries a watchdog timer of its own; its register map is unread here. - -Owned by `issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md`, -whose ruling is a watchdog on every machine. - -**Exit**: an AMD machine's boot arms its chipset's watchdog, and a row on the -AMD bench reads it armed. diff --git a/issues/the-fch-watchdog-is-judged-armed-without-family-1ahs-action-enable.md b/issues/the-fch-watchdog-is-judged-armed-without-family-1ahs-action-enable.md new file mode 100644 index 00000000000..5927cba1e26 --- /dev/null +++ b/issues/the-fch-watchdog-is-judged-armed-without-family-1ahs-action-enable.md @@ -0,0 +1,23 @@ +--- +status: open +kind: finding +opened: 2026-10-28 +--- + +# The FCH watchdog is judged armed without Family 1Ah's action enable + +On Family 1Ah, `WatchdogControl` bit 4, `watchdogactionen`, is set by MP1 and +clears on entry to a power-saving mode; while it is 0 an expiry sets +`WatchdogFired` and resets nothing (58264 §12.3.6). The same bit is reserved on +Family 17h, so `toyos_tco::fch::arm` and the kernel's take-over keep it as read +and the loader reports it as `action_en=`, but nothing judges it. A Family 1Ah +machine that reads 0 there is reported armed with no reset behind its bound. +No Family 1Ah machine is in reach. + +Because the bit clears on power-saving entry, a check at the arm alone does +not hold the bound for the boot; the kernel's feed already reads +`WatchdogControl`. + +**Exit**: on a Family 1Ah FCH, an arm that reads `watchdogactionen` 0 is +refused, a feed that reads it 0 says the machine is unwatched, and host model +cases hold both. diff --git a/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md b/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md index 06b0e24b399..9d4c2f62f21 100644 --- a/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md +++ b/issues/the-guest-suite-runs-only-what-no-cheaper-tier-reaches.md @@ -68,7 +68,8 @@ console's renderer. Exit: as stage A. `nested_fault_is_recursive`: a fault inside the report names the first. - metal `pre_idle_wedge_speaks`: a boot stopped in phase 3 says where. - metal `panic_halts_the_others_first`: no other CPU's record follows the fatal line. -- metal `watchdog_resets`: the TCO ends the machine. +- metal `watchdog_resets`: the chipset's watchdog ends the machine. Judged on the AMD bench's FCH; + the T14's TCO has never reset it (`issues/an-armed-tco-has-never-reset-the-t14.md`). - metal `quiesce_refuses_a_second_shutdown`: the machine has one shutdown, and a second caller is refused while the first holds it. - host `screen_early_panel`: the panel repaints after each committed record. diff --git a/issues/the-loader-does-only-what-must-precede-the-handover.md b/issues/the-loader-does-only-what-must-precede-the-handover.md index bcbc3824d5f..a16a6dce0ed 100644 --- a/issues/the-loader-does-only-what-must-precede-the-handover.md +++ b/issues/the-loader-does-only-what-must-precede-the-handover.md @@ -391,10 +391,20 @@ Each stage lands on its own, in this order. `TCO_RLD` at the arm and at the first feed (the counting read, with no stall), `TCO1_STS`, and `no_reboot`, `tco_lock` and `timeout`. That issue's exit reads the kernel's log in place of `loader.log`. - - Deleted: `loader/src/watchdog.rs`, `arch::pio`, the kernel's + - On an FCH the arm needs `PMx000`: the decode, which also gates the + timer, and the tick. That PM block shares its page with registers AML + uses, and a window `map_mmio` maps is refused the `acpi` claim's holder + for the machine's life. How the kernel reaches it and leaves the page to + AML is not decided. + - `loader/src/watchdog.rs`' `survey` stays: on every pass, the passes + that boot no kernel among them, it reads and clears the FCH's + `S5ResetStat` and `WatchdogFired` and stops a count the last boot left. + It goes to the kernel with stage 9, which removes those passes, and + needs the same PM access. + - Deleted: `loader/src/watchdog.rs`' arm, `arch::pio`, the kernel's on-arrival read with `ARMED_ON_ARRIVAL` and `UNARMED_ON_ARRIVAL`, and - `tests/common/power.rs`' `loader_armed`, `armed_on_arrival` and - `watchdog_quiet`'s loader half. `watchdog_armed` judges the kernel's + `tests/common/power.rs`' `loader_armed` and `watchdog_quiet`'s loader + half. `watchdog_armed` judges the kernel's read-back, and `loader_watchdog_arms` becomes the kernel's row. **Exit**: `loader/src` holds no TCO access. On the T14, `watchdog_armed` diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index fc229b7890f..e6d5e826f45 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -5,7 +5,7 @@ //! [`percpu`] owns every `gs:` access; nothing outside this directory writes one. //! //! The PC platform's own devices live here too, because no other architecture -//! has them: the I/O APIC, the CMOS RTC, the chipset's TCO watchdog, VT-d and +//! has them: the I/O APIC, the CMOS RTC, the chipset's watchdog, VT-d and //! AMD-Vi. Generic code reaches each through the concept it serves //! (`watchdog`, `iommu_unit`, …), never by its name. diff --git a/kernel/src/arch/x86_64/watchdog.rs b/kernel/src/arch/x86_64/watchdog.rs index 82839593dc5..c77cd87fc8d 100644 --- a/kernel/src/arch/x86_64/watchdog.rs +++ b/kernel/src/arch/x86_64/watchdog.rs @@ -1,11 +1,13 @@ -//! The chipset's TCO watchdog, armed on request and fed from the scheduler -//! pass — the one function an idle CPU and a busy one both run every trip, so -//! **what it proves alive is that some CPU still reaches it**. A panicked -//! machine is reset by the same bound, which is the loop's recovery: logkeeper is -//! dead after a kernel panic, so nothing more could be made durable anyway. +//! The chipset's watchdog, Intel's TCO or AMD's FCH timer, armed on request +//! and fed from the scheduler pass — the one function an idle CPU and a busy +//! one both run every trip, so **what it proves alive is that some CPU still +//! reaches it**. A panicked machine is reset by the same bound, which is the +//! loop's recovery: logkeeper is dead after a kernel panic, so nothing more +//! could be made durable anyway. use core::sync::atomic::{AtomicBool, AtomicU64, Ordering}; +use toyos_tco::fch; use toyos_tco::{ Chipset, TCO1_CNT, TCO1_CNT_HALT, TCO1_CNT_RUN, TCO2_STS, TCO_BOOT_STS, TCO_RLD, TCO_SECOND_TO_STS, TCO_TMR, TCO_TMR_HLT, TIMER, @@ -14,6 +16,9 @@ use toyos_tco::{ use crate::arch::pio::{self, Declared, Slot}; use crate::drivers::pci::PciDevice; use crate::log; +use crate::mm::policy::MmioPolicy; +use crate::mm::Mmio; +use crate::sync::Lock; /// Four, so the shipped 9.6 s bound is fed every 2.4 s. What makes a cadence /// that long sound is `kernel/CLAUDE.md`'s rule that no disk wait in this kernel @@ -32,6 +37,10 @@ const UNARMED_ON_ARRIVAL: &str = "nothing had armed the timer"; /// The TCO block, declared by `init`. static TCO: Slot = Slot::empty(); +/// The FCH's window, mapped by `init`. A lock, because a feed is a +/// read-modify-write that carries `WatchdogRunStopB` as read: one racing +/// `disarm`'s stop would start the timer again. +static FCH: Lock> = Lock::new(None); /// Written by `init` on the BSP before any AP exists, so a relaxed load is the whole of the ordering these need. static ARMED: AtomicBool = AtomicBool::new(false); static NEXT_FEED: AtomicU64 = AtomicU64::new(u64::MAX); @@ -41,16 +50,18 @@ pub fn init(devices: &[PciDevice]) { if !crate::params::watchdog() { return; } - let timer = TIMER; - - let Some((pci, row)) = devices - .iter() - .find_map(|d| toyos_tco::chipset(d.vendor_id(), d.device_id()).map(|row| (d, row))) - else { - log!("watchdog: no PCI function here carries a TCO block this kernel knows — not armed"); - return; - }; + for pci in devices { + if let Some(row) = toyos_tco::chipset(pci.vendor_id(), pci.device_id()) { + return init_tco(pci, row); + } + if fch::carries(pci.vendor_id(), pci.device_id(), pci.revision_id()) { + return init_fch(pci); + } + } + log!("watchdog: no PCI function here carries a chipset watchdog this kernel knows — not armed"); +} +fn init_tco(pci: &PciDevice, row: &Chipset) { let base = pci.read_config_u32(u64::from(row.base_reg)); // One read where a chipset keeps both in one register, which q35 does. let enable = if row.enable.reg == row.base_reg { @@ -75,10 +86,10 @@ pub fn init(devices: &[PciDevice]) { } }; TCO.set(block); - arm(row, block, timer); + arm_tco(row, block, TIMER); } -fn arm(row: &Chipset, block: Declared, timer: u16) { +fn arm_tco(row: &Chipset, block: Declared, timer: u16) { let port = block.ports().first(); // Read before anything here is written: the bootloader arms the same timer // on the same port and hands over a machine already inside the bound, and @@ -116,9 +127,7 @@ fn arm(row: &Chipset, block: Declared, timer: u16) { } let bound_ms = toyos_tco::bound_of(timer); - FEED_EVERY_NS.store(bound_ms * 1_000_000 / FEEDS_PER_BOUND, Ordering::Relaxed); - NEXT_FEED.store(0, Ordering::Relaxed); - ARMED.store(true, Ordering::Relaxed); + feeding(bound_ms); log!( "watchdog: {:04x}:{:04x} TCO at {port:#x} TCO_TMR={timer} — this machine resets if no \ scheduler pass runs for {bound_ms}ms", @@ -127,6 +136,59 @@ fn arm(row: &Chipset, block: Declared, timer: u16) { ); } +/// The FCH's watchdog window, in `fch`'s terms. +struct FchWindow(Mmio); + +impl fch::Window for FchWindow { + fn read(&mut self, register: fch::Register) -> u32 { + self.0.read_u32(register as u64) + } + + fn write(&mut self, register: fch::Register, value: u32) { + self.0.write_u32(register as u64, value) + } +} + +/// Take over the bootloader's arm. **The kernel arms nothing of its own +/// here**: the tick the count is in is `PMx000`'s, which the bootloader +/// selects, and the PM block's page is AML's, which the `acpi` claim's holder +/// is refused once this kernel maps it. +fn init_fch(pci: &PciDevice) { + let mapped = crate::mm::paging::map_mmio(fch::WDT, fch::WDT_BYTES, MmioPolicy::Uncacheable); + let mut window = FCH.lock(); + let wdt = window.insert(FchWindow(mapped)); + let found = fch::Found::read(wdt); + let already = found.armed(); + log!( + "watchdog: FCH WatchdogControl={:#010x} WatchdogCount={} on arrival, so {}", + found.control.0, + found.count, + if already { ARMED_ON_ARRIVAL } else { UNARMED_ON_ARRIVAL } + ); + if !already { + log!("watchdog: the FCH's tick is the bootloader's to select, and it armed nothing — not armed"); + return; + } + fch::feed(wdt); + + let bound_ms = fch::bound_of(fch::TIMER); + feeding(bound_ms); + log!( + "watchdog: {:04x}:{:04x} FCH watchdog at {:#x} WatchdogCount={} — this machine resets if \ + no scheduler pass runs for {bound_ms}ms", + pci.vendor_id(), + pci.device_id(), + fch::WDT, + fch::TIMER + ); +} + +fn feeding(bound_ms: u64) { + FEED_EVERY_NS.store(bound_ms * 1_000_000 / FEEDS_PER_BOUND, Ordering::Relaxed); + NEXT_FEED.store(0, Ordering::Relaxed); + ARMED.store(true, Ordering::Relaxed); +} + /// Reload the timer, at most once per feed cadence across every CPU. pub fn feed(now: u64) { // What an unarmed machine pays, and all of it: `NEXT_FEED` is `u64::MAX`. @@ -137,23 +199,34 @@ pub fn feed(now: u64) { if !ARMED.load(Ordering::Relaxed) { return; } - let block = TCO.get().expect("watchdog: armed with no TCO block"); let next = now + FEED_EVERY_NS.load(Ordering::Relaxed); - // A claim, so concurrent CPUs write the port once between them rather than each. + // A claim, so concurrent CPUs write the register once between them rather than each. if NEXT_FEED.compare_exchange(due, next, Ordering::Relaxed, Ordering::Relaxed).is_err() { return; } - // SAFETY: as `arm`'s, and a reload racing `disarm` restarts nothing — `hw/acpi/ich9_tco.c:146` reloads only while `TCO_TMR_HLT` is clear, and the PCH half is unverified. - unsafe { crate::arch::cpu::outw(block.port(TCO_RLD), 1) }; + match TCO.get() { + // SAFETY: as `arm_tco`'s, and a reload racing `disarm` restarts nothing — `hw/acpi/ich9_tco.c:146` reloads only while `TCO_TMR_HLT` is clear, and the PCH half is unverified. + Some(block) => unsafe { crate::arch::cpu::outw(block.port(TCO_RLD), 1) }, + None => { + let mut window = FCH.lock(); + // Asked again under the lock `disarm` stops the timer under. + if ARMED.load(Ordering::Relaxed) { + fch::feed(window.as_mut().expect("watchdog: armed with neither a TCO block nor an FCH window")); + } + } + } } pub fn disarm() { if !ARMED.swap(false, Ordering::Relaxed) { return; } - let block = TCO.get().expect("watchdog: armed with no TCO block"); NEXT_FEED.store(u64::MAX, Ordering::Relaxed); - // SAFETY: as `arm`'s. - unsafe { crate::arch::cpu::outw(block.port(TCO1_CNT), TCO1_CNT_HALT) }; + match TCO.get() { + // SAFETY: as `arm_tco`'s. + Some(block) => unsafe { crate::arch::cpu::outw(block.port(TCO1_CNT), TCO1_CNT_HALT) }, + // Before the reset that follows, which the FCH's block survives. + None => fch::stop(FCH.lock().as_mut().expect("watchdog: armed with neither a TCO block nor an FCH window")), + } log!("watchdog: disarmed"); } diff --git a/kernel/src/deadline.rs b/kernel/src/deadline.rs index 07d40d8b44f..02e87a9f6e7 100644 --- a/kernel/src/deadline.rs +++ b/kernel/src/deadline.rs @@ -287,7 +287,7 @@ pub fn stage_a_wedge() -> ! { /// hold this line is a machine the deadline ended for some other reason. Judged /// by the harness, so it is a constant (`build/bootlog.rs`). #[cfg(feature = "boot-actuators")] -pub const WEDGE_STAGED: &str = "wedge: staged, and only the boot deadline ends this machine"; +pub const WEDGE_STAGED: &str = "wedge: staged, and only a bound this machine runs under ends it"; /// A scheduler pass's first statement, where a staged wedge takes the CPU. #[cfg(feature = "boot-actuators")] diff --git a/kernel/src/drivers/pci.rs b/kernel/src/drivers/pci.rs index fd3bc681a00..d52087ecf58 100644 --- a/kernel/src/drivers/pci.rs +++ b/kernel/src/drivers/pci.rs @@ -10,6 +10,7 @@ use crate::log; const VENDOR_ID: u64 = 0x00; const DEVICE_ID: u64 = 0x02; const COMMAND: u64 = 0x04; +const REVISION_ID: u64 = 0x08; /// `COMMAND` bit 2, PCI 3.0 §6.2.2: without it the function issues no transaction of its own. const BUS_MASTER: u16 = 0x04; /// `COMMAND` bit 1, PCI 3.0 §6.2.2: without it the function decodes none of its BARs. @@ -134,6 +135,10 @@ impl PciDevice { self.mmio.read_u16(DEVICE_ID) } + pub fn revision_id(&self) -> u8 { + self.mmio.read_u8(REVISION_ID) + } + pub fn read_config_u8(&self, offset: u64) -> u8 { self.mmio.read_u8(offset) } diff --git a/lib/tco/Cargo.toml b/lib/tco/Cargo.toml index 5f75edec6ed..54998871369 100644 --- a/lib/tco/Cargo.toml +++ b/lib/tco/Cargo.toml @@ -1,13 +1,12 @@ # A member of the host workspace (root `Cargo.toml`), like toyos-pci: the # kernel depends on it by path and its tests run on the host. -# What lives here is where a chipset keeps its TCO watchdog and what timer value -# a bound in milliseconds comes to — arithmetic that decides which I/O port the -# kernel writes on a real laptop, which is exactly the decision a guest cannot -# exercise and a host test can. +# What lives here is where a chipset keeps its watchdog and what timer value a +# bound in milliseconds comes to — decisions about which register the kernel +# writes on a real machine, which a guest cannot exercise and a host test can. [package] name = "toyos-tco" -description = "Every bound a boot arms and the metal loop waits on, and Intel's TCO watchdog register block and where a chipset keeps it." +description = "Every bound a boot arms and the metal loop waits on, and the chipset watchdogs that hold it: Intel's TCO and AMD's FCH watchdog." version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" diff --git a/lib/tco/src/fch.rs b/lib/tco/src/fch.rs new file mode 100644 index 00000000000..aba3cd5ee30 --- /dev/null +++ b/lib/tco/src/fch.rs @@ -0,0 +1,388 @@ +//! AMD's FCH watchdog timer: where it is, how it is armed, fed and stopped, +//! and what the FCH records of the last reset. Pure: the caller supplies the +//! two register windows, and a host model drives every sequence here. +//! +//! Registers are cited from AMD's PPR for Family 17h Model 18h (55570-B1 +//! Rev 3.16), whose FCH chapter the public Renoir PPR (55922) does not have. +//! 58264 (Family 1Ah) and 52740 (Family 16h Models 30h-3Fh) describe the same +//! map. That Renoir's FCH matches is inferred from the generations on either +//! side of it and from its SMBus function's revision. +//! +//! **The WDT block is in the S5 power domain**, reset only by RsmRst and the +//! reset button (55570 Table 74). A CF9 reset and the watchdog's own expiry +//! leave its control and count as they were. What keeps a count the last boot +//! left harmless is `PMx000[7]`, which gates the timer as well as its window +//! (55570 and 58264, `PMx000`): firmware may clear it at reset, and where it +//! stays set [`survey`] stops the count. [`arm`] stops the timer before it +//! writes its own count. `WatchdogFired`, cleared +//! only by a cold reset, outlives warm boots the same way, so [`survey`] and +//! [`arm`] clear it wherever they can read it. + +/// The function this watchdog is found by: the FCH's SMBus controller. +pub const VENDOR: u16 = 0x1022; +pub const SMBUS: u16 = 0x790b; +/// The first revision whose FCH puts the watchdog at the fixed window behind +/// `PmDecodeEn[WatchdogTmrEn]`, with its PM block in ACPI-MMIO: Family 17h +/// and later. Earlier ones keep a programmable base in PM 48h, a layout this +/// module does not drive. +pub const MIN_REVISION: u8 = 0x51; + +pub fn carries(vendor: u16, device: u16, revision: u8) -> bool { + vendor == VENDOR && device == SMBUS && revision >= MIN_REVISION +} + +/// The ACPI-MMIO region, fixed on these parts (55570 PMx004 `MmioEn`, reset +/// and BIOS-init value 1; 56569 §7.2.2.1). [`AcpiMmio`] offsets count from here. +pub const ACPI_MMIO: u64 = 0xFED8_0000; +/// The bytes of it this module reads: through `MISCx0FC`. +pub const ACPI_MMIO_BYTES: u64 = 0xF00; +const PM: u16 = 0x300; + +/// `PMx000 PmDecodeEn`. Bit 7, `WatchdogTmrEn`, decodes the window at [`WDT`] +/// and enables the timer (55570; 58264). Byte 3 holds `WatchDogOptions` +/// [27:26], which must be 0, and `WatchDogFreq` [25:24]. +pub const PM_DECODE_EN: u16 = PM; +pub const WATCHDOG_TMR_EN: u32 = 1 << 7; +const DECODE_EN_BYTE3: u16 = PM + 3; +/// `WatchDogOptions` and `WatchDogFreq` within byte 3. +const OPTIONS_AND_FREQ: u8 = 0x0f; +/// `PMx0BE ResetCtl1`, bit 7 `RstToCpuPwrGdEn`: every FCH reset toggles +/// CpuPwrGood, a cold reset. A platform policy over CF9 and every other reset +/// alike, so it is read and reported and never written here. +pub const PM_RESET_CTL1: u16 = PM + 0xBE; +pub const RST_TO_CPU_PWR_GD_EN: u8 = 1 << 7; +/// `PMx0C0 S5ResetStat`: the source of the last reset, write-1-to-clear. +/// Hardware clears [27:16] on each new reset except the bit of that reset +/// (55570), but not every reset updates it, so it is cleared once read. +pub const PM_S5_RESET_STAT: u16 = PM + 0xC0; +/// Bit 25, `WatchdogIssueReset`. +pub const WATCHDOG_ISSUE_RESET: u32 = 1 << 25; +/// `PMx0C4` bit 2 `SelectDebug`: while set, `PMx0C0` reads as a debug register. +pub const PM_S5_RESET_STAT_SELECT: u16 = PM + 0xC4; +pub const SELECT_DEBUG: u8 = 1 << 2; +/// `PMx0C8 Misc`, bit 19 `UseCpuRst` (reset 1): when 0 a system reset asserts +/// INIT# in place of CPURST#, and an expiry would not reset the CPUs. +pub const PM_MISC: u16 = PM + 0xC8; +pub const USE_CPU_RST: u32 = 1 << 19; +/// `PMx0D5` bit 0 `AltAcpiMmioEn`: an alternate ACPI-MMIO base, which may move +/// the region this module reads at a fixed address. Refused. +pub const PM_ALT_ACPI_MMIO_EN: u16 = PM + 0xD5; +/// `MISCx0FC ClkGatingCntrl`, bit 12 `WatchDogTimerBlkClkGatingDis` (reset 1): +/// while 0 the block's clock may stop when idle, and whether it then counts is +/// undocumented (55570 §9.2.10.1). Reported. +pub const MISC_CLK_GATING: u16 = 0xEFC; +pub const WDT_BLK_CLK_GATING_DIS: u32 = 1 << 12; + +/// The watchdog's own window, decoded while [`WATCHDOG_TMR_EN`] is set (55570 +/// §9.2.6). Non-posted, so mapped uncached (55570 §9.1.3). +pub const WDT: u64 = 0xFEB0_0000; +/// The two registers driven here; the decode is sixteen bytes. +pub const WDT_BYTES: u64 = 8; + +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Register { + /// `WatchdogControl`. + Control = 0x0, + /// `WatchdogCount` [15:0]: a write sets the reload, a read is the counter. + Count = 0x4, +} + +/// The ACPI-MMIO region from [`ACPI_MMIO`]: bytes for the PM block, as its +/// registers are byte-addressed, and dwords for `PMx0C0` and `MISCx0FC`. +pub trait AcpiMmio { + fn read_u8(&mut self, offset: u16) -> u8; + fn write_u8(&mut self, offset: u16, value: u8); + fn read_u32(&mut self, offset: u16) -> u32; + fn write_u32(&mut self, offset: u16, value: u32); +} + +/// The window at [`WDT`], in dwords. +pub trait Window { + fn read(&mut self, register: Register) -> u32; + fn write(&mut self, register: Register, value: u32); +} + +/// `WatchdogControl` as read (55570 §9.2.6; 58264 §12.3.6). +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct Control(pub u32); + +impl Control { + /// `WatchdogRunStopB`. Setting it does not start a count interval: + /// [`TRIGGER`](Self::TRIGGER) does, and only once running. + pub const RUN: u32 = 1 << 0; + /// `WatchdogFired`, write-1-to-clear: the watchdog caused the current + /// restart. Only a cold reset clears it otherwise. + pub const FIRED: u32 = 1 << 1; + /// `WatchdogAction`: 0 resets the system, 1 powers it off. + pub const POWER_OFF: u32 = 1 << 2; + /// `WatchdogDisable`, read-only, the inverse of [`WATCHDOG_TMR_EN`]. + pub const DISABLED: u32 = 1 << 3; + /// `watchdogactionen` on Family 1Ah, set by MP1: while 0 an expiry sets + /// `FIRED` and does nothing else. Reserved on Family 17h. + pub const ACTION_EN: u32 = 1 << 4; + /// `WatchdogTrigger`, write-1-only, reads 0: restart the count from the + /// last value written to [`Register::Count`]. + pub const TRIGGER: u32 = 1 << 7; + + /// An undecoded window reads all ones. + pub fn decoded(self) -> bool { + self.0 != u32::MAX + } + + pub fn usable(self) -> bool { + self.decoded() && self.0 & Self::DISABLED == 0 + } + + pub fn running(self) -> bool { + self.usable() && self.0 & Self::RUN != 0 + } + + pub fn fired(self) -> bool { + self.usable() && self.0 & Self::FIRED != 0 + } + + pub fn powers_off(self) -> bool { + self.0 & Self::POWER_OFF != 0 + } + + /// The word that writes this one back changing nothing: `FIRED` written 0 + /// keeps it, and every other bit, `ACTION_EN` among them, is as read. + fn kept(self) -> u32 { + self.0 & !(Self::FIRED | Self::TRIGGER) + } +} + +/// The tick `WatchDogFreq` selects. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Tick { + Us32 = 0, + Ms10 = 1, + Ms100 = 2, + S1 = 3, +} + +/// 100 ms: the coarsest tick at which [`crate::BOUND_MS`] is a whole count. +/// One second, the reset value, cannot express it. +pub const TICK: Tick = Tick::Ms100; +pub const TICK_MS: u64 = 100; + +/// The count that is [`crate::BOUND_MS`]. One expiry resets the machine. +pub const TIMER: u16 = { + assert!(crate::BOUND_MS.is_multiple_of(TICK_MS), "the bound is not a whole number of ticks"); + let ticks = crate::BOUND_MS / TICK_MS; + assert!(ticks >= 1 && ticks <= u16::MAX as u64, "the bound is no 16-bit count"); + ticks as u16 +}; + +pub const fn bound_of(count: u16) -> u64 { + count as u64 * TICK_MS +} + +/// Why [`arm`] left the machine unwatched. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Refused { + /// `PMx000` reads all ones: nothing decodes ACPI-MMIO. + AcpiMmioAbsent, + /// `PMx0D5`, with `AltAcpiMmioEn` set. + AlternateBase(u8), + /// `PMx000` after the write, `WatchdogTmrEn` still clear. + DecodeStuck(u32), + /// `PMx000` after the write, byte 3 not [`TICK`] with options 0. + TickStuck(u32), + /// `WatchdogControl` reads all ones with the decode on. + WindowAbsent, + /// `WatchdogControl` reads `WatchdogDisable`. + Disabled(u32), + /// `WatchdogControl` after the start, not running to a reset. + NotStarted(u32), +} + +/// Stop the timer where its window answers. [`arm`]'s refusals of a tick or +/// a start that did not take go through here, so a count left from the last +/// boot does not resume behind them; the others leave nothing to stop. +fn stop_where_decoded(fch: &mut impl Window) { + if Control(fch.read(Register::Control)).decoded() { + stop(fch); + } +} + +/// What [`arm`] left counting. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct Armed { + pub decode_en: u32, + pub control: Control, + pub count: u16, + /// A `WatchdogFired` it found and cleared: one [`survey`] could not read, + /// which says nothing of the last reset. + pub cleared_fired: bool, +} + +/// Enable the window, select [`TICK`], and start a count of [`TIMER`] to a +/// system reset. A refusal once the decode is on leaves no timer running. +pub fn arm(fch: &mut (impl AcpiMmio + Window)) -> Result { + let decode_en = fch.read_u32(PM_DECODE_EN); + if decode_en == u32::MAX { + return Err(Refused::AcpiMmioAbsent); + } + let alternate = fch.read_u8(PM_ALT_ACPI_MMIO_EN); + if alternate & 1 != 0 { + return Err(Refused::AlternateBase(alternate)); + } + if decode_en & WATCHDOG_TMR_EN == 0 { + fch.write_u8(PM_DECODE_EN, decode_en as u8 | WATCHDOG_TMR_EN as u8); + } + let byte3 = (decode_en >> 24) as u8; + let wanted = byte3 & !OPTIONS_AND_FREQ | TICK as u8; + if byte3 != wanted { + fch.write_u8(DECODE_EN_BYTE3, wanted); + } + let decode_en = fch.read_u32(PM_DECODE_EN); + if decode_en & WATCHDOG_TMR_EN == 0 { + return Err(Refused::DecodeStuck(decode_en)); + } + if (decode_en >> 24) as u8 & OPTIONS_AND_FREQ != TICK as u8 { + stop_where_decoded(fch); + return Err(Refused::TickStuck(decode_en)); + } + + let was = Control(fch.read(Register::Control)); + if !was.decoded() { + return Err(Refused::WindowAbsent); + } + if !was.usable() { + return Err(Refused::Disabled(was.0)); + } + let stopped = was.kept() & !(Control::RUN | Control::POWER_OFF); + fch.write(Register::Control, stopped | Control::FIRED); + fch.write(Register::Count, u32::from(TIMER)); + // Two writes: one setting both bits does not start the count, which an + // AMD engineer measured and the register text implies. + fch.write(Register::Control, stopped | Control::RUN); + fch.write(Register::Control, stopped | Control::RUN | Control::TRIGGER); + + let control = Control(fch.read(Register::Control)); + if !control.running() || control.powers_off() { + stop_where_decoded(fch); + return Err(Refused::NotStarted(control.0)); + } + Ok(Armed { decode_en, control, count: fch.read(Register::Count) as u16, cleared_fired: was.fired() }) +} + +/// Restart the count from [`TIMER`]. +pub fn feed(wdt: &mut impl Window) { + let control = Control(wdt.read(Register::Control)); + wdt.write(Register::Control, control.kept() | Control::TRIGGER); +} + +pub fn stop(wdt: &mut impl Window) { + let control = Control(wdt.read(Register::Control)); + wdt.write(Register::Control, control.kept() & !Control::RUN); +} + +/// The timer as a boot finds it. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct Found { + pub control: Control, + pub count: u16, +} + +impl Found { + pub fn read(wdt: &mut impl Window) -> Self { + Self { control: Control(wdt.read(Register::Control)), count: wdt.read(Register::Count) as u16 } + } + + /// Counting to a reset within [`TIMER`]: the loader's arm, which leaves + /// no timer running when it refuses. + pub fn armed(self) -> bool { + self.control.running() && !self.control.powers_off() && self.count <= TIMER + } +} + +/// `PMx0C0` as [`survey`] found it. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum ResetStat { + /// Read, written back to clear, and read again. + Read { stat: u32, after: u32 }, + /// `SelectDebug` is set, with `PMx0C4` as read, so it was not read. + DebugSelected(u8), + /// All ones, which is no status. + Absent, +} + +/// What [`survey`] read and did. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub struct Survey { + pub decode_en: u32, + pub reset_stat: ResetStat, + /// `None` while [`WATCHDOG_TMR_EN`] is clear, which leaves the window + /// undecoded. + pub found: Option, + /// Whether it found the timer running and stopped it. + pub stopped: bool, +} + +/// Why [`survey`] read nothing: the region it would write is not where it reads. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum Unsurveyed { + AcpiMmioAbsent, + AlternateBase(u8), +} + +impl Survey { + pub fn issued_reset(&self) -> bool { + matches!(self.reset_stat, ResetStat::Read { stat, .. } if stat & WATCHDOG_ISSUE_RESET != 0) + } + + /// `None` where the window is undecoded, which leaves it unread. + pub fn fired(&self) -> Option { + self.found.map(|found| found.control.fired()) + } + + /// Either record says the watchdog caused the last reset. + pub fn watchdog_reset(&self) -> bool { + self.issued_reset() || self.fired() == Some(true) + } +} + +/// What the loader says on a pass whose [`Survey::watchdog_reset`] holds. +pub const RESET_SAID: &str = "the FCH watchdog caused the last reset"; + +/// Read and clear both records of the last reset's cause, and stop a timer +/// this boot has not armed. +pub fn survey(fch: &mut (impl AcpiMmio + Window)) -> Result { + let decode_en = fch.read_u32(PM_DECODE_EN); + if decode_en == u32::MAX { + return Err(Unsurveyed::AcpiMmioAbsent); + } + let alternate = fch.read_u8(PM_ALT_ACPI_MMIO_EN); + if alternate & 1 != 0 { + return Err(Unsurveyed::AlternateBase(alternate)); + } + let select = fch.read_u8(PM_S5_RESET_STAT_SELECT); + let reset_stat = if select & SELECT_DEBUG != 0 { + ResetStat::DebugSelected(select) + } else { + match fch.read_u32(PM_S5_RESET_STAT) { + u32::MAX => ResetStat::Absent, + stat => { + if stat != 0 { + fch.write_u32(PM_S5_RESET_STAT, stat); + } + ResetStat::Read { stat, after: fch.read_u32(PM_S5_RESET_STAT) } + } + } + }; + let found = (decode_en & WATCHDOG_TMR_EN != 0).then(|| Found::read(fch)); + let stopped = found.is_some_and(|found| found.control.running()); + if let Some(found) = found.filter(|found| stopped || found.control.fired()) { + let mut word = found.control.kept(); + if stopped { + word &= !Control::RUN; + } + if found.control.fired() { + word |= Control::FIRED; + } + fch.write(Register::Control, word); + } + Ok(Survey { decode_en, reset_stat, found, stopped }) +} diff --git a/lib/tco/src/lib.rs b/lib/tco/src/lib.rs index d0b6e9c0ab8..44f7ed05e94 100644 --- a/lib/tco/src/lib.rs +++ b/lib/tco/src/lib.rs @@ -1,5 +1,6 @@ -//! Every bound a boot arms and the metal loop waits on, and Intel's TCO -//! watchdog: its register block, and where a chipset keeps it. +//! Every bound a boot arms and the metal loop waits on, and the chipset +//! watchdogs that hold [`BOUND_MS`]: Intel's TCO, here, and AMD's FCH +//! watchdog, in [`fch`]. //! //! The block's layout is one thing across the generations this kernel targets; //! **where its base comes from is not**, so that is a table keyed by PCI id @@ -19,6 +20,8 @@ #![no_std] #![forbid(unsafe_code)] +pub mod fch; + /// Offsets from the block's base port. pub const TCO_RLD: u16 = 0x00; pub const TCO1_STS: u16 = 0x04; @@ -96,12 +99,13 @@ pub const fn timer_for(bound_ms: u64) -> Option { /// admits: the two differ, and a bound derived for hardware answers to this one. pub const TMR_MIN_HARDWARE: u16 = 0x04; -/// The TCO bound, in milliseconds, for the loader **and** the kernel: the -/// bootloader arms it before it jumps and the kernel keeps feeding that same -/// timer, so it is the whole machine's bound from the handoff onward and not a -/// handoff window the kernel widens afterwards. The largest the tick and the -/// double expiry make exact at or under ten seconds: eight ticks of 600 ms, -/// twice, fed four times a bound. +/// The chipset watchdog's bound, in milliseconds, for the loader **and** the +/// kernel: the bootloader arms it before it jumps and the kernel keeps feeding +/// that same timer, so it is the whole machine's bound from the handoff onward +/// and not a handoff window the kernel widens afterwards. The largest the TCO's +/// tick and double expiry make exact at or under ten seconds: eight ticks of +/// 600 ms, twice, fed four times a bound. The FCH counts it as 96 ticks of +/// 100 ms ([`fch::TIMER`]). pub const BOUND_MS: u64 = 9_600; /// The bound the firmware's own watchdog is set to, in milliseconds. It covers @@ -161,6 +165,14 @@ const _: () = assert!(wedge_bound_ms(JOB_BOUND_MS) > JOB_BOUND_MS); /// the lockup probe's own reach to its lock. pub const STAGED_BOUND_MS: u64 = 10_000; +/// The bound a boot that stages its wedge under the chipset watchdog carries, +/// in milliseconds, so that the watchdog ends it and not the deadline: the wedge +/// has begun by [`STAGED_BOUND_MS`], and [`BOUND_MS`] after the last feed the +/// watchdog resets the machine. A second [`BOUND_MS`] covers a tick whose +/// accuracy no vendor states; past it the deadline ends a machine whose +/// watchdog did not. +pub const WATCHED_WEDGE_BOUND_MS: u64 = STAGED_BOUND_MS + 2 * BOUND_MS; + /// The bound one *CPU* gets to take no interrupt at all while it is burning /// cycles, in milliseconds, before that CPU ends the machine from its own NMI. /// diff --git a/lib/tco/tests/fch.rs b/lib/tco/tests/fch.rs new file mode 100644 index 00000000000..332e70d4a16 --- /dev/null +++ b/lib/tco/tests/fch.rs @@ -0,0 +1,454 @@ +//! `toyos_tco::fch`'s sequences against a model of the FCH built from the +//! register descriptions it cites (55570 §9.2.6, PMx000, PMx0C0; 58264 +//! §12.3.6), so a sequence the hardware would ignore fails here. + +use toyos_tco::fch::{ + self, AcpiMmio, Control, Refused, Register, ResetStat, Unsurveyed, Window, TIMER, + WATCHDOG_ISSUE_RESET, +}; + +/// `PMx000`'s reset value on Family 17h (55570): `WatchdogTmrEn` clear, the +/// 1 s tick. +const DECODE_EN_RESET: u32 = 0x2302_0B10; + +/// One FCH, in microseconds. +struct Model { + decode_en: u32, + /// Firmware that holds `WatchdogTmrEn` clear. + decode_locked: bool, + /// Firmware that clears `WatchdogTmrEn` at every reset, as the AMD + /// bench's was read to. + decode_clears_on_reset: bool, + /// An FCH that ignores a write of `PMx000`'s byte 3. + tick_locked: bool, + alternate: u8, + select: u8, + reset_stat: u32, + run: bool, + fired: bool, + power_off: bool, + /// Family 1Ah's `watchdogactionen`; `None` on 17h, where bit 4 is reserved. + action_en: Option, + reload: u16, + counter: u16, + /// Whether a count interval is running: only `WatchdogTrigger` starts one, + /// and only while the timer runs. + counting: bool, + now_us: u64, + resets: u32, + writes: u32, +} + +impl Model { + fn new() -> Self { + Self { + decode_en: DECODE_EN_RESET, + decode_locked: false, + decode_clears_on_reset: false, + tick_locked: false, + alternate: 0, + select: 0, + reset_stat: 0, + run: false, + fired: false, + power_off: false, + action_en: None, + reload: 0x5555, + counter: 0x5555, + counting: false, + now_us: 0, + resets: 0, + writes: 0, + } + } + + fn enabled(&self) -> bool { + self.decode_en & fch::WATCHDOG_TMR_EN != 0 + } + + fn tick_us(&self) -> u64 { + match (self.decode_en >> 24) & 3 { + 0 => 32, + 1 => 10_000, + 2 => 100_000, + _ => 1_000_000, + } + } + + /// Time passes; the tick is free-running, so the first one after a + /// trigger comes anywhere inside its period. + fn advance_ms(&mut self, ms: u64) { + let end = self.now_us + ms * 1_000; + while self.now_us < end { + let tick = self.tick_us(); + let next = (self.now_us / tick + 1) * tick; + if next > end { + self.now_us = end; + break; + } + self.now_us = next; + if self.enabled() && self.run && self.counting { + self.counter = self.counter.saturating_sub(1); + if self.counter == 0 { + self.expire(); + } + } + } + } + + fn expire(&mut self) { + self.counting = false; + self.fired = true; + if self.action_en == Some(false) { + return; + } + // A reset of type 1: the WDT block is in the S5 domain and keeps its + // registers; the reset's own bit is the one `PMx0C0` keeps. + self.reset_stat = self.reset_stat & !0x0FFF_0000 | WATCHDOG_ISSUE_RESET; + self.reset(); + } + + /// A CF9 reset, which the S5-domain block also survives. + fn cf9(&mut self) { + self.reset_stat = self.reset_stat & !0x0FFF_0000 | 1 << 19; + self.reset(); + } + + fn reset(&mut self) { + self.resets += 1; + if self.decode_clears_on_reset { + self.decode_en &= !fch::WATCHDOG_TMR_EN; + } + } + + fn control(&self) -> u32 { + if !self.enabled() { + return Control::DISABLED; + } + u32::from(self.run) + | u32::from(self.fired) << 1 + | u32::from(self.power_off) << 2 + | u32::from(self.action_en == Some(true)) << 4 + } +} + +impl AcpiMmio for Model { + fn read_u8(&mut self, offset: u16) -> u8 { + match offset { + 0x300..=0x303 => (self.decode_en >> ((offset - 0x300) * 8)) as u8, + 0x3C4 => self.select, + 0x3D5 => self.alternate, + _ => panic!("a byte read at ACPI-MMIO {offset:#x}, which the model does not hold"), + } + } + + fn write_u8(&mut self, offset: u16, value: u8) { + self.writes += 1; + match offset { + 0x300 => { + let value = if self.decode_locked { value & !(fch::WATCHDOG_TMR_EN as u8) } else { value }; + self.decode_en = self.decode_en & !0xff | u32::from(value); + } + 0x303 if self.tick_locked => {} + 0x303 => self.decode_en = self.decode_en & 0x00ff_ffff | u32::from(value) << 24, + _ => panic!("a byte write at ACPI-MMIO {offset:#x}, which nothing here should write"), + } + } + + fn read_u32(&mut self, offset: u16) -> u32 { + match offset { + 0x300 => self.decode_en, + 0x3C0 if self.select & fch::SELECT_DEBUG != 0 => 0xdead_beef, + 0x3C0 => self.reset_stat, + _ => panic!("a dword read at ACPI-MMIO {offset:#x}, which the model does not hold"), + } + } + + fn write_u32(&mut self, offset: u16, value: u32) { + self.writes += 1; + match offset { + 0x3C0 => self.reset_stat &= !value, + _ => panic!("a dword write at ACPI-MMIO {offset:#x}, which nothing here should write"), + } + } +} + +impl Window for Model { + fn read(&mut self, register: Register) -> u32 { + if !self.enabled() { + return u32::MAX; + } + match register { + Register::Control => self.control(), + Register::Count => u32::from(self.counter), + } + } + + fn write(&mut self, register: Register, value: u32) { + self.writes += 1; + if !self.enabled() { + return; + } + match register { + Register::Count => self.reload = value as u16, + Register::Control => { + let was_running = self.run; + self.run = value & Control::RUN != 0; + if value & Control::FIRED != 0 { + self.fired = false; + } + self.power_off = value & Control::POWER_OFF != 0; + if self.action_en.is_some() { + self.action_en = Some(value & Control::ACTION_EN != 0); + } + if !self.run { + self.counting = false; + } else if value & Control::TRIGGER != 0 && was_running { + self.counter = self.reload; + self.counting = true; + } + } + } + } +} + +/// The loader's survey and arm, then the kernel's take-over, as one boot +/// runs them; the kernel's half is `kernel/src/arch/x86_64/watchdog.rs`'s. +fn boot(model: &mut Model) -> fch::Armed { + fch::survey(model).expect("ACPI-MMIO answers"); + let armed = fch::arm(model).expect("armed"); + take_over(model); + armed +} + +fn take_over(model: &mut Model) { + let found = fch::Found::read(model); + assert!(found.armed(), "the kernel finds the loader's arm: {found:?}"); + fch::feed(model); +} + +/// Fed every quarter of the bound, as the kernel's scheduler pass feeds it. +fn fed_for(model: &mut Model, ms: u64) { + let every = toyos_tco::BOUND_MS / 4; + for _ in 0..ms / every { + model.advance_ms(every); + fch::feed(model); + } +} + +#[test] +fn the_tick_holds_the_bound_exactly() { + assert_eq!(fch::TICK_MS, 100); + assert_eq!(fch::TIMER, 96); + assert_eq!(fch::bound_of(TIMER), toyos_tco::BOUND_MS); +} + +/// The whole claim: armed out of firmware's reset state, fed it stays up, and +/// unfed it resets the machine inside one tick of the bound. +#[test] +fn an_unfed_timer_resets_the_machine_at_its_bound() { + let mut model = Model::new(); + let armed = boot(&mut model); + assert_eq!(armed.decode_en & fch::WATCHDOG_TMR_EN, fch::WATCHDOG_TMR_EN); + assert_eq!((armed.decode_en >> 24) & 0xf, fch::TICK as u32); + assert_eq!(armed.decode_en >> 28, DECODE_EN_RESET >> 28, "byte 3's high nibble is not the watchdog's"); + assert_eq!(armed.count, TIMER); + + fed_for(&mut model, 60_000); + assert_eq!(model.resets, 0, "a fed timer reset the machine"); + + model.advance_ms(toyos_tco::BOUND_MS - fch::TICK_MS - 1); + assert_eq!(model.resets, 0, "the reset came a tick early"); + model.advance_ms(fch::TICK_MS + 1); + assert_eq!(model.resets, 1, "no reset at the bound"); + assert!(model.fired); +} + +/// Setting run and trigger in one write leaves the timer running with no +/// count, so it never expires (fact 3.2): the start takes two. +#[test] +fn one_write_of_run_and_trigger_starts_no_count() { + let mut model = Model::new(); + fch::arm(&mut model).expect("armed"); + fch::stop(&mut model); + Window::write(&mut model, Register::Control, Control::RUN | Control::TRIGGER); + model.advance_ms(2 * toyos_tco::BOUND_MS); + assert_eq!(model.resets, 0, "the model starts a count on one write, which the hardware does not"); +} + +#[test] +fn a_stopped_timer_resets_nothing() { + let mut model = Model::new(); + boot(&mut model); + fch::stop(&mut model); + model.advance_ms(10 * toyos_tco::BOUND_MS); + assert_eq!(model.resets, 0); +} + +/// The boot after the reset: both records say so, the loader clears both, +/// and the boot after that reads neither. +#[test] +fn the_boot_after_an_expiry_reads_it_and_the_next_does_not() { + let mut model = Model::new(); + boot(&mut model); + model.advance_ms(toyos_tco::BOUND_MS); + assert_eq!(model.resets, 1); + + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert!(survey.issued_reset() && survey.fired() == Some(true) && survey.watchdog_reset(), "{survey:?}"); + assert!(matches!(survey.reset_stat, ResetStat::Read { after: 0, .. }), "{survey:?}"); + assert_eq!(model.reset_stat, 0, "the loader left the FCH's record set"); + assert!(!model.fired, "the loader left the block's record set"); + + // Unarmed, then a CF9 reset: no watchdog caused that one. + model.cf9(); + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert!(!survey.watchdog_reset(), "a CF9 reset read as the watchdog's: {survey:?}"); +} + +/// Firmware that clears the decode at reset hides `WatchdogFired` from the +/// survey; it outlives warm resets, so `arm` clears it as stale and no later +/// pass reports it. +#[test] +fn a_fired_bit_the_survey_cannot_read_is_cleared_by_the_arm() { + let mut model = Model { decode_clears_on_reset: true, ..Model::new() }; + boot(&mut model); + model.advance_ms(toyos_tco::BOUND_MS); + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert_eq!(survey.fired(), None, "{survey:?}"); + assert!(survey.issued_reset(), "{survey:?}"); + assert!(model.fired); + + let armed = fch::arm(&mut model).expect("armed"); + assert!(armed.cleared_fired && !model.fired, "{armed:?}"); + take_over(&mut model); + fch::stop(&mut model); + model.cf9(); + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert!(!survey.watchdog_reset(), "{survey:?}"); + assert!(!fch::arm(&mut model).expect("armed").cleared_fired); +} + +/// The decode gates the timer: a count cut off by a reset that cleared it +/// stays still, the survey cannot stop it, and the arm restarts it whole. +#[test] +fn a_count_the_reset_left_undecoded_restarts_from_the_bound() { + let mut model = Model { decode_clears_on_reset: true, ..Model::new() }; + boot(&mut model); + model.advance_ms(toyos_tco::BOUND_MS / 2); + model.cf9(); + assert!(model.run && model.counting, "the reset stopped the count, so this tests nothing"); + + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert!(survey.found.is_none() && !survey.stopped, "{survey:?}"); + model.advance_ms(10 * toyos_tco::BOUND_MS); + assert_eq!(model.resets, 1, "an undecoded timer counted"); + + fch::arm(&mut model).expect("armed"); + take_over(&mut model); + model.advance_ms(toyos_tco::BOUND_MS - fch::TICK_MS - 1); + assert_eq!(model.resets, 1, "the arm resumed the old count instead of a whole bound"); + model.advance_ms(fch::TICK_MS + 1); + assert_eq!(model.resets, 2); +} + +/// A refusal once the decode is on leaves the timer stopped, so a count the +/// last boot left does not resume behind it for the kernel to take over. +#[test] +fn a_refusal_after_the_decode_leaves_the_timer_stopped() { + let mut model = Model { decode_clears_on_reset: true, ..Model::new() }; + boot(&mut model); + model.cf9(); + model.tick_locked = true; + model.decode_en &= !0x0f00_0000; + assert!(matches!(fch::arm(&mut model), Err(Refused::TickStuck(_)))); + assert!(!model.run); + assert!(!fch::Found::read(&mut model).armed()); +} + +/// The block survives a CF9 reset, so a boot that reset the machine with the +/// timer armed hands the next one a count still running. +#[test] +fn a_count_the_last_boot_left_running_is_stopped() { + let mut model = Model::new(); + boot(&mut model); + model.cf9(); + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert!(survey.stopped, "{survey:?}"); + assert!(!survey.watchdog_reset(), "{survey:?}"); + model.advance_ms(10 * toyos_tco::BOUND_MS); + assert_eq!(model.resets, 1, "the count the last boot left reset this one"); +} + +/// Family 1Ah's MP1-owned bit survives every write here: a pet that cleared it +/// would leave an expiry that only sets `WatchdogFired`. +#[test] +fn every_write_keeps_the_bits_it_does_not_own() { + let mut model = Model { action_en: Some(true), ..Model::new() }; + boot(&mut model); + fed_for(&mut model, 10_000); + assert_eq!(model.action_en, Some(true)); + model.advance_ms(toyos_tco::BOUND_MS); + assert_eq!(model.resets, 1); + fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert_eq!(model.action_en, Some(true)); +} + +/// Firmware's choices this cannot undo are refused with no timer written. +#[test] +fn what_firmware_holds_is_refused_by_name() { + let mut model = Model { alternate: 1, reset_stat: WATCHDOG_ISSUE_RESET, ..Model::new() }; + assert_eq!(fch::arm(&mut model), Err(Refused::AlternateBase(1))); + assert_eq!(fch::survey(&mut model), Err(Unsurveyed::AlternateBase(1))); + assert_eq!(model.writes, 0, "a refusal wrote a register"); + + let mut model = Model { decode_locked: true, ..Model::new() }; + assert!(matches!(fch::arm(&mut model), Err(Refused::DecodeStuck(_)))); + assert!(!model.run); + + // A machine whose ACPI-MMIO decodes nothing reads all ones. + struct Nothing; + impl AcpiMmio for Nothing { + fn read_u8(&mut self, _: u16) -> u8 { u8::MAX } + fn write_u8(&mut self, _: u16, _: u8) { panic!("written") } + fn read_u32(&mut self, _: u16) -> u32 { u32::MAX } + fn write_u32(&mut self, _: u16, _: u32) { panic!("written") } + } + impl Window for Nothing { + fn read(&mut self, _: Register) -> u32 { u32::MAX } + fn write(&mut self, _: Register, _: u32) { panic!("written") } + } + assert_eq!(fch::arm(&mut Nothing), Err(Refused::AcpiMmioAbsent)); + assert_eq!(fch::survey(&mut Nothing), Err(Unsurveyed::AcpiMmioAbsent)); +} + +/// Decode off: the block reads all ones, so the kernel finds nothing armed and +/// the survey reads only the FCH's record. +#[test] +fn an_undecoded_window_is_neither_armed_nor_read() { + let mut model = Model::new(); + let found = fch::Found::read(&mut model); + assert!(!found.armed() && !found.control.decoded(), "{found:?}"); + model.reset_stat = WATCHDOG_ISSUE_RESET; + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert_eq!(survey.found, None); + assert!(survey.watchdog_reset(), "{survey:?}"); +} + +#[test] +fn a_debug_view_of_the_reset_status_is_not_read_as_one() { + let mut model = Model { select: fch::SELECT_DEBUG, ..Model::new() }; + let survey = fch::survey(&mut model).expect("ACPI-MMIO answers"); + assert_eq!(survey.reset_stat, ResetStat::DebugSelected(fch::SELECT_DEBUG)); + assert!(!survey.watchdog_reset()); + assert_eq!(model.writes, 0); +} + +#[test] +fn the_function_is_found_by_its_revision_too() { + assert!(fch::carries(0x1022, 0x790b, 0x51)); + assert!(fch::carries(0x1022, 0x790b, 0x61)); + assert!(!fch::carries(0x1022, 0x790b, 0x4f), "the SB800 layout, a base in PM 48h"); + assert!(!fch::carries(0x1022, 0x780b, 0x51)); + assert!(!fch::carries(0x8086, 0x790b, 0x51)); +} diff --git a/loader/src/main.rs b/loader/src/main.rs index 66080fa5cbd..d3be919d334 100644 --- a/loader/src/main.rs +++ b/loader/src/main.rs @@ -927,6 +927,18 @@ fn main(handle: Handle, system_table: SystemTable) -> Status { Err(why) => println!("Anti-rollback floor: not raised, because the proven image is not verified: {why}"), } } + // Find ACPI 2.0 RSDP from UEFI configuration table + let rsdp_addr = system_table + .config_table() + .iter() + .find(|entry| entry.guid == ACPI2_GUID) + .map(|entry| entry.address as u64) + .expect("ACPI 2.0 RSDP not found in UEFI config table"); + // On every pass, before either end of the chain: the last reset's cause is + // the report this pass may be making, and a count the last boot left + // running on an FCH would end this pass or the boot it hands to. + let chipset = watchdog::locate(&system_table, rsdp_addr); + watchdog::survey(&chipset); if retry { // **The hang, and the only bound there is on one.** The last boot of // this image was handed the machine and never reported — no panic, no @@ -962,13 +974,6 @@ fn main(handle: Handle, system_table: SystemTable) -> Status { ), } - // Find ACPI 2.0 RSDP from UEFI configuration table - let rsdp_addr = system_table - .config_table() - .iter() - .find(|entry| entry.guid == ACPI2_GUID) - .map(|entry| entry.address as u64) - .expect("ACPI 2.0 RSDP not found in UEFI config table"); println!("RSDP address: {:#x}", rsdp_addr); let boot_part = boot_partition(handle, &system_table); @@ -1049,7 +1054,7 @@ fn main(handle: Handle, system_table: SystemTable) -> Status { // The last act before the jump, so the smallest possible span of this loader // is inside the bound: everything above it can still be reported, and a hang // between here and the kernel's own arm is what the bound is for. - watchdog::arm(&system_table, rsdp_addr, params); + watchdog::arm(&chipset, params); println!("Starting kernel..."); start_kernel(loaded_kernel, kernel_bytes, cmdline, rsdp_addr, gop, boot_part, log_guid, Handed { root: chosen.root, supervisor: chosen.supervisor, fileserver: chosen.fileserver }, chosen.volume, microcode, entry_counter, wall_clock, system_table); diff --git a/loader/src/watchdog.rs b/loader/src/watchdog.rs index 6e07a688b6d..6ec2397e7ad 100644 --- a/loader/src/watchdog.rs +++ b/loader/src/watchdog.rs @@ -1,22 +1,28 @@ -//! The chipset's TCO watchdog, armed here so the handoff is inside the bound. +//! The chipset's watchdog, Intel's TCO or AMD's FCH timer, armed here so the +//! handoff is inside the bound. //! -//! The kernel arms and feeds the same block, on the same [`toyos_tco::PARAM`], -//! at the same [`toyos_tco::TIMER`] — but only once it is up, and this covers -//! the span before that. +//! The kernel arms and feeds the same timer, on the same [`toyos_tco::PARAM`], +//! at the same [`toyos_tco::BOUND_MS`] — but only once it is up, and this covers +//! the span before that. The FCH's tick is selected here and only here: its PM +//! block shares a page with registers AML uses. //! -//! `TCO2_STS` is read and never written: whether the last boot ended in a TCO -//! reset is latched there and the kernel is the one place that clears it, but a -//! boot whose kernel never reaches its own arm still owes the value, and this -//! file is the only channel such a boot has. +//! `TCO2_STS` is read and never cleared here: whether the last boot ended in +//! a TCO reset is latched there and the kernel is the one place that clears +//! it, but a boot whose kernel never reaches its own arm still owes the value, +//! and this file is the only channel such a boot has. The FCH's two records of +//! it, `S5ResetStat` and `WatchdogFired`, are this file's alone: [`survey`] +//! reads and clears them on every pass, armed or not. //! //! Every machine this cannot arm is refused by name on the console and boots //! anyway. -use core::ptr::read_volatile; +use core::ptr::{read_volatile, write_volatile}; +use alloc::string::String; use toyos_acpi::Phys; +use toyos_tco::fch; use toyos_tco::{ - Chipset, TCO1_CNT, TCO1_CNT_LOCK, TCO1_CNT_NO_REBOOT, TCO1_CNT_RUN, TCO1_STS, TCO2_STS, + TCO1_CNT, TCO1_CNT_LOCK, TCO1_CNT_NO_REBOOT, TCO1_CNT_RUN, TCO1_STS, TCO2_STS, TCO1_STS_TIMEOUT, TCO_RLD, TCO_TMR, }; use crate::efi::{SystemTable, PAGE_SIZE}; @@ -45,33 +51,102 @@ impl Phys for Identity { } } -/// Arm the watchdog when `cmdline` names it, and say on the console what was -/// armed or why nothing was. -pub fn arm(system_table: &SystemTable, rsdp_addr: u64, cmdline: &str) { - if !toyos_abi::boot::tokens(cmdline).any(|token| token == toyos_tco::PARAM) { - return; - } - if !crate::arch::pio::EXISTS { - return refused(format_args!("this architecture has no I/O port space, where a TCO block answers")); - } - let allocations = match toyos_acpi::ecam_allocations(Identity, rsdp_addr) { - Ok(allocations) => allocations, - Err(e) => return refused(format_args!("this machine's tables name no ECAM ({e:?})")), - }; +/// The function on bus 0 whose identity names a watchdog this loader drives. +pub enum Chipset { + /// With the two config words its row reads. + Tco { row: &'static toyos_tco::Chipset, base: u32, enable: u32 }, + Fch { revision: u8 }, +} + +/// The machine's chipset watchdog, `Ok(None)` where no function names one, +/// or why bus 0 could not be read. +pub type Located = Result, String>; + +pub fn locate(system_table: &SystemTable, rsdp_addr: u64) -> Located { + let allocations = toyos_acpi::ecam_allocations(Identity, rsdp_addr) + .map_err(|e| alloc::format!("this machine's tables name no ECAM ({e:?})"))?; // Bus 0 is where the chipset is. - let Some(ecam) = allocations.flatten().find(|window| window.holds(0)).map(|window| window.base()) else { - return refused(format_args!("no ECAM window of the MCFG's decodes bus 0")); - }; + let ecam = allocations + .flatten() + .find(|window| window.holds(0)) + .map(|window| window.base()) + .ok_or_else(|| String::from("no ECAM window of the MCFG's decodes bus 0"))?; // The MCFG's word for where configuration space is, checked against // firmware's own map before anything dereferences it. if !described(system_table, ecam, BUS_ZERO_BYTES) { - return refused(format_args!( + return Err(alloc::format!( "firmware's memory map describes no {BUS_ZERO_BYTES:#x} bytes at the MCFG's {ecam:#x}" )); } - let Some((row, base_reg, enable_reg)) = chipset(ecam) else { - return refused(format_args!("no function on bus 0 carries a TCO block this loader knows")); + Ok(chipset(ecam)) +} + +/// On an FCH: say what the last reset was, clear the FCH's record of it, and +/// stop a count this boot did not start. Every pass, armed or not. +pub fn survey(located: &Located) { + let Ok(Some(Chipset::Fch { revision })) = located else { return }; + let survey = match fch::survey(&mut Fch) { + Ok(survey) => survey, + Err(why) => { + return println!( + "watchdog: FCH rev {revision:#04x}: ACPI-MMIO at {:#x} is not surveyed ({why:?}), so \ + the last reset's cause is unread", + fch::ACPI_MMIO + ) + } }; + match survey.reset_stat { + fch::ResetStat::Read { stat, after } => println!( + "watchdog: FCH S5ResetStat={stat:#010x} on this pass's entry, written back to clear it, \ + and reads {after:#010x} after" + ), + fch::ResetStat::DebugSelected(select) => { + println!("watchdog: FCH PMx0C4={select:#04x} selects S5ResetStat's debug view, so it is unread") + } + fch::ResetStat::Absent => println!("watchdog: FCH S5ResetStat reads all ones"), + } + match survey.found { + Some(found) => println!( + "watchdog: FCH WatchdogControl={:#010x} WatchdogCount={} on this pass's entry{}{}", + found.control.0, + found.count, + if survey.stopped { ", running, so this pass stops the count the last boot left" } else { "" }, + if found.control.fired() { ", and WatchdogFired is cleared" } else { "" } + ), + None => println!( + "watchdog: FCH PMx000={:#010x} leaves the watchdog undecoded, so its control is unread", + survey.decode_en + ), + } + println!( + "watchdog: watchdog_issue_reset={} fired={}, so {}", + u8::from(survey.issued_reset()), + match survey.fired() { + Some(fired) => if fired { "1" } else { "0" }, + None => "unread", + }, + if survey.watchdog_reset() { fch::RESET_SAID } else { "no FCH watchdog reset is recorded" } + ); +} + +/// Arm the watchdog when `cmdline` names it, and say on the console what was +/// armed or why nothing was. +pub fn arm(located: &Located, cmdline: &str) { + if !toyos_abi::boot::tokens(cmdline).any(|token| token == toyos_tco::PARAM) { + return; + } + match located { + Err(why) => refused(format_args!("{why}")), + Ok(None) => refused(format_args!("no function on bus 0 carries a chipset watchdog this loader knows")), + Ok(Some(Chipset::Tco { row, base, enable })) => arm_tco(row, *base, *enable), + Ok(Some(Chipset::Fch { revision })) => arm_fch(*revision), + } +} + +fn arm_tco(row: &toyos_tco::Chipset, base_reg: u32, enable_reg: u32) { + if !crate::arch::pio::EXISTS { + return refused(format_args!("this architecture has no I/O port space, where a TCO block answers")); + } let port = match row.port(base_reg, enable_reg) { Ok(port) => port, Err(why) => { @@ -109,6 +184,103 @@ pub fn arm(system_table: &SystemTable, rsdp_addr: u64, cmdline: &str) { report(port, cnt); } +fn arm_fch(revision: u8) { + let armed = match fch::arm(&mut Fch) { + Ok(armed) => armed, + Err(why) => return refused(format_args!("the FCH watchdog is not armed ({why:?})")), + }; + println!( + "watchdog: {:04x}:{:04x} rev {revision:#04x} FCH watchdog at {:#x} WatchdogCount={} at a \ + {} ms tick armed for {}ms, and the kernel takes it over", + fch::VENDOR, + fch::SMBUS, + fch::WDT, + fch::TIMER, + fch::TICK_MS, + fch::bound_of(fch::TIMER) + ); + let mut acpi = Fch; + let reset_ctl1 = fch::AcpiMmio::read_u8(&mut acpi, fch::PM_RESET_CTL1); + let misc = fch::AcpiMmio::read_u32(&mut acpi, fch::PM_MISC); + let gating = fch::AcpiMmio::read_u32(&mut acpi, fch::MISC_CLK_GATING); + let control = armed.control; + println!( + "watchdog: read back PMx000={:#010x} WatchdogControl={:#010x} WatchdogCount={} \ + PMx0BE={reset_ctl1:#04x} PMx0C8={misc:#010x} MISCx0FC={gating:#010x}{}", + armed.decode_en, + control.0, + armed.count, + if armed.cleared_fired { ", with a stale WatchdogFired cleared" } else { "" } + ); + // What decides whether an expiry resets this machine, and how: named + // rather than left for a reader to mask out. A warm reset is CPURST# + // alone; `RstToCpuPwrGdEn` makes every FCH reset a cold one (55570 §9.1.11). + let resets = !control.powers_off() && misc & fch::USE_CPU_RST != 0; + println!( + "watchdog: power_off={} use_cpu_rst={} cold={} clock_gating_dis={} action_en={}, so an \ + expiry {} reset this machine", + u8::from(control.powers_off()), + u8::from(misc & fch::USE_CPU_RST != 0), + u8::from(reset_ctl1 & fch::RST_TO_CPU_PWR_GD_EN != 0), + // All ones is also what an undecoded register reads. + match gating { + u32::MAX => "unread", + gating if gating & fch::WDT_BLK_CLK_GATING_DIS != 0 => "1", + _ => "0", + }, + u8::from(control.0 & fch::Control::ACTION_EN != 0), + if resets { "can" } else { "cannot" }, + ); +} + +/// The FCH's two windows at the addresses AMD fixes for them, which boot +/// services identity-map. +struct Fch; + +impl Fch { + fn acpi(offset: u16, len: u64) -> u64 { + assert!(u64::from(offset) + len <= fch::ACPI_MMIO_BYTES, "ACPI-MMIO offset {offset:#x}"); + fch::ACPI_MMIO + u64::from(offset) + } +} + +// SAFETY, for every access below: the address is inside the window AMD's PPR +// fixes for an FCH whose PCI identity `chipset` matched, and each is one +// volatile access of the register's own width. +impl fch::AcpiMmio for Fch { + fn read_u8(&mut self, offset: u16) -> u8 { + // SAFETY: as above. + unsafe { read_volatile(Self::acpi(offset, 1) as *const u8) } + } + + fn write_u8(&mut self, offset: u16, value: u8) { + // SAFETY: as above. + unsafe { write_volatile(Self::acpi(offset, 1) as *mut u8, value) } + } + + fn read_u32(&mut self, offset: u16) -> u32 { + // SAFETY: as above. + unsafe { read_volatile(Self::acpi(offset, 4) as *const u32) } + } + + fn write_u32(&mut self, offset: u16, value: u32) { + // SAFETY: as above. + unsafe { write_volatile(Self::acpi(offset, 4) as *mut u32, value) } + } +} + +impl fch::Window for Fch { + fn read(&mut self, register: fch::Register) -> u32 { + // SAFETY: as above. + unsafe { read_volatile((fch::WDT + register as u64) as *const u32) } + } + + fn write(&mut self, register: fch::Register, value: u32) { + // SAFETY: as above. + unsafe { write_volatile((fch::WDT + register as u64) as *mut u32, value) } + } +} + /// What the block holds once it is armed, as whole words. fn report(port: u16, cnt: u16) { let rld = crate::arch::pio::inw(port + TCO_RLD); @@ -132,22 +304,28 @@ fn report(port: u16, cnt: u16) { ); } -/// The first function on bus 0 a row names, with the two config words that row -/// reads. Bus 0, because the LPC bridge and the SMBus function both live there -/// and neither is behind a bridge on any machine this tree targets. -fn chipset(ecam: u64) -> Option<(&'static Chipset, u32, u32)> { +/// The first function on bus 0 that names a watchdog. Bus 0, because the +/// LPC bridge and the SMBus function both live there and neither is behind a +/// bridge on any machine this tree targets. +fn chipset(ecam: u64) -> Option { for device in 0..32u8 { for function in 0..8u8 { let id = config_u32(ecam, device, function, 0); - let Some(row) = toyos_tco::chipset(id as u16, (id >> 16) as u16) else { continue }; - let base = config_u32(ecam, device, function, row.base_reg); - // One read where a chipset keeps both in one register, which q35 does. - let enable = if row.enable.reg == row.base_reg { - base - } else { - config_u32(ecam, device, function, row.enable.reg) - }; - return Some((row, base, enable)); + let (vendor, device_id) = (id as u16, (id >> 16) as u16); + if let Some(row) = toyos_tco::chipset(vendor, device_id) { + let base = config_u32(ecam, device, function, row.base_reg); + // One read where a chipset keeps both in one register, which q35 does. + let enable = if row.enable.reg == row.base_reg { + base + } else { + config_u32(ecam, device, function, row.enable.reg) + }; + return Some(Chipset::Tco { row, base, enable }); + } + let revision = config_u32(ecam, device, function, 0x08) as u8; + if fch::carries(vendor, device_id, revision) { + return Some(Chipset::Fch { revision }); + } } } None diff --git a/tests/checks.rs b/tests/checks.rs index 817fcce946d..b513b75f0bf 100644 --- a/tests/checks.rs +++ b/tests/checks.rs @@ -1126,7 +1126,7 @@ mod checks { let wedged = |tail: &str| sealed(census, tail); let kernel = "[2026-09-29 10:33:28 0.000 cpu0 kernel] panic console: armed 1920x1080 \ stride=1920 format=1 at 0x4000000000, write-combining\n"; - let staged = "| [ 1.509 cpu1 kernel] wedge: staged, and only the boot deadline ends this machine: \ + let staged = "| [ 1.509 cpu1 kernel] wedge: staged, and only a bound this machine runs under ends it: \ every CPU stops taking scheduler passes from here\n"; let awake = |cpu: u32| format!("| [ 1.509 cpu{cpu} kernel] wedge: cpu{cpu} arrived with interrupts on\n"); let deaf = "| [ 1.509 cpu1 kernel] wedge: cpu1 arrived with interrupts off, through the syscall \ diff --git a/tests/common/metal.rs b/tests/common/metal.rs index 399962e6cf8..baeac099460 100644 --- a/tests/common/metal.rs +++ b/tests/common/metal.rs @@ -1182,9 +1182,10 @@ pub fn judge_readbacks( ); } let mut findings: Vec = Vec::new(); - // The census crosses only on the page, and a page the pass after the - // reset cleared as another image's carries none. - let owes_a_panel = bootlog::foreign_done(&back.loader).is_err(); + // The census crosses only on the page, and neither a page the pass + // after the reset cleared as another image's nor one no kernel sealed + // carries one. + let owes_a_panel = bootlog::foreign_done(&back.loader).is_err() && !bootlog::sealed_nothing(&back.loader); for (field, value, owed) in [ ("complete_ms", back.boot_ms, true), ("panel_max_us", panel.map(|panel| panel.max_micros), owes_a_panel), diff --git a/tests/common/power.rs b/tests/common/power.rs index 7f1cd586043..4bbdea0c80b 100644 --- a/tests/common/power.rs +++ b/tests/common/power.rs @@ -214,17 +214,14 @@ const ARMED_ON_ARRIVAL: &str = "so the bootloader had already armed the timer"; const UNARMED_ON_ARRIVAL: &str = "so nothing had armed the timer"; /// The tail of the loader's own arm line, which names the shipped bound and so -/// tells it from the kernel's, whatever the port and the PCI ids turn out to be. +/// tells it from the kernel's, whatever the chipset and its PCI ids turn out to be. fn loader_armed() -> String { - format!( - "armed for {}ms, and the kernel takes it over", - toyos_tco::bound_of(toyos_tco::TIMER) - ) + format!("armed for {}ms, and the kernel takes it over", toyos_tco::BOUND_MS) } -/// The register value the loader wrote, as the kernel reports finding it. -fn armed_on_arrival() -> String { - format!("TCO_TMR={} on arrival, {ARMED_ON_ARRIVAL}", toyos_tco::TIMER) +/// The kernel's line once it feeds the timer, whichever chipset it is. +fn kernel_feeds() -> String { + format!("this machine resets if no scheduler pass runs for {}ms", toyos_tco::BOUND_MS) } /// The `0x…` word printed straight after `label`, so a register is compared as @@ -250,18 +247,41 @@ fn decimal_field(line: &str, label: &str) -> Result { rest[..end].parse().map_err(|e| format!("{label:?} in {line:?}: {e}")) } -/// The armed boot's half: the loader wrote the register block and the kernel -/// found the timer already running. -/// -/// **`TCO1_CNT.TCO_LOCK` is not judged.** `toyos_tco`'s own header names it as -/// the one bit a read-back may not be held to — firmware that set it leaves it -/// set through every write this tree makes, and what it gates is `SMI_EN.TCO_EN` -/// rather than the countdown or the reboot. It is reported and passed over. +/// Each decoded bit `fields` names reads its value in `line`. +fn bits_are(line: &str, fields: &[(&str, u64)]) -> Result<(), String> { + for (field, want) in fields { + let seen = decimal_field(line, field)?; + if seen != *want { + return Err(format!("the loader read {field}{seen} back and this machine is {want}\n{line}")); + } + } + Ok(()) +} + +/// The armed boot's half: the loader wrote the chipset's watchdog and the +/// kernel found it running and feeds it. pub fn watchdog_armed( loader: &serial::Serial, kernel: &serial::Serial, ) -> Result<(), String> { let line = loader.must_say(&loader_armed())?.to_string(); + if line.contains(" FCH watchdog at ") { + fch_armed(loader)?; + } else { + tco_armed(loader)?; + } + let arrival = kernel.must_say(ARMED_ON_ARRIVAL)?; + eprintln!(" [power] {}", arrival.trim()); + kernel.must_say(&kernel_feeds())?; + eprintln!(" [power] the loader armed it and the kernel found it running: {}", line.trim()); + Ok(()) +} + +/// **`TCO1_CNT.TCO_LOCK` is not judged.** `toyos_tco`'s own header names it as +/// the one bit a read-back may not be held to — firmware that set it leaves it +/// set through every write this tree makes, and what it gates is `SMI_EN.TCO_EN` +/// rather than the countdown or the reboot. It is reported and passed over. +fn tco_armed(loader: &serial::Serial) -> Result<(), String> { // The words, not the line: a read-back that printed a register the loader // never wrote would satisfy the line and is the failure worth catching. let read_back = loader.must_say("watchdog: read back TCO_RLD=")?.to_string(); @@ -275,25 +295,69 @@ pub fn watchdog_armed( // The reset gate is inside this block on the generations the table names, so // a machine whose armed timer could not reset it is one the bound is a lie on. let gates = loader.must_say("so a second expiry can reset this machine")?.to_string(); - // The words of it, not the line: this is the register block's own account of - // whether an expiry reaches the machine, and a machine that has just armed - // the timer has not expired once. - for (field, want) in [("no_reboot=", 0), ("timeout=", 0)] { - let seen = decimal_field(&gates, field)?; - if seen != want { - return Err(format!( - "the loader read {field}{seen} back and this machine is {want}\n{gates}" - )); - } - } - kernel.must_say(&armed_on_arrival())?; - - eprintln!(" [power] the loader armed it and the kernel found it running: {}", line.trim()); + // This is the register block's own account of whether an expiry reaches + // the machine, and a machine that has just armed the timer has not expired once. + bits_are(&gates, &[("no_reboot=", 0), ("timeout=", 0)])?; eprintln!(" [power] {}", gates.trim()); eprintln!(" [power] tco_lock={}, which no read-back is judged on", decimal_field(&gates, "tco_lock=")?); Ok(()) } +/// The FCH's half: the decode and the tick took, the timer is counting the +/// bound to a reset of the CPUs, and nothing the PPR documents gates that. +/// `cold=`, `clock_gating_dis=` and `action_en=` are reported and not judged: +/// the first is firmware's policy over every reset, the second's effect on the +/// count is undocumented, and the third is reserved before Family 1Ah. +fn fch_armed(loader: &serial::Serial) -> Result<(), String> { + use toyos_tco::fch; + let read_back = loader.must_say("watchdog: read back PMx000=")?.to_string(); + let decode = hex_field(&read_back, "PMx000=")?; + let tick = (decode >> 24) & 0xf; + if decode & u64::from(fch::WATCHDOG_TMR_EN) == 0 || tick != fch::TICK as u64 { + return Err(format!("the loader read PMx000={decode:#010x} back: no decode, or not the {} ms tick\n{read_back}", fch::TICK_MS)); + } + let control = fch::Control(hex_field(&read_back, "WatchdogControl=")? as u32); + if !control.running() || control.powers_off() { + return Err(format!("the loader read {control:?} back, which is not counting to a reset\n{read_back}")); + } + // Read straight after the trigger: the count written, or one tick under it. + let count = decimal_field(&read_back, "WatchdogCount=")?; + let timer = u64::from(fch::TIMER); + if !(timer - 1..=timer).contains(&count) { + return Err(format!("the loader read WatchdogCount={count} back and wrote {timer}\n{read_back}")); + } + let gates = loader.must_say("so an expiry can reset this machine")?.to_string(); + bits_are(&gates, &[("power_off=", 0), ("use_cpu_rst=", 1)])?; + eprintln!(" [power] {}", read_back.trim()); + eprintln!(" [power] {}", gates.trim()); + Ok(()) +} + +/// A wedge under the chipset watchdog, read off the boot and the loader pass +/// after its reset: the kernel took the loader's arm over and fed it to the +/// stop where the wedge is staged; then the machine reset with nothing in the +/// kernel ending it, and the FCH says its watchdog did. +pub fn watchdog_reset_chain( + kernel: &serial::Serial, + log: &serial::Serial, + after: &serial::Serial, +) -> Result<(), String> { + kernel.must_say(ARMED_ON_ARRIVAL)?; + kernel.must_say(&kernel_feeds())?; + bootlog::stopping_line(log.text()).ok_or_else(|| { + format!("the armed boot's log carries no {:?}: it never reached the stop the wedge is staged at", bootlog::STOPPING) + })?; + // An ARMED page is a kernel that neither its deadline nor its lockup + // detector ended; one lost with the reset is a black box the FCH's reset + // does not keep, and fails here. + after.must_say(&armed_and_nothing_else())?; + let said = after.must_say(toyos_tco::fch::RESET_SAID)?.to_string(); + after.must_say(bootlog::CHAIN_ENDS_LINE)?; + says_nothing_of(after, bootlog::LOADER_LAST_LINE)?; + eprintln!(" [power] {}", said.trim()); + Ok(()) +} + /// The control: a boot that did not name the parameter arms nothing, and the /// kernel says nothing about a timer either way. pub fn watchdog_quiet( diff --git a/tests/metal/src/lib.rs b/tests/metal/src/lib.rs index 6f343eb2165..d37429e0c72 100644 --- a/tests/metal/src/lib.rs +++ b/tests/metal/src/lib.rs @@ -826,15 +826,21 @@ pub fn ended_by_its_deadline(armed: &[impl AsRef]) -> bool { } /// The `boot-deadline=` bound an image armed with `armed` carries, whose -/// runner bounds its list at `list_ms`. +/// runner bounds its list at `list_ms`. A wedge staged under the chipset +/// watchdog is the watchdog's to end, so its deadline stands behind it. pub fn bound_for(armed: &[impl AsRef], list_ms: u64) -> u64 { - if stages_a_wedge(armed) { - toyos_tco::STAGED_BOUND_MS - } else { - toyos_tco::wedge_bound_ms(list_ms) + match (stages_a_wedge(armed), watched(armed)) { + (true, true) => toyos_tco::WATCHED_WEDGE_BOUND_MS, + (true, false) => toyos_tco::STAGED_BOUND_MS, + (false, _) => toyos_tco::wedge_bound_ms(list_ms), } } +/// Whether this image is armed with the chipset watchdog. +fn watched(armed: &[impl AsRef]) -> bool { + armed.iter().any(|name| name.as_ref() == toyos_tco::PARAM) +} + /// Whether this image is armed so the pass after its reset clears its record /// as another image's: nothing crosses on the page but the record's state, and /// that pass hands the machine back as a hang. @@ -2003,7 +2009,7 @@ fn boot_verdict(armed: &[String], loader: &str, log: &str) -> Result Result Result { +/// carried the other. Under the chipset watchdog, its reset is the third +/// ending: the pass after it finds a page no kernel sealed, and the chipset +/// says its watchdog reset the machine. The wedge stops every feed, and the +/// deadline stands past the watchdog's bound. +fn wedged_boot(watched: bool, loader: &str, log: &str) -> Result { if bootlog::handed_back(loader).is_ok() { return Err(Refusal::Wedge { why: "it reached the shutdown's own last word, so nothing about it was wedged", }); } - let Some(said) = sealed_by(loader) else { + let Some(said) = sealed_by(loader).or_else(|| watched.then(|| reset_by_the_chipset(loader)).flatten()) else { return Err(Refusal::Wedge { - why: "the pass after the reset reports neither of this kernel's bounds, so what \ - ended this boot was not one of them", + why: "the pass after the reset reports neither of this kernel's bounds, nor a reset \ + by the chipset watchdog it may be armed with, so what ended this boot was not \ + one of them", }); }; let ms = bootlog::boot_millis(log).ok_or(Refusal::Wedge { @@ -2055,6 +2065,17 @@ fn sealed_by(loader: &str) -> Option<&str> { .find(|l| l.contains(bootlog::DEADLINE_EXPIRED) || l.contains(bootlog::LOCKED_UP)) } +/// The line the pass after the reset printed where the chipset says its +/// watchdog caused that reset, above a page no kernel sealed: a panicked +/// kernel the watchdog then reset is a panic. Only an FCH says so. +fn reset_by_the_chipset(loader: &str) -> Option<&str> { + if !bootlog::sealed_nothing(loader) { + return None; + } + let after = bootlog::after_the_reset(loader)?; + after.lines().find(|l| l.contains(toyos_tco::fch::RESET_SAID)) +} + /// The lateness of a deadline that expired, in milliseconds past the bound it /// was armed with, out of the line the pass after the reset printed; `None` on /// a boot whose deadline did not expire. @@ -2598,6 +2619,15 @@ mod tests { assert_eq!(bound_for(&["watchdog"], 135_680), 271_360); } + /// Under the chipset watchdog a staged wedge's deadline is past the + /// watchdog's reset, which lands within one bound of the last feed. + #[test] + fn a_wedge_under_the_watchdog_is_the_watchdogs_to_end() { + assert_eq!(bound_for(&[WEDGE_ARM, toyos_tco::PARAM], 60_000), 29_200); + const { assert!(toyos_tco::WATCHED_WEDGE_BOUND_MS > toyos_tco::STAGED_BOUND_MS + toyos_tco::BOUND_MS) }; + assert_eq!(bound_for(&[WEDGE_ARM], 60_000), toyos_tco::STAGED_BOUND_MS); + } + /// A row for a name the kernel no longer declares is a ruling about /// nothing, and it would keep an image admissible after the arm it cleared /// was deleted. Read off the kernel's own two declarations. @@ -3276,7 +3306,7 @@ mod tests { bootlog::DEADLINE_EXPIRED ); let booted = "[ 1.198 cpu0 kernel] Boot: complete (1198ms)\n"; - assert_eq!(wedged_boot(&sealed, booted), Ok(1198)); + assert_eq!(wedged_boot(false, &sealed, booted), Ok(1198)); let done = format!( "Black box: {}\n| {}[ 1.300 cpu0 kernel] {}\n", @@ -3284,16 +3314,45 @@ mod tests { bootlog::LOG_TAIL, bootlog::REBOOTING ); - let why = wedged_boot(&done, booted).unwrap_err().to_string(); + let why = wedged_boot(false, &done, booted).unwrap_err().to_string(); assert!(why.contains("nothing about it was wedged"), "{why}"); - let why = wedged_boot("no record here\n", booted).unwrap_err().to_string(); + let why = wedged_boot(false, "no record here\n", booted).unwrap_err().to_string(); assert!(why.contains("reports neither of this kernel's bounds"), "{why}"); - let why = wedged_boot(&sealed, "nothing at all\n").unwrap_err().to_string(); + let why = wedged_boot(false, &sealed, "nothing at all\n").unwrap_err().to_string(); assert!(why.contains("no `Boot: complete`"), "{why}"); } + /// A wedge under the chipset watchdog ends in the chipset's reset, which + /// only the pass after it reports, and only an image armed with the + /// watchdog may end that way. + #[test] + fn a_watched_wedge_is_ended_by_the_chipsets_reset() { + let booted = "[ 1.300 cpu0 kernel] Boot: complete (1300ms)\n"; + let said = format!("watchdog: watchdog_issue_reset=1 fired=0, so {}\n", toyos_tco::fch::RESET_SAID); + let after = format!( + "{}\n{said}Previous boot's panic: the page still reads ARMED, so that kernel died \ + without reaching either its panic path or its shutdown\n", + bootlog::SEPARATOR + ); + assert_eq!(wedged_boot(true, &after, booted), Ok(1300)); + let why = wedged_boot(false, &after, booted).unwrap_err().to_string(); + assert!(why.contains("reports neither of this kernel's bounds"), "{why}"); + // The first pass's survey speaks of the boot before this one. + let before = format!("{said}{}\nPrevious boot's panic: the page still reads ARMED\n", bootlog::SEPARATOR); + let why = wedged_boot(true, &before, booted).unwrap_err().to_string(); + assert!(why.contains("reports neither of this kernel's bounds"), "{why}"); + // A kernel that panicked and then went unfed: the chipset's reset + // ends it, and the page says it was a panic. + let panicked = format!( + "{}\n{said}Previous boot's panic: 812 bytes off 0x8000000\n", + bootlog::SEPARATOR + ); + let why = wedged_boot(true, &panicked, booted).unwrap_err().to_string(); + assert!(why.contains("reports neither of this kernel's bounds"), "{why}"); + } + /// **The record's own first line is what says which bound ended the boot.** /// Both of them seal `WEDGED` and either can reach a staged machine first, /// so a judge that knew only one read the T14's `hard-lockup-probe` boot — @@ -3307,7 +3366,7 @@ mod tests { | pc=0xffff800060a5903f >::lock+0x12f\n", bootlog::LOCKED_UP ); - assert_eq!(wedged_boot(&locked, booted), Ok(1200)); + assert_eq!(wedged_boot(false, &locked, booted), Ok(1200)); assert_eq!(lockup_lateness_ms(&locked), Some(Ok(4))); // Each bound's lateness is read off its own record and off no other. assert_eq!(deadline_lateness_ms(&locked, booted), None); diff --git a/tests/toyos.rs b/tests/toyos.rs index 09fb62989e8..f598aaecd20 100644 --- a/tests/toyos.rs +++ b/tests/toyos.rs @@ -1185,6 +1185,21 @@ const METAL: &[(&str, metal::Metal)] = &[ judge: |b| power::deadline_wedge_chain(&b[0].after_the_reset()?), }, ), + ( + // The same wedge under the chipset watchdog, with the deadline past + // its bound: the watchdog's reset and not the kernel's ends the boot. + // The AMD bench, whose FCH says which reset it was. Named only: no + // TCO has reset the T14 (`issues/an-armed-tco-has-never-reset-the-t14.md`), + // where this boot would wait out its deadline instead. + "watchdog_resets", + metal::Metal { + arms: &[metal::Arm { + named_only: true, + ..metal::once("watchdogwedge", "tests/images/jobcase.toml", &["watchdog", "wedge-before-reset"], &[]) + }], + judge: |b| power::watchdog_reset_chain(&b[0].kernel(), &b[0].log(), &b[0].after_the_reset()?), + }, + ), ( // Its own boot, and the one arm in this profile the machine itself is // the instrument for: QEMU's TCG guest has no performance counter, so