mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:10:53 +01:00
[PR #2190] runtime(cpu): emulate BMI1, BMI2, MOVBE and patch LZCNT/TZCNT on AVX1 hosts #2194
Labels
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
starred/leonida#2194
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 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)
564facbruntime(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.hand wire it intogta6rtandLoader::protect_sectionsso pre-HaswellAVX1 + SSE4.2hosts (Intel Sandy Bridge and Ivy Bridge) can execute the game's integer Haswell/ABM instruction delta (docs/cpu_features.md, PR #2188):#UDemulation (STATUS_ILLEGAL_INSTRUCTION):andn,bextr,blsi,blsmsk,blsr(32-bit and 64-bit VEX encodings, exactCF/ZF/SF/OFflag semantics).bzhi,mulx(64x64->128 via_umul128, preservingEFlagsand high-half precedence when both destinations match),pdep,pext,rorx,sarx,shlx,shrx.0F 38 F0 /r) and stores (0F 38 F1 /r), keepingCRC32(F2 0F 38 F0/F1) and registerr/mencodings untouched.__movsbhelper (no_builtin), taking zero syscalls on valid pages while routing first-touchxmempages and write-watched D3D12 shadow pages through the front of the VEH chain and propagating genuineSTATUS_ACCESS_VIOLATION/STATUS_GUARD_PAGE_VIOLATIONrecords without advancingRIP.TZCNT/LZCNTpatching (Loader::protect_sections):CPUID.07H:EBX.BMI1/CPUID.80000001H:ECX.LZCNT, legacyF3 0F BC(TZCNT) andF3 0F BD(LZCNT) execute silently asBSFandBSRwithout raising#UD..pdatafunctions and0xCC-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 branchlessBSF/BSR+CMOV+BTstubs within±2 GiB, and 4-byte, 16-bit or memory sites becomeUD2(0F 0B) backed by a side-table of original bytes for the#UDhandler (LEONIDA_LZCNT_PATCH=0disables patching).runtime/test/bmi_emulation_test.cpp):CONTEXTexecution + read-only fault propagation,.pdata+ leafTZCNT/LZCNTpatching (including an immediatemov eax, 0xBD0F48F3byte-trap and poisonedCF=1cmovbflag consumers), and live machine-code execution on hardware.Validation
ctest3x (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#UDand patchedTZCNT/LZCNTpaths on real hardware.python -I -m unittest discover -s tests -p "test_ctest_registration.py"andtest_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.