On Linux and macOS clock_gettime ids 1 and 2, and id 21 built on id 2,
came from times(), which counts in _SC_CLK_TCK ticks (usually 10 ms),
and threw when times() failed. They now read getrusage(RUSAGE_SELF)
ru_utime and ru_stime in microseconds and set the guest errno on
failure, as FreeBSD 11.4 kern_clock_gettime does through calcru and
TIMEVAL_TO_TIMESPEC.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
GetThreadTimes only advances at the 15.6 ms scheduler tick, so on
Windows the thread CPU clock moved in 15.6 ms steps where clock_getres
reports 1 us, and could read ahead of the monotonic clock under load.
With an invariant TSC, read the calling thread's clock and other guest
threads' CPU clocks from QueryThreadCycleTime, converted with the
measured host TSC frequency.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* feat(libkernel): report the clock resolutions a PS5 measured
* feat(libkernel): clock_getres fails the ids the console rejects with EINVAL
* docs(libkernel): keep the clock_getres debt entry UTF-8 and under the length limit
* fix(libkernel): answer EINVAL for clock ids from 32 in clock_getres
sceKernelWaitSema read steady_clock before and after every wait and passed the difference to KernelTraceWait_nid_postfix, which returns at once unless APS5_TRACE_WAITS is set. The wait now reads the clock only when the variable is set, through one cached flag shared with the trace function. A signal+wait pair drops from 88 ns to 39 ns and from 1909 to 1069 instructions; with a timeout, from 147 ns to 87 ns. The result codes, the wait conditions and the trace output are unchanged.
The measured EIO clock ids use GUEST_EIO in the shared clock_gettime path, but
the constant was declared only inside the Windows block, so non-Windows builds
failed. It now sits with the other guest errno constants.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
clock_gettime and sceKernelClockGettime threw for every clock id they do
not map. PS5 measurements of both exports (firmware 11.60, a payload
process, #16437) settle ids 3, 6 and 16 to 31 but 20 and 21:
- ids 3, 6 and 22 to 31 fail with EINVAL before the result is written,
as both exports did; the raw system call also failed them with EINVAL
(CFI-1216A, firmware 12.00, #15725);
- ids 16 to 19 fail with EIO (errno 5, 0x80020005 from
sceKernelClockGettime) before the result is written, as both exports
did.
Ids 20 and 21 succeed on the console with a time from clocks the probes
do not identify, so they still throw; the debt entry records what was
read and closes when what they count is measured.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sceKernelReadTsc and the TSC frequency measurement read the counter
through GuestCpu::ReadTsc, which is __rdtsc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The conflict in core/libs/tests/GuestRudpContextLifecycle.cpp with #13799 keeps both changes: this branch's removal of the check that sceRudpActivate throws, and main's sceRudpBind(ctx, 5, 0, 2) call after it.
libkernel has its own checked guest copy helpers that call
process_vm_readv/process_vm_writev directly: ReadGuest, WriteGuest and
StoreGuestId in Process.cpp, ReadGuest/WriteGuest in Udp.cpp (also
exported as GuestSocketReadGuest), CopyIovecs in Stdio.cpp and
ReadGuestTime in Time.cpp. On EFAULT they left errno at 14, which is the
guest errno, so a caller that reports the fault through a return code
(sceKernelReadv, or sceKernelGetAppInfo in #13798) still changed the
guest's errno. Windows and macOS do not touch errno there.
Apply the same rule as GuestCopyIn/GuestCopyOut: save errno before the
call and restore it when the call fails with EFAULT. Failures other than
EFAULT behave as before.
The process, thread and clock functions that read /proc or call glibc
extensions:
- The program arguments come from _NSGetArgc and _NSGetArgv instead of
/proc/self/cmdline.
- The current CPU comes from pthread_cpu_number_np.
- getrusage for a thread, the CPU clock of a thread and the CPU time of a
guest thread (clock_gettime with the thread CPU clock) read the Mach
thread_info of the thread. macOS has no pthread_getcpuclockid, and under
Rosetta CLOCK_THREAD_CPUTIME_ID advances by about 1/41.7 of the thread's
CPU time (24 MHz ticks read as nanoseconds), where natively it matches
thread_info.
- The stack limits of a thread come from pthread_get_stackaddr_np and
pthread_get_stacksize_np.
- sleeping with nanosleep restores errno, because macOS leaves ETIMEDOUT
in it on success and the guest's errno is the host's.
The first local of a guest thread lies 4,464 bytes below the top of its
stack on macOS, more than the frame the pthread stack test allowed, so the
test allows two frames there.
Titles that execute rdtsc themselves ignored APS5_TIME_SCALE, because
only sceKernelReadTsc scaled its ticks. The reported TSC frequency is
now divided by the scale and sceKernelReadTsc returns raw ticks, so
ticks over the reported frequency follow the scale for both. The
JobManager worker spin keeps real time through the host frequency.
AI-assisted: yes