From 3297bbec446f66e05d439f73bafcbb7b76cd7c5e Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Mon, 28 Sep 2026 14:25:26 +0200 Subject: [PATCH] arch/arm/mps: Let user code run modules from the text and data heaps. In a protected build a module loaded by exec() runs as a user task, and the loader puts it in the text and data heaps, which on this chip are SRAM2. Nothing gave user code access to SRAM2: the region that would have done so, in arm_addregion(), exists only with CONFIG_MM_REGIONS > 1, and it is built with mpu_user_intsram(), which on ARMv8-M is execute-never. So the module faulted on its first instruction. The protected build now maps SRAM2 for user code to read, write and execute when either heap is in use. Privileged execution stays allowed, since a kernel module loaded with insmod lands in the same heaps. On mps3-an547:knsh under QEMU, with CONFIG_ELF, both heaps and the ROMFS variant of examples/elf, errno faulted on its first instruction with an instruction access violation at 0x21000001. Now every module of the example runs to the end. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli --- arch/arm/src/mps/mps_allocateheap.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/arch/arm/src/mps/mps_allocateheap.c b/arch/arm/src/mps/mps_allocateheap.c index 4dd5bc21242..65db21b8cba 100644 --- a/arch/arm/src/mps/mps_allocateheap.c +++ b/arch/arm/src/mps/mps_allocateheap.c @@ -164,6 +164,16 @@ void up_allocate_heap(void **heap_start, size_t *heap_size) /* Allow user-mode access to the user heap memory */ mpu_user_intsram(ubase, usize); + +# if defined(CONFIG_ARCH_USE_TEXT_HEAP) || defined(CONFIG_ARCH_USE_DATA_HEAP) + /* A module loaded for a user task runs out of the text and data heaps in + * SRAM2, so user code must be able to execute it as well as read and + * write it. mpu_user_intsram() would make it execute-never. + */ + + mpu_configure_region(MPS_SRAM2_START, MPS_SRAM2_SIZE, + MPU_RBAR_AP_RWRW, MPU_RLAR_NONCACHEABLE); +# endif #elif defined(CONFIG_BUILD_PIC) /* Use different heap useful to debug */