Section 6.16

Watchdog timer

Definition

A watchdog timer is a hardware timer that resets the device unless software restarts it before it runs out. Restarting it is called feeding the watchdog; kicking, petting, refreshing and servicing mean the same. Software that runs as intended feeds the watchdog in time. Software that hangs stops feeding it, and the expiry forces a reset, which turns a permanent hang into a restart. A watchdog detects one thing: that no valid feed arrived within the timeout §9.34. What a feed proves depends on which code feeds and under what condition.

The idea

A sensor on a remote mast runs for years with nobody nearby. One day a rare timing bug leaves two threads each waiting for a lock that the other holds, a deadlock §9.14. Nothing crashes; the device simply stops doing its work, and it stays that way until someone travels to the mast and switches the power off and on. A watchdog timer replaces that trip: once the software stops feeding it, it resets the device within a known time.

A train’s dead man’s switch is a fair picture. The driver must press a pedal every so often, and if the presses stop, the train brakes. The switch proves only that the pedal is being pressed, not that the driver is watching the track. A watchdog has the same weakness: a feed proves only that the feeding code ran. If a timer interrupt §6.11 feeds the watchdog every 500 ms, the feeds continue while every application thread is deadlocked, and the watchdog never fires. The core design question is therefore where to place the feed: at a point that the program reaches only if the work that matters has made progress. The picture stops being accurate in one respect: a collapsed driver cannot keep pressing the pedal, but a stuck program can keep feeding, for example when the feed sits inside the very loop in which the program is stuck.

How it works

A watchdog has these parts:

Nordic Semiconductor’s nRF52 series of microcontrollers §6.1 shows these parts concretely. The watchdog counts the 32.768 kHz (kilohertz, thousands of cycles per second) low-frequency clock, and its timeout in seconds is (CRV+1)/32768(\mathrm{CRV} + 1) / 32768, where CRV (counter reload value) is a number software writes into a configuration register before starting it. A feed is a write of the value 0x6E524635 to a reload request register. There are up to eight reload request registers, and the watchdog restarts its countdown only when every enabled one has been written, so up to eight separate parts of the software can each be made responsible for one. Once started, the watchdog cannot be stopped or reconfigured, and a soft reset, a reset that software requests, does not stop it. Its configuration also chooses whether it keeps counting while the processor sleeps and while a debugger halts the processor.

A supervisor makes the feed depend on real progress:

  1. Each monitored thread checks in at the point where it has completed a unit of useful work, for example by setting its own bit in a shared word.

  2. A supervisor thread wakes at a fixed period. If every monitored thread has checked in since the previous feed, it feeds the watchdog and clears all the check-ins. If any thread has not, it does not feed.

  3. If a thread hangs, its check-in never appears again. Its last check-in, if no feed has used it yet, can allow one more feed. After that the supervisor stops feeding, and the watchdog resets the device no later than two timeouts after the hang.

  4. After the reset, start-up code reads the chip’s reset-reason register, sees that the watchdog caused the reset, and records that fact.

Clearing the check-ins at each feed makes each check-in count toward one feed only: a thread that checked in once and then hung cannot be counted a second time. Clearing at each feed does not keep check-ins fresh, though: a check-in stays set until a feed uses it, no matter how many supervisor periods pass first. The Zephyr RTOS §6.33 (real-time operating system) offers a similar pattern, its task watchdog, which gives each thread its own channel with its own timeout.

Worked example

Set a watchdog of the nRF52 kind to a timeout of 2 s. The timeout is (CRV+1)/32768(\mathrm{CRV} + 1) / 32768 seconds, so CRV+1=2×32768=65,536\mathrm{CRV} + 1 = 2 \times 32768 = 65{,}536, and CRV=65,535\mathrm{CRV} = 65{,}535.

Three threads A, B and C check in by setting the bits 0x1, 0x2 and 0x4 of a shared word, so the word reads 0x7 when all three have checked in. A and C check in every 0.1 s. The supervisor runs every 0.5 s. Thread B checks in at 0.2 s and deadlocks at 0.3 s.

The hang began at 0.3 s, so the reset came 2.5−0.3=2.22.5 - 0.3 = 2.2 s after it, within the bound that the supervisor steps give, two timeouts or 2×2=42 \times 2 = 4 s. The reset can come later when another thread is slow. Suppose the watchdog was started at 0 s and C checks in for the first time at 1.4 s. At 0.5 s and 1.0 s the word is 0x3, which is 0x1 plus 0x2: A and B checked in, C did not. The supervisor does not feed. At 1.5 s the word is 0x7, because no feed has used B’s check-in from 0.2 s yet, so the supervisor feeds and clears the word. B never checks in again, so no later feed comes, and the watchdog expires at 1.5+2=3.51.5 + 2 = 3.5 s. That is 3.5−0.3=3.23.5 - 0.3 = 3.2 s after the hang: more than one supervisor period plus one timeout, 0.5+2=2.50.5 + 2 = 2.5 s, and less than two timeouts, 4 s.

Now move the feed into a timer interrupt that fires every 0.5 s and feeds unconditionally. Feeds arrive at 0.5 s, 1.0 s, 1.5 s and so on; no gap between feeds ever reaches 2 s, so the watchdog never expires, and the device stays deadlocked for good. The watchdog hardware is the same in both designs; only the condition attached to the feed differs.

Stated precisely

