feat(libkernel): match event flags to PS5 and add shared flags #9134

Merged
orezraey merged 6 commits from upstream-pr/9134 into main 2026-10-23 20:12:15 +00:00
orezraey commented 2026-10-21 13:01:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-21T13:01:37Z.


What

Event flag behaviour measured on a retail PS5 (firmware 13.60) with five homebrew probes. The sources and console logs are in this gist (evflag to evflag5).

Attributes and wait modes

  • sceKernelCreateEventFlag accepts attribute bits 0x1, 0x2, 0x10, 0x20 and 0x100. It returns EINVAL for 0x1|0x2 (we accepted it), for 0x10|0x20 (unchanged), and for every other bit (0x4 to 0x80000000, one at a time).
  • Names of 32 bytes or more return ENAMETOOLONG (0x8002003F) with or without 0x100. An empty name is accepted.
  • sceKernelWaitEventFlag and sceKernelPollEventFlag ignore mode bits outside the match (0x3) and clear (0x30) fields, so OR|0x100, OR|0x80000000 and AND|0x4 behave like OR and AND. We returned EINVAL. A mode still needs exactly one of AND and OR, and not both clear bits.

Result pattern on errors

  • EPERM (a second waiter, or a poll, on a single-waiter flag): the console writes 0, even when the poll's condition is satisfied. We left the result untouched.
  • ECANCELED: the console writes 0. We wrote the pattern after the cancel.
  • EACCES (flag deleted while waiting): the console writes 0. We left the result untouched.
  • EINVAL leaves the result untouched, and ETIMEDOUT and EBUSY write the current pattern. Those were already right.
  • With both a bad mode and a single-waiter flag that already has a waiter, the console returns EINVAL, which is the order used here.

Shared flags (attribute 0x100) and sceKernelCloseEventFlag

  • Only flags created with 0x100 can be opened by name, including from another process. Opening a flag created without it, or a missing name, returns ESRCH; we opened any flag and returned ENOENT.
  • Each open returns a new handle on the same flag; the probe opened one flag 40 times. Closing an opened handle releases it, and a second close or any use afterwards returns ESRCH. Closing a creator handle returns EPERM, and so does deleting through an opened handle; both handles keep working.
  • Two live 0x100 flags cannot share a name (EPERM). Flags without 0x100, or a 0x100 flag and one without it, can.
  • When the creator deletes the flag, the name can be reused and waiters on any handle return EACCES with result 0. An opened handle then polls and waits with EACCES (result 0, after the EINVAL checks), sets, clears and cancels with no effect (cancel reports 0 waiters), still returns EPERM on delete, and can be closed.
  • sceKernelCloseEventFlag was a stub in libkernel_sys. It is now implemented next to the other event flag functions in libkernel, and the stub is removed, since check_conventions.py rejects the same export in two libraries.
  • Flags of other processes, such as the shell's SceShellCoreUtilAppFocus, can be opened on the console. They do not exist here, so opening them returns ESRCH; the debt entry says so.

Wake order
The multi-waiter wake order was also measured. Waiters are served in FIFO order, and each satisfied waiter's clear is applied before the next one is checked. Eight scenarios covered AND and OR, both clear modes, and FIFO and PRIO flags. They all match what we do, so the "unverified on PS5" debt entry for sceKernelSetEventFlag is removed.

Not measured: whether ENAMETOOLONG or EINVAL comes first when a call has both a bad attribute and a long name. Handles are still host addresses here, while the console uses small IDs. Both are recorded under sceKernelCreateEventFlag in docs/dev/debt/libkernel.md.

Tested

Windows 11, MinGW (winlibs). guest_kernel_errors, kernel_error, the guest_kernel_sys tests and the savedata tests pass (17 tests), and guest_kernel_errors passed 10 runs in a row. The new cases cover:

  • every invalid attribute bit and the 0x1|0x2 pair;
  • the ignored mode bits;
  • result 0 on EPERM, ECANCELED (single and multi flags) and EACCES;
  • name lengths;
  • shared and plain open, duplicate names, close and delete on both handle kinds;
  • an opened handle after its creator deletes the flag;
  • a single-waiter shared flag used through two handles.

