Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion build/bootlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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";
Expand Down
5 changes: 3 additions & 2 deletions issues/a-frozen-toyos-waits-for-a-hand-on-the-power-button.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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
Expand Down
22 changes: 0 additions & 22 deletions issues/no-watchdog-arms-on-an-amd-machine.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 13 additions & 3 deletions issues/the-loader-does-only-what-must-precede-the-handover.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/arch/x86_64/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
125 changes: 99 additions & 26 deletions kernel/src/arch/x86_64/watchdog.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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<Option<FchWindow>> = 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);
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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`.
Expand All @@ -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");
}
2 changes: 1 addition & 1 deletion kernel/src/deadline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
5 changes: 5 additions & 0 deletions kernel/src/drivers/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 4 additions & 5 deletions lib/tco/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Loading