fix(libkernel): implement clock_gettime for clock id 20 #18466

Merged
THEMANnt merged 1 commit from upstream-pr/18466 into main 2026-11-13 10:26:47 +00:00
THEMANnt commented 2026-11-11 17:03:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-11-11T17:03:42Z.


What

Implement clock ID 20 for clock_gettime and sceKernelClockGettime. Bring-up reports record a startup abort on this clock in Age of Mythology: Retold (PPSA18653) and Star Wars: Galactic Racer (PPSA28416). Age of Mythology report · Galactic Racer report

The firmware 13.590.001 payload readings advance by 7.8125 ms per half second. Their step values fit 1/32768-second ticks truncated to microseconds. The implementation uses the host monotonic clock as its origin. The console's clock origin and retail-export behavior remain unverified. The older firmware 12.00 raw syscall probe returned EINVAL. Console measurements

Tested

  • Windows x64, WinLibs GCC 15.2. Built the focused clock tests and their libkernel dependency.
  • The regression checks both entry points against monotonic time at the measured rate, with microsecond precision.
  • CTest passed guest_freebsd_clocks, guest_cpu_time_clocks, guest_thread_cpu_clock and public_headers. Repository conventions passed.
  • This branch was verified with the regression test. Retail title runs are pending.

The overlap checker scanned 1,959 open PRs. Review of the flagged diffs found nearby test and debt edits in #17551 and #17958. Those changes may require a rebase depending on merge order.

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: addresses the reported clock 20 stop for PPSA18653 #15328 and PPSA28416 #357. Retail retest pending.
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5263539065 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18466). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-11-11T17:03:42Z. --- ### What Implement clock ID 20 for `clock_gettime` and `sceKernelClockGettime`. Bring-up reports record a startup abort on this clock in Age of Mythology: Retold (PPSA18653) and Star Wars: Galactic Racer (PPSA28416). [Age of Mythology report](https://github.com/boykopovar/AnyPS5/issues/15725) · [Galactic Racer report](https://github.com/boykopovar/AnyPS5/discussions/357) The firmware 13.590.001 payload readings advance by 7.8125 ms per half second. Their step values fit 1/32768-second ticks truncated to microseconds. The implementation uses the host monotonic clock as its origin. The console's clock origin and retail-export behavior remain unverified. The older firmware 12.00 raw syscall probe returned EINVAL. [Console measurements](https://github.com/boykopovar/AnyPS5/issues/15725) ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Windows x64, WinLibs GCC 15.2. Built the focused clock tests and their libkernel dependency. - The regression checks both entry points against monotonic time at the measured rate, with microsecond precision. - CTest passed `guest_freebsd_clocks`, `guest_cpu_time_clocks`, `guest_thread_cpu_clock` and `public_headers`. Repository conventions passed. - This branch was verified with the regression test. Retail title runs are pending. The overlap checker scanned 1,959 open PRs. Review of the flagged diffs found nearby test and debt edits in #17551 and #17958. Those changes may require a rebase depending on merge order. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: addresses the reported clock 20 stop for PPSA18653 #15328 and PPSA28416 #357. Retail retest pending. <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-11 17:26:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T17:26:55Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 3007fe3334:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_freebsd_clocks_tests built cleanly.
  • CTest (guest_freebsd_clocks):
    1/1 Test #4383: guest_freebsd_clocks .............   Passed    0.51 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Time/Time.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4383: guest_freebsd_clocks .............Subprocess aborted***Exception:   0.38 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked ReadClock(SLOW_CLOCK_ID, tp) in core/libs/prx/libkernel/Time/Time.cpp: verified arithmetic scaling (rateDivisor = 64, ticksPerSecond = 32768, monotonicTicksPerSecond = 512) from monotonic clock 4, confirming int64_t calculations prevent integer overflow in ticks * 1000000, keep tv_nsec within [0, 999969000] < 10^9, and maintain strict monotonic progression across 64-second rollover boundaries.
    • Checked error handling and dispatch in clock_gettime_nid_postfix and sceKernelClockGettime in core/libs/prx/libkernel/Time/Time.cpp: verified clock ID 20 is permitted past MeasuredInvalidClock (clock IDs 3, 6, 22-31) and MeasuredIoErrorClock (clock IDs 16-19), and verified null pointer handling returns GUEST_EFAULT (errno 14) and SCE_KERNEL_ERROR_EFAULT (0x8002000e).
    • Checked concurrency and reentrancy in core/libs/prx/libkernel/Time/Time.cpp: verified ReadClock(20) uses a stack-local KernelTimespec and safely delegates to ReadClock(4, &monotonic) without acquiring unneeded locks or introducing shared mutable state races.
    • Checked LP64 and LLP64 portability in core/libs/prx/libkernel/Time/Time.cpp: confirmed all constants, variables, and KernelTimespec struct fields use fixed-width std::int64_t without signed/unsigned conversion issues or platform-dependent type mismatches.
    • Checked regression test coverage in core/libs/tests/GuestFreebsdClocks.cpp: verified Clock20UsesScaledMonotonicTime() validates both clock_gettime_nid_postfix and sceKernelClockGettime against GUEST_CLOCK_MONOTONIC / 64 within tickCeilingNanos tolerance and checks microsecond truncation (multiple of 1000).
    • Checked AGENTS.md compliance: confirmed no comments were introduced in modified code, conventional commit formatting is adhered to, and technical debt gaps regarding clock ID 20 and clock_getres are accurately updated in docs/dev/debt/libkernel.md.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5862778309 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18466#pullrequestreview-5862778309). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T17:26:55Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `3007fe3334`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_freebsd_clocks_tests` built cleanly. - **CTest** (`guest_freebsd_clocks`): ``` 1/1 Test #4383: guest_freebsd_clocks ............. Passed 0.51 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Time/Time.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4383: guest_freebsd_clocks .............Subprocess aborted***Exception: 0.38 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Checked ReadClock(SLOW_CLOCK_ID, tp) in core/libs/prx/libkernel/Time/Time.cpp: verified arithmetic scaling (rateDivisor = 64, ticksPerSecond = 32768, monotonicTicksPerSecond = 512) from monotonic clock 4, confirming int64_t calculations prevent integer overflow in ticks * 1000000, keep tv_nsec within [0, 999969000] < 10^9, and maintain strict monotonic progression across 64-second rollover boundaries. - Checked error handling and dispatch in clock_gettime_nid_postfix and sceKernelClockGettime in core/libs/prx/libkernel/Time/Time.cpp: verified clock ID 20 is permitted past MeasuredInvalidClock (clock IDs 3, 6, 22-31) and MeasuredIoErrorClock (clock IDs 16-19), and verified null pointer handling returns GUEST_EFAULT (errno 14) and SCE_KERNEL_ERROR_EFAULT (0x8002000e). - Checked concurrency and reentrancy in core/libs/prx/libkernel/Time/Time.cpp: verified ReadClock(20) uses a stack-local KernelTimespec and safely delegates to ReadClock(4, &monotonic) without acquiring unneeded locks or introducing shared mutable state races. - Checked LP64 and LLP64 portability in core/libs/prx/libkernel/Time/Time.cpp: confirmed all constants, variables, and KernelTimespec struct fields use fixed-width std::int64_t without signed/unsigned conversion issues or platform-dependent type mismatches. - Checked regression test coverage in core/libs/tests/GuestFreebsdClocks.cpp: verified Clock20UsesScaledMonotonicTime() validates both clock_gettime_nid_postfix and sceKernelClockGettime against GUEST_CLOCK_MONOTONIC / 64 within tickCeilingNanos tolerance and checks microsecond truncation (multiple of 1000). - Checked AGENTS.md compliance: confirmed no comments were introduced in modified code, conventional commit formatting is adhered to, and technical debt gaps regarding clock ID 20 and clock_getres are accurately updated in docs/dev/debt/libkernel.md. - Note: the AI-assisted checkbox can stay. Looks Good To Me
