Skip to content

feat(libkernel): implement the named ksem semaphores - #93

Open
Thimo188 wants to merge 13 commits into
mainfrom
upstream-pr/9293
Open

Thimo188 wants to merge 13 commits into
mainfrom
upstream-pr/9293

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#9293, originally opened by DavidSamir.

Source commit: b208fd6b210d26e58b969d2341404b4cf085f5b0. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

Builds on boykopovar#9284 and adds the named ksem_* exports to libkernel/Semaphore/src/Ksem.cpp, following FreeBSD 11 sys/kern/uipc_sem.c. Until boykopovar#9284 merges, this PR's diff also shows boykopovar#9284's commits.

Moved to libkernel so libkernel imports reach it on Windows, as boykopovar#9284 now is. The PS5 3.20 lists export ksem_* from libkernel too, and a Windows libkernel import never searches libkernel_sys. The libkernel_sys stubs for ksem_open, ksem_close and ksem_unlink are removed.

Export Behaviour
ksem_open(semid_t* idp, const char* name, int oflag, mode_t mode, u_int value) see the rules below
ksem_close(semid_t) EINVAL on an unnamed id, which needs ksem_destroy
ksem_unlink(const char* name) removes the name, ENOENT if it is absent; ids already open keep working, and a later O_CREAT open makes a new semaphore

ksem_open's rules, in the order of sys_ksem_open and ksem_create:

  1. EINVAL for oflag bits other than O_CREAT | O_EXCL, then for value > SEM_VALUE_MAX.
  2. The descriptor is allocated (EMFILE or ENFILE when none is left), and the id is written to *idp (EFAULT for a NULL idp) before the name is checked, as FreeBSD's early copyout does.
  3. EFAULT for a NULL name, ENAMETOOLONG for 1024 bytes or more, and EINVAL for a name that doesn't start with /.
  4. ENOENT for a missing name without O_CREAT, and EEXIST for an existing name with O_CREAT | O_EXCL.
  5. ENFILE when a new name would exceed the 30 semaphores (SEM_MAX).

Each successful open returns a new descriptor id that shares the semaphore.

The limit counts semaphore objects, as FreeBSD's nsems does. Two opens of one name are one semaphore, and a name still linked counts until ksem_unlink, even with no open id. Unnamed ksem_init gives ENOSPC past the limit, as in boykopovar#9284. (Points from pipe1os's review on boykopovar#9284.)

ksem_destroy now gives EINVAL on a named id, which needs ksem_close, as FreeBSD's KS_ANONYMOUS check does. A debt line in libkernel.md records that named semaphores are kept per process, which fits a single guest process: mode is accepted, and the owner and permission checks are not modelled.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved.

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: feat(libkernel): implement the unnamed ksem semaphores boykopovar/AnyPS5#9284
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

…elative name

An absolute host temp path is not mapped by the Linux guest resolver.
Found by xXPBSXx on boykopovar#9097.
The PS5 3.20 export lists have ksem_* in libkernel as well as
libkernel_sys, and on Windows a libkernel import never reaches
libkernel_sys, so titles importing them from libkernel did not find
them. Following pipe1os's review on boykopovar#9284, also: ENOSPC from
ksem_init when no descriptor is left, the SEM_MAX limit of 30 with
ENOSPC, and dup/dup2 from a ksem id throwing as for a kqueue.
… libkernel too

Following the move of the unnamed calls: ksem_open now allocates the
descriptor and writes the id before checking the name, as ksem_create
does, and the 30-semaphore limit counts semaphore objects, with a
linked name counting until it is unlinked (ENFILE for a new name).
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 94 core/libs/prx/libkernel/Semaphore/include/Ksem.hpp
core/libs/prx/libkernel/Semaphore/src/Ksem.cpp
core/libs/tests/GuestKernelSysKsem.cpp
PR 95 core/libs/prx/libkernel/Semaphore/include/Ksem.hpp
core/libs/prx/libkernel/Semaphore/src/Ksem.cpp
core/libs/tests/GuestKernelSysKsem.cpp

Exports with a name or library/NID already present in main:

  • ksem_destroy (same library/NID already present: core/libs/prx/libkernel (asSKL30+heA))
  • ksem_getvalue (same library/NID already present: core/libs/prx/libkernel (rOT0UMvtCCA))
  • ksem_init (same library/NID already present: core/libs/prx/libkernel (TEwqtzAmezo))
  • ksem_post (same library/NID already present: core/libs/prx/libkernel (s8gWJrY1W-k))
  • ksem_timedwait (same library/NID already present: core/libs/prx/libkernel (vE+WlomLSh4))
  • ksem_trywait (same library/NID already present: core/libs/prx/libkernel (gVaofuRvLdU))
  • ksem_wait (same library/NID already present: core/libs/prx/libkernel (rQvMIxmFD6A))

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants