Skip to contentSkip to conversation

kvm: implement VEX prefix decoder, bextr/andn

PATCH In review Mason Lee Back 2 participants 2 messages Jun 26, 2018 – Jun 29, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm: implement VEX prefix decoder, bextr/andn

---
 arch/x86/include/asm/kvm_emulate.h | 17 ++++++
 arch/x86/kvm/emulate.c             | 96 +++++++++++++++++++++++++++---
 2 files changed, 106 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_emulate.h +17 -0
index 0f82cd91cd3c..7aa42ae90bf7 100644
@@ -327,6 +327,23 @@ struct x86_emulate_ctxt {
 	 */
 	bool rip_relative;
 	u8 rex_prefix;
+	/* vex */
+	struct {
+		u8 prefix;
+		union {
+			struct {
+				uint8_t m : 5;
+				uint8_t b : 1;
+				uint8_t x : 1;
+				uint8_t r : 1;
+				uint8_t p : 2;
+				uint8_t l : 1;
+				uint8_t v : 4;
+				uint8_t w : 1;
+			};
+			u16 value;
+		};
+	}vex;
 	u8 lock_prefix;
 	u8 rep_prefix;
 	/* bitmaps of registers in _regs[] that can be read */
arch/x86/kvm/emulate.c +89 -7

135 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#1 lore raw

Thanks, I have some comments on the decoding logic.

arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5257

Quoted 1 line · Mason Lee Back
arch/x86/kvm/emulate.c +1 -0
+	if (ctxt->b == 0xC4 || ctxt->b == 0xC5) {

This should be exactly the condition that you are removing below:

	if ((ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
	    (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0))

so that you don't need the "reinterpret as LES or LDS" todo. It should also be moved earlier, right after done_prefixes. The existing code starting at

	/* Opcode byts(s).  */

and ending just before

	ctxt->d = opcode.flags;

will become the "else" branch.

Otherwise looks at least... sane. :)

Paolo

Quoted 17 lines
arch/x86/kvm/emulate.c +12 -4
+		ctxt->vex.prefix = ctxt->b;
+		if (ctxt->b == 0xC4) {
+			ctxt->vex.value = insn_fetch(u16, ctxt);
+		} else {
+			ctxt->vex.value = insn_fetch(u8, ctxt) << 8;
+			ctxt->vex.r = ctxt->vex.w;
+			ctxt->vex.w = 1;
+			ctxt->vex.x = 1;
+			ctxt->vex.b = 1;
+			ctxt->vex.m = 1;
+		}
 
-	/* vex-prefix instructions are not implemented */
-	if (ctxt->opcode_len == 1 && (ctxt->b == 0xc5 || ctxt->b == 0xc4) &&
-	    (mode == X86EMUL_MODE_PROT64 || (ctxt->modrm & 0xc0) == 0xc0)) {
-		ctxt->d = NotImpl;
+		if (mode != X86EMUL_MODE_PROT64 && (!ctxt->vex.r || !ctxt->vex.x)) {

arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5280

Quoted 10 lines
arch/x86/kvm/emulate.c +10 -0
+			/* todo: reinterpret as LES (0xC4) or LDS (0xC5) instruction */
+			return EMULATION_FAILED;
+		}
+
+		ctxt->rex_prefix |= ctxt->vex.r ? 0 : (1 << 2); /* rex.r */
+		ctxt->rex_prefix |= ctxt->vex.x ? 0 : (1 << 1); /* rex.x */
+		ctxt->rex_prefix |= ctxt->vex.b ? 0 : (1 << 0); /* rex.b */
+		if (mode == X86EMUL_MODE_PROT64 && ctxt->vex.w) {
+			ctxt->op_bytes = 8;
+		}

Please set ctxt->rex_prefix too for consistency.

arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5285

Quoted 5 lines
arch/x86/kvm/emulate.c +5 -0
+
+		ctxt->b = insn_fetch(u8, ctxt);
+		switch (ctxt->vex.m) {
+		case 1:
+			opcode = twobyte_table[ctxt->b];

"ctxt->opcode_len = 2;" missing here.

arch/x86/kvm/emulate.c int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len) · line 5288

Quoted 3 lines
arch/x86/kvm/emulate.c +3 -0
+			break;
+		case 2:
+			opcode = opcode_map_0f_38[ctxt->b];

"ctxt->opcode_len = 3;" missing here.

Quoted 8 lines
arch/x86/kvm/emulate.c +7 -0
+			break;
+		case 3:
+			/* KVM doesn't support this */
+			return EMULATION_FAILED;
+		default:
+			return EMULATION_FAILED;
+		}
 	}