mirror of
https://github.com/ubiquitarianism/leonida.git
synced 2026-11-26 23:05:53 +01:00
[PR #421] [MERGED] cpu_emulation: run a whole CLZERO zeroing loop in one emulated step (RT-CLZERO-LOOP) #471
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#471
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/421
Author: @oscarlosu-lab
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @QemmHD
Base:
main← Head:pr/clzero-loop-fastpath📝 Commits (2)
361e845cpu_emulation: run a whole CLZERO zeroing loop in one emulated step3b8c155cpu_emulation: update CMP flags on partial CLZERO batches and post-prefix faults📊 Changes
2 files changed (+174 additions, -0 deletions)
View changed files
📝
runtime/src/cpu_emulation.h(+97 -0)📝
runtime/test/cpu_emulation_test.cpp(+77 -0)📄 Description
Claimed in the coordination issue as: RT-CLZERO-LOOP
Summary
Adds a fast-path in
rt::cpu_emulation::handler(try_clzero_loop) for the Zen 2 cache-line zeroing loop pattern:clzero; add rax, 0x40; cmp rax, reg64; jb -12(12 bytes).On CPUs without native
CLZERO(e.g. Intel), executing one#UDexception +VirtualQuery+NtContinueper 64-byte line turns large zeroing loops into a heavy kernel-mode bottleneck. Recognizing the exact 12-byte loop lets the handler validate page writability once per 4 KiB page and zero the writable span in a singlememset(capped at 64 MiB per trap so large spans make bounded progress without starving the handler), while preserving exact hardware fault semantics (Rax,Rip, write-faultEXCEPTION_RECORD) if an unwritable/guard page is encountered mid-loop, and updatingEFlags(CF,PF,AF,ZF,SF,OF) to matchcmp end, limiton loop exit.Changes
runtime/src/cpu_emulation.h: addgpr_read,try_clzero_loop, and call it fromhandler(savingfault_ripbefore modifyingRipfor the first-emulation log).runtime/test/cpu_emulation_test.cpp: add live tests coveringrdxlimit,r8(REX.R) limit, and non-64-byte-aligned limit (300 -> 320round-up) while verifying surrounding guard bytes stay untouched. (Fault mid-loop and >64 MiB batching paths are covered by inspection, not live tests.)Verification
CLZERO), NVIDIA GeForce RTX 3060 (driver 621.35), MSVC x64.cpu_emulation_test.exe:cpu_emulation: all checks passed(exit 0).python -I scripts/check_no_game_data.py runtime/src/cpu_emulation.h runtime/test/cpu_emulation_test.cpp: PASS (exit 0).CLZEROexception overhead on Intel i5-12400F during runtime startup.AI-assisted.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.