Renderer port 1: the 32-bit heap's pages get their section's slices in address order
Run port1-1: the title maps the five pieces of its 185 MB heap in another
order than their addresses, so the heap was views of one section in a
scattered order ("not one stretch of a section: Scattered") and nothing
was placed. With VICTORY_PORT_CONSTANTS=1 xmem now gives the page numbers
mapped into a 32-bit graphics reservation the slices at their offset in
it, when the reservation is all one section and both slices are fresh
(never mapped or written: no commit bit), which the title cannot observe.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
(cherry picked from commit 4b82f6c259d7a693e0e30eaaafc4c5a54b77d9e8)
This commit is contained in:
1 parent
9075710d14
commit
35c57d666e
1 file changed
+124
@@ -188,6 +188,9 @@ pub extern "system" fn XMemVirtualAlloc(
|
||||
if !at.is_null() && asked & 0x400 != 0 {
|
||||
victory_gpu::title_memory_write_combined(at as usize, size);
|
||||
}
|
||||
if !at.is_null() && placeholder && constrained && flags & ADDRESS_32BIT != 0 {
|
||||
LOW_RESERVATIONS.lock().unwrap_or_else(|e| e.into_inner()).push((at as usize, size));
|
||||
}
|
||||
note_protection(asked, kind, flags, at as usize, size);
|
||||
at
|
||||
}
|
||||
@@ -1137,6 +1140,98 @@ pub(crate) fn lift_thread() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Renderer port 1 (`VICTORY_PORT_CONSTANTS=1`; the GPU side's half is
|
||||
/// `victory_gpu`'s `port`): read here for xmem's part, [`arrange`].
|
||||
fn port_on() -> bool {
|
||||
static ON: LazyLock<bool> = LazyLock::new(|| std::env::var("VICTORY_PORT_CONSTANTS").is_ok_and(|v| v.trim() == "1"));
|
||||
*ON
|
||||
}
|
||||
|
||||
/// The title's reservations of 64 KiB pages for 32-bit graphics memory:
|
||||
/// base and bytes (one, of 185 MB, in Grand Theft Auto VI).
|
||||
static LOW_RESERVATIONS: Mutex<Vec<(usize, usize)>> = Mutex::new(Vec::new());
|
||||
|
||||
/// Pages given another place in their section so far by [`arrange`], and
|
||||
/// the times it found a page that was not fresh.
|
||||
static ARRANGED: [AtomicU64; 2] = [const { AtomicU64::new(0) }; 2];
|
||||
|
||||
/// Before `pages` are mapped at `base`: if `base` lies in a 32-bit graphics
|
||||
/// reservation ([`LOW_RESERVATIONS`]) whose pages are all one section of
|
||||
/// the same size, the page numbers are given the slices of that section in
|
||||
/// the reservation's address order (each page at its offset in the
|
||||
/// reservation), so that the reservation becomes one stretch of the section
|
||||
/// and the GPU side can open it as one heap.
|
||||
///
|
||||
/// Grand Theft Auto VI takes all the pages of its 185 MB heap in one call
|
||||
/// and maps its five pieces in another order than their addresses (run
|
||||
/// port1-1: "Scattered"). Only fresh slices are exchanged: never mapped,
|
||||
/// never written (no commit bit), so every page still reads as zeros and
|
||||
/// which slice a number names is not observable by the title.
|
||||
fn arrange(base: usize, pages: &[usize]) {
|
||||
let Some((first, bytes)) = LOW_RESERVATIONS
|
||||
.lock()
|
||||
.unwrap_or_else(|e| e.into_inner())
|
||||
.iter()
|
||||
.copied()
|
||||
.find(|&(at, len)| at <= base && base + pages.len() * PAGE <= at + len)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let mut table = page_table_mut();
|
||||
let Some(section) = pages.first().and_then(|p| table.pages.get(p)).map(|s| s.section.clone()) else {
|
||||
return;
|
||||
};
|
||||
if section.pages * PAGE != bytes
|
||||
|| !pages.iter().all(|p| table.pages.get(p).is_some_and(|s| Arc::ptr_eq(&s.section, §ion)))
|
||||
{
|
||||
return;
|
||||
}
|
||||
let fresh = |index: usize| section.committed[index / 64].load(Ordering::Acquire) >> (index % 64) & 1 == 0;
|
||||
// Which number has which slice of the section.
|
||||
let mut owner: HashMap<usize, usize> = table
|
||||
.pages
|
||||
.iter()
|
||||
.filter(|(_, s)| Arc::ptr_eq(&s.section, §ion))
|
||||
.map(|(&number, s)| (s.index, number))
|
||||
.collect();
|
||||
let mut moved = 0u64;
|
||||
for (i, number) in pages.iter().enumerate() {
|
||||
let want = (base - first) / PAGE + i;
|
||||
let Some(have) = table.pages.get(number).map(|s| s.index) else {
|
||||
continue;
|
||||
};
|
||||
if have == want {
|
||||
continue;
|
||||
}
|
||||
if !fresh(have) || !fresh(want) {
|
||||
ARRANGED[1].fetch_add(1, Ordering::Relaxed);
|
||||
continue;
|
||||
}
|
||||
if let Some(other) = owner.get(&want).copied()
|
||||
&& let Some(slice) = table.pages.get_mut(&other)
|
||||
{
|
||||
slice.index = have;
|
||||
owner.insert(have, other);
|
||||
}
|
||||
if let Some(slice) = table.pages.get_mut(number) {
|
||||
slice.index = want;
|
||||
}
|
||||
owner.insert(want, *number);
|
||||
moved += 1;
|
||||
}
|
||||
if moved > 0 {
|
||||
let all = ARRANGED[0].fetch_add(moved, Ordering::Relaxed) + moved;
|
||||
tracing::info!(
|
||||
at = format_args!("{base:#x}"),
|
||||
pages = pages.len(),
|
||||
moved,
|
||||
all,
|
||||
not_fresh = ARRANGED[1].load(Ordering::Relaxed),
|
||||
"renderer port 1: the title's 32-bit graphics pages given their section's slices in address order"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps `n` pages at `address`, in place of what is mapped there; null
|
||||
/// `pages` unmaps. Returns `address`, or null if a page or the host fails.
|
||||
pub extern "system" fn XMemMapPhysicalPages(
|
||||
@@ -1155,6 +1250,11 @@ fn map_pages(address: *mut c_void, n: usize, pages: *const usize, lazy: bool) ->
|
||||
// the mapping call that holds them.
|
||||
// SAFETY: the title passes `n` page numbers, or null.
|
||||
let pages = (!pages.is_null()).then(|| unsafe { std::slice::from_raw_parts(pages, n) }.to_vec());
|
||||
if port_on()
|
||||
&& let Some(pages) = &pages
|
||||
{
|
||||
arrange(base, pages);
|
||||
}
|
||||
let mut costed = Costed::begin(Some(n));
|
||||
let _called = victory_gpu::timeline::span("the title's XMemMapPhysicalPages");
|
||||
let _busy = Busy::begin();
|
||||
@@ -2816,6 +2916,30 @@ mod tests {
|
||||
assert!(!fallback.contains("System commit:"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_32_bit_heap_gets_its_slices_in_address_order() {
|
||||
// As the title's heap: a 32-bit reservation of 64 KiB pages, all of
|
||||
// its pages from one call, mapped piece by piece in another order.
|
||||
let base = XMemVirtualAlloc(std::ptr::null_mut(), 4 * PAGE, MEM_RESERVE | PAGES_64K, GRAPHICS | ADDRESS_32BIT, PAGE_READWRITE);
|
||||
assert!(!base.is_null() && (base as usize) < 1 << 32);
|
||||
let pages = allocate(4);
|
||||
let index = |number: usize| page_table().pages.get(&number).map(|s| s.index);
|
||||
let at = |page: usize| base.wrapping_byte_add(page * PAGE);
|
||||
// The last two pages' place first, from the array's start.
|
||||
arrange(at(2) as usize, &pages[..2]);
|
||||
assert_eq!(XMemMapPhysicalPages(at(2), 2, pages.as_ptr()), at(2));
|
||||
arrange(base as usize, &pages[2..]);
|
||||
assert_eq!(XMemMapPhysicalPages(base, 2, pages[2..].as_ptr()), base);
|
||||
assert_eq!([index(pages[0]), index(pages[1]), index(pages[2]), index(pages[3])], [Some(2), Some(3), Some(0), Some(1)]);
|
||||
// Mapped already: nothing moves any more.
|
||||
arrange(base as usize, &pages[..2]);
|
||||
assert_eq!(index(pages[0]), Some(2));
|
||||
// SAFETY: mapped read-write.
|
||||
unsafe { at(3).cast::<u32>().write(7) };
|
||||
assert_eq!(XMemMapPhysicalPages(base, 4, std::ptr::null()), base);
|
||||
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn physical_pages_map_alias_and_unmap() {
|
||||
let mut pages = [0usize; 3];
|
||||
|
||||
Reference in new issue
Block a user