Skip to content
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ cargo build --profile diag -p xtp --features diagnostics # target\diag\xtp.exe
| `XTP_XMA_DUMP=<dir>` | saves the first 32 XMA voices' context and input |
| `XTP_WATCHDOG_HOOK=1` | every build: keeps the title's own watchdog from ending it after about 30 s without progress (slow world frames), and logs a warning (at most every 30 s) when it would have; a real deadlock then hangs for ever, with no `crashes.log` entry; known builds only (1.0.2.1), otherwise a warning and nothing patched |
| `XTP_SKIP_INTRO=1` | every build: cuts the intro movie of the start-up front end (sent to its last frame when it starts), so the world starts loading about 13 s sooner (docs/analysis/intro-skip.md); known builds only (1.0.2.1), otherwise a warning and nothing changed |
| `XTP_SKIP_CPU_CHECK=1` | every build: skips the host CPU feature check that `xtp run --execute` performs before starting the title |

Every build appends crashes and host GPU failures to `crashes.log` in the
title's data folder. The loader also prints the first 16 title C++ throws
Expand Down
1 change: 1 addition & 0 deletions apps/xtp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

mod census;
mod inspect;
mod preflight;
mod run;
mod shader;
mod survey;
Expand Down
188 changes: 188 additions & 0 deletions apps/xtp/src/preflight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
//! Host CPU preflight check before starting a title.
//!
//! Xbox Series consoles use an AMD Zen 2 CPU, so titles are compiled assuming
//! its baseline instruction-set extensions are always present. Checking them
//! before execution gives a clear error instead of an illegal-instruction crash
//! deep inside title code.
//!
//! Console-only or AMD-specific extensions that `xtp-loader` emulates in its
//! exception handler (`CLZERO`, `MONITORX`/`MWAITX`, `RDPRU`, `RDPID`, `CLWB`,
//! `SSE4a`) are intentionally omitted here.

use anyhow::Result;

/// Environment variable that bypasses the host CPU feature check when set to `"1"`.
pub(crate) const SKIP_ENV: &str = "XTP_SKIP_CPU_CHECK";

/// Console CPU features required by titles and not emulated by `xtp-loader`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CpuFeature {
Sse41,
Sse42,
Popcnt,
Avx,
Avx2,
Fma,
F16c,
Bmi1,
Bmi2,
Lzcnt,
Movbe,
}

impl CpuFeature {
/// Every required feature in display order.
pub(crate) const REQUIRED: &'static [Self] = &[
Self::Sse41,
Self::Sse42,
Self::Popcnt,
Self::Avx,
Self::Avx2,
Self::Fma,
Self::F16c,
Self::Bmi1,
Self::Bmi2,
Self::Lzcnt,
Self::Movbe,
];

/// Human-readable ISA name shown in diagnostics.
pub(crate) const fn name(self) -> &'static str {
match self {
Self::Sse41 => "SSE4.1",
Self::Sse42 => "SSE4.2",
Self::Popcnt => "POPCNT",
Self::Avx => "AVX",
Self::Avx2 => "AVX2",
Self::Fma => "FMA",
Self::F16c => "F16C",
Self::Bmi1 => "BMI1",
Self::Bmi2 => "BMI2",
Self::Lzcnt => "LZCNT",
Self::Movbe => "MOVBE",
}
}
}

/// Verify that the host CPU supports every required console feature, unless
/// `XTP_SKIP_CPU_CHECK=1` is set.
#[cfg(windows)]
pub(crate) fn check_cpu() -> Result<()> {
let skip = std::env::var(SKIP_ENV).is_ok_and(|v| v == "1");
check_cpu_with(skip, host_has_feature)
}

/// Core check with injectable skip flag and feature probe, for testing.
pub(crate) fn check_cpu_with(skip: bool, probe: impl FnMut(CpuFeature) -> bool) -> Result<()> {
if skip {
eprintln!("warning: skipping CPU feature check ({SKIP_ENV}=1)");
return Ok(());
}
let missing = missing_features(probe);
if let Some(message) = format_missing(&missing) {
anyhow::bail!("{message}");
}
Ok(())
}

