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
