feat(libkernel_sys): implement the scePthreadBarrier, cpuclockid, concurrency and msync forms #8987

Merged
simonbronk merged 1 commit from upstream-pr/8987 into main 2026-10-23 06:24:35 +00:00
simonbronk commented 2026-10-21 08:40:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-21T08:40:43Z.


What

Implements 10 libkernel_sys exports that were discovery stubs, as wrappers over libkernel's existing POSIX implementations:

  • scePthreadBarrierInit, scePthreadBarrierWait, scePthreadBarrierDestroy
  • scePthreadBarrierattrInit, scePthreadBarrierattrDestroy, scePthreadBarrierattrGetpshared, scePthreadBarrierattrSetpshared
  • scePthreadGetcpuclockid, scePthreadGetconcurrency
  • sceKernelMsync

A POSIX error is returned as 0x80020000 | errno (SceKernelError). scePthreadBarrierWait returns -1 (PTHREAD_BARRIER_SERIAL_THREAD) to the serial thread, and scePthreadBarrierInit ignores its name, as shadPS4 (PS4) does. Signatures follow the Orbital type database. Neither is measured on a PS5; this is recorded in docs/dev/debt/libkernel_sys.md.

sceKernelMlockall is left throwing: libkernel's mlockall throws for every valid flag, so wrapping it would only move the throw. scePthreadCondattrGetclock is left to #8373. libkernel_sys goes from 13 to 23 of 545 in tools/progress.py.

Tested

macOS 27 (Apple silicon), clang, C++20. The new guest_kernel_sys_barrier test (core/libs/tests/GuestKernelSysBarrier.cpp, registered next to guest_kernel_sys) was built against libkernel_sys/Export.Discovery.cpp and libkernel's real Pthread/Posix/Barrier.cpp, with minimal stand-ins for thread creation, pthread_getcpuclockid, pthread_*concurrency and msync, since the full build does not run on macOS. It passes: attribute errors, two rounds of a 4-thread barrier with exactly one serial thread, a 1-count barrier, destroy errors, the cpuclock id against the POSIX call, the concurrency level, and msync EINVAL/ENOMEM.

The CMake/ctest build on Linux and Windows was not run locally. check_conventions.py is clean and pr_overlap.py reports no overlap. No title tested.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4948819217 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-21T08:40:43Z. --- ### What Implements 10 libkernel_sys exports that were discovery stubs, as wrappers over libkernel's existing POSIX implementations: - `scePthreadBarrierInit`, `scePthreadBarrierWait`, `scePthreadBarrierDestroy` - `scePthreadBarrierattrInit`, `scePthreadBarrierattrDestroy`, `scePthreadBarrierattrGetpshared`, `scePthreadBarrierattrSetpshared` - `scePthreadGetcpuclockid`, `scePthreadGetconcurrency` - `sceKernelMsync` A POSIX error is returned as `0x80020000 | errno` (`SceKernelError`). `scePthreadBarrierWait` returns -1 (`PTHREAD_BARRIER_SERIAL_THREAD`) to the serial thread, and `scePthreadBarrierInit` ignores its name, as shadPS4 (PS4) does. Signatures follow the Orbital type database. Neither is measured on a PS5; this is recorded in `docs/dev/debt/libkernel_sys.md`. `sceKernelMlockall` is left throwing: libkernel's `mlockall` throws for every valid flag, so wrapping it would only move the throw. `scePthreadCondattrGetclock` is left to #8373. libkernel_sys goes from 13 to 23 of 545 in `tools/progress.py`. ### Tested macOS 27 (Apple silicon), clang, C++20. The new `guest_kernel_sys_barrier` test (`core/libs/tests/GuestKernelSysBarrier.cpp`, registered next to `guest_kernel_sys`) was built against `libkernel_sys/Export.Discovery.cpp` and libkernel's real `Pthread/Posix/Barrier.cpp`, with minimal stand-ins for thread creation, `pthread_getcpuclockid`, `pthread_*concurrency` and `msync`, since the full build does not run on macOS. It passes: attribute errors, two rounds of a 4-thread barrier with exactly one serial thread, a 1-count barrier, destroy errors, the cpuclock id against the POSIX call, the concurrency level, and `msync` EINVAL/ENOMEM. The CMake/ctest build on Linux and Windows was not run locally. `check_conventions.py` is clean and `pr_overlap.py` reports no overlap. No title tested. ### 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 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-21 08:41:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T08:41:31Z.


Progress report (7e2ba73 → 106e736)

📈 System libraries: 43.32% (+0.09%, +10 implemented functions)

