A ksem id is a host null device descriptor here, so fstat reported the
host's /dev/null and fchmod called the host's fchmod on it, which as
root changes the mode of the host's /dev/null. FreeBSD 11.4 answers
both from the semaphore: ksem_init creates it with S_IRWXU | S_IRWXG
under the umask, owned by the caller; ksem_stat reports a zeroed stat
with S_IFREG, that mode, the owner and the four times; ksem_chmod keeps
the mode's ACCESSPERMS bits. The times are taken to the microsecond, as
vfs_timestamp does by default: all four at creation, the change time on
ksem_post and the access time on every wait and on ksem_getvalue, also
when its copy-out then fails.
The equeue read and write events and AIO decided from fstat whether a
descriptor is a regular file, which a ksem id would now pass. They ask
an exported IsKsemDescriptor_nid_no_patch instead: the equeue calls keep
their throw, and AIO keeps EOPNOTSUPP for a read and gives
aio_fsync EINVAL, as aio_aqueue does for a file without a vnode.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
kevent, sceKernelAddReadEvent and sceKernelAddWriteEvent threw for read
and write events on /dev/random and /dev/urandom. FreeBSD 11.4 attaches
these filters through the file's fo_kqfilter; devfs passes it to the
device's d_kqfilter, which random_cdevsw leaves unset and prep_cdevsw
fills with enodev. kqueue_register clears fflags before the attach, so
the error event has fflags 0 and ENODEV in data. The check now comes
before the standard input shortcut, so a random device duplicated onto
descriptor 0 answers the same, and the two equeue calls check a bad
queue first, as kern_kevent fetches the kqueue before the attach.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a PS5 (firmware 13.590.001), user events 1, 2 and 3 added in that order
and triggered as 2, 1, 3 were reported as 2, 1, 3, again as 2, 1, 3 on the
next wait, and unchanged by a second trigger of an event already
triggered. They were reported here in registration order. A user event
that goes from idle to triggered now moves behind the other events, as
FreeBSD's knote_enqueue queues it.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
* fix(libkernel): zero sceKernelWaitEqueue's count on every failure as firmware 13.42 does
* docs(libkernel): describe sceKernelWaitEqueue's null event list without naming kevent
Triggers and AddEvent past the 64-event pending cap dropped the oldest queued
events with no record. Each dropped-oldest batch is now counted per queue
(DroppedPendingEvents()) and logged, gated on APS5_TRACE_EQUEUE and
rate-limited to the first drop and each power-of-two of the cumulative total,
so a long burst reports in log2 lines rather than one per drop. The cap value
and the drop-oldest (keep-newest) semantics are unchanged, pending the console
queue-depth / keep-newest-vs-oldest question on #7509.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
A socket EVFILT_READ knote whose NOTE_LOWAT mark is below 1 is now always ready and reports the bytes in the receive buffer, 0 when it is empty, because FreeBSD 11.4 filt_soread returns kn_data >= kn_sdata and the bytes available are never negative. This holds for level-triggered and EV_CLEAR knotes and when EV_ADD changes the mark of an existing knote. A listening socket ignores the mark, as filt_solisten does. An EVFILT_WRITE mark below 1 still throws.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
obSCEne's PS5 run (system software 12.40) returned 0x80020009 for a null
equeue and for one that is not live; the host returned 0 for both,
dropping the EBADF its own add already reported.
Firmware 13.42's libkernel_sys.sprx creates a kqueue for every equeue,
returns (named object id << 32) | descriptor, waits with kevent on the
descriptor and closes it on delete. sceKernelGetKqueueFromEqueue
stores the descriptor. Equeue handles were counters in their own table,
so the descriptor could not be used with kevent or close and the export
threw.
Equeues now reserve a descriptor and live in the kqueue table.
sceKernelDeleteEqueue checks the whole handle and closes the
descriptor, and sceKernelGetKqueueFromEqueue returns its low 32 bits.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- GuestSockets::Ready on Windows reads a datagram socket's next datagram size
with an overlapped MSG_PEEK receive, cancelled if it would wait, since
Winsock's FIONREAD reports 1 for a zero-length datagram.
- Adding an existing socket event again replaces its low watermark.
- A socket low watermark above INT64_MAX throws instead of being capped.
- A write event re-added on a pipe whose read end closed succeeds and replaces
its udata; EPIPE is kept for a new event.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A size of 1 set a watermark of 1, so a readiness with no data, such as
a zero-length datagram, no longer fired.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A size that does not fit in int64_t became negative and let every
readable or writable event through; it now caps at INT64_MAX, which
only end of file reaches. The tests drop the throw assertions for
unimplemented cases and run the pipe write path only where the host
reports pipe write readiness.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceKernelAddWriteEvent takes the write end of a pipe, with EPIPE once
the read end is closed, and a size above 1 sets the low watermark of a
stream socket, as kevent already does for both.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A cleared kqueue watch read the watched queue's triggered count and its
activation counter under two separate locks, so an activation between the
reads was marked seen without making the watch active, and the wakeup was
lost. SnapshotTriggered now returns both under one lock. Every poll of an
active watch also rechecks the count, as kqueue_scan reruns filt_kqueue:
it drops when the watched queue empties and reports the current count.
EV_ADD and EV_ENABLE of an existing watch re-arm it. A separate
guest_kqueue_cleared_race test triggers the watched queue from another
thread while the watching queue waits.
FreeBSD re-evaluates a kqueue's EVFILT_READ note from kqueue_wakeup,
which runs whenever the watched kqueue queues a note, so an EV_CLEAR
note fires again only after such an activation. The equeue now counts
activations, and the EV_CLEAR note fires when the count moves while
notes are queued.
MeasureFiles registered the descriptors it measures in m_measuring and removed them only on the normal path, so an exception between unlocking and relocking left them there and every later RemoveDescriptorEvents for one of them waited forever. Both vectors are reserved before anything is registered, and a guard owns the registration: on every exit it relocks if needed, removes the entries and notifies.
Carries the additive pieces of #6510 onto main after its feature was
superseded by #5847: the deleteEventFunc invocation when an EV_ONESHOT
note retires in CollectTriggered, the Windows $Deleted path detection
in FileEvent (IsDeleted transitions), the relaxed timer-modify and
SetVnodeNotes guards, and the compatible kevent tests.
In payload processes on a PS5 (firmware 12.70), re-adding an existing
periodic EVFILT_TIMER with EV_ADD, fflags 0 and a positive period kept
the armed expiry and the expirations already counted, and applied the
new period after that expiry, as FreeBSD 11.0 does. EV_ONESHOT in the
re-add left the timer periodic. Here such a re-add threw; it now does
the same. Re-adding a one-shot or disabled timer, a re-add with nonzero
fflags, a period of 0 or less, a period that overflows the armed expiry
or flags other than EV_ONESHOT, and the other changes to an existing
timer except EV_DELETE still throw.
There, EV_ADD failed with EPERM for EVFILT_AIO, EVFILT_LIO and
EVFILT_SENDFILE at every ident and flag set tried, and EV_DELETE and
EV_ENABLE of them without EV_ADD failed with ENOENT. EVFILT_PROCDESC
failed with EBADF on a descriptor that is not open and with EINVAL on
every kind of open descriptor tried, and EV_DELETE of it on a socket
failed with ENOENT. These filters threw here; they now answer the same
way. EV_ADD of EVFILT_PROCDESC fails with EINVAL on the kinds of
descriptor tried and on shared memory, and throws on other kinds and on
descriptors 0 to 2 that are not sockets or kqueues; a change without
EV_ADD fails with ENOENT, after the descriptor check for
EVFILT_PROCDESC.
A new kevent measurements debt entry, which the kqueue and kevent entry
points to, records these measurements and the answers for NOTE_LOWAT
marks and EVFILT_READ on another kqueue, which still throw here. The
kqueue and kevent entry also stops listing a NOTE_NSECONDS period above
one second with a fraction among the cases that throw, since d71a1c577
made it work.
FreeBSD 11.4 kqueue_scan (sys/kern/kern_event.c) puts a reported level
knote back at the tail of the queue and takes an EV_CLEAR or EV_DISPATCH
knote off it, and knote_enqueue queues that one at the tail when it fires
again. kevent moved a reported knote to the tail only while it stayed
triggered, so a level socket or pipe knote and a re-fired EV_CLEAR or
EV_DISPATCH knote kept their place and could starve later knotes when
nevents is smaller than the number of ready ones. Now every reported
knote that is not deleted moves to the tail.
Merge the file repoll into GatherInterests through WaitSet, so a wait on a
queue that watches another queue still polls regular-file events every
millisecond.
FreeBSD 11 kqueue_scan (sys/kern/kern_event.c) re-inserts a reported
knote without EV_CLEAR or EV_DISPATCH at the tail of kq_head, and
knote_enqueue puts a knote that fires again at the tail as well, so the
next scan starts with the events that were not reported yet.
sceKernelWaitEqueue scanned the event list in place, so with num smaller
than the number of ready events the first ones were reported on every
call and the later ones starved. Each reported event that stays
registered now moves to the end of the list, in the order reported.
WaitForDescriptors always handed the knote's own descriptor to the host wait. A filter that tracks readiness through a descriptor of its own, such as an edge watch, needs the wait to block on that descriptor instead, or a descriptor that stays ready keeps waking the wait. KernelFilter gets a waitDescriptor, and the filter's owner is held for the length of the wait so the descriptor cannot be closed under it.