feat(libkernel): run threads for a lifted guest #15803

Closed
oberonlift wants to merge 27 commits from upstream-pr/15803 into main
oberonlift commented 2026-11-04 16:24:04 +00:00 (Migrated from github.com)

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


Merge after #15802. Until the PRs before it merge, this PR also shows their changes; oberonlift/AnyPS5#13 shows only its own.

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 7 of 8 PRs that let AnyPS5's libraries run a lifted program; the lifter itself follows them.

x86-64: behaviour unchanged. Thread objects stay on the host heap; the stack size arithmetic shared with lifted builds computes the same values.

What

  • Guest thread entries and exception handlers are called through GuestCpu, and a lifted thread entry, which the bridge already turned into a native thunk, is called directly. The x86 assembly for sigsetjmp, siglongjmp, getcontext and their kin is left out of lifted builds, where the runtime provides the jumps and context switching reports itself as not implemented.
  • A lifted guest thread runs on a guarded guest stack of the size it asked for, handed to the runtime through GuestCpuUseStack and reported by scePthreadAttrGet; the host stack is the size GuestCpuHostStackSize gives. The thread takes its guest stack and the console's x87 control word before it unblocks signals, so a signal raised at a new or suspended thread runs its handler on that guest stack.
  • In lifted builds thread objects come from the guest heap, so every thread handle is a guest address, as on the console. On an aarch64 host the host heap lies outside anything the guest can address.
  • DoMprotect reports the pages each protection change makes writable to GuestCpuMakingWritable_nid_no_patch. ps5lift loads from read-only segments without ordering, and its runtime stops the program when such pages overlap one. A MAP_FIXED mapping over such a segment is not reported yet.
  • A guest exception handler runs through GuestCpuCallInterrupted and sees the guest registers that GuestCpuInterruptedContext reports; the signal-delivery assembly is left out of lifted builds.
  • sceKernelWaitOnAddress reads a misaligned value with a plain copy followed by an acquire fence instead of x86's mov.
  • EnterGuestMainStack calls the guest entry on the stack the runtime gave the main thread, instead of switching to a main stack at the console's address.
  • The libkernel debt file lists what still differs in lifted builds, among them the MAP_FIXED gap, the host stack that the main and adopted threads report, and the zero fpstate, cs and ss an exception handler reads.

#5021 also changes some of the same files: Thread.cpp, Pthread.hpp, Exception.cpp, SignalSets.cpp, DirectMemory.cpp.

Tested

Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 158 of the 162 objects it reaches are byte-identical; Thread.cpp differs in scePthreadCreate's register allocation and a line-number constant, and Exception.cpp, SyncOnAddress.cpp and UserContext.cpp only in line-number constants.

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,285 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 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: #15802
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5157736177 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15803). Originally posted by [oberonlift](https://github.com/oberonlift) on 2026-11-04T16:24:04Z. --- **Merge after #15802.** Until the PRs before it merge, this PR also shows their changes; oberonlift/AnyPS5#13 shows only its own. **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 7 of 8 PRs that let AnyPS5's libraries run a lifted program; the lifter itself follows them. **x86-64:** behaviour unchanged. Thread objects stay on the host heap; the stack size arithmetic shared with lifted builds computes the same values. ### What - Guest thread entries and exception handlers are called through `GuestCpu`, and a lifted thread entry, which the bridge already turned into a native thunk, is called directly. The x86 assembly for `sigsetjmp`, `siglongjmp`, `getcontext` and their kin is left out of lifted builds, where the runtime provides the jumps and context switching reports itself as not implemented. - A lifted guest thread runs on a guarded guest stack of the size it asked for, handed to the runtime through `GuestCpuUseStack` and reported by `scePthreadAttrGet`; the host stack is the size `GuestCpuHostStackSize` gives. The thread takes its guest stack and the console's x87 control word before it unblocks signals, so a signal raised at a new or suspended thread runs its handler on that guest stack. - In lifted builds thread objects come from the guest heap, so every thread handle is a guest address, as on the console. On an aarch64 host the host heap lies outside anything the guest can address. - `DoMprotect` reports the pages each protection change makes writable to `GuestCpuMakingWritable_nid_no_patch`. ps5lift loads from read-only segments without ordering, and its runtime stops the program when such pages overlap one. A `MAP_FIXED` mapping over such a segment is not reported yet. - A guest exception handler runs through `GuestCpuCallInterrupted` and sees the guest registers that `GuestCpuInterruptedContext` reports; the signal-delivery assembly is left out of lifted builds. - `sceKernelWaitOnAddress` reads a misaligned value with a plain copy followed by an acquire fence instead of x86's `mov`. - `EnterGuestMainStack` calls the guest entry on the stack the runtime gave the main thread, instead of switching to a main stack at the console's address. - The libkernel debt file lists what still differs in lifted builds, among them the `MAP_FIXED` gap, the host stack that the main and adopted threads report, and the zero `fpstate`, `cs` and `ss` an exception handler reads. #5021 also changes some of the same files: `Thread.cpp`, `Pthread.hpp`, `Exception.cpp`, `SignalSets.cpp`, `DirectMemory.cpp`. ### Tested Compiled for x86-64 Linux before and after this PR (GCC 13, same flags): 158 of the 162 objects it reaches are byte-identical; `Thread.cpp` differs in `scePthreadCreate`'s register allocation and a line-number constant, and `Exception.cpp`, `SyncOnAddress.cpp` and `UserContext.cpp` only in line-number constants. 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,285 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 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: #15802 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)

Pull request closed

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!15803
No description provided.