Skip to content

fix(libkernel): answer sched_rr_get_interval with 4 ms and accept pid 1 as the console does - #22219

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/kernel-sys-sched-rr-interval-4ms
Nov 27, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
xXPBSXx:fix/kernel-sys-sched-rr-interval-4ms

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

What

sched_rr_get_interval in libkernel_sys/Export.cpp returned the FreeBSD 11 ULE default time slice of 94 ms and failed with ESRCH for every pid except 0 and the process's own. A PS5 payload process on firmware 11.60 called the console's sched_rr_get_interval through the libkernel export and got 4 ms (sec 0, nsec 4000000, 16 bytes written) for pid 0, for its own pid and for pid 1 (cases 1.1 to 1.3, lines 5 to 7 of the raw output). The pids 0x7fffffff and -1 returned -1 with errno 3 (ESRCH) and left the interval untouched (cases 1.4 and 1.5, lines 8 and 9), and a null interval returned -1 with errno 14 (EFAULT; case 1.6, line 10). The function now returns {0, 4000000} for pid 0, pid 1 and the own pid and keeps ESRCH for the others; the null interval still gives EFAULT. The entry in docs/dev/debt/libkernel_sys.md said that the console-specific clock rates and quantum configuration were unverified.

The 4 ms is the value the console reported to this process; this library has no guest scheduler and no per-process quantum, so the number is the console's reported value and not runtime scheduling, as the entry says. The test commit changes the three expectations of 94 ms in GuestSchedPriority.cpp to 4 ms and adds a check for pid 1; the ESRCH checks (for -1 and for the own pid plus 1) and the EFAULT checks are unchanged. The debt fragment states the console result for pid 0, the own pid and pid 1 and says that pids which exist on the console are not measured.

