Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ xcl rpf D:\data\shaders.rpf --extract NAME --out folder
(read, write or execute, the address, some registers and the callers on the stack), before the
title's own handlers see them – for a title whose crash filter ends the process without a trace.
- `XCL_TRACE_MEM=1` (any value turns it on): the title's memory allocations of 64 KiB or more
are logged, the first 60 (`XCL_TRACE_MEM=n` with `n > 1`: the first `n`). The same variable
are logged, the first 60 (`XCL_TRACE_MEM=n` with `n > 1`: the first `n`). Each
`XMemVirtualAlloc` call also logs its decoded flags and result. The same variable
also logs XCL's own allocations of 1 MB or more in the title's committed memory window, and
the ones that fail.
- `XCL_TRACE_IMPORTS`: logs the arguments and the result of every call of the imports it names.
Expand Down
185 changes: 179 additions & 6 deletions crates/xcl/src/hle/kernelx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,16 +695,127 @@ pub unsafe fn runtime_free(p: usize, attrs: u64) {
}
}

/// `void* XMemVirtualAlloc(void* address, SIZE_T size, DWORD type, XALLOC_ATTRIBUTES attributes,
/// DWORD protect)`: VirtualAlloc with an allocator's attributes – a graphics memory type is
/// GPU-visible memory (as MEM_GRAPHICS: sparse VRAM pool).
pub unsafe extern "system" fn XMemVirtualAlloc(addr: *const c_void, size: usize, t: u32, attrs: u64, p: u32) -> *mut c_void {
let a = XAllocAttrs::decode(attrs);
/// XMemVirtualAlloc's 4th argument: a flags word (not an allocator's XALLOC_ATTRIBUTES)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct VirtualAllocFlags {
/// 0x1: CPU memory
pub cpu: bool,
/// 0x2 (with 0x1: 0x3): GPU-visible memory (it needs a graphics page protection)
pub graphics: bool,
/// 0x4 (0x7): graphics memory whose address the system picks below 4 GiB
pub below_4g: bool,
/// 0x10: charged to the tools pool (one pool here)
pub tools: bool,
/// 0x20: a reservation for physical-page mapping
pub mappable: bool,
/// bits 7–9: bandwidth required / preferred (not modelled)
pub bandwidth: u8,
/// bits 12–14, n = 1…7: the address must fit in 47 − n bits
pub address_bits: Option<u8>,
/// bits 40–63: a tag for tools
pub tag: u32,
}

impl VirtualAllocFlags {
pub fn decode(w: u64) -> Self {
let n = ((w >> 12) & 7) as u8;
VirtualAllocFlags {
cpu: w & 0x1 != 0,
graphics: w & 0x2 != 0,
below_4g: w & 0x4 != 0,
tools: w & 0x10 != 0,
mappable: w & 0x20 != 0,
bandwidth: ((w >> 7) & 7) as u8,
address_bits: (n != 0).then(|| 47 - n),
tag: (w >> 40) as u32,
}
}

/// The first address the memory must stay below (None: no limit).
pub fn limit(&self) -> Option<u64> {
let bits = if self.below_4g { Some(32) } else { self.address_bits };
bits.map(|b| 1u64 << b)
}
}

/// Address-limited XMemVirtualAlloc requests seen this run and how many landed above their limit,
/// split by the limit's source: `below_4g` (a 32-bit address) and `address_bits` (40..46 bits).
/// Diagnostics only; placement is unchanged (PR 2 places them). Reported by `report_address_limits`.
static LIMIT_32_REQUESTS: AtomicUsize = AtomicUsize::new(0);
static LIMIT_32_ABOVE: AtomicUsize = AtomicUsize::new(0);
static LIMIT_BITS_REQUESTS: AtomicUsize = AtomicUsize::new(0);
static LIMIT_BITS_ABOVE: AtomicUsize = AtomicUsize::new(0);

/// Whether an allocation of `size` bytes placed at `got` ends above `limit` (its exclusive upper
/// bound, matching the `end <= limit` "fits" test). `None` when the allocation failed (`got == 0`):
/// there is no address to judge.
fn above_limit(limit: u64, size: usize, got: usize) -> Option<bool> {
(got != 0).then(|| (got as u64).saturating_add(size as u64) > limit)
}

/// Count one XMemVirtualAlloc request against its address limit, if it has one (diagnostics only).
fn note_address_limit(f: &VirtualAllocFlags, size: usize, got: usize) {
let Some(limit) = f.limit() else { return };
let (requests, above) = if f.below_4g {
(&LIMIT_32_REQUESTS, &LIMIT_32_ABOVE)
} else {
(&LIMIT_BITS_REQUESTS, &LIMIT_BITS_ABOVE)
};
requests.fetch_add(1, Ordering::Relaxed);
if above_limit(limit, size, got) == Some(true) {
above.fetch_add(1, Ordering::Relaxed);
}
}

/// Log the run's address-limited XMemVirtualAlloc tally at shutdown, if any were seen. Titles ask
/// for 32-bit- or N-bit-addressable graphics memory (`XMEM_GRAPHICS_32BIT_ADDRESS`,
/// `XMEM_40BIT_ADDRESS`..); XCL does not place by the limit yet, so this reports how many landed
/// above it (PR 2 places them).
pub fn report_address_limits() {
let (req32, above32) = (LIMIT_32_REQUESTS.load(Ordering::Relaxed), LIMIT_32_ABOVE.load(Ordering::Relaxed));
let (reqbits, abovebits) = (LIMIT_BITS_REQUESTS.load(Ordering::Relaxed), LIMIT_BITS_ABOVE.load(Ordering::Relaxed));
if req32 + reqbits == 0 {
return;
}
crate::xlog_info!(
"mem: XMemVirtualAlloc address limits (placement unchanged): below-4GiB {above32}/{req32} above the limit, N-bit {abovebits}/{reqbits} above the limit"
);
}

/// `XCL_TRACE_MEM`: every XMemVirtualAlloc call's flags word, decoded, with its result; and,
/// once per run, a request with an address limit and whether its address fits (PR 2 places them).
/// Always tallies the request against its address limit (`note_address_limit`).
fn trace_virtual_alloc_flags(addr: usize, size: usize, w: u64, f: &VirtualAllocFlags, got: usize) {
note_address_limit(f, size, got);
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
static LIMITED: AtomicUsize = AtomicUsize::new(0);
keep_last_error(|| {
if *ON.get_or_init(|| std::env::var_os("XCL_TRACE_MEM").is_some()) {
crate::xlog_info!("mem: XMemVirtualAlloc({addr:#x}, {size:#x}) flags {w:#x} = {f:?} → {got:#x}");
}
if let Some(limit) = f.limit() {
if LIMITED.fetch_add(1, Ordering::Relaxed) == 0 {
let end = (got as u64).saturating_add(size as u64);
let fits = if got == 0 { "failed" } else if end <= limit { "fits" } else { "is above it: not placed yet" };
crate::xlog_info!("mem: XMemVirtualAlloc asks for an address below {limit:#x} (flags {w:#x}): {got:#x} + {size:#x} {fits}");
}
}
})
}

/// `void* XMemVirtualAlloc(void* address, SIZE_T size, DWORD type, ULONG64 flags, DWORD protect)`:
/// Keep the existing allocation routing for callers passing allocator attributes. The separate
/// flags decode is only for tracing. In particular, bit 0x2 does not route large heaps to sparse VRAM.
pub unsafe extern "system" fn XMemVirtualAlloc(addr: *const c_void, size: usize, t: u32, flags: u64, p: u32) -> *mut c_void {
let f = VirtualAllocFlags::decode(flags);
let a = XAllocAttrs::decode(flags);
let mut t = if a.memory_type != 0 { t | MEM_GRAPHICS } else { t };
if a.memory_type != 0 && a.page_size == 2 {
t |= 0x8000_0000;
}
VirtualAlloc(addr, size, t, p)
let got = VirtualAlloc(addr, size, t, p);
trace_virtual_alloc_flags(addr as usize, size, flags, &f, got as usize);
got
}

/// Fast-path `VirtualQuery` / `VirtualQueryEx` / `XMemVirtualQuery` for a committed coherent
Expand Down Expand Up @@ -2319,6 +2430,68 @@ mod tests {
}
}

