Commit Graph
86 Commits
Author SHA1 Message Date
Dean b3b449cfe4 Merge pull request #8916 from Henryisdjfunkyho/fix/cancellable-timed-sleeps
fix(libkernel): wake timed sleeps for thread cancellation
2026-10-28 13:48:32 +13:00
Dean a6816811f3 Merge pull request #10975 from xXPBSXx/fix/gettimeofday-itimer-efault
fix(libkernel): fail gettimeofday, getitimer, setitimer and the sceKernel time getters with EFAULT on an unusable pointer
2026-10-28 13:02:14 +13:00
Dean 7f02cb1802 Merge pull request #11402 from 00xDisaster/fix/libkernel-settime-efault
fix(libkernel): check settimeofday and clock_settime pointers
2026-10-27 21:25:30 +13:00
Henryisdjfunkyho 42bbc98235 chore: merge main into cancellable timed sleeps 2026-10-26 17:13:30 -07:00
xXPBSXx 506d69b015 Merge main (e0b13f2990) into fix/gettimeofday-itimer-efault
The only conflict is sceKernelGettimezone in Time.cpp: main now reports the host zone, so the zone is built from that into a local and copied out with the checked write.
2026-10-26 20:49:00 +01:00
Disaster 78571ca2fb fix(libkernel): check settimeofday and clock_settime pointers
Both read their caller's structures in place before validation, so an unreadable pointer crashed the host. Copy them in with a checked read first and return EFAULT, as FreeBSD 11.4 does.

Fixes #11370
2026-10-26 14:29:02 +03:00
frankea 4db6ed7635 Merge remote-tracking branch 'origin/main' into fix/kernel-gettimezone-host-zone
# Conflicts:
#	core/libs/tests/GuestFreebsdClocks.cpp
2026-10-25 20:22:45 -04:00
xXPBSXxandClaude Sonnet 5.5 080c77547c Merge main (49d1f9b07e) into fix/gettimeofday-itimer-efault
The only conflict is in Time.cpp with the guest timezone store of the
settimeofday change: gettimeofday reads the timezone under the mutex into
a local copy, releases it, then copies it out with the checked write.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-26 00:52:14 +01:00
Adrià d19a809639 Merge pull request #9682 from Gruzzberry/feat/settimeofday
feat(libkernel_sys): implement settimeofday
2026-10-25 23:04:11 +01:00
Adrià e1ef22e888 Merge pull request #9461 from milescoviello/fix/tsc-calibration-one-cpu
fix(libkernel): measure the TSC frequency on one CPU
2026-10-25 19:48:55 +01:00
Itamar Medyoni 3db438f997 fix(libkernel): answer process CPU clocks like FreeBSD instead of throwing
A clock id with CPUCLOCK_PROCESS_BIT threw in clock_gettime and
clock_getres. FreeBSD 11 get_cputime reads the process's CPU time, or
fails with EINVAL when pget finds no such process, and kern_clock_getres
gives every CPU clock the CPU time resolution without a lookup. The
guest's own process clock now reads CLOCK_PROCESS_CPUTIME_ID, any other
fails with EINVAL, as kill already finds no other process.
2026-10-25 16:07:10 +01:00
GruzzberryandClaude Opus 5.5 e6b07323de feat(libkernel_sys): implement settimeofday
FreeBSD 11.4's kern_settimeofday rejects a timeval with a microsecond
field outside 0 to 999999 or a negative second field with EINVAL before
changing anything, sets the clock from a valid timeval, and then stores
the timezone, which gettimeofday reports. settimeofday now checks the
timeval the same way and throws for a valid one, as clock_settime does,
because the host clock is not changed. A timezone given without a
timeval is stored and gettimeofday reports it instead of 0 and 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 13:20:31 +00:00
xXPBSXxandClaude Sonnet 5.5 c8430e739c fix(libkernel): fail gettimeofday, getitimer, setitimer and the sceKernel time getters with EFAULT on an unusable pointer
These wrote their result through the caller's pointer in place, or read
the itimerval of setitimer without a check, so a pointer into unmapped
memory crashed the host. Build the result locally and copy it out with
the checked write (and read the setitimer value with a checked read),
failing with EFAULT as FreeBSD 11.4 does: copyout in sys_gettimeofday
(sys/kern/kern_time.c:646 and 651) and sys_getitimer (kern_time.c:745),
copyin (kern_time.c:803) and copyout (kern_time.c:808) in sys_setitimer.

