liveBook · Manning

MemManage Fault

As explained in Embedded Software with Rust

A memory management fault is a processor exception raised when an access violates a memory protection region's permissions.

A MemManage fault is a processor exception caused by a memory protection violation. For example, a Memory Protection Unit (MPU) region configured for no access raises a fault when code reads or writes within it. section 6.4.6 section 7.4.5

On Armv7-M and Armv8-M mainline targets, MemoryManagement is disabled at reset. Unless enabled, it escalates to HardFault; the HFSR FORCED bit indicates that escalation occurred. Enabling it separately makes the fault easier to classify. section 6.4.6

use cortex_m::peripheral::{scb::Exception, Peripherals};

let mut p = Peripherals::take().unwrap();
p.SCB.enable(Exception::MemoryManagement);
p.SCB.enable(Exception::BusFault);
p.SCB.enable(Exception::UsageFault);

Enable MemoryManagement through the System Control Block (SCB), alongside the other configurable faults. section 6.4.6

use cortex_m_rt::exception;

#[exception]
unsafe fn MemoryManagement() -> ! {
    panic!("MemoryManagement fault");
}

#[exception]
unsafe fn BusFault() -> ! {
    panic!("BusFault");
}

#[exception]
unsafe fn UsageFault() -> ! {
    panic!("UsageFault");
}

Register a dedicated MemoryManagement handler with cortex-m-rt. Unlike a HardFault handler, it receives no ExceptionFrame argument. section 6.4.6

For diagnosis, read the memory fault status and address registers, MMFSR and MMFAR, from the SCB. They provide details about the faulting access that can be logged for later analysis. section 6.4.6 section 7.4.5

Stack overflow guard

Place a no-access MPU region just below the stack’s lowest legitimate address. An overflow that reaches the guard faults on the offending instruction instead of silently corrupting memory. A 32-byte guard must have a 32-byte-aligned base address; it also consumes one of the MPU’s limited regions. section 7.4.5

How Embedded Software with Rust covers it

How deep it goes
Explains fault escalation, handler registration, diagnostic registers, and guard-region constraints.
Examples
Uses a 32-byte no-access stack guard and Rust handlers for configurable faults.
What you can do after
Diagnose memory protection faults by enabling MemoryManagement and inspecting its status and address registers.
How it connects
Connects MPU protection with HardFault escalation, exception handlers, stack overflow, and fault diagnosis.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Embedded Software with Rust. The section links point to the parts of the book it draws on.