refactor: route the libraries' fenv, TSC, cpuid and pause instructions through helpers #15797

Merged
oberonlift merged 5 commits from upstream-pr/15797 into main 2026-11-08 18:08:43 +00:00
oberonlift commented 2026-11-04 16:23:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oberonlift on 2026-11-04T16:23:47Z.


Merge first. The ps5lift PRs after it merge in this order: #15798, #15799, #15800, #15801, #15802, #15803, #15804, then #15805, #15806 and #15807.

ps5lift is a static recompiler for PS5 executables: it lifts a game's x86-64 code to LLVM IR and compiles it ahead of time to aarch64, which runs on Linux against AnyPS5's own libraries with no interpreter or JIT. It runs AnyPS5's PS5-style test programs and a homebrew media player to its home screen; commercial games are not playable yet. Branch ps5lift-preview merges all of its PRs (how to try it); native Apple Silicon support is coming along fast. It is a separate approach from the ARM64 compiler in #5021; #1835 has an introduction.

This is 1 of 8 PRs that let AnyPS5's libraries run a lifted program, and it needs none of the others; the lifter itself follows them.

x86-64: behaviour unchanged: every fenv function leaves the same x87 control word, status flags, saved environment and MXCSR as before. Some take a few more instructions:

  • fegetenv, feholdexcept and feupdateenv: feholdexcept, for example, masks the x87 exceptions with an explicit fldcw instead of relying on fnstenv's side effect.
  • _Feraise, feraiseexcept, fesetexceptflag and feclearexcept of some of the flags read the x87 status with an extra fnstsw.
  • fesetenv copies the environment to a stack temporary before its fldenv.

webrtc's CPU feature query compiles to the same object.

What

The CPU-specific instructions in the places below go through inline helpers in GuestCpu.hpp, which are the instructions the code used before. The header supports x86-64 and stops the build elsewhere with an #error; a later PR in this series adds its ANYPS5_GUEST_LIFTED half, whose functions ps5lift's runtime provides.

  • libc: the fenv functions read and write the x87 control, status and environment and MXCSR through GuestCpu. feholdexcept keeps the exceptions masked as the fnstenv it replaces did.
  • libkernel: sceKernelReadTsc and the TSC frequency measurement read the counter through GuestCpu::ReadTsc, which is __rdtsc on x86-64.
  • libwebrtc: the CPU feature query runs cpuid and xgetbv through GuestCpu::Cpuid and GuestCpu::Xgetbv, which issue the same instructions as GCC's <cpuid.h>.
  • AGC driver and libSceJobManager: spin loops pause through PreciseSpinPause, now defined on every platform and using the aarch64 yield hint, and WriteChanged falls back to its byte loop on targets other than x86-64.
  • Host-side timing: JobManager's worker spins and the TSC frequency measurement read the counter through GuestCpu::ReadTsc too, so in a lifted build they read the counter the guest reads.

#5021 also changes some of the same files: Time.cpp, WaitMemory.cpp, GuestMemory.cpp.

Tested

Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 170 of the 173 objects it reaches are byte-identical. In FloatingPoint.cpp and Math.cpp only the fenv functions differ, and Texture.cpp differs only in the generated name of a switch table.

Built and run on x86-64 Linux (Intel N100, GCC 15.2, Ubuntu 26.04, in a container) as the Linux CI job builds it, on main and on this branch:

  • The tests that reach the changed code pass on both, three runs each: the fenv tests (guest_floating_point, guest_update_env, guest_set_env, guest_raise_except), agc_driver_pm4 and its variants, which run WriteChanged, agc_handoff_spin, the guest_process_time TSC tests, guest_webrtc_cpu_features and the JobManager tests.
  • The whole suite on lavapipe: no test that passes on main fails on this branch. The 54 tests that fail on both need guest write watch or /dev/udmabuf, which the container lacked, or time out there.
  • The AGC driver's focused tests (agc_handoff_spin, agc_driver_pm4, and the finisher-label, pipelined-fill and same-queue-label-wait tests, with and without the draw pipeline) pass on the N100's GPU on both with VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ANYPS5_NO_SHADER_CACHE=1 and ANYPS5_REQUIRE_VULKAN=1. The layer loaded, and neither log reports a VUID- error. The partially-resident snapshot tests skip there without write watch.

On a DGX Spark (aarch64 Linux, NVIDIA GB10, LLVM 18), with all eight PRs and ps5lift on top: every library builds for a lifted program, 2,288 of AnyPS5's own tests pass in that build on lavapipe, and ps5lift's 69 tests pass, among them its libc, exceptions, kernel, fiber and module-loading fixtures and upstream's libc test programs. The same focused AGC tests, and the partially-resident snapshot tests, pass on the GB10 with the validation layer and report no VUID- errors.