getrlimit and getrusage are left to #8986 and #8988, which cover the same
family for those two calls.

Builds on fix/output-structs-efault, which adds GuestCopyOut_nid_no_patch.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-25 12:22:40 +01:00
milescovielloandClaude Opus 5.5 6508231c0e fix(libkernel): measure the TSC frequency on one CPU
sceKernelGetTscFrequency times 20 ms of TSC ticks against the monotonic
clock. A thread that moves to another CPU during the sleep reads its end
value from another TSC. Where the TSCs of the CPUs differ, as on a Windows
VM whose vCPU 0 counts about 3.8 billion ticks ahead of the others, the
frequency came out as 0 or as 180 GHz. Pin the calibrating thread to the
CPU it runs on and restore its affinity afterwards.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-25 02:24:56 +00:00
Henryisdjfunkyho 5d0ddabbde Merge remote-tracking branch 'origin/main' into fix/cancellable-timed-sleeps
# Conflicts:
#	core/libs/tests/GuestSleepCancel.cpp
2026-10-24 18:06:20 -07:00
Henryisdjfunkyho 4d8f05891b test(libkernel): restore synchronized SCE sleep cancellation coverage 2026-10-24 13:58:16 -07:00
Henryisdjfunkyho 7b7ff0bbae Merge remote-tracking branch 'origin/main' into fix/cancellable-timed-sleeps 2026-10-24 13:46:20 -07:00
boykopovar f38d29fd08 refactor(sdk): add canonical headers for remaining libraries 2026-10-24 23:17:42 +03:00
Emanuele d93cdf402d fix(libkernel): defer raises while a thread holds the address-wait lock on Linux and macOS
On Linux and macOS, sceKernelRaiseException runs the guest handler in
the target's SIGUSR1 handler wherever the thread was interrupted. A
thread interrupted while it held the address-wait lock of
sceKernelSyncOnAddressWait, sceKernelSyncOnAddressWake or _umtx_op ran
the handler with the lock held, so a handler that waits, as a
stop-the-world collector's does, blocked every other thread that waits
on or wakes an address. #5756 fixed this on Windows.

The address-wait lock is now taken under DeliveryDeferral on every
host. On POSIX the deferral is a count per thread: while it is raised
the SIGUSR1 handler only records the signal, and DeliverDeferred sends
it to the thread again once the lock is released. An address waiter
waits on its own semaphore (sem_t, a dispatch semaphore on macOS) with
the lock released and the deferral lifted, so a raise reaches a thread
that waits for an address without a timeout.

The address-lock phases of guest_raise_exception now run on every
host, with a new phase that raises at a thread in an unbounded address
wait.
2026-10-24 08:40:37 +02:00
frankea fabe448517 fix(libkernel): report the host zone's standard offset and daylight saving amount from sceKernelGettimezone
sceKernelGettimezone wrote {0, 0} for every zone. A PS5 on firmware
13.40 set to US Eastern returned tz_minuteswest -300 and tz_dsttime 60:
the standard offset in minutes east of UTC, without the summer time in
effect on the run date, and the zone's daylight saving amount in
minutes. Both now come from the host zone, through the same HostZoneAt
and HostDaylightAmount the calendar conversions use.

guest_freebsd_clocks checks five TZ settings (UTC0, IST-5:30, BRT+3,
EST5EDT, CET-1CEST) and the null pointer.
2026-10-23 23:27:29 -04:00
Dean Galvin c7e2611b6d Merge main into fix/cancellable-timed-sleeps 2026-10-24 15:38:58 +13:00
Dean f075f91737 Merge pull request #9314 from DavidSamir/feat/libkernel-sys-ktimer
feat(libkernel): implement SIGEV_NONE POSIX timers
2026-10-23 20:34:29 +13:00
David 7820a5353a refactor(libkernel): move the ktimer calls into libkernel
The PS5 3.20 export lists have ktimer_* in libkernel as well as
libkernel_sys, and on Windows a libkernel import never reaches
libkernel_sys. The implementation moves unchanged to
libkernel/Time/Ktimer.cpp.
2026-10-21 22:22:16 +01:00
Henryisdjfunkyho 476716546e fix(libkernel): preserve SCE timed sleep behavior 2026-10-21 14:21:16 -07:00
Henryisdjfunkyho bc0f1dc27e fix(libkernel): wake timed sleeps for thread cancellation 2026-10-21 14:11:23 -07:00
Adrià 3d29fe0825 Merge pull request #8487 from xXPBSXx/fix/nanosleep-clamp-long-requests
fix(libkernel): clamp long nanosleep requests like FreeBSD instead of wrapping them
2026-10-21 20:58:12 +02:00
memob 95050dda24 fix(libkernel): tick the process time counter at the TSC frequency
sceKernelGetProcessTimeCounter counted nanoseconds and
sceKernelGetProcessTimeCounterFrequency returned 1 GHz, while
sceKernelGetTscFrequency reports the host TSC rate. On the console the
process time counter runs at the TSC frequency, so a title may scale
the counter by sceKernelGetTscFrequency. PPSA34501 does, and its clock
ran 3.4 times slow on a 3.4 GHz host: it paced its 60 Hz frames to
17.6 fps and its game time and audio drifted apart.

