Definition
An exception handler is the routine a processor runs when an exception occurs, an event that makes it suspend the running code. A fault handler is the exception handler for a fault: an exception the processor raises itself because an instruction cannot complete correctly, such as a forbidden memory access. The processor stops the faulting instruction and jumps to the handler. The fault handler decides the outcome: resume, terminate the faulting thread (one of several tasks sharing the processor), or halt or reset the whole device.
The idea
A thread follows a corrupted pointer and tries to store 4 bytes at an address where the memory protection unit §6.8 (MPU), the hardware that limits which memory each piece of code may use, forbids writing. The processor cannot carry out the store, and it cannot simply skip it, because the following instructions assume that the store happened. The hardware takes the first step, detection: it blocks the store before memory changes and transfers control to the fault handler. Everything after that is a decision the handler makes; the hardware makes none of it.
A fire alarm is a fair picture. The alarm reliably reports that something is wrong, and in which room. It does not decide whether to clear one room or the whole building; that depends on what was in the room and what is connected to it. A fault handler faces the same question about the faulting code: whether it held a lock that other threads wait for, or left a shared data structure half updated. Terminating the thread undoes neither. The picture stops being accurate in one respect: the room in which the alarm sounds is not always where the fire started. The faulting instruction often only used a bad pointer that another part of the program produced much earlier.
How it works
A processor that implements the Armv7-M architecture, such as the Cortex-M3 or the Cortex-M4 §6.3, has four fault exceptions:
MemManage (memory management fault): an access breaks a rule of the MPU, or the processor fetches an instruction from memory marked execute-never.
BusFault: the memory system answers an instruction fetch, a load or a store with an error, for example because nothing exists at the address.
UsageFault: the instruction itself is invalid. Examples are an undefined instruction, a jump to an address whose bit 0 is 0 (Cortex-M processors run only Thumb code, Arm’s compact instruction encoding §2.7, and bit 0 set marks an address as Thumb code), an invalid return from an exception, and, when software has enabled trapping for them, an unaligned access or a division by zero.
HardFault: the catch-all. It is always enabled and has the fixed priority , more urgent than every priority software can set. It takes every fault that the fault’s own handler does not take, and bus errors while the processor reads the vector table §6.14, the table of handler addresses.
MemManage, BusFault and UsageFault are disabled after reset. Software enables each of them with a bit in the SHCSR (System Handler Control and State Register).
From the faulting instruction to the outcome:
Detection. The instruction attempts the operation, and the processor stops it. Most faults are precise: the processor stops the faulting instruction itself, so the target memory has not changed, and the processor knows the instruction’s address. Some bus errors on stores are imprecise, as the worked example shows.
Escalation. For a precise fault, the fault’s own handler runs only if software has enabled it and its priority is more urgent than the priority of the code running now. Otherwise the fault escalates: the processor takes HardFault instead and sets the
FORCEDbit in the HFSR (HardFault Status Register) to record the escalation. A precise fault inside the HardFault handler, or inside the handler of the NMI (non-maskable interrupt §6.11), cannot escalate further: the processor enters lockup and stops executing. Some chips, such as Nordic Semiconductor’s nRF52840 §6.4 microcontroller §6.1, reset themselves when the processor locks up. An imprecise bus error follows another rule. It never causes lockup, and it does not escalate when BusFault’s priority is not more urgent than that of the running code. Instead the processor marks BusFault as pending, and the BusFault handler runs as soon as its priority is more urgent than that of the code then running, which may be at once or only later, for example after other handlers have returned. If software has not enabled BusFault, HardFault runs in its place.Saving state. The processor pushes eight registers onto the stack, among them the return address, which for a precise fault is the address of the faulting instruction, and jumps to the handler through the vector table.
Diagnosis. The handler reads the fault status registers. The CFSR (Configurable Fault Status Register), at address
0xE000ED28, holds MemManage causes in bits 0 to 7, BusFault causes in bits 8 to 15 and UsageFault causes in bits 16 to 31. The MMFAR (MemManage Fault Address Register) and the BFAR (BusFault Address Register) hold the address of a faulting data access when a valid bit in the CFSR says so.Decision. The handler, usually part of the operating system, chooses the outcome.
The outcome is one of four.
Resume: the handler removes the cause and returns, and the faulting instruction runs again. This is correct only if the cause is truly gone; otherwise the same instruction faults again at once, forever.
Terminate the faulting thread: the operating system removes that thread and lets the others run.
Halt: the device stops with interrupts disabled, often in an endless loop, and waits for a debugger or a person.
Reset: the device restarts, usually after saving the cause in memory that survives the reset.
The Zephyr RTOS §6.33 (real-time operating system) shows a typical policy. It turns a processor fault into a fatal error with a reason code, which says for example whether a processor fault or a detected stack overflow §4.10 caused it, logs the saved registers, and calls the function k_sys_fatal_error_handler(). The default version of that function halts the whole system. An application may supply its own version. If that version returns, the kernel terminates the faulting thread and keeps the rest running. A product typically supplies a version that records the cause and resets the device instead.
Worked example
A thread executes a store instruction at address 0x00012A4C that writes to address 0x20008000, which lies in a region that the MPU marks read-only for this thread. MemManage is enabled. The processor blocks the store and takes MemManage, and the handler reads:
CFSR
0x00000082. In binary,0x82is1000 0010, so bit 1 and bit 7 are set. Bit 1 isDACCVIOL, data access violation: a load or store broke an MPU rule. Bit 7 isMMARVALID, MemManage fault address valid: the MMFAR holds the address. All other bits are 0, so there is no bus or usage cause.MMFAR
0x20008000: the address that the store tried to write.The saved return address on the stack is
0x00012A4C: the store instruction itself, because MemManage faults are precise.HFSR
0x00000000: no escalation happened.
Had MemManage been disabled, the processor would have taken HardFault instead, the HFSR would read 0x40000000, which is bit 30, FORCED, alone, and the CFSR would still read 0x00000082.
The registers say what happened. They do not say what to do. Say that the thread held a mutex §9.5, a lock that lets only one thread at a time use a shared buffer of 12 sensor values, and had written 6 of the 12 values when it faulted.
Resume fails: on return, the store at
0x00012A4Cruns again and faults again.Terminate the thread keeps the device running, but the mutex is never released, because terminating a thread does not by itself release its locks. The next thread that asks for the mutex without a time limit waits forever, and the buffer holds 6 new values and 6 old ones. The fault was contained to one thread in name only.
Reset interrupts the service for the length of a reboot and loses the half-written buffer, which lives in SRAM §6.7 (static random-access memory, the chip’s working memory). The device comes back with consistent memory, provided that data kept in flash memory §6.6 across resets was written so that an interruption at any moment leaves it valid, the property called crash consistency §8.4.
A BusFault on a store can be imprecise. A CFSR value of 0x00000400 has only bit 10 set, IMPRECISERR, imprecise data bus error: the processor had already moved on when the error came back from the memory system, so the saved return address points at a later instruction, and the BFAR holds no valid address. The handler then knows the class of fault but not the instruction that caused it.
Stated precisely
Write for the priority of a fault’s own handler and for the execution priority, the priority of the code running when the fault occurs; a smaller number is more urgent. For MemManage, UsageFault and every BusFault except an imprecise one, on Armv7-M:
if , which is the case while the HardFault handler (priority ) or the NMI handler (priority ) runs, the processor enters lockup, unless the fault is a precise bus error on a data access and software has set the bit
BFHFNMIGNin the Configuration and Control Register, which makes the processor ignore such errors at these priorities;otherwise, if the fault’s enable bit in the SHCSR is 1 and , the processor takes the fault’s own handler;
otherwise it takes HardFault and sets
FORCEDin the HFSR.
An imprecise BusFault follows a different rule: it never causes lockup, and its priority never makes it escalate. The processor marks BusFault as pending. If BusFault’s enable bit in the SHCSR is 1, the BusFault handler runs as soon as is smaller than the execution priority of the code then running, which is at once if . If the enable bit is 0, the processor treats the error as a HardFault, which runs as soon as its priority is more urgent than the execution priority of the code then running.
The outcome has no formula; it is a contract stated per fault class and per context:
Recoverable means that after the handler returns, the cause is gone, and every condition that the interrupted code and every other part of the system rely on still holds.
Fatal to one thread means that after the faulting thread is terminated, every other thread can still make progress and every shared data structure is valid. This holds only if all four of the following hold: was a thread, not an interrupt handler §6.12 or the kernel itself; cannot have changed memory outside its own before the fault, which needs the MPU to confine ’s writes; held no lock or other resource that another thread waits for, or the system recovers it; and no other thread needs in order to make progress.
Fatal to the device is every other case: the device must halt or reset.
Limits and common mistakes
A fault catches only what the hardware checks. A write that goes past the end of an array §14.5 but lands in other valid, writable memory raises no fault at all; it silently corrupts data. Freedom from faults is not memory safety.
A software check is a different mechanism. An interpreter §2.13, a program that executes another program’s instructions one at a time, can compare each address with the bounds of the memory it allows before the access and return an error without any hardware fault; the Runtime bounds check §2.27 and Fault-to-verdict mapping §2.34 entries treat this. The interpreter’s own code then decides the outcome, not a fault handler.
On a Cortex-M chip whose vector table sits at address 0 in flash memory, a read through a null pointer §4.26 returns the initial stack pointer instead of faulting, unless the MPU forbids access to the lowest addresses.
A stack overflow can defeat the diagnosis. The processor saves the eight registers on the stack that was in use when the fault occurred; if that stack has run out, the saving itself fails, and the CFSR reports a stacking error. On Cortex-M the handler itself always runs on the main stack. An RTOS that gives each thread its own stack, as Zephyr does, therefore leaves the handler a working stack when a thread’s stack overflows, although the saved registers may be missing; an overflow of the main stack leaves no such reserve.
Diagnostics may be wrong. The handler reads registers and memory of a system that has just misbehaved, so the saved state, the logs and even the handler’s own data may be corrupted. A robust handler relies on as little state as possible.
Reset does not guarantee availability §1.10. A fault that recurs at every boot turns reset into a reset loop, and the device is never available. A boot counter kept in memory that survives the reset, with a fallback such as a previous firmware image, breaks the loop.
Halting and resetting serve different goals. Halting keeps the faulted state for a debugger and makes the fault impossible to overlook, which suits a test bench. Resetting restores service, which suits a product, and needs the cause recorded first, because the reset destroys it. A watchdog timer §6.16, a hardware timer that resets the device unless software restarts it in time, covers the case in which the handler itself hangs.
A log line that says “fault” establishes only that a fault exception ran. Whether the outcome was recovery, loss of one thread or loss of the device has to be shown separately.
Questions answered
Is the fault recoverable, fatal to one thread, or fatal to the whole device?
The fault itself does not decide; the answer depends on where the fault occurred, what state the faulting code held, and the policy of the fault handler, the routine that runs after the processor detects the fault. The processor’s part ends with detection: it blocks the faulting instruction, such as a forbidden memory access, and transfers control. Three questions, asked in order, settle the classification:
Where did the fault occur? A fault in an interrupt handler or inside the kernel leaves no thread that could be removed on its own, so it is fatal to the whole device, which must halt or reset.
Can the faulting thread be removed cleanly? The fault is fatal to one thread only if that thread cannot have written memory outside its own, because the memory protection unit confined it; holds no lock or other resource that another thread waits for; and is not needed by any other thread to make progress. A thread that dies holding a lock leaves a stuck service even though the device keeps running.
Can the cause be removed? The fault is recoverable only if the handler can remove the cause and every condition the rest of the system relies on still holds. That is rare for a hardware fault: returning re-runs the faulting instruction, which faults again.
Then check the configured policy, because it can make every fault fatal to the device. The Zephyr RTOS (real-time operating system), for example, halts the whole system on any fatal error by default, and terminates only the faulting thread when the application supplies a fatal-error handler that returns. The errors that are truly recoverable are usually those that a software check catches before the access happens; such a check returns an error code and raises no fault at all.
Related entries: Fault-to-verdict mapping (§2.34), Watchdog timer (§6.16), Fault model (§1.30), Vector table (§6.14), Memory protection unit (MPU) (§6.8), Mutex (§9.5), Runtime bounds check (§2.27). Further reading: [81].