MarcZX10 commented 2026-11-12 14:26:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T14:26:59Z.

Review: COMMENTED


Reviewed — no blocking findings

I found no blocking defect in the clock 20 code I read at head 3007fe3334e27a49680552cd886f4cfd0701e216. The regression test file was excluded from this review, so its contents are not verified.

What I ran

  • Head SHA: 3007fe3334e27a49680552cd886f4cfd0701e216, base main.
  • Read the console-prepared description.md, commits.md, manifest.json, diff.patch and files.json.
  • Read repository text at head with the reader only (search and cat, about 11 calls).
  • No build and no test run. This review is by reading only.

Results

Check Result Note
Clock 20 arithmetic passed Read by hand; monotonic-derived, non-decreasing
Clock 20 reaches clock_gettime passed Dispatch at Time.cpp:644; slow-clock branch runs before the CPU-clock test
Build and CTest not run Author-reported only
Regression test file not run Excluded by rule
Reader calls (unedited)
repo_read.py search ReadClock core/libs/prx/libkernel/Time/Time.cpp
repo_read.py search IsCpuClock core/libs/prx/libkernel/Time/Time.cpp
repo_read.py search CPU_CLOCK_BIT core/libs/prx/libkernel/Time/Time.cpp
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 80 92
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 440 560
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 560 640
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 641 700
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 700 716
repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 716 730

