libkernel: threads of a PS5 payload process start with MXCSR 0x9fc0 (FTZ and DAZ); AnyPS5 does not set it on guest threads #7548
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 ionut27001 on 2026-10-18T13:06:38Z.
AnyPS5 does not set the MXCSR that threads of a PS5 payload process start with (0x9fc0: FTZ and DAZ set)
In a payload process on a PS5 (firmware 12.70), every thread I measured started with MXCSR = 0x9fc0. That is the default 0x1f80 with flush-to-zero (bit 15) and denormals-are-zero (bit 6) also set. FreeBSD starts threads with 0x1f80 (
__INITIAL_MXCSR__in sys/x86/include/fpu.h). The x87 control word was 0x37f.This value does not depend on the creating thread. After the main thread set its own MXCSR to 0x1f80, the threads it created next with
pthread_createandscePthreadCreatestill started at 0x9fc0. So in this payload process a new thread does not take its MXCSR from its creator.I could not find any place on
mainwhere AnyPS5 sets MXCSR for a guest thread:scePthreadCreateincore/libs/prx/libkernel/Pthread/src/Thread.cppstarts guest threads with_beginthreadexon Windows andpthread_createelsewhere, withStartNativeThreadas the start routine. The guestpthread_creategoes through it too (Pthread/Posix/Thread.cpp). Neither function sets MXCSR, so a guest thread keeps the MXCSR the host gives it.StartNativeThread. It is the host process's initial thread. The relinker's entry stub calls the ELF entry on it (core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp,core/relinker/elfpatcher/src/general/EntryStubBuilder.cpp), and nothing undercore/relinkersets MXCSR either. I did not measure its value under AnyPS5.JumpBuffer.cpp,UserContext.cpp,Exception.cppandlibSceFiber/Export.cppsave and restore MXCSR, but none sets a starting value for a thread. A new fiber starts with the MXCSR of the thread that first runs it (PrepareInitialStackreads it with_mm_getcsr).What the host gives a new thread:
_beginthreadexthread and astd::threadboth still read 0x1f80.The x87 control word is covered by the existing
asinlentry indocs/dev/debt/libc.md, which records that guest threads do not set it either.Without FTZ and DAZ, subnormal results are kept instead of flushed, and subnormal inputs are used instead of read as zero.
What the console's own
libSceLibcInternal.sprxreturns under each MXCSR. These are the console's functions, not AnyPS5's (see theresolvelines). AnyPS5'sstrtodcalls the host'sstd::strtod(Strings.cpp), so what it returns under 0x9fc0 would depend on the host C library.strtod("4.9e-324")returns 0 withERANGEunder 0x9fc0. With MXCSR forced to 0x1f80 it returns 0x1, still withERANGE.strtof("1.17549435e-38")returns 0 withERANGEunder 0x9fc0. With 0x1f80 it returns 0x00800000 and leaveserrnoat 0.The low six bits of each
mxcsr=value are sticky exception flags. The probe does not clear them between cases, so they include flags raised by earlier calls. The control bits are 0x9fc0 and 0x1f80.For some inputs, the console's
nextafteranderfcf, from the same module, also depend on MXCSR:nextafter(0.0, 1.0)returns 0 under the start MXCSR and the smallest subnormal under 0x1f80.erfcf(10.0f)returns 0 under the start MXCSR and 0x00000002 under 0x1f80.Not every subnormal result changes.
nextafter(DBL_MIN, 0.0)returns the largest subnormal, 0x000fffffffffffff, under both values, andtgamma(-171.5)returns the same subnormal, 0x0000238ee05c87a0, under both:The
pNN:prefix names the probe; the rest of each line is copied unchanged.Not established:
lgammaof ±2^-1074 returned 0x40875427f6acad6f (about 746.52) where -log(|x|) is about 744.44. It ran only under the start MXCSR, so whether that difference comes from MXCSR is not measured.Method:
stmxcsrand the x87 control word withfnstcw. This was done in the main thread and in threads made withpthread_create(the payload's import) and withscePthreadCreate, which was resolved fromlibkernel_sys.sprx.strtod,strtofandlgamma. Itsresolvelines show that all three are thelibSceLibcInternal.sprxexports. p20 callsnextafter,erfcfandtgammathrough pointers resolved from that module.AI-assisted.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T13:13:49Z.
I checked the current thread path:
StartNativeThreadcallsRunThreadon the native host thread, where guest execution and emulator host work share that thread. Setting MXCSR once at thread start would therefore affect host-side work too. Before a patch, could you confirm whether a game-process thread was measured (the report currently says it was not), and whether the console applies0x9fc0only while guest code runs or for the full thread lifetime? That determines whether this belongs at guest-entry boundaries or in native thread initialization.Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-18T14:19:19Z.
I am taking this up to implement setting MXCSR to
0x9fc0(FTZ and DAZ) at theAps5CallGuestEntry_nid_no_patchguest-entry boundary incore/libs/prx/libkernel/Pthread/src/Thread.cpp. This preserves host MXCSR across CRT and driver threads while ensuring all guest execution starts under the measured console hardware state (0x9fc0). A focused test verifying guest MXCSR state and host preservation will be included.Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-18T14:48:49Z.
@MarcZX10 No game process was measured; everything here comes from a homebrew payload process, as the "Not established" section says. In that process there is no host-side code, so the console cannot show a difference between "only while guest code runs" and "the whole thread". What the logs do show:
pthread_createandscePthreadCreate(p15posix_thread).oracle_begin, before the p14 and p15 reads).libSceLibcInternal.sprx: the p14strtolines read 0x9ffd, which is 0x9fc0 plus sticky flags.Not measured: MXCSR after a blocking syscall, inside a signal handler, or in a game process.
@pushkarreddyy Two things from the current code that may matter for the guest-entry approach:
Aps5CallGuestEntry_nid_no_patchis only called fromRunThread, so it covers threads created throughscePthreadCreate(and the guestpthread_create, which goes through it). The guest main thread does not pass through it: the relinker's entry stub (core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp,core/relinker/elfpatcher/src/general/EntryStubBuilder.cpp) calls the ELF entry directly, and the main thread is where p14 and p15 also read 0x9fc0.finishThread) would keep the host value, if MXCSR is restored after the guest entry returns.Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-18T15:11:06Z.
macOS host data for the guest-entry approach in #7565: Rosetta 2 honours
0x9fc0the way the console value needs, so the sameldmxcsrworks on the macOS port, where guest code runs as x86_64 under Rosetta.Measured on an M2 Max, macOS 27.2, with a standalone x86_64 probe (Apple clang,
-O1, and again with-mavx2) that sets MXCSR withldmxcsrand runs the instructions through inline asm:comissdenormal vs 0 is equaladdss/addsddenormal + 0cvtss2sddenormalFLT_MIN * 1/3(mulss,vmulps),DBL_MIN * 1/3(mulsd)0x1f800x1f800x37a23450000000000x002aaaab,0x0005555555555555)0x9f80(FTZ)0x9f8000x37a234500000000000x1fc0(DAZ)0x1fc0000x9fc00x9fc0000The sticky bits after the run are what x86 sets (
DE,UE,PE:0x1fb2without DAZ,0x1ff0with it), and the AVXvaddps/vmulpsrows match the SSE ones. So FTZ and DAZ are applied separately for f32 and f64, scalar, SSE packed and AVX.The rest of the thread lifetime, with
0x9fc0set on the thread first:raise(SIGUSR1): the handler runs with0x9fc0. After it returns, the thread is back to0x9fc0, even though the handler had loaded0x1f80.SIGSEGV/SIGBUShandler that callsmprotectand returns: the same result.usleep:0x9fc0is kept.pthread_createfrom that thread: the new thread starts with0x1f80. On this host a new thread does not take its creator's MXCSR, so it gets0x9fc0only if something loads it on that thread, asAps5CallGuestEntry_nid_no_patchdoes in #7565. As @ionut27001 noted, the guest main thread does not go through that function.Not run: AnyPS5 itself.
maindoes not build in full on macOS yet, so this covers only the host behaviour that the #7565 approach relies on.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-18T16:19:14Z.
I opened #7594 for the separate main guest ELF entry path. It initializes MXCSR to 0x9fc0 immediately before guest entry in the Linux and Windows startup stubs and restores the host MXCSR if guest entry returns. This complements #7565, which covers created guest threads. The relinker-only Linux build and all 198 registered CTest tests passed; the Windows-generated stub is byte-checked on Linux, with no Windows runtime test. This does not cover the separate macOS launcher path or close this issue.
Imported from boykopovar/AnyPS5. Originally posted by pushkarreddyy on 2026-10-18T16:41:54Z.
Thanks @MarcZX10! That fits cleanly together: #7594 handles the relinker entry stubs for the main guest thread, while #7565 covers created guest threads via
Aps5CallGuestEntry_nid_no_patchand preserves0x9fc0acrossfinishThreaddestructors before restoring the worker's host MXCSR inRunThread.