tools/check_conventions.py passes. tools/progress.py counts sceKernelCreateEventFlag and the new sceKernelCloseEventFlag as implemented. Linux was not built.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951992360 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9134). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-21T13:01:37Z. --- ### What Event flag behaviour measured on a retail PS5 (firmware 13.60) with five homebrew probes. The sources and console logs are in [this gist](https://gist.github.com/orezraey/4b6fd7fe96808654dae5037e64ccadcd) (`evflag` to `evflag5`). **Attributes and wait modes** - `sceKernelCreateEventFlag` accepts attribute bits 0x1, 0x2, 0x10, 0x20 and 0x100. It returns EINVAL for 0x1|0x2 (we accepted it), for 0x10|0x20 (unchanged), and for every other bit (0x4 to 0x80000000, one at a time). - Names of 32 bytes or more return ENAMETOOLONG (`0x8002003F`) with or without 0x100. An empty name is accepted. - `sceKernelWaitEventFlag` and `sceKernelPollEventFlag` ignore mode bits outside the match (0x3) and clear (0x30) fields, so `OR|0x100`, `OR|0x80000000` and `AND|0x4` behave like `OR` and `AND`. We returned EINVAL. A mode still needs exactly one of AND and OR, and not both clear bits. **Result pattern on errors** - EPERM (a second waiter, or a poll, on a single-waiter flag): the console writes 0, even when the poll's condition is satisfied. We left the result untouched. - ECANCELED: the console writes 0. We wrote the pattern after the cancel. - EACCES (flag deleted while waiting): the console writes 0. We left the result untouched. - EINVAL leaves the result untouched, and ETIMEDOUT and EBUSY write the current pattern. Those were already right. - With both a bad mode and a single-waiter flag that already has a waiter, the console returns EINVAL, which is the order used here. **Shared flags (attribute 0x100) and `sceKernelCloseEventFlag`** - Only flags created with 0x100 can be opened by name, including from another process. Opening a flag created without it, or a missing name, returns ESRCH; we opened any flag and returned ENOENT. - Each open returns a new handle on the same flag; the probe opened one flag 40 times. Closing an opened handle releases it, and a second close or any use afterwards returns ESRCH. Closing a creator handle returns EPERM, and so does deleting through an opened handle; both handles keep working. - Two live 0x100 flags cannot share a name (EPERM). Flags without 0x100, or a 0x100 flag and one without it, can. - When the creator deletes the flag, the name can be reused and waiters on any handle return EACCES with result 0. An opened handle then polls and waits with EACCES (result 0, after the EINVAL checks), sets, clears and cancels with no effect (cancel reports 0 waiters), still returns EPERM on delete, and can be closed. - `sceKernelCloseEventFlag` was a stub in `libkernel_sys`. It is now implemented next to the other event flag functions in `libkernel`, and the stub is removed, since `check_conventions.py` rejects the same export in two libraries. - Flags of other processes, such as the shell's `SceShellCoreUtilAppFocus`, can be opened on the console. They do not exist here, so opening them returns ESRCH; the debt entry says so. **Wake order** The multi-waiter wake order was also measured. Waiters are served in FIFO order, and each satisfied waiter's clear is applied before the next one is checked. Eight scenarios covered AND and OR, both clear modes, and FIFO and PRIO flags. They all match what we do, so the "unverified on PS5" debt entry for `sceKernelSetEventFlag` is removed. Not measured: whether ENAMETOOLONG or EINVAL comes first when a call has both a bad attribute and a long name. Handles are still host addresses here, while the console uses small IDs. Both are recorded under `sceKernelCreateEventFlag` in `docs/dev/debt/libkernel.md`. ### Tested Windows 11, MinGW (winlibs). `guest_kernel_errors`, `kernel_error`, the `guest_kernel_sys` tests and the `savedata` tests pass (17 tests), and `guest_kernel_errors` passed 10 runs in a row. The new cases cover: - every invalid attribute bit and the 0x1|0x2 pair; - the ignored mode bits; - result 0 on EPERM, ECANCELED (single and multi flags) and EACCES; - name lengths; - shared and plain open, duplicate names, close and delete on both handle kinds; - an opened handle after its creator deletes the flag; - a single-waiter shared flag used through two handles. `tools/check_conventions.py` passes. `tools/progress.py` counts `sceKernelCreateEventFlag` and the new `sceKernelCloseEventFlag` as implemented. Linux was not built. ### 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 (Claude Code)
harisibrar1 commented 2026-10-22 05:47:44 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Checked against main 4a17c7175 and the console logs in the linked gist (evflag…evflag5):

  • The changed behaviours match the logs. Attribute 0x3 gives EINVAL, names of 32 bytes or more give 0x8002003f, and the result pattern is 0 on ECANCELED, EACCES and EPERM. Closing an opened handle returns 0, and deleting through it returns EPERM. Lock order (registry, then flag) is consistent across Create, Open and Delete. The only other internal caller, libSceSaveData's create_signalled_flag with attribute 0x20, is unaffected.
  • The order between the attribute EINVAL and ENAMETOOLONG was chosen without measurement. sceKernelCreateEventFlag(&ef, <32-char name>, 0x3, 0, nullptr) returns EINVAL here because the attribute checks run before the strlen(name) >= EVF_NAME_CAPACITY check, and evflag3 only tries long names with 0x20 and 0x120. The PR description says this order was not measured, but docs/dev/debt/libkernel.md doesn't record it. Please add a sceKernelCreateEventFlag debt entry, or extend the event flag entry, saying so.
  • That entry could also note the handle difference the description mentions: handles are host addresses here, while the console uses small IDs.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5609774971 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9134#pullrequestreview-5609774971). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T05:47:44Z. **Review:** COMMENTED --- Checked against main 4a17c7175 and the console logs in the linked gist (`evflag`…`evflag5`): - The changed behaviours match the logs. Attribute 0x3 gives EINVAL, names of 32 bytes or more give `0x8002003f`, and the result pattern is 0 on ECANCELED, EACCES and EPERM. Closing an opened handle returns 0, and deleting through it returns EPERM. Lock order (registry, then flag) is consistent across Create, Open and Delete. The only other internal caller, libSceSaveData's `create_signalled_flag` with attribute 0x20, is unaffected. - The order between the attribute EINVAL and ENAMETOOLONG was chosen without measurement. `sceKernelCreateEventFlag(&ef, <32-char name>, 0x3, 0, nullptr)` returns EINVAL here because the attribute checks run before the `strlen(name) >= EVF_NAME_CAPACITY` check, and `evflag3` only tries long names with 0x20 and 0x120. The PR description says this order was not measured, but `docs/dev/debt/libkernel.md` doesn't record it. Please add a `sceKernelCreateEventFlag` debt entry, or extend the event flag entry, saying so. - That entry could also note the handle difference the description mentions: handles are host addresses here, while the console uses small IDs. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