The counter now ticks at sceKernelGetTscFrequency, over the same
monotonic clock and APS5_TIME_SCALE, and its frequency is that rate.
2026-10-20 17:43:18 -04:00
xXPBSXxandClaude Sonnet 5.5 62e6ba7481 fix(libkernel): clamp long nanosleep requests like FreeBSD instead of wrapping them
kern_clock_nanosleep limits the sleep to INT32_MAX / 2 seconds and sleeps
for that long (sys/kern/kern_time.c:543-547). The guest multiplied tv_sec
by 1e9 in 64 bits, so a request of 18446744074 seconds or more wrapped to a
short sleep: nanosleep with tv_sec = INT64_MAX returned after about 0.1 s.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-20 21:24:54 +02:00
Nicolas AboschandClaude Opus 5.5 09093bfa26 fix(libkernel): reset the wake event before a wakeable sleep and keep coarse and sub-50 us sleeps as before
A wake set after the previous wait returned left the per-thread
auto-reset event signalled, so the next short sleep returned from its
wait at once and spun to its deadline. With APS5_COARSE_TIMED_WAITS or
a request under 50 us the sleep now takes TimedWait::SleepNanos as on
main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 11:31:17 -03:00
Nicolas Abosch d22094d066 Merge remote-tracking branch 'origin/main' into perf/gpu-wake-sleeps
# Conflicts:
#	docs/dev/debt/libkernel.md
2026-10-18 16:04:17 -03:00
MarcZX100 e88f7fc157 fix(libkernel): return errno when Windows CPU clock queries fail 2026-10-18 17:14:22 +00:00
MarcZX100 40bc17cb86 fix(libkernel): return errno when Windows CPU clock queries fail 2026-10-18 17:04:35 +00:00
Cesar Rodriguez b771ce4f37 fix(libkernel): name module-relative call sites and thread ids in Linux wait traces
On Linux, APS5_TRACE_WAITS and APS5_TRACE_USLEEP used an image base and a thread
id of 0, so "exe+0x..." printed absolute addresses and every site reported
"tid 0". The call site is now resolved with dladdr1(RTLD_DL_LINKMAP) and printed
relative to its module's load address ("exe+..." for the executable, the module
file name otherwise), matching guest offsets in a disassembly; the thread id is
gettid(). Descriptions are cached per call site, so polling titles do not pay a
dladdr1 per wait. Windows output is unchanged.
2026-10-18 12:10:35 +13:00
mattogepetoandClaude Sonnet 5.5 c17c6a5114 fix(libkernel): return EFAULT from clock_gettime for a null result pointer
clock_gettime threw invalid_argument when the result pointer was null,
before looking at the clock id. FreeBSD 11.4's sys_clock_gettime reads the
clock through kern_clock_gettime, which fails with EINVAL for an unknown
clock, and only then copies the result out, so a null pointer fails with
EFAULT.

Read the clock into a local value first, then fail with -1 and EFAULT when
the pointer is null. A clock that is not supported still throws before the
pointer is looked at. Test the null pointer for the supported clocks.

