From b54f6fe555c1b4cf275bacae5ab92dda2ec1a9ab Mon Sep 17 00:00:00 2001 From: JFyuhong Date: Tue, 24 Nov 2026 14:55:22 +0800 Subject: [PATCH 1/2] kernelx: track paired 64 KiB views across sparse reservations A successful sparse reservation returned before mirror tracking saw it. When a CPU view fit the sparse windows and its GPU view fell back to host address space, texture uploads read the empty GPU view. Register the reservation on each allocation path and invalidate cached texture sources when either end is released. Assisted-by: Codex (GPT-6) --- README.md | 2 + crates/xcl/src/hle/kernelx.rs | 18 +++++--- crates/xcl/tests/sparse_mirrors.rs | 66 ++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 crates/xcl/tests/sparse_mirrors.rs diff --git a/README.md b/README.md index 4d1932d2a4..8c07725d2c 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ next steps: console upscaler runs a whole frame on a PC GPU, the GDK HTTP library initialises. - **Console textures decode exactly** – the GFX10 tiling and mip layout reproduce 909 of 912 textures of the game's texture archive byte for byte, its volume textures included. + Texture uploads can read the paired CPU view of a GPU-only 64 KiB page reservation, including + when the CPU view is in a sparse window. - **370+ automated tests**, including GPU tests that compare translated shaders against expected results instruction by instruction. diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 5d2be0114f..edfc76e45a 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -203,8 +203,11 @@ pub mod mirrors { pub fn released(base: usize) { let mut s = STATE.lock().unwrap(); s.cpu.retain(|&(b, _)| b != base); - s.gpu.remove(&base); - s.gpu.retain(|_, &mut (_, c)| c != base); + let paired = s.gpu.len(); + s.gpu.retain(|&g, &mut (_, c)| g != base && c != base); + if s.gpu.len() != paired { + super::GPU_VIEW_MAP_GEN.fetch_add(1, std::sync::atomic::Ordering::Release); + } } /// The CPU-view address behind `va` when `va` lies in a GPU view. @@ -251,6 +254,7 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: uma.map(|u| u.sparse_release(q)); return std::ptr::null_mut(); } + mirrors::reserved(q, size, t, p); trace_mem("VirtualAlloc (64K pages, sparse)", 0, size, t as u64, p, q); return q as *mut c_void; } @@ -284,10 +288,12 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: uma.and_then(|u| u.sparse_reserve(size)) }; if let Some(q) = reserved { + mirrors::reserved(q, size, t, p); trace_mem("VirtualAlloc (graphics reserve, sparse)", 0, size, t as u64, p, q); return q as *mut c_void; } if let Some(q) = uma.and_then(|u| u.reserve(size, 0x1_0000)) { + mirrors::reserved(q as usize, size, t, p); trace_mem("VirtualAlloc (graphics reserve, UMA)", 0, size, t as u64, p, q as usize); return q as *mut c_void; } @@ -295,6 +301,7 @@ pub unsafe extern "system" fn VirtualAlloc(addr: *const c_void, size: usize, t: if !q.is_null() { GRAPHICS_RESERVATIONS.lock().unwrap().insert(q as usize, size); GRAPHICS_RESERVATIONS_GEN.fetch_add(1, Ordering::Release); + mirrors::reserved(q as usize, size, t, p); return q; } } @@ -429,12 +436,14 @@ pub unsafe extern "system" fn VirtualFree(addr: *mut c_void, size: usize, t: u32 } } if release { + mirrors::released(a); u.sparse_release(a); } return 1; } if let Some(u) = crate::gpu::uma::get().filter(|u| u.contains(addr as usize)) { if t & 0x8000 != 0 { + mirrors::released(addr as usize); return u.free(addr as usize) as BOOL; } if t & 0x4000 != 0 && size > 0 { @@ -819,11 +828,10 @@ pub fn physical_page_at(va: usize) -> bool { AWAY.lock().unwrap().as_ref().is_some_and(|a| a.at.contains_key(&(va & !(PHYS_PAGE - 1)))) } -/// Counts the calls that map or unmap physical pages in a GPU view (`mirrors`): what -/// `physical_page_at` says there may have changed. +/// Changes when pages are mapped or unmapped in a GPU view or its backing reservation is lost. static GPU_VIEW_MAP_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); -/// Generation of the physical-page mappings inside GPU views. +/// Generation of the backing memory seen by textures in GPU views. pub fn gpu_view_map_gen() -> u64 { GPU_VIEW_MAP_GEN.load(Ordering::Acquire) } diff --git a/crates/xcl/tests/sparse_mirrors.rs b/crates/xcl/tests/sparse_mirrors.rs new file mode 100644 index 0000000000..31533d4ef0 --- /dev/null +++ b/crates/xcl/tests/sparse_mirrors.rs @@ -0,0 +1,66 @@ +use windows_sys::Win32::System::Memory::{MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE}; +use xcl::hle::kernelx::{gpu_view_map_gen, in_graphics_reservation, mirrors, VirtualAlloc, VirtualFree}; + +// In its own process so no other test can allocate the address just released. +#[test] +fn sparse_64k_views_pair_and_forget_reused_addresses() { + let Some(uma) = xcl::gpu::uma::get().filter(|u| u.has_sparse()) else { + xcl::gpu::recomp::runner::skip_test("no sparse UMA address space"); + return; + }; + let size = 3 * 65536; + let reserve = |size, protection| { + let p = unsafe { VirtualAlloc(std::ptr::null(), size, MEM_RESERVE | 0x2040_0000, protection) }; + assert!(!p.is_null(), "failed to reserve {size:#x} bytes with protection {protection:#x}"); + p + }; + let gpu_protection = PAGE_NOACCESS | 0x800; // PAGE_GRAPHICS_NOACCESS + let cpu = reserve(size, PAGE_READWRITE); + let gpu = reserve(size, gpu_protection); + assert!(uma.is_sparse(cpu as usize) && uma.is_sparse(gpu as usize)); + let sample = gpu as usize + 65536; + let backing = cpu as usize + 65536; + assert_eq!(mirrors::source(sample), Some(backing)); + + unsafe { + let paired = gpu_view_map_gen(); + assert_eq!(VirtualFree(gpu, size, MEM_RELEASE), 0); + assert_eq!(VirtualFree(gpu, size, MEM_DECOMMIT), 1); + assert_eq!(gpu_view_map_gen(), paired); + assert_eq!(mirrors::source(sample), Some(backing)); + + assert_eq!(VirtualFree(gpu, 0, MEM_RELEASE), 1); + assert_eq!(mirrors::source(sample), None); + assert_eq!(gpu_view_map_gen(), paired + 1); + let replacement = reserve(size, PAGE_READWRITE); + assert_eq!(replacement, gpu); + assert_eq!(mirrors::source(sample), None); + assert_eq!(VirtualFree(replacement, 0, MEM_RELEASE), 1); + assert_eq!(gpu_view_map_gen(), paired + 1); + + let gpu_again = reserve(size, gpu_protection); + assert_eq!(gpu_again, gpu); + assert_eq!(mirrors::source(sample), Some(backing)); + let paired = gpu_view_map_gen(); + assert_eq!(VirtualFree(cpu, 0, MEM_RELEASE), 1); + assert_eq!(mirrors::source(sample), None); + assert_eq!(gpu_view_map_gen(), paired + 1); + assert_eq!(VirtualFree(gpu_again, 0, MEM_RELEASE), 1); + assert_eq!(gpu_view_map_gen(), paired + 1); + + // Only the first of two reservations this large fits in the sparse windows. + let sparse_bytes: usize = uma.watch_regions().into_iter() + .filter(|&(base, _)| uma.is_sparse(base)).map(|(_, len)| len).sum(); + let size = sparse_bytes / 2 + 65536; + let cpu = reserve(size, PAGE_READWRITE); + let gpu = reserve(size, gpu_protection); + assert!(uma.is_sparse(cpu as usize)); + assert!(!uma.contains(gpu as usize) && in_graphics_reservation(gpu as usize)); + assert_eq!(mirrors::source(gpu as usize + 65536), Some(cpu as usize + 65536)); + let paired = gpu_view_map_gen(); + assert_eq!(VirtualFree(gpu, 0, MEM_RELEASE), 1); + assert_eq!(mirrors::source(gpu as usize + 65536), None); + assert_eq!(gpu_view_map_gen(), paired + 1); + assert_eq!(VirtualFree(cpu, 0, MEM_RELEASE), 1); + } +} From 1b2b636a034faba6dafb0e3ed37cb0cffb286d4a Mon Sep 17 00:00:00 2001 From: JFyuhong Date: Tue, 24 Nov 2026 16:17:20 +0800 Subject: [PATCH 2/2] kernelx: refresh texture sources when a GPU view regains its pair A texture registered while a mirror pair was live kept the bare GPU address after the title released that view and paired its address again. Publish the mapping generation on insertion too, so the next texture submit can pick up the CPU backing. Cover same-address reuse independently of allocator placement and exercise the surviving texture on D3D12. Assisted-by: Codex (GPT-6) --- README.md | 3 +- crates/xcl/src/hle/d3d12x/title_textures.rs | 16 ++++++-- crates/xcl/src/hle/kernelx.rs | 3 +- crates/xcl/tests/sparse_mirrors.rs | 41 ++++++++++++++++++--- 4 files changed, 51 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8c07725d2c..f33ba53e55 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,8 @@ next steps: - **Console textures decode exactly** – the GFX10 tiling and mip layout reproduce 909 of 912 textures of the game's texture archive byte for byte, its volume textures included. Texture uploads can read the paired CPU view of a GPU-only 64 KiB page reservation, including - when the CPU view is in a sparse window. + when the CPU view is in a sparse window. A texture registered in a paired view follows its + new CPU backing if the title releases and reuses the GPU address. - **370+ automated tests**, including GPU tests that compare translated shaders against expected results instruction by instruction. diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs index 7332fd32c7..ad6f9657a8 100644 --- a/crates/xcl/src/hle/d3d12x/title_textures.rs +++ b/crates/xcl/src/hle/d3d12x/title_textures.rs @@ -410,9 +410,8 @@ fn mirror_source(va: usize) -> usize { if mapped { va } else { src } } -/// Physical pages were mapped or unmapped in a GPU view since the last look: textures there -/// whose source changed (a page now mapped under one, or gone from it) read from the new one, -/// uploaded again at this submit. +/// A GPU view's backing reservation or physical page mappings changed since the last look. +/// Textures whose source changed read from the new one and are uploaded again at this submit. fn recheck_gpu_view_sources() { static SEEN: AtomicU64 = AtomicU64::new(0); let gen = crate::hle::kernelx::gpu_view_map_gen(); @@ -1228,7 +1227,8 @@ mod tests { } // Larger than all four UMA windows together. These public allocations // must use the watched Windows fallback, reserving space rather than RAM. - const SIZE: usize = 24usize << 30; + // Distinct from the 24 GiB reservation in the parallel kernelx bookkeeping test. + const SIZE: usize = (24usize << 30) + PAGE; const OFFSET: usize = 0x2_0000; const PAGE: usize = 0x1_0000; struct Reservation(usize); @@ -1345,6 +1345,14 @@ mod tests { refresh(queue.as_raw()); assert_eq!(read_r8(dev, &queue, &texture), vec![0x9A; 64 * 64], "unmapping restores the earlier CPU view"); assert_eq!(address(key), Some(va)); + // Keep the texture alive while its GPU address loses and regains its CPU view. + kernelx::mirrors::released(gpu.0); + recheck_gpu_view_sources(); + assert_eq!(TEXTURES.lock().unwrap().by_res[&key].src, va); + kernelx::mirrors::reserved(gpu.0, SIZE, 0x2040_2000, 0x801); + recheck_gpu_view_sources(); + let src_after_reuse = TEXTURES.lock().unwrap().by_res[&key].src; + assert_eq!(src_after_reuse, src); dev.GetDeviceRemovedReason().unwrap(); } } diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index edfc76e45a..fbb85676ff 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -192,6 +192,7 @@ pub mod mirrors { let taken: Vec = s.gpu.values().map(|&(_, c)| c).collect(); if let Some(&(cpu, _)) = s.cpu.iter().rev().find(|&&(b, n)| n == size && !taken.contains(&b)) { s.gpu.insert(base, (size, cpu)); + super::GPU_VIEW_MAP_GEN.fetch_add(1, std::sync::atomic::Ordering::Release); crate::xlog_info!("kernelx: reservation {base:#x} ({size:#x} bytes, no CPU access) is the GPU view of {cpu:#x}"); } } else if p & 0xFF != super::mem::PAGE_NOACCESS { @@ -828,7 +829,7 @@ pub fn physical_page_at(va: usize) -> bool { AWAY.lock().unwrap().as_ref().is_some_and(|a| a.at.contains_key(&(va & !(PHYS_PAGE - 1)))) } -/// Changes when pages are mapped or unmapped in a GPU view or its backing reservation is lost. +/// Changes when a GPU view gains or loses its backing reservation or physical page mappings. static GPU_VIEW_MAP_GEN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); /// Generation of the backing memory seen by textures in GPU views. diff --git a/crates/xcl/tests/sparse_mirrors.rs b/crates/xcl/tests/sparse_mirrors.rs index 31533d4ef0..b8f9d8fc30 100644 --- a/crates/xcl/tests/sparse_mirrors.rs +++ b/crates/xcl/tests/sparse_mirrors.rs @@ -1,9 +1,34 @@ use windows_sys::Win32::System::Memory::{MEM_DECOMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_NOACCESS, PAGE_READWRITE}; use xcl::hle::kernelx::{gpu_view_map_gen, in_graphics_reservation, mirrors, VirtualAlloc, VirtualFree}; -// In its own process so no other test can allocate the address just released. +// Only these tests run in this process and change the generation. +static TEST_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + #[test] -fn sparse_64k_views_pair_and_forget_reused_addresses() { +fn reusing_a_gpu_view_with_new_backing_notifies_its_textures() { + let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); + // Bookkeeping only. Supply the same GPU address on both reservations. + let (first, second, gpu, size) = (0x10_0000, 0x20_0000, 0x30_0000, 0x2_0000); + let reserve = |base, p| mirrors::reserved(base, size, MEM_RESERVE | 0x2040_0000, p); + reserve(first, PAGE_READWRITE); + reserve(gpu, PAGE_NOACCESS | 0x800); + assert_eq!(mirrors::source(gpu + 65536), Some(first + 65536)); + mirrors::released(gpu); + mirrors::released(first); + let unpaired = gpu_view_map_gen(); + assert_eq!(mirrors::source(gpu + 65536), None); + reserve(second, PAGE_READWRITE); + assert_eq!(gpu_view_map_gen(), unpaired); + reserve(gpu, PAGE_NOACCESS | 0x800); + assert_eq!(mirrors::source(gpu + 65536), Some(second + 65536)); + assert_eq!(gpu_view_map_gen(), unpaired + 1, "notify a surviving texture of the new backing"); + mirrors::released(second); + mirrors::released(gpu); +} + +#[test] +fn sparse_64k_views_pair_and_forget_released_addresses() { + let _guard = TEST_LOCK.lock().unwrap_or_else(|e| e.into_inner()); let Some(uma) = xcl::gpu::uma::get().filter(|u| u.has_sparse()) else { xcl::gpu::recomp::runner::skip_test("no sparse UMA address space"); return; @@ -15,7 +40,9 @@ fn sparse_64k_views_pair_and_forget_reused_addresses() { p }; let gpu_protection = PAGE_NOACCESS | 0x800; // PAGE_GRAPHICS_NOACCESS + let before = gpu_view_map_gen(); let cpu = reserve(size, PAGE_READWRITE); + assert_eq!(gpu_view_map_gen(), before); let gpu = reserve(size, gpu_protection); assert!(uma.is_sparse(cpu as usize) && uma.is_sparse(gpu as usize)); let sample = gpu as usize + 65536; @@ -24,6 +51,7 @@ fn sparse_64k_views_pair_and_forget_reused_addresses() { unsafe { let paired = gpu_view_map_gen(); + assert_eq!(paired, before + 1, "notify when the sparse views first pair"); assert_eq!(VirtualFree(gpu, size, MEM_RELEASE), 0); assert_eq!(VirtualFree(gpu, size, MEM_DECOMMIT), 1); assert_eq!(gpu_view_map_gen(), paired); @@ -33,14 +61,14 @@ fn sparse_64k_views_pair_and_forget_reused_addresses() { assert_eq!(mirrors::source(sample), None); assert_eq!(gpu_view_map_gen(), paired + 1); let replacement = reserve(size, PAGE_READWRITE); - assert_eq!(replacement, gpu); - assert_eq!(mirrors::source(sample), None); + assert_eq!(mirrors::source(replacement as usize + 65536), None); assert_eq!(VirtualFree(replacement, 0, MEM_RELEASE), 1); assert_eq!(gpu_view_map_gen(), paired + 1); let gpu_again = reserve(size, gpu_protection); - assert_eq!(gpu_again, gpu); + let sample = gpu_again as usize + 65536; assert_eq!(mirrors::source(sample), Some(backing)); + assert_eq!(gpu_view_map_gen(), paired + 2); let paired = gpu_view_map_gen(); assert_eq!(VirtualFree(cpu, 0, MEM_RELEASE), 1); assert_eq!(mirrors::source(sample), None); @@ -48,7 +76,8 @@ fn sparse_64k_views_pair_and_forget_reused_addresses() { assert_eq!(VirtualFree(gpu_again, 0, MEM_RELEASE), 1); assert_eq!(gpu_view_map_gen(), paired + 1); - // Only the first of two reservations this large fits in the sparse windows. + // Two reservations this large exceed all sparse windows together. These flags contain + // neither MEM_GRAPHICS nor MEM_COMMIT, so there is no coherent-window fallback. let sparse_bytes: usize = uma.watch_regions().into_iter() .filter(|&(base, _)| uma.is_sparse(base)).map(|(_, len)| len).sum(); let size = sparse_bytes / 2 + 65536;