x64_recomp: the QEMU harness maps guest RAM as Normal memory, so unaligned accesses work (X64-RECOMP-QEMU-UNALIGNED) #2143

Open
github-bot wants to merge 2 commits from github-bot/gh-2143 into main AGit
Owner

@⁠kevincommits on GitHub · 2026-11-25 23:05 UTC · original
kevincommits:x64-recomp-qemu-unaligned → main · +62 −0 · 2 files · 2 commits · open

Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (#1608)

What and why

Under qemu-system-aarch64, every unaligned guest memory access in the tools/x64_recomp.py test harness hangs until the 15 s timeout. This is on main, with QEMU 11.1.3:

mov rbx, 0x47100000
mov edi, [rbx+2]      ; any offset that is not a multiple of the access size
ret

-d int shows why:

Taking exception 4 [Data Abort] on CPU 0
...with ESR 0x25/0x96000021        (DFSC 0x21: alignment fault)
...with FAR 0x47100002
...to EL1 PC 0x200
Taking exception 1 [Undefined Instruction] on CPU 0   (repeats: there is no vector table)

build_test_kernel's boot stub never turns the MMU on. With the MMU off every data access is Device memory, and an unaligned access to Device memory is an alignment fault. x86 code reads and writes unaligned everywhere, so the guest needs Normal memory.

The fix is in the boot stub only: _emit_identity_mmu writes 1 GiB block entries for the image and the RW window into one level-1 table page at KERNEL_RW + 0x10000 (between the guest stack at +0x8000 and the tests' scratch at +0x100000), sets MAIR_EL1, TCR_EL1 (39-bit VA, 4 KiB granule) and TTBR0_EL1, then sets SCTLR_EL1 M, C and I with A clear. The map is an identity map, so no address the translator or the tests use changes. The Linux ELF path and the translator itself are untouched.

I found this while running #1213, whose test_std_overlapping_backward_rep_movsb ends in mov edi, [rbx+2] and times out for this reason.

Tested

Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, at 8564bb48b.

  • New ExecTests.test_unaligned_memory_access: 2-, 4- and 8-byte unaligned loads and stores, seven cases. With main's tools/x64_recomp.py all seven time out; with this change all seven return the expected value.
  • python -I tests/test_x64_recomp.py: 34/34 under QEMU (33/33 on main, so the existing exec tests are unaffected by the MMU being on). python -I tests/test_recomp_census.py: 11/11.
  • Every standalone tests/test_*.py under python -I: 511 run, 507 pass; the 4 that fail are the same 4 that fail on main.

Not tested: any QEMU older than 11.1.3 (I expect the MMU setup to be valid there too, but did not run it), qemu-system-aarch64 on Linux or Windows hosts, and real arm64 hardware. The table relies on QEMU zeroing RAM at reset.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no shader translator change)
**@⁠kevincommits** on GitHub · 2026-11-25 23:05 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2143) `kevincommits:x64-recomp-qemu-unaligned` → `main` · +62 −0 · 2 files · 2 commits · open Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why Under `qemu-system-aarch64`, every unaligned guest memory access in the `tools/x64_recomp.py` test harness hangs until the 15 s timeout. This is on `main`, with QEMU 11.1.3: ``` mov rbx, 0x47100000 mov edi, [rbx+2] ; any offset that is not a multiple of the access size ret ``` `-d int` shows why: ``` Taking exception 4 [Data Abort] on CPU 0 ...with ESR 0x25/0x96000021 (DFSC 0x21: alignment fault) ...with FAR 0x47100002 ...to EL1 PC 0x200 Taking exception 1 [Undefined Instruction] on CPU 0 (repeats: there is no vector table) ``` `build_test_kernel`'s boot stub never turns the MMU on. With the MMU off every data access is Device memory, and an unaligned access to Device memory is an alignment fault. x86 code reads and writes unaligned everywhere, so the guest needs Normal memory. The fix is in the boot stub only: `_emit_identity_mmu` writes 1 GiB block entries for the image and the RW window into one level-1 table page at `KERNEL_RW + 0x10000` (between the guest stack at `+0x8000` and the tests' scratch at `+0x100000`), sets `MAIR_EL1`, `TCR_EL1` (39-bit VA, 4 KiB granule) and `TTBR0_EL1`, then sets `SCTLR_EL1` M, C and I with A clear. The map is an identity map, so no address the translator or the tests use changes. The Linux ELF path and the translator itself are untouched. I found this while running #1213, whose `test_std_overlapping_backward_rep_movsb` ends in `mov edi, [rbx+2]` and times out for this reason. ### Tested Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, at `8564bb48b`. - New `ExecTests.test_unaligned_memory_access`: 2-, 4- and 8-byte unaligned loads and stores, seven cases. With `main`'s `tools/x64_recomp.py` all seven time out; with this change all seven return the expected value. - `python -I tests/test_x64_recomp.py`: 34/34 under QEMU (33/33 on `main`, so the existing exec tests are unaffected by the MMU being on). `python -I tests/test_recomp_census.py`: 11/11. - Every standalone `tests/test_*.py` under `python -I`: 511 run, 507 pass; the 4 that fail are the same 4 that fail on `main`. Not tested: any QEMU older than 11.1.3 (I expect the MMU setup to be valid there too, but did not run it), `qemu-system-aarch64` on Linux or Windows hosts, and real arm64 hardware. The table relies on QEMU zeroing RAM at reset. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a: no shader translator change)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:20:00 +00:00
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 03:49 UTC · original

