Repository navigation
fix(libc): start the Windows initial thread with FreeBSD's x87 control word - #27619
kiansaeidnia wants to merge 1 commit into
Conversation
…l word EnterGuestMainStack clears the x87 exception flags and loads the control word 0x037f before it captures the context it restores on the guest main stack, so the title's startup and main entry no longer run at the Windows start value 0x27f (53-bit precision) or inherit exception flags the host raised. FreeBSD 11.4 starts every thread from fpu_initialstate, whose control word is __INITIAL_FPUCW__ 0x037F, as guest threads created through libkernel already do. Only the control word and the exception flags are reset; MXCSR is left to the entry stub. Both are set after every check that can throw, so a failed entry leaves the caller's x87 state alone. The Windows guest main-stack test enters at 0x27f with an invalid flag set and requires 0x037f and clear flags on the guest stack, and requires the nullptr-entry and duplicate-entry failures to leave 0x27f and the flag in place. The debt now limits the 53-bit initial thread to a Windows entry that skips EnterGuestMainStack. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The change at head 7bf383c30fc7abef3153364d45d40de526444842 sets the initial Windows guest stack’s x87 control state after entry checks; the source and test changes I read support merging, while execution results are author-reported.
What I ran
No commands or tests were run. I read the prepared description, commits, manifest, diff, all three changed files, the existing GuestCpu helpers, and the test registration in core/libs/prx/libc/CMakeLists.txt. The files.json exclusion list and a separate review/files/ directory listing were unavailable through the provided reader.
Results
| Check | Result | Note |
|---|---|---|
| Source review | passed | All three changed files read |
| Windows tests | not run | Results are author-reported |
| Exclusion metadata | not run | files.json unavailable |
Findings
Blocking
None.
Non-blocking
None.
Not verified
The author reports that windows_guest_main_stack, windows_guest_main_stack_failure, and windows_guest_main_stack_entry passed on Windows 11 with MinGW-w64 GCC 16.2, and that debt_fold.py --check passed; these were not run in this review. The changed files listed in the manifest were all read. The contents of the files.json exclusion list could not be checked, so other excluded files, if any, were not verified. Title behavior and console measurements were not verified.
To get this to a good state
Nothing is required.
Plyxen
left a comment
There was a problem hiding this comment.
Checked head 7bf383c30f. I applied it to main ad7582bad4 together with #27667, #27662, #27689, #27554 and #27553, which touch different files. On Windows 11 with WinLibs GCC 15.2 (Release), tools/check_conventions.py --head on each head and tools/debt_fold.py --check passed, the build succeeded, and all 4,930 CPU tests passed (I left out the GPU tests).
windows_guest_main_stack, windows_guest_main_stack_failure and windows_guest_main_stack_entry passed, with the WinLibs bin first on PATH. With main's GuestStacks.cpp and the new checks, windows_guest_main_stack fails.
The two GuestCpu calls sit after the last check that can throw and just before RtlCaptureContext, so the captured context carries 0x037f onto the guest stack, while a refused entry leaves 0x27f and the flag, as the test checks.
AI-assisted: yes (Claude Code)
|
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.
|
ligmaSec
left a comment
There was a problem hiding this comment.
Cross-compiled the exact Windows stack-switch unit to assembly and confirmed that fnclex and fldcw are emitted before RtlCaptureContext. A focused MinGW executable under Wine captured control word 0x037f with cleared exception bits. This run covers the floating-point capture sequence. The complete guest main-stack transition remains unverified here.
AI-assisted: yes.
|
Windows test at head Setup: Windows 11 Pro, WinLibs GCC 15.2.0, Release, Ninja. Head merged onto
|
What
On Windows, a title's startup code and its main entry ran with the x87 control word the host process starts with,
0x27f(53-bit precision). Guest threads created through libkernel already start with0x037f(64-bit precision, every exception masked) since #11849, which named the initial thread as the follow-up. libc's x87long doublemath therefore gave different results on the main thread. The debt records one case:asinl(0.75)has mantissa…e000instead of…e041there, and RetroArch (PPSA99169) imports that code from the systemlibSceLibcInternal.FreeBSD 11.4 starts every thread of a new process, the initial one included, from
fpu_initialstate, whose control word is__INITIAL_FPUCW__(0x037F). Now libc'sEnterGuestMainStack, which the Windows entry stub calls before the guest entry, does the same for the initial thread:0x037f, through the existingGuestCpuhelpers.MXCSR is not touched, because the entry stub already loads
0x9fc0. Linux and macOS processes already start at0x037f, so the POSIX path is left alone. The libkernel debt entry says it keeps the host's start word.Debt:
acosl,_LLogandasinlentries now say that only a Windows entry that skipsEnterGuestMainStackruns at 53 bits.The
fmalentry needs no edit, because the pendingfix-fmal-msun-9.mdfragment already drops its pointer toasinl.pr_overlapcould not compareGuestStacks.cppwith #7149 and #9938, because they conflict withmain. I read their diffs. They change the Windows pthread and fiber stack functions in that file (GuestStackRegister,GuestStackSwitch), notEnterGuestMainStackor the x87 state.Tested
Windows 11, MinGW-w64 GCC 16.2, Release.
windows_guest_main_stack,windows_guest_main_stack_failureandwindows_guest_main_stack_entrypass.windows_guest_main_stack_entryneeds the MinGWbindirectory first onPATH, a known environment issue unrelated to this change.windows_guest_main_stacknow:0x27fwith the invalid flag set, and requires0x037fand clear exception flags on the guest stack;0x27fand the flag in place.With the
GuestStacks.cppchange reverted, that test aborts (0xc0000409).GuestStacks.cppwas syntax-checked with g++ 11 on Linux (WSL Ubuntu).debt_fold.py --checkpasses. Not tested with a title, and not measured on a console.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)long doubleresults on the main thread match the other guest threads)🤖 Generated with Claude Code
Summary by CodeRabbit