cpu: lower RDTSC and RDTSCP in the shared lowering #789

Manually merged
github merged 1 commit from gh-pr/789/head into gh-pr/789/base 2026-11-26 22:18:51 +00:00
Owner

@⁠legacy-code-archaeologist on GitHub · 2026-11-23 03:28 UTC · original
legacy-code-archaeologist:cpu/lower-rdtsc → main · +484 −0 · 11 files · 1 commits · merged 2026-11-23 03:45 UTC

Change

Lowers RDTSC and RDTSCP in tools/ffx_version_query.py as the explicit host TSC profile from #739. The reads supply host ticks and, for RDTSCP, the host IA32_TSC_AUX. There is no guarantee of a guest TSC frequency or of a virtual-CPU number.

  • EAX gets the low half and EDX the high half of the counter. RAX and RDX are zero-extended, and ZF/CF stay unchanged.
  • RDTSCP also writes TSC_AUX to ECX and zero-extends RCX.
  • The lowering calls __rdtsc() and __rdtscp(). It adds no fence to the generated code.
  • The result is stored in a volatile local, so a read stays in the code even when the guest overwrites its result. Without volatile, MSVC /O2 removes such an RDTSC. The plain control below shows this.

Tests

  • cpu_aot_rdtsc (rdtsc.asm) runs on every host. It covers one RDTSC, two RDTSC in a row, and one RDTSC whose result is overwritten.
  • cpu_aot_rdtscp (rdtscp.asm) covers one RDTSCP and one RDTSCP whose result is overwritten. It exits 77 on a host without RDTSCP (CPUID 80000001h EDX bit 27). It also exits 77, after its other checks, when TSC_AUX is 0 on every processor of the affinity mask. RDTSCP has its own source file, so its coverage follows only this test.
  • Native harness: rdtsc_harness.asm is never translated. It supplies CPUID and the bracket reads, each serialized by LFENCE before and after. It has no coverage header and is listed as a native harness in tools/form_coverage.py.

Both checks pin the thread to one logical processor and confirm through GetCurrentProcessorNumber that it runs there before reading. Over 2,000 rounds they require:

  • The single reads lie between two serialized native reads.
  • The upper halves of the results are zero.
  • ZF/CF keep their preset (four presets from add r8, r8).
  • The second of two RDTSC is not earlier than the first and advances in at least one round.
  • RDTSCP returns the pinned processor's TSC_AUX.
  • The reads with overwritten results return the overwritten value.

Controls: each mutated C++ control must break a property:

  • RDTSC: constant (zero counter), swap, noext, stale (each read keeps its first value), flags.
  • RDTSCP: swap, noext, noaux, pflags.

/O2 builds: the translation is also built with cl /O2. A walk that follows every branch from each export must find every RDTSC and RDTSCP of the source, and the /O2 build must keep the properties. The plain control removes volatile and is built only at /O2. There the walk must report the overwritten RDTSC missing.

Verification

At 749131f on Windows 11 Pro (build 26100), Intel Core i7-6820HQ (RDTSCP present, TSC_AUX 0x7 on the pinned processor 7), MSVC from VS 2026 BuildTools, Python 3.12:

  • ctest --preset dev --no-tests=error -R '^cpu_aot_rdtscp?$' -V: both pass. Every control reports its broken property, for example control noaux: rdtscp_op ECX 0x0, TSC_AUX of the pinned processor 0x7. The plain /O2 build reports rdtsc_unused: ().
  • ctest --preset dev --no-tests=error -R '^cpu_' -j 4: 137 of 137 pass. The capability skips are cpu_aot_fences_hints_clwb, cpu_aot_crypto_sha and cpu_shim_chain_hardware, as on main for this host.
  • A simulated host without RDTSCP (has_rdtscp forced to 0, RDTSCP exports raising): cpu_aot_rdtsc passes, cpu_aot_rdtscp exits 77.
  • tools/form_coverage.py measure() with cpu_aot_rdtscp skipped places rdtscp in blocked_by_skip, and rdtsc stays covered.
  • tests/docs_links_check.py, tests/bidi_controls_check.py and tests/form_coverage_check.py pass.

Limits

  • Not run on AMD, on a host without RDTSCP (only simulated), or under a hypervisor that virtualizes TSC_AUX.
  • noext can only fail once the counter exceeds 2^32, that is, a few seconds after reset.
  • Whether a guest-frequency profile belongs in the runtime stays open in #739.

Refs #739

  • I did not include game files, dumps, distribution links or secrets.

AI-assisted: written with Claude Code, three precommit review rounds by fresh agents; checked and submitted by the account owner.

🤖 Generated with Claude Code

