diff --git a/HANDOFF.md b/HANDOFF.md index 29eb3e2..0bc1258 100644 --- a/HANDOFF.md +++ b/HANDOFF.md @@ -536,6 +536,20 @@ Nic otwartego. Niewyjaśnione: 6 pakietów na podblok przy różnicy w tablicy 12·960 – TOC 0xFC to CELT FB 20 ms **stereo**, więc tablica liczy chyba próbki × kanały. +## MicrosoftGame.Config (sesja 5, 2026-11-21) +- Z manifestu gry (od użytkownika; reszta pliku bez znaczenia dla XCL): `PersistentLocalStorage/SizeMB` = 2048, + `VirtualMachine/SMTEnabled` = true, `XboxAnacondaTitleMemory` / `XboxLockhartTitleMemory` = Advanced. +- `manifest.rs` (NOWE) czyta te pola z folderu gry; `Config::resolve` (launcher) wypełnia ustawienia „auto”: + - `pls_mb` → XPersistentLocalStorageGetSpaceInfo (było na sztywno 4 GiB); + - `smt` → kernelx: GetSystemInfo / GetNativeSystemInfo / GetActive/MaximumProcessorCount = **14** (7 rdzeni × SMT; + 7 bez SMT), GetCurrentProcessorNumber(Ex) modulo 14 (tablice per rdzeń w grze), SetThreadAffinityMask / + SetThreadIdealProcessor / GetProcessAffinityMask – maski konsoli składane na procesory PC (`fold_affinity`); + - `title_memory` → `uma_mb` 4095 dla „advanced” (inaczej 2048; `XCL_UMA_MB` nadal wygrywa). Budżet + TitleMemoryStatus zostaje standardowy (13 824 / 8 192 MB) – liczby trybu „Advanced” nie są publiczne + (docs.xbox.com za logowaniem), `title_memory_mb` w xcl.toml go nadpisuje. +- Test: manifest testowy (`tests/data/testgame/MicrosoftGame.config`) ma te wartości, e2e sprawdza log launchera i + boot.json; `manifest::tests`, `affinity_masks_fold_onto_the_pc`. + ## Instrukcje spoza paczki, sterty konsoli, struktura (sesja 5, 2026-11-21) - **Pokrycie ISA** (`isa_coverage`, każda instrukcja dekodera osobno; MIMG z DMASK=1, VOP3_SDST z VCC – wcześniej fałszywe braki): **975 / 1157 (84,3%)**, było 766. Dodane (test GPU `gpu_more_atomics_sub_dword_scratch_valu` + diff --git a/crates/xcl/src/config.rs b/crates/xcl/src/config.rs index caece5c..a9f2c67 100644 --- a/crates/xcl/src/config.rs +++ b/crates/xcl/src/config.rs @@ -17,8 +17,19 @@ pub struct Config { pub user_id: u64, pub console_id: String, pub sandbox_id: String, - /// Title memory budget reported by TitleMemoryStatus (MiB). + /// Title memory budget reported by TitleMemoryStatus (MiB); 0 = the standard budget of + /// `device_type` (13 824 Series X, 8 192 Series S). pub title_memory_mb: u64, + /// "auto" (MicrosoftGame.Config), "standard" or "advanced" title memory mode. + pub title_memory: String, + /// Persistent local storage quota (MiB); 0 = MicrosoftGame.Config, else 4096. + pub pls_mb: u64, + /// SMT of the title's VM: "auto" (MicrosoftGame.Config, else on), "on" (7 cores, + /// 14 logical processors) or "off" (7). + pub smt: String, + /// Unified (GPU-visible) memory (MiB, ≤ 4095); 0 = 4095 for "advanced" title memory, + /// else 2048. `XCL_UMA_MB` overrides. + pub uma_mb: u64, pub log_level: String, pub log_to_stderr: bool, /// Executable inside the game folder (auto-detected when empty). @@ -42,6 +53,63 @@ pub struct Config { pub window_height: u32, } +impl Config { + /// Fill the "auto" settings from the game's MicrosoftGame.Config (if any); afterwards every + /// setting has a concrete value. Returns what came from the manifest (for the log). + pub fn resolve(&mut self, manifest: Option<&crate::manifest::Manifest>) -> Vec { + let mut from = Vec::new(); + let series_s = self.device_type.eq_ignore_ascii_case("lockhart"); + if self.title_memory == "auto" { + let m = manifest.and_then(|m| if series_s { m.lockhart_memory.clone() } else { m.anaconda_memory.clone() }); + if let Some(m) = &m { + from.push(format!("pamięć tytułu {m}")); + } + self.title_memory = m.unwrap_or_else(|| "standard".into()).to_ascii_lowercase(); + } + // the standard budgets; "advanced" adds memory the console docs do not give publicly – + // the standard figure stays (xcl.toml can raise it) + self.title_memory_mb = self.title_memory_budget_mb(); + if self.pls_mb == 0 { + self.pls_mb = match manifest.and_then(|m| m.pls_mb) { + Some(v) => { + from.push(format!("PLS {v} MB")); + v + } + None => 4096, + }; + } + if self.smt == "auto" { + let on = manifest.and_then(|m| m.smt); + if let Some(v) = on { + from.push(format!("SMT {}", if v { "włączone" } else { "wyłączone" })); + } + self.smt = if on.unwrap_or(true) { "on" } else { "off" }.into(); + } + if self.uma_mb == 0 { + self.uma_mb = if self.title_memory == "advanced" { 4095 } else { 2048 }; + } + from + } + + /// TitleMemoryStatus budget (MiB): `title_memory_mb`, 0 = the device's standard budget. + pub fn title_memory_budget_mb(&self) -> u64 { + match self.title_memory_mb { + 0 if self.device_type.eq_ignore_ascii_case("lockhart") => 8_192, + 0 => 13_824, + v => v, + } + } + + /// Logical processors of the title's VM: 7 cores, 2 threads each with SMT. + pub fn console_processors(&self) -> u32 { + if self.smt == "off" { + 7 + } else { + 14 + } + } +} + impl Default for Config { fn default() -> Self { let mut mounts = BTreeMap::new(); @@ -55,7 +123,11 @@ impl Default for Config { user_id: 0x0009_0000_0000_0001, console_id: "XCL00000000000000000000000000000000000001".into(), sandbox_id: "RETAIL".into(), - title_memory_mb: 13_824, + title_memory_mb: 0, + title_memory: "auto".into(), + pls_mb: 0, + smt: "auto".into(), + uma_mb: 0, log_level: "info".into(), log_to_stderr: true, exe: String::new(), @@ -136,7 +208,7 @@ mod tests { let s = toml::to_string_pretty(&c).unwrap(); let back: Config = toml::from_str(&s).unwrap(); assert_eq!(back.mounts.get("G:").map(String::as_str), Some("{game}")); - assert_eq!(back.title_memory_mb, 13_824); + assert_eq!(back.title_memory_mb, 0); } #[test] diff --git a/crates/xcl/src/hle/kernelx.rs b/crates/xcl/src/hle/kernelx.rs index 6a045c8..23a52ed 100644 --- a/crates/xcl/src/hle/kernelx.rs +++ b/crates/xcl/src/hle/kernelx.rs @@ -49,6 +49,15 @@ pub fn exports() -> Vec<(&'static str, usize)> { GetThreadName, SetThreadpoolAffinityMask, QueryProcessorSchedulingStatistics, + GetSystemInfo, + GetNativeSystemInfo, + GetActiveProcessorCount, + GetMaximumProcessorCount, + GetCurrentProcessorNumber, + GetCurrentProcessorNumberEx, + SetThreadAffinityMask, + SetThreadIdealProcessor, + GetProcessAffinityMask, GetConsoleType, OutputDebugStringA, OutputDebugStringW, @@ -313,7 +322,7 @@ pub unsafe extern "system" fn TitleMemoryStatus(buf: *mut TitleMemoryStatusT) -> fnd::SetLastError(fnd::ERROR_INVALID_PARAMETER); return 0; } - let total = crate::state::config().title_memory_mb * 1024 * 1024; + let total = crate::state::config().title_memory_budget_mb() * 1024 * 1024; let used = process_private_bytes().min(total); let b = &mut *buf; b.total = total; @@ -446,6 +455,104 @@ pub unsafe extern "system" fn GetThreadName(h: HANDLE, buf: *mut u16, len: usize 1 } +// --------------------------------------------------------------------------------------- +// processors: the title sees its VM (7 cores, 14 logical processors with SMT – per-core +// tables sized for that stay in range); affinity masks fold onto the PC's processors +// --------------------------------------------------------------------------------------- + +fn console_cpus() -> u32 { + crate::state::config().console_processors() +} + +fn pc_cpus() -> u32 { + static N: std::sync::OnceLock = std::sync::OnceLock::new(); + *N.get_or_init(|| unsafe { + let mut si = std::mem::zeroed::(); + windows_sys::Win32::System::SystemInformation::GetSystemInfo(&mut si); + si.dwNumberOfProcessors.clamp(1, 64) + }) +} + +/// A mask over the title's logical processors → the same work spread over the PC's. +pub fn fold_affinity(mask: usize, pc: u32) -> usize { + let mut out = 0usize; + for i in 0..usize::BITS { + if mask >> i & 1 == 1 { + out |= 1 << (i % pc); + } + } + if out == 0 { + (1usize << pc.min(63)) - 1 + } else { + out + } +} + +fn console_mask() -> usize { + (1usize << console_cpus()) - 1 +} + +unsafe fn patch_system_info(si: *mut windows_sys::Win32::System::SystemInformation::SYSTEM_INFO) { + if !si.is_null() { + (*si).dwNumberOfProcessors = console_cpus(); + (*si).dwActiveProcessorMask = console_mask(); + } +} + +pub unsafe extern "system" fn GetSystemInfo(si: *mut windows_sys::Win32::System::SystemInformation::SYSTEM_INFO) { + windows_sys::Win32::System::SystemInformation::GetSystemInfo(si); + patch_system_info(si); +} + +pub unsafe extern "system" fn GetNativeSystemInfo(si: *mut windows_sys::Win32::System::SystemInformation::SYSTEM_INFO) { + windows_sys::Win32::System::SystemInformation::GetNativeSystemInfo(si); + patch_system_info(si); +} + +pub unsafe extern "system" fn GetActiveProcessorCount(_group: u16) -> u32 { + console_cpus() +} + +pub unsafe extern "system" fn GetMaximumProcessorCount(_group: u16) -> u32 { + console_cpus() +} + +pub unsafe extern "system" fn GetCurrentProcessorNumber() -> u32 { + thr::GetCurrentProcessorNumber() % console_cpus() +} + +/// PROCESSOR_NUMBER { WORD Group; BYTE Number; BYTE Reserved; } +pub unsafe extern "system" fn GetCurrentProcessorNumberEx(out: *mut [u8; 4]) { + if !out.is_null() { + *out = [0, 0, (GetCurrentProcessorNumber() & 0xFF) as u8, 0]; + } +} + +pub unsafe extern "system" fn SetThreadAffinityMask(thread: fnd::HANDLE, mask: usize) -> usize { + match thr::SetThreadAffinityMask(thread, fold_affinity(mask, pc_cpus())) { + 0 => 0, + _ => console_mask(), + } +} + +pub unsafe extern "system" fn SetThreadIdealProcessor(thread: fnd::HANDLE, ideal: u32) -> u32 { + // MAXIMUM_PROCESSORS (32 on x64 for this call) asks for the current one + let real = if ideal >= 32 { ideal } else { ideal % pc_cpus() }; + match thr::SetThreadIdealProcessor(thread, real) { + u32::MAX => u32::MAX, + prev => prev % console_cpus(), + } +} + +pub unsafe extern "system" fn GetProcessAffinityMask(_process: fnd::HANDLE, proc_mask: *mut usize, sys_mask: *mut usize) -> BOOL { + for p in [proc_mask, sys_mask] { + if !p.is_null() { + *p = console_mask(); + } + } + 1 +} + pub unsafe extern "system" fn SetThreadpoolAffinityMask(_pool: *mut c_void, _mask: usize) -> BOOL { 1 } @@ -807,3 +914,16 @@ mod tests { assert_eq!(unsafe { TitleMemoryStatus(&mut s) }, 0); } } + +#[cfg(test)] +mod affinity_tests { + #[test] + fn affinity_masks_fold_onto_the_pc() { + use super::fold_affinity; + // 14 console processors on an 8-thread PC: 8..13 land on 0..5 + assert_eq!(fold_affinity(1 << 13, 8), 1 << 5); + assert_eq!(fold_affinity(0x3FFF, 8), 0xFF); + assert_eq!(fold_affinity(1 << 3, 16), 1 << 3); + assert_eq!(fold_affinity(0, 4), 0xF, "empty mask: every PC processor"); + } +} diff --git a/crates/xcl/src/hle/xgameruntime/mod.rs b/crates/xcl/src/hle/xgameruntime/mod.rs index c6a1110..3405ae5 100644 --- a/crates/xcl/src/hle/xgameruntime/mod.rs +++ b/crates/xcl/src/hle/xgameruntime/mod.rs @@ -328,7 +328,11 @@ pub unsafe extern "system" fn XPersistentLocalStorageGetSpaceInfo(info: *mut XPe } let (mut avail, mut total, mut free) = (0u64, 0u64, 0u64); GetDiskFreeSpaceExW(crate::util::wide(&pls_path()).as_ptr(), &mut avail, &mut total, &mut free); - let budget = 4u64 << 30; // report a 4 GiB PLS quota + // the title's quota (MicrosoftGame.Config , else 4 GiB) + let budget = match crate::state::config().pls_mb { + 0 => 4096, + mb => mb, + } << 20; *info = XPersistentLocalStorageSpaceInfo { available_free_bytes: avail.min(budget), total_free_bytes: free.min(budget), diff --git a/crates/xcl/src/host.rs b/crates/xcl/src/host.rs index d765fea..c14258e 100644 --- a/crates/xcl/src/host.rs +++ b/crates/xcl/src/host.rs @@ -25,6 +25,9 @@ pub fn host_main(boot_path: &Path) -> i32 { crate::xlog_info!("gra: {} (folder tylko do odczytu: {})", boot.exe_path.display(), boot.game_root.display()); crate::xlog_info!("dane XCL: {}", boot.data_dir.display()); + if std::env::var_os("XCL_UMA_MB").is_none() && boot.config.uma_mb != 0 { + std::env::set_var("XCL_UMA_MB", boot.config.uma_mb.to_string()); + } crate::state::set_boot(boot.clone()); crate::stubs::init_db(&boot.config.stub_returns); crate::hle::init(&boot.config); diff --git a/crates/xcl/src/launcher.rs b/crates/xcl/src/launcher.rs index dd54942..f4196db 100644 --- a/crates/xcl/src/launcher.rs +++ b/crates/xcl/src/launcher.rs @@ -200,6 +200,8 @@ pub fn run(target: &Path, args: &[String]) -> i32 { }; let exe = if !config.exe.is_empty() && root.join(&config.exe).is_file() { root.join(&config.exe) } else { exe }; config.exe = exe.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default(); + let manifest = crate::manifest::load(&root); + let from_manifest = config.resolve(manifest.as_ref()); crate::tls::cleanup_cache(&data_dir.join("cache")); let boot = Boot { title: title.clone(), exe_path: exe.clone(), game_root: root.clone(), data_dir: data_dir.clone(), config }; let boot_path = data_dir.join("cache").join("boot.json"); @@ -212,6 +214,9 @@ pub fn run(target: &Path, args: &[String]) -> i32 { println!("XCL {} – uruchamiam '{title}'", env!("CARGO_PKG_VERSION")); println!(" exe: {}", exe.display()); println!(" dane: {}", data_dir.display()); + if !from_manifest.is_empty() { + println!(" MicrosoftGame.Config: {}", from_manifest.join(", ")); + } match crate::inspect::console_imports(&exe) { Some(c) if c.is_empty() => println!( " UWAGA: ten plik nie importuje żadnej biblioteki konsoli (kernelx / xgameruntime / d3d12_xs) –\n to raczej zwykły program Windows, a nie gra z Xbox Series." diff --git a/crates/xcl/src/lib.rs b/crates/xcl/src/lib.rs index cb373e4..d9ba072 100644 --- a/crates/xcl/src/lib.rs +++ b/crates/xcl/src/lib.rs @@ -7,6 +7,7 @@ #[macro_use] pub mod log; +pub mod manifest; pub mod config; pub mod disasm; pub mod eh; diff --git a/crates/xcl/src/manifest.rs b/crates/xcl/src/manifest.rs new file mode 100644 index 0000000..65cb4ae --- /dev/null +++ b/crates/xcl/src/manifest.rs @@ -0,0 +1,69 @@ +//! The few `MicrosoftGame.Config` settings XCL uses (game manifest in the game folder): +//! the persistent local storage quota, SMT and the title memory mode. Plain text search – +//! the file is small XML and only these elements matter. + +use std::path::Path; + +#[derive(Debug, Default, Clone, PartialEq, Eq)] +pub struct Manifest { + /// `` + pub pls_mb: Option, + /// `` + pub smt: Option, + /// `` / ``: "Standard" or "Advanced" + pub anaconda_memory: Option, + pub lockhart_memory: Option, +} + +fn element<'a>(xml: &'a str, name: &str) -> Option<&'a str> { + let open = format!("<{name}>"); + let at = xml.find(&open)? + open.len(); + let end = xml[at..].find(&format!(""))?; + Some(xml[at..at + end].trim()) +} + +pub fn parse(xml: &str) -> Manifest { + let pls = xml.find("").and_then(|i| element(&xml[i..], "SizeMB")); + Manifest { + pls_mb: pls.and_then(|v| v.parse().ok()), + smt: element(xml, "SMTEnabled").map(|v| v.eq_ignore_ascii_case("true")), + anaconda_memory: element(xml, "XboxAnacondaTitleMemory").map(str::to_string), + lockhart_memory: element(xml, "XboxLockhartTitleMemory").map(str::to_string), + } +} + +/// `MicrosoftGame.Config` of a game folder (None: no manifest). +pub fn load(game_root: &Path) -> Option { + let p = std::fs::read_dir(game_root).ok()?.flatten().map(|e| e.path()).find(|p| p.file_name().is_some_and(|n| n.eq_ignore_ascii_case("MicrosoftGame.Config")))?; + let raw = std::fs::read(p).ok()?; + // UTF-8 or UTF-16 LE (with BOM) + let text = if raw.starts_with(&[0xFF, 0xFE]) { + String::from_utf16_lossy(&raw[2..].chunks(2).map(|c| u16::from_le_bytes([c[0], *c.get(1).unwrap_or(&0)])).collect::>()) + } else { + String::from_utf8_lossy(&raw).into_owned() + }; + Some(parse(&text)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn reads_the_settings_xcl_uses() { + let xml = r#" + + 2048 + + + true + Advanced + Advanced + +"#; + let m = parse(xml); + assert_eq!((m.pls_mb, m.smt), (Some(2048), Some(true))); + assert_eq!((m.anaconda_memory.as_deref(), m.lockhart_memory.as_deref()), (Some("Advanced"), Some("Advanced"))); + assert_eq!(parse(""), Manifest::default()); + } +} diff --git a/crates/xcl/tests/e2e.rs b/crates/xcl/tests/e2e.rs index 3f7bee8..d9cd478 100644 --- a/crates/xcl/tests/e2e.rs +++ b/crates/xcl/tests/e2e.rs @@ -94,6 +94,12 @@ fn test_title_runs_and_game_folder_is_untouched() { // (2) the game folder is untouched assert_eq!(snapshot(&game), before, "FOLDER GRY ZOSTAŁ ZMIENIONY"); + // (2b) MicrosoftGame.config (tests/data/testgame): PLS quota, SMT and the title memory mode + assert!(stdout.contains("MicrosoftGame.Config: pamięć tytułu Advanced, PLS 2048 MB, SMT włączone"), "launcher: {stdout}"); + let boot: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(data.join("XCL.TestTitle").join("cache").join("boot.json")).unwrap()).unwrap(); + let c = &boot["config"]; + assert_eq!((c["pls_mb"].as_u64(), c["smt"].as_str(), c["title_memory"].as_str(), c["uma_mb"].as_u64()), (Some(2048), Some("on"), Some("advanced"), Some(4095))); + // (3) overlay contents let tdata = data.join("XCL.TestTitle"); let ov = tdata.join("overlay").join("data"); diff --git a/tests/data/testgame/MicrosoftGame.config b/tests/data/testgame/MicrosoftGame.config index 75914e4..1c91ca5 100644 --- a/tests/data/testgame/MicrosoftGame.config +++ b/tests/data/testgame/MicrosoftGame.config @@ -6,4 +6,13 @@ + + + 2048 + + + true + Advanced + Advanced +