Follow+Star Public Account Number, don’t miss out on exciting content
Author | strongerHuang
WeChat Public Account | strongerHuang
First, let’s clarify what MPU means. MPU has many meanings, the most common ones are:
MPU: Memory Protection Unit, which is the focus of this article;
MPU: Microprocessor Unit;
Additionally, some may associate it with modules like MPU-6050. So, please do not confuse MPU.
Embedded Column
1
Why Use MPU?If your embedded project has suffered significant economic losses or serious accidents due to memory overflow or memory faults, you will understand the importance of using a Memory Protection Unit (MPU). Using an MPU in embedded systems can help identify memory-related bugs early in the development process, saving more development time. Moreover, when modifying bugs or adding features later in the project, it can reduce the time required for documentation and testing. In other words, using an MPU can prevent the situation where fixing one bug leads to multiple new bugs (0 gives birth to 1, 1 gives birth to myriad things
).
Embedded Column
2
How MPU Achieves Memory Protection In simple terms, it protects all data unrelated to the currently executing code. Take RTOS tasks A and B as an example: Tasks A and B should not interact with each other’s data, but there is an error where Task A may accidentally write to some data occasionally used by Task B, which does not affect the correct operation of Task A. However, when Task B tries to use the corrupted data, it may crash unexpectedly. If the MPU is not configured to prevent Task A from writing to Task B’s data, it may take a long time for developers to track down the error. If the error is minor, or if Task B rarely uses that data, it will be difficult to resolve that bug. However, if an MPU is used, the bug will be detected early. On some architectures, the MPU can even help detect NULL pointer references, as you can set MPU regions to prevent non-privileged code from accessing memory 0x0. A well-designed set of MPU regions in an application can effectively protect important memory areas from specific issues. A good example is placing buffers at the end of MPU regions to prevent buffer overflows; you can also place task stacks in areas inaccessible to any non-privileged code. If done this way, each task must use one of its MPU regions to set its own access permissions to its stack.
Embedded Column
3
Benefits of Using MPU Whether in an operating system or a bare-metal system, without the ability to prevent malicious access to erroneous memory, the system will have significant security issues and be a minefield of vulnerabilities. The use of Memory Protection Units (MPU) has many advantages; MPUs typically allow you to run in privileged or unprivileged mode and use a set of “regions” to determine whether the currently executing code has permission to access code and data. Each region is a contiguous block of memory with a set of permissions for that memory, allowing both privileged and unprivileged access. Compared to a subset of non-privileged code, privileged code often has access to most (but not all) of the memory. These regions do not have to be the same throughout the system’s runtime. MPU regions can be modified for each task, and each task can have its own unique set of regions configured when the task transitions to the running state. This allows you to set access permissions only for tasks that need code and data, leveraging the MPU’s embedded operating system to manage each task’s regions and privilege levels during every context switch. For example, setting different memory protection regions for two RTOS tasks:
This image is easy to understand, right? The Flash and memory regions are protected separately. Two global protection regions: at the start of Flash and at the start of RAM; In Flash, one part is accessible only to Task 1, while another part is accessible only to Task 2, preventing access from Task 1. If these two regions are accessed by each other, an MPU fault will occur. In the RAM region, the same part of the region can be read by one task and written by another; if not operated according to the agreement, an MPU fault will also occur.
Embedded Column
4
When Not to Use MPU? Generally, there are two situations where the MPU functionality on the processor can be omitted:
-
A simple project
-
A project where performance is critical
The first is straightforward: a very simple application does not need to use an MPU, as it would only add complexity to the system. Without setting memory protection, you can easily spot bugs in RAM and peripheral device MPU regions. The second is for projects with high performance requirements; setting memory protection and stack operations during context switching may affect the system’s real-time performance, leading to system anomalies. This needs to be considered based on the actual situation of the project regarding whether to use the MPU functionality.———— END ————Recommended Reading:
Selected Summary | Column | Directory | Search
Selected Summary | Embedded C Language
Selected Summary | STM32, Microcontrollers
Welcome to follow my public account, reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content. Welcome to follow my video account:
Click “Read the Original” for more shares, and feel free to share, bookmark, like, and view.