From c702d7d573f6b829d77b633c83a160f91f2f31c0 Mon Sep 17 00:00:00 2001 From: b3IKK <278540256+b3IKK@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:47:22 +0000 Subject: [PATCH] kernelx: VirtualAllocEx for this process is the title's allocator VirtualAllocEx forwarded every call to Windows' VirtualAllocEx with the console's allocation flags sanitised, so for this process - the title's own memory - MEM_GRAPHICS was dropped and the title got a plain reservation the GPU has no address for. VirtualAlloc is where the console's memory comes from: placeholders, the sparse windows, video memory in the unified region, 64 KiB pages, the write-watched graphics fallback. VirtualFreeEx already routed this process back to VirtualFree for exactly those allocations; the allocation side did not. Route VirtualAllocEx(GetCurrentProcess(), ...) to VirtualAlloc, as VirtualFreeEx does to VirtualFree. Assisted-by: OpenCode (Gemini 4 Neon) --- crates/xcl/src/hle/kernelx.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index c0b8c236ac..6047a84f4b 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -464,6 +464,15 @@ pub unsafe extern "system" fn VirtualFreeEx(proc_: HANDLE, addr: *mut c_void, si } pub unsafe extern "system" fn VirtualAllocEx(proc_: HANDLE, addr: *const c_void, size: usize, t: u32, p: u32) -> *mut c_void { + // this process is the title's: what it asks for itself goes where VirtualAlloc puts it – + // graphics memory, a sparse window, the unified region – and not into plain Windows memory + // the GPU has no address for (`sanitize_alloc_type` drops MEM_GRAPHICS) + // this process is the title's: what it asks for itself goes where VirtualAlloc puts it – + // graphics memory, a sparse window, the unified region – and not into plain Windows memory + // the GPU has no address for (`sanitize_alloc_type` drops MEM_GRAPHICS) + if proc_ == thr::GetCurrentProcess() { + return VirtualAlloc(addr, size, t, p); + } mem::VirtualAllocEx(proc_, addr, size, sanitize_alloc_type(t), sanitize_protect(p)) } @@ -2858,6 +2867,26 @@ mod tests { } } + /// `VirtualAllocEx(GetCurrentProcess(), …)` is the title asking for its own memory: it must + /// be the title's (graphics memory here), not a plain Windows reservation the GPU cannot see. + #[test] + fn virtual_alloc_ex_for_this_process_is_the_titles_allocator() { + unsafe { + if crate::gpu::uma::get().is_none() { + return; + } + let cur = thr::GetCurrentProcess(); + let q = VirtualAllocEx(cur, std::ptr::null(), 0x1_0000, mem::MEM_RESERVE | MEM_GRAPHICS, mem::PAGE_READWRITE); + assert!(!q.is_null(), "a graphics reservation"); + let a = q as usize; + assert!( + crate::gpu::uma::get().is_some_and(|u| u.contains(a) || u.is_sparse(a)), + "VirtualAllocEx(GetCurrentProcess()) must land in the title's memory, got {a:#x}" + ); + assert_eq!(VirtualFreeEx(cur, q, 0, mem::MEM_RELEASE), 1); + } + } + #[test] fn virtual_free_ex_and_sparse_validation() { unsafe {