Skip to content
Closed
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
9 changes: 9 additions & 0 deletions crates/xcl/src/eh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ unsafe fn emulate_console_only(ctx: &mut windows_sys::Win32::System::Diagnostics
];
*regs[r as usize & 15] = cpu;
}
// SSE4a on the low quadword of an XMM register (the exception's context has them all)
ConsoleOnly::Insertq { dst, src, len, idx } => {
let x = &mut ctx.Anonymous.FltSave.XmmRegisters;
x[dst as usize & 15].Low = crate::insn::insertq(x[dst as usize & 15].Low, x[src as usize & 15].Low, len, idx);
}
ConsoleOnly::Extrq { dst, len, idx } => {
let x = &mut ctx.Anonymous.FltSave.XmmRegisters;
x[dst as usize & 15].Low = crate::insn::extrq(x[dst as usize & 15].Low, len, idx);
}
}
ctx.Rip += len as u64;
-1 // EXCEPTION_CONTINUE_EXECUTION
Expand Down
77 changes: 73 additions & 4 deletions crates/xcl/src/insn.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Instructions the console's CPU (Zen 2) has and a PC's may lack – a title raises
//! STATUS_ILLEGAL_INSTRUCTION on them on an Intel CPU before Ice Lake / Alder Lake (CLWB,
//! SHA) or on any Intel CPU (CLZERO, RDPRU, MONITORX / MWAITX, which are AMD's). The
//! exception handler (`eh`) emulates the ones decoded here and steps over them.
//! SHA) or on any Intel CPU (CLZERO, RDPRU, MONITORX / MWAITX and SSE4a's EXTRQ / INSERTQ,
//! which are AMD's). The exception handler (`eh`) emulates the ones decoded here and steps
//! over them.

/// A console-only instruction at the faulting address.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -18,10 +19,35 @@ pub enum ConsoleOnly {
/// `rdpid r64`: TSC_AUX (the processor number) into the register (CONTEXT order: rax, rcx,
/// rdx, rbx, rsp, rbp, rsi, rdi, r8..r15)
Rdpid(u8),
/// SSE4a `insertq xmm(dst), xmm(src), len, idx` (F2 0F 78 /r ib ib): `insertq` of the low
/// quadwords (a retail game has 19, in its float → half packing)
Insertq { dst: u8, src: u8, len: u8, idx: u8 },
/// SSE4a `extrq xmm(dst), len, idx` (66 0F 78 /0 ib ib): `extrq` of the low quadword
Extrq { dst: u8, len: u8, idx: u8 },
/// `ud2`: the title's own trap (not emulated)
Ud2,
}

/// The mask of an SSE4a bit field of `len` bits (6 bits; 0 means 64).
fn field_mask(len: u8) -> u64 {
match len & 63 {
0 => u64::MAX,
n => (1u64 << n) - 1,
}
}

/// SSE4a INSERTQ on the low quadwords: the low `len` bits of `src` replace bits `idx ..` of
/// `dst` (the upper quadword is undefined on the hardware; the emulation leaves it).
pub fn insertq(dst: u64, src: u64, len: u8, idx: u8) -> u64 {
let (mask, idx) = (field_mask(len), (idx & 63) as u32);
(dst & !(mask << idx)) | ((src & mask) << idx)
}

/// SSE4a EXTRQ on the low quadword: bits `idx ..` of `v`, `len` of them, in its low bits.
pub fn extrq(v: u64, len: u8, idx: u8) -> u64 {
(v >> (idx & 63)) & field_mask(len)
}

