You copied the Doc URL to your clipboard.

How do I test the memory fault handler in a Cortex-M system?

Information in this article applies to:

  • Cortex-M3

  • Cortex-M4

  • Cortex-M7

  • Cortex-M33


How do I test the memory fault handler in a Cortex-M system?


To test the handling of a memory fault (MemManage or BusFault), use an LDR or STR assembly instruction to a faulting address. The LDR and STR instructions always take a register as their base address pointer, for example, [r0]. The faulting access causes the exception and, if the fault is enabled, the handler is called. Typically, LDR is used for this purpose because it always produces a precise error.

The return address for a precise memory fault is the address of the faulting instruction. The fault handler might try to fix the problem, or might take some different action such as requesting that the current task is killed. If the handler tries to fix the problem, then this return address allows the same instruction to be retried.

The fault handler in a test program might record that the handler has been run and then change the system so that the retried access now succeeds. You can change the base address pointer within the handler so that it points to a non-faulting address on the retry.

However, changing [r0] directly does not work.


The processor architecture defines that registers r0-r3 and r12 are preserved by the exception handling mechanism. For this reason, any change of these register values inside an exception handler will not be visible to the code that was interrupted by the exception.

The simple way to let the handler change the address is to use a base address register in the range r5-r11. These registers are not automatically preserved by the exception handling mechanism. If the handler code does not include the explicit push and pop of these registers, any changes to their values can be seen by the interrupted code.

Alternative methods of adjusting the system (such as finding and modifying the stacked copy of r0 or of the return address, or modifying the instruction opcode) are more complicated.





Related Information


Was this page helpful? Yes No