MPU
Definitions for ARMv8 MPU configuration.
This file contains definitions and macros for configuring the Memory Protection Unit (MPU) on ARM cores. It includes definitions for execution permissions, access permissions, memory attributes, and memory types.
- Copyright
Copyright (c) 2026 TNX Software Ltd. All rights reserved. Licensed under the terms of the QuantumRT Kernel License. the LICENSE file in the root of this package for details.
Defines
-
QRT_MPU_EXECUTE (0u)
Execute
-
QRT_MPU_EXECUTE_NEVER (1u)
Execute never
-
QRT_MPU_READWRITE (0u)
Read/Write access
-
QRT_MPU_READONLY (1u)
Read-only access
-
QRT_MPU_DEVICE_nGnRnE (0u)
-
QRT_MPU_DEVICE_nGnRE (1u)
-
QRT_MPU_DEVICE_GRE (2u)
Not supported on ARMv6/7
-
QRT_MPU_STRONGLY_ORDERED (QRT_MPU_DEVICE_nGnRnE)
ARMv6/7 compatibility
-
QRT_MPU_DEVICE (QRT_MPU_DEVICE_nGnRE)
ARMv6/7 compatibility
-
QRT_MPU_NORM_NCACHE (3u)
-
QRT_MPU_NORM_WB_WRA (4u)
Outer & inner cacheable, write-back, write allocate
-
QRT_MPU_NORM_WT_NWA (5u)
Outer & inner cacheable, write-through, no write allocate
-
QRT_MPU_NORM_WB_NWA (6u)
Outer & inner cacheable, write-back, no write allocate
-
QRT_MPU_NONSHAREABLE (0x00u)
Non-shareable
-
QRT_MPU_INNER_SHAREABLE (0x01u)
Inner Shareable (Inner + Outer Shareable for ARMv6/7)
-
QRT_MPU_OUTER_SHAREABLE (0x02u)
Outer Shareable (Inner + Outer Shareable for ARMv6/7)
-
QRT_MPU_SHAREABLE (QRT_MPU_OUTER_SHAREABLE)
Inner + Outer Shareable
-
QRT_MPU_SUBREGION_DISABLE(mask) (((mask) & 0xFFu) << 8)
Not supported on ARMv8
Functions
-
qrt_s32_t qrt_mpu_threadregionadd(pthread_t thread, qrt_s32_t *region_id, qrt_u32_t executability, qrt_u32_t permissions, qrt_u32_t shareability, qrt_u32_t attributes, qrt_u32_t address, qrt_u32_t size)
Add a memory region to threads region table.
Error numbers:
EINVAL Region size is not power of two.
EINVAL Region size is less than the minimum size (128 bytes for ARMv6, 32 bytes for ARMv7 and ARMv8).
EINVAL Region base address is not sized aligned (ARMv6 and ARMv7) or 32-byte aligned (ARMv8).
ENOSPC No more MPU regions available.
EPERM Called from unprivileged thread.
- Parameters:
thread – Thread handle.
region_id – Pointer to assigned region ID. NULL if not needed.
executability – Executability of the region.
permissions – Permissions of the region.
shareability – Shareability of the region.
attributes – Attributes of the region.
address – Base address of the region. Must be aligned by its size.
size – Size of the region in bytes.
- Returns:
0 if success, error number otherwise.
pthread_t thread; void *thread_routine(void *arg) { qrt_mpu_threadregionadd( thread, // Thread handle NULL, // No need for storing region ID QRT_MPU_EXECUTE, // Executable QRT_MPU_AP_RW, // Read/Write access QRT_MPU_SHAREABLE, // Region is shareable QRT_MPU_NORM_WB_WRA, // Cacheable, write-back, write allocate 0x20000000u, // Base address 0x1000u); // 4KB region for (;;) { ... } return NULL; }
-
qrt_s32_t qrt_mpu_threadregionremove(pthread_t thread, qrt_s32_t region_number)
Remove a memory region from a thread’s region table. The calling thread must be privileged and the following rule applies: (5 < region_number < QRT_CFG_MPU_ENTRIES).
- Parameters:
thread – Thread handle.
region_number – Region number to remove.
- Returns:
0 if success EINVAL if 5 < region_number < QRT_CFG_MPU_ENTRIES not fulfilled. EPERM if called from unprivileged thread.
-
qrt_s32_t qrt_mpu_faultcallbackset(void (*callback)(pthread_t, qrt_u32_t, qrt_u32_t, qrt_u32_t))
Set an optional callback function that is invoked when a Memory Protection Unit (MPU) fault occurs. The registered callback allows the application to handle MPU faults, such as invalid memory accesses.
The registered callback will be called in the fault handler context, so it must be minimal and should avoid blocking operations. ARMv7 and ARMv8 Mainline only.
Error numbers:
EINVAL Callback function pointer is NULL.
- Parameters:
callback –
Pointer to the callback function. The function must match the signature void (*callback)(
pthread_t thread,
qrt_u32_t address,
qrt_u32_t pc,
qrt_u32_t status).
- Returns:
0 if success, error number otherwise.
void MyMPU_FaultHandler(pthread_t thread, qrt_u32_t address, qrt_u32_t pc, qrt_u32_t status) { // Handle the MPU fault (e.g., log error, reset system, etc.). } int main(void) { ... qrt_mpu_faultcallbackset(MyMPU_FaultHandler); ... }
-
void qrt_mpu_faulthandler(void)
MPU fault handler called when a Memory Protection Unit (MPU) fault occurs.
Must be called from the Memory Management interrupt handler.
void MemManage_Handler(void) { qrt_mpu_faulthandler(); }