0
0
Cnc-programmingConceptBeginner · 3 min read

MPU Memory Protection Unit in ARM: What It Is and How It Works

The MPU (Memory Protection Unit) in ARM processors is a hardware feature that controls access permissions to different memory regions. It helps protect memory by defining rules on who can read, write, or execute code in specific areas, improving system safety and security.
⚙️

How It Works

The MPU acts like a security guard for the processor's memory. It divides the memory into regions and sets rules for each region about who can access it and how. For example, it can say "this area is read-only" or "only trusted code can run here."

When the processor tries to access memory, the MPU checks these rules. If the access breaks the rules, the MPU stops it and raises an error. This prevents bugs or malicious code from accidentally or intentionally changing important data or running unsafe code.

Think of it like different rooms in a house with locked doors. Only people with the right keys can enter or change things inside. The MPU manages these keys and locks for the processor's memory.

💻

Example

This example shows how to configure an MPU region in ARM Cortex-M processors using C code. It sets a memory region as read-only and executable.

c
#include "core_cm4.h"  // CMSIS header for Cortex-M4

void MPU_Config(void) {
    // Disable MPU before configuration
    MPU->CTRL = 0;

    // Configure region 0
    MPU->RNR = 0; // Select region 0

    // Set base address of region 0
    MPU->RBAR = 0x20000000; // Start of SRAM

    // Configure region attributes:
    // Size = 32KB (encoded as 0x0E), enable region, read-only, executable
    MPU->RASR = (1 << 0) |       // Enable region
                  (0x0E << 1) |  // Size = 32KB
                  (0 << 28) |    // No execute disable (executable)
                  (6 << 24);     // Access permission: read-only

    // Enable MPU with default memory map background
    MPU->CTRL = (1 << 2) | 1; // PRIVDEFENA and ENABLE
}
Output
No direct output; MPU is configured to protect memory region as read-only and executable.
🎯

When to Use

Use the MPU when you want to improve system reliability and security by controlling memory access. It is especially useful in embedded systems where safety is critical, such as in medical devices, automotive controllers, or industrial machines.

For example, you can protect critical data from accidental overwrites or prevent untrusted code from running in sensitive memory areas. This helps catch programming errors early and stops some types of attacks.

Key Points

  • The MPU divides memory into regions with specific access rules.
  • It prevents unauthorized read, write, or execute operations.
  • MPU configuration is done in software but enforced by hardware.
  • It enhances security and stability in embedded ARM systems.
  • Commonly used in safety-critical and real-time applications.

Key Takeaways

The MPU controls access permissions to memory regions to protect system integrity.
It helps prevent bugs and security issues by enforcing read, write, and execute rules.
MPU is essential in embedded ARM systems for safety and security.
Configuring the MPU involves defining regions and their access rights in software.
Use the MPU to isolate critical code and data from untrusted or faulty parts.