1. Today’s topic

Today we examine hardware memory protection:

text
MPU — Memory Protection Unit
privileged and unprivileged code
read-only and execute-never regions
stack guard
configuration protection
DMA buffer isolation
FreeRTOS MPU tasks
ESP32 stack watchpoints and memory protection

The central idea: MPU does not fix a bug. It can trap a forbidden access to a protected region close to the responsible operation. An out-of-bounds object write that stays inside an allowed region may go undetected.

2. Why this matters in your projects

For EC25 and the AT parser, MPU may detect entry into a forbidden region before a later crash in malloc() or MQTT. MPU boundaries are not the boundaries of every C array. A phase detector can protect read-only tables:

text
GPIO mapping
channel active level
threshold values
phase transition policy
permitted phase combinations

The core MPU generally does not constrain a separate DMA controller. It can set CPU attributes for a DMA pool, such as execute-never and non-cacheable access. A CPU guard does not stop DMA; a canary after the buffer can help detect corruption but does not prevent it.

3. Theory

3.1. What MPU is

MPU describes memory regions and assigns:

text
address and size
privileged/unprivileged access
read-only or read-write
execute-never
cacheable/bufferable/shareable
memory type

MPU does not create virtual addresses or replace an MMU; it sets CPU permissions for physical regions. Region formats depend on the architecture: Armv7-M size/alignment settings cannot be copied unchanged to an Armv8-M base/limit MPU.

3.2. A typical protection map

RegionAccessExecution
Flash containing coderead-onlypermitted
.rodataread-onlyforbidden where MPU granularity permits
SRAM used for dataread-writeforbidden
Task stacksread-writeforbidden
Heapread-writeforbidden
DMA poolread-write; explicit cache policyforbidden
Peripheral registersprivileged read-writeforbidden
Configuration snapshotread-only after initializationforbidden
Unused memoryno accessforbidden

Execute-never is useful for ordinary data RAM. If firmware needs code in RAM, such as a handler used while Flash is unavailable, assign it a minimal separate executable region with suitable permissions. This map is a design example, not a ready-made configuration for every STM32:

text
RAM = XN, execute-never

3.3. Stack overflow: protection levels

text
1. Correct stack size.
2. Software canary for FreeRTOS.
3. Hardware watchpoint at the stack end on ESP32.
4. MPU guard region next to the stack.

A CPU access to a forbidden guard region faults when MPU permissions apply to the current privilege mode. This does not catch every overflow: a large erroneous access may jump over a narrow guard. Correct stack sizing, access rules, and a fault handler with a usable stack are still required.

3.4. FreeRTOS MPU port

FreeRTOS MPU ports support restricted tasks. The following lists API names rather than complete calls; required parameters and alignment rules must be checked for the selected port:

text
xTaskCreateRestricted
vTaskAllocateMPURegions

The idea:

text
unprivileged modem logic
→ command queue
→ privileged UART driver

The AT parser should not directly access power GPIOs, Flash configuration, or another task’s stack.

3.5. MPU and DMA

A DMA controller is a separate bus master; the core MPU is not a universal filter for its transactions. Therefore, you still need:

text
exact transfer length
static assertions for sizes
DMA descriptors
guard/canary after the buffer
sequence numbers
DMA error flags checking

On STM32H7/F7, a non-cacheable pool may simplify DMA, but that memory must be reachable by the selected DMA controller. Not every DMA can reach every SRAM bank. Correct cache maintenance with the required alignment is an alternative; MPU attributes alone do not establish buffer ownership.

3.6. ESP32 without a classic MPU

For ESP32-WROOM-32U, a practical protection set is:

text
FreeRTOS end-of-stack watchpoint
FreeRTOS stack overflow checks
GCC stack protector
heap poisoning
heap integrity checks
hardware watchpoints
core dump
owner-task architecture
bounds-checked parsers

4. Common mistakes

text
1. Enabling MPU without a complete memory map.
2. Allowing privileged default map and assuming everything is protected.
3. Making all SRAM executable.
4. Protecting the wrong side of the stack.
5. Guard lies in the same permissive MPU region.
6. Copying MPU settings from another Cortex-M.
7. Incorrect DMA-region cacheability.
8. Expecting MPU to stop DMA.
9. A guard that is too small.
10. A HardFault handler prints through a corrupt stack.
11. Making MPU the first step instead of fixing ownership.

5. Practical assignment for 30-60 minutes

Create MEMORY_PROTECTION_POLICY.md:

text
# Memory protection policy
1. Flash code is read-only and executable.
2. General SRAM is read-write and execute-never.
3. Peripheral memory is privileged and execute-never.
4. DMA pool is execute-never and has explicit cache policy.
5. Validated runtime configuration becomes read-only.
6. Critical task stacks have guard protection where possible.
7. MemManage faults save PC, LR, CFSR and MMFAR.
8. MPU configuration is generated for the exact MCU architecture.

For a supported STM32, reserve a configuration region and use privileged code with the required barriers to make it read-only. Test an intentional write in isolated debug firmware. Investigate ESP32 stack watchpoints and compiler protection separately in the same controlled way. These tests have not been run here. An application may implement this diagnostic command:

text
memprot status

Example:

text
memory protection:
MPU=enabled
regions=6
RAM_XN=yes
config_RO=yes
DMA_region=non-cacheable
stack_guards=3
last_fault=none

6. Further reading

  • CMSIS-Core MPU API.
  • STM32CubeMX/CubeMX2 MPU configuration for the selected STM32.
  • FreeRTOS MPU support.
  • ESP-IDF Fatal Errors and Heap Memory Debugging.

Brief recap

text
owner-task architecture
→ bounded buffers
→ stack sizing
→ canaries/watchpoints
→ XN for RAM
→ read-only config
→ DMA memory policy
→ stack guards
→ restricted RTOS tasks
→ HIL fault injection

Exercise

A CPU write into a protected configuration snapshot faults correctly, but DMA still corrupts adjacent memory. Explain why the MPU result does not prove DMA safety.

Self-check criteria: Separate CPU access control from DMA bounds. A successful CPU protection test cannot replace DMA ownership/length validation.

Show the supplied answer

The CPU MPU may not constrain the separate DMA bus master. Verify transfer lengths, destination capacity, descriptors, buffer ownership, and guard/canary evidence; use the target’s DMA-access and cache policy.

Exercise

Design a controlled stack-guard test for a debug/HIL build. What target-specific properties must be checked before deliberately reaching the guard?

Self-check criteria: Show that the guard lies outside permitted stack access and that diagnostics do not depend on the already corrupted stack.

Show the supplied answer

Check stack growth direction, region alignment/size, overlapping MPU permissions, the target’s watchpoint/MPU capability, and the fault handler’s usable stack. Trigger the test only in the isolated debug setup and retain fault evidence.