Skip to content

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

Closed
p3n1cillin wants to merge 1 commit into
boykopovar:mainfrom
p3n1cillin:claude/sysctl-tsc-freq
Closed

p3n1cillin wants to merge 1 commit into
boykopovar:mainfrom
p3n1cillin:claude/sysctl-tsc-freq

Conversation

@p3n1cillin

Copy link
Copy Markdown

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

PS5SX2 (PPSA99203) reads machdep.tsc_freq at start-up and stopped on the
unsupported name. It reports the host TSC frequency that
sceKernelGetTscFrequency measures, as guest rdtsc reads the host counter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@p3n1cillin

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown

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

  • 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

Copy link
Copy Markdown
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants