diff --git a/crates/victory-loader/src/crash.rs b/crates/victory-loader/src/crash.rs index eb6bb5e..b28a939 100644 --- a/crates/victory-loader/src/crash.rs +++ b/crates/victory-loader/src/crash.rs @@ -136,8 +136,12 @@ const FATAL: [i32; 6] = [ /// Console CPUs are Zen 2: titles use AMD-only instructions Intel CPUs /// lack. Does their job instead: `clzero` (`0f 01 fc`) zeroes the 64-byte -/// line at `rax`; `monitorx`/`mwaitx` (`0f 01 fa`/`fb`) are wait hints, so -/// a spin loop around them works with mwaitx as a yield. +/// line at `rax` (see [`clzero`] for the loops around it); +/// `monitorx`/`mwaitx` (`0f 01 fa`/`fb`) are wait hints, so a spin loop +/// around them works with mwaitx as a yield; `rdpru` (`0f 01 fd`) reads +/// MPERF (`ecx` 0) or APERF (1), cycles at the nominal and the actual +/// frequency, and gets the time-stamp counter for both: a core that never +/// changes frequency. unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool { // SAFETY: the system passes valid exception pointers; the faulting // instruction's bytes are readable, as the CPU just fetched them. @@ -149,9 +153,18 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool { } let code = context.Rip as *const u8; match code.cast::<[u8; 3]>().read_unaligned() { - [0x0f, 0x01, 0xfc] => ((context.Rax & !63) as *mut u8).write_bytes(0, 64), + [0x0f, 0x01, 0xfc] => return clzero(context), [0x0f, 0x01, 0xfa] => {} [0x0f, 0x01, 0xfb] => std::thread::yield_now(), + [0x0f, 0x01, 0xfd] => { + let cycles = if context.Rcx as u32 <= 1 { + std::arch::x86_64::_rdtsc() + } else { + 0 + }; + context.Rax = cycles & 0xffff_ffff; + context.Rdx = cycles >> 32; + } _ => { let Some(len) = sse4a(code, &mut context.Anonymous.FltSave.XmmRegisters) else { return false; @@ -165,6 +178,129 @@ unsafe fn emulate_amd_only(info: *mut EXCEPTION_POINTERS) -> bool { true } +/// `clzero` at `context.Rip`. An exception costs about a microsecond and +/// titles clear tens of kilobytes at a time, a line per pass, so the two +/// loops compilers build around the instruction are done whole in one +/// exception, leaving registers and flags as their last pass would (`a` +/// and `b` any registers but `rax`): +/// +/// ```text +/// top: clzero top: mov rax, a +/// add rax, 0x40 add a, 0x40 +/// cmp rax, b clzero +/// jb top cmp a, b +/// jb top +/// ``` +/// +/// Anything else around it is one line and the next instruction. +/// +/// # Safety +/// `context` is the state of a thread that faulted at a `clzero`; the +/// lines its registers name are the title's to zero. +unsafe fn clzero(context: &mut CONTEXT) -> bool { + // SAFETY: (every use) a line the title asked to zero. + let zero = |rax: u64| unsafe { ((rax & !63) as *mut u8).write_bytes(0, 64) }; + let at = context.Rip as usize; + // The loops' other instructions are read only where that stays inside + // the faulting instruction's page (the next one may not be mapped). + let page = at & 0xfff; + if page + 12 <= 0x1000 { + // SAFETY: inside the page the CPU fetched the instruction from. + let after = unsafe { &*((at + 3) as *const [u8; 9]) }; + if let [0x48, 0x83, 0xc0, 0x40, cmp @ .., 0x72, 0xf4] = after + && let Some((0, limit)) = compare_registers(cmp) + { + let limit = *register(context, limit); + loop { + zero(context.Rax); + context.Rax = context.Rax.wrapping_add(0x40); + if context.Rax >= limit { + break; + } + } + context.EFlags = context.EFlags & !STATUS_FLAGS | compare(context.Rax, limit); + context.Rip += 12; + return true; + } + } + if (7..=0x1000 - 8).contains(&page) { + // SAFETY: inside the page the CPU fetched the instruction from. + let (before, after) = + unsafe { (&*((at - 7) as *const [u8; 7]), &*((at + 3) as *const [u8; 5])) }; + if let [rex @ (0x48 | 0x4c), 0x89, load, add_rex @ (0x48 | 0x49), 0x83, add, 0x40] = *before + && let [cmp @ .., 0x72, 0xf1] = after + && let Some((a, b)) = compare_registers(cmp) + && load & 0xc7 == 0xc0 + && a == (load >> 3) & 7 | (rex & 4) << 1 + && add & 0xf8 == 0xc0 + && a == add & 7 | (add_rex & 1) << 3 + && a != 0 + && b != 0 + { + let limit = *register(context, b); + loop { + zero(context.Rax); + let next = *register(context, a); + if next >= limit { + break; + } + context.Rax = next; + *register(context, a) = next.wrapping_add(0x40); + } + context.EFlags = + context.EFlags & !STATUS_FLAGS | compare(*register(context, a), limit); + context.Rip += 8; + return true; + } + } + zero(context.Rax); + context.Rip += 3; + true +} + +/// `cmp rm, reg` between 64-bit registers (`REX.W 39 /r`): (`rm`, `reg`). +fn compare_registers(code: &[u8]) -> Option<(u8, u8)> { + let &[rex @ 0x48..=0x4f, 0x39, modrm] = code else { + return None; + }; + (modrm >> 6 == 3).then_some((modrm & 7 | (rex & 1) << 3, (modrm >> 3) & 7 | (rex & 4) << 1)) +} + +/// The status flags `cmp a, b` leaves (CF, PF, AF, ZF, SF, OF), and their +/// bits in `EFLAGS`. +fn compare(a: u64, b: u64) -> u32 { + let r = a.wrapping_sub(b); + u32::from(a < b) + | u32::from((r as u8).count_ones().is_multiple_of(2)) << 2 + | (((a ^ b ^ r) >> 4) as u32 & 1) << 4 + | u32::from(r == 0) << 6 + | ((r >> 63) as u32) << 7 + | ((((a ^ b) & (a ^ r)) >> 63) as u32) << 11 +} +const STATUS_FLAGS: u32 = 0x8d5; + +/// The general register numbered `n` as instructions encode it. +fn register(context: &mut CONTEXT, n: u8) -> &mut u64 { + match n & 15 { + 0 => &mut context.Rax, + 1 => &mut context.Rcx, + 2 => &mut context.Rdx, + 3 => &mut context.Rbx, + 4 => &mut context.Rsp, + 5 => &mut context.Rbp, + 6 => &mut context.Rsi, + 7 => &mut context.Rdi, + 8 => &mut context.R8, + 9 => &mut context.R9, + 10 => &mut context.R10, + 11 => &mut context.R11, + 12 => &mut context.R12, + 13 => &mut context.R13, + 14 => &mut context.R14, + _ => &mut context.R15, + } +} + /// SSE4a's `extrq`/`insertq` (register forms, the only ones there are) on /// the saved XMM registers; returns the instruction's length. unsafe fn sse4a(code: *const u8, xmm: &mut [M128A; 16]) -> Option { @@ -487,5 +623,73 @@ mod tests { std::arch::asm!(".byte 0x0f, 0x01, 0xfa", ".byte 0x0f, 0x01, 0xfb", in("rax") at, in("ecx") 0, in("edx") 0) }; + // rdpru of MPERF: a count in edx:eax, their high halves clear. + let (low, high): (u64, u64); + // SAFETY: touches only the named registers. + unsafe { + std::arch::asm!(".byte 0x0f, 0x01, 0xfd", + out("rax") low, inout("rcx") 0u64 => _, out("rdx") high) + }; + assert_ne!(high << 32 | low, 0); + assert!(high >> 32 == 0 && low >> 32 == 0); + } + + /// The flags a native `cmp a, b` leaves. + fn native_compare(a: u64, b: u64) -> u32 { + let flags: u64; + // SAFETY: compares two registers and reads the flags. + unsafe { + std::arch::asm!("cmp {a}, {b}", "pushfq", "pop {f}", + a = in(reg) a, b = in(reg) b, f = out(reg) flags) + }; + flags as u32 & STATUS_FLAGS + } + + #[test] + fn compare_sets_flags_like_the_cpu() { + let values = [0, 1, 0x40, 0x7f, 0x80, 0xff, 1 << 63, (1 << 63) - 1, !0, !0 - 0x40]; + for a in values { + for b in values { + assert_eq!(compare(a, b), native_compare(a, b), "cmp {a:#x}, {b:#x}"); + } + } + } + + #[test] + fn clzero_loops_end_like_their_last_pass() { + report_crashes(); + #[repr(align(64))] + struct Lines([u8; 192]); + let zeroed = |l: &Lines| [l.0[0] == 0, l.0[64] == 0, l.0[128] == 0]; + // clzero; add rax, 0x40; cmp rax, r10; jb + let mut mem = Lines([0xff; 192]); + let start = mem.0.as_mut_ptr() as u64; + let (end, flags): (u64, u64); + // SAFETY: zeroes the first two lines of `mem`. + unsafe { + std::arch::asm!( + ".byte 0x0f, 0x01, 0xfc, 0x48, 0x83, 0xc0, 0x40, 0x4c, 0x39, 0xd0, 0x72, 0xf4", + "pushfq", "pop {f}", + inout("rax") start => end, in("r10") start + 100, f = out(reg) flags) + }; + assert_eq!(end, start + 128); + assert_eq!(flags as u32 & STATUS_FLAGS, native_compare(end, start + 100)); + assert_eq!(zeroed(&mem), [true, true, false]); + // mov rax, rdx; add rdx, 0x40; clzero; cmp rdx, r8; jb + let mut mem = Lines([0xff; 192]); + let start = mem.0.as_mut_ptr() as u64; + let (last, next, flags): (u64, u64, u64); + // SAFETY: zeroes the last two lines of `mem`. + unsafe { + std::arch::asm!( + ".byte 0x48, 0x89, 0xd0, 0x48, 0x83, 0xc2, 0x40, 0x0f, 0x01, 0xfc", + ".byte 0x4c, 0x39, 0xc2, 0x72, 0xf1", + "pushfq", "pop {f}", + out("rax") last, inout("rdx") start + 64 => next, in("r8") start + 192, + f = out(reg) flags) + }; + assert_eq!((last, next), (start + 128, start + 192)); + assert_eq!(flags as u32 & STATUS_FLAGS, native_compare(next, start + 192)); + assert_eq!(zeroed(&mem), [false, true, true]); } }