orezraey commented 2026-10-22 05:52:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-22T05:52:59Z.


Added a sceKernelCreateEventFlag entry to docs/dev/debt/libkernel.md in 8a217a1224. It says that a call with both an invalid attribute and a 32-byte or longer name returns EINVAL here, because the attribute checks run first, and that the console order was not measured since evflag3 only used 0x20 and 0x120. It also notes that handles are host addresses here while the console returns small IDs. This is a docs-only change; check_conventions.py passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6333815183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9134#issuecomment-6333815183). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-22T05:52:59Z. --- Added a `sceKernelCreateEventFlag` entry to `docs/dev/debt/libkernel.md` in 8a217a1224. It says that a call with both an invalid attribute and a 32-byte or longer name returns EINVAL here, because the attribute checks run first, and that the console order was not measured since `evflag3` only used 0x20 and 0x120. It also notes that handles are host addresses here while the console returns small IDs. This is a docs-only change; `check_conventions.py` passes.
harisibrar1 commented 2026-10-22 12:54:52 +00:00 (Migrated from github.com)

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

Review: APPROVED


Rechecked at 8a217a1224:

  • The new sceKernelCreateEventFlag entry (libkernel.md:63) matches the code. The attribute checks at EventFlag.cpp:150-152 return EINVAL before the name-length check at line 153, and the entry records that the console order wasn't measured (only 0x20/0x120 with long names).
  • It also notes the host-address handles, which RegisterEventFlag (EventFlag.cpp:109) does produce.
  • Removing the old sceKernelSetEventFlag "unverified" entry is backed by the wake-order measurements in the description, and the Set loop is unchanged.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5614477398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9134#pullrequestreview-5614477398). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T12:54:52Z. **Review:** APPROVED --- Rechecked at 8a217a1224: - The new `sceKernelCreateEventFlag` entry (libkernel.md:63) matches the code. The attribute checks at EventFlag.cpp:150-152 return EINVAL before the name-length check at line 153, and the entry records that the console order wasn't measured (only 0x20/0x120 with long names). - It also notes the host-address handles, which `RegisterEventFlag` (EventFlag.cpp:109) does produce. - Removing the old `sceKernelSetEventFlag` "unverified" entry is backed by the wake-order measurements in the description, and the Set loop is unchanged. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9134
No description provided.