fix(libkernel): start guest threads with the x87 control word 0x037f #11849
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11849"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by AlexFigas on 2026-10-27T09:50:28Z.
What
A guest thread started with whatever x87 control word the host gave it: on Linux the creator's, on Windows
_beginthreadex's default0x027f(53-bit precision). On the console it starts with0x037f, as on FreeBSD 11:cpu_copy_threadclearsPCB_FPUINITDONEandPCB_USERFPUINITDONEfor the new thread, so its first FPU instruction goes throughfpudnaand loadsfpu_initialstate, whose control word is__INITIAL_FPUCW__(0x037F), whatever the creator uses. Newer FreeBSD copies the creator's FPU state instead; thelibc.prxthat titles bundle follows FreeBSD 11's msun.0x037f.RunThreadnow loads0x037fon the new thread before it runs the guest entry. MXCSR is not touched here; #7565 sets the measured0x9fc0.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 mantissa0xd91a98ae3406e000instead of0xd91a98ae3406e041, andexp2lresults 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 (
0x027fon 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
0x037fcomes 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 thepthread_attrentry, away from the lines other open PRs change.libc's
asinlentry 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 thecbrtlandhypotlentries directly above that line, so this PR declaresDepends on: #11632, #11634and I'll rebase once they land. #12319, which rewrote theacosfline below, has merged, and this branch is rebased onto it.Tested
On Ubuntu 24.04 under WSL2 (GCC 13.3, Release), head
950ce5a4fc, which mergesmain98ddd9c179into this PR's commit:guest_kernel_sys_thread_defaultsgainsCheckFpuControl. The main thread sets0x027fand creates a thread. The new thread must read0x037f, and the creator's own control word must still be0x027fafter the join. It passes 3 runs, with the otherguest_kernel_sys_thread*,pthreadandfibertests (67,main's newguest_pthread_cancel_inactive_wait_*cases included;agc_driver_optimize_on_fiberskips itself in this build, the other 66 pass); without thefldcwit aborts, because a Linux thread inherits the creator's0x027f.guest_kernel_sys_thread_defaultsat 739318563f, where it passes 3 runs and aborts withThread.cppfrom the merge base; maskym ran it at 4efa6aae4d merged intomain1089f2bbfd, where it passes.python3 tools/check_conventions.py --base origin/mainpasses.python3 tools/pr_overlap.py --branch fix/libkernel-guest-thread-x87-control-wordreports #11632, #11634 and #8417, all indocs/dev/debt/libc.md. The 79 pull requests it could not compare give the samegit merge-treeconflicts with this branch as withmain.Pthread/Posix/Thread.cpp, has merged, and this head contains it.asin/acosentry away from theasinlline, so the two merge cleanly in either order and this PR no longer depends on it.cbrtlandhypotlfrom the host-computed list and add their entries directly above that line, so they conflict with this PR there too. They land first; this PR declaresDepends onboth. With #11634's head72ec427b78, which contains #11632 andmain98ddd9c179, merged into this head, they conflict only on that line, and keeping their entries with this PR'sasinlline leaves this PR's diff unchanged (4 files, +27 −1) and passescheck_conventions.py.acosfline directly belowasinl, has merged.tgammato the host-computed list line aboveasinl. It has had changes requested since October 26, so neither this PR nor #11632 and #11634 wait for it.Pthread/src/Thread.cppwith #3986, #4563, #5021, #7149, #7804 and #8592, which conflict withmain. #3986 inserts a line right afterRunThread'sRegisterStack(self)and #8592 one right beforeCurrentGuestState().current = self. Thefldcwnow sits between those two calls, and a three-way merge of each PR'sThread.cppwith this branch's gives no conflict; the other four do not touch those lines.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T10:55:55Z.
Review: APPROVED
Reviewed the exact head 739318563f3cc6cfeaf27458c974b2457807b7a3. 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 maxpassed, including the new control-word case. The PR's Windows check also passes. I did not run the full suite independently.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T13:00:08Z.
Review: CHANGES_REQUESTED
The current overlap report still lists this PR's changes to
docs/dev/debt/libc.mdwith #10253 anddocs/dev/debt/libkernel.mdwith #11865. It also says it could not compareThread.cppagainst #11865. #11865's current description records the debt-file conflict with this PR, but this PR still saysDepends on: noneand 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 theThread.cppchange 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.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T13:35:40Z.
Review: CHANGES_REQUESTED
Re-reviewed new exact head
4efa6aae4ddd2230340b03d2fd131eccb1c11ef2. The changes resolve the earlier direct overlap:libc.mdis 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
asinldebt sentence inlibc.mdstill says Windows guest threads run at 53 bits. The description says that sentence will be narrowed after #10253 lands, but the checklist still saysDepends on: none. Please record #10253 as the dependency and, after it lands, update/rebase this PR to narrow theasinlnote before merging; otherwise the code and documented behavior disagree.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T14:58:24Z.
Review: APPROVED
Reviewed exact head
ac680b0c0bc37d6ef6fd78bdedfccdfafa076e99against currentmainf2f759ae31f2e51f767b8eb83c6c7e7132cab6d8.The
fldcwexecutes 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 editslibc.md, and the declared dependency on #10253 is explicit; keep that landing order and rebase/recheck ifmainchanges before merge.Local verification: Release
guest_kernel_sys_thread_defaults_testsbuild 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 andgit diff --checkpassed, 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.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:49:23Z.
Review: CHANGES_REQUESTED
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
main3a2b919816, 36 commits behind currentmain52c09d567d2fa453494dd720fcf53a98149468e4. The repository checklist requires a current-main base, and the reported whole-suite run is on an earlier head.What I ran
57c9f5d2554deb338b1306b38b93d0f7ff129f36against currentmain.git merge-tree --write-treeandgit diff --check.Results
mainis not in the branch historygit diff --check3a2b919816basePlease refresh onto current
mainand rerun the focused thread tests on that exact head. I did not build or run the C++ tests locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T11:07:52Z.
Review: CHANGES_REQUESTED
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; currentmainisba5b5330b3, 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.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T10:14:37Z.
Review: APPROVED
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 currentmaina5939f10a85fdbcd3e0fe75e5eea278f4fe3e52ais 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.Results
main.core/libs/CMakeLists.txtregistersguest_kernel_sys_thread_defaults_testsand theguest_kernel_sys_thread_defaultsCTest.Selected unedited local output
Negative control
0x037f, while the creator retains0x027f.main'sThread.cppwithout thefldcwchange.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.