feat(libkernel): answer kern.boottime in sysctl and sysctlbyname #9794

Merged
Retr0G28 merged 1 commit from upstream-pr/9794 into main 2026-10-24 12:58:02 +00:00
Retr0G28 commented 2026-10-22 15:28:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-10-22T15:28:02Z.


What

sysctl threw unsupported MIB 1.21 for kern.boottime ({CTL_KERN, KERN_BOOTTIME}), and sysctlbyname and sysctlnametomib threw for the name. In #9683, Java 25 File I/O (PPSA99866) reads it from HotSpot's os::print_os_info while it writes its hs_err report, and the process aborts at that point instead of writing the report. #9684 fixes the stack-guard error that led to the report in that thread. Any other VM error goes through the same report code, so this is still needed with #9684.

FreeBSD 11.4 declares the node in sys/kern/kern_tc.c as SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD, ...). Its handler, sysctl_kern_boottime, returns a 16-byte struct timeval from getboottime through SYSCTL_OUT. In sys/kern/kern_sysctl.c, sysctl_root returns EPERM for a write to a node without CTLFLAG_WR. sysctl_old_user copies as much as fits and returns ENOMEM when the buffer is short, and userland_sysctl sets the length to what was copied. A size query reports 16. getboottime stays fixed unless the clock is stepped.

sysctl {1, 21}, sysctlbyname("kern.boottime") and sysctlnametomib("kern.boottime") now answer the node. The value is the guest CLOCK_REALTIME minus the guest CLOCK_UPTIME, both read through clock_gettime, so boot time plus uptime matches the current time. It is computed at the first read and then kept. Size queries, short buffers and writes go through the same CopyOut and EPERM paths as the other read-only nodes in the file. A MIB with a component after {1, 21} returns ENOTDIR like the other leaves. A new entry in docs/dev/debt/libkernel.md covers what is left open. A later step of the host clock does not move the value, and the value a PS5 title gets has not been measured.

Test changes in guest_system_configuration:

  • New CheckBootTimeSysctl. A size query reports 16 by MIB and by name. A read gives 0 < tv_sec <= time(nullptr) and 0 <= tv_usec < 1000000, and the value plus clock_gettime(CLOCK_UPTIME) is within one second of clock_gettime(CLOCK_REALTIME). Two reads agree, and the name read agrees with the MIB read. sysctlnametomib gives {1, 21}. An 8-byte buffer gets a length of 8 and ENOMEM with the bytes after it untouched, by MIB and by name, and the MIB read fills it with tv_sec. A write returns EPERM, by MIB and by name.
  • {1, 21} is added to CheckLeafWithTrailingComponent and kern.boottime to CheckNameToMib.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, -DBUILD_TESTING=ON, rebased on main 11f976b27c.

  • Before the fix, guest_system_configuration with the new test fails with terminate called after throwing an instance of 'std::runtime_error', what(): sysctl_nid_postfix: unsupported MIB 1.21 (exit 0xc0000409). With this PR it passes.
  • On this machine the value read was 1792468899.137238. Windows reports LastBootUpTime 1792468899 (Win32_OperatingSystem).
  • Run serially: guest_system_configuration, guest_getrlimit, guest_software_version, guest_thread_sanitizer, guest_hostname, guest_freebsd_clocks, guest_cpu_time_clocks, guest_process_time, guest_process_time_scaled, guest_process_time_counter, guest_process_time_counter_scaled and guest_time_extras. All 12 pass.
  • After the rebase onto 11f976b27c, with the CLOCK_UPTIME check added after review, guest_system_configuration passes. With the implementation changed to subtract only half the uptime, it fails at that check (exit 0xc0000409).
  • python tools/check_conventions.py --base origin/main reports no errors.
  • Not tested on Linux: no Linux machine here. There, guest CLOCK_UPTIME reads the host CLOCK_MONOTONIC, so the value is the host's boot time moved later by any time spent suspended. That keeps kern.boottime + CLOCK_UPTIME = CLOCK_REALTIME, which is how FreeBSD defines it (tc_setclock sets boottimebin to the real time minus binuptime), and the new check tests that relation on every host.
  • No title or homebrew run.

