feat(libkernel_sys): identify the initial pthread #9160

Merged
yabanana merged 7 commits from upstream-pr/9160 into main 2026-10-24 23:57:54 +00:00
yabanana commented 2026-10-21 14:04:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by yabanana on 2026-10-21T14:04:49Z.


What

Implement pthread_main_np from the libkernel_sys discovery exports. Linux identifies the process's initial thread using gettid() == getpid(), including when a worker first loads libkernel. Other hosts retain the library-initialization-thread model and its explicit limitation in technical debt. Querying does not change errno; the runtime does not expose a pre-initialization -1 state.

The signature and initial-thread semantics follow FreeBSD 11.4 thr_main_np.c and the public PS5 SDK pthread_np.h. Console behavior is unmeasured.

Following the maintainer's landing-order request, the attribute implementations and tests are removed. The current #9156 covers all four formerly overlapping attributes, so this PR now supplies only the initial-thread query. It adds no affinityTail field or attribute-layout changes. The earlier implementation remains in published history; its clearing/tail behavior came from FreeBSD, not a console measurement. This remaining query can land independently of #9156.

Tested

24 October update: head 12ff96135fc8ba65a24b49ac15ebf2751ef03986 includes main c8d34aa8f4d8058d57169cc1e423ba6db00faaf0.

Built with GCC 13.3 / Ubuntu 24.04 targeting Linux x86-64. SHA256-verified copies of the unchanged build outputs ran in a full Linux guest kernel: Alpine 6.12.110-0-virt, QEMU system 8.2.2 TCG, cpu=max, 2 vCPUs, 1536 MiB. CPU execution is emulated; process_vm_* calls run in the guest kernel. No syscall shim or unchecked-copy fallback.

10/10 executions pass: four initial-thread query cases (ordinary and worker-first loading, each unpatched/NID-patched) plus six common kernel/filesystem regressions. The worker-first negative control against the previous libraries remains historical evidence. Attribute tests and implementations belong to #9156.

benjaminmathias's native Windows report passes the preceding head's ten Windows cases and twelve shared regressions three times each on WinLibs GCC 15.2.0 r7, Windows 11 Pro / Ryzen 7 5700X3D. It used main c9b2660d563 plus cd3e37795d, without #9156; selected-DLL checks completed and original-stub controls failed. This is evidence for that integration, not a native Windows execution of the current reduced head. The two remaining Windows main-query cases retain the same behavior/assertions and the current fixture cross-links locally with MinGW 13.2.

