hujun260 opened a new pull request, #22048:
URL: https://github.com/apache/nuttx/pull/22048
# arch/arm64: add 16-byte alignment to `struct arm_mmu_region`
## Summary
Add `aligned_data(16)` to `struct arm_mmu_region` so that every element of
the
`g_mmu_regions[]` array used by `setup_page_tables()` is naturally 16-byte
aligned. This makes 128-bit NEON loads safe on the early-boot Device-nGnRnE
mapping that is still in effect before the MMU is enabled.
One-line change:
```diff
--- a/arch/arm64/src/common/arm64_mmu.h
+++ b/arch/arm64/src/common/arm64_mmu.h
@@ -312,7 +312,7 @@
/* Region definition data structure */
-struct arm_mmu_region
+struct aligned_data(16) arm_mmu_region
{
/* Region Base Physical Address */
uint64_t base_pa;
```
## Problem
`setup_page_tables()` walks `g_mmu_config.mmu_regions[]` twice and reads four
fields (`base_pa`, `base_va`, `size`, `name`) plus `attrs` from each element.
This happens in `arm64_mmu_init()` **before**
`enable_mmu_el1()`/`enable_mmu_el3()`
runs, so the accesses hit memory that the CPU treats as Device-nGnRnE.
Device memory requires the access size to match the natural alignment of the
access. A 128-bit access must therefore be 16-byte aligned. Because the
struct
was only 8-byte aligned and had a 40-byte size, elements alternated between
aligned and unaligned.
Measured on the real `qemu-armv8a:nsh` link (`g_mmu_regions` for that board
has
6 elements), read from the linked ELF with `readelf -sW`:
```text
regions = 6
baseline addr=0x403d9108 size=240 stride=40 addr%16=8
patched addr=0x403d9100 size=288 stride=48 addr%16=0
```
The *absolute* address is not stable across builds (a second build of the
patched tree placed the array at `0x403d9130`); the size and the alignment
are:
| build | symbol size | addr `% 16` |
| --- | --- | --- |
| `apache/master` (2 builds) | 240 B | 8, 8 |
| with this patch (2 builds) | 288 B | 0, 0 |
Per-element alignment, derived arithmetically from those measured base
addresses
and strides — every even index is misaligned before the patch:
```text
baseline (stride 40) patched (stride 48)
[0] 0x403d9108 %16 = 8 [0] 0x403d9100 %16 = 0
[1] 0x403d9130 %16 = 0 [1] 0x403d9130 %16 = 0
[2] 0x403d9158 %16 = 8 [2] 0x403d9160 %16 = 0
[3] 0x403d9180 %16 = 0 [3] 0x403d9190 %16 = 0
[4] 0x403d91a8 %16 = 8 [4] 0x403d91c0 %16 = 0
[5] 0x403d91d0 %16 = 0 [5] 0x403d91f0 %16 = 0
```
Which reduces to:
```text
baseline unaligned indices: [0, 2, 4]
patched unaligned indices: []
```
Adding `aligned_data(16)` to the struct raises both `__alignof__` and the
size
(48 bytes, a multiple of 16), so the array base and every element step land
on a
16-byte boundary.
## Verification
### 1. Struct semantics
Standalone probe compiled with `aarch64-none-elf-gcc -O2`, run natively:
```text
sizeof(orig) = 40 __alignof__ = 8
sizeof(fixed) = 48 __alignof__ = 16
g_orig base % 16 = 0
g_fixed base % 16 = 0
g_orig[1] addr % 16 = 8
g_fixed[1] addr % 16 = 0
```
Note that the array *base* happened to be aligned in this TU while the
elements
were not, which is why the per-element table above is the meaningful
measurement.
### 2. Build
Note: `apache/master` at the time of writing does not compile
`qemu-armv8a:nsh`
because `fs/vfs/fs_sync.c:124` calls `foreach_inode()` with two arguments
while
`fs/inode/inode.h` declares three (from master commit `72ba9b9d95f`,
unrelated to
this change). All builds below were made after applying this throwaway
one-liner
to *both* trees so that the comparison is controlled:
```diff
- foreach_inode(mountpt_syncfs, NULL);
+ foreach_inode(mountpt_syncfs, NULL, false);
```
That edit is **not** part of this PR.
```text
$ export PATH=<aarch64-none-elf>/bin:$PATH
$ tools/configure.sh -a ../apps qemu-armv8a:nsh
$ make -j$(nproc)
...
LD: nuttx
Memory region Used Size Region Size %age Used
CP: nuttx.hex
CP: nuttx.bin
```
### 3. Runtime
`qemu-system-aarch64` with the board's own `config.yaml` arguments:
```text
$ qemu-system-aarch64 -cpu cortex-a53 -nographic \
-machine virt,virtualization=on,gic-version=3 \
-net none -chardev stdio,id=con,mux=on \
-serial chardev:con -monitor none -kernel nuttx
```
Output with this patch:
```text
- Ready to Boot Primary CPU
- Boot from EL2
- Boot from EL1
- Boot to C runtime for OS Initialize
nsh>
```
The `apache/master` build on the same configuration produces the same boot
output, i.e. the extra padding is behaviour-preserving.
### 4. checkpatch
```text
$ tools/checkpatch.sh -c -u -m -g apache/master..HEAD
Used config files:
1: .codespellrc
✔️ All checks pass.
```
## Honest note on the trigger
The commit message motivates the change by saying the compiler "may vectorize
loops in `setup_page_tables()` using 128-bit NEON loads (e.g. `ldur q0`)".
While
verifying, **I was not able to reproduce that vectorization** for the loops
that
read `g_mmu_regions`:
| optimization | `q` registers in `arm64_mmu_init` (contains the inlined
`setup_page_tables`) |
| --- | --- |
| `-Os` (NuttX default here) | 0 |
| `-O2` | 0 |
| `-O3` | 0 |
The `-O3` build *does* contain NEON (`ldr q3, ...` / `stp q2, q0, ...`), but
the
relocations show it is a vectorized constant fill of `.bss.xlat_tables` from
`.rodata.cst16` inside `init_xlat_tables()`, not a load from `g_mmu_regions`.
`-Os` also disables `-ftree-slp-vectorize` and `-ftree-vectorize`, so the SLP
path that would merge the field loads is off by default:
```text
$ aarch64-none-elf-gcc -Q --help=optimizers -Os | grep -E
'slp|tree-vectorize'
-ftree-slp-vectorize [disabled]
-ftree-vectorize [disabled]
```
So the alignment fix is defensive rather than a reproduction of an observed
crash on this toolchain/configuration. The failure mode it guards against is
real for any build that does emit a 128-bit load of a misaligned element,
because indices `0, 2, 4` sit at `%16 == 8` before the patch (see the table
above), but I have not observed such a load in the configurations I built.
This
should be stated plainly rather than presenting the commit message as
measured
fact.
## Change size
```text
arch/arm64/src/common/arm64_mmu.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
```
The cost is 8 bytes of padding per region descriptor. `g_mmu_regions` grows
from
240 B to 288 B on `qemu-armv8a`, which is a single array in `.rodata` and
does
not affect any other structure layout — `struct arm_mmu_config` only stores a
pointer to the array.
## Commit
```text
0d36afdd0ee arch/arm64: add 16-byte alignment to struct arm_mmu_region
```
--
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]