The new GuestCpu helpers and libwebrtc's CpuFeatures.cpp compile for Windows with MinGW-w64 GCC (syntax only). The conventions check passes on this branch.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: Intel Graphics (ADL-N, N100), Mesa 26.0.8, Ubuntu 26.04; NVIDIA GB10, driver 580.178.04, Ubuntu 24.04.5 (DGX OS)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5157732234 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15797). Originally posted by [oberonlift](https://github.com/oberonlift) on 2026-11-04T16:23:47Z. --- **Merge first.** The ps5lift PRs after it merge in this order: #15798, #15799, #15800, #15801, #15802, #15803, #15804, then #15805, #15806 and #15807. **ps5lift** is a static recompiler for PS5 executables: it lifts a game's x86-64 code to LLVM IR and compiles it ahead of time to aarch64, which runs on Linux against AnyPS5's own libraries with no interpreter or JIT. It runs AnyPS5's PS5-style test programs and a homebrew media player to its home screen; commercial games are not playable yet. Branch [`ps5lift-preview`](https://github.com/oberonlift/AnyPS5/tree/ps5lift-preview) merges all of its PRs ([how to try it](https://github.com/oberonlift/AnyPS5/blob/ps5lift-preview/PS5LIFT_PREVIEW.md)); native Apple Silicon support is coming along fast. It is a separate approach from the ARM64 compiler in #5021; #1835 has an introduction. This is 1 of 8 PRs that let AnyPS5's libraries run a lifted program, and it needs none of the others; the lifter itself follows them. **x86-64:** behaviour unchanged: every fenv function leaves the same x87 control word, status flags, saved environment and MXCSR as before. Some take a few more instructions: - `fegetenv`, `feholdexcept` and `feupdateenv`: `feholdexcept`, for example, masks the x87 exceptions with an explicit `fldcw` instead of relying on `fnstenv`'s side effect. - `_Feraise`, `feraiseexcept`, `fesetexceptflag` and `feclearexcept` of some of the flags read the x87 status with an extra `fnstsw`. - `fesetenv` copies the environment to a stack temporary before its `fldenv`. webrtc's CPU feature query compiles to the same object. ### What The CPU-specific instructions in the places below go through inline helpers in `GuestCpu.hpp`, which are the instructions the code used before. The header supports x86-64 and stops the build elsewhere with an `#error`; a later PR in this series adds its `ANYPS5_GUEST_LIFTED` half, whose functions ps5lift's runtime provides. - **libc**: the fenv functions read and write the x87 control, status and environment and MXCSR through `GuestCpu`. `feholdexcept` keeps the exceptions masked as the `fnstenv` it replaces did. - **libkernel**: `sceKernelReadTsc` and the TSC frequency measurement read the counter through `GuestCpu::ReadTsc`, which is `__rdtsc` on x86-64. - **libwebrtc**: the CPU feature query runs `cpuid` and `xgetbv` through `GuestCpu::Cpuid` and `GuestCpu::Xgetbv`, which issue the same instructions as GCC's `<cpuid.h>`. - **AGC driver and libSceJobManager**: spin loops pause through `PreciseSpinPause`, now defined on every platform and using the aarch64 yield hint, and `WriteChanged` falls back to its byte loop on targets other than x86-64. - **Host-side timing**: JobManager's worker spins and the TSC frequency measurement read the counter through `GuestCpu::ReadTsc` too, so in a lifted build they read the counter the guest reads. #5021 also changes some of the same files: `Time.cpp`, `WaitMemory.cpp`, `GuestMemory.cpp`. ### Tested Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 170 of the 173 objects it reaches are byte-identical. In `FloatingPoint.cpp` and `Math.cpp` only the fenv functions differ, and `Texture.cpp` differs only in the generated name of a switch table. Built and run on x86-64 Linux (Intel N100, GCC 15.2, Ubuntu 26.04, in a container) as the Linux CI job builds it, on `main` and on this branch: - The tests that reach the changed code pass on both, three runs each: the fenv tests (`guest_floating_point`, `guest_update_env`, `guest_set_env`, `guest_raise_except`), `agc_driver_pm4` and its variants, which run `WriteChanged`, `agc_handoff_spin`, the `guest_process_time` TSC tests, `guest_webrtc_cpu_features` and the JobManager tests. - The whole suite on lavapipe: no test that passes on `main` fails on this branch. The 54 tests that fail on both need guest write watch or `/dev/udmabuf`, which the container lacked, or time out there. - The AGC driver's focused tests (`agc_handoff_spin`, `agc_driver_pm4`, and the finisher-label, pipelined-fill and same-queue-label-wait tests, with and without the draw pipeline) pass on the N100's GPU on both with `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation`, `ANYPS5_NO_SHADER_CACHE=1` and `ANYPS5_REQUIRE_VULKAN=1`. The layer loaded, and neither log reports a `VUID-` error. The partially-resident snapshot tests skip there without write watch. On a DGX Spark (aarch64 Linux, NVIDIA GB10, LLVM 18), with all eight PRs and ps5lift on top: every library builds for a lifted program, 2,288 of AnyPS5's own tests pass in that build on lavapipe, and ps5lift's 69 tests pass, among them its libc, exceptions, kernel, fiber and module-loading fixtures and upstream's libc test programs. The same focused AGC tests, and the partially-resident snapshot tests, pass on the GB10 with the validation layer and report no `VUID-` errors. The new `GuestCpu` helpers and libwebrtc's `CpuFeatures.cpp` compile for Windows with MinGW-w64 GCC (syntax only). The conventions check passes on this branch. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: Intel Graphics (ADL-N, N100), Mesa 26.0.8, Ubuntu 26.04; NVIDIA GB10, driver 580.178.04, Ubuntu 24.04.5 (DGX OS) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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
boykopovar/AnyPS5!15797
No description provided.