diff --git a/crates/victory-gpu/src/host.rs b/crates/victory-gpu/src/host.rs index 90ccafb..1140960 100644 --- a/crates/victory-gpu/src/host.rs +++ b/crates/victory-gpu/src/host.rs @@ -1085,6 +1085,59 @@ mod tests { } } + /// Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`) are + /// placeholders to the host: their bytes are read from their pages, + /// beside memory read as before and memory that is not there (zeros), + /// and they count as committed. + #[test] + fn views_in_the_books_alone_are_read_from_their_pages() { + use crate::watch::test_books::{IN_BOOKS, books}; + use windows_sys::Win32::System::Memory::*; + const K: usize = 64 << 10; + books(); + let (pages, more) = (vec![0x22u8; K], vec![0x33u8; K]); + // SAFETY: a reservation of ours, of which the first block is + // committed and written; the second and the fourth are views in the + // books, the third is not there. + unsafe { + let at = VirtualAlloc(std::ptr::null(), 4 * K, MEM_RESERVE, PAGE_NOACCESS).cast::(); + assert!(!at.is_null()); + assert!(!VirtualAlloc(at.cast(), K, MEM_COMMIT, PAGE_READWRITE).is_null()); + at.write_bytes(0x11, K); + let views = [(at as usize + K, K, pages.as_ptr() as usize), (at as usize + 3 * K, K, more.as_ptr() as usize)]; + IN_BOOKS.lock().unwrap().extend(views); + let expected = |from: usize| match from / K { + 0 => 0x11, + 1 => 0x22, + 3 => 0x33, + _ => 0, + }; + // All there, from inside a page into the view in the books. + let mut out = vec![0xeeu8; 2 * K - 0x300]; + assert!(super::read_into(at.add(0x300), &mut out)); + assert!(out.iter().enumerate().all(|(i, &b)| b == expected(i + 0x300))); + // Over the block that is not there: no whole read; the copy has + // zeros there and the bytes around it, the views' from the pages. + let mut out = vec![0xeeu8; 4 * K]; + assert!(!super::read_into(at, &mut out)); + let mut out = vec![0xeeu8; 4 * K - 54]; + super::copy_mapped(out.as_mut_ptr(), at.add(27), 4 * K - 54, false); + for (i, &b) in out.iter().enumerate() { + assert_eq!(b, expected(i + 27), "byte {:#x}", i + 27); + } + let mut out = vec![0xeeu8; 4 * K]; + assert_eq!(super::read_there(at, &mut out), 3 * K); + assert!(out.iter().enumerate().all(|(i, &b)| b == expected(i))); + // Committed, as far as the title goes; the hole is not. + assert!(crate::texture::committed(at, 2 * K)); + assert!(crate::texture::committed(at.add(K + 5), K - 5)); + assert!(!crate::texture::committed(at, 3 * K)); + assert!(crate::texture::committed(at.add(3 * K), K)); + IN_BOOKS.lock().unwrap().retain(|view| !views.contains(view)); + assert_ne!(VirtualFree(at.cast(), 0, MEM_RELEASE), 0); + } + } + /// Zeros for a title destination that went meanwhile are no fault. #[test] fn zeros_where_nothing_is_are_lost() { @@ -8463,11 +8516,26 @@ fn read_sparse(src: *const u8, dst: *mut u8, len: usize) { let at = src.wrapping_add(offset); let to = dst.wrapping_add(offset); let remaining = len - offset; + // A view in the runtime's books alone (`VICTORY_SOFT_VIEWS`) is a + // placeholder to the host, which would have it read as zeros: its + // bytes come from the pages, and the host is asked only about what + // lies before the next such view. + let mut plain = remaining; + if let Some(soft) = crate::watch::soft() { + let (from_pages, n) = (soft.read)(at as usize, to, remaining); + if from_pages && n != 0 { + offset += n; + continue; + } + if n != 0 { + plain = n; + } + } // SAFETY: the query inspects an address and fills a local structure. let mut info: MEMORY_BASIC_INFORMATION = unsafe { std::mem::zeroed() }; let size = unsafe { VirtualQuery(at.cast(), &mut info, size_of_val(&info)) }; let n = (info.BaseAddress as usize).saturating_add(info.RegionSize) - .saturating_sub(at as usize).min(remaining); + .saturating_sub(at as usize).min(plain); if size != size_of_val(&info) || n == 0 { read_pieces(at, to, remaining); break; @@ -8569,11 +8637,38 @@ pub(crate) fn read_there(src: *const u8, dst: &mut [u8]) -> usize { /// Copies `len` bytes from `src` to `dst` unless a source page is not /// readable. fn read(src: *const u8, dst: *mut u8, len: usize) -> bool { - use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; - use windows_sys::Win32::System::Threading::GetCurrentProcess; + // Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`) are + // read from their pages, and stay as they are: no view of the host's is + // made for our reading. What lies between them is read as before. + if let Some(soft) = crate::watch::soft() { + let mut done = 0; + while done < len { + let (at, to) = (src.wrapping_add(done), dst.wrapping_add(done)); + let (mut from_pages, mut n) = (soft.read)(at as usize, to, len - done); + if !from_pages && n != 0 && !read_plain(at, to, n) { + // (Mapped so between the question and the read: once more.) + (from_pages, n) = (soft.read)(at as usize, to, len - done); + if !from_pages { + return false; + } + } + if n == 0 { + return false; + } + done += n; + } + return true; + } // (The kernel's read does not fault into the handler that makes lazily // mapped title memory real.) crate::watch::materialize(src as usize, len); + read_plain(src, dst, len) +} + +/// [`read`] of memory the host has mapped, or has not. +fn read_plain(src: *const u8, dst: *mut u8, len: usize) -> bool { + use windows_sys::Win32::System::Diagnostics::Debug::ReadProcessMemory; + use windows_sys::Win32::System::Threading::GetCurrentProcess; if crate::safecopy::on() { // SAFETY: the destination is mapped writable memory of `len` bytes; // a source page that is not there fails the copy. diff --git a/crates/victory-gpu/src/lib.rs b/crates/victory-gpu/src/lib.rs index 6fa6987..9708b93 100644 --- a/crates/victory-gpu/src/lib.rs +++ b/crates/victory-gpu/src/lib.rs @@ -119,7 +119,7 @@ pub use inplace::{title_memory_freeing, title_memory_section, title_memory_secti pub use shadow::title_memory_write_combined; #[cfg(windows)] pub use watch::{ - kernel_writing, title_memory_allocated, title_memory_freed, title_memory_reading, title_memory_remapped, - title_memory_remapping, title_memory_splitting, title_memory_unmapping, title_memory_written, use_lazy_views, - writing as kernel_will_write, + SoftViews, kernel_writing, soft_view_guards, title_memory_allocated, title_memory_freed, title_memory_made_real, + title_memory_reading, title_memory_remapped, title_memory_remapping, title_memory_splitting, title_memory_unmapping, + title_memory_written, use_lazy_views, use_soft_views, writing as kernel_will_write, }; diff --git a/crates/victory-gpu/src/texture.rs b/crates/victory-gpu/src/texture.rs index 77e49d3..601cdcb 100644 --- a/crates/victory-gpu/src/texture.rs +++ b/crates/victory-gpu/src/texture.rs @@ -1436,7 +1436,11 @@ impl Staging { } return size; } - if through_copy() { + // (A texture with pages in views the runtime keeps in its books + // alone, `VICTORY_SOFT_VIEWS`, has no memory to read in place: its + // bytes come from the pages, through the copy. `_mapped` is held: no + // view becomes one of those before the read in place below is done.) + if through_copy() || !crate::watch::soft_runs(start, size).is_empty() { return with_title(self.memory, size, |tiled| { let hash = digest(tiled); #[cfg(feature = "diagnostics")] @@ -1790,9 +1794,31 @@ fn by_views() -> bool { /// Whether `[at, at + len)` is committed memory (streamed textures may be /// unmapped until their pages arrive). pub(crate) fn committed(at: *const u8, len: usize) -> bool { + if crate::watch::soft().is_some() { + // Views the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`) + // are committed as far as the title goes, and stay as they are; + // Windows is asked about what lies between them. + let runs = crate::watch::soft_runs(at as usize, len); + if runs.is_empty() { + return committed_there(at, len); + } + let (mut from, end) = (at as usize, at as usize + len); + for (run, bytes) in runs { + if run > from && !committed_there(from as *const u8, run - from) { + return false; + } + from = run + bytes; + } + return from >= end || committed_there(from as *const u8, end - from); + } // Memory the runtime mapped lazily is committed as far as the title // goes: made real before Windows is asked. crate::watch::materialize(at as usize, len); + committed_there(at, len) +} + +/// [`committed`], of memory the host has mapped or has not. +fn committed_there(at: *const u8, len: usize) -> bool { if by_query() { return committed_by_query(at, len); } diff --git a/crates/victory-gpu/src/watch.rs b/crates/victory-gpu/src/watch.rs index 220083d..151de09 100644 --- a/crates/victory-gpu/src/watch.rs +++ b/crates/victory-gpu/src/watch.rs @@ -673,8 +673,9 @@ pub(crate) fn asked(start: usize, end: usize) -> Option<(u64, Vec<(usize, u64)>) /// with `guarded`, view memory, which is then guarded). pub(crate) fn writes(start: usize, end: usize, guarded: bool) -> Option<(u64, Vec<(usize, u64)>)> { // Guarding asks Windows about the pages and protects them: a lazy view - // is none to it yet. - if guarded { + // is none to it yet. (Not so a view in the books alone, which is + // guarded there: [`use_soft_views`].) + if guarded && soft().is_none() { materialize(start, end.saturating_sub(start)); } let (first, last) = (start / PAGE, end.div_ceil(PAGE)); @@ -2200,7 +2201,24 @@ fn protect_alias(g: &mut Guards, block: usize) -> bool { if g.protected.contains(block) { return true; } - if g.left_alone(block) || g.rests(block, rest()) { + if g.left_alone(block) { + return false; + } + // A view in the runtime's books alone ([`use_soft_views`]): nothing + // writes through it before a fault makes it a mapping of the host's, + // which opens it and every other view of its page + // ([`title_memory_made_real`]). Guarded in the books, with no call to + // the host, which has a placeholder there and would refuse: then the + // view guarded with it was never clean and copied again at every asking + // (run xw-lazy, `VICTORY_LAZY_VIEWS=1`: 2.4 to 3.4 GB of copies a frame, + // 2 fps). + if soft_block(block) { + PROTECTED.set(block); + g.protected.insert(block); + SOFT_GUARDS[1].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + return true; + } + if g.rests(block, rest()) { return false; } // Ahead of the protection. @@ -2266,12 +2284,24 @@ fn protect_here(g: &mut Guards, from: usize, to: usize) { }; // Ahead of the protection (`PROTECTED`). (from..to).for_each(|block| PROTECTED.set(block)); - if set(from, to) { + // Views in the runtime's books alone ([`use_soft_views`]) are guarded + // there: the host has a placeholder at their addresses, would refuse, + // and the block would count as not there. Nothing writes through one + // before a fault makes it real, which opens it + // ([`title_memory_made_real`]). + let soft = soft_runs(from * BLOCK, (to - from) * BLOCK); + let in_books = |block: usize| soft.iter().any(|&(at, n)| at <= block * BLOCK && (block + 1) * BLOCK <= at + n); + if soft.is_empty() && set(from, to) { note(g, from, to); return; } let mut uncommitted_until = from; for block in from..to { + if in_books(block) { + note(g, block, block + 1); + SOFT_GUARDS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed); + continue; + } // (Not one question a block of a region that is not there.) if block >= uncommitted_until && set(block, block + 1) { note(g, block, block + 1); @@ -2931,7 +2961,90 @@ pub fn use_lazy_views(materialize: fn(usize, usize) -> bool) { /// questions to Windows about the pages. (A fault in title memory, the /// title's or ours, comes to [`install`]'s handler, which asks first.) pub(crate) fn materialize(at: usize, len: usize) -> bool { - LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1))) + match soft() { + Some(soft) => (soft.materialize)(at, len.max(1)), + None => LAZY_VIEWS.get().is_some_and(|f| f(at, len.max(1))), + } +} + +/// What the runtime gives for the views it keeps in its books alone +/// (`xmem`, `VICTORY_SOFT_VIEWS`): title memory it tells of as mapped +/// ([`title_memory_remapped`], `title_memory_view`) where the host has a +/// placeholder and no view, until the title's CPU touches it. +/// +/// With these the GPU side leaves such a view as it is where it can: it +/// reads its bytes from the pages ([`SoftViews::read`], `crate::host`'s +/// reads; a texture all or partly in such views is read through a copy), +/// takes it for committed (`crate::texture::committed`) and guards it in +/// the books ([`protect_here`], [`protect_alias`]: no protection change, no +/// question to the host). Whatever else touches one makes it a mapping of +/// the host's first: a fault, the title's or ours (the handler below), and +/// what that handler does not see ([`materialize`]: our writes through the +/// kernel, the kernel's writes the runtime announces). +pub struct SoftViews { + /// `(address, bytes)`: makes every such view that overlaps the range a + /// mapping of the host's; whether there was one and all could be made. + pub materialize: fn(usize, usize) -> bool, + /// The same for an access that faulted: `(address, the code's address)`. + pub fault: fn(usize, usize) -> bool, + /// `(address, to, bytes)`: if such a view holds `address`, copies its + /// bytes from there on to `to`, to the view's end at most, and says + /// `(true, bytes copied)`; else `(false, n)`: the next `n` bytes (up to + /// `bytes`) are in no such view, and are read as other memory is. + pub read: fn(usize, *mut u8, usize) -> (bool, usize), + /// `(address, bytes)`: the parts of the range such views show, in + /// order, those that touch as one: `(address, bytes)`. + pub runs: fn(usize, usize) -> Vec<(usize, usize)>, +} + +static SOFT_VIEWS: std::sync::OnceLock = std::sync::OnceLock::new(); + +/// The runtime keeps views in its books alone ([`SoftViews`]). +pub fn use_soft_views(hooks: SoftViews) { + let _ = SOFT_VIEWS.set(hooks); +} + +pub(crate) fn soft() -> Option<&'static SoftViews> { + SOFT_VIEWS.get() +} + +/// The parts of `[at, at + len)` that views in the runtime's books alone +/// show ([`SoftViews::runs`]); none without such views. +pub(crate) fn soft_runs(at: usize, len: usize) -> Vec<(usize, usize)> { + match soft() { + Some(soft) if len != 0 => (soft.runs)(at, len), + _ => Vec::new(), + } +} + +/// Whether a view in the runtime's books alone shows all of `block`. +fn soft_block(block: usize) -> bool { + soft_runs(block * BLOCK, BLOCK) == [(block * BLOCK, BLOCK)] +} + +/// Since the start: blocks of views in the runtime's books alone guarded +/// there ([`protect_here`]), and such blocks guarded there as another view +/// of a page that is guarded ([`protect_alias`]). +static SOFT_GUARDS: [std::sync::atomic::AtomicU64; 2] = [const { std::sync::atomic::AtomicU64::new(0) }; 2]; + +/// [`SOFT_GUARDS`], for the runtime's line about its views. +pub fn soft_view_guards() -> [u64; 2] { + [0, 1].map(|i| SOFT_GUARDS[i].load(std::sync::atomic::Ordering::Relaxed)) +} + +/// The runtime made a view it had in its books alone a mapping of the +/// host's, read-write, at `[at, at + len)` ([`SoftViews`]): from now on the +/// title writes through it without a fault. What was guarded of it in the +/// books is opened, and with it every other view of its pages +/// ([`open`]): all of them count as written at the next asking. Called when +/// the view is there (as [`title_memory_remapped`] is), with the runtime's +/// lock on its views held. +pub fn title_memory_made_real(at: usize, len: usize) { + if len == 0 { + return; + } + let mut g = guards(); + open(&mut g, at, at.saturating_add(len)); } /// Registers the handler that takes writes to guarded blocks, once, ahead @@ -2962,7 +3075,12 @@ pub(crate) fn install() { // made again (any access, the title's or ours). if r.ExceptionCode == EXCEPTION_ACCESS_VIOLATION as NTSTATUS && r.NumberParameters >= 2 - && materialize(r.ExceptionInformation[1], 1) + && match soft() { + // (Counted by whose code it was: the title's own touch of a + // view is what the books cannot spare it.) + Some(soft) => (soft.fault)(r.ExceptionInformation[1], r.ExceptionAddress as usize), + None => materialize(r.ExceptionInformation[1], 1), + } { return EXCEPTION_CONTINUE_EXECUTION; } @@ -3079,6 +3197,48 @@ fn writable(at: usize) -> bool { n != 0 && info.State == MEM_COMMIT && info.Protect == PAGE_READWRITE } +/// A runtime that keeps views in its books alone, for tests +/// ([`use_soft_views`]; one set of hooks for the whole test process). +#[cfg(test)] +pub(crate) mod test_books { + /// What that runtime keeps in its books alone: address, bytes, and + /// where their pages are (0: nowhere, never read); apart from each other. + pub(crate) static IN_BOOKS: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); + + /// Its hooks: the views are [`IN_BOOKS`], read from their pages; it + /// makes none real itself. + pub(crate) fn books() { + super::use_soft_views(super::SoftViews { + materialize: |_, _| false, + fault: |_, _| false, + read: |at, to, len| { + let views = IN_BOOKS.lock().unwrap_or_else(|e| e.into_inner()); + let readable = |view: &&(usize, usize, usize)| view.2 != 0 && (view.0..view.0 + view.1).contains(&at); + if let Some(&(base, bytes, pages)) = views.iter().find(readable) { + let n = (base + bytes - at).min(len); + // SAFETY: the test's pages hold the view's bytes, and + // its `to` as many as it asked for. + unsafe { std::ptr::copy_nonoverlapping((pages + (at - base)) as *const u8, to, n) }; + return (true, n); + } + let next = views.iter().filter(|view| view.2 != 0).map(|view| view.0).filter(|&base| base > at).min(); + (false, next.map_or(len, |next| (next - at).min(len))) + }, + runs: |at, len| { + let views = IN_BOOKS.lock().unwrap_or_else(|e| e.into_inner()); + let mut runs: Vec<(usize, usize)> = views + .iter() + .map(|&(base, bytes, _)| (base.max(at), (base + bytes).min(at + len))) + .filter(|&(from, to)| from < to) + .map(|(from, to)| (from, to - from)) + .collect(); + runs.sort_unstable(); + runs + }, + }); + } +} + #[cfg(test)] mod parts_tests { use super::*; @@ -3872,6 +4032,107 @@ mod tests { drop_views(section, &[guarded, other], SIZE); } + use super::test_books::{IN_BOOKS, books}; + + /// A view the runtime keeps in its books alone (`VICTORY_SOFT_VIEWS`): a + /// placeholder to the host. It is guarded in the books, on its own and + /// as another view of a guarded page, so that both it and that view come + /// clean; a write told of at it, or through the other view, is reported + /// at both; and made a view of the host's, it and the other view count + /// as written, and it is guarded as any view from then on. + #[test] + fn a_view_in_the_books_alone_is_guarded_there() { + use windows_sys::Win32::System::Memory::{ + CreateFileMappingW, FILE_MAP_WRITE, MEM_REPLACE_PLACEHOLDER, MEM_RESERVE_PLACEHOLDER, MEMORY_MAPPED_VIEW_ADDRESS, + MapViewOfFile, MapViewOfFile3, PAGE_NOACCESS, UnmapViewOfFile, VirtualAlloc2, + }; + use windows_sys::Win32::System::Threading::GetCurrentProcess; + const SIZE: usize = 2 * BLOCK; + books(); + // SAFETY: a fresh placeholder of two blocks, and a pagefile section + // of two with one view of the host's. + let (soft, section, real) = unsafe { + let soft = VirtualAlloc2( + GetCurrentProcess(), + std::ptr::null(), + SIZE, + MEM_RESERVE | MEM_RESERVE_PLACEHOLDER, + PAGE_NOACCESS, + std::ptr::null_mut(), + 0, + ) as usize; + assert_ne!(soft, 0); + let section = CreateFileMappingW(-1isize as _, std::ptr::null(), PAGE_READWRITE, 0, SIZE as u32, std::ptr::null()); + assert!(!section.is_null()); + let real = MapViewOfFile(section, FILE_MAP_WRITE, 0, 0, SIZE).Value as usize; + assert_ne!(real, 0); + (soft, section, real) + }; + // As the runtime tells of them: the one in the books first known as + // such, then told of as mapped. + IN_BOOKS.lock().unwrap().push((soft, SIZE, 0)); + title_memory_remapped(soft, SIZE); + crate::inplace::title_memory_view(soft, SIZE, section as usize, 0); + title_memory_remapped(real, SIZE); + crate::inplace::title_memory_view(real, SIZE, section as usize, 0); + let blocks = |at: usize| vec![at / BLOCK, at / BLOCK + 1]; + let block = |at: usize, k: usize| vec![at / BLOCK + k]; + let none = Some(Vec::new()); + // Guarded in the books: told once, then clean, with the host never + // asked to protect the placeholder (which it refuses). + assert_eq!(guard(soft, soft + SIZE), Some(blocks(soft))); + assert_eq!(guard(soft, soft + SIZE), none); + assert!(guards().absent.range(soft / BLOCK..soft / BLOCK + 2).next().is_none(), "there, as far as the title goes"); + // The view of the host's comes clean beside it. + assert_eq!(guard(real, real + SIZE), Some(blocks(real))); + assert_eq!(guard(real, real + SIZE), none); + assert_eq!(guard(soft, soft + SIZE), none); + assert!(!writable(real)); + // A write through the view of the host's: reported at both. + // SAFETY: the view, read-write once the handler has opened it. + unsafe { ((real + BLOCK + 8) as *mut u8).write_volatile(1) }; + assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 1))); + assert_eq!(guard(real, real + SIZE), Some(block(real, 1))); + assert_eq!(guard(soft, soft + SIZE), none); + assert_eq!(guard(real, real + SIZE), none); + // A write the runtime tells of at the view in its books (bytes + // DirectStorage put into the pages): reported at both. + writing(soft, BLOCK); + assert_eq!(guard(real, real + SIZE), Some(block(real, 0))); + assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 0))); + assert_eq!(guard(real, real + SIZE), none); + assert_eq!(guard(soft, soft + SIZE), none); + // The title's CPU touched it: the runtime maps it, read-write, and + // tells. Both views count as written, whatever was written. + // SAFETY: the placeholder takes a view of the section. + let view = unsafe { + MapViewOfFile3(section, GetCurrentProcess(), soft as *const _, 0, SIZE, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE, std::ptr::null_mut(), 0) + }; + assert_eq!(view.Value as usize, soft); + IN_BOOKS.lock().unwrap().retain(|view| view.0 != soft); + title_memory_made_real(soft, SIZE); + assert!(writable(soft)); + assert_eq!(guard(real, real + SIZE), Some(blocks(real))); + assert_eq!(guard(soft, soft + SIZE), Some(blocks(soft))); + assert_eq!(guard(real, real + SIZE), none); + // A view as any other now: read-only by our hand, and a write + // through it faults and is reported at both. + assert!(!writable(soft)); + // SAFETY: the view, read-write once the handler has opened it. + unsafe { ((soft + 8) as *mut u8).write_volatile(2) }; + assert_eq!(guard(real, real + SIZE), Some(block(real, 0))); + assert_eq!(guard(soft, soft + SIZE), Some(block(soft, 0))); + // SAFETY: the bytes went into the section's page. + assert_eq!(unsafe { ((real + 8) as *const u8).read_volatile() }, 2); + for at in [soft, real] { + title_memory_remapped(at, SIZE); + // SAFETY: our views. + unsafe { UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: at as *mut _ }) }; + } + // SAFETY: the section's last reference. + unsafe { windows_sys::Win32::Foundation::CloseHandle(section) }; + } + /// A write of the runtime's own, told at one view of the pages /// (`writing`): the other views' next checks report the pages written. #[test] diff --git a/crates/victory-hle/src/modules/xmem.rs b/crates/victory-hle/src/modules/xmem.rs index 9a8796d..ce69111 100644 --- a/crates/victory-hle/src/modules/xmem.rs +++ b/crates/victory-hle/src/modules/xmem.rs @@ -93,7 +93,7 @@ pub extern "system" fn XMemVirtualAlloc( ) -> *mut c_void { // Asked of addresses a lazy view shows: as if the view were mapped. if !base.is_null() { - materialize(base as usize, size); + materialize_for(base as usize, size, Touch::TitleCall); } let (lowest, highest) = address_range(flags); let mut requirements = MEM_ADDRESS_REQUIREMENTS { @@ -235,7 +235,7 @@ pub extern "system" fn XMemVirtualQuery( ) -> BOOL { let size = size_of::(); // A lazy view is committed memory to the title. - materialize(address as usize, 1); + materialize_for(address as usize, 1, Touch::TitleCall); // SAFETY: the title passes room for the whole structure; the host // fails on a null one. unsafe { @@ -1017,12 +1017,21 @@ pub(crate) fn pages_written(pages: &[usize], offset: usize, len: usize) { static MAPPING: [std::sync::atomic::AtomicU64; 17] = [const { std::sync::atomic::AtomicU64::new(0) }; 17]; /// The streaming log's line of mapping costs ([`MAPPING`]), then, with -/// `VICTORY_LAZY_VIEWS=1`, lazy views noted, let go untouched and made real -/// ([`LAZY_COUNTS`]). +/// `VICTORY_LAZY_VIEWS=1` or `VICTORY_SOFT_VIEWS`, of the views kept in the +/// books alone, since the last line: noted, let go untouched, made real; +/// noted by the title's mapping calls and by page-mapping copies; made real +/// by a fault of the title's code, by a fault of other code, asked for by +/// the GPU side, by a call of the title's; reads of the GPU side served +/// from the pages and their KiB ([`Soft`]); and how many such views there +/// are now. pub(crate) fn map_costs() -> Option { + static BEFORE: Mutex<[u64; SOFT_FIELDS]> = Mutex::new([0; SOFT_FIELDS]); let mapping: [u64; 17] = std::array::from_fn(|i| MAPPING[i].swap(0, Ordering::Relaxed)); - let lazy: [u64; 3] = std::array::from_fn(|i| LAZY_COUNTS[i].swap(0, Ordering::Relaxed)); - let n: Vec = mapping.iter().chain(if lazy_views() { &lazy[..] } else { &[] }).copied().collect(); + let mut soft = soft_since(&mut BEFORE.lock().unwrap_or_else(|e| e.into_inner())).to_vec(); + soft[Soft::ReadBytes as usize] >>= 10; + soft.push(LAZY_N.load(Ordering::Relaxed) as u64); + let books = lazy_views() || soft_views() != 0; + let n: Vec = mapping.iter().chain(if books { &soft[..] } else { &[] }).copied().collect(); let time = |i: usize| matches!(i, 2 | 3 | 4 | 6 | 8 | 10 | 12 | 14); (n[0] + n[5] > 0).then(|| { let all: Vec = n @@ -1135,11 +1144,11 @@ pub extern "system" fn XMemMapPhysicalPages( n: usize, pages: *const usize, ) -> *mut c_void { - map_pages(address, n, pages, lazy_views()) + map_pages(address, n, pages, lazy_views() || soft_views() & SOFT_POOL != 0) } /// [`XMemMapPhysicalPages`]; `lazy`: views are mapped on the host when first -/// touched ([`lazy_views`]). +/// touched ([`lazy_views`], [`soft_views`]). fn map_pages(address: *mut c_void, n: usize, pages: *const usize, lazy: bool) -> *mut c_void { let base = address as usize; // Read before any lock is taken: a fault in title memory may wait for @@ -1184,12 +1193,7 @@ fn map_pages(address: *mut c_void, n: usize, pages: *const usize, lazy: bool) -> }) }) .count(); - let mapped = if lazy { - map_lazy(&mut physical, base + i * PAGE, run * PAGE, first) - } else { - map(&mut physical, base + i * PAGE, run * PAGE, first) - }; - if !mapped { + if !place(&mut physical, base + i * PAGE, run * PAGE, first, lazy.then_some(Soft::NotedPool)) { return std::ptr::null_mut(); } i += run; @@ -1249,13 +1253,15 @@ pub fn map_alias(to: u64, from: u64, len: u64) -> bool { /// [`map_alias`], not counted as a call of its own. fn alias(to: u64, from: u64, len: u64) -> bool { - alias_as(to, from, len, alias_runs()) + alias_as(to, from, len, alias_runs(), soft_views() & SOFT_COPIES != 0) } /// [`alias`]; `runs`: a view per run of consecutive section pages -/// ([`alias_runs`]), else a view per page. -fn alias_as(to: u64, from: u64, len: u64, runs_in_one: bool) -> bool { +/// ([`alias_runs`]), else a view per page; `lazy`: the views are kept in the +/// books alone ([`soft_views`]), a page each. +fn alias_as(to: u64, from: u64, len: u64, runs_in_one: bool, lazy: bool) -> bool { let (to, from, len) = (to as usize, from as usize, len as usize); + let lazy = lazy.then_some(Soft::NotedCopies); let _busy = Busy::begin(); // As in `XMemMapPhysicalPages`: no read in place meets the gap (taken // before the views, as there). @@ -1272,7 +1278,7 @@ fn alias_as(to: u64, from: u64, len: u64, runs_in_one: bool) -> bool { let Some(slice) = physical.slice_at(from + k) else { return false; }; - if !map(&mut physical, to + k, PAGE, slice) { + if !place(&mut physical, to + k, PAGE, slice, lazy) { return false; } } @@ -1296,7 +1302,7 @@ fn alias_as(to: u64, from: u64, len: u64, runs_in_one: bool) -> bool { MAPPING[15].fetch_add(slices.len() as u64, Ordering::Relaxed); MAPPING[16].fetch_add((slices.len() - runs.len()) as u64, Ordering::Relaxed); } - if runs_in_one { + if runs_in_one && lazy.is_none() { // One view per run of consecutive section pages, where its // addresses are one placeholder or can be joined into one; else a // view a page, as before. @@ -1329,7 +1335,7 @@ fn alias_as(to: u64, from: u64, len: u64, runs_in_one: bool) -> bool { return found; } for (k, slice) in slices.into_iter().enumerate() { - if !map(&mut physical, to + k * PAGE, PAGE, slice) { + if !place(&mut physical, to + k * PAGE, PAGE, slice, lazy) { return false; } } @@ -1509,6 +1515,11 @@ const GROUP: usize = 64; /// queue once at a time. In a bench (60,000 views, five threads changing /// protections) one thread mapped 1,500 pages a second, eight 30,000. fn side_by_side() -> usize { + // Views kept in the books alone are noted by the caller, one after the + // other, with no call to stand in the host's queue for. + if soft_views() != 0 { + return 1; + } // Diagnostics: 1 and 8 by turns. #[cfg(all(windows, feature = "diagnostics"))] if let Some(on) = victory_gpu::switched("VICTORY_MAP_THREADS") { @@ -1645,7 +1656,7 @@ fn change(physical: &mut Physical, mut ops: Vec) -> bool { physical.holes.remove(&op.at); if unlazy(op.at) { // Only in our books: its own placeholder is there. - LAZY_COUNTS[1].fetch_add(1, Ordering::Relaxed); + count(Soft::Gone, 1); if op.slice.is_none() { physical.holes.insert(op.at, PAGE); } @@ -1994,16 +2005,16 @@ fn unmap(physical: &mut Physical, base: usize, len: usize) { // A view only in our books ([`map_lazy`]): no host view to // unmap; its own placeholder is there, and what stays of it // stays lazy. - LAZY_COUNTS[1].fetch_add(1, Ordering::Relaxed); + count(Soft::Gone, 1); if at >= base && at + size <= end { physical.holes.insert(at, size); } if at < base { - map_lazy(physical, at, base - at, slice.clone()); + map_lazy(physical, at, base - at, slice.clone(), None); } if at + size > end { let index = slice.index + (end - at) / PAGE; - map_lazy(physical, end, at + size - end, Slice { index, ..slice }); + map_lazy(physical, end, at + size - end, Slice { index, ..slice }, None); } continue; } @@ -2065,36 +2076,272 @@ fn lazy_views() -> bool { *ON } -/// The views in our books that are no host mapping yet: address and -/// bytes. Written with [`PHYSICAL`] held (after it); read without it, so -/// that the many questions of the GPU side about title memory cost no wait -/// behind a mapping call. -static LAZY: LazyLock>> = LazyLock::new(Default::default); +/// The title's own mapping calls keep their views in our books alone +/// ([`soft_views`]). +const SOFT_POOL: u8 = 1; +/// The GPU's page-mapping copies do. +const SOFT_COPIES: u8 = 2; -/// Since the last asking (the streaming log): lazy views noted, let go -/// before anything touched them, made real. -static LAZY_COUNTS: [AtomicU64; 3] = [const { AtomicU64::new(0) }; 3]; +/// Which of the title's views are kept in our books alone, with no view of +/// the host's until the title's CPU touches them (`VICTORY_SOFT_VIEWS`; off +/// unless asked for). `1`: those of the title's own mapping calls +/// (`XMemMapPhysicalPages`, into its pool) and those of the GPU's +/// page-mapping copies (`CopyPageMappingsBatchX`, into its tiled heap); +/// `pool`: the first alone; `copies`: the second alone. +/// +/// Why: every view of the host's is a call to map it (45 us) and one to +/// unmap it (93 us), each waiting for a lock of the kernel's that our +/// guards' protection changes take too. In 143 s of run stream2: 445,503 +/// maps (20.1 s) and 349,175 unmaps (32.6 s); the title's 359,969 mapping +/// calls took 23.8 s and its 3,294 page-mapping copies 33.7 s, one of them +/// 467 ms of a frame of 608 (timeline tl.120). A view kept in the books is +/// one split of a placeholder the first time its addresses are used and no +/// call after that. +/// +/// Who needs the bytes at such a view, and how they get them: +/// - DirectStorage writes the pages by number, through the section's own +/// view ([`Section::window`]): as before, no view of the title's needed. +/// - The page-mapping copies take what the pool shows from the books +/// ([`Physical::slice_at`]): as before. +/// - The GPU side reads title memory through one function, which takes the +/// bytes of such a view from the pages ([`soft_read`]), asks which memory +/// is such a view ([`soft_runs`]) to take it for committed and to read a +/// texture there through a copy, and guards such a view in its books, +/// with no protection change (`victory_gpu::SoftViews`). +/// - The title's CPU, and whatever of ours touches the memory itself: the +/// access faults, the GPU side's handler (first in the process) makes the +/// view a mapping of the host's ([`materialize_for`]) and the access is +/// made again. From then on it is a view as before, until the title +/// unmaps it. What that handler does not see asks first: our writes +/// through the kernel, the kernel's writes the runtime announces (file +/// reads, DirectStorage's memory destinations), the title's +/// `XMemVirtualQuery`, `VirtualFree` and `XMemVirtualAlloc` there, and, +/// with this switch, its `VirtualQuery` and `VirtualProtect` +/// ([`title_virtual_query`]). Not covered: another call of the host's +/// that the title makes itself with such memory as an argument. +/// +/// What it differs in from `VICTORY_LAZY_VIEWS=1` (2 fps in run xw-lazy, +/// 2.4 to 3.4 GB of copies a frame): there the GPU side made a lazy view +/// real whenever it guarded a range (every refresh of a copy or texture +/// over it), and a view guarded together with a lazy view of the same page +/// was never clean, since the host refused to protect the placeholder; and +/// only the title's own calls mapped lazily. Here a view in the books is +/// guarded in the books, read from the pages, and the copies' views are +/// kept there too. +/// +/// What a run must show (the streaming log's M line, and a line of the log +/// every 10 s): how many views were made real, and by whose touch. If the +/// title's code touches most of them, nothing is gained (a fault and a map +/// where a map was). +/// +/// With it one thread maps (`VICTORY_MAP_THREADS` counts as 1): mapping +/// side by side maps views of the host's. +fn soft_views() -> u8 { + static ON: LazyLock = LazyLock::new(|| { + let which = match std::env::var("VICTORY_SOFT_VIEWS").as_deref().map(str::trim) { + Ok("1") => SOFT_POOL | SOFT_COPIES, + Ok("pool") => SOFT_POOL, + Ok("copies") => SOFT_COPIES, + _ => 0, + }; + if which != 0 { + use_soft_views(); + let said = std::thread::Builder::new().name("victory-soft-views".into()).spawn(|| { + let mut before = ([0; SOFT_FIELDS], [0; 2]); + loop { + std::thread::sleep(std::time::Duration::from_secs(10)); + if let Some(line) = soft_line(&mut before, 10) { + tracing::info!("{line}"); + } + } + }); + tracing::info!( + pool = which & SOFT_POOL != 0, + copies = which & SOFT_COPIES != 0, + line = said.is_ok(), + "title views are kept in the books alone until the title's CPU touches them (VICTORY_SOFT_VIEWS)" + ); + } + which + }); + *ON +} + +/// Tells the GPU side how views in our books alone are read, asked about +/// and made real ([`soft_views`]). +fn use_soft_views() { + victory_gpu::use_soft_views(victory_gpu::SoftViews { + materialize: |at, len| materialize_for(at, len, Touch::Asked), + fault: |at, code| materialize_for(at, 1, Touch::Fault(code)), + read: soft_read, + runs: soft_runs, + }); +} + +/// The views in our books that are no host mapping yet: address, bytes and +/// what they show. Written with [`PHYSICAL`] held (after it); read without +/// it, so that the many questions and reads of the GPU side about title +/// memory cost no wait behind a mapping call. +static LAZY: LazyLock>> = LazyLock::new(Default::default); /// How many views [`LAZY`] holds: none, and nothing is asked of it. static LAZY_N: AtomicUsize = AtomicUsize::new(0); +/// How many views in [`LAZY`] touch each 256 MiB of the first 2^40 bytes +/// (where the title's memory is): what lies in a piece with none (the +/// title's heaps, which the GPU side reads far more often than its views) +/// is no question to [`LAZY`], which costs a lock and a search. +static LAZY_AT: [std::sync::atomic::AtomicU32; 1 << 12] = [const { std::sync::atomic::AtomicU32::new(0) }; 1 << 12]; + +/// The pieces of [`LAZY_AT`], as a shift of the address. +const LAZY_PIECE: u32 = 28; + +/// Notes in [`LAZY_AT`] that a view at `[base, base + len)` is in [`LAZY`] +/// (`there`), or no more. +fn lazy_at(base: usize, len: usize, there: bool) { + for piece in base >> LAZY_PIECE..=(base + len.max(1) - 1) >> LAZY_PIECE { + match LAZY_AT.get(piece) { + Some(n) if there => n.fetch_add(1, Ordering::AcqRel), + Some(n) => n.fetch_sub(1, Ordering::AcqRel), + None => break, + }; + } +} + +/// Whether a view in [`LAZY`] may overlap `[at, at + len)`. +fn maybe_lazy(at: usize, len: usize) -> bool { + if LAZY_N.load(Ordering::Acquire) == 0 { + return false; + } + let (first, last) = (at >> LAZY_PIECE, at.saturating_add(len.max(1) - 1) >> LAZY_PIECE); + // (A range over many pieces, or past them: [`LAZY`] says.) + last - first >= 16 || last >= LAZY_AT.len() || LAZY_AT[first..=last].iter().any(|n| n.load(Ordering::Acquire) != 0) +} + +/// What is counted of the views in our books alone, since the start: the +/// fields of the streaming log's M line after the mapping costs, in this +/// order (there since the last line), then how many there are now. +#[derive(Clone, Copy)] +enum Soft { + /// Views noted with no view of the host's. + Noted = 0, + /// Let go before anything touched them: no map, no unmap. + Gone = 1, + /// Made views of the host's. + Real = 2, + /// Of the noted: by the title's own mapping calls. + NotedPool = 3, + /// By the GPU's page-mapping copies. + NotedCopies = 4, + /// Of those made real: by a fault of code in the title's main image. + FaultTitle = 5, + /// By a fault of other code (ours, a library's, the system's). + FaultOther = 6, + /// Asked for by the GPU side ahead of a touch through the kernel. + Asked = 7, + /// By a call of the title's about the memory. + TitleCall = 8, + /// Reads of the GPU side served from the pages. + Reads = 9, + /// Their bytes. + ReadBytes = 10, +} + +const SOFT_FIELDS: usize = 11; + +static SOFT_N: [AtomicU64; SOFT_FIELDS] = [const { AtomicU64::new(0) }; SOFT_FIELDS]; + +fn count(what: Soft, n: u64) { + SOFT_N[what as usize].fetch_add(n, Ordering::Relaxed); +} + +/// [`SOFT_N`] since `before` held it, which holds it now. +fn soft_since(before: &mut [u64; SOFT_FIELDS]) -> [u64; SOFT_FIELDS] { + let now: [u64; SOFT_FIELDS] = std::array::from_fn(|i| SOFT_N[i].load(Ordering::Relaxed)); + let since = std::array::from_fn(|i| now[i].wrapping_sub(before[i])); + *before = now; + since +} + +/// The last access that made a view real by a fault: its address and the +/// code's, for a fault of the title's main image and for one of other code +/// (0: none yet). Said in the log's line, not from the fault handler. +static LAST_FAULT: [AtomicUsize; 4] = [const { AtomicUsize::new(0) }; 4]; + +/// The log's line about the views in our books alone over the last +/// `seconds` (since `before` held the counts), or `None` if there was +/// nothing to say. +fn soft_line(before: &mut ([u64; SOFT_FIELDS], [u64; 2]), seconds: u64) -> Option { + let n = soft_since(&mut before.0); + let guards = victory_gpu::soft_view_guards(); + let guarded = [guards[0].wrapping_sub(before.1[0]), guards[1].wrapping_sub(before.1[1])]; + before.1 = guards; + let now = LAZY_N.load(Ordering::Relaxed); + if n.iter().all(|&n| n == 0) && now == 0 { + return None; + } + let of = |what: Soft| n[what as usize]; + let image = crate::runtime::main_image().unwrap_or(0); + let last = |i: usize| { + let (at, code) = (LAST_FAULT[i].load(Ordering::Relaxed), LAST_FAULT[i + 1].load(Ordering::Relaxed)); + match (at, i) { + (0, _) => String::new(), + (_, 0) => format!(" (the last at {at:#x}, code title+{:#x})", code.wrapping_sub(image)), + _ => format!(" (the last at {at:#x}, code {code:#x})"), + } + }; + Some(format!( + "title views kept in the books alone (VICTORY_SOFT_VIEWS), last {seconds} s: {} noted with no view of the host's ({} by the title's mapping calls, {} by page-mapping copies), \ + {} let go untouched (no map, no unmap); {} made views of the host's: {} by a fault of the title's code{}, {} by a fault of other code{}, \ + {} asked for by the GPU side, {} by a call of the title's; {} reads of the GPU side served from the pages ({} MB); \ + guarded in the books: {} blocks, {} more as other views of guarded pages; {now} such views now", + of(Soft::Noted), + of(Soft::NotedPool), + of(Soft::NotedCopies), + of(Soft::Gone), + of(Soft::Real), + of(Soft::FaultTitle), + last(0), + of(Soft::FaultOther), + last(2), + of(Soft::Asked), + of(Soft::TitleCall), + of(Soft::Reads), + of(Soft::ReadBytes) >> 20, + guarded[0], + guarded[1], + )) +} + /// Takes the view at `at` out of [`LAZY`]; whether it was lazy. fn unlazy(at: usize) -> bool { if LAZY_N.load(Ordering::Acquire) == 0 { return false; } - let gone = LAZY.write().unwrap_or_else(|e| e.into_inner()).remove(&at).is_some(); - if gone { + let gone = LAZY.write().unwrap_or_else(|e| e.into_inner()).remove(&at); + if let Some((size, _)) = &gone { + lazy_at(at, *size, false); LAZY_N.fetch_sub(1, Ordering::AcqRel); } - gone + gone.is_some() } -/// [`map`], lazily ([`lazy_views`]): the addresses become one placeholder -/// of their own, as the view would leave them, and the view is noted and -/// told of; no host view yet. Where they cannot be made one placeholder, -/// the view is mapped now, as [`map`] does. -fn map_lazy(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> bool { +/// [`map`], in the books alone ([`lazy_views`], [`soft_views`]): the +/// addresses become one placeholder of their own, as the view would leave +/// them, and the view is noted and told of; no host view yet. Where they +/// cannot be made one placeholder, the view is mapped now, as [`map`] does. +/// `who`: whose view it is, for the counts (`None`: what stays of one that +/// was cut). +fn map_lazy(physical: &mut Physical, base: usize, len: usize, slice: Slice, who: Option) -> bool { + // The pages are committed now, as mapping a view commits them: the GPU + // side reads and writes them through views of its own of the section + // (ours, [`soft_read`]; its own, where it puts bytes in beside a guard), + // and a page nothing has written yet would fault there. Through the + // section's view, once a page ([`Section::window`]); without that view + // (`VICTORY_SECTION_WINDOW=0`) a view of the host's, as before. + if slice.section.window(slice.index, len / PAGE).is_none() { + return map(physical, base, len, slice); + } let own = physical.holes.remove(&base) == Some(len); if !own { split(base, len); @@ -2102,17 +2349,127 @@ fn map_lazy(physical: &mut Physical, base: usize, len: usize, slice: Slice) -> b return map(physical, base, len, slice); } } + // Known as in the books alone before the GPU side is told of the view: + // a refresh that guards it in between would find a placeholder of the + // host's, take the block for not there, and leave it out of its copies + // until the next mapping there. + lazy_at(base, len, true); + match LAZY.write().unwrap_or_else(|e| e.into_inner()).insert(base, (len, slice.clone())) { + // (None was there: `unmap` took it out. Counted right all the same.) + Some((before, _)) => lazy_at(base, before, false), + None => { + LAZY_N.fetch_add(1, Ordering::AcqRel); + } + } shown_mut().note(base, len, &slice); victory_gpu::title_memory_remapped(base, len); victory_gpu::title_memory_view(base, len, slice.section.handle as usize, (slice.index * PAGE) as u64); physical.views.insert(base, (len, slice)); - if LAZY.write().unwrap_or_else(|e| e.into_inner()).insert(base, len).is_none() { - LAZY_N.fetch_add(1, Ordering::AcqRel); + if let Some(who) = who { + count(Soft::Noted, 1); + count(who, 1); } - LAZY_COUNTS[0].fetch_add(1, Ordering::Relaxed); true } +/// [`map`], or [`map_lazy`] for `lazy`. +fn place(physical: &mut Physical, base: usize, len: usize, slice: Slice, lazy: Option) -> bool { + match lazy { + Some(who) => map_lazy(physical, base, len, slice, Some(who)), + None => map(physical, base, len, slice), + } +} + +/// For the GPU side (`victory_gpu::SoftViews::read`): if a view in our +/// books alone holds `at`, its bytes from there on, to its end or `len` at +/// most, are copied from the pages to `to`: `(true, bytes copied)`. Else +/// `(false, n)`: the next `n` bytes, `len` at most, are in no such view. +/// (A view whose section has no view of ours, [`Section::window`], is made +/// a mapping of the host's and answered as the second.) +fn soft_read(at: usize, to: *mut u8, len: usize) -> (bool, usize) { + if len == 0 || !maybe_lazy(at, len) { + return (false, len); + } + let found = { + let lazy = LAZY.read().unwrap_or_else(|e| e.into_inner()); + match lazy.range(..=at).next_back().filter(|(base, view)| at < **base + view.0) { + Some((&base, (size, slice))) => Ok((base, *size, slice.clone())), + None => Err(lazy.range(at..).next().map_or(len, |(&next, _)| (next - at).min(len))), + } + }; + let (base, size, slice) = match found { + Ok(view) => view, + Err(n) => return (false, n), + }; + let n = (base + size - at).min(len); + let (first, within) = ((at - base) / PAGE, (at - base) % PAGE); + // Committed, as the view would be when mapped: a page nothing wrote + // reads as zeros, as it would there. (The section is held: its view + // stands while the bytes are copied, whatever the title unmaps.) + let Some(window) = slice.section.window(slice.index + first, (within + n).div_ceil(PAGE)) else { + materialize_for(at, n, Touch::Asked); + return (false, n); + }; + // SAFETY: `n` committed bytes of the section's view; the caller's `to` + // holds as many. The title's writes to the pages race as on the console. + unsafe { std::ptr::copy_nonoverlapping((window + within) as *const u8, to, n) }; + count(Soft::Reads, 1); + count(Soft::ReadBytes, n as u64); + (true, n) +} + +/// For the GPU side (`victory_gpu::SoftViews::runs`): the parts of +/// `[at, at + len)` that views in our books alone show, in order, those +/// that touch as one: (address, bytes). +fn soft_runs(at: usize, len: usize) -> Vec<(usize, usize)> { + let mut runs: Vec<(usize, usize)> = Vec::new(); + if len == 0 || !maybe_lazy(at, len) { + return runs; + } + let end = at.saturating_add(len); + let lazy = LAZY.read().unwrap_or_else(|e| e.into_inner()); + let first = lazy.range(..=at).next_back().filter(|(base, view)| **base + view.0 > at).map(|(&base, view)| (base, view.0)); + let rest = lazy.range(at.saturating_add(1)..end.max(at.saturating_add(1))).map(|(&base, view)| (base, view.0)); + for (base, size) in first.into_iter().chain(rest) { + let (from, to) = (base.max(at), (base + size).min(end)); + match runs.last_mut() { + Some((start, bytes)) if *start + *bytes == from => *bytes += to - from, + _ => runs.push((from, to - from)), + } + } + runs +} + +/// What made a view in our books alone a view of the host's. +#[derive(Clone, Copy)] +enum Touch { + /// An access that faulted, of the code at this address. + Fault(usize), + /// The GPU side, ahead of a touch through the kernel. + Asked, + /// A call of the title's about the memory. + TitleCall, +} + +/// Whether `code` lies in the title's main image. +fn title_code(code: usize) -> bool { + static SIZE: AtomicUsize = AtomicUsize::new(0); + let Some(base) = crate::runtime::main_image() else { + return false; + }; + let mut size = SIZE.load(Ordering::Relaxed); + if size == 0 { + // SAFETY: a mapped image: its headers are there (the DOS header's + // offset of the PE header, and that header's size of the image). + size = unsafe { + let pe = base + ((base + 0x3c) as *const u32).read_unaligned() as usize; + ((pe + 0x50) as *const u32).read_unaligned() as usize + }; + SIZE.store(size, Ordering::Relaxed); + } + (base..base + size).contains(&code) +} + /// Whether `[base, base + len)` is one placeholder, exactly (Windows asked). fn one_placeholder_there(base: usize, len: usize) -> bool { use windows_sys::Win32::System::Memory::VirtualQuery; @@ -2134,12 +2491,17 @@ fn one_placeholder_there(base: usize, len: usize) -> bool { /// below. Not on a thread that holds the views (one of our mapping calls /// touching title memory: never, by design). fn materialize(at: usize, len: usize) -> bool { - if LAZY_N.load(Ordering::Acquire) == 0 { + materialize_for(at, len, Touch::Asked) +} + +/// [`materialize`]; `touch`: what asks, for the counts. +fn materialize_for(at: usize, len: usize, touch: Touch) -> bool { + if !maybe_lazy(at, len.max(1)) { return false; } let end = at.saturating_add(len.max(1)); - let overlapping = |lazy: &BTreeMap| -> Vec { - lazy.range(..end).rev().take_while(|&(&base, &size)| base + size > at).map(|(&base, _)| base).collect() + let overlapping = |lazy: &BTreeMap| -> Vec { + lazy.range(..end).rev().take_while(|&(&base, view)| base + view.0 > at).map(|(&base, _)| base).collect() }; { let lazy = LAZY.read().unwrap_or_else(|e| e.into_inner()); @@ -2162,7 +2524,22 @@ fn materialize(at: usize, len: usize) -> bool { // Its own placeholder is there ([`map_lazy`]). let made = show(base, size, &slice, true); unlazy(base); - LAZY_COUNTS[2].fetch_add(1, Ordering::Relaxed); + // Read-write, and there: the title writes through it from now on + // with no fault. What the GPU side guarded of it in its books, and + // the other views of its pages with it, counts as written. + victory_gpu::title_memory_made_real(base, size); + count(Soft::Real, 1); + match touch { + Touch::Fault(code) => { + let title = title_code(code); + count(if title { Soft::FaultTitle } else { Soft::FaultOther }, 1); + let last = if title { 0 } else { 2 }; + LAST_FAULT[last].store(at, Ordering::Relaxed); + LAST_FAULT[last + 1].store(code, Ordering::Relaxed); + } + Touch::Asked => count(Soft::Asked, 1), + Touch::TitleCall => count(Soft::TitleCall, 1), + } all &= made; } drop(physical); @@ -2261,7 +2638,7 @@ extern "system" fn title_virtual_free(address: *mut c_void, size: usize, kind: u const MEM_COALESCE_PLACEHOLDERS: u32 = 1; // A lazy view there is a view to the title (whose calls on it then fail // as they did), not the placeholder the host has. - materialize(address as usize, size); + materialize_for(address as usize, size, Touch::TitleCall); // 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); @@ -2270,11 +2647,36 @@ extern "system" fn title_virtual_free(address: *mut c_void, size: usize, kind: u unsafe { VirtualFree(address, size, kind) } } +/// The title's own `VirtualQuery`, while views are kept in the books alone +/// ([`soft_views`]): such a view at the address is a view to the title, not +/// the placeholder the host has. +extern "system" fn title_virtual_query(address: *const c_void, out: *mut MEMORY_BASIC_INFORMATION, size: usize) -> usize { + use windows_sys::Win32::System::Memory::VirtualQuery; + materialize_for(address as usize, 1, Touch::TitleCall); + // SAFETY: the title's own arguments. + unsafe { VirtualQuery(address, out, size) } +} + +/// The title's own `VirtualProtect`, as [`title_virtual_query`]. +extern "system" fn title_virtual_protect(address: *const c_void, size: usize, protect: u32, old: *mut u32) -> BOOL { + use windows_sys::Win32::System::Memory::VirtualProtect; + materialize_for(address as usize, size, Touch::TitleCall); + // SAFETY: the title's own arguments. + unsafe { VirtualProtect(address, size, protect, old) } +} + pub(crate) fn module() -> HleModule { + // (Asked of the environment here, not of [`soft_views`], which says what + // it does to a log that is not open yet.) + let soft = std::env::var_os("VICTORY_SOFT_VIEWS").is_some_and(|v| v != "0"); + let asks = [ + crate::HleExport { name: "VirtualQuery", address: title_virtual_query as *const () as usize }, + crate::HleExport { name: "VirtualProtect", address: title_virtual_protect as *const () as usize }, + ]; HleModule { name: "xmem", dlls: &["xmem.dll", "kernel32.dll", "kernelbase.dll", "api-ms-win-core-memory-*"], - exports: vec![ + exports: soft.then_some(asks).into_iter().flatten().chain([ crate::HleExport { name: "VirtualFree", address: title_virtual_free as *const () as usize }, hle_export!(XMemAlloc), hle_export!(XMemFree), @@ -2289,7 +2691,8 @@ pub(crate) fn module() -> HleModule { hle_export!(XMemVirtualQuery), hle_export!(XMemGetWorkingSetStatistics), hle_export!(XMemTransferMemory), - ], + ]) + .collect(), } } @@ -2576,6 +2979,109 @@ mod tests { assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1); } + /// Views kept in the books alone (`VICTORY_SOFT_VIEWS`), the title's own + /// and the page-mapping copies': the host has a placeholder, the GPU + /// side's reads take the bytes from the pages and are told where such + /// views are; mapped anew or unmapped they change with no view of the + /// host's; a touch of the CPU's maps one with its pages' bytes, and it is + /// a view as any other from then on. + #[test] + fn views_in_the_books_alone_are_read_from_their_pages() { + use_soft_views(); + let counted = |what: Soft| SOFT_N[what as usize].load(Ordering::Relaxed); + let before = [Soft::Noted, Soft::NotedPool, Soft::NotedCopies, Soft::Gone, Soft::Real, Soft::FaultOther, Soft::Reads].map(counted); + let mut pages = [0usize; 4]; + let mut n = 4; + assert_eq!(XMemAllocatePhysicalPages(0, &mut n, pages.as_mut_ptr()), 1); + let reserve = || XMemVirtualAlloc(std::ptr::null_mut(), 4 * PAGE, MEM_RESERVE | PAGES_64K, 0, PAGE_READWRITE) as usize; + let (pool, heap) = (reserve(), reserve()); + let lazy = |at: usize| LAZY.read().unwrap().contains_key(&at); + // What the host has there, not asking the runtime. + let host = |at: usize| { + use windows_sys::Win32::System::Memory::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. + unsafe { VirtualQuery(at as *const c_void, &mut info, size_of_val(&info)) }; + info.State + }; + let read = |at: usize, len: usize| { + let mut out = vec![0xeeu8; len]; + let answer = soft_read(at, out.as_mut_ptr(), len); + (answer, out) + }; + let alias = |to: usize, from: usize| alias_as(to as u64, from as u64, PAGE as u64, false, true); + // The title maps its pool a call a page, DirectStorage fills the + // pages by number, and the GPU maps them into the heap out of order. + for k in 0..4 { + assert_eq!(map_pages((pool + k * PAGE) as *mut c_void, 1, pages[k..].as_ptr(), true) as usize, pool + k * PAGE); + assert!(write_physical(&pages[k..k + 1], 0, &vec![k as u8 + 1; PAGE])); + } + for (to, from) in [(0, 2), (1, 0), (2, 3)] { + assert!(alias(heap + to * PAGE, pool + from * PAGE)); + } + for k in 0..4 { + assert!(lazy(pool + k * PAGE)); + assert_eq!(host(pool + k * PAGE), MEM_RESERVE); + assert_eq!(lazy(heap + k * PAGE), k < 3); + assert_eq!(host(heap + k * PAGE), MEM_RESERVE); + } + // Where such views are: those that touch as one, cut to the range. + assert_eq!(soft_runs(heap, 4 * PAGE), [(heap, 3 * PAGE)]); + assert_eq!(soft_runs(heap + PAGE / 2, PAGE), [(heap + PAGE / 2, PAGE)]); + assert!(soft_runs(heap + 3 * PAGE, PAGE).is_empty()); + // Their bytes, a view at a time; from inside one, to its end. + for (k, byte) in [(0, 3), (1, 1), (2, 4)] { + let ((from_pages, n), out) = read(heap + k * PAGE, 3 * PAGE); + assert!(from_pages); + assert_eq!(n, PAGE); + assert!(out[..PAGE].iter().all(|&b| b == byte) && out[PAGE..].iter().all(|&b| b == 0xee), "page {k}"); + } + let (answer, out) = read(heap + PAGE - 4, 8); + assert_eq!((answer, &out[..]), ((true, 4), &[3, 3, 3, 3, 0xee, 0xee, 0xee, 0xee][..])); + // No such view there: as much as was asked (or up to the next one, + // below). + assert_eq!(read(heap + 3 * PAGE, PAGE).0, (false, PAGE)); + // None of that made a view of the host's. + assert!((0..3).all(|k| lazy(heap + k * PAGE) && host(heap + k * PAGE) == MEM_RESERVE)); + // Mapped anew in the books: the other page's bytes. + assert!(alias(heap, pool + PAGE)); + assert!(lazy(heap) && host(heap) == MEM_RESERVE); + assert_eq!(read(heap, 1), ((true, 1), vec![2])); + // Unmapped: gone, with no view of the host's ever there. + assert!(alias_as((heap + PAGE) as u64, 0, PAGE as u64, false, true)); + assert!(!lazy(heap + PAGE) && physical().views.get(&(heap + PAGE)).is_none()); + assert_eq!(soft_runs(heap, 4 * PAGE), [(heap, PAGE), (heap + 2 * PAGE, PAGE)]); + assert_eq!(read(heap + PAGE, 2 * PAGE).0, (false, PAGE)); + // The CPU touches one: the handler maps it with its page's bytes, + // and a write through it lands in the page, which the pool's view of + // it (in the books still) shows. + // SAFETY: mapped in the title's eyes; the fault handler maps it. + assert_eq!(unsafe { ((heap + 2 * PAGE) as *const u8).read_volatile() }, 4); + assert!(!lazy(heap + 2 * PAGE)); + assert_eq!(host(heap + 2 * PAGE), MEM_COMMIT); + // SAFETY: mapped now. + unsafe { ((heap + 2 * PAGE) as *mut u8).write_volatile(77) }; + assert_eq!(read(pool + 3 * PAGE, 2), ((true, 2), vec![77, 4])); + assert_eq!(read(heap + 2 * PAGE, 1).0, (false, 1)); + // The pool lets go of the page: the heap keeps showing it. + assert_eq!(map_pages((pool + 3 * PAGE) as *mut c_void, 1, std::ptr::null(), true) as usize, pool + 3 * PAGE); + assert!(!lazy(pool + 3 * PAGE)); + // SAFETY: mapped. + assert_eq!(unsafe { ((heap + 2 * PAGE) as *const u8).read_volatile() }, 77); + // Counted (other tests count too: at least these). + let after = [Soft::Noted, Soft::NotedPool, Soft::NotedCopies, Soft::Gone, Soft::Real, Soft::FaultOther, Soft::Reads].map(counted); + for (i, least) in [8, 4, 4, 3, 1, 1, 6].into_iter().enumerate() { + assert!(after[i] - before[i] >= least, "count {i}: {} to {}", before[i], after[i]); + } + let line = soft_line(&mut ([0; SOFT_FIELDS], [0; 2]), 10).expect("something to say"); + assert!(line.contains("noted with no view of the host's") && line.contains("by a fault of other code (the last at"), "{line}"); + assert!(alias_as(heap as u64, 0, 4 * PAGE as u64, false, true)); + map_pages(pool as *mut c_void, 4, std::ptr::null(), true); + assert_eq!(host(heap + 2 * PAGE), MEM_RESERVE); + assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1); + } + /// A reader of title memory in place (holding the runtime's reading /// lock, as the GPU side's texture refresh does) that faults into a lazy /// view gets it mapped while a mapping call waits for that lock: the @@ -2640,7 +3146,7 @@ mod tests { }; assert_eq!(runs, vec![4]); for (to, in_one) in [(b, false), (c, true)] { - assert!(alias_as(to as u64, a as u64, 4 * PAGE as u64, in_one)); + assert!(alias_as(to as u64, a as u64, 4 * PAGE as u64, in_one, false)); for k in 0..4 { // SAFETY: mapped read-write. assert_eq!(unsafe { at(to, k).cast::().read() }, 10 + k as u8); @@ -2650,7 +3156,7 @@ mod tests { assert_eq!(physical().views.get(&(b as usize)).map(|v| v.0), Some(PAGE)); // Mapped again in one view where four views of a page were: their // holes are joined into one placeholder. - assert!(alias_as(b as u64, a as u64, 4 * PAGE as u64, true)); + assert!(alias_as(b as u64, a as u64, 4 * PAGE as u64, true, false)); assert_eq!(physical().views.get(&(b as usize)).map(|v| v.0), Some(4 * PAGE)); for k in 0..4 { // SAFETY: mapped read-write. @@ -2665,7 +3171,7 @@ mod tests { } // A source page not mapped: the pages before it are mapped all the same. assert!(XMemMapPhysicalPages(at(a, 2), 1, std::ptr::null()) == at(a, 2)); - assert!(!alias_as(c as u64, a as u64, 4 * PAGE as u64, true)); + assert!(!alias_as(c as u64, a as u64, 4 * PAGE as u64, true, false)); // SAFETY: mapped. assert_eq!(unsafe { at(c, 1).cast::().read() }, 11); assert_eq!(query(at(c, 2)).host.State, MEM_RESERVE);