upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/pe_loader.cpp
diff options
context:
space:
mode:
authorrobot00f <robot00f@users.noreply.github.com>2026-11-26 00:56:49 -0300
committerrobot00f <robot00f@users.noreply.github.com>2026-11-27 18:49:03 -0300
commit66ef2bd689a9e273697d2143e082c3288ae81481 (patch)
tree7640b72de1d0fa1254716ad18a13c82771b9366b /runtime/src/pe_loader.cpp
parent39f52f39c435e04273bc822f258f2c4bb1b7fec0 (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/pe_loader.cpp')
-rw-r--r--runtime/src/pe_loader.cpp27
1 files changed, 27 insertions, 0 deletions
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 @@
19#include "../shims/shims.h" 19#include "../shims/shims.h"
20#include "../shims/startup_trace.h" 20#include "../shims/startup_trace.h"
21#include "base_reservation.h" 21#include "base_reservation.h"
22#include "bmi_emulation.h"
22#include "clzero_patch.h" 23#include "clzero_patch.h"
23#include "cpu_emulation.h" 24#include "cpu_emulation.h"
24 25
@@ -2215,6 +2216,32 @@ void Loader::protect_sections(Module& m) {
2215 "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n", 2216 "[rt] loader: %s: %zu CLZERO loops run from stubs at %p (%zu bytes), not as a #UD per line\n",
2216 m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes); 2217 m.name.c_str(), patched.sites, static_cast<void*>(patched.stubs), patched.stub_bytes);
2217 } 2218 }
2219 static const auto bmi_host = rt::bmi_emulation::detect_host();
2220 if ((!bmi_host.bmi1 || !bmi_host.lzcnt) && rt::bmi_emulation::lzcnt_patch_enabled()) {
2221 std::vector<std::pair<uint32_t, uint32_t>> exec_ranges;
2222 const IMAGE_SECTION_HEADER* cs = IMAGE_FIRST_SECTION(nt);
2223 for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++cs) {
2224 if (!(cs->Characteristics & IMAGE_SCN_MEM_EXECUTE) || cs->VirtualAddress >= m.size) continue;
2225 const uint64_t size = std::min<uint64_t>(std::min(cs->Misc.VirtualSize, cs->SizeOfRawData),
2226 m.size - cs->VirtualAddress);
2227 if (size) exec_ranges.push_back({cs->VirtualAddress, static_cast<uint32_t>(size)});
2228 }
2229 const auto exc = directory(nt, IMAGE_DIRECTORY_ENTRY_EXCEPTION);
2230 const RUNTIME_FUNCTION* pdata = nullptr;
2231 uint32_t pdata_count = 0;
2232 if (exc.VirtualAddress && exc.Size >= sizeof(RUNTIME_FUNCTION) && exc.VirtualAddress < m.size) {
2233 const uint64_t max_bytes = std::min<uint64_t>(exc.Size, m.size - exc.VirtualAddress);
2234 pdata_count = static_cast<uint32_t>(max_bytes / sizeof(RUNTIME_FUNCTION));
2235 pdata = at<RUNTIME_FUNCTION>(m.base, exc.VirtualAddress);
2236 }
2237 const auto patched = rt::bmi_emulation::patch_image(
2238 m.base, m.size, exec_ranges, pdata, pdata_count, !bmi_host.bmi1, !bmi_host.lzcnt);
2239 if (patched.tzcnt_sites || patched.lzcnt_sites)
2240 std::fprintf(stderr,
2241 "[rt] loader: %s: patched %zu TZCNT and %zu LZCNT sites (%zu stubs at %p, %zu #UD)\n",
2242 m.name.c_str(), patched.tzcnt_sites, patched.lzcnt_sites, patched.stub_sites,
2243 static_cast<void*>(patched.stubs), patched.ud2_sites);
2244 }
2218 VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old); 2245 VirtualProtect(m.base, nt->OptionalHeader.SizeOfHeaders, PAGE_READONLY, &old);
2219 auto* sec = IMAGE_FIRST_SECTION(nt); 2246 auto* sec = IMAGE_FIRST_SECTION(nt);
2220 for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) { 2247 for (unsigned i = 0; i < nt->FileHeader.NumberOfSections; ++i, ++sec) {