diff --git a/crates/xcl/src/eh.rs b/crates/xcl/src/eh.rs index 7db83c2..e26b345 100644 --- a/crates/xcl/src/eh.rs +++ b/crates/xcl/src/eh.rs @@ -300,7 +300,8 @@ unsafe fn emulate_console_only(ctx: &mut windows_sys::Win32::System::Diagnostics ]; *regs[r as usize & 15] = cpu; } - // the low quadword changes; the high one is undefined on the console and kept here + // the low quadword changes; the high one, undefined in AMD's manual, is zeroed – what a + // Zen 5 does in all of 32 779 cases compared (Kalmarv on #209) ConsoleOnly::Extrq { dst, src, len, idx } => { let x = &mut ctx.Anonymous.FltSave.XmmRegisters; let (len, idx) = match src { @@ -312,6 +313,7 @@ unsafe fn emulate_console_only(ctx: &mut windows_sys::Win32::System::Diagnostics }; let d = &mut x[dst as usize & 15]; d.Low = crate::insn::extrq(d.Low, len, idx); + d.High = 0; } ConsoleOnly::Insertq { dst, src, reg, len, idx } => { let x = &mut ctx.Anonymous.FltSave.XmmRegisters; @@ -319,6 +321,7 @@ unsafe fn emulate_console_only(ctx: &mut windows_sys::Win32::System::Diagnostics let (len, idx) = if reg { (s.High as u8, (s.High >> 8) as u8) } else { (len, idx) }; let d = &mut x[dst as usize & 15]; d.Low = crate::insn::insertq(d.Low, s.Low, len, idx); + d.High = 0; } } ctx.Rip += len as u64; diff --git a/crates/xcl/src/insn.rs b/crates/xcl/src/insn.rs index 94bb87a..3f41ee5 100644 --- a/crates/xcl/src/insn.rs +++ b/crates/xcl/src/insn.rs @@ -21,7 +21,7 @@ pub enum ConsoleOnly { Rdpid(u8), /// SSE4a `extrq xmm, imm8 (length), imm8 (index)` / `extrq xmm1, xmm2` (`src` Some: the /// length and index in xmm2's bits 5:0 and 13:8): a bit field of the low quadword, moved - /// down and zero-extended. Registers are XMM numbers 0..15. + /// down and zero-extended (the high quadword zeroed). Registers are XMM numbers 0..15. Extrq { dst: u8, src: Option, len: u8, idx: u8 }, /// SSE4a `insertq xmm1, xmm2, imm8 (length), imm8 (index)` / `insertq xmm1, xmm2` (`reg`: /// the length and index in xmm2's bits 69:64 and 77:72): the low bits of xmm2 put into a @@ -85,20 +85,26 @@ fn modrm_len(b: &[u8]) -> Option { /// 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 pf2, mut pf3, mut rex) = (false, false, false, 0u8); + // `rep`: the last of F2 / F3, which decides between them (and over 66) for SSE4a + let (mut p66, mut pf3, mut rep, mut rex) = (false, false, 0u8, 0u8); while let Some(&b) = code.get(i) { match b { 0x66 => p66 = true, - 0xF2 => pf2 = true, - 0xF3 => pf3 = true, - 0x2E | 0x3E | 0x26 | 0x36 | 0x64 | 0x65 => {} + 0xF2 => rep = 0xF2, + 0xF3 => { + pf3 = true; + rep = 0xF3; + } + 0x2E | 0x3E | 0x26 | 0x36 | 0x64 | 0x65 | 0x67 => {} + // REX counts only right before the opcode: one before a legacy prefix is ignored 0x40..=0x4F => { rex = b; i += 1; - break; // REX is the last prefix + continue; } _ => break, } + rex = 0; i += 1; } if *code.get(i)? != 0x0F { @@ -111,11 +117,12 @@ pub fn decode(code: &[u8]) -> Option<(ConsoleOnly, usize)> { (0x01, Some(0xFB)) => Some((ConsoleOnly::Mwaitx, i + 3)), (0x0B, _) => Some((ConsoleOnly::Ud2, i + 2)), // SSE4a: register operands only (ModRM mod 3); REX.R extends reg, REX.B rm - (0x78 | 0x79, Some(m)) if (p66 || pf2) && m >> 6 == 3 => { + // (the last of F2 / F3 decides: F2 INSERTQ, F3 #UD; neither with 66: EXTRQ) + (0x78 | 0x79, Some(m)) if (rep == 0xF2 || (rep == 0 && p66)) && m >> 6 == 3 => { let reg = ((m >> 3) & 7) | (rex & 4) << 1; let rm = (m & 7) | (rex & 1) << 3; let imm = |k: usize| code.get(i + 3 + k).copied(); - match (code[i + 1], pf2) { + match (code[i + 1], rep == 0xF2) { // extrq xmm(rm), imm8, imm8 – the ModRM reg field is /0 (0x78, false) if (m >> 3) & 7 == 0 => Some((ConsoleOnly::Extrq { dst: rm, src: None, len: imm(0)?, idx: imm(1)? }, i + 5)), (0x79, false) => Some((ConsoleOnly::Extrq { dst: reg, src: Some(rm), len: 0, idx: 0 }, i + 3)), @@ -179,6 +186,16 @@ mod tests { assert_eq!(decode(&[0x66, 0x0F, 0x79, 0xCA]), Some((ConsoleOnly::Extrq { dst: 1, src: Some(2), len: 0, idx: 0 }, 4))); assert_eq!(decode(&[0xF2, 0x0F, 0x78, 0xC1, 0x08]), None, "an immediate missing"); assert_eq!(decode(&[0x0F, 0x78, 0xC1, 0x08, 0x10]), None, "VMREAD without a prefix is not SSE4a"); + // prefixes as a Zen 5 runs them (Kalmarv on #209): the last of F2 / F3 decides, 66 next + // to them does not; 0x67 and a REX before a legacy prefix are ignored + let insertq = Some((ConsoleOnly::Insertq { dst: 0, src: 1, reg: false, len: 16, idx: 16 }, 7)); + for code in [[0x66, 0xF2, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0xF2, 0x66, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0xF3, 0xF2, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0x67, 0xF2, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0x41, 0xF2, 0x0F, 0x78, 0xC1, 0x10, 0x10]] { + assert_eq!(decode(&code), insertq, "{code:02x?}"); + } + for code in [[0xF2, 0xF3, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0xF3, 0x66, 0x0F, 0x78, 0xC1, 0x10, 0x10], [0x66, 0xF3, 0x0F, 0x78, 0xC0, 0x10, 0x10]] { + assert_eq!(decode(&code), None, "#UD on the CPU: {code:02x?}"); + } + assert_eq!(decode(&[0x67, 0x0F, 0x01, 0xFC]), Some((ConsoleOnly::Clzero, 4))); assert_eq!(decode(&[0x66, 0x0F, 0xAE, 0x38]), None); assert_eq!(decode(&[0x0F, 0xAE, 0x30]), None); assert_eq!(decode(&[0x48, 0x8B, 0x01]), None);