fix(kernel): reserve guest stack window before host GPU driver init #116
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!116
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/116/head"
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?
@Scarlet-Raine on GitHub · 2026-07-27 23:46 UTC · original
Scarlet-Raine:kernel-guest-stack-reserve→main· +95 −2 · 5 files · 1 commits · closed without mergingUsing build
8fe4765the tested game Grand Theft Auto: San Andreas The Definitive Edition dies instantly on boot withVirtualAlloc() failed: 0x000001e7 Not implemented (result != OK) in src/kernel/pthread.cpp:932
Error 0x1e7 is Windows' ERROR_INVALID_ADDRESS, a memory allocation was requested but the memory address is already occupied, the emulator gives every guest thread a stack that MUST live at exact addresses. On windows when the emu inits Vulkan, the GPU Driver (NVIDIA Game Ready Driver 610.74) reserves a massive section of address space for itself, measured ~256 GiB of reserved private memory in one block.
The blob covered the stack's expected range, when the game's main threa asked for it, it received an occupied signal and the emulator aborted before the game even started.
Note: The same blob also covers the preferred ELF load address (0x900000000), but it survives because it's allowed to relocate the binary elsewhere.
This symptom involves a degree of RNG as it most likely differs per machine/driver.
The Fix
TLDR: fix(kernel): stake the memory territory before the GPU has a chance to, avoiding the occupied memory address conflict altogether.
src/kernel/pthread.h— the stack-top constant moved here asGUEST_STACK_TOP, plus a newGUEST_STACK_AREA_SIZE(1 GiB) so both the pthread code and the memory code agree on the window.src/kernel/memory.cpp— newReserveGuestStackArea(), called at the end of Memory subsystem init. The Memory subsystem initializes before the Graphics subsystem (which is when the driver does its thing), so the 1 GiB stack window below GUEST_STACK_TOP is reserved early, as a Windows "placeholder" reservation registered as a Reserved range namedguest_stack_area.src/common/platform/sysWindowsVirtual.cpp— better diagnostics: when a fixed allocation fails, it now prints the requested address/size AND asks Windows what's occupying the range (VirtualQuery loop). This is how the root cause was found, and it stays in to make the next conflict obvious.Fully generic fix, no game specific workarounds implemented.
Investigation methodology
PthreadCreateMainGuestStack→CreateGuestStack→KernelMapNamedFlexibleMemory(MAP_FIXED)→VirtualAllocat fixed addr.SysVirtualAllocFixed.Testing
New regression test:
GuestStackAreaIsReservedForFixedStackMapsintests/VirtualMemoryAllocationTests.cpp. It checks the window is registered after Memory init, then performs a MAP_FIXED stack-style mapping inside it (same flags asCreateGuestStack), writes/reads the memory, and unmaps. Passes.Full local test sweep (12 suites): 10 pass. The 2 failures are PRE-EXISTING and were proven unrelated by disabling the new
ReserveGuestStackArea()call and re-running — they fail identically without the fix:virtual_memory_allocation_tests→DirectMapUnmapReusesHostAddress(remap address changes 0x840000 → 0xc40000; deterministic on this machine, needs its own investigation)shader_recompiler_compute_tests→ImageTransitionState("combined depth/stencil mip copy lost an aspect"; likely GPU/driver-dependent).Runtime: GTA SA: TDE previously aborted in <2 s. With the fix, a 150 s bounded run had zero fatal errors across a 1.2M-line log, played the 4 intro videos (AvPlayer), and reached in-engine rendering (render targets, instanced draws, dozens of guest threads), menu is reachable, the next block is on the post main menu loading splash screen probably texture related.
Host used for testing: Windows 11 23H2, Ryzen 7 7745HX, NVIDIA RTX 4070 Laptop GPU (NVIDIA Game Ready Driver 610.74), Vulkan SDK 1.4.350, clang-cl (VS 2022 Build Tools), Release build.
Tested with whole GTA Remaster Trilogy, same behavior across the board
AI Disclosure
The investigation and initial implementation were AI-assisted, I personally managed, reviewed the diff, understood the root cause, rebuilt, ran the full test suite including the new regression test, and verified my test game boots (the intro videos play, the menu is reachable, you can continue past the menu but it hangs right after). Several MCP servers were used on top of repo wiki indexing, if anyone needs any of the MCPs IDA Pro (decompiler), PS5RS (binary analysis toolkit, Kyty MCP (Homebrew, for logging and emulation runs).
Files in the diff (5 files, +95/-2)
@Nmzik on GitHub · 2026-07-28 00:20 UTC · original
@Scarlet-Raine Thanks! I’ll take a look. Just wanted to mention that the current implementation in KytyPS5 is kind of hacky: CreateGuestStack calls KernelMapNamedFlexibleMemory, even though stack memory shouldn’t be allocated from flexible memory at all. We also still don’t have a single proper memory pool combining direct and flexible memory. Anyway, I’ll take a look. Thank you!
@Nmzik on GitHub · 2026-07-29 04:01 UTC · original · approved these changes
@Scarlet-Raine on GitHub · 2026-07-29 09:12 UTC · original
I don't mean to be pushy but can this be merged sooner rather than later? 3/5 reports in discord are due to this exact error, thanks
@annahana on GitHub · 2026-07-29 10:50 UTC · original
Please merge as fast as possible. Without that fix I cannot test any progress
@Crispy81 on GitHub · 2026-07-29 21:58 UTC · original
Didn't know there was a discord for this emulator yet.
@Nmzik on GitHub · 2026-07-29 22:04 UTC · original · edited 2026-07-29 22:05 UTC
@Scarlet-Raine @Crispy81 @annahana I see the issue, guys. I couldn’t reproduce it on three separate Windows machines with different hardware/NVIDIA GPUs/driver versions. I’ll update to exactly 610.74 (currently using 610.47) and report back. Same game runs perfectly fine till main menu
@Scarlet-Raine on GitHub · 2026-07-30 00:30 UTC · original
Interesting, i just ran with baseline main @
85622be, and both minecraft and gta sa fail on it, still VirtualAlloc() failed: 0x000001e7, i really have no idea why it can't be reproducedI don't think it's official we've just ended up in this one after sharpemu went tts up https://discord.com/invite/emulation
@Nmzik on GitHub · 2026-07-30 00:53 UTC · original
Does it happen only with these two specific games, or with basically every game? Also, make sure the Windows page file is enabled and sufficiently large.
@annahana on GitHub · 2026-07-30 03:45 UTC · original
it Happens on every game i have tested
KytyPS5-2026-07-29-85622be
my Pagefile is 12.460.429.312 Bytes
if it helps
_kyty.txt
and a small video of my games.
https://github.com/user-attachments/assets/24d12191-d052-45b9-a428-4e218507200a
Operating System: Windows 10; CPU: AMD Ryzen 9 9950X3D; GPU: AMD Radeon RX 7900 XT, RAM: 253,6 GB
@Nmzik on GitHub · 2026-07-31 00:42 UTC · original · edited 2026-07-31 01:59 UTC
@Scarlet-Raine This should now be fixed. Please retest it. Credit goes to you for finding the root cause. Linux support may be regressed, though. We’ll fix it later.
PS: I couldnt reproduce this issue on any of my machines, but this fix should work.
@annahana on GitHub · 2026-07-31 05:02 UTC · original
Seems good to me with the new build
Pull request closed