From 7bf383c30fc7abef3153364d45d40de526444842 Mon Sep 17 00:00:00 2001 From: Kian Date: Fri, 27 Nov 2026 04:59:27 +1000 Subject: [PATCH] fix(libc): start the Windows initial thread with FreeBSD's x87 control 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 --- core/libs/prx/libc/src/GuestStacks.cpp | 4 ++++ core/libs/tests/WindowsGuestMainStack.cpp | 10 +++++++++- ...libc-windows-initial-thread-x87-control.md | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md diff --git a/core/libs/prx/libc/src/GuestStacks.cpp b/core/libs/prx/libc/src/GuestStacks.cpp index 2403cd429783..cace5cd89b2a 100644 --- a/core/libs/prx/libc/src/GuestStacks.cpp +++ b/core/libs/prx/libc/src/GuestStacks.cpp @@ -2,6 +2,7 @@ #ifdef _WIN32 #include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/GuestCpu.hpp" #include #include #include @@ -100,6 +101,7 @@ void APS5_VABI GuestStackSwitch_nid_no_patch(std::uintptr_t stackPointer) { if (entry == nullptr) throw std::invalid_argument("Guest main stack requires an entry point"); const auto host = CurrentBounds(); constexpr std::size_t guardBytes = 0x10000; + constexpr std::uint16_t initialFpuControl = 0x037f; if (host.base <= host.deallocation || host.base - host.deallocation > std::numeric_limits::max() - guardBytes) throw std::runtime_error("Cannot obtain the host main stack capacity"); const auto capacity = host.base - host.deallocation; @@ -122,6 +124,8 @@ void APS5_VABI GuestStackSwitch_nid_no_patch(std::uintptr_t stackPointer) { entered.clear(); throw; } + GuestCpu::X87ClearExceptions(); + GuestCpu::SetX87Control(initialFpuControl); CONTEXT context{}; RtlCaptureContext(&context); context.Rsp = low + capacity - 40; diff --git a/core/libs/tests/WindowsGuestMainStack.cpp b/core/libs/tests/WindowsGuestMainStack.cpp index a27669d809b9..04c2c2a66d5c 100644 --- a/core/libs/tests/WindowsGuestMainStack.cpp +++ b/core/libs/tests/WindowsGuestMainStack.cpp @@ -1,5 +1,6 @@ #define _WIN32_WINNT 0x0602 #include "prx/libc/include/GuestArena.hpp" +#include "prx/libc/include/GuestCpu.hpp" #include "prx/libc/include/GuestStacks.hpp" #include #include @@ -21,6 +22,7 @@ static void APS5_VABI OnGuestStack(void* arguments) { Require(arguments == &originalThread); Require((reinterpret_cast(&local) & 0xffffffffc0000000ull) != 0); Require(GuestArena::GuestArenaContains_nid_postfix(&local, sizeof(local))); + Require(GuestCpu::X87Control() == 0x037f && (GuestCpu::X87Status() & 0xff) == 0); Require(GetCurrentThreadId() == originalThread && &tlsValue == originalTls && tlsValue == 0x123456789abcdef0ull); ULONG_PTR low = 0; ULONG_PTR high = 0; @@ -49,14 +51,17 @@ static void APS5_VABI OnGuestStack(void* arguments) { } catch (const std::runtime_error&) { Require(destroyed); } + GuestCpu::SetX87Control(0x027f); + GuestCpu::SetX87Status(0x0001); try { GuestStacks::EnterGuestMainStack_nid_no_patch(&originalThread, OnGuestStack); std::abort(); } catch (const std::runtime_error&) { } + Require(GuestCpu::X87Control() == 0x027f && (GuestCpu::X87Status() & 0xff) == 0x0001); void* frames[16]{}; Require(CaptureStackBackTrace(0, 16, frames, nullptr) != 0); - std::puts("Guest main stack: placement, TLS, bounds, guard, unwind and duplicate-entry checks passed"); + std::puts("Guest main stack: placement, TLS, x87 control and status words, bounds, guard, unwind and duplicate-entry checks passed"); std::exit(0); } @@ -70,11 +75,14 @@ int main(int argc, char**) { const auto deallocation = *reinterpret_cast(reinterpret_cast(tib) + 0x1478); originalCapacity = base - deallocation; if (argc > 1) OnGuestStack(&originalThread); + GuestCpu::SetX87Control(0x027f); + GuestCpu::SetX87Status(0x0001); try { GuestStacks::EnterGuestMainStack_nid_no_patch(&originalThread, nullptr); std::abort(); } catch (const std::invalid_argument&) { } Require(reinterpret_cast(tib->StackBase) == base); + Require(GuestCpu::X87Control() == 0x027f && (GuestCpu::X87Status() & 0xff) == 0x0001); GuestStacks::EnterGuestMainStack_nid_no_patch(&originalThread, OnGuestStack); } diff --git a/docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md b/docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md new file mode 100644 index 000000000000..db9c37e6ef57 --- /dev/null +++ b/docs/dev/debt/changes/fix-libc-windows-initial-thread-x87-control.md @@ -0,0 +1,19 @@ +# libc + +## change [acosl] + +- [acosl](../../../core/libs/prx/libc/src/Math.cpp) (libc) runs at the caller's x87 precision, as the binary does, so on a Windows initial thread at 53 bits (see `asinl`) it returns the binary's 53-bit result, not the console's 64-bit one. It follows the `acosl` of the `libc.prx` bundled with PPSA21564 (0x3e5a0): FreeBSD msun's [`e_acosl.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/src/e_acosl.c) with the ld80 `invtrig.c` coefficients that `asinl` uses, where `_Feraise(FE_INVALID)` raises `FE_INVALID` and sets `errno` to `EDOM` for |x| > 1, the infinities and NaN before `(x - x) / (x - x)`. 20 million random arguments in (-1, 1) give the same bits as that code run natively on Linux at 64-, 53- and 24-bit precision; the host `acosl` differed on 1,603,469 of them at 64 bits. Other SDK versions and the system `libSceLibcInternal` are not compared + +## change [_LLog] + +- [_LLog](../../../core/libs/prx/libc/src/DinkumwareInternals.cpp) (libc) runs at the caller's x87 precision, as the binary does, so a Windows initial thread at 53 bits (see `asinl`) gets the binary's 53-bit result, not the console's 64-bit one. It follows the `_LLog` of the `libc.prx` bundled with PPSA21564 (0x3fd50): Dinkumware's `_LDunscale` split, the 8-term `_LPoly` table and the split `ln 2`, `log10 e` and `log2 e` constants taken from the binary, with `_Feraise` raising `FE_DIVBYZERO` and `ERANGE` for a zero mantissa, pseudo-zeros included, and `FE_INVALID` and `EDOM` below zero. 4 million random arguments under bases 0, 1, -1, 7 and -9 give the same bits, `errno` and flags as that code run natively on Linux at 64-, 53- and 24-bit precision; the host `logl`, `log10l` and `log2l` differed on 869,197, 2,000,696 and 306,649 at 64 bits. Its `logl`, `log10l` and `log2l` call `_LLog` with 0, 1 and -1, and this libc forwards them there too. Other SDK versions are not compared + +## change [asinl] + +- [asinl](../../../core/libs/prx/libc/src/Math.cpp) (libc) follows the `asinl` of the `libc.prx` bundled with PPSA28319 (disassembly), FreeBSD msun's [`e_asinl.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/src/e_asinl.c) with the ld80 [`invtrig.c`](https://github.com/freebsd/freebsd-src/blob/9f0432041b8fb69832f10c32da50a67f34feeb61/lib/msun/ld80/invtrig.c) coefficients, which the binary holds as the same 80-bit constants. For |x| > 1, the infinities and NaN, Sony's error helper raises `FE_INVALID` and sets `errno` to `EDOM` before returning `(x - x) / (x - x)`, so a NaN argument also sets `EDOM`; msun sets no `errno`. 20 million random arguments in (-1, 1) give the same bits as that code run on a Windows host. A Windows initial thread outside `EnterGuestMainStack` runs at 53 bits, where `asinl(0.75)` has mantissa `…e000`, not `…e041`. The system `libSceLibcInternal` that RetroArch (PPSA99169) imports is assumed to have the same code + +# libkernel + +## change [Guest thread x87 control word] + +- [Guest thread x87 control word](../../../core/libs/prx/libkernel/Pthread/src/Thread.cpp) (libkernel) - not measured on a console, and only on Windows does the emulator load `0x037f` into the initial thread, when the entry stub enters through libc's `EnterGuestMainStack` (`libc.prx` imported, or unscoped imports); otherwise the initial thread keeps the host's start word, `0x27f` on Windows and `0x037f` on Linux and macOS unless the host changed it. A new guest thread starts with `0x037f` (64-bit precision, every exception masked) whatever its creator uses. That is FreeBSD 11's start state, whose [`cpu_copy_thread`](https://github.com/freebsd/freebsd-src/blob/release/11.0.0/sys/amd64/amd64/vm_machdep.c#L428-L446) leaves a new thread's FPU state uninitialized so that its first FPU instruction loads `fpu_initialstate` (`__INITIAL_FPUCW__`), and the control word of the libSceFiber option that starts fibers with the default FPU state