Overlap: tools/pr_overlap.py reports no shared lines with the PRs it could compare. It could not compare #8553 or #6044, and some PRs that touch only other lines of docs/dev/debt/libkernel.md. None of the open PRs that edit SystemConfiguration.cpp (#8910 unknown names, #9736 kern.proc.env, #8553 MIB mappings) implements kern.boottime. #8910 returns ENOENT for unknown names but still throws for an unknown MIB such as {1, 21}. Textual conflicts are possible:

  • #8553 adds names right after kern.rng_pseudo in the CheckNameToMib table, next to the line this PR adds.
  • #9736 adds a branch to sysctl_nid_postfix and a call in main() a few lines from this PR's additions.

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: PPSA99866 #9683
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4969879392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9794). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-10-22T15:28:02Z. --- ### What `sysctl` threw `unsupported MIB 1.21` for `kern.boottime` ({`CTL_KERN`, `KERN_BOOTTIME`}), and `sysctlbyname` and `sysctlnametomib` threw for the name. In #9683, Java 25 File I/O (PPSA99866) reads it from HotSpot's `os::print_os_info` while it writes its `hs_err` report, and the process aborts at that point instead of writing the report. #9684 fixes the stack-guard error that led to the report in that thread. Any other VM error goes through the same report code, so this is still needed with #9684. FreeBSD 11.4 declares the node in `sys/kern/kern_tc.c` as `SYSCTL_PROC(_kern, KERN_BOOTTIME, boottime, CTLTYPE_STRUCT|CTLFLAG_RD, ...)`. Its handler, `sysctl_kern_boottime`, returns a 16-byte `struct timeval` from `getboottime` through `SYSCTL_OUT`. In `sys/kern/kern_sysctl.c`, `sysctl_root` returns `EPERM` for a write to a node without `CTLFLAG_WR`. `sysctl_old_user` copies as much as fits and returns `ENOMEM` when the buffer is short, and `userland_sysctl` sets the length to what was copied. A size query reports 16. `getboottime` stays fixed unless the clock is stepped. `sysctl` {1, 21}, `sysctlbyname("kern.boottime")` and `sysctlnametomib("kern.boottime")` now answer the node. The value is the guest `CLOCK_REALTIME` minus the guest `CLOCK_UPTIME`, both read through `clock_gettime`, so boot time plus uptime matches the current time. It is computed at the first read and then kept. Size queries, short buffers and writes go through the same `CopyOut` and `EPERM` paths as the other read-only nodes in the file. A MIB with a component after {1, 21} returns `ENOTDIR` like the other leaves. A new entry in `docs/dev/debt/libkernel.md` covers what is left open. A later step of the host clock does not move the value, and the value a PS5 title gets has not been measured. Test changes in `guest_system_configuration`: - New `CheckBootTimeSysctl`. A size query reports 16 by MIB and by name. A read gives `0 < tv_sec <= time(nullptr)` and `0 <= tv_usec < 1000000`, and the value plus `clock_gettime(CLOCK_UPTIME)` is within one second of `clock_gettime(CLOCK_REALTIME)`. Two reads agree, and the name read agrees with the MIB read. `sysctlnametomib` gives {1, 21}. An 8-byte buffer gets a length of 8 and `ENOMEM` with the bytes after it untouched, by MIB and by name, and the MIB read fills it with `tv_sec`. A write returns `EPERM`, by MIB and by name. - {1, 21} is added to `CheckLeafWithTrailingComponent` and `kern.boottime` to `CheckNameToMib`. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0 r7, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, rebased on `main` 11f976b27c. - Before the fix, `guest_system_configuration` with the new test fails with `terminate called after throwing an instance of 'std::runtime_error'`, `what(): sysctl_nid_postfix: unsupported MIB 1.21` (exit 0xc0000409). With this PR it passes. - On this machine the value read was 1792468899.137238. Windows reports `LastBootUpTime` 1792468899 (`Win32_OperatingSystem`). - Run serially: `guest_system_configuration`, `guest_getrlimit`, `guest_software_version`, `guest_thread_sanitizer`, `guest_hostname`, `guest_freebsd_clocks`, `guest_cpu_time_clocks`, `guest_process_time`, `guest_process_time_scaled`, `guest_process_time_counter`, `guest_process_time_counter_scaled` and `guest_time_extras`. All 12 pass. - After the rebase onto 11f976b27c, with the `CLOCK_UPTIME` check added after review, `guest_system_configuration` passes. With the implementation changed to subtract only half the uptime, it fails at that check (exit 0xc0000409). - `python tools/check_conventions.py --base origin/main` reports no errors. - Not tested on Linux: no Linux machine here. There, guest `CLOCK_UPTIME` reads the host `CLOCK_MONOTONIC`, so the value is the host's boot time moved later by any time spent suspended. That keeps `kern.boottime` + `CLOCK_UPTIME` = `CLOCK_REALTIME`, which is how FreeBSD defines it (`tc_setclock` sets `boottimebin` to the real time minus `binuptime`), and the new check tests that relation on every host. - No title or homebrew run. Overlap: `tools/pr_overlap.py` reports no shared lines with the PRs it could compare. It could not compare #8553 or #6044, and some PRs that touch only other lines of `docs/dev/debt/libkernel.md`. None of the open PRs that edit `SystemConfiguration.cpp` (#8910 unknown names, #9736 `kern.proc.env`, #8553 MIB mappings) implements `kern.boottime`. #8910 returns `ENOENT` for unknown names but still throws for an unknown MIB such as {1, 21}. Textual conflicts are possible: - #8553 adds names right after `kern.rng_pseudo` in the `CheckNameToMib` table, next to the line this PR adds. - #9736 adds a branch to `sysctl_nid_postfix` and a call in `main()` a few lines from this PR's additions. ### 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 - [x] Fixes: none - [x] Unblocks: PPSA99866 #9683 - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-22 16:21:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:21:57Z.

