feat(libkernel): report machdep.tsc_freq through sysctlbyname #5078

Closed
p3n1cillin wants to merge 1 commit from upstream-pr/5078 into main
p3n1cillin commented 2026-10-13 10:56:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by p3n1cillin on 2026-10-13T10:56:05Z.


What

PS5SX2 (PPSA99203, catalog release) stops right after its "starting" notification on Windows:

terminate called after throwing an instance of 'std::runtime_error'
  what():  sysctlbyname_nid_postfix: unsupported name machdep.tsc_freq

The caller is its boot-time clock survey (guest 0x7428E, next to its [cpuclk] boot: measured TSC ... and [console] CPU: %llu Hz strings). Read with gdb at sysctlbyname and disassembled, it calls sysctlbyname("machdep.tsc_freq", &u64, &len = 8, NULL, 0). It uses the value when the call returns 0 and the value is above 100 MHz; otherwise it measures the TSC against its monotonic clock for 50 ms. The repository's main-boot.cpp has the other call, which also reads machdep.tsc_freq as an unsigned long long. That one runs only when sceKernelGetCpuFrequency returns 0, which AnyPS5's never does.

machdep.tsc_freq is FreeBSD's 64-bit sysctl_machdep_tsc_freq, present on every amd64 kernel with a TSC. sysctlbyname now reports the host TSC frequency that sceKernelGetTscFrequency already measures, because guest rdtsc reads the host counter. It uses the existing CopyOut, so a null buffer gets the length 8 and a short buffer gets a partial copy and ENOMEM, as sysctl_handle_64 does. A write, which FreeBSD would take as a new frequency, throws. The console's value is unknown; this is recorded under Unknown function info in docs/dev/debt/libkernel.md.

Tested

  • PS5SX2 PPSA99203 on Windows 11 (MinGW GCC 15.2.0), from the catalog release. The relinker refuses it without --trap-sdk-syscall-stubs (a forbidden inline syscall), so this run used that debugging flag; no trapped instruction was reached. Before, it stops as above within 1 s. After, it runs for the whole 90 s run and keeps rewriting its data/PCSX2/ps5sx2-alive.txt heartbeat; nothing else reached was checked.
  • guest_system_configuration: the length query, the value (nonzero and equal to sceKernelGetTscFrequency, with errno untouched), a 4-byte buffer (ENOMEM, 4 bytes copied, the rest untouched) and a throwing write. All 124 libkernel tests pass on Windows; Linux was not built locally.
  • python tools/check_conventions.py --base origin/main and tools/pr_overlap.py pass.

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
  • Unblocks: PS5SX2 (PPSA99203) start-up, when relinked with --trap-sdk-syscall-stubs
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4842158314 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5078). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-13T10:56:05Z. --- ### What PS5SX2 (PPSA99203, catalog release) stops right after its "starting" notification on Windows: ``` terminate called after throwing an instance of 'std::runtime_error' what(): sysctlbyname_nid_postfix: unsupported name machdep.tsc_freq ``` The caller is its boot-time clock survey (guest 0x7428E, next to its `[cpuclk] boot: measured TSC ...` and `[console] CPU: %llu Hz` strings). Read with gdb at `sysctlbyname` and disassembled, it calls `sysctlbyname("machdep.tsc_freq", &u64, &len = 8, NULL, 0)`. It uses the value when the call returns 0 and the value is above 100 MHz; otherwise it measures the TSC against its monotonic clock for 50 ms. The repository's [main-boot.cpp](https://github.com/Swordpdf/PS5SX2/blob/b4cf55e87c34e8a2a1cd9302df4373e5a94d28f4/ps5/coreorbis/main-boot.cpp#L1920-L1932) has the other call, which also reads `machdep.tsc_freq` as an `unsigned long long`. That one runs only when `sceKernelGetCpuFrequency` returns 0, which AnyPS5's never does. `machdep.tsc_freq` is FreeBSD's 64-bit [`sysctl_machdep_tsc_freq`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/x86/x86/tsc.c#L717-L736), present on every amd64 kernel with a TSC. `sysctlbyname` now reports the host TSC frequency that `sceKernelGetTscFrequency` already measures, because guest `rdtsc` reads the host counter. It uses the existing `CopyOut`, so a null buffer gets the length 8 and a short buffer gets a partial copy and `ENOMEM`, as `sysctl_handle_64` does. A write, which FreeBSD would take as a new frequency, throws. The console's value is unknown; this is recorded under Unknown function info in `docs/dev/debt/libkernel.md`. ### Tested - PS5SX2 PPSA99203 on Windows 11 (MinGW GCC 15.2.0), from the catalog release. The relinker refuses it without `--trap-sdk-syscall-stubs` (a forbidden inline syscall), so this run used that debugging flag; no trapped instruction was reached. Before, it stops as above within 1 s. After, it runs for the whole 90 s run and keeps rewriting its `data/PCSX2/ps5sx2-alive.txt` heartbeat; nothing else reached was checked. - `guest_system_configuration`: the length query, the value (nonzero and equal to `sceKernelGetTscFrequency`, with `errno` untouched), a 4-byte buffer (`ENOMEM`, 4 bytes copied, the rest untouched) and a throwing write. All 124 libkernel tests pass on Windows; Linux was not built locally. - `python tools/check_conventions.py --base origin/main` and `tools/pr_overlap.py` pass. ### Checklist - [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 - [x] Unblocks: PS5SX2 (PPSA99203) start-up, when relinked with `--trap-sdk-syscall-stubs` - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
p3n1cillin commented 2026-10-13 11:59:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by p3n1cillin on 2026-10-13T11:59:08Z.


