diff options
| author | robot00f <robot00f@users.noreply.github.com> | 2026-11-26 00:56:49 -0300 |
|---|---|---|
| committer | robot00f <robot00f@users.noreply.github.com> | 2026-11-27 18:49:03 -0300 |
| commit | 66ef2bd689a9e273697d2143e082c3288ae81481 (patch) | |
| tree | 7640b72de1d0fa1254716ad18a13c82771b9366b /runtime/src/main.cpp | |
| parent | 39f52f39c435e04273bc822f258f2c4bb1b7fec0 (diff) | |
runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts
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)
Diffstat (limited to 'runtime/src/main.cpp')
| -rw-r--r-- | runtime/src/main.cpp | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index c8545f338..a797fc19c 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp | |||
| @@ -178,6 +178,7 @@ void start_title_peek(std::uint8_t* base) { | |||
| 178 | 178 | ||
| 179 | #include "base_reservation.h" | 179 | #include "base_reservation.h" |
| 180 | #include "compile_gate_policy.h" | 180 | #include "compile_gate_policy.h" |
| 181 | #include "bmi_emulation.h" | ||
| 181 | #include "code_hooks.h" | 182 | #include "code_hooks.h" |
| 182 | #include "cpu_emulation.h" | 183 | #include "cpu_emulation.h" |
| 183 | #include "host_cpu.h" | 184 | #include "host_cpu.h" |
| @@ -559,6 +560,13 @@ int wmain(int argc, wchar_t** argv) { | |||
| 559 | if (!rt::cpu_emulation::install()) | 560 | if (!rt::cpu_emulation::install()) |
| 560 | std::fprintf(stderr, "[rt] cpu: AddVectoredExceptionHandler failed (error %lu); instructions this CPU lacks " | 561 | std::fprintf(stderr, "[rt] cpu: AddVectoredExceptionHandler failed (error %lu); instructions this CPU lacks " |
| 561 | "will stop the game\n", GetLastError()); | 562 | "will stop the game\n", GetLastError()); |
| 563 | if (const auto bmi = rt::bmi_emulation::detect_host(); !bmi.bmi1 || !bmi.bmi2 || !bmi.lzcnt || !bmi.movbe) | ||
| 564 | std::fprintf(stderr, "[rt] cpu: this CPU lacks%s%s%s%s; the game's uses are emulated (docs/cpu_features.md)\n", | ||
| 565 | bmi.bmi1 ? "" : " BMI1", bmi.bmi2 ? "" : " BMI2", bmi.lzcnt ? "" : " LZCNT", | ||
| 566 | bmi.movbe ? "" : " MOVBE"); | ||
| 567 | if (!rt::bmi_emulation::install()) | ||
| 568 | std::fprintf(stderr, "[rt] cpu: AddVectoredExceptionHandler (BMI) failed (error %lu); instructions this CPU " | ||
| 569 | "lacks will stop the game\n", GetLastError()); | ||
| 562 | 570 | ||
| 563 | try { | 571 | try { |
| 564 | rt::Loader loader(game_dir); | 572 | rt::Loader loader(game_dir); |