Whitespace and current-main conventions checks pass. Published ancestry and contributor authorship are retained; this head merges cleanly with the pinned main. Full upstream Linux/Windows CI requires a fresh maintainer dispatch. No console or title-unblock claim.

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: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4952903668 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9160). Originally posted by [yabanana](https://github.com/yabanana) on 2026-10-21T14:04:49Z. --- ### What Implement `pthread_main_np` from the libkernel_sys discovery exports. Linux identifies the process's initial thread using `gettid() == getpid()`, including when a worker first loads libkernel. Other hosts retain the library-initialization-thread model and its explicit limitation in technical debt. Querying does not change errno; the runtime does not expose a pre-initialization -1 state. The signature and initial-thread semantics follow [FreeBSD 11.4 thr_main_np.c](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/libthr/thread/thr_main_np.c) and the public PS5 SDK [pthread_np.h](https://github.com/ps5-payload-dev/sdk/blob/a7685ce8e97ce1f8a4f38253a41d54241c9124f1/include/freebsd/pthread_np.h). Console behavior is unmeasured. Following the maintainer's landing-order request, the attribute implementations and tests are removed. The current #9156 covers all four formerly overlapping attributes, so this PR now supplies only the initial-thread query. It adds no affinityTail field or attribute-layout changes. The earlier implementation remains in published history; its clearing/tail behavior came from FreeBSD, not a console measurement. This remaining query can land independently of #9156. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> 24 October update: head `12ff96135fc8ba65a24b49ac15ebf2751ef03986` includes main `c8d34aa8f4d8058d57169cc1e423ba6db00faaf0`. Built with GCC 13.3 / Ubuntu 24.04 targeting Linux x86-64. SHA256-verified copies of the unchanged build outputs ran in a full Linux guest kernel: Alpine 6.12.110-0-virt, QEMU system 8.2.2 TCG, cpu=max, 2 vCPUs, 1536 MiB. CPU execution is emulated; process_vm_* calls run in the guest kernel. No syscall shim or unchecked-copy fallback. **10/10 executions pass:** four initial-thread query cases (ordinary and worker-first loading, each unpatched/NID-patched) plus six common kernel/filesystem regressions. The worker-first negative control against the previous libraries remains historical evidence. Attribute tests and implementations belong to #9156. [benjaminmathias's native Windows report](https://github.com/boykopovar/AnyPS5/pull/9160#issuecomment-6385311444) passes the preceding head's ten Windows cases and twelve shared regressions three times each on WinLibs GCC 15.2.0 r7, Windows 11 Pro / Ryzen 7 5700X3D. It used main `c9b2660d563` plus `cd3e37795d`, without #9156; selected-DLL checks completed and original-stub controls failed. This is evidence for that integration, not a native Windows execution of the current reduced head. The two remaining Windows main-query cases retain the same behavior/assertions and the current fixture cross-links locally with MinGW 13.2. Whitespace and current-main conventions checks pass. Published ancestry and contributor authorship are retained; this head merges cleanly with the pinned main. Full upstream Linux/Windows CI requires a fresh maintainer dispatch. No console or title-unblock claim. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
DavidSamir commented 2026-10-21 15:09:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T15:09:33Z.


A comparison from a branch I wrote for the same export, which I closed in favour of this PR (#9197). The design is the same. The one difference is how the initial thread is identified on Linux: this PR records the thread that ran the module's initializer (initialThreadId), while I used gettid() == getpid(), which is the definition of a process's main thread and does not depend on which thread loads libkernel. The two agree whenever the main thread loads libkernel, which is the case at start-up.

On Windows I checked that the relinked executable's entry stub loads the libraries and then enters the ELF entry itself on the process's primary thread, since it imports no thread-creation function, so the initializer-thread approach is right there. I did not test the Linux case.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6319362673 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9160#issuecomment-6319362673). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T15:09:33Z. --- A comparison from a branch I wrote for the same export, which I closed in favour of this PR (#9197). The design is the same. The one difference is how the initial thread is identified on Linux: this PR records the thread that ran the module's initializer (`initialThreadId`), while I used `gettid() == getpid()`, which is the definition of a process's main thread and does not depend on which thread loads libkernel. The two agree whenever the main thread loads libkernel, which is the case at start-up. On Windows I checked that the relinked executable's entry stub loads the libraries and then enters the ELF entry itself on the process's primary thread, since it imports no thread-creation function, so the initializer-thread approach is right there. I did not test the Linux case. AI-assisted: yes
MarcZX10 commented 2026-10-23 11:59:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T11:59:29Z.

Review: CHANGES_REQUESTED


The Linux behavior has substantial evidence, including the worker-first-load regression, but the Windows target still lacks a supported-toolchain result. The earlier Windows run failed to link guest_kernel_sys_threads_tests.exe with undefined __gxx_personality_v0; the current target still links only ${CMAKE_DL_LIBS}. The reported cross-link used MinGW 13.2 and explicitly is not the repository’s supported WinLibs 15.2 runtime check. The automatic Windows job is path-skipped, so the ten Windows query cases and selected patched/unpatched DLL assertion have not run on the supported toolchain. Please provide that build/run (or correct the target link and then run it) before approval. The latest Linux 22/22 result is useful and does not resolve this Windows-specific gap.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5627898493 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9160#pullrequestreview-5627898493). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T11:59:29Z. **Review:** CHANGES_REQUESTED --- The Linux behavior has substantial evidence, including the worker-first-load regression, but the Windows target still lacks a supported-toolchain result. The earlier Windows run failed to link `guest_kernel_sys_threads_tests.exe` with undefined `__gxx_personality_v0`; the current target still links only `${CMAKE_DL_LIBS}`. The reported cross-link used MinGW 13.2 and explicitly is not the repository’s supported WinLibs 15.2 runtime check. The automatic Windows job is path-skipped, so the ten Windows query cases and selected patched/unpatched DLL assertion have not run on the supported toolchain. Please provide that build/run (or correct the target link and then run it) before approval. The latest Linux 22/22 result is useful and does not resolve this Windows-specific gap.
MarcZX10 commented 2026-10-23 18:42:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T18:42:49Z.

Review: APPROVED


Reviewed the implementation, the new test fixture, technical-debt note, and current merge/check status. The affinity setter and getter preserve FreeBSD 11.4’s reset, size, zero-tail, and unsupported-bit behavior; the extra mask storage is appended so existing private-field offsets stay intact, and both SCE attribute paths clear the added tail when replacing the mask. The Linux main-thread check uses kernel TID/PID identity, which addresses late loading from a worker, while the non-Linux initialization-thread limitation is explicit. The focused fixture covers patched and unpatched exports, error precedence, mask boundaries, thread identity, and late loading. The PR reports 22/22 Linux guest-VM executions and Windows cross-link validation; current hosted product jobs are skipped. I found no blocking issue in the reviewed diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5632976080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9160#pullrequestreview-5632976080). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T18:42:49Z. **Review:** APPROVED --- Reviewed the implementation, the new test fixture, technical-debt note, and current merge/check status. The affinity setter and getter preserve FreeBSD 11.4’s reset, size, zero-tail, and unsupported-bit behavior; the extra mask storage is appended so existing private-field offsets stay intact, and both SCE attribute paths clear the added tail when replacing the mask. The Linux main-thread check uses kernel TID/PID identity, which addresses late loading from a worker, while the non-Linux initialization-thread limitation is explicit. The focused fixture covers patched and unpatched exports, error precedence, mask boundaries, thread identity, and late loading. The PR reports 22/22 Linux guest-VM executions and Windows cross-link validation; current hosted product jobs are skipped. I found no blocking issue in the reviewed diff.
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!9160
No description provided.