/// The length of a ModRM-encoded operand (ModRM, SIB and displacement) at `b[0]`, None when the
/// bytes run out.
fn modrm_len(b: &[u8]) -> Option<usize> {
Expand All @@ -48,12 +74,13 @@ fn modrm_len(b: &[u8]) -> Option<usize> {
/// The console-only instruction at the start of `code` and its length.
pub fn decode(code: &[u8]) -> Option<(ConsoleOnly, usize)> {
let mut i = 0;
let (mut p66, mut pf3, mut rex) = (false, false, 0u8);
let (mut p66, mut pf3, mut pf2, mut rex) = (false, false, false, 0u8);
while let Some(&b) = code.get(i) {
match b {
0x66 => p66 = true,
0xF3 => pf3 = true,
0xF2 | 0x2E | 0x3E | 0x26 | 0x36 | 0x64 | 0x65 => {}
0xF2 => pf2 = true,
0x2E | 0x3E | 0x26 | 0x36 | 0x64 | 0x65 => {}
0x40..=0x4F => {
rex = b;
i += 1;
Expand All @@ -76,6 +103,19 @@ pub fn decode(code: &[u8]) -> Option<(ConsoleOnly, usize)> {
(0xAE, Some(m)) if p66 && (m >> 3) & 7 == 6 && m >> 6 != 3 => Some((ConsoleOnly::Clwb, i + 2 + modrm_len(&code[i + 2..])?)),
// F3 0F C7 /7 with a register operand
(0xC7, Some(m)) if pf3 && (m >> 3) & 7 == 7 && m >> 6 == 3 => Some((ConsoleOnly::Rdpid((m & 7) | (rex & 1) << 3), i + 3)),
// SSE4a, register operands and two immediates (length, index): F2 0F 78 /r insertq,
// 66 0F 78 /0 extrq
(0x78, Some(m)) if m >> 6 == 3 && (pf2 || p66) => {
let (len, idx) = (*code.get(i + 3)? & 63, *code.get(i + 4)? & 63);
let (reg, rm) = (((m >> 3) & 7) | (rex & 4) << 1, (m & 7) | (rex & 1) << 3);
if pf2 {
Some((ConsoleOnly::Insertq { dst: reg, src: rm, len, idx }, i + 5))
} else if (m >> 3) & 7 == 0 {
Some((ConsoleOnly::Extrq { dst: rm, len, idx }, i + 5))
} else {
None
}
}
_ => None,
}
}
Expand Down Expand Up @@ -112,4 +152,33 @@ mod tests {
assert_eq!(decode(&[0x66, 0x0F, 0xAE, 0x74, 0x24]), None);
assert_eq!(decode(&[0x0F]), None);
}

#[test]
fn decodes_sse4a() {
// a retail game's: insertq xmm0, xmm1, 16, 16; insertq xmm9, xmm7, 8, 0 (REX.R);
// insertq xmm11, xmm12, 16, 16 (REX.R + REX.B)
assert_eq!(decode(&[0xF2, 0x0F, 0x78, 0xC1, 0x10, 0x10]), Some((ConsoleOnly::Insertq { dst: 0, src: 1, len: 16, idx: 16 }, 6)));
assert_eq!(decode(&[0xF2, 0x44, 0x0F, 0x78, 0xCF, 0x08, 0x00]), Some((ConsoleOnly::Insertq { dst: 9, src: 7, len: 8, idx: 0 }, 7)));
assert_eq!(decode(&[0xF2, 0x45, 0x0F, 0x78, 0xDC, 0x10, 0x10]), Some((ConsoleOnly::Insertq { dst: 11, src: 12, len: 16, idx: 16 }, 7)));
// extrq xmm2, 8, 4; extrq xmm10, 16, 32
assert_eq!(decode(&[0x66, 0x0F, 0x78, 0xC2, 0x08, 0x04]), Some((ConsoleOnly::Extrq { dst: 2, len: 8, idx: 4 }, 6)));
assert_eq!(decode(&[0x66, 0x41, 0x0F, 0x78, 0xC2, 0x10, 0x20]), Some((ConsoleOnly::Extrq { dst: 10, len: 16, idx: 32 }, 7)));
// not SSE4a: 0F 78 without a prefix (vmread), a memory operand, extrq with /1, cut short
assert_eq!(decode(&[0x0F, 0x78, 0xC1]), None);
assert_eq!(decode(&[0xF2, 0x0F, 0x78, 0x01, 0x10, 0x10]), None);
assert_eq!(decode(&[0x66, 0x0F, 0x78, 0xCA, 0x08, 0x04]), None);
assert_eq!(decode(&[0xF2, 0x0F, 0x78, 0xC1, 0x10]), None);
}

/// As AMD's manual defines them (APM vol. 4): the field of `len` bits at `idx`, 0 = 64 bits.
#[test]
fn sse4a_semantics() {
assert_eq!(insertq(u64::MAX, 0x1234, 16, 16), 0xFFFF_FFFF_1234_FFFF);
assert_eq!(insertq(0, 0xABCD_EF12, 8, 0), 0x12, "only the low len bits of the source");
assert_eq!(insertq(0x1111_1111_1111_1111, 0x2222_2222_2222_2222, 0, 0), 0x2222_2222_2222_2222, "length 0 is 64");
assert_eq!(insertq(0, 0x3, 2, 62), 0xC000_0000_0000_0000);
assert_eq!(extrq(0xFFFF_FFFF_1234_FFFF, 16, 16), 0x1234);
assert_eq!(extrq(0x8000_0000_0000_0001, 0, 0), 0x8000_0000_0000_0001, "length 0 is 64");
assert_eq!(extrq(0xF0, 4, 4), 0xF);
}
}