Let WW be the timeout, measured on the watchdog’s own clock, and let tlastt_{\mathrm{last}} be the time of the most recent accepted feed. The watchdog expires at tlast+Wt_{\mathrm{last}} + W unless another feed is accepted before then; equivalently, it resets the device exactly when some gap between consecutive accepted feeds reaches WW. A windowed watchdog with minimum gap WminW_{\mathrm{min}} accepts a feed at time tt only if t−tlast≥Wmint - t_{\mathrm{last}} \ge W_{\mathrm{min}}, and resets the device on a feed with t−tlast<Wmint - t_{\mathrm{last}} < W_{\mathrm{min}}.

With the supervisor of the steps above, let hh be the time at which a monitored thread hangs, and let t0≤ht_0 \le h be the time of the last accepted feed at or before hh, or of the watchdog’s start if no feed has been accepted since. The thread’s last check-in comes at or before hh. Every feed needs a check-in from the thread made since the previous feed, and the first feed after the last check-in clears it, so at most one feed comes after hh. That feed must be accepted before the watchdog expires at t0+Wt_0 + W. The device therefore resets no later than t0+2W≤h+2Wt_0 + 2W \le h + 2W, plus the chip’s own reset delay. In the example, h=0.3h = 0.3 s and W=2W = 2 s give the bound 0.3+2×2=4.30.3 + 2 \times 2 = 4.3 s; the reset came at 2.5 s, and at 3.5 s when C was slow. Let PP be the supervisor period. The bound does not shrink to h+P+Wh + P + W, because a check-in made before the hang stays set until a feed uses it, however many wake-ups pass first. A supervisor that clears every check-in at every wake-up, whether it feeds or not, counts a check-in only at the first wake-up after it, at most PP later. With that supervisor the device resets no later than h+P+Wh + P + W, but each feed then needs a check-in from every monitored thread within one supervisor period.

Both bounds rest on four assumptions.

  1. The watchdog’s clock keeps running, including while the device sleeps.

  2. Nothing can stop or reconfigure the watchdog once it runs.

  3. Only the supervisor can feed, and only after checking every monitored thread.

  4. Every check-in is cleared at each feed, so a check-in counts toward one feed only.

Under these four assumptions the watchdog enforces a bounded-response property: if some monitored thread makes no check-in during a stretch of 2W2W, the watchdog expires by the end of that stretch. Because the property has a time limit, every violation shows after a finite time, so it is a safety property §1.11, not a liveness property §1.12, which would promise only that something good happens eventually. Each feed shows only that every monitored thread has checked in at least once since the previous feed; a thread that misses a supervisor period merely delays the next feed. A reset does not show that useful work resumes after it. The watchdog guarantees nothing that the check-ins do not express. A thread that checks in while computing wrong answers is, to the watchdog, healthy.

Limits and common mistakes

A stuck task keeps the watchdog fed whenever the feed does not depend on that task: a feed from a timer interrupt, a feed from a thread that does not check on the others, or a feed inside the loop in which the task is stuck.

A compromised task, one running code that an attacker chose, keeps the watchdog fed whenever it can write the feed register or forge check-ins. On a microcontroller without memory protection, every piece of code can write the feed register. The MPU §6.8 (memory protection unit) can restrict the watchdog’s registers to privileged code, but a compromised privileged component can still feed. A watchdog recovers from faults; it is not a defense against an attacker.

The timeout must exceed the longest legitimate gap between feeds, including slow operations such as erasing flash memory §6.6 or a long cryptographic computation. Too short a timeout resets a healthy device in the middle of valid work; too long a timeout leaves a hung device out of service for longer.

A watchdog reset says that feeding stopped, not why. Saving the cause needs code that runs before the reset, such as a handler for the interrupt that some watchdogs raise just before resetting, or a record of which check-in was missing.

A reset can recur. If the cause of the hang returns at every boot, the watchdog produces a reset loop, which harms availability §1.10 as much as the hang. A boot counter in memory that survives the reset, with a fallback such as a previous firmware image, breaks the loop. Every watchdog reset also carries the risks of any reset: data in flight is lost, and persistent data must have been written so that an interruption leaves it valid.

A running watchdog can outlive a reset. On the nRF52 series a soft reset does not stop it, so the code that runs after the soft reset, a bootloader §6.30 included, must feed it in time or be reset again.

If the watchdog keeps counting while a debugger holds the processor at a breakpoint §14.29, the device resets under the debugger. Where the chip allows it, configure the watchdog to pause while the processor is halted.

Questions answered

Can a compromised or stuck task continue feeding the watchdog?

Yes, in both cases, unless the system is designed to prevent it. A watchdog timer is a hardware timer that resets the device unless software restarts it, or feeds it, within a timeout. It can check only that feeds keep arriving, not what the feeding code has achieved.

A stuck task keeps the watchdog fed whenever the feed does not depend on its progress: the feed sits in a timer interrupt, in another thread that never checks on it, or inside the very loop in which it is stuck. The fix is a supervisor that feeds only after every monitored task has checked in since the previous feed, with the check-ins cleared at each feed so that an old one cannot count twice. A windowed watchdog, which also resets the device on a feed that comes too early, additionally catches a loop that feeds too often.

A compromised task, one running an attacker’s code, keeps the watchdog fed whenever it can write the feed register or forge its own check-ins. Memory protection that lets only the supervisor reach the watchdog’s registers narrows this, but a compromised privileged component can always feed. The watchdog is a recovery mechanism for hangs and crashes, not a defense against a deliberate attacker.

Related entries: Brownout and power-on reset (§6.17), Deadline versus timeout (§9.34), Trusted computing base (TCB) (§1.4), Watchdog coverage and diagnostic independence (§19.4.8), Liveness property (§1.12), Exception and fault handler (§6.15).