Kernel reads, remapped views, freed memory and lost writes of title memory
- What the kernel is writing (file reads) is not guarded by a refresh before the call is done (`kernel_writing`): the read would have failed. - A title thread that uses a page while the runtime remaps another page of the same view waits for that in the fault handler; it faulted on a page the title never gave up. - Blocks being unmapped are not guarded again, memory the title frees leaves no notes of its blocks (its VirtualFree is taken over), and other memory at an address once guarded or absent keeps its protection. - The second part of a submission's one asking has a number of its own. - What shaders wrote waits for a change of mapping instead of being lost; what could not be written is counted and said; zeros into title memory that went are no fault. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
9f22d574bc
commit
8a475da9cf
9 files changed
+803
-32
No files matched your search
@@ -1035,12 +1035,14 @@ fn into_destination(
|
||||
) -> Result<(), HRESULT> {
|
||||
if request.options & PAGE_DESTINATION == 0 {
|
||||
// Raw reads land through the kernel, which does not fault into the
|
||||
// GPU side's guard on title buffers; open them first.
|
||||
victory_gpu::kernel_will_write(request.destination as usize, len);
|
||||
// GPU side's guard on title buffers; open them first, and until the
|
||||
// bytes are there.
|
||||
let writing = victory_gpu::kernel_writing(request.destination as usize, len);
|
||||
// SAFETY: the title's destination holds `destination_size` bytes, at
|
||||
// least `len`, and nothing else writes it until completion.
|
||||
let out = unsafe { std::slice::from_raw_parts_mut(request.destination.cast::<u8>(), len) };
|
||||
let filled = fill(out).map(drop);
|
||||
drop(writing);
|
||||
// Again now that the bytes are there: a texture looked at in between
|
||||
// was uploaded without them (`xmem::pages_written`).
|
||||
if super::xmem::stream_notice() {
|
||||
|
||||
@@ -519,7 +519,7 @@ pub extern "C" fn _wfsopen(path: *const u16, mode: *const u16, share: i32) -> Fi
|
||||
// fail there instead of faulting into its handler; the title's
|
||||
// synchronous reads (title+0x313fc0) read straight into 4 KiB-aligned
|
||||
// destinations and retry a failed `ReadFile` forever. So the destination
|
||||
// is opened first.
|
||||
// is opened first, and not guarded again before the call is done.
|
||||
|
||||
pub extern "system" fn ReadFile(
|
||||
file: *mut c_void,
|
||||
@@ -528,15 +528,36 @@ pub extern "system" fn ReadFile(
|
||||
read: *mut u32,
|
||||
overlapped: *mut c_void,
|
||||
) -> i32 {
|
||||
victory_gpu::kernel_will_write(buffer as usize, len as usize);
|
||||
host!("kernel32.dll", "ReadFile": extern "system" fn(*mut c_void, *mut u8, u32, *mut u32, *mut c_void) -> i32)(
|
||||
let writing = victory_gpu::kernel_writing(buffer as usize, len as usize);
|
||||
let done = host!("kernel32.dll", "ReadFile": extern "system" fn(*mut c_void, *mut u8, u32, *mut u32, *mut c_void) -> i32)(
|
||||
file, buffer, len, read, overlapped,
|
||||
)
|
||||
);
|
||||
// The call's error is the title's to ask for.
|
||||
// SAFETY: plain calls.
|
||||
let error = unsafe { windows_sys::Win32::Foundation::GetLastError() };
|
||||
// shortcut: a read that goes on after the call (overlapped) is written
|
||||
// into pages the kernel has locked by now: it lands all the same, but a
|
||||
// refresh that guards its blocks before it does will not see it.
|
||||
if done == 0 && error == windows_sys::Win32::Foundation::ERROR_IO_PENDING {
|
||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||
ONCE.call_once(|| tracing::info!(len, "ReadFile: a read of the title's goes on after the call"));
|
||||
}
|
||||
if done == 0 && error == windows_sys::Win32::Foundation::ERROR_NOACCESS {
|
||||
static FAILED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
let n = FAILED.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1;
|
||||
if n.is_power_of_two() {
|
||||
tracing::warn!(n, buffer = format_args!("{buffer:p}"), len, "ReadFile: the title's buffer could not be written");
|
||||
}
|
||||
}
|
||||
drop(writing);
|
||||
// SAFETY: as above.
|
||||
unsafe { windows_sys::Win32::Foundation::SetLastError(error) };
|
||||
done
|
||||
}
|
||||
|
||||
/// The host CRT reads large requests straight into the caller's buffer.
|
||||
pub extern "C" fn fread(buffer: *mut u8, size: usize, count: usize, stream: File) -> usize {
|
||||
victory_gpu::kernel_will_write(buffer as usize, size.saturating_mul(count));
|
||||
let _writing = victory_gpu::kernel_writing(buffer as usize, size.saturating_mul(count));
|
||||
host!("ucrtbase.dll", "fread": extern "C" fn(*mut u8, usize, usize, File) -> usize)(
|
||||
buffer, size, count, stream,
|
||||
)
|
||||
|
||||
@@ -1136,11 +1136,24 @@ pub extern "system" fn XMemTransferMemory(kind: u32, size: usize) -> BOOL {
|
||||
moved.into()
|
||||
}
|
||||
|
||||
/// The title's own `VirtualFree`. What the GPU side noted of the memory goes
|
||||
/// first: the address may be other memory's the moment it is free.
|
||||
extern "system" fn title_virtual_free(address: *mut c_void, size: usize, kind: u32) -> BOOL {
|
||||
const MEM_COALESCE_PLACEHOLDERS: u32 = 1;
|
||||
// Splitting and joining placeholders changes nothing they show.
|
||||
if kind & (MEM_PRESERVE_PLACEHOLDER | MEM_COALESCE_PLACEHOLDERS) == 0 {
|
||||
victory_gpu::title_memory_freed(address as usize, size);
|
||||
}
|
||||
// SAFETY: the title's own arguments.
|
||||
unsafe { VirtualFree(address, size, kind) }
|
||||
}
|
||||
|
||||
pub(crate) fn module() -> HleModule {
|
||||
HleModule {
|
||||
name: "xmem",
|
||||
dlls: &["xmem.dll"],
|
||||
dlls: &["xmem.dll", "kernel32.dll", "kernelbase.dll", "api-ms-win-core-memory-*"],
|
||||
exports: vec![
|
||||
crate::HleExport { name: "VirtualFree", address: title_virtual_free as *const () as usize },
|
||||
hle_export!(XMemAlloc),
|
||||
hle_export!(XMemFree),
|
||||
hle_export!(XMemAllocDefault),
|
||||
@@ -1383,7 +1396,9 @@ mod tests {
|
||||
let stop = stop.clone();
|
||||
std::thread::spawn(move || {
|
||||
let mut reads = 0u64;
|
||||
while !stop.load(Ordering::Relaxed) {
|
||||
// (Once at least: the mapping thread may be done before this
|
||||
// one's first turn.)
|
||||
while reads == 0 || !stop.load(Ordering::Relaxed) {
|
||||
let _reading = victory_gpu::title_memory_reading();
|
||||
// SAFETY: the page is mapped whenever no change of
|
||||
// mapping is under way, and none is while `_reading`
|
||||
@@ -1405,6 +1420,57 @@ mod tests {
|
||||
assert_eq!(XMemFreePhysicalPages(3, pages.as_ptr()), 1);
|
||||
}
|
||||
|
||||
/// A title thread that reads and writes a page of a view while other
|
||||
/// pages of the view are unmapped and mapped again, as the console lets
|
||||
/// it: the page was never given up.
|
||||
#[test]
|
||||
fn a_title_thread_goes_on_using_the_rest_of_a_view() {
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
let pages = allocate(4);
|
||||
for _ in 0..200 {
|
||||
// A new reservation each time: the pieces of a view cut up stay
|
||||
// cut up.
|
||||
let a = XMemVirtualAlloc(
|
||||
std::ptr::null_mut(),
|
||||
4 * PAGE,
|
||||
MEM_RESERVE | PAGES_64K,
|
||||
0,
|
||||
PAGE_READWRITE,
|
||||
);
|
||||
assert!(!a.is_null());
|
||||
assert_eq!(XMemMapPhysicalPages(a, 4, pages.as_ptr()), a);
|
||||
let (first, stop) = (a as usize, AtomicBool::new(false));
|
||||
// SAFETY: mapped read-write.
|
||||
unsafe { (first as *mut u64).write_volatile(0) };
|
||||
std::thread::scope(|s| {
|
||||
let title = s.spawn(|| {
|
||||
let mut n = 0u64;
|
||||
// SAFETY (the block): the first page is the title's all
|
||||
// the time; a fault would end the process.
|
||||
unsafe {
|
||||
while !stop.load(Ordering::Relaxed) {
|
||||
assert_eq!((first as *const u64).read_volatile(), n);
|
||||
n += 1;
|
||||
(first as *mut u64).write_volatile(n);
|
||||
}
|
||||
}
|
||||
n
|
||||
});
|
||||
// SAFETY: as above.
|
||||
while !title.is_finished() && unsafe { (first as *const u64).read_volatile() } < 100 {}
|
||||
for page in [3, 2, 1] {
|
||||
let at = a.wrapping_byte_add(page * PAGE);
|
||||
assert_eq!(XMemMapPhysicalPages(at, 1, std::ptr::null()), at);
|
||||
assert_eq!(XMemMapPhysicalPages(at, 1, pages[page..].as_ptr()), at);
|
||||
}
|
||||
stop.store(true, Ordering::Relaxed);
|
||||
assert!(title.join().expect("the title's thread never faulted") > 0);
|
||||
});
|
||||
assert_eq!(XMemMapPhysicalPages(a, 4, std::ptr::null()), a);
|
||||
}
|
||||
assert_eq!(XMemFreePhysicalPages(4, pages.as_ptr()), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mapping_no_pages_changes_nothing() {
|
||||
let pages = allocate(2);
|
||||
|
||||
Reference in new issue
Block a user