Runtime: validate Zen 2 ISA features and CPUID at startup in launcher and host

This commit is contained in:
slopgobblercobbler
2026-11-23 22:42:53 +01:00
parent 6da33fa7ad
commit 5ba2b6a67f
2 changed files with 130 additions and 23 deletions
+48
View File
@@ -8,6 +8,18 @@ use crate::config::Boot;
pub static HOST_MODE: AtomicBool = AtomicBool::new(false);
/// Validate that this CPU has the console Zen 2 instruction set extensions.
/// Returns Ok(()) if all features are present or if XCL_SKIP_CPU_CHECK is set;
/// returns Err(missing) listing any missing features.
pub fn check_cpu_requirements() -> Result<(), Vec<&'static str>> {
let missing = crate::launcher::missing_console_cpu_features();
if missing.is_empty() || std::env::var_os("XCL_SKIP_CPU_CHECK").is_some() {
Ok(())
} else {
Err(missing)
}
}
pub fn host_main(boot_path: &Path) -> i32 {
let boot: Boot = match std::fs::read_to_string(boot_path).map_err(|e| e.to_string()).and_then(|s| serde_json::from_str(&s).map_err(|e| e.to_string())) {
Ok(b) => b,
@@ -29,6 +41,22 @@ pub fn host_main(boot_path: &Path) -> i32 {
std::env::set_var("XCL_UMA_MB", boot.config.uma_mb.to_string());
}
crate::state::set_boot(boot.clone());
// the console's CPU is a Zen 2: its titles are compiled for it and fault with an illegal
// instruction (0xC000001D) on a PC without its extensions – said here instead
let missing = crate::launcher::missing_console_cpu_features();
if !missing.is_empty() {
if std::env::var_os("XCL_SKIP_CPU_CHECK").is_some() {
crate::xlog_warn!("this CPU lacks {} (XCL_SKIP_CPU_CHECK set, continuing)", missing.join(", "));
println!(" WARNING: this CPU lacks {} (XCL_SKIP_CPU_CHECK set, continuing)", missing.join(", "));
} else {
let msg = crate::launcher::format_missing_cpu_message(&missing);
crate::xlog_error!("{msg}");
eprintln!("xcl host: {msg}");
crate::report::write_final(Some(format!("CPU lacks required Zen 2 features: {}", missing.join(", "))));
return 0x7C1_0003;
}
}
// `XCL_ACTIVITY=1`: every 2 s what the title did since (a timeline of where it stops)
if std::env::var_os("XCL_ACTIVITY").is_some() {
let _ = std::thread::Builder::new().name("xcl-activity".into()).spawn(|| {
@@ -144,3 +172,23 @@ pub fn host_main(boot_path: &Path) -> i32 {
crate::log::flush();
code as i32
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn host_cpu_validation() {
std::env::set_var("XCL_SKIP_CPU_CHECK", "1");
assert!(check_cpu_requirements().is_ok());
std::env::remove_var("XCL_SKIP_CPU_CHECK");
let result = check_cpu_requirements();
let expected_missing = crate::launcher::missing_console_cpu_features();
if expected_missing.is_empty() {
assert!(result.is_ok());
} else {
assert_eq!(result.unwrap_err(), expected_missing);
}
}
}
+82 -23
View File
@@ -360,10 +360,7 @@ pub fn run(target: &Path, args: &[String]) -> i32 {
if std::env::var_os("XCL_SKIP_CPU_CHECK").is_some() {
println!(" WARNING: this CPU lacks {} (XCL_SKIP_CPU_CHECK set, continuing)", missing.join(", "));
} else {
eprintln!(
"xcl: this CPU lacks {}: Xbox Series X|S games are compiled for the console's Zen 2 CPU\n and need them (they would stop with an illegal instruction). XCL_SKIP_CPU_CHECK=1 runs anyway.",
missing.join(", ")
);
eprintln!("xcl: {}", format_missing_cpu_message(&missing));
return 2;
}
}
@@ -384,28 +381,53 @@ pub fn run(target: &Path, args: &[String]) -> i32 {
code as i32
}
/// Formats a friendly error message listing the missing console Zen 2 CPU features.
pub fn format_missing_cpu_message(missing: &[&str]) -> String {
format!(
"this CPU lacks {}: Xbox Series X|S games are compiled for the console's Zen 2 CPU\n and need them (they would stop with an illegal instruction). XCL_SKIP_CPU_CHECK=1 runs anyway.",
missing.join(", ")
)
}
/// Helper to evaluate CPU features using a query closure, returning missing feature names.
pub fn check_cpu_features<F: Fn(&str) -> bool>(has: F) -> Vec<&'static str> {
const REQUIRED: &[(&str, &str)] = &[
("sse4.2", "SSE4.2"),
("popcnt", "POPCNT"),
("avx", "AVX"),
("avx2", "AVX2"),
("fma", "FMA"),
("f16c", "F16C"),
("bmi1", "BMI1"),
("bmi2", "BMI2"),
("lzcnt", "LZCNT"),
("movbe", "MOVBE"),
];
let mut missing = Vec::new();
for &(feature, name) in REQUIRED {
if !has(feature) {
missing.push(name);
}
}
missing
}
/// Instruction set extensions of the console's Zen 2 that this CPU does not have (titles are
/// compiled for them: AVX2, FMA, F16C, BMI1 / BMI2, LZCNT, POPCNT, MOVBE, SSE4.2).
pub fn missing_console_cpu_features() -> Vec<&'static str> {
let mut missing = Vec::new();
macro_rules! need {
($($feature:tt => $name:literal),* $(,)?) => {
$(if !std::arch::is_x86_feature_detected!($feature) { missing.push($name); })*
};
}
need!(
"sse4.2" => "SSE4.2",
"popcnt" => "POPCNT",
"avx" => "AVX",
"avx2" => "AVX2",
"fma" => "FMA",
"f16c" => "F16C",
"bmi1" => "BMI1",
"bmi2" => "BMI2",
"lzcnt" => "LZCNT",
"movbe" => "MOVBE",
);
missing
check_cpu_features(|f| match f {
"sse4.2" => std::arch::is_x86_feature_detected!("sse4.2"),
"popcnt" => std::arch::is_x86_feature_detected!("popcnt"),
"avx" => std::arch::is_x86_feature_detected!("avx"),
"avx2" => std::arch::is_x86_feature_detected!("avx2"),
"fma" => std::arch::is_x86_feature_detected!("fma"),
"f16c" => std::arch::is_x86_feature_detected!("f16c"),
"bmi1" => std::arch::is_x86_feature_detected!("bmi1"),
"bmi2" => std::arch::is_x86_feature_detected!("bmi2"),
"lzcnt" => std::arch::is_x86_feature_detected!("lzcnt"),
"movbe" => std::arch::is_x86_feature_detected!("movbe"),
_ => false,
})
}
fn spawn_host(cmdline: &str, cwd: &Path, boot_path: &Path) -> Result<u32, String> {
@@ -645,4 +667,41 @@ mod tests {
assert_eq!(quote("say \"hi\""), "\"say \\\"hi\\\"\"");
assert_eq!(quote(""), "\"\"");
}
#[test]
fn cpu_feature_detection_identifies_missing_features() {
let all_present = check_cpu_features(|_| true);
assert!(all_present.is_empty());
let none_present = check_cpu_features(|_| false);
assert_eq!(
none_present,
vec!["SSE4.2", "POPCNT", "AVX", "AVX2", "FMA", "F16C", "BMI1", "BMI2", "LZCNT", "MOVBE"]
);
let missing_avx2 = check_cpu_features(|f| f != "avx2");
assert_eq!(missing_avx2, vec!["AVX2"]);
let missing_fma = check_cpu_features(|f| f != "fma");
assert_eq!(missing_fma, vec!["FMA"]);
let missing_bmi = check_cpu_features(|f| f != "bmi1" && f != "bmi2");
assert_eq!(missing_bmi, vec!["BMI1", "BMI2"]);
let missing_zen2_subset = check_cpu_features(|f| {
!["avx2", "fma", "bmi1", "bmi2", "popcnt", "sse4.2", "lzcnt"].contains(&f)
});
assert_eq!(
missing_zen2_subset,
vec!["SSE4.2", "POPCNT", "AVX2", "FMA", "BMI1", "BMI2", "LZCNT"]
);
}
#[test]
fn missing_cpu_message_format() {
let msg = format_missing_cpu_message(&["AVX2", "FMA", "BMI1", "BMI2", "POPCNT", "SSE4.2", "LZCNT"]);
assert!(msg.contains("this CPU lacks AVX2, FMA, BMI1, BMI2, POPCNT, SSE4.2, LZCNT"));
assert!(msg.contains("Zen 2 CPU"));
assert!(msg.contains("XCL_SKIP_CPU_CHECK=1"));
}
}