**@⁠legacy-code-archaeologist** on GitHub · 2026-11-23 03:28 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/789) `legacy-code-archaeologist:cpu/lower-rdtsc` → `main` · +484 −0 · 11 files · 1 commits · merged 2026-11-23 03:45 UTC ## Change Lowers RDTSC and RDTSCP in `tools/ffx_version_query.py` as the explicit host TSC profile from #739. The reads supply host ticks and, for RDTSCP, the host IA32_TSC_AUX. There is no guarantee of a guest TSC frequency or of a virtual-CPU number. - EAX gets the low half and EDX the high half of the counter. RAX and RDX are zero-extended, and ZF/CF stay unchanged. - RDTSCP also writes TSC_AUX to ECX and zero-extends RCX. - The lowering calls `__rdtsc()` and `__rdtscp()`. It adds no fence to the generated code. - The result is stored in a `volatile` local, so a read stays in the code even when the guest overwrites its result. Without `volatile`, MSVC `/O2` removes such an RDTSC. The `plain` control below shows this. ## Tests - **`cpu_aot_rdtsc`** ([`rdtsc.asm`](tests/cpu/rdtsc.asm)) runs on every host. It covers one RDTSC, two RDTSC in a row, and one RDTSC whose result is overwritten. - **`cpu_aot_rdtscp`** ([`rdtscp.asm`](tests/cpu/rdtscp.asm)) covers one RDTSCP and one RDTSCP whose result is overwritten. It exits 77 on a host without RDTSCP (CPUID 80000001h EDX bit 27). It also exits 77, after its other checks, when TSC_AUX is 0 on every processor of the affinity mask. RDTSCP has its own source file, so its coverage follows only this test. - **Native harness:** [`rdtsc_harness.asm`](tests/cpu/rdtsc_harness.asm) is never translated. It supplies CPUID and the bracket reads, each serialized by LFENCE before and after. It has no coverage header and is listed as a native harness in `tools/form_coverage.py`. Both checks pin the thread to one logical processor and confirm through `GetCurrentProcessorNumber` that it runs there before reading. Over 2,000 rounds they require: - The single reads lie between two serialized native reads. - The upper halves of the results are zero. - ZF/CF keep their preset (four presets from `add r8, r8`). - The second of two RDTSC is not earlier than the first and advances in at least one round. - RDTSCP returns the pinned processor's TSC_AUX. - The reads with overwritten results return the overwritten value. **Controls:** each mutated C++ control must break a property: - RDTSC: `constant` (zero counter), `swap`, `noext`, `stale` (each read keeps its first value), `flags`. - RDTSCP: `swap`, `noext`, `noaux`, `pflags`. **`/O2` builds:** the translation is also built with `cl /O2`. A walk that follows every branch from each export must find every RDTSC and RDTSCP of the source, and the `/O2` build must keep the properties. The `plain` control removes `volatile` and is built only at `/O2`. There the walk must report the overwritten RDTSC missing. ## Verification At `749131f` on Windows 11 Pro (build 26100), Intel Core i7-6820HQ (RDTSCP present, TSC_AUX 0x7 on the pinned processor 7), MSVC from VS 2026 BuildTools, Python 3.12: - `ctest --preset dev --no-tests=error -R '^cpu_aot_rdtscp?$' -V`: both pass. Every control reports its broken property, for example `control noaux: rdtscp_op ECX 0x0, TSC_AUX of the pinned processor 0x7`. The `plain` `/O2` build reports `rdtsc_unused: ()`. - `ctest --preset dev --no-tests=error -R '^cpu_' -j 4`: 137 of 137 pass. The capability skips are `cpu_aot_fences_hints_clwb`, `cpu_aot_crypto_sha` and `cpu_shim_chain_hardware`, as on `main` for this host. - A simulated host without RDTSCP (`has_rdtscp` forced to 0, RDTSCP exports raising): `cpu_aot_rdtsc` passes, `cpu_aot_rdtscp` exits 77. - `tools/form_coverage.py` `measure()` with `cpu_aot_rdtscp` skipped places `rdtscp` in `blocked_by_skip`, and `rdtsc` stays covered. - `tests/docs_links_check.py`, `tests/bidi_controls_check.py` and `tests/form_coverage_check.py` pass. ## Limits - Not run on AMD, on a host without RDTSCP (only simulated), or under a hypervisor that virtualizes TSC_AUX. - `noext` can only fail once the counter exceeds 2^32, that is, a few seconds after reset. - Whether a guest-frequency profile belongs in the runtime stays open in #739. Refs #739 - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted: written with Claude Code, three precommit review rounds by fresh agents; checked and submitted by the account owner. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Lower RDTSC and RDTSCP as an explicit host TSC profile, as agreed in #739:
they supply host ticks and the host TSC_AUX, with no guest frequency or
virtual-CPU guarantee. EAX/EDX get the low and high halves, RAX and RDX are
zero-extended, RDTSCP also writes TSC_AUX to RCX, and ZF/CF stay unchanged.
The lowering calls __rdtsc() and __rdtscp() without adding fences and keeps
the result in a volatile local, so a read whose result the guest overwrites
stays in the code. Without volatile, MSVC /O2 removes such an RDTSC.

The checks compare properties, not bytes. cpu_aot_rdtsc runs on every host;
cpu_aot_rdtscp has its own fixture and exits 77 without RDTSCP, so coverage
of RDTSCP follows that test only. A native harness supplies CPUID and
LFENCE-serialized bracket reads. Both checks pin the thread and confirm the
processor before comparing. Mutated controls must each break a property,
and /O2 builds are walked to confirm every read survives; the plain control,
without volatile, must lose the overwritten RDTSC.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 22:18:48 +00:00
github manually merged commit 749131f365 into gh-pr/789/base 2026-11-26 22:18:51 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.