fix(libkernel): take NOTE_LOWAT on socket read and write events #7092

Merged
milescoviello merged 2 commits from upstream-pr/7092 into main 2026-10-17 20:40:44 +00:00
milescoviello commented 2026-10-17 11:10:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T11:10:04Z.


What

kevent threw NOTE_LOWAT not implemented. Wine's wineserver registers each of its sockets with EVFILT_READ and EVFILT_WRITE carrying NOTE_LOWAT and a mark of 1, so Forever Launcher (PPSA99030) stops on it as its wineserver starts. NOTE_LOWAT now sets the low-water mark of a socket's read and write events, as FreeBSD 11's filt_soread and filt_sowrite read kn_sdata:

  • EVFILT_READ on a socket fires once the buffered bytes reach the mark. It also fires at end of file, and on a listening socket, which FreeBSD checks before the mark. A read mark above 1 is taken on stream sockets only. On a datagram socket, Linux's FIONREAD reports only the next datagram, where FreeBSD's sbavail counts every queued byte, so such a mark throws.
  • EVFILT_WRITE on a socket takes a mark of 1. The host's writability stands in for one free byte, since the host doesn't report the send buffer's free space. Other write marks throw.
  • A later change of the knote replaces the mark (or clears it without NOTE_LOWAT), as kqueue_register replaces kn_sdata.
  • A mark below 1 throws. It makes FreeBSD's read filter fire even with nothing buffered, which the host's readiness can't express.
  • A pipe ignores the mark, as filt_piperead does.

The debt entries say the same.

Tested

Ubuntu 24.04, GCC 13.3, Release, main 864729dc0d.

  • New LowWaterMarks case in guest_kqueue:
    • A socket read event with mark 4 stays quiet at 2 buffered bytes and fires at 4 with data 4.
    • A change to mark 8 makes it quiet again, a change without NOTE_LOWAT brings back the default, and end of file fires below the mark.
    • A write event with mark 1 fires; a write mark of 2 and a read mark of 0 throw.
    • On a datagram socket pair a read mark of 2 throws, and a mark of 1 fires on a one-byte datagram (91fbafd94a, after review).
    • A pipe's read event with mark 64 fires on one byte.
    • With main's Kqueue.cpp the test aborts on kevent: NOTE_LOWAT not implemented. It passes 3 of 3.
  • ctest -R '^guest_|libc': 401 of 401 pass.
  • Forever Launcher (PPSA99030, 01.000.012), in gdb: wineserver's first kevent adds EVFILT_READ (EV_ADD | EV_ENABLE) and EVFILT_WRITE (EV_ADD | EV_DISABLE) on one end of a socket pair, both with NOTE_LOWAT and data 1.
    • On main with the PRs for its earlier stops merged locally (#6915, #6028, #6939, #6787, #7070, #6469, #7090), it stops 1 s in on kevent: NOTE_LOWAT not implemented, 2 of 2.
    • With this PR it gets past it 2 of 2 and stops at its next call, sysarch, which #6028 declares as throwing.

#7075, #7088 and #7049 change lines next to these in Kqueue.cpp, GuestKqueue.cpp and the libkernel debt file (#7075 also edits the two debt lines that list NOTE_LOWAT as throwing). The conflicts are textual, and I'll rebase this one if any of them lands first.

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: PPSA99030 (Forever Launcher), after #7090 and the PRs for its earlier stops; no bring-up discussion exists yet
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4899565061 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7092). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T11:10:04Z. --- ### What `kevent` threw `NOTE_LOWAT not implemented`. Wine's wineserver registers each of its sockets with `EVFILT_READ` and `EVFILT_WRITE` carrying `NOTE_LOWAT` and a mark of 1, so Forever Launcher (PPSA99030) stops on it as its wineserver starts. `NOTE_LOWAT` now sets the low-water mark of a socket's read and write events, as FreeBSD 11's `filt_soread` and `filt_sowrite` read `kn_sdata`: - **`EVFILT_READ` on a socket** fires once the buffered bytes reach the mark. It also fires at end of file, and on a listening socket, which FreeBSD checks before the mark. A read mark above 1 is taken on stream sockets only. On a datagram socket, Linux's `FIONREAD` reports only the next datagram, where FreeBSD's `sbavail` counts every queued byte, so such a mark throws. - **`EVFILT_WRITE` on a socket** takes a mark of 1. The host's writability stands in for one free byte, since the host doesn't report the send buffer's free space. Other write marks throw. - **A later change of the knote replaces the mark** (or clears it without `NOTE_LOWAT`), as `kqueue_register` replaces `kn_sdata`. - **A mark below 1 throws.** It makes FreeBSD's read filter fire even with nothing buffered, which the host's readiness can't express. - **A pipe ignores the mark**, as `filt_piperead` does. The debt entries say the same. ### Tested Ubuntu 24.04, GCC 13.3, Release, `main` 864729dc0d. - **New `LowWaterMarks` case in `guest_kqueue`:** - A socket read event with mark 4 stays quiet at 2 buffered bytes and fires at 4 with `data` 4. - A change to mark 8 makes it quiet again, a change without `NOTE_LOWAT` brings back the default, and end of file fires below the mark. - A write event with mark 1 fires; a write mark of 2 and a read mark of 0 throw. - On a datagram socket pair a read mark of 2 throws, and a mark of 1 fires on a one-byte datagram (91fbafd94a, after review). - A pipe's read event with mark 64 fires on one byte. - With `main`'s `Kqueue.cpp` the test aborts on `kevent: NOTE_LOWAT not implemented`. It passes 3 of 3. - `ctest -R '^guest_|libc'`: 401 of 401 pass. - Forever Launcher (PPSA99030, 01.000.012), in gdb: wineserver's first `kevent` adds `EVFILT_READ` (`EV_ADD | EV_ENABLE`) and `EVFILT_WRITE` (`EV_ADD | EV_DISABLE`) on one end of a socket pair, both with `NOTE_LOWAT` and data 1. - On `main` with the PRs for its earlier stops merged locally (#6915, #6028, #6939, #6787, #7070, #6469, #7090), it stops 1 s in on `kevent: NOTE_LOWAT not implemented`, 2 of 2. - With this PR it gets past it 2 of 2 and stops at its next call, `sysarch`, which #6028 declares as throwing. #7075, #7088 and #7049 change lines next to these in `Kqueue.cpp`, `GuestKqueue.cpp` and the libkernel debt file (#7075 also edits the two debt lines that list `NOTE_LOWAT` as throwing). The conflicts are textual, and I'll rebase this one if any of them lands first. ### 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: PPSA99030 (Forever Launcher), after #7090 and the PRs for its earlier stops; no bring-up discussion exists yet - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-17 11:17:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-17T11:17:48Z.


