universalisos/microkernel/include/uos_mpu.h
Fábio Coutada 62e8454f91 build: add UEFI ISO builder, test boot scripts, EIM config
- make-uefi-iso.sh: UEFI ISO image builder
- test_boot.S, test_boot.ld: test boot assembly and linker script
- eim_config.toml: EIM (External Interface Module) configuration
- edk2-ovmf RPM for UEFI firmware
- GAP_ANALYSIS_PIKEOS_PARITY.md: PikeOS parity gap analysis
2026-07-15 15:33:07 +01:00

98 lines
3.6 KiB
C

/*
* UniversalisOS Microkernel — MPU Region Abstraction
*
* ARMv6-M (Cortex-M0/M0+): No MPU.
* ARMv7-M (Cortex-M3/M4/M7): Up to 16 MPU regions.
* ARMv8-M (Cortex-M23/M33): Up to 16 MPU regions + optional TrustZone.
*
* Region assignment (hard RT):
* Region 0: Kernel code (FLASH) — priv RX, unpriv none
* Region 1: Kernel data (RAM) — priv RW, unpriv none
* Region 2: Per-task stack — reconfigured on every context switch
* Region 3: Peripherals (0x40000000+) — priv RW, device, XN
*/
#ifndef UOS_MPU_H
#define UOS_MPU_H
#include "uos_types.h"
#include "uos_config.h"
/* ====================================================================== */
/* Constants (always available — no MPU required) */
/* ====================================================================== */
/* Region access permissions (RASR.AP field, bits 4:2) */
#define UOS_MPU_AP_NONE 0x00 /* No access */
#define UOS_MPU_AP_RW_PRIV 0x03 /* RW privileged only */
#define UOS_MPU_AP_RO_PRIV 0x05 /* RO privileged only */
/* Region enable/disable */
#define UOS_MPU_REGION_ENABLE 1
#define UOS_MPU_REGION_DISABLE 0
/* MPU region assignments */
#define UOS_MPU_REGION_KERNEL_CODE 0
#define UOS_MPU_REGION_KERNEL_DATA 1
#define UOS_MPU_REGION_TASK_STACK 2
#define UOS_MPU_REGION_PERIPHERALS 3
/* Region sizes (log2) */
#define UOS_MPU_SIZE_32B 4
#define UOS_MPU_SIZE_64B 5
#define UOS_MPU_SIZE_128B 6
#define UOS_MPU_SIZE_256B 7
#define UOS_MPU_SIZE_512B 8
#define UOS_MPU_SIZE_1KB 9
#define UOS_MPU_SIZE_2KB 10
#define UOS_MPU_SIZE_4KB 11
#define UOS_MPU_SIZE_8KB 12
#define UOS_MPU_SIZE_16KB 13
#define UOS_MPU_SIZE_32KB 14
#define UOS_MPU_SIZE_64KB 15
#define UOS_MPU_SIZE_128KB 16
#define UOS_MPU_SIZE_256KB 17
#define UOS_MPU_SIZE_512KB 18
#define UOS_MPU_SIZE_1MB 19
#define UOS_MPU_SIZE_2MB 20
#define UOS_MPU_SIZE_4MB 21
/* ====================================================================== */
/* MPU Register Access (Cortex-M3/M4/M7) */
/* ====================================================================== */
#if UOS_HAS_MPU
#define UOS_MPU_TYPE (*(volatile uint32_t*)0xE000ED90)
#define UOS_MPU_CTRL (*(volatile uint32_t*)0xE000ED94)
#define UOS_MPU_RNR (*(volatile uint32_t*)0xE000ED98)
#define UOS_MPU_RBAR (*(volatile uint32_t*)0xE000ED9C)
#define UOS_MPU_RLAR (*(volatile uint32_t*)0xE000EDA0)
void uos_mpu_init(void);
void uos_mpu_set_region(uint32_t region_num, uint32_t base,
uint32_t size_log2, uint32_t attr);
void uos_mpu_set_region_rasar(uint32_t region_num, uint32_t rbar,
uint32_t rasr);
void uos_mpu_region_enable(uint32_t region_num);
void uos_mpu_region_disable(uint32_t region_num);
void uos_mpu_enable(void);
void uos_mpu_disable(void);
/* Configure MPU region 2 for a task's stack (called on context switch).
* O(1) — 3 register writes + DSB. */
void uos_mpu_apply_task(const void* task);
#else /* No MPU — no-ops */
UOS_INLINE void uos_mpu_init(void) {}
UOS_INLINE void uos_mpu_set_region(uint32_t r, uint32_t b, uint32_t s, uint32_t a) { (void)r; (void)b; (void)s; (void)a; }
UOS_INLINE void uos_mpu_set_region_rasar(uint32_t r, uint32_t b, uint32_t rasr) { (void)r; (void)b; (void)rasr; }
UOS_INLINE void uos_mpu_region_enable(uint32_t r) { (void)r; }
UOS_INLINE void uos_mpu_region_disable(uint32_t r) { (void)r; }
UOS_INLINE void uos_mpu_enable(void) {}
UOS_INLINE void uos_mpu_disable(void) {}
UOS_INLINE void uos_mpu_apply_task(const void* t) { (void)t; }
#endif /* UOS_HAS_MPU */
#endif /* UOS_MPU_H */