runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts #2190

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

@⁠robot00f on GitHub · 2026-11-26 04:01 UTC · original
robot00f:feat/rt-cpu-bmi-lzcnt-movbe → main · +1513 −0 · 6 files · 2 commits · open

Summary

Add runtime/src/bmi_emulation.h and wire it into gta6rt and Loader::protect_sections so pre-Haswell AVX1 + SSE4.2 hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game's integer Haswell/ABM instruction delta (docs/cpu_features.md, PR #2188):

  • #UD emulation (STATUS_ILLEGAL_INSTRUCTION):
    • BMI1: andn, bextr, blsi, blsmsk, blsr (32-bit and 64-bit VEX encodings, exact CF/ZF/SF/OF flag semantics).
    • BMI2: bzhi, mulx (64x64->128 via _umul128, preserving EFlags and high-half precedence when both destinations match), pdep, pext, rorx, sarx, shlx, shrx.
    • MOVBE: 16/32/64-bit big-endian loads (0F 38 F0 /r) and stores (0F 38 F1 /r), keeping CRC32 (F2 0F 38 F0/F1) and register r/m encodings untouched.
    • Memory operands copy through a non-inlined SEH-guarded __movsb helper (no_builtin), taking zero syscalls on valid pages while routing first-touch xmem pages and write-watched D3D12 shadow pages through the front of the VEH chain and propagating genuine STATUS_ACCESS_VIOLATION / STATUS_GUARD_PAGE_VIOLATION records without advancing RIP.
  • Load-time TZCNT / LZCNT patching (Loader::protect_sections):
    • Without CPUID.07H:EBX.BMI1 / CPUID.80000001H:ECX.LZCNT, legacy F3 0F BC (TZCNT) and F3 0F BD (LZCNT) execute silently as BSF and BSR without raising #UD.
    • Walks .pdata functions and 0xCC-delimited leaf routines with an x86-64 instruction-length decoder before executable sections are write-protected: >=5-byte 32/64-bit register sites jump (E9 rel32) to branchless BSF/BSR + CMOV + BT stubs within ±2 GiB, and 4-byte, 16-bit or memory sites become UD2 (0F 0B) backed by a side-table of original bytes for the #UD handler (LEONIDA_LZCNT_PATCH=0 disables patching).
  • Tests (runtime/test/bmi_emulation_test.cpp):
    • Decoder positive/negative cases, synthetic CONTEXT execution + read-only fault propagation, .pdata + leaf TZCNT/LZCNT patching (including an immediate mov eax, 0xBD0F48F3 byte-trap and poisoned CF=1 cmovb flag consumers), and live machine-code execution on hardware.

Validation

  • Built and executed ctest 3x (bmi_emulation, cpu_emulation, clzero_patch, run_tiny_game) on an Intel Core i7-2600 (Sandy Bridge, AVX1 + SSE4.2, BMI1=0 BMI2=0 LZCNT=0 MOVBE=0), exercising the live #UD and patched TZCNT/LZCNT paths on real hardware.
  • python -I -m unittest discover -s tests -p "test_ctest_registration.py" and test_text_write_line_endings.py (OK).
  • python -I scripts/check_no_game_data.py (clean).

Claim: #1608
AI-assisted: yes (Antigravity, Gemini 4 Carbon)