Thanks, this matches FreeBSD 11's filt_soread/filt_sowrite well: the listening-socket bypass, EOF/error firing past the mark, write mark 1 only, the mark reset on re-add, and pipes ignoring the flag all look right.

One change before merge: on Linux, FIONREAD on a UDP socket returns the size of the next datagram only (udp_ioctl -> first_packet_length), while FreeBSD's sbavail counts every queued byte (Windows also returns the total). So with a read mark N > 1 on a datagram socket and several small datagrams queued whose total reaches N, FreeBSD fires and this never does. That turns a loud "not implemented" throw on main into a silent hang. Could you keep throwing for a read mark above 1 on non-stream sockets (or, if you need it, sum the queue some other way)? Wine's mark 1 isn't affected.

Minor, optional: Listening() does a getsockopt on every poll of a below-mark socket. It's cheap, but could be cached once the socket starts listening.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233261092 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7092#issuecomment-6233261092). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-17T11:17:48Z. --- Thanks, this matches FreeBSD 11's filt_soread/filt_sowrite well: the listening-socket bypass, EOF/error firing past the mark, write mark 1 only, the mark reset on re-add, and pipes ignoring the flag all look right. One change before merge: on Linux, FIONREAD on a UDP socket returns the size of the next datagram only (udp_ioctl -> first_packet_length), while FreeBSD's sbavail counts every queued byte (Windows also returns the total). So with a read mark N > 1 on a datagram socket and several small datagrams queued whose total reaches N, FreeBSD fires and this never does. That turns a loud "not implemented" throw on main into a silent hang. Could you keep throwing for a read mark above 1 on non-stream sockets (or, if you need it, sum the queue some other way)? Wine's mark 1 isn't affected. Minor, optional: Listening() does a getsockopt on every poll of a below-mark socket. It's cheap, but could be cached once the socket starts listening.
milescoviello commented 2026-10-17 11:20:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-17T11:20:45Z.


Thanks @oneandonlydean, good catch on the datagram case. 91fbafd94a keeps throwing for a read mark above 1 on any socket that isn't a stream (a new GuestSockets::Type), and adds a datagram case to the test: a read mark of 2 throws, and a mark of 1 fires on a one-byte datagram. Stream sockets and Wine's mark of 1 are unchanged, and the debt entry says why. guest_kqueue passes 3 of 3 and still fails with main's Kqueue.cpp.

