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

Open
opened 2026-11-26 21:20:12 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2143
Author: @kevincommits
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: x64-recomp-qemu-unaligned


📝 Commits (2)

  • 8fe4123 fix: x64_recomp QEMU harness maps guest RAM as Normal memory so unaligned accesses work
  • c6029b8 test: x64_recomp guest call and ret under the QEMU harness at any code length

📊 Changes

2 files changed (+62 additions, -0 deletions)

View changed files

📝 tests/test_x64_recomp.py (+36 -0)
📝 tools/x64_recomp.py (+26 -0)

📄 Description

Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7177136024)

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2143 **Author:** [@kevincommits](https://github.com/kevincommits) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `x64-recomp-qemu-unaligned` --- ### 📝 Commits (2) - [`8fe4123`](https://github.com/ubiquitarianism/leonida/commit/8fe41233d066bcea49d120486529fea9944c6d28) fix: x64_recomp QEMU harness maps guest RAM as Normal memory so unaligned accesses work - [`c6029b8`](https://github.com/ubiquitarianism/leonida/commit/c6029b8699b58f96418e48b1c671ff2ecdc421b4) test: x64_recomp guest call and ret under the QEMU harness at any code length ### 📊 Changes **2 files changed** (+62 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_x64_recomp.py` (+36 -0) 📝 `tools/x64_recomp.py` (+26 -0) </details> ### 📄 Description Claimed in the coordination issue as: X64-RECOMP-QEMU-UNALIGNED (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7177136024) ### 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#2147
No description provided.