From: Mason Lee Back <masonleeback@gmail.com>
To: pbonzini@redhat.com
Cc: "Mason Lee Back" <masonleeback@gmail.com>,
"Radim Krčmář" <rkrcmar@redhat.com>,
"Thomas Gleixner" <tglx@linutronix.de>,
"Ingo Molnar" <mingo@redhat.com>,
"H. Peter Anvin" <hpa@zytor.com>,
x86@kernel.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: [PATCH] kvm: implement VEX prefix decoder, bextr/andn
Date: Tue, 26 Jun 2018 06:14:49 -0600 [thread overview]
Message-ID: <20180626121449.30353-1-masonleeback@gmail.com> (raw)
---
arch/x86/include/asm/kvm_emulate.h | 17 ++++++
arch/x86/kvm/emulate.c | 96 +++++++++++++++++++++++++++---
2 files changed, 106 insertions(+), 7 deletions(-)
diff --git a/arch/x86/include/asm/kvm_emulate.h b/arch/x86/include/asm/kvm_emulate.h
index 0f82cd91cd3c..7aa42ae90bf7 100644
--- a/arch/x86/include/asm/kvm_emulate.h
+++ b/arch/x86/include/asm/kvm_emulate.h
@@ -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 */
diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index 4c4f4263420c..2cca6d61a12f 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -66,6 +66,7 @@
#define OpXLat 28ull /* memory at BX/EBX/RBX + zero-extended AL */
#define OpAccLo 29ull /* Low part of extended acc (AX/AX/EAX/RAX) */
#define OpAccHi 30ull /* High part of extended acc (-/DX/EDX/RDX) */
+#define OpVex 31ull /* Vector extensions */
#define OpBits 5 /* Width of operand field */
#define OpMask ((1ull << OpBits) - 1)
@@ -117,6 +118,7 @@
#define SrcMem8 (OpMem8 << SrcShift)
#define SrcAccHi (OpAccHi << SrcShift)
#define SrcMask (OpMask << SrcShift)
+#define SrcVex (OpVex << SrcShift)
#define BitOp (1<<11)
#define MemAbs (1<<12) /* Memory operand is absolute displacement */
#define String (1<<13) /* String instruction (rep capable) */
@@ -160,6 +162,7 @@
#define Src2FS (OpFS << Src2Shift)
#define Src2GS (OpGS << Src2Shift)
#define Src2Mask (OpMask << Src2Shift)
+#define Src2Vex (OpVex << Src2Shift)
#define Mmx ((u64)1 << 40) /* MMX Vector instruction */
#define AlignMask ((u64)7 << 41)
#define Aligned ((u64)1 << 41) /* Explicitly aligned (e.g. MOVDQA) */
@@ -4118,6 +4121,46 @@ static int em_fxrstor(struct x86_emulate_ctxt *ctxt)
return rc;
}
+static int em_andn(struct x86_emulate_ctxt *ctxt)
+{
+ u64 temp;
+ int signbit = 63;
+
+ temp = ~ctxt->src.val & ctxt->src2.val;
+ if (ctxt->op_bytes == 4) {
+ temp &= 0xFFFFFFFF;
+ signbit = 31;
+ }
+ ctxt->dst.val = temp;
+ ctxt->eflags &= ~EFLAGS_MASK;
+ ctxt->eflags |= (temp == 0) ? X86_EFLAGS_ZF : 0;
+ ctxt->eflags |= (temp >> signbit) ? X86_EFLAGS_SF : 0;
+
+ return X86EMUL_CONTINUE;
+}
+
+static int em_bextr(struct x86_emulate_ctxt *ctxt)
+{
+ u8 start, len;
+ u64 temp;
+
+ start = ctxt->src2.val & 0xFF;
+ len = (ctxt->src2.val >> 8) & 0xFF;
+ temp = ctxt->src.val;
+ if (ctxt->op_bytes == 4)
+ {
+ temp &= 0xFFFFFFFF;
+ }
+
+ temp >>= start;
+ temp &= ~(~0ULL << len);
+ ctxt->dst.val = temp;
+ ctxt->eflags &= ~EFLAGS_MASK;
+ ctxt->eflags |= (temp == 0) ? X86_EFLAGS_ZF : 0;
+
+ return X86EMUL_CONTINUE;
+}
+
static bool valid_cr(int nr)
{
switch (nr) {
@@ -4849,7 +4892,10 @@ static const struct opcode opcode_map_0f_38[256] = {
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f0),
GP(EmulateOnUD | ModRM, &three_byte_0f_38_f1),
/* 0xf2 - 0xff */
- N, N, X4(N), X8(N)
+ I(ModRM | DstReg | SrcVex | Src2Mem, em_andn),
+ X4(N),
+ I(ModRM | DstReg | SrcMem | Src2Vex, em_bextr),
+ X8(N)
};
#undef D
@@ -5208,15 +5254,51 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len)
}
ctxt->d = opcode.flags;
- if (ctxt->d & ModRM)
- ctxt->modrm = insn_fetch(u8, ctxt);
+ if (ctxt->b == 0xC4 || ctxt->b == 0xC5) {
+ 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)) {
+ /* 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;
+ }
+
+ ctxt->b = insn_fetch(u8, ctxt);
+ switch (ctxt->vex.m) {
+ case 1:
+ opcode = twobyte_table[ctxt->b];
+ break;
+ case 2:
+ opcode = opcode_map_0f_38[ctxt->b];
+ break;
+ case 3:
+ /* KVM doesn't support this */
+ return EMULATION_FAILED;
+ default:
+ return EMULATION_FAILED;
+ }
}
+ ctxt->d = opcode.flags;
+ if (ctxt->d & ModRM)
+ ctxt->modrm = insn_fetch(u8, ctxt);
+
while (ctxt->d & GroupMask) {
switch (ctxt->d & GroupMask) {
case Group:
--
2.17.1
next reply other threads:[~2018-06-26 16:15 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-26 12:14 Mason Lee Back [this message]
2018-06-29 11:47 ` [PATCH] kvm: implement VEX prefix decoder, bextr/andn Paolo Bonzini
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180626121449.30353-1-masonleeback@gmail.com \
--to=masonleeback@gmail.com \
--cc=hpa@zytor.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=pbonzini@redhat.com \
--cc=rkrcmar@redhat.com \
--cc=tglx@linutronix.de \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).