I left Listening() uncached: it only runs while a socket is below its mark, and a cached flag would need updating from listen and close.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233286350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7092#issuecomment-6233286350). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-17T11:20:45Z. --- Thanks @oneandonlydean, good catch on the datagram case. 91fbafd94a keeps throwing for a read mark above 1 on any socket that isn't a stream (a new `GuestSockets::Type`), and adds a datagram case to the test: a read mark of 2 throws, and a mark of 1 fires on a one-byte datagram. Stream sockets and Wine's mark of 1 are unchanged, and the debt entry says why. `guest_kqueue` passes 3 of 3 and still fails with `main`'s `Kqueue.cpp`. I left `Listening()` uncached: it only runs while a socket is below its mark, and a cached flag would need updating from `listen` and `close`. AI-assisted: yes (Claude Code)
github-actions[bot] commented 2026-10-17 11:22:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-17T11:22:02Z.


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 6343 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129
docs/dev/debt/libkernel.md L112
PR 6542 docs/dev/debt/libkernel.md L111-112
PR 7049 core/libs/tests/GuestKqueue.cpp L652
PR 7075 docs/dev/debt/libkernel.md L112, L174
PR 7088 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129, L222-224
docs/dev/debt/libkernel.md L112, L174
PR 7121 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L222-228
docs/dev/debt/libkernel.md L112-113
PR 7129 core/libs/prx/libkernel/Equeue/src/Kqueue.cpp L129, L145-147, L151
core/libs/prx/libkernel/Socket/include/SocketRuntime.hpp L18
core/libs/tests/GuestKqueue.cpp L272
docs/dev/debt/libkernel.md L112, L173-174
PR 7139 docs/dev/debt/libkernel.md L173-174
PR 7248 docs/dev/debt/libkernel.md L173-174

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6044 core/libs/tests/GuestKqueue.cpp
PR 6222 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 6447 core/libs/prx/libkernel/Equeue/Equeue.cpp
PR 6510 core/libs/prx/libkernel/Equeue/Equeue.cpp
core/libs/prx/libkernel/Equeue/Equeue.hpp
core/libs/prx/libkernel/Equeue/src/Kqueue.cpp
core/libs/tests/GuestKqueue.cpp
docs/dev/debt/libkernel.md
PR 6710 docs/dev/debt/libkernel.md
PR 6962 core/libs/prx/libkernel/Socket/src/Udp.cpp
docs/dev/debt/libkernel.md
PR 7201 core/libs/prx/libkernel/Socket/src/Udp.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6233297258 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7092#issuecomment-6233297258). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-17T11:22:02Z. --- <!-- pr-overlap --> 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 6343](https://www.github.com/boykopovar/AnyPS5/pull/6343) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp` [L129](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L129)<br>`docs/dev/debt/libkernel.md` [L112](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L112) | | | | [PR 6542](https://www.github.com/boykopovar/AnyPS5/pull/6542) | `docs/dev/debt/libkernel.md` [L111-112](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L111-L112) | | | | [PR 7049](https://www.github.com/boykopovar/AnyPS5/pull/7049) | `core/libs/tests/GuestKqueue.cpp` [L652](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/tests/GuestKqueue.cpp#L652) | | | | [PR 7075](https://www.github.com/boykopovar/AnyPS5/pull/7075) | `docs/dev/debt/libkernel.md` [L112](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L112), [L174](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L174) | | | | [PR 7088](https://www.github.com/boykopovar/AnyPS5/pull/7088) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp` [L129](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L129), [L222-224](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L222-L224)<br>`docs/dev/debt/libkernel.md` [L112](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L112), [L174](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L174) | | | | [PR 7121](https://www.github.com/boykopovar/AnyPS5/pull/7121) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp` [L222-228](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L222-L228)<br>`docs/dev/debt/libkernel.md` [L112-113](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L112-L113) | | | | [PR 7129](https://www.github.com/boykopovar/AnyPS5/pull/7129) | `core/libs/prx/libkernel/Equeue/src/Kqueue.cpp` [L129](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L129), [L145-147](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L145-L147), [L151](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Equeue/src/Kqueue.cpp#L151)<br>`core/libs/prx/libkernel/Socket/include/SocketRuntime.hpp` [L18](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/prx/libkernel/Socket/include/SocketRuntime.hpp#L18)<br>`core/libs/tests/GuestKqueue.cpp` [L272](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/core/libs/tests/GuestKqueue.cpp#L272)<br>`docs/dev/debt/libkernel.md` [L112](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L112), [L173-174](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L173-L174) | | | | [PR 7139](https://www.github.com/boykopovar/AnyPS5/pull/7139) | `docs/dev/debt/libkernel.md` [L173-174](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L173-L174) | | | | [PR 7248](https://www.github.com/boykopovar/AnyPS5/pull/7248) | `docs/dev/debt/libkernel.md` [L173-174](https://github.com/boykopovar/AnyPS5/blob/922dc8999ea024c9d5bce46049aa22d6dd72f534/docs/dev/debt/libkernel.md#L173-L174) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestKqueue.cpp` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libkernel/Equeue/Equeue.cpp` | | [PR 6510](https://www.github.com/boykopovar/AnyPS5/pull/6510) | `core/libs/prx/libkernel/Equeue/Equeue.cpp`<br>`core/libs/prx/libkernel/Equeue/Equeue.hpp`<br>`core/libs/prx/libkernel/Equeue/src/Kqueue.cpp`<br>`core/libs/tests/GuestKqueue.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 6710](https://www.github.com/boykopovar/AnyPS5/pull/6710) | `docs/dev/debt/libkernel.md` | | [PR 6962](https://www.github.com/boykopovar/AnyPS5/pull/6962) | `core/libs/prx/libkernel/Socket/src/Udp.cpp`<br>`docs/dev/debt/libkernel.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/prx/libkernel/Socket/src/Udp.cpp` |
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!7092
No description provided.