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
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.
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
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 <noreply@anthropic.com>
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.
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 <noreply@anthropic.com>
sceKernelInternalMemoryGetAvailableSize with an output, mlockall with valid flags and clock_settime with a valid CLOCK_REALTIME request threw bare exceptions on every such call, so progress.py counted them as done. Internal pool statistics, locking every mapping and setting the clock are not implemented, so those calls now go through NotImplemented_nid_no_patch. The -1, EINVAL and EFAULT answers before them are unchanged.
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>