maxikrie opened a new pull request, #22028: URL: https://github.com/apache/nuttx/pull/22028
## Summary `mpu_modify_region()` aligns the base down to the region size, recomputes the size for the now larger span, and stops. That second step can make the region larger again, and the base is not aligned to the new size. The ARMv7-M ARM (B3.5.9, MPU_RBAR) requires the base to be aligned to the region size; the function's own `DEBUGASSERT((alignedbase & ((1 << l2size) - 1)) == 0)` fires. Example: base 0x21adfcc8, size 0xc059. The first pass gives a 64 KiB region at 0x21ad0000; that region must reach 0x21aebd21, so the second pass picks 128 KiB, which needs 0x20000 alignment, but RBAR stays 0x21ad0000. A host model of the code shows how common this is: for random base/size pairs (base 0x20000000..0x24080000, size 32 B..512 KiB) 37 % of the results were misaligned. This PR repeats aligning and recomputing until the base is aligned. `l2size` only grows, so the loop ends after a few steps; the 4 GiB case (`l2size == 32`) is excluded from the shift. Results that were already aligned are unchanged. On the same model with 300000 random base/size pairs (base 0x20000000..0xc0000000, size up to 64 MiB), the new code always gives an aligned base, always covers `[base, base + size)`, and always picks the smallest aligned power-of-two region that does. Related: #8780. That report's example (base 0x80800048, size 0xd15620) is aligned with the current code (0x80000000, 32 MiB, subregions disable the first and last two 4 MiB); it does not change with this PR. ## Impact All ARMv7-M MPU users (protected builds, and drivers that set up MPU regions). Regions whose base was misaligned now get an aligned base and, where needed, a larger region with subregions disabled, instead of an UNPREDICTABLE RBAR value and a failing `DEBUGASSERT`. Regions that were already aligned are configured exactly as before. ## Testing NUCLEO-H753ZI (Cortex-M7), master fc8d354f9a, `nucleo-h753zi:nsh` + `CONFIG_ARM_MPU`, `CONFIG_DEBUG_ASSERTIONS`. Arm GNU Toolchain 13.3.Rel1. `tools/checkpatch.sh -g HEAD` passes. A test-only NSH app allocates a region with `mpu_allocregion()`, calls `mpu_modify_region()` with full access and normal write-back attributes for five base/size pairs, and reads back RBAR and RASR: | base, size | master | this PR | |---|---|---| | 0x24001cc8, 0xc059 | RBAR 0x24000000, 64 KiB, ok | same | | 0x240e5414, 0x3766e | `Assertion failed: armv7-m/arm_mpu.c:501` | RBAR 0x24000000, 2 MiB, SRD 0xe7, aligned, covers | | 0x2400b4cc, 0x57e0b | (not reached) | RBAR 0x24000000, 512 KiB, SRD 0x80, aligned, covers | | 0x24000000, 0x80000 | (not reached) | RBAR 0x24000000, 512 KiB, SRD 0x00 | | 0x80800048, 0xd15620 (#8780) | (not reached) | RBAR 0x80000000, 32 MiB, SRD 0xc3, aligned, covers | <details><summary>this PR</summary> ``` nsh> mputest MPU_CTRL=00000005 region 0 base 24001cc8 size 0000c059 -> RBAR 24000000 RASR 030b801f 2^16 aligned=1 covers=1 srd=80 base 240e5414 size 0003766e -> RBAR 24000000 RASR 030be729 2^21 aligned=1 covers=1 srd=e7 base 2400b4cc size 00057e0b -> RBAR 24000000 RASR 030b8025 2^19 aligned=1 covers=1 srd=80 base 24000000 size 00080000 -> RBAR 24000000 RASR 030b0025 2^19 aligned=1 covers=1 srd=00 base 80800048 size 00d15620 -> RBAR 80000000 RASR 030bc331 2^25 aligned=1 covers=1 srd=c3 mputest: 0 bad ``` </details> <details><summary>master</summary> ``` nsh> mputest MPU_CTRL=00000005 base 24001cc8 size 0000c059 -> RBAR 24000000 RASR 030b801f 2^16 aligned=1 covers=1 srd=80 dump_assert_info: Assertion failed : at file: armv7-m/arm_mpu.c:501 task: <noname> process: <noname> 0x8024591 ``` </details> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