/// Return the required console CPU features that `probe` reports as absent.
pub(crate) fn missing_features(mut probe: impl FnMut(CpuFeature) -> bool) -> Vec<CpuFeature> {
CpuFeature::REQUIRED
.iter()
.copied()
.filter(|&feature| !probe(feature))
.collect()
}

/// Format a user-facing error listing `missing` features, or `None` if empty.
pub(crate) fn format_missing(missing: &[CpuFeature]) -> Option<String> {
if missing.is_empty() {
return None;
}
let names: Vec<&str> = missing.iter().map(|f| f.name()).collect();
Some(format!(
"this CPU lacks console features the title requires: {}\n\
(features emulated by XTP are not listed; set {SKIP_ENV}=1 to run anyway)",
names.join(", ")
))
}

#[cfg(all(windows, any(target_arch = "x86", target_arch = "x86_64")))]
fn host_has_feature(feature: CpuFeature) -> bool {
match feature {
CpuFeature::Sse41 => std::arch::is_x86_feature_detected!("sse4.1"),
CpuFeature::Sse42 => std::arch::is_x86_feature_detected!("sse4.2"),
CpuFeature::Popcnt => std::arch::is_x86_feature_detected!("popcnt"),
CpuFeature::Avx => std::arch::is_x86_feature_detected!("avx"),
CpuFeature::Avx2 => std::arch::is_x86_feature_detected!("avx2"),
CpuFeature::Fma => std::arch::is_x86_feature_detected!("fma"),
CpuFeature::F16c => std::arch::is_x86_feature_detected!("f16c"),
CpuFeature::Bmi1 => std::arch::is_x86_feature_detected!("bmi1"),
CpuFeature::Bmi2 => std::arch::is_x86_feature_detected!("bmi2"),
CpuFeature::Lzcnt => std::arch::is_x86_feature_detected!("lzcnt"),
CpuFeature::Movbe => std::arch::is_x86_feature_detected!("movbe"),
}
}

#[cfg(all(windows, not(any(target_arch = "x86", target_arch = "x86_64"))))]
fn host_has_feature(_: CpuFeature) -> bool {
false
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn all_features_present_produces_no_message() {
let missing = missing_features(|_| true);
assert!(missing.is_empty());
assert_eq!(format_missing(&missing), None);
assert!(check_cpu_with(false, |_| true).is_ok());
}

#[test]
fn missing_avx2_is_named_in_error() {
let missing = missing_features(|f| f != CpuFeature::Avx2);
assert_eq!(missing, [CpuFeature::Avx2]);

let message = format_missing(&missing).expect("missing AVX2 should produce a message");
assert!(message.contains("AVX2"), "message: {message}");
assert!(message.contains(SKIP_ENV), "message: {message}");

let err = check_cpu_with(false, |f| f != CpuFeature::Avx2).unwrap_err();
assert!(err.to_string().contains("AVX2"), "err: {err}");
}

#[test]
fn multiple_missing_features_are_all_listed() {
let missing = missing_features(|f| {
!matches!(f, CpuFeature::Avx2 | CpuFeature::Fma | CpuFeature::Bmi2)
});
assert_eq!(
missing,
[CpuFeature::Avx2, CpuFeature::Fma, CpuFeature::Bmi2]
);

let message = format_missing(&missing).expect("missing features should produce a message");
assert!(message.contains("AVX2, FMA, BMI2"), "message: {message}");
}

#[test]
fn skip_flag_bypasses_missing_features() {
assert!(check_cpu_with(true, |_| false).is_ok());
}

#[test]
fn emulated_features_are_not_required() {
let names: Vec<&str> = CpuFeature::REQUIRED.iter().map(|f| f.name()).collect();
for emulated in [
"CLZERO", "MONITORX", "MWAITX", "RDPRU", "RDPID", "CLWB", "SSE4a",
] {
assert!(
!names.contains(&emulated),
"emulated feature {emulated} should not be required"
);
}
}
}
4 changes: 4 additions & 0 deletions apps/xtp/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ pub fn run(
use xtp_hle::{HleResolver, HostPolicy};
use xtp_loader::{BindReport, UnimplementedPolicy, WithTitleDlls};

if execute {
crate::preflight::check_cpu()?;
}

// A title that relaunched itself: the old process must be gone first.
xtp_hle::modules::lifecycle::wait_for_previous();
let cfg = xtp_core::Config::load()?;
Expand Down
Loading