Written with AI assistance (Claude).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-17 15:33:47 +02:00
Nicolas AboschandClaude Opus 5.5 82dd286192 perf(libkernel): end short sleeps when the GPU or a flip completes
A sleep of 1 ms or less returns once the AGC driver sees a GPU batch
complete (a thread waits on the recorder's timeline semaphore), runs a
batch's completion actions, or VideoOut completes a flip. A thread
returns early at most 8 times in a run of back-to-back sleeps; the run
restarts when the thread submits AGC work or did not sleep for the
requested time. APS5_NO_SLEEP_WAKE=1 keeps every sleep in full.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-15 22:50:57 -03:00
Adrià 4b11b97ebb Merge pull request #5096 from sguckiran/main
fix(libkernel): convert local time with the host time zone
2026-10-14 21:38:24 +02:00
Dean Galvin 52813787cf fix(libkernel): defer raises while a thread holds the address-wait lock on Windows 2026-10-14 23:03:06 +13:00
Claude bb5f7d7e07 Merge remote-tracking branch 'upstream/main' into pr-main
# Conflicts:
#	core/libs/tests/GuestFreebsdClocks.cpp
2026-10-14 06:49:51 +00:00
Evgeniy KazarinandClaude Opus 5.5 99eaf73102 perf(libkernel): wake guest mutex waiters on release on Windows
Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 02:18:33 +03:00
ae9978a98e feat(libkernel): add clock_settime with FreeBSD argument checks
PS5MC (PPSA99420, Kodi 22) imports clock_settime (d7nUj1LOdDU) from
libkernel.prx, one reference, after sigwaitinfo in its import order.
Predicted stop once the imports ahead of it are covered (not a run):

  FAIL: unresolved ELF import d7nUj1LOdDU from libkernel.prx

The NID is computed with tools/nid_names.py and matches aerolib.

Follows FreeBSD 11.4 sys/kern/kern_time.c sys_clock_settime and
kern_clock_settime (8c639945a44588f6cfdddfb057bb5aad88735da0) for a
root caller: EFAULT for a null time (copyin), EINVAL for any clock but
CLOCK_REALTIME and for a negative tv_sec or a tv_nsec outside 0 to
999999999. A valid CLOCK_REALTIME request throws with the requested
time named, since the host clock must not be set and no guest clock
offset is kept.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

Co-authored-by: David <[email protected]>
2026-10-13 14:57:46 +01:00
Claude 15969f2753 fix(libkernel): report the zone's daylight saving amount from sceKernelConvertLocaltimeToUtc
libc mktime subtracts it when tm_isdst is set outside summer time, so it
must not be zero there.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017iLxaSz4qjmirfqKZdhVQk
2026-10-13 12:19:55 +00:00
Claude 1614d82fc8 fix(libkernel): call tzset before reading the host zone offsets
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017iLxaSz4qjmirfqKZdhVQk
2026-10-13 11:48:31 +00:00
Claude ae7f5e3aaa fix(libkernel): report west_sec as seconds east of UTC
PPSA99203 (PS5SX2) documents that the console fills west_sec with the
offset east of UTC despite the field's name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017iLxaSz4qjmirfqKZdhVQk
2026-10-13 11:37:12 +00:00
Claude 56c8f346fb fix(libkernel): convert local time with the host time zone
sceKernelConvertUtcToLocaltime and sceKernelConvertLocaltimeToUtc returned
the time unchanged while libc localtime follows the host zone, so a title
that mixes them shows UTC as local time (#5067).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017iLxaSz4qjmirfqKZdhVQk
2026-10-13 11:27:28 +00:00
Dean 60f4c71608 Merge pull request #4466 from babyhuey/feat/libkernel-socid-cpumode
feat(libkernel): export sceKernelGetMainSocId, sceKernelGetCpumode and sceKernelGetCpuFrequency
2026-10-13 07:27:28 +13:00
Dean fefecc61c2 Merge pull request #3954 from MarcZX100/codex/carry-clock-error
fix(libkernel): return SCE errors when clock calls fail
2026-10-13 02:33:12 +13:00
Hannah Lyons 4e3aa159d9 feat(libkernel): export sceKernelGetMainSocId, sceKernelGetCpumode and sceKernelGetCpuFrequency 2026-10-12 08:32:16 -04:00
Dean 1470522ff3 Merge pull request #3964 from MarcZX100/codex/carry-nanosleep-rmtp
fix(libkernel): leave remaining time untouched when nanosleep completes
2026-10-12 16:11:26 +13:00
Dean f3abf38307 Merge pull request #3855 from Lorkydey/fix/libkernel-utc-dst-width
fix(libkernel): limit UTC-to-local DST output to 32 bits
2026-10-12 16:07:49 +13:00
Hussein Kadhim 8db0f89e7b fix(libkernel): return SCE error codes from sceKernelClockGettime and sceKernelClockGetres
A thread CPU clock whose thread has finished made clock_gettime fail with errno EINVAL, but the sce wrappers passed the raw -1 through to the title. They now return the encoded errno like the other sce kernel calls.
2026-10-11 19:47:03 +00:00