Linux run of this change: Ubuntu in WSL2, GCC 15.2.0, Ninja Release, BUILD_TESTING=ON, full build. Built from a local integration head a14a49d8d, which is main 5d50886ee with this PR and my other open library PRs merged (#5013, #5009, #5061, #5078, #5098; no conflicts). All 121 libkernel tests and all 68 libc tests pass, including guest_system_configuration, guest_getrlimit and guest_select. Not run alone on Linux; no title run on Linux.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145033717 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5078#issuecomment-6145033717). Originally posted by [p3n1cillin](https://github.com/p3n1cillin) on 2026-10-13T11:59:08Z. --- Linux run of this change: Ubuntu in WSL2, GCC 15.2.0, Ninja Release, `BUILD_TESTING=ON`, full build. Built from a local integration head `a14a49d8d`, which is `main` `5d50886ee` with this PR and my other open library PRs merged (#5013, #5009, #5061, #5078, #5098; no conflicts). All 121 libkernel tests and all 68 libc tests pass, including `guest_system_configuration`, `guest_getrlimit` and `guest_select`. Not run alone on Linux; no title run on Linux.
coltken95 commented 2026-10-13 12:48:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-13T12:48:30Z.


Tested on Linux x86-64 (Release build), with 46f3d779e cherry-picked onto main 14036cb0e:

  • With the PR: guest_system_configuration passes.
  • With main's SystemConfiguration.cpp and the PR's test it aborts with
    sysctlbyname_nid_postfix: unsupported name machdep.tsc_freq

It also cherry-picks cleanly together with #5098, which edits the same files.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6145903668 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5078#issuecomment-6145903668). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-13T12:48:30Z. --- Tested on Linux x86-64 (Release build), with 46f3d779e cherry-picked onto main 14036cb0e: - With the PR: `guest_system_configuration` passes. - With main's `SystemConfiguration.cpp` and the PR's test it aborts with `sysctlbyname_nid_postfix: unsupported name machdep.tsc_freq` It also cherry-picks cleanly together with #5098, which edits the same files.
oneandonlydean commented 2026-10-14 01:13:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:13:51Z.


Closing: same change as #4776, which came first and is now merged. Reopen if something here is still missing on current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6158659947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5078#issuecomment-6158659947). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T01:13:51Z. --- Closing: same change as #4776, which came first and is now merged. Reopen if something here is still missing on current main.

Pull request closed

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!5078
No description provided.