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

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

📋 Pull Request Information

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

Base: main ← Head: feat/rt-cpu-bmi-lzcnt-movbe


📝 Commits (1)

  • 564facb runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts

📊 Changes

5 files changed (+1508 additions, -0 deletions)

View changed files

📝 runtime/CMakeLists.txt (+9 -0)
➕ runtime/src/bmi_emulation.h (+1148 -0)
📝 runtime/src/main.cpp (+8 -0)
📝 runtime/src/pe_loader.cpp (+27 -0)
➕ runtime/test/bmi_emulation_test.cpp (+316 -0)

📄 Description

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://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7181466437
AI-assisted: yes (Antigravity, Gemini 4 Carbon)


🔄 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/2190 **Author:** [@robot00f](https://github.com/robot00f) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/rt-cpu-bmi-lzcnt-movbe` --- ### 📝 Commits (1) - [`564facb`](https://github.com/ubiquitarianism/leonida/commit/564facb61258c30c7cf764940ce8d138a91dab75) runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts ### 📊 Changes **5 files changed** (+1508 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+9 -0) ➕ `runtime/src/bmi_emulation.h` (+1148 -0) 📝 `runtime/src/main.cpp` (+8 -0) 📝 `runtime/src/pe_loader.cpp` (+27 -0) ➕ `runtime/test/bmi_emulation_test.cpp` (+316 -0) </details> ### 📄 Description ## 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://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7181466437 AI-assisted: yes (Antigravity, Gemini 4 Carbon) --- <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#2194
No description provided.