A second symptom of the same harness bug, found while reviewing #1321, and it is more common than the one in the title: under run_kernel on main, a guest ret (or any other rt_dispatch) hangs whenever the pc table lands on an address that is 4 mod 8.

  • build_test_kernel places the pc table right after the translated code and the runtime, so its address depends on how many arm64 words the test happened to translate to. rt_dispatch reads it with 8-byte loads (ldr_post(0, LINK, 16)). With the MMU off those are Device-memory accesses, an 8-byte load at 4 mod 8 takes an alignment fault, and the run sits there until the timeout.

  • Measured on main at a73a57e13:

    guest code pc table at result
    test_call_and_ret from tests/test_x64_recomp.py 0x400801a0 (0 mod 8) R=7
    call helper; mov edi,1; ret; helper: mov eax,7; ret 0x4008019c (4 mod 8) timeout
  • So the existing call test passes on main only because its translation has the right length. Eight small call/ret variants I tried (sub rsp/add rsp around the call, push/pop around it, and so on) all time out on main and all pass on this branch, since with the MMU on the load is an ordinary Normal-memory access.

  • Pushed c6029b869: ExecTests.test_call_and_ret_at_any_code_length (4 subtests). With this branch's tool: tests/test_x64_recomp.py 35/35. The same test against main's tool: 4 of 4 subtests time out.

This is why exec tests with a guest call or ret imm16 in other open PRs fail under QEMU for some inputs and not others (for example ret 16 in #1321 hangs while ret 0x1000 passes; with this branch merged in, that file is 9/9). No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 03:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2143#issuecomment-7182058365) A second symptom of the same harness bug, found while reviewing #1321, and it is more common than the one in the title: **under `run_kernel` on `main`, a guest `ret` (or any other `rt_dispatch`) hangs whenever the pc table lands on an address that is 4 mod 8.** - `build_test_kernel` places the pc table right after the translated code and the runtime, so its address depends on how many arm64 words the test happened to translate to. `rt_dispatch` reads it with 8-byte loads (`ldr_post(0, LINK, 16)`). With the MMU off those are Device-memory accesses, an 8-byte load at 4 mod 8 takes an alignment fault, and the run sits there until the timeout. - Measured on `main` at `a73a57e13`: | guest code | pc table at | result | |---|---|---| | `test_call_and_ret` from `tests/test_x64_recomp.py` | `0x400801a0` (0 mod 8) | `R=7` | | `call helper; mov edi,1; ret; helper: mov eax,7; ret` | `0x4008019c` (4 mod 8) | timeout | - So the existing call test passes on `main` only because its translation has the right length. Eight small call/ret variants I tried (`sub rsp`/`add rsp` around the call, `push`/`pop` around it, and so on) all time out on `main` and all pass on this branch, since with the MMU on the load is an ordinary Normal-memory access. - Pushed `c6029b869`: `ExecTests.test_call_and_ret_at_any_code_length` (4 subtests). With this branch's tool: `tests/test_x64_recomp.py` 35/35. The same test against `main`'s tool: 4 of 4 subtests time out. This is why exec tests with a guest call or `ret imm16` in other open PRs fail under QEMU for some inputs and not others (for example `ret 16` in #1321 hangs while `ret 0x1000` passes; with this branch merged in, that file is 9/9). No game files used. AI-assisted.
github-bot force-pushed github-bot/gh-2143 from c6029b8699 to 2aeacc34f3 2026-11-27 02:41:25 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2143/head:github-bot/gh-2143'
git switch 'github-bot/gh-2143'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2143'
git switch 'github-bot/gh-2143'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2143'
git switch 'github-bot/gh-2143'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2143'
git switch 'main'
git merge --squash 'github-bot/gh-2143'
git switch 'main'
git merge --ff-only 'github-bot/gh-2143'
git switch 'main'
git merge 'github-bot/gh-2143'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.