# 内存保护单元

内存保护单元（MPU）是处理器内部一个可编程的硬件模块，它为物理地址空间中的各个范围定义内存访问权限和内存属性，例如可缓存性和可执行性。与内存管理单元（MMU）不同，MPU 不执行虚拟地址到物理地址的转换；它只对既有的物理内存映射强制执行访问规则。在 Arm 处理器中，MPU 是受保护内存系统架构（PMSA）的硬件实现，它是 Cortex-M0+、Cortex-M3、Cortex-M4 和 Cortex-M7 处理器以及 Cortex-R 系列上的可选特性。<sup>[1](https://www.oreilly.com/library/view/the-definitive-guide/9780128032787/XHTML/B9780128032770000126/B9780128032770000126.xhtml)</sup><sup> • </sup><sup>[2](https://wiki.osdev.org/ARMv7_Common_Memory_System_Architecture)</sup>

| 关键事实 | 详情 |
|---|---|
| 功能 | 按物理地址空间的区域定义访问权限和内存属性（可缓存性、可共享性、永不执行）；不做地址转换<sup>[1](https://www.oreilly.com/library/view/the-definitive-guide/9780128032787/XHTML/B9780128032770000126/B9780128032770000126.xhtml)</sup><sup> • </sup><sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| 区域数量 | Armv7-M：8 个区域（Cortex-M3、M4）；Cortex-M7：8 或 16 个；Armv8-M：可配置，Cortex-M33、M55 和 M85 中最多 16 个<sup>[4](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm%20Cortex-M3%20Processor%20Datasheet.pdf?hash=3B8331E80EAFF9A54820052F1B82D818&revision=9f0ebb42-4bcb-4ba4-879b-4f6b2a1f6ab7)</sup><sup> • </sup><sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm-Cortex-M7-Processor-Datasheet.pdf?hash=5C37DFAA43B532B8BD2DB277685F76007FCFEAE5&la=en&revision=35233191-7cdb-429e-9170-e07ad9d87c7a)</sup><sup> • </sup><sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| 故障行为 | 违反权限或落在已定义区域之外的访问会被阻止，并触发 MemManage 故障<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| 编程 | 必须由特权软件编程并启用；若未启用，从软件角度看 MPU 实际上不存在<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| Armv7-M 约束 | 区域大小均为 2 的幂，基地址必须按区域大小对齐，区域可以重叠，编号较大的区域优先<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| Armv8-M 约束 | 起始和结束地址只需 32 字节对齐，区域可为任意大小，不允许区域重叠<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> |
| TrustZone | 在带 TrustZone 的 Armv8-M 上，可实现独立的安全与非安全 MPU，区域数量可按需不同<sup>[6](https://documentation-service.arm.com/static/5f872f18405d955c5176dec9?token=)</sup> |

## 目的及其与 MMU 的关系

MPU 之所以存在，是因为许多嵌入式系统无法承担 MMU 的成本。[虚拟内存](https://www.edgepedia.org/zh/virtual-memory)和缓存转换硬件会带来内存和速度上的开销，在资源受限或性能关键的微控制器上很难被接受，因此这些系统改用更简单的 PMSA 方案。<sup>[2](https://wiki.osdev.org/ARMv7_Common_Memory_System_Architecture)</sup> Armv8-M 架构支持 32 位寻址和 4 GB 线性地址空间，Armv8-M 中使用的所有地址都是物理地址；不存在虚拟地址的概念。<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup>

MPU 带来的是无地址转换的隔离。Armv8-M 文档列出了四种典型用途：防止应用程序任务破坏其他任务和操作系统内核使用的栈或数据内存，防止非特权任务访问关键外设，将 SRAM 标记为永不执行（XN）以阻止代码注入攻击，以及使用特权永不执行（PXN）特性阻止操作系统意外执行用户代码和数据。PXN 仅在 Armv8.1-M 中可用，Armv6-M、Armv7-M 和 Armv8.0-M 中没有。<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup>

## Armv7-M 中区域的工作方式

在 Armv7-M 的 MPU 中，每个区域由基地址和编码在 MPU 区域属性与大小寄存器（MPU_RASR）中的大小来描述，其中区域大小（以字节计）等于 \( 2^{\mathrm{SIZE}+1} \)，允许的最小 SIZE 值为 3。基地址必须按区域大小对齐；例如一个 64 KB 的区域必须位于 64 KB 的整数倍处。<sup>[7](https://documentation-service.arm.com/static/5f2ac4ab60a93e65927bbdbf?token=)</sup>

**子区域**在实践中放宽了 2 的幂约束。256 字节及以上的区域被划分为八个等大的子区域，MPU_RASR 的八个子区域禁用（SRD）位可分别禁用各个子区域。禁用一个子区域会让另一个重叠区域的属性适用于该范围；如果没有其他已启用区域覆盖它，MPU 会发出故障。32、64 和 128 字节的区域不支持子区域，它们的 SRD 字段必须写为 0x00。<sup>[7](https://documentation-service.arm.com/static/5f2ac4ab60a93e65927bbdbf?token=)</sup>

Cortex-M3 和 Cortex-M4 实现了一个可选的 8 区域 MPU，带有子区域和一个背景区域；Cortex-M7 的数据手册列出 8 或 16 个区域。<sup>[4](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm%20Cortex-M3%20Processor%20Datasheet.pdf?hash=3B8331E80EAFF9A54820052F1B82D818&revision=9f0ebb42-4bcb-4ba4-879b-4f6b2a1f6ab7)</sup><sup> • </sup><sup>[5](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm-Cortex-M7-Processor-Datasheet.pdf?hash=5C37DFAA43B532B8BD2DB277685F76007FCFEAE5&la=en&revision=35233191-7cdb-429e-9170-e07ad9d87c7a)</sup> 使用前必须在 MPU_CTRL 寄存器中启用 MPU，而当重新编程一个先前已配置的 MPU 时，应禁用未使用的区域，以免旧设置影响新配置。<sup>[8](https://developer.arm.com/documentation/ddi0489/b/initialization/about-initialization/initializing-the-mpu)</sup>

## Armv8-M 中的变化

Armv8-M 引入了 PMSAv8 架构，其程序员模型不同于 Cortex-M0+、M3、M4 和 M7 所使用的 PMSAv7 模型。在 PMSAv8 中，区域的起始和结束地址只需按 32 字节边界对齐，因此区域可为任意大小；不允许区域重叠——指南指出，鉴于地址配置更加灵活，区域重叠已无必要；内存属性通过一组内存属性寄存器中的索引来定义，而不是在 MPU_RASR 中逐区域编程。PMSAv8 中不存在子区域的概念。<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> 区域数量可由芯片设计者配置，在当前 Armv8-M 的 Cortex-M 处理器（Cortex-M33、Cortex-M55、Cortex-M85）中最多可达 16 个。<sup>[3](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)</sup> 例如，Cortex-M35P 提供可选的 MPU，最多 16 个区域外加一个背景区域，并且在带 TrustZone 时可以有一个安全 MPU 和一个非安全 MPU。<sup>[9](https://support.arm.com/compute-ip/cortex-m35p)</sup>

Armv8-M 还将 MPU 与安全属性单元（SAU）配对使用，后者将内存和外设划分为安全与非安全两部分，其程序员模型与 MPU 类似。总线传输根据处理器当前的安全状态，使用安全或非安全 MPU 进行查找。<sup>[6](https://documentation-service.arm.com/static/5f872f18405d955c5176dec9?token=)</sup> Cortex-M23 提供可选的 SAU 和 MPU，用于细粒度的内存控制，支持多特权级别以及代码、数据和栈的逐任务分离。<sup>[10](https://support.arm.com/documentation/dui1095/a/Cortex-M23-Peripherals/Security-Attribution-and-Memory-Protection)</sup>

## Cortex-M 之外的 MPU

MPU 并非微控制器架构类别所独有。ARMv7-R 内存系统包含一个由 CP15 寄存器控制的 MPU，不支持虚拟寻址——这是针对硬实时系统的有意选择，因为转换引起的延迟变化在这些系统中是不可接受的。<sup>[11](https://support.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Application-Level-Memory-Model/Virtual-and-physical-addressing)</sup> 这一谱系可追溯到 1990 年代末：ARM946E-S 实现了一个 MPU，让软件能够将代码和数据划分到具有可配置访问权限的内存区域中，Arm 社区博客将其描述为安全关键系统的基本要求。<sup>[12](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/armv7-r---hidden-power)</sup>

## 与实时操作系统配合使用

**FreeRTOS** 在 ARMv7-M（Cortex-M3、M4、M7）和 ARMv8-M（Cortex-M23、M33）内核上提供官方的 MPU 支持，并由第三方维护到其他内核的移植。其 MPU 移植版本让任务以特权或非特权模式运行，并限制对 RAM、可执行代码、外设以及任务栈限制之外内存的访问。FreeRTOS 指出，防止代码从 RAM 中执行可以抵御缓冲区溢出漏洞利用或执行加载到 RAM 中的恶意代码等攻击途径。非特权任务只能访问自己的栈以及最多三个用户可定义的内存区域，这些区域在任务创建时分配，并可在运行时重新配置；非特权任务之间不共享数据内存，但它们可以通过标准队列和信号量机制交换消息。<sup>[13](https://github.com/FreeRTOS/FreeRTOS-Website-Content/blob/main/content/en-us/Security/04-FreeRTOS-MPU-memory-protection-unit.md)</sup> 使用 MPU 会使应用设计更加复杂，因为必须确定区域限制并将其描述给 RTOS，而且任务会失去不受限制的能力；FreeRTOS 建议采用伪进程与线程模型，例如为可信的第一方代码分配一个内存空间，为不可信的第三方代码分配另一个。<sup>[13](https://github.com/FreeRTOS/FreeRTOS-Website-Content/blob/main/content/en-us/Security/04-FreeRTOS-MPU-memory-protection-unit.md)</sup>

**Zephyr** 将 MPU 用于用户模式线程和栈保护。在配备标准 Arm MPU 或类似逻辑（例如 Kinetis 平台的 NXP MPU）的 Cortex-M 平台上支持用户模式，线程通过触发 SVC 异常切换到特权模式来执行系统调用。`CONFIG_MPU_STACK_GUARD` 选项通常为一个栈保护保留一个 MPU 区域，在启用间隙填充的某些 Armv8-M 配置中则保留两个，MPU 保护在每次上下文切换时都会重新编程，给线程切换例程增加少量开销。<sup>[14](https://d25yug97gus487.cloudfront.net/latest/hardware/arch/arm_cortex_m.html)</sup>

## 采用情况与批评

尽管许多设备上已具备这一硬件，但由 Wei Zhou、Le Guan、Peng Liu 和 Yuqing Zhang（当时发表嵌入式系统安全研究的几位安全研究人员）在 2019 年进行的一项研究发现，MPU 在现实产品中很少被使用。作者报告称，除了兼容性问题等非技术原因之外，所实现的 MPU 实际上没有带来任何安全增强，反而牺牲了性能和响应性；他们考察了主要实时操作系统（尤其是 FreeRTOS）中 MPU 的采用情况，以解释其为何不受青睐。<sup>[15](https://arxiv.org/abs/1908.03638)</sup> 让 MPU 式保护更易用的研究仍在继续：Pip-MPU 项目改造了围绕 MMU 设计的 Pip 内核，使其能在配备 MPU 的资源受限设备（如 ARMv7 Cortex-M4 和 ARMv8 Cortex-M33）上运行，在没有转换单元的情况下保留多级隔离。<sup>[16](https://arxiv.org/abs/2301.04546)</sup>

## References

1. [Chapter 12, Memory Protection Unit, The Definitive Guide to ARM Cortex-M0 and Cortex-M0+ Processors, 2nd Edition (O'Reilly)](https://www.oreilly.com/library/view/the-definitive-guide/9780128032787/XHTML/B9780128032770000126/B9780128032770000126.xhtml)
2. [ARMv7 Common Memory System Architecture, OSDev Wiki](https://wiki.osdev.org/ARMv7_Common_Memory_System_Architecture)
3. [Armv8-M Memory Model and Memory Protection User Guide, Arm](https://documentation-service.arm.com/static/64c783b138511951cb7a2575?token=)
4. [Arm Cortex-M3 Processor Datasheet](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm%20Cortex-M3%20Processor%20Datasheet.pdf?hash=3B8331E80EAFF9A54820052F1B82D818&revision=9f0ebb42-4bcb-4ba4-879b-4f6b2a1f6ab7)
5. [Arm Cortex-M7 Processor Datasheet](https://developer.arm.com/-/media/Arm%20Developer%20Community/PDF/Processor%20Datasheets/Arm-Cortex-M7-Processor-Datasheet.pdf?hash=5C37DFAA43B532B8BD2DB277685F76007FCFEAE5&la=en&revision=35233191-7cdb-429e-9170-e07ad9d87c7a)
6. [TrustZone technology for ARMv8-M Architecture, Arm](https://documentation-service.arm.com/static/5f872f18405d955c5176dec9?token=)
7. [Cortex-M4 Devices Generic User Guide, Arm](https://documentation-service.arm.com/static/5f2ac4ab60a93e65927bbdbf?token=)
8. [Initializing the MPU, Arm Cortex-M7 Processor Technical Reference Manual](https://developer.arm.com/documentation/ddi0489/b/initialization/about-initialization/initializing-the-mpu)
9. [Cortex-M35P Product Support, Arm](https://support.arm.com/compute-ip/cortex-m35p)
10. [Arm Cortex-M23 Devices Generic User Guide r1p0, Arm](https://support.arm.com/documentation/dui1095/a/Cortex-M23-Peripherals/Security-Attribution-and-Memory-Protection)
11. [ARM Architecture Reference Manual ARMv7-A and ARMv7-R edition, Arm](https://support.arm.com/documentation/ddi0406/c/Application-Level-Architecture/Application-Level-Memory-Model/Virtual-and-physical-addressing)
12. [ARMv7-R - Hidden Power, Arm Community blog](https://developer.arm.com/community/arm-community-blogs/b/architectures-and-processors-blog/posts/armv7-r---hidden-power)
13. [Memory Protection Unit (MPU) Support, FreeRTOS documentation](https://github.com/FreeRTOS/FreeRTOS-Website-Content/blob/main/content/en-us/Security/04-FreeRTOS-MPU-memory-protection-unit.md)
14. [Arm Cortex-M Developer Guide, Zephyr Project Documentation](https://d25yug97gus487.cloudfront.net/latest/hardware/arch/arm_cortex_m.html)
15. [Good Motive but Bad Design: Why ARM MPU Has Become an Outcast in Embedded Systems (Zhou et al., 2019), arXiv](https://arxiv.org/abs/1908.03638)
16. [From MMU to MPU: adaptation of the Pip kernel to constrained devices (Dejon et al., 2023), arXiv](https://arxiv.org/abs/2301.04546)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Processors & processor engineering › Instruction set architectures › CPU operating modes and ISA-support mechanisms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/memory-protection-unit

English original: https://www.edgepedia.org/memory-protection-unit