#[test]
fn virtual_alloc_flags_decode() {
let d = VirtualAllocFlags::decode;
assert_eq!(d(0x1), VirtualAllocFlags { cpu: true, ..Default::default() });
assert_eq!(d(0x3), VirtualAllocFlags { cpu: true, graphics: true, ..Default::default() });
let low = d(0x7);
assert!(low.graphics && low.below_4g);
assert_eq!(low.limit(), Some(1 << 32));
let mappable = d(0x23);
assert!(mappable.graphics && mappable.mappable && !mappable.below_4g);
assert_eq!(mappable.limit(), None);
let bits40 = d(0x7001);
assert_eq!((bits40.cpu, bits40.graphics, bits40.address_bits), (true, false, Some(40)));
assert_eq!(bits40.limit(), Some(1 << 40));
assert_eq!(d(0x1001).address_bits, Some(46));
assert_eq!(d(0x180).bandwidth, 3);
let tagged = d((0x41_4243u64 << 40) | 0x11);
assert_eq!((tagged.tag, tagged.tools, tagged.cpu, tagged.graphics), (0x41_4243, true, true, false));
}

#[test]
fn above_limit_judges_placement_against_the_address_limit() {
const G4: u64 = 1 << 32;
// a failed allocation has no address to judge
assert_eq!(above_limit(G4, 0x1000, 0), None);
// placed wholly below the limit: within it
assert_eq!(above_limit(G4, 0x1000, 0x1000_0000), Some(false));
// ends exactly at the limit (exclusive upper bound): within it, as the "fits" test
assert_eq!(above_limit(G4, 0x1000, (1usize << 32) - 0x1000), Some(false));
// the one GTA VI asks for and XCL places at ~18 GiB: above the 4 GiB limit
assert_eq!(above_limit(G4, 0xb90_0000, 0x4_4c61_0000), Some(true));
// straddles the limit: above it
assert_eq!(above_limit(G4, 0x2000, (1usize << 32) - 0x1000), Some(true));
// saturating add: a huge size never wraps to look within the limit
assert_eq!(above_limit(G4, usize::MAX, 0x1000), Some(true));
}

#[test]
fn virtual_alloc_graphics_flag_keeps_the_type() {
unsafe {
// a committed heap with the flags' graphics bit (0x283, as a title passes): plain
// memory as for VirtualAlloc with the same type, not the GPU-visible pool
let p = XMemVirtualAlloc(std::ptr::null(), 0x10000, 0x3000, 0x283, mem::PAGE_READWRITE);
assert!(!p.is_null());
*(p as *mut u64) = 0xABCD;
assert!(crate::gpu::uma::get().is_none_or(|u| !u.contains(p as usize)), "graphics flag made the heap GPU-visible");
assert_eq!(VirtualFree(p, 0, mem::MEM_RELEASE), 1);
}
}

#[test]
fn virtual_alloc_preserves_legacy_graphics_routing() {
let Some(uma) = crate::gpu::uma::get() else { return };
unsafe {
let p = XMemVirtualAlloc(std::ptr::null(), 0x10000, 0x3000, 1 << 29, mem::PAGE_READWRITE);
assert!(!p.is_null());
let in_uma = uma.contains(p as usize);
assert_eq!(VirtualFree(p, 0, mem::MEM_RELEASE), 1);
assert!(in_uma, "legacy graphics memory type allocated outside UMA");
}
}

#[test]
fn xmem_placeholder_split_replace_and_coalesce() {
unsafe {
Expand Down
2 changes: 2 additions & 0 deletions crates/xcl/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ pub fn write_final(crash: Option<String>) {
crate::hle::kbm::release();
// `XCL_DIRECT_DXIL_STATS=1`: the direct DXIL translator's live fallback summary
crate::gpu::dxil::coverage::dump_fallbacks();
// the run's address-limited XMemVirtualAlloc requests and how many landed above their limit
crate::hle::kernelx::report_address_limits();
// a crash is kept even while another thread writes the report, and is written once that
// thread is done – the process ends right after (#106); other calls give way at once
let wait = if crash.is_some() { Duration::from_secs(2) } else { Duration::ZERO };
Expand Down
Loading