diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 1b3da3ba26b8..3a66741b6c8c 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -245,6 +245,7 @@ enum { REX_X = 2, REX_R = 4, REX_W = 8, + REX_M = 0x80, }; static void writeback_registers(struct x86_emulate_ctxt *ctxt) @@ -4849,6 +4850,18 @@ static int x86_decode_avx(struct x86_emulate_ctxt *ctxt, return rc; } +static inline bool rex2_invalid(struct x86_emulate_ctxt *ctxt) +{ + const struct x86_emulate_ops *ops = ctxt->ops; + u64 xcr = 0; + + return ctxt->rex_prefix == REX_PREFIX || + (ctxt->rex_prefix == REX2_PREFIX && !(ctxt->rex_bits & REX_M)) || + !(ops->get_cr(ctxt, 4) & X86_CR4_OSXSAVE) || + ops->get_xcr(ctxt, 0, &xcr) || + !(xcr & XFEATURE_MASK_APX); +} + int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int emulation_type) { int rc = X86EMUL_CONTINUE; @@ -4902,7 +4915,7 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int ctxt->op_bytes = def_op_bytes; ctxt->ad_bytes = def_ad_bytes; - /* Legacy prefixes. */ + /* Legacy and REX/REX2 prefixes. */ for (;;) { switch (ctxt->b = insn_fetch(u8, ctxt)) { case 0x66: /* operand-size override */ @@ -4945,9 +4958,23 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int case 0x40 ... 0x4f: /* REX */ if (mode != X86EMUL_MODE_PROT64) goto done_prefixes; + if (ctxt->rex_prefix == REX2_PREFIX) { + opcode = ud; + goto done_modrm; + } ctxt->rex_prefix = REX_PREFIX; ctxt->rex_bits = ctxt->b & 0xf; continue; + case 0xd5: /* REX2 */ + if (mode != X86EMUL_MODE_PROT64) + goto done_prefixes; + if (rex2_invalid(ctxt)) { + opcode = ud; + goto done_modrm; + } + ctxt->rex_prefix = REX2_PREFIX; + ctxt->rex_bits = insn_fetch(u8, ctxt); + continue; case 0xf0: /* LOCK */ ctxt->lock_prefix = 1; break; @@ -4970,6 +4997,9 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int if (ctxt->rex_bits & REX_W) ctxt->op_bytes = 8; + if (ctxt->rex_bits & REX_M) + goto decode_twobytes; + /* Opcode byte(s). */ if (ctxt->b == 0xc4 || ctxt->b == 0xc5) { /* VEX or LDS/LES */ @@ -4987,8 +5017,9 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int goto done; } else if (ctxt->b == 0x0f) { /* Two- or three-byte opcode */ - ctxt->opcode_len = 2; ctxt->b = insn_fetch(u8, ctxt); +decode_twobytes: + ctxt->opcode_len = 2; opcode = twobyte_table[ctxt->b]; /* 0F_38 opcode map */