✅ 10 implemented
Library Name
libkernel_sys sceKernelMsync
libkernel_sys scePthreadBarrierDestroy
libkernel_sys scePthreadBarrierInit
libkernel_sys scePthreadBarrierWait
libkernel_sys scePthreadBarrierattrDestroy
libkernel_sys scePthreadBarrierattrGetpshared
libkernel_sys scePthreadBarrierattrInit
libkernel_sys scePthreadBarrierattrSetpshared
libkernel_sys scePthreadGetconcurrency
libkernel_sys scePthreadGetcpuclockid
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6311802112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#issuecomment-6311802112). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-21T08:41:31Z. --- <!-- progress-report --> ### Progress report (7e2ba73 → 106e736) 📈 **System libraries**: 43.32% (+0.09%, +10 implemented functions) <details> <summary>✅ 10 implemented</summary> | Library | Name | | - | - | | libkernel_sys | `sceKernelMsync` | | libkernel_sys | `scePthreadBarrierDestroy` | | libkernel_sys | `scePthreadBarrierInit` | | libkernel_sys | `scePthreadBarrierWait` | | libkernel_sys | `scePthreadBarrierattrDestroy` | | libkernel_sys | `scePthreadBarrierattrGetpshared` | | libkernel_sys | `scePthreadBarrierattrInit` | | libkernel_sys | `scePthreadBarrierattrSetpshared` | | libkernel_sys | `scePthreadGetconcurrency` | | libkernel_sys | `scePthreadGetcpuclockid` | </details>
xXPBSXx commented 2026-10-21 14:43:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T14:43:38Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_kernel_sys_barrier_tests; ctest -N -R guest_kernel_sys_barrier lists exactly one test.

PR head tested: 6131ec265d3269b130185cd7a2cf26929d304de8.

Results (3 runs each):

  • PR head: guest_kernel_sys_barrier passes 3/3.
  • Baseline: I kept the PR's test and replaced only core/libs/prx/libkernel_sys/Export.Discovery.cpp with the version from the PR's merge base (27c88a8d75), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6318827821 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#issuecomment-6318827821). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T14:43:38Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_kernel_sys_barrier_tests`; `ctest -N -R guest_kernel_sys_barrier` lists exactly one test. **PR head tested:** `6131ec265d3269b130185cd7a2cf26929d304de8`. **Results (3 runs each):** - PR head: `guest_kernel_sys_barrier` passes 3/3. - Baseline: I kept the PR's test and replaced only `core/libs/prx/libkernel_sys/Export.Discovery.cpp` with the version from the PR's merge base (`27c88a8d75`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
simonbronk commented 2026-10-21 18:16:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-21T18:16:58Z.


Linux run of 6131ec265 merged onto main 99844c7ee (clean merge).

Environment: Ubuntu 24.04 x86-64 container under Rosetta on Apple Silicon (Colima), GCC 13, RelWithDebInfo, BUILD_TESTING=ON, ANYPS5_ENABLE_ASAN=ON. ASan builds dynamic executables, so this is not the CI configuration.

Results: the full build has no errors. the 2 tests matching kernel_sys, including guest_kernel_sys_barrier: all pass, with no AddressSanitizer report.

Not run: the static CI configuration, native x86-64 hardware, GPU tests, a title.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323253830 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#issuecomment-6323253830). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-21T18:16:58Z. --- Linux run of 6131ec265 merged onto `main` 99844c7ee (clean merge). **Environment:** Ubuntu 24.04 x86-64 container under Rosetta on Apple Silicon (Colima), GCC 13, `RelWithDebInfo`, `BUILD_TESTING=ON`, `ANYPS5_ENABLE_ASAN=ON`. ASan builds dynamic executables, so this is not the CI configuration. **Results:** the full build has no errors. the 2 tests matching `kernel_sys`, including `guest_kernel_sys_barrier`: all pass, with no AddressSanitizer report. **Not run:** the static CI configuration, native x86-64 hardware, GPU tests, a title. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-22 05:47:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T05:47:40Z.

Review: COMMENTED


Checked against main 4a17c7175 and FreeBSD 11.4 lib/libthr/thread/thr_barrier.c and thr_barrierattr.c:

  • The SCE encoding looks right. Errors come back as 0x80020000|errno, scePthreadBarrierWait passes -1 to the serial thread, and sceKernelMsync reads the errno that libkernel's SetError stores through __error. Nothing reports success without doing the work.
  • scePthreadBarrierattrSetpshared(&attr, 1 /* PTHREAD_PROCESS_SHARED */) returns 0x80020016, because libkernel's Barrier.cpp:56 rejects anything but PROCESS_PRIVATE. That is the FreeBSD 9 behaviour. FreeBSD 11.4's _pthread_barrierattr_setpshared accepts both PRIVATE and SHARED and returns 0, and the PS5 is 11-based. GuestKernelSysBarrier.cpp:62 now asserts the EINVAL for the sce form too.
  • In the same file, Barrier.cpp:63 rejects only count == 0. FreeBSD 11.4's _pthread_barrier_init also returns EINVAL for count > INT_MAX, so scePthreadBarrierInit(&b, nullptr, 0x80000000u, nullptr) succeeds here and fails on FreeBSD.
  • Both come from the existing Barrier.cpp rather than this PR. But the new line 62 assertion locks the first one in, and neither is in the debt files. Suggestion: either fix both in Barrier.cpp and change line 62, or drop that assertion and record both differences in the debt entry.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609774583 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#pullrequestreview-5609774583). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:40Z. **Review:** COMMENTED --- Checked against main 4a17c7175 and FreeBSD 11.4 lib/libthr/thread/thr_barrier.c and thr_barrierattr.c: - The SCE encoding looks right. Errors come back as `0x80020000|errno`, `scePthreadBarrierWait` passes -1 to the serial thread, and `sceKernelMsync` reads the errno that libkernel's `SetError` stores through `__error`. Nothing reports success without doing the work. - `scePthreadBarrierattrSetpshared(&attr, 1 /* PTHREAD_PROCESS_SHARED */)` returns 0x80020016, because libkernel's Barrier.cpp:56 rejects anything but PROCESS_PRIVATE. That is the FreeBSD 9 behaviour. FreeBSD 11.4's `_pthread_barrierattr_setpshared` accepts both PRIVATE and SHARED and returns 0, and the PS5 is 11-based. GuestKernelSysBarrier.cpp:62 now asserts the EINVAL for the sce form too. - In the same file, Barrier.cpp:63 rejects only `count == 0`. FreeBSD 11.4's `_pthread_barrier_init` also returns EINVAL for `count > INT_MAX`, so `scePthreadBarrierInit(&b, nullptr, 0x80000000u, nullptr)` succeeds here and fails on FreeBSD. - Both come from the existing Barrier.cpp rather than this PR. But the new line 62 assertion locks the first one in, and neither is in the debt files. Suggestion: either fix both in Barrier.cpp and change line 62, or drop that assertion and record both differences in the debt entry. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
