***** HARD FAULT ***** and the other Cortex-M fault lines
A Cortex-M core hit an instruction it could not complete, and the firmware’s fault handler printed which kind of fault it was. The indented lines under the banner say why.
The line
[00:00:00.757,000] <err> os: ***** BUS FAULT *****
[00:00:00.757,000] <err> os: Precise data bus error
[00:00:00.757,000] <err> os: BFAR Address: 0x50080000
[00:00:00.757,000] <err> os: r0/a1: 0x50080000 r1/a2: 0x00000000 r2/a3: 0x00000001
[00:00:00.757,000] <err> os: r3/a4: 0x00000000 r12/ip: 0x00000000 r14/lr: 0x000004b7
[00:00:00.757,000] <err> os: xpsr: 0x61000000
[00:00:00.757,000] <err> os: Faulting instruction address (r15/pc): 0x000004a2
[00:00:00.757,000] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0Zephyr on a Cortex-M4; the addresses are illustrative. Firmware that uses CmBacktrace prints the same fault as Bus fault is caused by precise data access violation.
What it means
The banner is printed by the firmware, not by the CPU. Zephyr prints ***** HARD FAULT *****, ***** BUS FAULT *****, ***** USAGE FAULT *****, ***** MPU FAULT ***** or ***** SECURE FAULT *****; CmBacktrace prints sentences that start with Hard fault is caused by, Bus fault is caused by, Usage fault is caused by or Memory management fault is caused by.
The kind of fault is the exception the core took. The indented lines are the set bits of the fault status registers, decoded: Precise data bus error, Stacking error, Division by zero and so on. When a BFAR Address or MMFAR Address line is present, it is the address the faulting instruction tried to use. The register lines after it are the values the core pushed on the stack when the fault was taken.
Common causes
A pointer to an address with nothing behind it
A read or write through a wild, freed or uninitialised pointer lands where there is no memory or peripheral. Zephyr reports Precise data bus error with the address in BFAR Address (CmBacktrace: Bus fault is caused by precise data access violation), or, when an MPU region forbids the access, ***** MPU FAULT ***** with Data Access Violation and MMFAR Address. Zephyr’s NULL pointer detection for Cortex-M is a build option that is off by default, so a read through a NULL pointer does not have to fault.
Sources: Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0), the code that prints these lines · CmBacktrace: cmb_en_US.h, the message strings it prints · Zephyr: arch/arm/core/cortex_m/Kconfig, NULL_POINTER_EXCEPTION_DETECTION
A stack overflow
A thread or the interrupt stack ran past its end. With a stack guard the report is ***** MPU FAULT ***** with Stacking error (context area might be not valid), or ***** USAGE FAULT ***** with Stack overflow (context area not valid) on cores that have stack limit registers. Without a guard the overflow quietly overwrites whatever lies below the stack, and the fault that follows later can be any other one on this page.
Sources: Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0), the code that prints these lines · Zephyr docs: Fatal Errors, Stack Overflows
A jump to something that is not code
A corrupted function pointer, or a return address overwritten on the stack, sends the core into data or to an even address. The lines are Instruction bus error, Attempt to execute undefined instruction, or Illegal use of the EPSR, which is what a branch to an address without the Thumb bit produces, a NULL function pointer included.
Sources: Memfault Interrupt: How to debug a HardFault on an ARM Cortex-M MCU · Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0), the code that prints these lines
An imprecise bus fault
Imprecise data bus error means the bad write went through the write buffer and the fault was raised some instructions later. The stacked program counter is then near the culprit, not on it, and no fault address is recorded.
Sources: Memfault Interrupt: How to debug a HardFault on an ARM Cortex-M MCU · Arm: Cortex-M4 Devices Generic User Guide (fault handling, CCR, ACTLR)
A divide by zero or an unaligned access, with trapping on
Division by zero and Unaligned memory access appear only when the firmware has set the DIV_0_TRP or UNALIGN_TRP bit of the Configuration and Control Register. Zephyr sets DIV_0_TRP at boot, so an integer divide by zero is a usage fault there; with the bit clear the division returns 0 and the program carries on with a wrong value.
Sources: Arm: Cortex-M4 Devices Generic User Guide (fault handling, CCR, ACTLR) · Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0), the code that prints these lines
A fault that could not run its own handler
***** HARD FAULT ***** followed by Fault escalation (see below) means a bus, usage or memory fault was escalated because its handler is disabled or masked at that moment; the block printed underneath names the original fault. Bus fault on vector table read is different: the core could not read a handler address from the vector table itself.
Sources: Zephyr: arch/arm/core/cortex_m/fault.c (v4.2.0), the code that prints these lines · Memfault Interrupt: How to debug a HardFault on an ARM Cortex-M MCU
What to check first
- Read the indented lines under the banner before anything else. They are the decoded fault status bits, and a
BFAR AddressorMMFAR Addressline is the exact address the code touched. - Resolve the value on the
Faulting instruction address (r15/pc)line against the ELF that is running on the board, for examplearm-none-eabi-addr2line -f -e build/zephyr/zephyr.elf 0x000004a2. Resolver14/lrthe same way to find the caller. - Compare the fault address with the memory map. Just below a stack buffer points at a stack overflow, a small value such as
0x00000008at a member read through a NULL pointer, and an address in no region at a wild pointer. - If the line is
Imprecise data bus error, the program counter is late. On a Cortex-M3 or M4, set the DISDEFWBUF bit of the Auxiliary Control Register for a test run so that the fault becomes precise, then read the address again. - If the banner is
HARD FAULTwithFault escalation, debug the fault named in the block below it, not the hard fault. - If the fault moves between runs, suspect a stack overflow or a use after free first: enlarge the stack of the thread on the
Current threadline and see whether the fault changes.
In NeuroTerm
On a serial tab with pattern markers on, NeuroTerm recognises this line as “Board crash: Cortex-M fault”. A coding agent connected through Terminal Use gets that name in its list of tabs and among the markers of the output it reads, with a cursor for reading the lines around it. Setup is in Terminal Use (MCP).