Not measured on the console: other existing pids (this library returns ESRCH for them, and also for the own pid plus 1), the value after a scheduler setting, a title process, and what pid 1 is there. No title is shown to call the function.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr) that loads libkernel.sprx and takes sched_rr_get_interval with dlsym, on 2026-11-17 at 06:35 UTC. A title process may differ. The cases are 0 to 1.6, lines 2 to 10 of the raw output in the first comment (the probe also read adjtime, which is another pull request's subject).

Call PS5 payload process, firmware 11.60 AnyPS5 main before (test commit only) here (test and fix)
pid 0, own pid, pid 1 0, 4 ms each (lines 5 to 7) guest_sched_priority fails (94 ms, ESRCH for pid 1) passes
pid 0x7fffffff, -1 -1, ESRCH, interval untouched (lines 8, 9) the checks for -1 and for the own pid plus 1 existed before; none for 0x7fffffff pass
null interval -1, EFAULT (line 10) the same check existed before passes

On main, with the test commit alone, one test, guest_sched_priority, fails; it contains all the checks above. Windows 10 Pro 10.0.19045 and Linux (WSL), one build per commit, on base upstream/main bdc13ebcde2, on 2026-11-17 between 07:24 and 07:34 UTC. Windows: the test commit alone built with ninja returning 0 and ctest returned 8; the head of this pull request built with ninja returning 0 and ctest returned 0. Linux: on commits whose trees equal the three commits of this pull request, ninja returned 0 for all three, the test commit alone aborted guest_sched_priority (1 of 1 tests failed), and the fix commit and the documentation commit passed it in three runs each; tools/check_conventions.py passed for each commit.

tools/check_conventions.py --base upstream/main passes with no error and no warning (the changed entry is 647 characters, up from 519, counted without the leading '- '). The change is 3 files, +12/-5, with no new comments in code. Not tested: macOS, the CI runner, a title process, other firmware, and the cases listed above as not measured.

Overlap: the other open pull requests that change one of the files of this branch (core/libs/prx/libkernel_sys/Export.cpp, core/libs/tests/GuestSchedPriority.cpp) or the debt fragment, found by reading the changed files of all 1644 open pull requests at about 08:07 UTC, by reading what they change in docs/dev/debt/libkernel_sys.md and in the fragments under docs/dev/debt/changes/ for sched_rr_get_interval, and by searching their titles and branch names for the same name (one hit, #17518 below), were test-merged on 2026-11-17 at about 09:02 UTC against main cf5e16b6c9e together with this head; the branch is based on bdc13ebcde2, an ancestor of it, merges into main without a conflict (also against main 0e925c3970d at about 09:05 UTC), and tools/debt_fold.py --check applies its fragment without an error (checked without the fragment libressl3-20171-inventory.md, which already conflicts with feat-libressl3-evp-pkey-new-mac-key-v2.md on main and is not part of this change). Two open pull requests matter. #17518 (test(libkernel): audit sched_rr_get_interval timespec and pid boundary validation) adds GuestKernelSchedRrInterval.cpp, which requires 94 ms for pid 0 and the own pid; it does not conflict textually, but its test contradicts the console value measured here and would fail after this change; its reviewer noted on 2026-11-11 that GuestSchedPriority.cpp on main already asserts the same cases, which this change updates to 4 ms. #18190 (feat(libs): mirror libkernel exports through libkernel_sys) merges cleanly with main and conflicts only with this change, in Export.cpp around sched_rr_get_interval; whichever lands second needs a rebase. Of the other nine pull requests that list one of the files, #12315 and #9389 already conflict with main and conflict in the same files with this head merged first, and #21984, #17908, #17897, #17885, #15037 and #6918 merge cleanly with main and with this head. No open pull request changes the sched_rr_get_interval debt entry. The merged result was not built or run.

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: none
  • GPUs tested: n/a (no graphics change)
  • AI-assisted: yes

xXPBSXx and others added 3 commits November 17, 2026 08:22
… pid and pid 1 as measured on the console

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… 1 as the console does

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@xXPBSXx

xXPBSXx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Probe source (built with ps5-payload-sdk v0.45; PROBE_BUILD is the first 12 hex digits of the source's SHA-256, ace230ef0cab) and its raw output from the run on 2026-11-17 at 06:35 UTC. The clock in the PROBE line is the console's own clock. The probe loads libkernel.sprx, takes sched_rr_get_interval and adjtime with dlsym and calls sched_rr_get_interval for pid 0, its own pid, pid 1, two pids that do not exist and a null interval, and adjtime with a null delta; every call is guarded against signals. The adjtime cases are not the subject of this pull request. An internal probe number and case reference in the header comment were removed; the code is unchanged, and the build ID is the hash of the original file.

probe.c
/* sched_rr_get_interval and adjtime (read only), payload process, firmware 11.60 (debt libkernel_sys.md
   sched_rr_get_interval entry: AnyPS5 accepts pid 0 or the guest pid and reports the FreeBSD 11 ULE default time slice
   (94 ms), other pids return ESRCH and a null interval EFAULT; the console's values are not measured; adjtime entry: under
   the model agreed on #11216 every readable non-null delta returns EPERM without writing the old delta, bad input returns
   EFAULT first and a null delta reports zero; console privileges and clock slewing are unverified). The probe looks both up
   with dlsym in libkernel.sprx and libc.sprx. sched_rr_get_interval(0, &ts) and (getpid(), &ts) on a
   0xaa-filled timespec (rc, seconds, nanoseconds), then pid 1, 0x7fffffff and -1 (rc, errno, whether the timespec changed),
   and a NULL timespec once at the end of that group. adjtime is called ONLY with a NULL delta and an old-delta buffer
   filled with 0xaa (rc, errno, the old delta); it is never called with a delta that is not NULL, because that would set a
   clock adjustment, and then once with both pointers NULL as the last call of the probe. Nothing is written, nothing is
   sent. */
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ps5/kernel.h>   /* only for kernel_get_fw_version() */

#ifndef PROBE_NAME
#define PROBE_NAME "unnamed"
#endif
#ifndef PROBE_BUILD
#define PROBE_BUILD "unbuilt"
#endif
/* Self limit: the runner can only close the connection, it cannot end the process on the console, so the probe ends
   itself after PROBE_MAX_S seconds at the latest (set by the build script). */
#ifndef PROBE_MAX_S
#define PROBE_MAX_S 50
#endif

static volatile int g_handle = -1;
static int (*volatile g_setmargins)(int, int, int) = 0; /* unused here */
static void reset_rate(void) {
  if (g_setmargins && g_handle >= 0) g_setmargins(g_handle, 0, 0);
}

static void probe_alarm(int sig) {
  static const char msg[] = "TIMEOUT\tprobe self limit\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
}

/* Watchdog thread: alarm()/SIGALRM does not help when the main thread hangs in a blocking system call or IPC. The
   thread sleeps PROBE_MAX_S seconds, writes TIMEOUT with write() and ends the process with _exit(124). alarm() stays
   as a second safeguard. */
static void* probe_watchdog(void* arg) {
  static const char msg[] = "TIMEOUT\tprobe watchdog thread\n";
  unsigned left = PROBE_MAX_S;
  (void)arg;
  while (left) left = sleep(left);
  (void)!write(1, msg, sizeof msg - 1);
  reset_rate();
  _exit(124);
  return 0;
}

static void probe_watchdog_start(void) {
  pthread_t t;
  if (pthread_create(&t, 0, probe_watchdog, 0) == 0) pthread_detach(t);
}

static int cases = 0;

static void probe_header(void) {
  char utc[32];
  time_t now = time(0);
  probe_watchdog_start();
  struct tm tmv;
  gmtime_r(&now, &tmv);
  strftime(utc, sizeof utc, "%Y-%m-%dT%H:%M:%SZ", &tmv);
  printf("PROBE\t%s\tfw=0x%08x\tbuild=%s\tpid=%d\tutc=%s\n", PROBE_NAME, kernel_get_fw_version(), PROBE_BUILD,
         getpid(), utc);
  fflush(stdout);
}

static void probe_case(const char* id, const char* call, long ret, int err, const char* extra) {
  printf("CASE\t%s\t%s\t%ld\t%d\t%s\n", id, call, ret, err, extra ? extra : "-");
  fflush(stdout);
  cases++;
}

static void probe_end(void) {
  printf("END\t%d\n", cases);
  fflush(stdout);
}

#include <time.h>
#include <sys/time.h>
typedef int (*fn_rr)(int, struct timespec*);
typedef int (*fn_adj)(const struct timeval*, struct timeval*);

static void fault(int sig) {
  static const char msg[] = "FAULT\tsignal in the call\n";
  (void)sig;
  (void)!write(1, msg, sizeof msg - 1);
  _exit(125);
}

static void rr_case(const char* id, const char* label, fn_rr f, int pid) {
  struct timespec ts;
  memset(&ts, 0xaa, sizeof ts);
  char extra[120];
  errno = 0;
  int r = f(pid, &ts);
  int e = errno;
  unsigned char* p = (unsigned char*)&ts;
  int changed = 0;
  for (size_t i = 0; i < sizeof ts; ++i) if (p[i] != 0xaa) changed++;
  snprintf(extra, sizeof extra, "rc=%d errno=%d changed=%d sec=%lld nsec=%lld", r, e, changed, (long long)ts.tv_sec, (long long)ts.tv_nsec);
  probe_case(id, label, r, e, extra);
}

int main(void) {
  signal(SIGALRM, probe_alarm);
  signal(SIGSEGV, fault);
  signal(SIGBUS, fault);
  alarm(PROBE_MAX_S);
  probe_header();
  char extra[160];

  errno = 0;
  void* lk = dlopen("libkernel.sprx", RTLD_LAZY);
  probe_case("0", "load libkernel.sprx", lk ? 0 : -1, errno, lk ? "loaded" : dlerror());
  errno = 0;
  void* ls = dlopen("libc.sprx", RTLD_LAZY);
  probe_case("0a", "load libc.sprx", ls ? 0 : -1, errno, ls ? "loaded" : dlerror());
  fn_rr rr = 0;
  fn_adj adj = 0;
  const char* wr = "none";
  const char* wa = "none";
  if (lk) { rr = (fn_rr)dlsym(lk, "sched_rr_get_interval"); if (rr) wr = "libkernel"; adj = (fn_adj)dlsym(lk, "adjtime"); if (adj) wa = "libkernel"; }
  if (!rr && ls) { rr = (fn_rr)dlsym(ls, "sched_rr_get_interval"); if (rr) wr = "libc"; }
  if (!adj && ls) { adj = (fn_adj)dlsym(ls, "adjtime"); if (adj) wa = "libc"; }
  snprintf(extra, sizeof extra, "sched_rr_get_interval_in=%s adjtime_in=%s", wr, wa);
  probe_case("0b", "dlsym of the functions", 0, 0, extra);

  if (rr) {
    rr_case("1.1", "sched_rr_get_interval(0, &ts)", rr, 0);
    rr_case("1.2", "sched_rr_get_interval(getpid(), &ts)", rr, (int)getpid());
    rr_case("1.3", "sched_rr_get_interval(1, &ts)", rr, 1);
    rr_case("1.4", "sched_rr_get_interval(0x7fffffff, &ts)", rr, 0x7fffffff);
    rr_case("1.5", "sched_rr_get_interval(-1, &ts)", rr, -1);
    errno = 0;
    int r = rr(0, 0);
    snprintf(extra, sizeof extra, "rc=%d errno=%d", r, errno);
    probe_case("1.6", "sched_rr_get_interval(0, NULL)", r, errno, extra);
  }

  if (adj) {
    struct timeval old;
    memset(&old, 0xaa, sizeof old);
    errno = 0;
    int r = adj(0, &old);
    snprintf(extra, sizeof extra, "rc=%d errno=%d old=%lld.%lld", r, errno, (long long)old.tv_sec, (long long)old.tv_usec);
    probe_case("2", "adjtime(NULL, &old), delta NULL only", r, errno, extra);
    errno = 0;
    r = adj(0, 0);
    snprintf(extra, sizeof extra, "rc=%d errno=%d", r, errno);
    probe_case("3", "adjtime(NULL, NULL)", r, errno, extra);
  }

  probe_end();
  return 0;
}
stdout
PROBE	libkernel_sys_rrinterval_adjtime	fw=0x11600005	build=ace230ef0cab	pid=212	utc=2026-11-17T08:17:19Z
CASE	0	load libkernel.sprx	0	2	loaded
CASE	0a	load libc.sprx	-1	2	error_code=0xffffffff
CASE	0b	dlsym of the functions	0	0	sched_rr_get_interval_in=libkernel adjtime_in=libkernel
CASE	1.1	sched_rr_get_interval(0, &ts)	0	0	rc=0 errno=0 changed=16 sec=0 nsec=4000000
CASE	1.2	sched_rr_get_interval(getpid(), &ts)	0	0	rc=0 errno=0 changed=16 sec=0 nsec=4000000
CASE	1.3	sched_rr_get_interval(1, &ts)	0	0	rc=0 errno=0 changed=16 sec=0 nsec=4000000
CASE	1.4	sched_rr_get_interval(0x7fffffff, &ts)	-1	3	rc=-1 errno=3 changed=0 sec=-6148914691236517206 nsec=-6148914691236517206
CASE	1.5	sched_rr_get_interval(-1, &ts)	-1	3	rc=-1 errno=3 changed=0 sec=-6148914691236517206 nsec=-6148914691236517206
CASE	1.6	sched_rr_get_interval(0, NULL)	-1	14	rc=-1 errno=14
CASE	2	adjtime(NULL, &old), delta NULL only	0	0	rc=0 errno=0 old=0.0
CASE	3	adjtime(NULL, NULL)	0	0	rc=0 errno=0
END	11

AI-assisted (Claude Code).

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 53e3c00988:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_sched_priority_tests built cleanly.
  • CTest (guest_sched_priority):
    1/1 Test #5432: guest_sched_priority .............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel_sys/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5432: guest_sched_priority .............Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified sched_rr_get_interval_nid_postfix accepts pid 0, pid 1, and the current guest pid (self = getpid_nid_postfix()), returning quantum {0, 4000000} (4 ms) matching PS5 firmware 11.60 console probe measurements.
    • Verified error paths: invalid pids return -1 with errno ESRCH (3), and null or unwritable interval pointers return -1 with errno EFAULT (14) via GuestCopyOut_nid_no_patch.
    • Verified struct KernelTimespec layout and 4 ms values (tv_sec = 0, tv_nsec = 4000000) are cross-platform safe across LP64 and LLP64 targets.
    • Verified AGENTS.md technical debt documentation conventions: fragment added at docs/dev/debt/changes/fix-kernel-sys-sched-rr-interval-4ms.md with proper markdown header and explanation.
    • Verified GuestSchedPriority.cpp unit test updates check pid 0, self, pid 1, invalid pids (-1), and preserve errno verification.

Looks Good To Me

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 18190 core/libs/prx/libkernel_sys/Export.cpp L1129-1131

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9389 core/libs/prx/libkernel_sys/Export.cpp
PR 15037 core/libs/prx/libkernel_sys/Export.cpp
PR 23786 core/libs/prx/libkernel_sys/Export.cpp

This was referenced Nov 21, 2026
@kiwe147

kiwe147 commented Nov 21, 2026

Copy link
Copy Markdown

Tested head 53e3c00988, applied with git apply (with -C1, context shifted) on main fd4aeed483 together with the other 27 PRs of one batch (#11049, #20072, #20406, #22100, #22892, #23774, #24199, #24495, #15314, #21180, #21221, #21486, #22597, #22839, #22895, #23550, #24672, #14573, #16873, #17113, #17879, #19183, #20057, #22066, #22781, #22958, #24478). All applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
guest_sched_priority (1 test, matched by name to the test file this PR changes) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6522 tests) on the RTX 3080 has 8 failing tests; the 4 that also fail on main fd4aeed483 are agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10. Intermittent in this run (pass when rerun alone, or known flaky): agc_driver_store_over_untracked, agc_storage_write_back_host_retile. Traced to other PRs of the batch, each by reverting it and testing again: #21486 (agc_driver_graphics, agc_graphics_profile_draw), #24672 (guest_kernel_sys_neo_mode); none is in this PR's area. Failing tests I could not trace to a single PR (reverting the likely candidates did not clear them): guest_indexed_draw. The 2125 agc_/video tests on the iGPU have 5 failing: agc_driver_graphics, agc_driver_stencil_plane_memory, agc_driver_store_over_untracked, agc_graphics_profile_draw, agc_storage_write_back_host_retile (agc_driver_stencil_plane_memory also fails on main).

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the reported console measurement is reflected in the implementation, tests, and debt entry.

What I ran

Reviewed head 53e3c009889c40a7f5963389eea6b6148c5a2eb2. I read description.md, commits.md, manifest.json, diff.patch, and the full text of all three changed files via repo_read.py. I did not run commands or tests.

Results

Check Result Note
Changed code review passed Implementation, tests, and debt entry agree
Tests not run Test and build results were supplied by the author

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently verify the console measurement or run builds and tests. Firmware, title process behavior, and other existing process IDs were not independently checked. files.json was unavailable in the review bundle; manifest.json lists three changed files, and I read all three.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit 407a089 into boykopovar:main Nov 27, 2026
14 checks passed
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.

5 participants