diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 624bee86f1..a29d3c4498 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -1817,8 +1817,36 @@ pub mod pagelog { /// Size of one console physical page (64 KiB). pub const PHYS_PAGE_SIZE: usize = PHYS_PAGE; -/// first page index → (pages in the block, pages still allocated, the block's home) -static PHYS_BLOCKS: std::sync::Mutex> = std::sync::Mutex::new(std::collections::BTreeMap::new()); +struct PhysicalBlock { + count: usize, + live: usize, + home: usize, + freed: Vec, +} + +impl PhysicalBlock { + const WORD_BITS: usize = usize::BITS as usize; + + fn new(count: usize, home: usize) -> Self { + Self { count, live: count, home, freed: vec![0; count.div_ceil(Self::WORD_BITS)] } + } + + fn is_live(&self, offset: usize) -> bool { + offset < self.count && self.freed[offset / Self::WORD_BITS] & (1 << (offset % Self::WORD_BITS)) == 0 + } + + fn free_page(&mut self, offset: usize) -> bool { + if !self.is_live(offset) { + return false; + } + self.freed[offset / Self::WORD_BITS] |= 1 << (offset % Self::WORD_BITS); + self.live -= 1; + true + } +} + +/// first page index → its physical page allocation +static PHYS_BLOCKS: std::sync::Mutex> = std::sync::Mutex::new(std::collections::BTreeMap::new()); /// the next page index handed out (page numbers are not addresses: a block's home is in a /// sparse window, the coherent one, or – without room there – plain reserved memory) @@ -1929,12 +1957,13 @@ fn page_index(page: usize) -> Option { page.is_multiple_of(FRAMES).then(|| (page / FRAMES).checked_sub(1)).flatten() } -/// The home address of page `page` if it belongs to a live allocation. +/// The home address of page `page` if that specific page is still allocated. fn page_home(page: usize) -> Option { let index = page_index(page)?; let blocks = PHYS_BLOCKS.lock().ok()?; - let (&first, &(count, live, home)) = blocks.range(..=index).next_back()?; - (live > 0 && index < first.checked_add(count)?).then(|| home.checked_add((index - first).checked_mul(PHYS_PAGE)?)).flatten() + let (&first, block) = blocks.range(..=index).next_back()?; + let offset = index - first; + block.is_live(offset).then(|| block.home.checked_add(offset.checked_mul(PHYS_PAGE)?)).flatten() } /// Whether every page of `pages` (at least one) is a physical page allocated now. @@ -2378,7 +2407,7 @@ pub unsafe extern "system" fn AllocateTitlePhysicalPages(_p: HANDLE, _t: u32, nu for i in 0..n { *arr.add(i) = page_number(first + i); } - PHYS_BLOCKS.lock().unwrap().insert(first, (n, n, block)); + PHYS_BLOCKS.lock().unwrap().insert(first, PhysicalBlock::new(n, block)); 1 } @@ -2388,47 +2417,55 @@ pub unsafe extern "system" fn FreeTitlePhysicalPages(_p: HANDLE, n: usize, arr: fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); return 0; } + // Use the same lock order as map and read so they cannot see a partially freed page. + let mut away = AWAY.lock().unwrap(); + let mut blocks = PHYS_BLOCKS.lock().unwrap(); + let mut freed = Vec::new(); + let mut homes = Vec::new(); + for i in 0..n { + let page = *arr.add(i); + let Some(index) = page_index(page) else { continue }; + let Some((&first, block)) = blocks.range_mut(..=index).next_back() else { continue }; + if !block.free_page(index - first) { + continue; + } + freed.push(page); + if block.live == 0 { + homes.push((block.home, block.count)); + blocks.remove(&first); + } + } + drop(blocks); // freed pages mapped elsewhere: that address keeps whatever it holds, the mapping goes - if let Some(away) = AWAY.lock().unwrap().as_mut() { - for i in 0..n { - if let Some(at) = away.page.remove(&*arr.add(i)) { + if let Some(away) = away.as_mut() { + for &page in &freed { + if let Some(at) = away.page.remove(&page) { away.at.remove(&at); lazy::unmark(at); note_mapping_at(at); - pagelog::note("page freed while mapped (the address keeps its bytes)", at, 0, *arr.add(i)); + pagelog::note("page freed while mapped (the address keeps its bytes)", at, 0, page); } // (those at home too: their home goes with the block) - crate::hle::d3d12x::sparse::remapped(&mirrors::page_freed(*arr.add(i))); + crate::hle::d3d12x::sparse::remapped(&mirrors::page_freed(page)); } // (page numbers are never handed out again: their entries go) if !away.title.is_empty() { - let freed: std::collections::HashSet = (0..n).map(|i| *arr.add(i)).collect(); + let freed: std::collections::HashSet = freed.iter().copied().collect(); away.title.retain(|_, p| !freed.contains(p)); } } - let mut g = PHYS_BLOCKS.lock().unwrap(); - for i in 0..n { - let Some(page) = page_index(*arr.add(i)) else { continue }; - // the block holding this page - let Some((&first, &(count, _, home))) = g.range(..=page).next_back() else { continue }; - if page >= first + count { - continue; - } - let e = g.get_mut(&first).unwrap(); - e.1 = e.1.saturating_sub(1); - if e.1 == 0 { - g.remove(&first); - // (a tile may show a page kept in the block: `source_part`) - source_going(home, count * PHYS_PAGE, true, || { - if u.is_sparse(home) { - crate::hle::d3d12x::sparse::free(home); - } else if u.contains(home) { - u.free(home); - } else { - mem::VirtualFree(home as *mut c_void, 0, mem::MEM_RELEASE); - } - }); - } + drop(away); + for (home, count) in homes { + // (a tile may show a page kept in the block: `source_part`) + source_going(home, count * PHYS_PAGE, true, || { + if u.is_sparse(home) { + crate::hle::d3d12x::sparse::free(home); + } else if u.contains(home) { + u.free(home); + } else { + mem::VirtualFree(home as *mut c_void, 0, mem::MEM_RELEASE); + } + }); } 1 } @@ -3611,6 +3648,93 @@ mod tests { } } + #[test] + fn physical_page_liveness_crosses_bitmap_words() { + let boundary = PhysicalBlock::WORD_BITS; + let mut block = PhysicalBlock::new(boundary + 1, 0); + assert!(block.free_page(boundary - 1)); + assert!(block.is_live(boundary)); + assert!(block.free_page(boundary)); + assert!(!block.free_page(boundary)); + assert!(!block.is_live(boundary - 1)); + assert!(!block.free_page(boundary + 1)); + assert!(block.is_live(0)); + assert_eq!(block.live, boundary - 1); + } + + #[test] + fn repeated_physical_page_release_preserves_live_siblings() { + unsafe { + if crate::gpu::uma::get().is_none() { + crate::gpu::recomp::runner::skip_test("no UMA"); + return; + } + let mut n = 3; + let mut pages = [0; 3]; + assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1); + let [mut first, second, mut sibling] = pages; + assert!(write_physical_pages(&[sibling], 0, &[0x7d])); + assert!(write_physical_pages(&[sibling], PHYS_PAGE - 1, &[0x4a])); + + let mut duplicate = [first, first]; + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 2, duplicate.as_mut_ptr()), 1); + let mut bytes = [0; 2]; + assert!(!read_physical_pages(&[first], 0, &mut bytes)); + assert!(!write_physical_pages(&[first], 0, &[0])); + assert!(MapTitlePhysicalPages(std::ptr::null_mut(), 1, 0, 0, &mut first).is_null()); + assert!(page_home(second).is_some()); + + let mut mixed = [first, second]; + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 2, mixed.as_mut_ptr()), 1); + assert!(page_home(second).is_none()); + assert!(read_physical_pages(&[sibling, sibling], PHYS_PAGE - 1, &mut bytes)); + assert_eq!(bytes, [0x4a, 0x7d]); + let home = MapTitlePhysicalPages(std::ptr::null_mut(), 1, 0, 0, &mut sibling); + assert!(!home.is_null()); + assert_eq!(MapTitlePhysicalPages(home, 1, 0, 0, &mut sibling), home); + + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, &mut sibling), 1); + assert!(page_home(sibling).is_none()); + } + } + + /// A page freed twice and then a page mapped away from home: the mapping of the page freed + /// goes (its address keeps its bytes), and the block stays for the page still allocated. + #[test] + fn repeated_free_then_freeing_a_page_mapped_away_keeps_the_last_sibling() { + unsafe { + if crate::gpu::uma::get().is_none() { + crate::gpu::recomp::runner::skip_test("no UMA"); + return; + } + let mut n = 3; + let mut pages = [0; 3]; + assert_eq!(AllocateTitlePhysicalPages(std::ptr::null_mut(), 0, &mut n, pages.as_mut_ptr()), 1); + let [first, mut away, mut last] = pages; + let at = mem::VirtualAlloc(std::ptr::null(), PHYS_PAGE, mem::MEM_RESERVE | mem::MEM_COMMIT, mem::PAGE_READWRITE) as usize; + assert_ne!(at, 0); + assert_eq!(MapTitlePhysicalPages(at as *mut c_void, 1, 0, 0, &mut away) as usize, at); + assert!(physical_page_at(at)); + assert!(write_physical_pages(&[away], 0, &[0xB6])); + assert!(write_physical_pages(&[last], 0, &[0x5C])); + + let mut twice = [first, first]; + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 2, twice.as_mut_ptr()), 1); + assert!(physical_page_at(at), "a repeated free of another page keeps this mapping"); + + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, &mut away), 1); + assert!(!physical_page_at(at), "a freed page is no longer mapped there"); + assert_eq!(*(at as *const u8), 0xB6, "the address keeps its bytes"); + let mut byte = [0]; + assert!(read_physical_pages(&[last], 0, &mut byte), "the page still allocated keeps its block"); + assert_eq!(byte, [0x5C]); + + assert_eq!(FreeTitlePhysicalPages(std::ptr::null_mut(), 1, &mut last), 1); + assert!(page_home(last).is_none()); + mem::VirtualFree(at as *mut c_void, 0, mem::MEM_RELEASE); + } + } + #[test] fn physical_pages_in_uma() { unsafe {