[PR #421] [MERGED] cpu_emulation: run a whole CLZERO zeroing loop in one emulated step (RT-CLZERO-LOOP) #471

Closed
opened 2026-11-26 20:17:59 +01:00 by 4ld0 · 0 comments
Owner

📋 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)

  • 361e845 cpu_emulation: run a whole CLZERO zeroing loop in one emulated step
  • 3b8c155 cpu_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 #UD exception + VirtualQuery + NtContinue per 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 single memset (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-fault EXCEPTION_RECORD) if an unwritable/guard page is encountered mid-loop, and updating EFlags (CF, PF, AF, ZF, SF, OF) to match cmp end, limit on loop exit.

Changes

  • runtime/src/cpu_emulation.h: add gpr_read, try_clzero_loop, and call it from handler (saving fault_rip before modifying Rip for the first-emulation log).
  • runtime/test/cpu_emulation_test.cpp: add live tests covering rdx limit, r8 (REX.R) limit, and non-64-byte-aligned limit (300 -> 320 round-up) while verifying surrounding guard bytes stay untouched. (Fault mid-loop and >64 MiB batching paths are covered by inspection, not live tests.)

Verification

  • Hardware/OS: Windows 11, Intel Core i5-12400F (no 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).
  • Observed ~100x reduction in CLZERO exception 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.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/421 **Author:** [@oscarlosu-lab](https://github.com/oscarlosu-lab) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@QemmHD](https://github.com/QemmHD) **Base:** `main` ← **Head:** `pr/clzero-loop-fastpath` --- ### 📝 Commits (2) - [`361e845`](https://github.com/ubiquitarianism/leonida/commit/361e8455a20aa6606fb542d86e39b552da2e60e6) cpu_emulation: run a whole CLZERO zeroing loop in one emulated step - [`3b8c155`](https://github.com/ubiquitarianism/leonida/commit/3b8c1555d8d375762e907927417bbaca96accbbf) cpu_emulation: update CMP flags on partial CLZERO batches and post-prefix faults ### 📊 Changes **2 files changed** (+174 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/cpu_emulation.h` (+97 -0) 📝 `runtime/test/cpu_emulation_test.cpp` (+77 -0) </details> ### 📄 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 `#UD` exception + `VirtualQuery` + `NtContinue` per 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 single `memset` (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-fault `EXCEPTION_RECORD`) if an unwritable/guard page is encountered mid-loop, and updating `EFlags` (`CF`, `PF`, `AF`, `ZF`, `SF`, `OF`) to match `cmp end, limit` on loop exit. ### Changes - `runtime/src/cpu_emulation.h`: add `gpr_read`, `try_clzero_loop`, and call it from `handler` (saving `fault_rip` before modifying `Rip` for the first-emulation log). - `runtime/test/cpu_emulation_test.cpp`: add live tests covering `rdx` limit, `r8` (`REX.R`) limit, and non-64-byte-aligned limit (`300 -> 320` round-up) while verifying surrounding guard bytes stay untouched. (Fault mid-loop and >64 MiB batching paths are covered by inspection, not live tests.) ### Verification - Hardware/OS: Windows 11, Intel Core i5-12400F (no `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). - Observed ~100x reduction in `CLZERO` exception overhead on Intel i5-12400F during runtime startup. AI-assisted. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:17:59 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#471
No description provided.