Will the MPU be reconfigured after a task switch for a non-privileged task? If so, where is it configured? I didn’t see any implementation in the source code, only a portTASK_SWITCH_HOOK
hook. If not, how is it ensured that each non-privileged task can only access its own address space?
Hi @liu
Welcome to the FreeRTOS Community Forums !
Yes, the MPU will be reconfigured after a task switch for a non-privileged task.
Each task has its own MPU settings stored in its Task Control Block (TCB) (xMPUSettings
). When a task switch happens, the new task’s MPU settings are loaded into the MPU registers inside xPortPendSVHandler()
You can refer to the process of MPU reconfiguration here for CORTEX M4 MPU port.
This function does the following operations in sequence:
- Saves the current task’s context.
- Selects the next task(
vTaskSwitchContext()
). - Fetches the new task’s
xMPUSettings
from the TCB. - Disables the MPU to safely update regions.
- Loads the new task’s MPU regions into MPU registers (
RBAR
,RASR
). - Re-enables the MPU after writing settings.
- Restores task context and updates CONTROL register (if needed for privilege level change).
Thank you for your explanation. I noticed it might be related to ARM_CRx_MPU
, but I couldn’t find the relevant operations. Could you please help clarify this further?
Here is the code in the ARM_CRxz_MPU
port which reprograms the MPU on a context switch - FreeRTOS-Kernel/portable/GCC/ARM_CRx_MPU/portASM.S at main · FreeRTOS/FreeRTOS-Kernel · GitHub.
thank you very much。