Skip to content

fix(libkernel): start guest threads with the x87 control word 0x037f - #11849

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
AlexFigas:fix/libkernel-guest-thread-x87-control-word
Nov 2, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
AlexFigas:fix/libkernel-guest-thread-x87-control-word

Conversation

@AlexFigas

@AlexFigas AlexFigas commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

What

A guest thread started with whatever x87 control word the host gave it: on Linux the creator's, on Windows _beginthreadex's default 0x027f (53-bit precision). On the console it starts with 0x037f, as on FreeBSD 11:

  • FreeBSD 11's cpu_copy_thread clears PCB_FPUINITDONE and PCB_USERFPUINITDONE for the new thread, so its first FPU instruction goes through fpudna and loads fpu_initialstate, whose control word is __INITIAL_FPUCW__ (0x037F), whatever the creator uses. Newer FreeBSD copies the creator's FPU state instead; the libc.prx that titles bundle follows FreeBSD 11's msun.
  • libSceFiber's option that starts fibers with the default FPU state loads the same 0x037f.

RunThread now loads 0x037f on the new thread before it runs the guest entry. MXCSR is not touched here; #7565 sets the measured 0x9fc0.

This matters for the long-double math in libc, which runs at the caller's x87 precision as the console's own code does (see #11260, #11632). At 53 bits, asinl(0.75) returns mantissa 0xd91a98ae3406e000 instead of 0xd91a98ae3406e041, and exp2l results move far from the 64-bit ones. With this change, created guest threads compute at 64 bits, as on the console, unless the title changes the control word itself.

The initial thread is not covered: the relinker's entry stub runs the guest entry on the host's main thread, which keeps the control word the host starts it with (0x027f on Windows). The entry stub is where #7594 sets MXCSR for that thread, and the control word would go there too.

A new libkernel debt entry records that 0x037f comes from FreeBSD 11 and the fiber default, not from a measurement, and that the initial thread keeps the host's control word. It sits next to the pthread_attr entry, away from the lines other open PRs change.

libc's asinl entry said guest threads start at 53 bits on Windows; it now says that of the initial thread only, trimmed to stay under 1000 characters. #11632 and #11634 add the cbrtl and hypotl entries directly above that line, so this PR declares Depends on: #11632, #11634 and I'll rebase once they land. #12319, which rewrote the acosf line below, has merged, and this branch is rebased onto it.

Tested

On Ubuntu 24.04 under WSL2 (GCC 13.3, Release), head 950ce5a, which merges main 98ddd9c into this PR's commit:

Checklist

@github-actions

github-actions Bot commented Oct 27, 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 8417 docs/dev/debt/libc.md L180-181

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 3986 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4563 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 4630 docs/dev/debt/libkernel.md
PR 5021 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libc.md
docs/dev/debt/libkernel.md
PR 5303 docs/dev/debt/libc.md
PR 6565 docs/dev/debt/libc.md
PR 6936 docs/dev/debt/libkernel.md
PR 7081 docs/dev/debt/libc.md
PR 7088 docs/dev/debt/libkernel.md
PR 7105 docs/dev/debt/libkernel.md
PR 7121 docs/dev/debt/libkernel.md
PR 7135 docs/dev/debt/libkernel.md
PR 7149 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 7192 docs/dev/debt/libkernel.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
docs/dev/debt/libkernel.md
PR 7464 docs/dev/debt/libkernel.md
PR 7715 docs/dev/debt/libc.md
PR 7804 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 8150 docs/dev/debt/libc.md
PR 8592 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 8965 docs/dev/debt/libc.md
PR 9156 docs/dev/debt/libkernel.md
PR 9199 docs/dev/debt/libkernel.md
PR 9293 docs/dev/debt/libkernel.md
PR 9297 docs/dev/debt/libkernel.md
PR 9302 docs/dev/debt/libkernel.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9389 docs/dev/debt/libkernel.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9557 docs/dev/debt/libkernel.md
PR 9566 docs/dev/debt/libkernel.md
PR 9843 docs/dev/debt/libkernel.md
PR 9929 docs/dev/debt/libc.md
PR 9938 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 9964 docs/dev/debt/libc.md
PR 10025 docs/dev/debt/libkernel.md
PR 10113 docs/dev/debt/libc.md
PR 10226 docs/dev/debt/libc.md
PR 10245 docs/dev/debt/libc.md
PR 10250 docs/dev/debt/libc.md
PR 10253 docs/dev/debt/libc.md
PR 10265 docs/dev/debt/libc.md
PR 10274 docs/dev/debt/libc.md
PR 10315 docs/dev/debt/libc.md
PR 10319 docs/dev/debt/libkernel.md
PR 10325 docs/dev/debt/libc.md
PR 10328 docs/dev/debt/libc.md
PR 10345 docs/dev/debt/libc.md
PR 10349 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10442 docs/dev/debt/libkernel.md
PR 10506 docs/dev/debt/libc.md
PR 10510 docs/dev/debt/libc.md
PR 10512 docs/dev/debt/libc.md
PR 10565 docs/dev/debt/libc.md
PR 10987 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11831 docs/dev/debt/libc.md
PR 11896 docs/dev/debt/libkernel.md
PR 11907 docs/dev/debt/libc.md
PR 11939 docs/dev/debt/libkernel.md
PR 12057 docs/dev/debt/libc.md
PR 12104 docs/dev/debt/libkernel.md
PR 12122 docs/dev/debt/libc.md
PR 12130 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12292 docs/dev/debt/libc.md
PR 12397 docs/dev/debt/libc.md
PR 12514 docs/dev/debt/libc.md
PR 12544 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12560 docs/dev/debt/libc.md
docs/dev/debt/libkernel.md
PR 12574 docs/dev/debt/libc.md
PR 12627 docs/dev/debt/libc.md
PR 12642 core/libs/prx/libkernel/Pthread/src/Thread.cpp
PR 12970 docs/dev/debt/libkernel.md
PR 13336 docs/dev/debt/libkernel.md
PR 13390 docs/dev/debt/libc.md
PR 13582 core/libs/prx/libkernel/Pthread/src/Thread.cpp
docs/dev/debt/libkernel.md
PR 13587 docs/dev/debt/libkernel.md
PR 13804 docs/dev/debt/libc.md
PR 14103 docs/dev/debt/libkernel.md
PR 14128 docs/dev/debt/libkernel.md
PR 14212 docs/dev/debt/libc.md
docs/dev/debt/libkernel.md
PR 14264 docs/dev/debt/libkernel.md
PR 14310 docs/dev/debt/libkernel.md
PR 14373 docs/dev/debt/libc.md
PR 14497 docs/dev/debt/libkernel.md
PR 14585 docs/dev/debt/libc.md
PR 14610 docs/dev/debt/libc.md

@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 the exact head 7393185. The control word is set on the newly created host thread before the guest entry runs, and the change leaves the creator's state untouched. The initial-thread limitation is clearly recorded in the debt entry. I found no correctness issue in this narrow change.

I built guest_kernel_sys_thread_defaults_tests successfully. The direct CTest run stopped at the existing host CPU capability guard (this machine lacks AVX2/FMA); running the exact test executable under qemu-x86_64 -cpu max passed, including the new control-word case. The PR's Windows check also passes. I did not run the full suite independently.

@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.

The current overlap report still lists this PR's changes to docs/dev/debt/libc.md with #10253 and docs/dev/debt/libkernel.md with #11865. It also says it could not compare Thread.cpp against #11865. #11865's current description records the debt-file conflict with this PR, but this PR still says Depends on: none and claims its overlap check found no other author's overlap. Please update the overlap report/checklist to record the agreed landing order for the shared debt entry, and manually compare the Thread.cpp change with #11865 before saying there is no overlap. The source change and test evidence otherwise look sound; this is a contribution-rule/merge-coordination blocker.

@MarcZX10

Copy link
Copy Markdown
Contributor

Follow-up to my overlap review: I compared #11865's current diff directly with this head. Its implementation is in core/libs/prx/libkernel/Pthread/Posix/Thread.cpp, while this PR changes core/libs/prx/libkernel/Pthread/src/Thread.cpp; the thread-start code changes are distinct. I also ran git merge-tree for the exact current heads: the branches conflict in docs/dev/debt/libkernel.md. The same check against #10253 conflicts in docs/dev/debt/libc.md. So the remaining coordination is limited to agreeing and recording the landing order for those two debt-file overlaps, then ensuring the overlap report/checklist reflects it.

@JJenkx

JJenkx commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Windows run at this PR's exact head 739318563f (739318563f3cc6cfeaf27458c974b2457807b7a3, merge base 801d5b9bc1), not merged onto anything else. This is the platform where the default the PR describes (_beginthreadex starting threads at 0x027f) applies.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, in fresh build directories.

  • This head: guest_kernel_sys_thread_defaults passes in each of 3 runs, including the new CheckFpuControl.
  • Negative control: the same head with only core/libs/prx/libkernel/Pthread/src/Thread.cpp restored from the merge base. The test aborts (exit 0xc0000409) in a single run. The test's Require prints no message, so the log doesn't show which check fired. Thread.cpp is the only difference, and the other two checks don't depend on it.

So on Windows the new check catches a missing fldcw, and the change makes it pass. The initial-thread gap the PR describes (the relinker's entry stub) was not exercised, and no title or console was run.

AI-assisted: yes (Claude Code).

@AlexFigas
AlexFigas force-pushed the fix/libkernel-guest-thread-x87-control-word branch from 7393185 to 4efa6aa Compare October 27, 2026 13:33
@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Rebased onto main 1089f2b (4efa6aa). Instead of ordering the overlaps, I removed them:

pr_overlap.py now reports no comparable overlap, and the whole suite passes on this head (2551 tests). The description is updated.

@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.

Re-reviewed new exact head 4efa6aae4ddd2230340b03d2fd131eccb1c11ef2. The changes resolve the earlier direct overlap: libc.md is no longer edited, the libkernel debt entry moved away from #11865's edited lines, and merge-tree checks of both current PR heads are clean.

One landing-order/content issue remains. This code changes the precision of newly created guest threads, while the existing asinl debt sentence in libc.md still says Windows guest threads run at 53 bits. The description says that sentence will be narrowed after #10253 lands, but the checklist still says Depends on: none. Please record #10253 as the dependency and, after it lands, update/rebase this PR to narrow the asinl note before merging; otherwise the code and documented behavior disagree.

@maskym

maskym commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Windows result for head 4efa6aae4d, merged into main 1089f2bbfd (the PR was tested under WSL2 only).

Host: Windows 11 26300, Intel i7-12700K, WinLibs GCC 15.2.0 (x86_64-ucrt-posix-seh, r6), Release, BUILD_TESTING=ON, Ninja. Build clean.

ctest --test-dir build -R "kernel_sys_thread|pthread_create|thread_defaults|guest_thread": 9/9 passed, guest_kernel_sys_thread_defaults with the new CheckFpuControl in 0.04 s. So a guest thread created from a thread whose x87 control word is 0x027f starts with 0x037f on the Windows host as well, and the creator keeps 0x027f. Not run on a PS5.

AI-assisted: yes (Claude Code), the runs and the numbers above are from this machine.

@AlexFigas
AlexFigas force-pushed the fix/libkernel-guest-thread-x87-control-word branch from 4efa6aa to ac680b0 Compare October 27, 2026 14:45
@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Done in ac680b0, rebased onto main f2f759a. The asinl sentence is narrowed again, so it now says only the initial thread runs at 53 bits on Windows, and the checklist records Depends on: #10253. I'll rebase once #10253 lands and keep its new entry below asinl. The whole suite passes on this head (2556 tests).

@AlexFigas
AlexFigas requested a review from MarcZX10 October 27, 2026 14:57

@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 exact head ac680b0c0bc37d6ef6fd78bdedfccdfafa076e99 against current main f2f759ae31f2e51f767b8eb83c6c7e7132cab6d8.

The fldcw executes on the newly created host thread before the guest entry starts, setting only that thread's x87 control word; the test also checks the creator retains its previous value. The initial-thread limitation is documented. The updated diff no longer edits libc.md, and the declared dependency on #10253 is explicit; keep that landing order and rebase/recheck if main changes before merge.

Local verification: Release guest_kernel_sys_thread_defaults_tests build succeeded from this exact head; the test passed three runs under QEMU Haswell. Native CTest reaches the repository's host CPU guard because this Xeon lacks AVX2/FMA and related features. Conventions and git diff --check passed, and a merge-tree preview against current main is clean. Hosted Windows, eligibility, conventions, labels, and report checks pass; Linux product and the relinker/register-context/write-watch matrix are skipped for this PR. Author-reported full-suite and Windows runs are described in the PR; I did not independently run the full suite or test on a PS5.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 29, 2026

@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.

Re-checked — changes requested

The reported test demonstrates the x87 control-word reset and that the creator thread retains its own value. The branch is still based on main 3a2b919816, 36 commits behind current main 52c09d567d2fa453494dd720fcf53a98149468e4. The repository checklist requires a current-main base, and the reported whole-suite run is on an earlier head.

What I ran

  • Reviewed exact head 57c9f5d2554deb338b1306b38b93d0f7ff129f36 against current main.
  • Ran git merge-tree --write-tree and git diff --check.
  • Checked the exact-head thread test report and current hosted checks.

Results

Check Result
Current-main merge preview Clean; current main is not in the branch history
git diff --check Passed
Focused thread tests Author reports 67 thread/pthread/fiber tests and three focused runs on the older 3a2b919816 base
Whole suite Reported on an earlier head, not rerun after the rebase
Windows Existing report is on earlier heads; current-head Windows behavior is not reported

Please refresh onto current main and rerun the focused thread tests on that exact head. I did not build or run the C++ tests locally.

@AlexFigas
AlexFigas force-pushed the fix/libkernel-guest-thread-x87-control-word branch from 57c9f5d to 8b626e3 Compare October 29, 2026 10:17
@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Rebased onto main 1b580f1: head 8b626e3, which merges cleanly with current main 1d8fd64. On this head guest_kernel_sys_thread_defaults passes 3 runs and aborts with main's Thread.cpp. The 67 tests matching guest_kernel_sys_thread, pthread or fiber pass, except agc_driver_optimize_on_fiber, which skips itself in this build. The order is unchanged: #10253, #11632 and #11634 land first.

@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.

Re-checked — changes requested

The exact-head report now includes three focused runs, the 67 thread/pthread/fiber tests, and notes that the default-thread test aborts with main's implementation but passes with this change. The branch is based on 1b580f13cf; current main is ba5b5330b3, with updates to the debt files this PR changes. Please merge current main and rerun the focused thread-default test on the resulting exact head. Keep the declared landing order with #10253, #11632 and #11634.

@AlexFigas

Copy link
Copy Markdown
Contributor Author

@MarcZX10 Merged main 98ddd9c, which is still current main: head 950ce5a. On this head guest_kernel_sys_thread_defaults passes 3 runs and aborts with main's Thread.cpp. The 67 thread, pthread and fiber tests pass, except agc_driver_optimize_on_fiber, which skips itself in this build.

One change to the order: #10253 was rebased yesterday and its entry no longer sits next to the asinl line, so the two now merge cleanly in either order. The PR now declares Depends on: #11632, #11634 only.

@AlexFigas
AlexFigas requested a review from MarcZX10 October 30, 2026 09:49

@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

Reviewed exact open head 950ce5a4fc7a0faa02e2f2a36f0f73891e72b53e; I found no blocking issue in the per-thread x87 control-word initialization, test, or dependency declaration.

What I ran

The PR base is 98ddd9c1792bbab016ceed5db267a93d1d61f00a. Its merge preview onto current main a5939f10a85fdbcd3e0fe75e5eea278f4fe3e52a is clean. Local checks ran on Linux (Debian, Python 3.11.2, GCC 12.2.0); I did not configure or build. I inspected the exact diff, CMake registration, prior discussion, and CI. The author reports the focused test passing three times on this exact head under Ubuntu 24.04/WSL2 with GCC 13.3; I did not independently run it.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 11849
git diff --check 98ddd9c1792bbab016ceed5db267a93d1d61f00a...950ce5a4fc7a0faa02e2f2a36f0f73891e72b53e
git merge-tree --write-tree refs/remotes/upstream/main 950ce5a4fc7a0faa02e2f2a36f0f73891e72b53e
python3 tools/pr_stack_check.py 11849 --repo boykopovar/AnyPS5
python3 tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-11849-950ce5a4fc7a/head-950ce5a4fc7a-494f0c81 --json core/libs/tests/GuestKernelSysThreadDefaults.cpp

Results

Check Result
Diff check and conventions Passed locally; hosted conventions passed on this head.
Merge preview Clean against current main.
Dependency order Directly verified as #11632 → #11634 → #11849. The stack helper reported a cycle, but the live PR bodies show #11632 has no dependency and #11634 depends only on #11632; this is a helper false positive.
Test registration The heuristic flagged the new C++ test as possibly unregistered; exact-head core/libs/CMakeLists.txt registers guest_kernel_sys_thread_defaults_tests and the guest_kernel_sys_thread_defaults CTest.
Windows CI Job succeeded and compiled the changed library sources. Its log says no changed guest test sources were selected, so it did not execute the new test.
Focused behavior test Author reports three exact-head passes and that the test aborts against the base implementation; not independently run here.
No-build precheck The convention/diff checks passed. Its isolated checkout lacked the local ABI and test-quality helper scripts, and its dependency helper reported the false cycle above.
Selected unedited local output
All conventions checks passed.

1af306b7560db22f489bee4a435fe59bf28c15e8
No guest core/libs test sources changed
No guest core/libs test sources changed; the libs build still ran
{
  "findings": [
    {
      "path": "core/libs/tests/GuestKernelSysThreadDefaults.cpp",
      "line": 1,
      "kind": "possible-unregistered-cpp-test",
      "message": "Changed C++ test source path was not found literally in CMake files; inspect registration, generated source lists, and workflow coverage."
    }
  ],
  "negative_control_candidates": [],
  "limits": [
    "Heuristics can miss weak assertions and can flag valid tests; inspect every finding.",
    "A test that passes against base is not by itself proof that the PR test is inadequate.",
    "Negative controls are only candidates until the test actually runs against base and fails by assertion rather than import/setup error."
  ]
}

Negative control

Assertion Change to code under test Did the test fail?
New guest threads start with control word 0x037f, while the creator retains 0x027f. Author reports testing against main's Thread.cpp without the fldcw change. Yes, author-reported; not independently run.

Sources

Not verified

I did not build or run tests locally. The hosted Windows job did not execute the new CTest, and hosted Linux was skipped. The Windows focused result in the discussion is from an earlier head; the implementation and test logic are unchanged, but it is not an exact-current-head Windows run. No physical-console behavior was tested.

@AlexFigas

AlexFigas commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor Author

@MarcZX10 The approved head 950ce5a still merges cleanly into current main 8a4fc62. main has changed Thread.cpp and both debt files since, so I built the merge result (d21feb4510) separately and did not move this branch. On it guest_kernel_sys_thread_defaults passes, and aborts with main's Thread.cpp. The whole 67-test thread family (guest_libc_threads, guest_pthread_*, guest_kernel_sys_pthread* and threads*, guest_std_thread*) pass 3 runs each. Depends on: #11632, #11634 is unchanged; both are refreshed on current main.

@oneandonlydean
oneandonlydean merged commit eeb3dfe into boykopovar:main Nov 2, 2026
12 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