//! 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. Blocks found //! wholly uncommitted are skipped while they remain so; one query can //! confirm a whole region, including ordinary reservations between views. //! //! 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::{Condvar, Mutex, RwLock}; /// 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; /// Numbers the askings; 0 is "never". Counted up with the journal held /// (shared will do: what notes an answer holds it alone). static ASKED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); fn next_asking() -> u64 { ASKED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1 } struct Journal { /// Page number → the asking that last found it written. written: BTreeMap, /// Page ranges asked ahead for a submission's refreshes ([`ask_ahead`]) /// and the asking that did: `writes` answers for a range inside one /// from the journal, without another kernel call. ahead: Vec<(usize, usize)>, ahead_asked: u64, /// The ahead asking stands for a whole submission ([`ask_once`]): no /// other ahead asking is made, and `ahead` stays, until /// [`ask_once_end`]. kept: bool, } /// Read by many threads at once (a submission's refreshes run on several), /// written only to note an answer: the kernel is asked, and view memory /// guarded, with the range held in [`ASKING`] and not under this lock. Under /// one lock for all of it those threads ran one at a time: 22 of 96 looks at /// the submitting thread, and one in six of those at the six threads beside /// it, found it waiting for that lock (run pooloff1). static JOURNAL: RwLock = RwLock::new(Journal { written: BTreeMap::new(), ahead: Vec::new(), ahead_asked: 0, kept: false, }); fn journal() -> std::sync::RwLockReadGuard<'static, Journal> { JOURNAL.read().unwrap_or_else(|e| e.into_inner()) } fn journal_mut() -> std::sync::RwLockWriteGuard<'static, Journal> { JOURNAL.write().unwrap_or_else(|e| e.into_inner()) } /// The page ranges some thread is asking about: asking forgets what it /// answers, so until the answer is in the journal no other thread may ask /// about any of the same pages (it would be told they were not written). /// Taken before the journal's lock, never under it. static ASKING: (Mutex>, Condvar) = (Mutex::new(Vec::new()), Condvar::new()); /// Pages `[first, last)` held in [`ASKING`] until dropped. struct Asking(usize, usize); impl Asking { fn hold(first: usize, last: usize) -> Asking { let mut held = ASKING.0.lock().unwrap_or_else(|e| e.into_inner()); while held.iter().any(|&(a, b)| a < last && first < b) { held = ASKING.1.wait(held).unwrap_or_else(|e| e.into_inner()); } held.push((first, last)); Asking(first, last) } /// Everything: an asking for a whole submission. fn hold_all() -> Asking { Asking::hold(0, usize::MAX) } } impl Drop for Asking { fn drop(&mut self) { let mut held = ASKING.0.lock().unwrap_or_else(|e| e.into_inner()); if let Some(i) = held.iter().position(|&r| r == (self.0, self.1)) { held.swap_remove(i); } drop(held); ASKING.1.notify_all(); } } thread_local! { /// Room for the kernel's answer. static FOUND: std::cell::RefCell> = const { std::cell::RefCell::new(Vec::new()) }; } /// The pages of `[first, last)` the kernel says were written since it was /// last asked, in order, each once; `None` if the range is not write-watched /// memory of one reservation. The caller holds the range ([`Asking`]). fn kernel_written(first: usize, last: usize) -> Option> { use windows_sys::Win32::System::Memory::GetWriteWatch; const WRITE_WATCH_FLAG_RESET: u32 = 1; FOUND.with(|found| { let mut found = found.borrow_mut(); let mut count = (last - first) * PAGE / KERNEL_PAGE; if found.len() < count { found.resize(count, 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, found.as_mut_ptr().cast(), &mut count, &mut granularity, ) } != 0; if failed { return None; } #[cfg(feature = "diagnostics")] { use crate::copystat::{C, add}; add(C::Watches, 1); add(C::WatchUs, started.elapsed().as_micros() as u64); slow(started.elapsed().as_micros() as u64); } let mut pages: Vec = found[..count].iter().map(|&at| at / PAGE).collect(); pages.dedup(); Some(pages) }) } /// Ranges of one submission closer than this are asked about together: /// walking the pages between them costs less than another kernel call (a /// call that resets written pages costs 30-70 microseconds whatever the /// range). const AHEAD_GAP: usize = (8 << 20) / PAGE; /// The most one asking covers (its answer needs 8 bytes per 4 KiB page). const AHEAD_MAX: usize = (1 << 30) / PAGE; /// Asks the kernel about many ranges `(start, end)` at once, before the /// [`writes`] of each: ranges close together are one asking, and `writes` /// then answers from the journal until [`ask_ahead_done`]. The world's /// submissions refresh thousands of host copies (6,664 askings a frame, /// 456 ms, in run 14). Ranges that are not write-watched, or lie alone, are /// left to `writes`. pub(crate) fn ask_ahead(ranges: &[(usize, usize)]) { let mut pieces: Vec<(usize, usize)> = ranges .iter() .filter(|r| r.0 < r.1) .map(|&(start, end)| (start / PAGE, end.div_ceil(PAGE))) .collect(); if pieces.len() < 2 { return; } pieces.sort_unstable(); // The submission's one asking stands ([`ask_once`]): what it does not // hold is left to `writes`. if journal().kept { return; } let _all = Asking::hold_all(); let (mut asked, mut written) = (Vec::new(), Vec::new()); let mut i = 0; while i < pieces.len() { let first = pieces[i].0; let mut last = pieces[i].1; let mut k = i + 1; while k < pieces.len() && pieces[k].0 <= last + AHEAD_GAP && last.max(pieces[k].1) - first <= AHEAD_MAX { last = last.max(pieces[k].1); k += 1; } ask_run(&pieces[i..k], &mut asked, &mut written); i = k; } let mut j = journal_mut(); j.ahead_asked = next_asking(); j.ahead = asked; let now = j.ahead_asked; j.written.extend(written.into_iter().map(|page| (page, now))); } /// Since the last frame line: the kernel askings that took a millisecond /// or more, their microseconds, and the longest asking's. An asking walks /// page tables, some ten microseconds for most ranges; in run 75 the 2,644 /// askings of a frame took 219 ms in the lines where the pass over the /// heap's views ran long, 25-50 ms in the others. These numbers say /// whether a few askings wait for something or all of them are slow. #[cfg(feature = "diagnostics")] static SLOW: [std::sync::atomic::AtomicU64; 3] = [const { std::sync::atomic::AtomicU64::new(0) }; 3]; #[cfg(feature = "diagnostics")] fn slow(us: u64) { use std::sync::atomic::Ordering::Relaxed; if us >= 1000 { SLOW[0].fetch_add(1, Relaxed); SLOW[1].fetch_add(us, Relaxed); } SLOW[2].fetch_max(us, Relaxed); } /// [`SLOW`], taken. #[cfg(feature = "diagnostics")] pub(crate) fn slow_asks() -> [u64; 3] { [0, 1, 2].map(|i| SLOW[i].swap(0, std::sync::atomic::Ordering::Relaxed)) } /// The submission's refreshes are done: `writes` asks the kernel again. pub(crate) fn ask_ahead_done() { let mut j = journal_mut(); if !j.kept { j.ahead.clear(); } } /// Whether a submission asks the kernel once, before its first refresh, /// about every range its lists refresh (`VICTORY_ASK_ONCE=1`; off by /// default). /// /// A submission's refreshes are done in groups, between the title's own /// buffer copies (each copy flushes the refreshes recorded before it), and /// every group asked the kernel again: 2,644 askings a frame, 70 a /// submission. With the switch the journal answers all of them from one /// asking made when the submission starts. /// /// What changes: memory the title's other threads write while the /// submission is being prepared is seen by the next submission's asking, /// not by this one's later groups. What this submission's lists read was /// written before they were submitted, so before the asking. Our own /// writes into title memory while preparing are told to the journal /// ([`wrote`]) or end the asking ([`ask_once_end`]). pub(crate) fn once() -> bool { static ON: std::sync::OnceLock = std::sync::OnceLock::new(); // On by default since run 81 (with card buffers: 375-450 ms a frame // against 573-647, the picture unchanged over four minutes); // `VICTORY_ASK_ONCE=0` asks per range, as before. *ON.get_or_init(|| std::env::var_os("VICTORY_ASK_ONCE").is_none_or(|v| v != "0")) } /// With [`once`]: asks about `ranges` (everything a submission's lists /// refresh) as [`ask_ahead`] does, and lets that asking stand until /// [`ask_once_end`]. pub(crate) fn ask_once(ranges: &[(usize, usize)]) { if !once() { return; } ask_once_end(); ask_ahead(ranges); let _all = Asking::hold_all(); let mut ahead = { let mut j = journal_mut(); if j.ahead.is_empty() { // `ask_ahead` asked nothing (fewer than two ranges). j.ahead_asked = next_asking(); } std::mem::take(&mut j.ahead) }; // `ask_ahead` leaves a range that lies alone to `writes`, which asks // once per flush: with one asking for the submission that was every // such range asked again at each of its 70 flushes (run 76: 2,300 // askings a frame with the switch on, as without it). They are asked // here, once; what is not write-watched stays with `writes`. let mut written = Vec::new(); for &(start, end) in ranges { if start >= end { continue; } let (first, last) = (start / PAGE, end.div_ceil(PAGE)); if !ahead.iter().any(|&(a, b)| a <= first && last <= b) && let Some(pages) = kernel_written(first, last) { written.extend(pages); ahead.push((first, last)); } } let mut j = journal_mut(); let now = j.ahead_asked; j.written.extend(written.into_iter().map(|page| (page, now))); j.kept = !ahead.is_empty(); j.ahead = ahead; } /// The submission's one asking stands no longer: its refreshes are done, /// or title memory was written in a way the journal was not told. pub(crate) fn ask_once_end() { let mut j = journal_mut(); if j.kept { j.kept = false; j.ahead.clear(); } } /// We wrote title memory `[at, at + len)` while a submission's one asking /// stands (the title's buffer copies, done at submission): its pages count /// as written after that asking, so the refreshes that follow copy them. pub(crate) fn wrote(at: usize, len: usize) { let mut j = journal_mut(); if !j.kept || len == 0 { return; } let now = next_asking(); // What is copied from now on counts as copied after these writes (fork // PR 410): answers older than the writes in them made every refresh // until the next asking copy the pages again. j.ahead_asked = now; for page in at / PAGE..at.saturating_add(len).div_ceil(PAGE) { j.written.insert(page, now); } } /// One kernel asking for `pieces` (page ranges in order); by halves if it /// fails, as a run across two reservations or into memory that is not /// write-watched does. A piece alone is left to [`writes`]. Adds what was /// asked to `asked` and the pages found written to `written`. fn ask_run(pieces: &[(usize, usize)], asked: &mut Vec<(usize, usize)>, written: &mut Vec) { if pieces.len() < 2 { return; } let first = pieces[0].0; let last = pieces.iter().map(|p| p.1).max().unwrap_or(first); if let Some(pages) = kernel_written(first, last) { written.extend(pages); asked.push((first, last)); return; } let (a, b) = pieces.split_at(pieces.len() / 2); ask_run(a, asked, written); ask_run(b, asked, written); } /// What [`writes`] would say of `[start, end)` from the journal alone, if /// the range lies in one asked ahead ([`ask_ahead`], [`ask_once`]): the /// pages written since tracking began and the asking that last found each /// written, with the number of the asking that stands (as [`writes`] gives /// it). `None` if the kernel would have to be asked. pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>)> { let (first, last) = (start / PAGE, end.div_ceil(PAGE)); let j = journal(); j.ahead .iter() .any(|&(a, b)| a <= first && last <= b) .then(|| (j.ahead_asked, j.written.range(first..last).map(|(&p, &w)| (p, w)).collect())) } /// 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)>)> { let (first, last) = (start / PAGE, end.div_ceil(PAGE)); let of = |j: &Journal| -> Vec<(usize, u64)> { j.written.range(first..last).map(|(&p, &w)| (p, w)).collect() }; // Asked ahead with the submission's other ranges: the journal has it. let ahead = |j: &Journal| j.ahead.iter().any(|&(a, b)| a <= first && last <= b).then(|| (j.ahead_asked, of(j))); if let Some(known) = ahead(&journal()) { return Some(known); } // Asked, and the answer noted, with the range held: nothing else asks // about any of it in between. let _range = Asking::hold(first, last); if let Some(known) = ahead(&journal()) { return Some(known); } let pages = match kernel_written(first, last) { Some(pages) => pages, None => { if !guarded { return None; } #[cfg(feature = "diagnostics")] let guarding = std::time::Instant::now(); let written = guard(first * PAGE, last * PAGE); #[cfg(feature = "diagnostics")] { use crate::copystat::{C, add}; let us = guarding.elapsed().as_micros() as u64; add(C::GuardUs, us); if written.is_some() { add(C::Watches, 1); add(C::WatchUs, us); slow(us); } } written? } }; if pages.is_empty() { // Nothing to note: the others may go on reading. let j = journal(); return Some((next_asking(), of(&j))); } let mut j = journal_mut(); let now = next_asking(); j.written.extend(pages.into_iter().map(|page| (page, now))); Some((now, of(&j))) } /// 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, /// Blocks already copied as zeros. While they remain wholly uncommitted, /// the copy can skip hundreds of MB of failing kernel reads per world frame. absent: BTreeSet, } static GUARDS: Mutex = Mutex::new(Guards { protected: BTreeSet::new(), clean: BTreeSet::new(), absent: BTreeSet::new(), }); fn guards() -> std::sync::MutexGuard<'static, Guards> { GUARDS.lock().unwrap_or_else(|e| e.into_inner()) } /// The 4 KiB pages of title memory that what shaders wrote was put into, /// and the frame it last was. static LANDED: Mutex>> = Mutex::new(None); /// What shaders wrote is about to go into title memory at `[at, at + len)`. pub(crate) fn landing(at: usize, len: usize) { { let frame = crate::frame::current(); let mut landed = LANDED.lock().unwrap_or_else(|e| e.into_inner()); let landed = landed.get_or_insert_with(Default::default); for page in at / 4096..=(at + len.max(1) - 1) / 4096 { landed.insert(page, frame); } } writing(at, len); } /// Whether shaders' writes went into a page of `[at, at + len)` in the last /// `frames` frames. pub(crate) fn landed_lately(at: usize, len: usize, frames: u64) -> bool { let since = crate::frame::current().saturating_sub(frames); let landed = LANDED.lock().unwrap_or_else(|e| e.into_inner()); landed.as_ref().is_some_and(|landed| { (at / 4096..=(at + len.max(1) - 1) / 4096).any(|page| landed.get(&page).is_some_and(|&f| f >= since)) }) } /// Whether every block of `[at, at + len)` is view memory written since a /// host copy last took it: guarded then, and the guard is off. (Written by /// the title, or by us putting a flight's shader writes into it.) Not for /// blocks no copy ever took, which were never guarded: the caller's range /// lies in a copy a flight was given, which took all of it. pub(crate) fn written_since_taken(at: usize, len: usize) -> bool { if len == 0 { return false; } let g = guards(); (at / BLOCK..=(at + len - 1) / BLOCK) .all(|b| !g.protected.contains(&b) && !g.clean.contains(&b) && !g.absent.contains(&b)) } /// 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> { // A range whose first block the guards hold already (read-only by our // hand, or noted absent) is a view: no question to Windows. The // question walks the page tables to the end of the pages that share the // first one's state, and a guarded view is one long run of read-only // pages: 0.6 ms for the title's 174 MB heap, 16 times a frame, and most // of the 68 ms a frame of "guards" (run 58). // `VICTORY_GUARD_ASK=1` asks every time, as before. static ASK: std::sync::OnceLock = std::sync::OnceLock::new(); let known = !*ASK.get_or_init(|| std::env::var_os("VICTORY_GUARD_ASK").is_some_and(|v| v == "1")) && { let g = guards(); let first = start / BLOCK; g.protected.contains(&first) || g.absent.contains(&first) }; if !known && !is_view(start, end) { 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)); // The range's clean blocks, in order: read off the set in one pass, // not looked up one block at a time. A frame asks about some 10 GB of // view memory, 150,000 blocks, nearly all clean: the lookups were most // of the 60-90 ms a frame of "guards" (run 54). What the loop adds to // the set meanwhile is the block it stands at or ones before it. let clean: Vec = g.clean.range(first..last).copied().collect(); if clean.len() == last - first { // Nothing written, mapped or new since the range was last taken. return Some(written); } let mut next_clean = 0; let mut uncommitted_until = first; for block in first..=last { while next_clean < clean.len() && clean[next_clean] < block { next_clean += 1; } let is_clean = next_clean < clean.len() && clean[next_clean] == block; let mut open = block < last && !is_clean; if open && g.absent.contains(&block) { // Ordinary commits need no runtime mapping notification. Reuse // this asking's query for the following blocks in the same region. if block >= uncommitted_until { uncommitted_until = uncommitted_end(block); } open = block >= uncommitted_until; if open { g.absent.remove(&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) } /// Whether `[start, end)` is view memory. The title's views lie in /// placeholders, mapped and unmapped block by block as it streams, so a /// range can begin in a block that is not mapped right now: the first /// pages of the range that are there decide. A range with nothing there /// counts as a view (its blocks are noted absent, and looked at again when /// the runtime maps them). /// /// Looking at the first address only took such a range for "not a view": /// its copy was marked not watched for good and copied whole at every /// refresh, around its unmapped pages (run 54: 300 such ranges, 570 MB /// copied a frame for them, 255 copies a frame reading around holes). /// `VICTORY_GUARD_HOLES=0` looks at the first address only, as before. fn is_view(start: usize, end: usize) -> bool { use windows_sys::Win32::System::Memory::{ MEM_COMMIT, MEM_MAPPED, MEMORY_BASIC_INFORMATION, VirtualQuery, }; static HOLES: std::sync::OnceLock = std::sync::OnceLock::new(); let holes = *HOLES.get_or_init(|| std::env::var_os("VICTORY_GUARD_HOLES").is_none_or(|v| v != "0")); let mut at = start; loop { // 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)) }; if n == 0 { return false; } if info.Type == MEM_MAPPED { return true; } // Committed memory that is no view (the title's heaps, ours), or // the answer as it was before. if info.State == MEM_COMMIT || !holes { return false; } // A hole (a placeholder, or free): on to what follows it. let next = (info.BaseAddress as usize).saturating_add(info.RegionSize); if next <= at || next >= end { return true; } at = next; } } /// Makes blocks `[from, to)` read-only and notes them guarded and clean; /// block by block if they span views, leaving out any that fail, and /// noting those not committed (unmapped) absent. 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); } else if block < uncommitted_end(block) { g.absent.insert(block); } } } /// The end (exclusive) of the whole uncommitted blocks starting at `block`. fn uncommitted_end(block: usize) -> usize { use windows_sys::Win32::System::Memory::{ MEM_FREE, MEM_RESERVE, 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((block * BLOCK) as *const _, &mut info, size_of_val(&info)) }; if n != 0 && matches!(info.State, MEM_RESERVE | MEM_FREE) { (info.BaseAddress as usize).saturating_add(info.RegionSize) / BLOCK } else { block } } /// 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) { // The length is the title's (`fread`'s size times count, saturated). open(&mut guards(), at, at.saturating_add(len)); // A placed texture there changes with no descriptor written. crate::descriptor::host::remapped(at, len); } /// Held shared by a read of title memory in place, and alone by the runtime /// while it changes what title memory shows. /// /// The runtime does not change a mapping in one step. To unmap one page of /// a view it unmaps the whole view and maps what stays again /// (`xmem.rs` `unmap`), and a view it maps is a reservation until the next /// call commits it (`map`, then `commit`). For that moment pages the title /// never gave up are "reserved, never committed", and a thread reading /// them in place takes an access violation: run 71 in `Layout::detile`, /// run 74 in the hash before it, both under `Staging::refresh_now`, which /// had asked whether the texture was all mapped and been told yes. Asking /// cannot help; not changing the mapping while such a read is under way /// does. static MAPPING: std::sync::RwLock<()> = std::sync::RwLock::new(()); /// What the runtime holds while it changes what title memory shows /// ([`title_memory_remapping`]). pub struct Remapping(#[allow(dead_code)] std::sync::RwLockWriteGuard<'static, ()>); /// The runtime is about to map, unmap or move physical pages of title /// memory: until what it returns is dropped, no read of title memory in /// place is under way and none begins ([`title_memory_reading`]). Taken /// around the whole change, the unmapping and the mapping again. pub fn title_memory_remapping() -> Remapping { Remapping(MAPPING.write().unwrap_or_else(|e| e.into_inner())) } /// What a read of title memory in place holds ([`title_memory_reading`]). pub struct Reading(#[allow(dead_code)] std::sync::RwLockReadGuard<'static, ()>); /// For reading title memory in place: until what it returns is dropped, /// the runtime changes no mapping, so memory found mapped stays mapped. /// Not to be taken twice by one thread, nor held across anything that /// maps title memory. pub fn title_memory_reading() -> Reading { Reading(MAPPING.read().unwrap_or_else(|e| e.into_inner())) } /// [`title_memory_reading`] if it can be had at once: `None` while the /// runtime changes a mapping (or waits to), for a caller that has another /// way to read and would rather not wait. pub fn title_memory_reading_now() -> Option { match MAPPING.try_read() { Ok(guard) => Some(Reading(guard)), Err(std::sync::TryLockError::Poisoned(e)) => Some(Reading(e.into_inner())), Err(std::sync::TryLockError::WouldBlock) => None, } } /// The runtime mapped or unmapped title memory `[at, at + len)`: what it /// shows is new and read-write, so it is no longer guarded (nor known /// unmapped). pub fn title_memory_remapped(at: usize, len: usize) { // A placed texture there arrives, goes or changes with no descriptor // written. crate::descriptor::host::remapped(at, len); // The views the runtime told us of there are no longer what it shows. crate::inplace::views_gone(at, len); // And nothing stays in place over it. crate::inplace::title_memory_freeing(at, len); let mut g = guards(); let range = at / BLOCK..at.saturating_add(len).div_ceil(BLOCK); let absent: Vec = g.absent.range(range.clone()).copied().collect(); for block in absent { g.absent.remove(&block); } let blocks: Vec = g.protected.range(range).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. pub(crate) 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; // A fault of our own copies (`crate::safecopy`): whatever it is, if // it is not a block we guard, the copy fails and goes on from there. let ours = crate::safecopy::failing_exit(r.ExceptionAddress as usize); let fail = |exit: usize| { // SAFETY: the context the system passes, to go on from. unsafe { (*(*info).ContextRecord).Rip = exit as u64 }; EXCEPTION_CONTINUE_EXECUTION }; if r.ExceptionCode != EXCEPTION_ACCESS_VIOLATION as NTSTATUS || r.NumberParameters < 2 || r.ExceptionInformation[0] != WRITE { return match ours { Some(exit) => fail(exit), None => EXCEPTION_CONTINUE_SEARCH, }; } let at = r.ExceptionInformation[1]; // Held across both checks: another thread may have opened the block // since this one faulted, and a copy may have guarded it again since // (then it is ours again, not read-only for some other reason). let mut g = guards(); if open(&mut g, at, at + 1) { #[cfg(feature = "diagnostics")] crate::copystat::add(crate::copystat::C::GuardFaults, 1); return EXCEPTION_CONTINUE_EXECUTION; } if writable(at) { return EXCEPTION_CONTINUE_EXECUTION; } match ours { Some(exit) => fail(exit), None => 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. let landed = unsafe { std::slice::from_raw_parts((view + 100) as *const u8, 13) }; assert_eq!(landed, 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); } #[test] fn writes_in_partly_committed_view_blocks_are_reported() { use windows_sys::Win32::Foundation::{CloseHandle, INVALID_HANDLE_VALUE}; use windows_sys::Win32::System::Memory::{ CreateFileMappingW, FILE_MAP_WRITE, MEMORY_MAPPED_VIEW_ADDRESS, MapViewOfFile, SEC_RESERVE, UnmapViewOfFile, }; const SIZE: usize = 3 * BLOCK; struct View(MEMORY_MAPPED_VIEW_ADDRESS); impl Drop for View { fn drop(&mut self) { title_memory_remapped(self.0.Value as usize, SIZE); // SAFETY: our view is still mapped. unsafe { UnmapViewOfFile(self.0) }; } } // SAFETY: a new reserved pagefile section. The view holds its own // reference after we close the handle, including on assertion failure. let view = unsafe { let section = CreateFileMappingW( INVALID_HANDLE_VALUE, std::ptr::null(), PAGE_READWRITE | SEC_RESERVE, 0, SIZE as u32, std::ptr::null(), ); assert!(!section.is_null()); let view = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, SIZE); CloseHandle(section); assert!(!view.Value.is_null()); View(view) }; let start = view.0.Value as usize; let commit = |at: usize, len| { // SAFETY: these pages belong to our reserved view. let committed = unsafe { VirtualAlloc(at as _, len, MEM_COMMIT, PAGE_READWRITE) }; assert_eq!(committed as usize, at); }; let early = start + BLOCK + 3 * KERNEL_PAGE; let late = start + 2 * BLOCK + 3 * KERNEL_PAGE; commit(start, BLOCK); commit(early, KERNEL_PAGE); let refresh = || guard(start, start + SIZE).unwrap(); assert_eq!(refresh(), [start / PAGE, early / PAGE, late / PAGE]); // The last block is still entirely reserved. assert!(!refresh().contains(&(late / PAGE))); commit(late, KERNEL_PAGE); // Ordinary commit, without a runtime mapping notification. for at in [early, late] { assert!(writable(at)); // SAFETY: each address is inside its committed read-write page. unsafe { (at as *mut u8).write_volatile(1) }; } assert_eq!(refresh(), [early / PAGE, late / PAGE]); } /// A range of view memory whose first block is not mapped is a view /// all the same: its blocks are reported once, then the ones written /// or mapped since. #[test] fn a_range_that_begins_in_an_unmapped_block_is_guarded() { use windows_sys::Win32::System::Memory::{ CreateFileMappingW, MEM_PRESERVE_PLACEHOLDER, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE_PLACEHOLDER, MapViewOfFile3, PAGE_NOACCESS, UnmapViewOfFile2, VirtualAlloc2, }; use windows_sys::Win32::System::Threading::GetCurrentProcess; // SAFETY: a fresh placeholder of three blocks and a pagefile // section of three; the middle block is mapped first. let (p, section) = unsafe { let p = VirtualAlloc2( GetCurrentProcess(), std::ptr::null(), 3 * BLOCK, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, std::ptr::null_mut(), 0, ) as usize; assert_ne!(p, 0); let section = CreateFileMappingW( -1isize as _, std::ptr::null(), PAGE_READWRITE, 0, (3 * BLOCK) as u32, std::ptr::null(), ); assert!(!section.is_null()); (p, section) }; let map = |block: usize| { // SAFETY: splits the placeholder at the block and maps the // section's block there. let view = unsafe { VirtualFree( (p + block * BLOCK) as *mut _, BLOCK, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER, ); MapViewOfFile3( section, GetCurrentProcess(), (p + block * BLOCK) as *const _, (block * BLOCK) as u64, BLOCK, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, std::ptr::null_mut(), 0, ) }; assert_eq!(view.Value as usize, p + block * BLOCK); view }; let pages = |block: usize| (p + block * BLOCK) / PAGE..(p + (block + 1) * BLOCK) / PAGE; let all: Vec = (0..3).flat_map(pages).collect(); // Nothing mapped at all: a view with every block absent. assert!(is_view(p, p + 3 * BLOCK)); let middle = map(1); title_memory_remapped(p + BLOCK, BLOCK); // The range begins in the placeholder, its middle block is a view. assert!(is_view(p, p + 3 * BLOCK)); assert_eq!(guard(p, p + 3 * BLOCK), Some(all)); assert_eq!(guard(p, p + 3 * BLOCK), Some(Vec::new()), "nothing written since"); // SAFETY: inside the mapped middle block. unsafe { ((p + BLOCK + 8) as *mut u8).write_volatile(1) }; assert_eq!(guard(p, p + 3 * BLOCK), Some(pages(1).collect())); let first = map(0); title_memory_remapped(p, BLOCK); assert_eq!(guard(p, p + 3 * BLOCK), Some(pages(0).collect())); assert_eq!(guard(p, p + 3 * BLOCK), Some(Vec::new())); // Memory of ours that is no view stays none. let mut local = [0u8; 16]; let at = local.as_mut_ptr() as usize; assert!(!is_view(at, at + local.len())); // SAFETY: our views and placeholder. unsafe { UnmapViewOfFile2(GetCurrentProcess(), first, MEM_PRESERVE_PLACEHOLDER); UnmapViewOfFile2(GetCurrentProcess(), middle, MEM_PRESERVE_PLACEHOLDER); for block in 0..3 { VirtualFree((p + block * BLOCK) as *mut _, 0, MEM_RELEASE); } windows_sys::Win32::Foundation::CloseHandle(section); } title_memory_remapped(p, 3 * BLOCK); } /// Blocks of a placeholder between views are reported once, then not /// again (they cannot be guarded) until the runtime maps them. #[test] fn unmapped_blocks_are_reported_once_until_mapped() { use windows_sys::Win32::System::Memory::{ CreateFileMappingW, MEM_PRESERVE_PLACEHOLDER, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE_PLACEHOLDER, MapViewOfFile3, PAGE_NOACCESS, UnmapViewOfFile2, VirtualAlloc2, }; use windows_sys::Win32::System::Threading::GetCurrentProcess; // SAFETY: a fresh placeholder of three blocks and a pagefile // section of two; block 0 shows the section's first block, as the // runtime maps physical pages. let (p, section) = unsafe { let p = VirtualAlloc2( GetCurrentProcess(), std::ptr::null(), 3 * BLOCK, MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, PAGE_NOACCESS, std::ptr::null_mut(), 0, ) as usize; assert_ne!(p, 0); let section = CreateFileMappingW( -1isize as _, std::ptr::null(), PAGE_READWRITE, 0, (2 * BLOCK) as u32, std::ptr::null(), ); assert!(!section.is_null()); (p, section) }; let map = |block: usize| { // SAFETY: splits the placeholder at the block and maps the // section's block there. let view = unsafe { VirtualFree( (p + block * BLOCK) as *mut _, BLOCK, MEM_RELEASE | MEM_PRESERVE_PLACEHOLDER, ); MapViewOfFile3( section, GetCurrentProcess(), (p + block * BLOCK) as *const _, (block * BLOCK) as u64, BLOCK, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, std::ptr::null_mut(), 0, ) }; assert_eq!(view.Value as usize, p + block * BLOCK); view }; let first = map(0); let pages = |block: usize| (p + block * BLOCK) / PAGE..(p + (block + 1) * BLOCK) / PAGE; let all: Vec = (0..3).flat_map(pages).collect(); assert_eq!(guard(p, p + 3 * BLOCK), Some(all)); assert_eq!( guard(p, p + 3 * BLOCK), Some(Vec::new()), "nothing written since" ); let second = map(1); title_memory_remapped(p + BLOCK, BLOCK); assert_eq!(guard(p, p + 3 * BLOCK), Some(pages(1).collect())); assert_eq!(guard(p, p + 3 * BLOCK), Some(Vec::new())); // SAFETY: our views and placeholder. unsafe { UnmapViewOfFile2(GetCurrentProcess(), first, MEM_PRESERVE_PLACEHOLDER); UnmapViewOfFile2(GetCurrentProcess(), second, MEM_PRESERVE_PLACEHOLDER); for block in 0..3 { VirtualFree((p + block * BLOCK) as *mut _, 0, MEM_RELEASE); } windows_sys::Win32::Foundation::CloseHandle(section); } title_memory_remapped(p, 3 * BLOCK); } /// A write or mapping that runs past the end of the address space (a /// title's `fread` with a huge count) covers up to the end; the sum used /// to overflow. Up there no test has guarded blocks. #[test] fn ranges_past_the_address_space_end_do_not_overflow() { let top = usize::MAX - 16; writing(top, PAGE); title_memory_remapped(top, usize::MAX); } } #[cfg(test)] mod mapping_tests { use super::{title_memory_reading, title_memory_remapping}; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, mpsc}; use std::time::Duration; /// A change of mapping waits for a read in place that is under way, and /// a read in place waits for a change of mapping. #[test] fn reads_in_place_and_remappings_wait_for_each_other() { // A reader holds; the change does not begin until it lets go. let changed = Arc::new(AtomicBool::new(false)); let reading = title_memory_reading(); let (began, wait) = mpsc::channel(); let change = { let changed = changed.clone(); std::thread::spawn(move || { began.send(()).unwrap(); let _changing = title_memory_remapping(); changed.store(true, Ordering::SeqCst); }) }; wait.recv().unwrap(); std::thread::sleep(Duration::from_millis(100)); assert!(!changed.load(Ordering::SeqCst), "the mapping changed under a read in place"); drop(reading); change.join().unwrap(); assert!(changed.load(Ordering::SeqCst)); // A change holds; the read does not begin until it is done. let read = Arc::new(AtomicBool::new(false)); let changing = title_memory_remapping(); let (began, wait) = mpsc::channel(); let reader = { let read = read.clone(); std::thread::spawn(move || { began.send(()).unwrap(); let _reading = title_memory_reading(); read.store(true, Ordering::SeqCst); }) }; wait.recv().unwrap(); std::thread::sleep(Duration::from_millis(100)); assert!(!read.load(Ordering::SeqCst), "a read in place began during a change of mapping"); drop(changing); reader.join().unwrap(); assert!(read.load(Ordering::SeqCst)); } } #[cfg(test)] mod many_askers_tests { use super::{PAGE, writes}; use windows_sys::Win32::System::Memory::*; /// Threads that write pages and ask about overlapping ranges at the same /// time: whoever wrote a page before asking is told of that write (by /// its own asking or by one that took the kernel's answer before it). #[test] fn no_write_is_lost_between_askers() { const MEM_WRITE_WATCH: u32 = 0x20_0000; const PAGES: usize = 48; const THREADS: usize = 8; // SAFETY: a reservation of our own, freed at the end. let at = unsafe { VirtualAlloc(std::ptr::null(), PAGES * PAGE, MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH, PAGE_READWRITE) } as usize; assert!(at != 0 && at.is_multiple_of(PAGE)); std::thread::scope(|s| { for t in 0..THREADS { s.spawn(move || { let mut last = [0u64; PAGES]; let mut seed = t as u64 * 7919 + 1; let mut next = |n: usize| { seed = seed.wrapping_mul(6364136223846793005).wrapping_add(1442695040888963407); (seed >> 33) as usize % n }; for _ in 0..4000 { let page = next(PAGES); let from = page - next(page.min(12) + 1); let to = page + 1 + next((PAGES - page).min(12)); // SAFETY: committed memory of ours; what the racing // writers leave in it does not matter. unsafe { ((at + page * PAGE + t * 8) as *mut u64).write_volatile(1) }; let (now, written) = writes(at + from * PAGE, at + to * PAGE, false).expect("write-watched"); let number = written.iter().find(|w| w.0 == at / PAGE + page).map_or(0, |w| w.1); assert!(number > last[page], "page {page}: {number} is not after {}", last[page]); for l in &mut last[from..to] { *l = now; } } }); } }); // SAFETY: the reservation made above. unsafe { VirtualFree(at as *mut _, 0, MEM_RELEASE) }; } }