Skip to content

fix(libc): start the Windows initial thread with FreeBSD's x87 control word - #27619

Open
kiansaeidnia wants to merge 1 commit into
boykopovar:mainfrom
kiansaeidnia:fix/libc-windows-initial-thread-x87-control
Open

kiansaeidnia wants to merge 1 commit into
boykopovar:mainfrom
kiansaeidnia:fix/libc-windows-initial-thread-x87-control

Conversation

@kiansaeidnia

@kiansaeidnia kiansaeidnia commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

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 with 0x037f (64-bit precision, every exception masked) since #11849, which named the initial thread as the follow-up. libc's x87 long double math therefore gave different results on the main thread. The debt records one case: asinl(0.75) has mantissa …e000 instead of …e041 there, and RetroArch (PPSA99169) imports that code from the system libSceLibcInternal.

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's EnterGuestMainStack, which the Windows entry stub calls before the guest entry, does the same for the initial thread:

  • It clears the x87 exception flags and loads 0x037f, through the existing GuestCpu helpers.
  • It does this after every check that can throw, and just before it captures the context it restores on the guest main stack. A failed entry therefore leaves the caller's x87 state alone.

MXCSR is not touched, because the entry stub already loads 0x9fc0. Linux and macOS processes already start at 0x037f, so the POSIX path is left alone. The libkernel debt entry says it keeps the host's start word.

Debt:

  • The acosl, _LLog and asinl entries now say that only a Windows entry that skips EnterGuestMainStack runs at 53 bits.
  • The libkernel entry on the guest thread x87 control word now covers the initial thread.

The fmal entry needs no edit, because the pending fix-fmal-msun-9.md fragment already drops its pointer to asinl.

pr_overlap could not compare GuestStacks.cpp with #7149 and #9938, because they conflict with main. I read their diffs. They change the Windows pthread and fiber stack functions in that file (GuestStackRegister, GuestStackSwitch), not EnterGuestMainStack or the x87 state.

Tested

Windows 11, MinGW-w64 GCC 16.2, Release. windows_guest_main_stack, windows_guest_main_stack_failure and windows_guest_main_stack_entry pass. windows_guest_main_stack_entry needs the MinGW bin directory first on PATH, a known environment issue unrelated to this change.

windows_guest_main_stack now:

  • enters at 0x27f with the invalid flag set, and requires 0x037f and clear exception flags on the guest stack;
  • requires the null-entry and duplicate-entry failures to leave 0x27f and the flag in place.

With the GuestStacks.cpp change reverted, that test aborts (0xc0000409).

GuestStacks.cpp was syntax-checked with g++ 11 on Linux (WSL Ubuntu). debt_fold.py --check passes. Not tested with a title, and not measured on a console.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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 (x87 long double results on the main thread match the other guest threads)
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Windows guest-stack execution now starts with a standard x87 floating-point control state. The calling thread’s x87 control and status values remain intact after nested entry and when entry fails.

…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>
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Windows guest-stack x87 state

Layer / File(s) Summary
Initialize x87 state before guest-stack entry
core/libs/prx/libc/src/GuestStacks.cpp
The Windows guest main-stack entry clears x87 exceptions and sets the control word to 0x037f before transferring execution.
Check x87 state and document behavior
core/libs/tests/WindowsGuestMainStack.cpp, docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md
The test checks guest-stack x87 values and verifies caller values remain unchanged after duplicate-entry and null-callback errors. The change record describes acosl, _LLog, and asinl, and compares x87 control-word behavior across platforms.

Suggested reviewers: eliasbruno124-dev, marczx10

Change: Bug fix

Merge Risk: Minimal · up to 7bf38

This change makes the Windows initial guest thread start with the same x87 control word as other guest threads. It is narrow and covered by targeted tests, and no merge-blocking risk was found.

Security architecture risk: Minimal · up to 7bf38

The change gives the Windows guest’s initial thread a consistent floating-point starting state. Rejected startup attempts still leave the caller’s state untouched, and the change does not grant new access or privileges.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The changed state is local to the Windows thread performing the existing startup handoff. The patch adds no caller or input selector and does not expand the handoff’s memory or execution authority.

Trust Boundaries and Controls

  • observed — The existing Windows launcher resolves the entry helper from libc.prx and supplies its generated startup continuation. The change leaves that selection and the helper’s null-callback and duplicate-entry controls intact.

Resilience and Maintainability Implications

  • observed — Setup errors and nested or competing entries reach no new x87 writes. Once initialization begins, the existing transfer has no normal return to the caller: the guest stack has a null return address, and an unexpected return from context restore aborts.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cdab1bc7-65dc-4ba1-9656-3f3eaef46bbb
Commits

Reviewing files that changed from the base of the PR and between debacdd and 7bf383c.

Files selected for processing: 3

  • core/libs/prx/libc/src/GuestStacks.cpp
  • core/libs/tests/WindowsGuestMainStack.cpp
  • docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review.

@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

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

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)

@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

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 9938 core/libs/prx/libc/src/GuestStacks.cpp
PR 27864 core/libs/prx/libc/src/GuestStacks.cpp

@ligmaSec ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@B-Howell

B-Howell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Windows test at head 7bf383c30fc7: passes, control fails.

Setup: Windows 11 Pro, WinLibs GCC 15.2.0, Release, Ninja. Head merged onto main 56daaadc3e2c (merges cleanly). This is a native Windows run, not Wine.

  • windows_guest_main_stack and windows_guest_main_stack_failure pass in 3 of 3 runs; the main-stack test prints its new "x87 control and status words" line.
  • Control (main's GuestStacks.cpp, new checks kept): windows_guest_main_stack fails (exit 0xc0000409), the failure test still passes.

This branch has not been deployed

No deployments
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