//! Which title memory the title wrote since a host copy took it (decision //! 0007, "Copies of title memory"). The runtime reserves title GPU memory //! with write watching (`victory-hle` xmem), so the kernel keeps a dirty bit //! per 4 KiB page; asking for a range's written pages also clears them. Host //! copies of the same memory can overlap (nested buffers, textures in //! buffers), so what one asks is kept here for the others: a journal of //! each page's last write, numbered by the asking. //! //! Physical-page views (xmem's sections) cannot be write-watched. There a //! copy leaves each 64 KiB block it took read-only, and our vectored handler //! takes the first write to it: it notes the block as written and gives the //! write access back. The runtime tells us when it maps or unmaps views //! ([`title_memory_remapped`]), which resets their protection. //! //! shortcut: writes into physical pages through another view (xmem's //! `write_physical`, DirectStorage's page destinations) are not seen at //! the guarded addresses; they go to the texture pool, whose textures are //! hashed, not guarded. A system call writing into a guarded block fails //! (none is known to). use std::collections::{BTreeMap, BTreeSet}; use std::sync::Mutex; /// The unit tracked and copied: 64 KiB, the console's page (the kernel's /// 4 KiB pages fold into it). Finer units made the checks of the world's /// big buffers cost more than the copies they saved. pub(crate) const PAGE: usize = 64 << 10; /// The kernel's page, the unit of its answers. const KERNEL_PAGE: usize = 4096; struct Journal { /// Numbers the askings; 0 is "never". asked: u64, /// Page number → the asking that last found it written. written: BTreeMap, /// Room for the kernel's answer. found: Vec, } static JOURNAL: Mutex = Mutex::new(Journal { asked: 0, written: BTreeMap::new(), found: Vec::new(), }); /// The pages of `[start, end)` (whole pages) as last written, for pages /// written since tracking began: `(page number, asking)` in page order, and /// this asking's number, which is higher than any in the list but those the /// kernel just reported. `None` if the memory is not write-watched (nor, /// with `guarded`, view memory, which is then guarded). pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Vec<(usize, u64)>)> { use windows_sys::Win32::System::Memory::GetWriteWatch; const WRITE_WATCH_FLAG_RESET: u32 = 1; let (first, last) = (start / PAGE, end.div_ceil(PAGE)); let mut j = JOURNAL.lock().unwrap_or_else(|e| e.into_inner()); let j = &mut *j; let mut count = (last - first) * PAGE / KERNEL_PAGE; j.found.resize(count.max(j.found.len()), 0); let mut granularity = 0; #[cfg(feature = "diagnostics")] let started = std::time::Instant::now(); // SAFETY: the answer buffer holds `count` entries. let failed = unsafe { GetWriteWatch( WRITE_WATCH_FLAG_RESET, (first * PAGE) as *const _, (last - first) * PAGE, j.found.as_mut_ptr().cast(), &mut count, &mut granularity, ) } != 0; let guarded = if failed { count = 0; if !guarded { return None; } guard(first * PAGE, last * PAGE)? } else { Vec::new() }; #[cfg(feature = "diagnostics")] { use crate::copystat::{C, add}; add(C::Watches, 1); add(C::WatchUs, started.elapsed().as_micros() as u64); } j.asked += 1; let now = j.asked; let pages = j.found[..count].iter().map(|&at| at / PAGE).chain(guarded); for page in pages.collect::>() { j.written.insert(page, now); } Some(( now, j.written .range(first..last) .map(|(&p, &w)| (p, w)) .collect(), )) } /// Guarded blocks of view memory. const BLOCK: usize = PAGE; struct Guards { /// Blocks we made read-only. protected: BTreeSet, /// Of those, the ones not written since a copy took them. clean: BTreeSet, } static GUARDS: Mutex = Mutex::new(Guards { protected: BTreeSet::new(), clean: BTreeSet::new(), }); fn guards() -> std::sync::MutexGuard<'static, Guards> { GUARDS.lock().unwrap_or_else(|e| e.into_inner()) } /// For view memory: the pages of `[start, end)`'s blocks written since /// they were last guarded (all of a block's pages), guarding them again /// before the caller copies them. `None` if the memory is not a view. fn guard(start: usize, end: usize) -> Option> { use windows_sys::Win32::System::Memory::{MEM_MAPPED, MEMORY_BASIC_INFORMATION, VirtualQuery}; // SAFETY: plain data filled in by the query. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: a valid out pointer of the right size. let n = unsafe { VirtualQuery(start as *const _, &mut info, size_of_val(&info)) }; if n == 0 || info.Type != MEM_MAPPED { return None; } install(); let mut g = guards(); let mut written = Vec::new(); let mut run: Option = None; let (first, last) = (start / BLOCK, end.div_ceil(BLOCK)); for block in first..=last { let open = block < last && !g.clean.contains(&block); if open { written.extend(block * BLOCK / PAGE..(block + 1) * BLOCK / PAGE); if g.protected.contains(&block) { // Written through the runtime, still read-only. g.clean.insert(block); } else { run.get_or_insert(block); continue; } } if let Some(from) = run.take() { protect(&mut g, from, block); } } Some(written) } /// Makes blocks `[from, to)` read-only and notes them guarded and clean; /// block by block if they span views, leaving out any that fail (unmapped). fn protect(g: &mut Guards, from: usize, to: usize) { use windows_sys::Win32::System::Memory::{PAGE_READONLY, VirtualProtect}; let set = |from: usize, to: usize| { let mut old = 0; // SAFETY: title view memory; only its protection changes. unsafe { VirtualProtect( (from * BLOCK) as *const _, (to - from) * BLOCK, PAGE_READONLY, &mut old, ) != 0 } }; let note = |g: &mut Guards, from: usize, to: usize| { g.protected.extend(from..to); g.clean.extend(from..to); }; if set(from, to) { note(g, from, to); return; } for block in from..to { if set(block, block + 1) { note(g, block, block + 1); } } } /// Gives write access back to the guarded blocks of `[start, end)` and /// notes them written; true if any was guarded. fn open(g: &mut Guards, start: usize, end: usize) -> bool { use windows_sys::Win32::System::Memory::{PAGE_READWRITE, VirtualProtect}; let blocks: Vec = g .protected .range(start / BLOCK..end.div_ceil(BLOCK)) .copied() .collect(); for &block in &blocks { g.protected.remove(&block); g.clean.remove(&block); let mut old = 0; // SAFETY: a block we made read-only. unsafe { VirtualProtect((block * BLOCK) as *const _, BLOCK, PAGE_READWRITE, &mut old) }; } !blocks.is_empty() } /// Before our own write into title memory `[at, at + len)` through the /// kernel (which does not fault into our handler): opens its guarded blocks. /// The runtime calls it (as `kernel_will_write`) before the title's kernel /// reads into its own memory. pub fn writing(at: usize, len: usize) { open(&mut guards(), at, at + len); } /// The runtime mapped or unmapped title memory `[at, at + len)`: what it /// shows is new and read-write, so it is no longer guarded. pub fn title_memory_remapped(at: usize, len: usize) { let mut g = guards(); let blocks: Vec = g .protected .range(at / BLOCK..(at + len).div_ceil(BLOCK)) .copied() .collect(); for block in blocks { g.protected.remove(&block); g.clean.remove(&block); } } /// Registers the handler that takes writes to guarded blocks, once, ahead /// of the crash reporter's. fn install() { use windows_sys::Win32::Foundation::{EXCEPTION_ACCESS_VIOLATION, NTSTATUS}; use windows_sys::Win32::System::Diagnostics::Debug::{ AddVectoredExceptionHandler, EXCEPTION_CONTINUE_EXECUTION, EXCEPTION_CONTINUE_SEARCH, EXCEPTION_POINTERS, }; unsafe extern "system" fn on_write(info: *mut EXCEPTION_POINTERS) -> i32 { // SAFETY: the system passes the exception being dispatched. let r = unsafe { &*(*info).ExceptionRecord }; const WRITE: usize = 1; if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS || r.NumberParameters < 2 || r.ExceptionInformation[0] != WRITE { return EXCEPTION_CONTINUE_SEARCH; } let at = r.ExceptionInformation[1]; if open(&mut guards(), at, at + 1) { #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::GuardFaults, 1); return EXCEPTION_CONTINUE_EXECUTION; } // Another thread may have opened the block since this one faulted. if writable(at) { EXCEPTION_CONTINUE_EXECUTION } else { EXCEPTION_CONTINUE_SEARCH } } static ONCE: std::sync::Once = std::sync::Once::new(); // SAFETY: the handler only touches our guard state and protections. ONCE.call_once(|| unsafe { AddVectoredExceptionHandler(1, Some(on_write)); }); } /// Whether `at` is committed read-write memory now. fn writable(at: usize) -> bool { use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEMORY_BASIC_INFORMATION, PAGE_READWRITE, VirtualQuery, }; // SAFETY: plain data filled in by the query. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: a valid out pointer of the right size. let n = unsafe { VirtualQuery(at as *const _, &mut info, size_of_val(&info)) }; n != 0 && info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE } #[cfg(test)] mod tests { use super::*; use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_RELEASE, MEM_RESERVE, PAGE_READWRITE, VirtualAlloc, VirtualFree, }; const MEM_WRITE_WATCH: u32 = 0x20_0000; #[test] fn written_pages_are_reported_to_every_asker() { // SAFETY: a fresh write-watched allocation, freed at the end. let p = unsafe { VirtualAlloc( std::ptr::null(), 4 * PAGE, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE, ) } as usize; assert_ne!(p, 0); let (start, end) = (p, p + 4 * PAGE); let (_, before) = writes(start, end, false).expect("watched"); assert!(before.is_empty()); // SAFETY: committed pages of ours. unsafe { ((p + PAGE + 8) as *mut u8).write(1) }; let (now, after) = writes(start, end, false).expect("watched"); assert_eq!(after, [(p / PAGE + 1, now)]); // Another asker after the kernel's bit was cleared still sees it. let (later, again) = writes(p + PAGE, p + 2 * PAGE, false).expect("watched"); assert!(later > now); assert_eq!(again, [(p / PAGE + 1, now)]); let mut plain = vec![0u8; PAGE]; assert!( writes( plain.as_mut_ptr() as usize, plain.as_ptr() as usize + PAGE, true ) .is_none() ); // SAFETY: our allocation. unsafe { VirtualFree(p as *mut _, 0, MEM_RELEASE) }; } /// A kernel read into a guarded view block fails; opened first, it /// lands and the block counts as written. #[test] fn kernel_reads_into_guarded_blocks_after_opening() { use std::io::{Read, Seek, Write}; use windows_sys::Win32::System::Memory::{ CreateFileMappingW, FILE_MAP_WRITE, MapViewOfFile, UnmapViewOfFile, }; // SAFETY: a pagefile section of two blocks, mapped read-write. let view = unsafe { let section = CreateFileMappingW( -1isize as _, std::ptr::null(), PAGE_READWRITE, 0, (2 * BLOCK) as u32, std::ptr::null(), ); assert!(!section.is_null()); MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, 2 * BLOCK).Value as usize }; assert_ne!(view, 0); assert!(guard(view, view + 2 * BLOCK).is_some()); // now read-only let mut file = tempfile::tempfile().unwrap(); file.write_all(b"from the disk").unwrap(); // `ReadFile` inside: the kernel writes the view. let read = |file: &mut std::fs::File| { file.rewind().unwrap(); // SAFETY: 13 bytes of our view, nothing else refers to them. let into = unsafe { std::slice::from_raw_parts_mut((view + 100) as *mut u8, 13) }; file.read(into).is_ok_and(|n| n == 13) }; assert!(!read(&mut file), "the kernel does not fault into our handler"); writing(view + 100, 13); assert!(read(&mut file)); // SAFETY: the bytes just read. assert_eq!(unsafe { std::slice::from_raw_parts((view + 100) as *const u8, 13) }, b"from the disk"); assert_eq!(guard(view, view + 2 * BLOCK), Some((view / PAGE..view / PAGE + 1).collect())); // SAFETY: our view. unsafe { UnmapViewOfFile(windows_sys::Win32::System::Memory::MEMORY_MAPPED_VIEW_ADDRESS { Value: view as *mut _ }) }; title_memory_remapped(view, 2 * BLOCK); } }