**@⁠robot00f** on GitHub · 2026-11-26 04:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2190) `robot00f:feat/rt-cpu-bmi-lzcnt-movbe` → `main` · +1513 −0 · 6 files · 2 commits · open ## Summary Add `runtime/src/bmi_emulation.h` and wire it into `gta6rt` and `Loader::protect_sections` so pre-Haswell `AVX1 + SSE4.2` hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game's integer Haswell/ABM instruction delta (`docs/cpu_features.md`, PR #2188): - **`#UD` emulation (`STATUS_ILLEGAL_INSTRUCTION`):** - **BMI1:** `andn`, `bextr`, `blsi`, `blsmsk`, `blsr` (32-bit and 64-bit VEX encodings, exact `CF`/`ZF`/`SF`/`OF` flag semantics). - **BMI2:** `bzhi`, `mulx` (64x64->128 via `_umul128`, preserving `EFlags` and high-half precedence when both destinations match), `pdep`, `pext`, `rorx`, `sarx`, `shlx`, `shrx`. - **MOVBE:** 16/32/64-bit big-endian loads (`0F 38 F0 /r`) and stores (`0F 38 F1 /r`), keeping `CRC32` (`F2 0F 38 F0/F1`) and register `r/m` encodings untouched. - Memory operands copy through a non-inlined SEH-guarded `__movsb` helper (`no_builtin`), taking zero syscalls on valid pages while routing first-touch `xmem` pages and write-watched D3D12 shadow pages through the front of the VEH chain and propagating genuine `STATUS_ACCESS_VIOLATION` / `STATUS_GUARD_PAGE_VIOLATION` records without advancing `RIP`. - **Load-time `TZCNT` / `LZCNT` patching (`Loader::protect_sections`):** - Without `CPUID.07H:EBX.BMI1` / `CPUID.80000001H:ECX.LZCNT`, legacy `F3 0F BC` (`TZCNT`) and `F3 0F BD` (`LZCNT`) execute silently as `BSF` and `BSR` without raising `#UD`. - Walks `.pdata` functions and `0xCC`-delimited leaf routines with an x86-64 instruction-length decoder before executable sections are write-protected: `>=5`-byte 32/64-bit register sites jump (`E9 rel32`) to branchless `BSF`/`BSR` + `CMOV` + `BT` stubs within `±2 GiB`, and 4-byte, 16-bit or memory sites become `UD2` (`0F 0B`) backed by a side-table of original bytes for the `#UD` handler (`LEONIDA_LZCNT_PATCH=0` disables patching). - **Tests (`runtime/test/bmi_emulation_test.cpp`):** - Decoder positive/negative cases, synthetic `CONTEXT` execution + read-only fault propagation, `.pdata` + leaf `TZCNT`/`LZCNT` patching (including an immediate `mov eax, 0xBD0F48F3` byte-trap and poisoned `CF=1` `cmovb` flag consumers), and live machine-code execution on hardware. ## Validation - Built and executed `ctest` 3x (`bmi_emulation`, `cpu_emulation`, `clzero_patch`, `run_tiny_game`) on an **Intel Core i7-2600** (Sandy Bridge, `AVX1 + SSE4.2`, `BMI1=0 BMI2=0 LZCNT=0 MOVBE=0`), exercising the live `#UD` and patched `TZCNT`/`LZCNT` paths on real hardware. - `python -I -m unittest discover -s tests -p "test_ctest_registration.py"` and `test_text_write_line_endings.py` (OK). - `python -I scripts/check_no_game_data.py` (clean). Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 AI-assisted: yes (Antigravity, Gemini 4 Carbon)
Add runtime/src/bmi_emulation.h and wire it into gta6rt and Loader::protect_sections
so pre-Haswell AVX1 + SSE4.2 CPUs (Intel Sandy Bridge and Ivy Bridge) can execute
the game's integer Haswell/ABM instruction delta:

- Decode and emulate VEX-encoded BMI1 (andn, bextr, blsi, blsmsk, blsr), BMI2
  (bzhi, mulx, pdep, pext, rorx, sarx, shlx, shrx), and legacy 16/32/64-bit MOVBE
  on STATUS_ILLEGAL_INSTRUCTION (#UD), using SEH-guarded non-inlined __movsb copies
  so valid pages take zero syscalls while xmem first-touch, write-watched shadow
  pages, read-only faults and unmapped faults behave like native accesses.
- Patch non-faulting legacy TZCNT (F3 0F BC) and LZCNT (F3 0F BD) before executable
  sections are protected by walking .pdata functions and 0xCC-delimited leaf
  routines: >=5-byte 32/64-bit register sites jump to branchless BSF/BSR + CMOV +
  BT stubs within 2 GiB, and 4-byte, 16-bit or memory sites become UD2 (0F 0B)
  backed by a side-table for the #UD handler (configurable via LEONIDA_LZCNT_PATCH).
- Add runtime/test/bmi_emulation_test.cpp covering decoding, synthetic CONTEXT
  application, immediate byte-trap rejection, stub/UD2 flag consumers, and live
  execution on hardware.

Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7181466437
AI-assisted: yes (Antigravity, Gemini 4 Carbon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:21:49 +00:00
This pull request has changes conflicting with the target branch.
  • runtime/src/main.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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