Kernel reads, remapped views, freed memory and lost writes of title memory

- What the kernel is writing (file reads) is not guarded by a refresh before
  the call is done (`kernel_writing`): the read would have failed.
- A title thread that uses a page while the runtime remaps another page of
  the same view waits for that in the fault handler; it faulted on a page
  the title never gave up.
- Blocks being unmapped are not guarded again, memory the title frees leaves
  no notes of its blocks (its VirtualFree is taken over), and other memory at
  an address once guarded or absent keeps its protection.
- The second part of a submission's one asking has a number of its own.
- What shaders wrote waits for a change of mapping instead of being lost;
  what could not be written is counted and said; zeros into title memory
  that went are no fault.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
localandClaude Opus 5.5 committed 2026-11-24 23:10:44 -06:00
1 parent 9f22d574bc
commit 8a475da9cf
9 files changed
+803 -32

No files matched your search

+68 -2
View File
@@ -1136,11 +1136,24 @@ pub extern "system" fn XMemTransferMemory(kind: u32, size: usize) -> BOOL {
moved.into()
}
/// The title's own `VirtualFree`. What the GPU side noted of the memory goes
/// first: the address may be other memory's the moment it is free.
extern "system" fn title_virtual_free(address: *mut c_void, size: usize, kind: u32) -> BOOL {
const MEM_COALESCE_PLACEHOLDERS: u32 = 1;
// Splitting and joining placeholders changes nothing they show.
if kind & (MEM_PRESERVE_PLACEHOLDER | MEM_COALESCE_PLACEHOLDERS) == 0 {
victory_gpu::title_memory_freed(address as usize, size);
}
// SAFETY: the title's own arguments.
unsafe { VirtualFree(address, size, kind) }
}
pub(crate) fn module() -> HleModule {
HleModule {
name: "xmem",
dlls: &["xmem.dll"],
dlls: &["xmem.dll", "kernel32.dll", "kernelbase.dll", "api-ms-win-core-memory-*"],
exports: vec![
crate::HleExport { name: "VirtualFree", address: title_virtual_free as *const () as usize },
hle_export!(XMemAlloc),
hle_export!(XMemFree),
hle_export!(XMemAllocDefault),
@@ -1383,7 +1396,9 @@ mod tests {
let stop = stop.clone();
std::thread::spawn(move || {
let mut reads = 0u64;
while !stop.load(Ordering::Relaxed) {
// (Once at least: the mapping thread may be done before this
// one's first turn.)
while reads == 0 || !stop.load(Ordering::Relaxed) {
let _reading = victory_gpu::title_memory_reading();
// SAFETY: the page is mapped whenever no change of
// mapping is under way, and none is while `_reading`
@@ -1405,6 +1420,57 @@ mod tests {
assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1);
}
/// A title thread that reads and writes a page of a view while other
/// pages of the view are unmapped and mapped again, as the console lets
/// it: the page was never given up.
#[test]
fn a_title_thread_goes_on_using_the_rest_of_a_view() {
use std::sync::atomic::{AtomicBool, Ordering};
let pages = allocate(4);
for _ in 0..200 {
// A new reservation each time: the pieces of a view cut up stay
// cut up.
let a = XMemVirtualAlloc(
std::ptr::null_mut(),
4 * PAGE,
MEM_RESERVE | PAGES_64K,
0,
PAGE_READWRITE,
);
assert!(!a.is_null());
assert_eq!(XMemMapPhysicalPages(a, 4, pages.as_ptr()), a);
let (first, stop) = (a as usize, AtomicBool::new(false));
// SAFETY: mapped read-write.
unsafe { (first as *mut u64).write_volatile(0) };
std::thread::scope(|s| {
let title = s.spawn(|| {
let mut n = 0u64;
// SAFETY (the block): the first page is the title's all
// the time; a fault would end the process.
unsafe {
while !stop.load(Ordering::Relaxed) {
assert_eq!((first as *const u64).read_volatile(), n);
n += 1;
(first as *mut u64).write_volatile(n);
}
}
n
});
// SAFETY: as above.
while !title.is_finished() && unsafe { (first as *const u64).read_volatile() } < 100 {}
for page in [3, 2, 1] {
let at = a.wrapping_byte_add(page * PAGE);
assert_eq!(XMemMapPhysicalPages(at, 1, std::ptr::null()), at);
assert_eq!(XMemMapPhysicalPages(at, 1, pages[page..].as_ptr()), at);
}
stop.store(true, Ordering::Relaxed);
assert!(title.join().expect("the title's thread never faulted") > 0);
});
assert_eq!(XMemMapPhysicalPages(a, 4, std::ptr::null()), a);
}
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
}
#[test]
fn mapping_no_pages_changes_nothing() {
let pages = allocate(2);