Review: DISMISSED


The source changes and added MIB/name tests look consistent with the existing sysctl paths. I configured the exact PR head and built guest_system_configuration_tests successfully. The focused CTest could not execute: libkernel stops at its documented host CPU gate because this Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE and AVX2. The PR reports Windows 11 tests passing, while Linux behavior remains untested; the new value uses Linux CLOCK_MONOTONIC for guest uptime, so please confirm the focused test on a supported Linux host. Hosted checks currently cover eligibility, conventions, intake, labels and report; full product builds were skipped.

The branch is eight commits behind current main (8499ea52e571b0edfc1feecfd3f6b9944012b359). CONTRIBUTING.md requires branches to be based on and kept up to date with main. Please sync it, resolve any conflicts, and provide the Linux focused-test result before merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617456765 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9794#pullrequestreview-5617456765). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:21:57Z. **Review:** DISMISSED --- The source changes and added MIB/name tests look consistent with the existing sysctl paths. I configured the exact PR head and built `guest_system_configuration_tests` successfully. The focused CTest could not execute: libkernel stops at its documented host CPU gate because this Xeon E5-1620 v2 lacks LZCNT, BMI1, BMI2, MOVBE and AVX2. The PR reports Windows 11 tests passing, while Linux behavior remains untested; the new value uses Linux `CLOCK_MONOTONIC` for guest uptime, so please confirm the focused test on a supported Linux host. Hosted checks currently cover eligibility, conventions, intake, labels and report; full product builds were skipped. The branch is eight commits behind current `main` (`8499ea52e571b0edfc1feecfd3f6b9944012b359`). CONTRIBUTING.md requires branches to be based on and kept up to date with `main`. Please sync it, resolve any conflicts, and provide the Linux focused-test result before merge.
MarcZX10 commented 2026-10-22 16:55:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:55:21Z.

Review: COMMENTED


I reviewed the current exact head b95092a431d60c2688335c3b75bfb1f3c8e43b32. Conventions and git diff --check pass, and the merge tree with current main is clean. The new tests cover MIB and name lookup, cached value, size query, short buffers, writes and leaf handling. The reported Windows runs are contributor-reported; hosted product jobs are skipped. The branch remains 21 commits behind current main (11f976b27c16ed91633f00039dba5dfd97dd7cc1), and the PR still reports no Linux test. That matters for this implementation because BootTime() computes realtime minus guest CLOCK_UPTIME, while the existing Linux clock mapping uses CLOCK_MONOTONIC; please rebase and provide the focused Linux test result before merge. I could not rerun the libkernel test locally because this VM’s CPU lacks the required feature set.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617841412 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9794#pullrequestreview-5617841412). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:55:21Z. **Review:** COMMENTED --- I reviewed the current exact head `b95092a431d60c2688335c3b75bfb1f3c8e43b32`. Conventions and `git diff --check` pass, and the merge tree with current `main` is clean. The new tests cover MIB and name lookup, cached value, size query, short buffers, writes and leaf handling. The reported Windows runs are contributor-reported; hosted product jobs are skipped. The branch remains 21 commits behind current `main` (`11f976b27c16ed91633f00039dba5dfd97dd7cc1`), and the PR still reports no Linux test. That matters for this implementation because `BootTime()` computes realtime minus guest `CLOCK_UPTIME`, while the existing Linux clock mapping uses `CLOCK_MONOTONIC`; please rebase and provide the focused Linux test result before merge. I could not rerun the libkernel test locally because this VM’s CPU lacks the required feature set.
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!9794
No description provided.