Findings

Blocking

None.

Non-blocking

  • docs/dev/debt/libkernel.md:35 (removed line, diff.patch line 35): the entry drops the firmware 11.60 observation for id 20 (a constant 2033.63095 s over three reads, and 79,200 s below uptime). The new entry at diff.patch line 39 cites only 13.590.001. The 11.60 reading conflicts with the new monotonic-derived origin, so the conflict should stay recorded until it is resolved. Keep the removed evidence, or move it to a line that says it contradicts the current model.
  • docs/dev/debt/libkernel.md:40 (diff.patch line 40): the entry says clock_getres for id 20 is measured as EINVAL, but the code still throws for it. On the Windows branch this is Time.cpp:723. The same PR could return GUEST_EINVAL for id 20 in clock_getres, which matches the measurement the entry cites.

Sources

Not verified

  • core/libs/tests/GuestFreebsdClocks.cpp is excluded from this review. The exclusion rule *lock* matches the name because "Clocks" contains "lock", so the rule excludes a regression test by accident. The test changes are not reviewed here.
  • The CTest results (guest_freebsd_clocks, guest_cpu_time_clocks, guest_thread_cpu_clock, public_headers), the WinLibs GCC 15.2 build, and "Repository conventions passed" are author-reported. I did not run them.
  • The console values (1/64 rate, the 1/32768 s step, the origin) are inferred from the 13.590.001 payload readings. The code cannot confirm them, and the PR says so too.
  • Time.cpp was read at head only in parts (lines 80–92 and 440–730 of 909). The Linux branch of clock_getres for id 20 was not read, so the "throws" claim there is unchecked.
  • docs/dev/debt/libkernel.md was read only through its changed hunk, not in full.
  • CPU_CLOCK_BIT was not located in Time.cpp. It does not change the result, because the id 20 branch runs before the CPU-clock test.
  • The overlap with #17551 and #17958 (rebase order) was not checked.

To get this to a good state

  • Keep or relocate the removed firmware 11.60 evidence for id 20 (docs/dev/debt/libkernel.md:35).
  • Decide whether clock_getres for id 20 should return EINVAL (docs/dev/debt/libkernel.md:40).
  • Author: rerun the focused clock tests and attach the output on the new head, since the regression test file was not reviewed here.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5876111681 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18466#pullrequestreview-5876111681). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-12T14:26:59Z. **Review:** COMMENTED --- # Reviewed — no blocking findings I found no blocking defect in the clock 20 code I read at head `3007fe3334e27a49680552cd886f4cfd0701e216`. The regression test file was excluded from this review, so its contents are not verified. ## What I ran - Head SHA: `3007fe3334e27a49680552cd886f4cfd0701e216`, base `main`. - Read the console-prepared `description.md`, `commits.md`, `manifest.json`, `diff.patch` and `files.json`. - Read repository text at head with the reader only (`search` and `cat`, about 11 calls). - No build and no test run. This review is by reading only. ## Results | Check | Result | Note | |---|---|---| | Clock 20 arithmetic | passed | Read by hand; monotonic-derived, non-decreasing | | Clock 20 reaches `clock_gettime` | passed | Dispatch at `Time.cpp:644`; slow-clock branch runs before the CPU-clock test | | Build and CTest | not run | Author-reported only | | Regression test file | not run | Excluded by rule | <details> <summary>Reader calls (unedited)</summary> ``` repo_read.py search ReadClock core/libs/prx/libkernel/Time/Time.cpp repo_read.py search IsCpuClock core/libs/prx/libkernel/Time/Time.cpp repo_read.py search CPU_CLOCK_BIT core/libs/prx/libkernel/Time/Time.cpp repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 80 92 repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 440 560 repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 560 640 repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 641 700 repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 700 716 repo_read.py cat core/libs/prx/libkernel/Time/Time.cpp 716 730 ``` </details> ## Findings **Blocking** None. **Non-blocking** - `docs/dev/debt/libkernel.md:35` (removed line, `diff.patch` line 35): the entry drops the firmware 11.60 observation for id 20 (a constant 2033.63095 s over three reads, and 79,200 s below uptime). The new entry at `diff.patch` line 39 cites only 13.590.001. The 11.60 reading conflicts with the new monotonic-derived origin, so the conflict should stay recorded until it is resolved. Keep the removed evidence, or move it to a line that says it contradicts the current model. - `docs/dev/debt/libkernel.md:40` (`diff.patch` line 40): the entry says `clock_getres` for id 20 is measured as `EINVAL`, but the code still throws for it. On the Windows branch this is `Time.cpp:723`. The same PR could return `GUEST_EINVAL` for id 20 in `clock_getres`, which matches the measurement the entry cites. ## Sources - [`Time.cpp` at head, lines 500–514](https://github.com/boykopovar/AnyPS5/blob/3007fe3334e27a49680552cd886f4cfd0701e216/core/libs/prx/libkernel/Time/Time.cpp#L500-L514): the clock 20 branch. - [`Time.cpp` at head, lines 634–651](https://github.com/boykopovar/AnyPS5/blob/3007fe3334e27a49680552cd886f4cfd0701e216/core/libs/prx/libkernel/Time/Time.cpp#L634-L651): `clock_gettime` dispatch. - [`Time.cpp` at head, lines 683–723](https://github.com/boykopovar/AnyPS5/blob/3007fe3334e27a49680552cd886f4cfd0701e216/core/libs/prx/libkernel/Time/Time.cpp#L683-L723): `clock_getres` for unsupported ids. ## Not verified - `core/libs/tests/GuestFreebsdClocks.cpp` is excluded from this review. The exclusion rule `*lock*` matches the name because "Clocks" contains "lock", so the rule excludes a regression test by accident. The test changes are not reviewed here. - The CTest results (`guest_freebsd_clocks`, `guest_cpu_time_clocks`, `guest_thread_cpu_clock`, `public_headers`), the WinLibs GCC 15.2 build, and "Repository conventions passed" are author-reported. I did not run them. - The console values (1/64 rate, the 1/32768 s step, the origin) are inferred from the 13.590.001 payload readings. The code cannot confirm them, and the PR says so too. - `Time.cpp` was read at head only in parts (lines 80–92 and 440–730 of 909). The Linux branch of `clock_getres` for id 20 was not read, so the "throws" claim there is unchecked. - `docs/dev/debt/libkernel.md` was read only through its changed hunk, not in full. - `CPU_CLOCK_BIT` was not located in `Time.cpp`. It does not change the result, because the id 20 branch runs before the CPU-clock test. - The overlap with #17551 and #17958 (rebase order) was not checked. ## To get this to a good state - Keep or relocate the removed firmware 11.60 evidence for id 20 (`docs/dev/debt/libkernel.md:35`). - Decide whether `clock_getres` for id 20 should return `EINVAL` (`docs/dev/debt/libkernel.md:40`). - Author: rerun the focused clock tests and attach the output on the new head, since the regression test file was not reviewed here.
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!18466
No description provided.