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

Merged
legacy-code-archaeologist merged 1 commit from upstream-pr/789 into main 2026-11-23 03:45:47 +00:00
legacy-code-archaeologist commented 2026-11-23 03:28:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-23T03:28:25Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6551648475 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/789). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-23T03:28:25Z. --- ## 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)
upstream-sync closed this pull request 2026-11-24 06:44:48 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!783
No description provided.