From 66ef2bd689a9e273697d2143e082c3288ae81481 Mon Sep 17 00:00:00 2001 From: robot00f Date: Thu, 26 Nov 2026 00:56:49 -0300 Subject: 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) --- runtime/src/pe_loader.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'runtime/src/pe_loader.cpp') diff --git a/runtime/src/pe_loader.cpp b/runtime/src/pe_loader.cpp index fc73f37f5..f87531063 100644 --- a/runtime/src/pe_loader.cpp +++ b/runtime/src/pe_loader.cpp @@ -19,6 +19,7 @@ #include "../shims/shims.h" #include "../shims/startup_trace.h" #include "base_reservation.h" +#include "bmi_emulation.h" #include "clzero_patch.h" #include "cpu_emulation.h" @@ -2215,6 +2216,32 @@ void Loader::protect_sections(Module& m) { "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", m.name.c_str(), patched.sites, static_cast(patched.stubs), patched.stub_bytes); } + static const auto bmi_host = rt::bmi_emulation::detect_host(); + if ((!bmi_host.bmi1 || !bmi_host.lzcnt) && rt::bmi_emulation::lzcnt_patch_enabled()) { + std::vector> exec_ranges; + const IMAGE_SECTION_HEADER* cs = IMAGE_FIRST_SECTION(nt); + for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++cs) { + if (!(cs->Characteristics & IMAGE_SCN_MEM_EXECUTE) || cs->VirtualAddress >= m.size) continue; + const uint64_t size = std::min(std::min(cs->Misc.VirtualSize, cs->SizeOfRawData), + m.size - cs->VirtualAddress); + if (size) exec_ranges.push_back({cs->VirtualAddress, static_cast(size)}); + } + const auto exc = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION); + const RUNTIME_FUNCTION* pdata = nullptr; + uint32_t pdata_count = 0; + if (exc.VirtualAddress && exc.Size >= sizeof(RUNTIME_FUNCTION) && exc.VirtualAddress < m.size) { + const uint64_t max_bytes = std::min(exc.Size, m.size - exc.VirtualAddress); + pdata_count = static_cast(max_bytes / sizeof(RUNTIME_FUNCTION)); + pdata = at(m.base, exc.VirtualAddress); + } + const auto patched = rt::bmi_emulation::patch_image( + m.base, m.size, exec_ranges, pdata, pdata_count, !bmi_host.bmi1, !bmi_host.lzcnt); + if (patched.tzcnt_sites || patched.lzcnt_sites) + std::fprintf(stderr, + "[rt] loader: %s: patched %zu TZCNT and %zu LZCNT sites (%zu stubs at %p, %zu #UD)\n", + m.name.c_str(), patched.tzcnt_sites, patched.lzcnt_sites, patched.stub_sites, + static_cast(patched.stubs), patched.ud2_sites); + } VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); auto* sec = IMAGE_FIRST_SECTION(nt); for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { -- cgit v1.2.3