simonbronk commented 2026-10-22 09:07:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simonbronk on 2026-10-22T09:07:13Z.


@harisibrar1 thanks for checking it against libthr.

  • setpshared(PTHREAD_PROCESS_SHARED): the PS5 differs from FreeBSD 11.4 here. On firmware 12.70, pthread_barrierattr_setpshared, pthread_mutexattr_setpshared and pthread_spin_init accept only PTHREAD_PROCESS_PRIVATE, and 1, 2 and -1 return EINVAL. That is what db504f6b9 on main measured and implemented. So line 62 asserts the console's behaviour, and the sce form passes it through as 0x80020016.
  • count > INT_MAX: main has rejected it since 669455d1f. I rebased onto d6eafa54d and added scePthreadBarrierInit(&b, &attr, 0x80000000u, ...) == 0x80020016 with the barrier left null, so the sce form covers it too.

Tested: Linux, Ubuntu 24.04 x86-64 container (Colima/Rosetta), GCC 13, RelWithDebInfo with ASan, merged onto main d6eafa54d: guest_kernel_sys, guest_kernel_sys_barrier, guest_kernel_sys_thread_defaults and guest_pthread_barrier pass, no ASan reports.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337027641 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#issuecomment-6337027641). Originally posted by [simonbronk](https://github.com/simonbronk) on 2026-10-22T09:07:13Z. --- @harisibrar1 thanks for checking it against libthr. - **`setpshared(PTHREAD_PROCESS_SHARED)`:** the PS5 differs from FreeBSD 11.4 here. On firmware 12.70, `pthread_barrierattr_setpshared`, `pthread_mutexattr_setpshared` and `pthread_spin_init` accept only `PTHREAD_PROCESS_PRIVATE`, and 1, 2 and -1 return `EINVAL`. That is what db504f6b9 on `main` measured and implemented. So line 62 asserts the console's behaviour, and the sce form passes it through as 0x80020016. - **`count > INT_MAX`:** `main` has rejected it since 669455d1f. I rebased onto d6eafa54d and added `scePthreadBarrierInit(&b, &attr, 0x80000000u, ...) == 0x80020016` with the barrier left null, so the sce form covers it too. **Tested:** Linux, Ubuntu 24.04 x86-64 container (Colima/Rosetta), GCC 13, RelWithDebInfo with ASan, merged onto `main` d6eafa54d: `guest_kernel_sys`, `guest_kernel_sys_barrier`, `guest_kernel_sys_thread_defaults` and `guest_pthread_barrier` pass, no ASan reports. AI-assisted: yes
harisibrar1 commented 2026-10-22 12:47:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T12:47:33Z.


@simonbronk you're right on both points, and I'm sorry for the noise. My check read Barrier.cpp from this branch's older base instead of main. On main 4a17c7175, db504f6b9 already rejects PTHREAD_PROCESS_SHARED from the 12.70 measurement, and 669455d1f already rejects counts above INT_MAX (Barrier.cpp:57 and :64). So line 62 asserts measured console behaviour and is correct, and the new 0x80000000u sce case is a good addition. Please disregard that part of my review; the rest still stands.

Reviewed with AI assistance (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6341119680 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8987#issuecomment-6341119680). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T12:47:33Z. --- @simonbronk you're right on both points, and I'm sorry for the noise. My check read `Barrier.cpp` from this branch's older base instead of main. On main 4a17c7175, db504f6b9 already rejects `PTHREAD_PROCESS_SHARED` from the 12.70 measurement, and 669455d1f already rejects counts above `INT_MAX` (`Barrier.cpp:57` and `:64`). So line 62 asserts measured console behaviour and is correct, and the new `0x80000000u` sce case is a good addition. Please disregard that part of my review; the rest still stands. Reviewed with AI assistance (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!8987
No description provided.