Skip to contentSkip to conversation

MMX/SSE data movement

PATCH Applied Avi Kivity 2 participants 7 messages Apr 9, 2012 – Apr 13, 2012

Subsystems (3)
lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover MMX/SSE data movement

This patchset implements MMX registers, SSE data alignment, and three instructions:

MOVQ (MMX)
MOVNTPS (SSE)
MOVDQA (SSE)

all used in accessing framebuffers from guests.

Avi Kivity (4):
KVM: x86 emulator: add support for vector alignment
KVM: x86 emulator: implement movntps
KVM: x86 emulator: MMX support
KVM: x86 emulator: implement MMX MOVQ (opcodes 0f 6f, 0f 7f)

Stefan Hajnoczi (1):
KVM: x86: emulate movdqa

 arch/x86/include/asm/kvm_emulate.h |    4 +-
 arch/x86/kvm/emulate.c             |  148 ++++++++++++++++++++++++++++++++----
 2 files changed, 138 insertions(+), 14 deletions(-)
Signature · 1 line
1.7.10
author
#1 lore raw

PATCH 1/5 KVM: x86 emulator: add support for vector alignment

x86 defines three classes of vector instructions: explicitly aligned (#GP(0) if unaligned, explicitly unaligned, and default (which depends on the encoding: AVX is unaligned, SSE is aligned).

Add support for marking an instruction as explicitly aligned or unaligned, and mark MOVDQU as unaligned.

---
 arch/x86/kvm/emulate.c |   30 +++++++++++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +29 -1
index 8375622..6302e5c 100644
@@ -142,6 +142,9 @@
 #define Src2FS      (OpFS << Src2Shift)
 #define Src2GS      (OpGS << Src2Shift)
 #define Src2Mask    (OpMask << Src2Shift)
+#define Aligned     ((u64)1 << 41)  /* Explicitly aligned (e.g. MOVDQA) */
+#define Unaligned   ((u64)1 << 42)  /* Explicitly unaligned (e.g. MOVDQU) */
+#define Avx         ((u64)1 << 43)  /* Advanced Vector Extensions */
 
 #define X2(x...) x, x
 #define X3(x...) X2(x), x
@@ -557,6 +560,29 @@ static void set_segment_selector(struct x86_emulate_ctxt *ctxt, u16 selector,
 	ctxt->ops->set_segment(ctxt, selector, &desc, base3, seg);
 }
 
+/*
+ * x86 defines three classes of vector instructions: explicitly
+ * aligned, explicitly unaligned, and the rest, which change behaviour
+ * depending on whether they're AVX encoded or not.
+ *
+ * Also included is CMPXCHG16B which is not a vector instruction, yet it is
+ * subject to the same check.
+ */
+static bool insn_aligned(struct x86_emulate_ctxt *ctxt, unsigned size)
+{
+	if (likely(size < 16))
+		return false;
+
+	if (ctxt->d & Aligned)
+		return true;
+	else if (ctxt->d & Unaligned)
+		return false;
+	else if (ctxt->d & Avx)
+		return false;
+	else
+		return true;
+}
+
 static int __linearize(struct x86_emulate_ctxt *ctxt,
 		     struct segmented_address addr,
 		     unsigned size, bool write, bool fetch,
@@ -621,6 +647,8 @@ static int __linearize(struct x86_emulate_ctxt *ctxt,
 	}
 	if (fetch ? ctxt->mode != X86EMUL_MODE_PROT64 : ctxt->ad_bytes != 8)
 		la &= (u32)-1;
+	if (insn_aligned(ctxt, size) && ((la & (size - 1)) != 0))
+		return emulate_gp(ctxt, 0);
 	*linear = la;
 	return X86EMUL_CONTINUE;
 bad:
@@ -3415,7 +3443,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 };
 
 static struct gprefix pfx_0f_6f_0f_7f = {
-	N, N, N, I(Sse, em_movdqu),
+	N, N, N, I(Sse | Unaligned, em_movdqu),
 };
 
 static struct opcode opcode_table[256] = {
Signature · 1 line
1.7.10
author
#2 lore raw

PATCH 2/5 KVM: x86: emulate movdqa

In reply to Avi Kivity

From: Stefan Hajnoczi <[email protected]>

An Ubuntu 9.10 Karmic Koala guest is unable to boot or install due to missing movdqa emulation:

kvm_exit: reason EXCEPTION_NMI rip 0x7fef3e025a7b info 7fef3e799000 80000b0e
kvm_page_fault: address 7fef3e799000 error_code f
kvm_emulate_insn: 0:7fef3e025a7b: 66 0f 7f 07 (prot64)

movdqa %xmm0,(%rdi)

[avi: mark it explicitly aligned]

---
 arch/x86/kvm/emulate.c |   10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
arch/x86/kvm/emulate.c +2 -8
index 6302e5c..b160fb1f 100644
@@ -2818,7 +2818,7 @@ static int em_rdpmc(struct x86_emulate_ctxt *ctxt)
 
 static int em_mov(struct x86_emulate_ctxt *ctxt)
 {
-	ctxt->dst.val = ctxt->src.val;
+	memcpy(ctxt->dst.valptr, ctxt->src.valptr, ctxt->op_bytes);
 	return X86EMUL_CONTINUE;
 }
 
@@ -2898,12 +2898,6 @@ static int em_mov_sreg_rm(struct x86_emulate_ctxt *ctxt)
 	return load_segment_descriptor(ctxt, sel, ctxt->modrm_reg);
 }
 
-static int em_movdqu(struct x86_emulate_ctxt *ctxt)
-{
-	memcpy(&ctxt->dst.vec_val, &ctxt->src.vec_val, ctxt->op_bytes);
-	return X86EMUL_CONTINUE;
-}
-
 static int em_invlpg(struct x86_emulate_ctxt *ctxt)
 {
 	int rc;
@@ -3443,7 +3437,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 };
 
 static struct gprefix pfx_0f_6f_0f_7f = {
-	N, N, N, I(Sse | Unaligned, em_movdqu),
+	N, I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
 };
 
 static struct opcode opcode_table[256] = {
Signature · 1 line
1.7.10
author
#3 lore raw

PATCH 3/5 KVM: x86 emulator: implement movntps

In reply to Avi Kivity

Used to write to framebuffers (by at least Icaros).

---
 arch/x86/kvm/emulate.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/x86/kvm/emulate.c +6 -1
index b160fb1f..fb39e0b 100644
@@ -3440,6 +3440,10 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 	N, I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
 };
 
+static struct gprefix pfx_vmovntpx = {
+	I(0, em_mov), N, N, N,
+};
+
 static struct opcode opcode_table[256] = {
 	/* 0x00 - 0x07 */
 	I6ALU(Lock, em_add),
@@ -3571,7 +3575,8 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 	IIP(ModRM | SrcMem | Priv | Op3264, em_cr_write, cr_write, check_cr_write),
 	IIP(ModRM | SrcMem | Priv | Op3264, em_dr_write, dr_write, check_dr_write),
 	N, N, N, N,
-	N, N, N, N, N, N, N, N,
+	N, N, N, GP(ModRM | DstMem | SrcReg | Sse | Mov | Aligned, &pfx_vmovntpx),
+	N, N, N, N,
 	/* 0x30 - 0x3F */
 	II(ImplicitOps | Priv, em_wrmsr, wrmsr),
 	IIP(ImplicitOps, em_rdtsc, rdtsc, check_rdtsc),
Signature · 1 line
1.7.10
author
#4 lore raw

PATCH 4/5 KVM: x86 emulator: MMX support

In reply to Avi Kivity

General support for the MMX instruction set. Special care is taken to trap pending x87 exceptions so that they are properly reflected to the guest.

---
 arch/x86/include/asm/kvm_emulate.h |    4 +-
 arch/x86/kvm/emulate.c             |  103 ++++++++++++++++++++++++++++++++++--
 2 files changed, 102 insertions(+), 5 deletions(-)
arch/x86/include/asm/kvm_emulate.h +3 -1
index c222e1a..1ac46c22 100644
@@ -200,7 +200,7 @@ typedef u32 __attribute__((vector_size(16))) sse128_t;
 
 /* Type, address-of, and value of an instruction's operand. */
 struct operand {
-	enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_NONE } type;
+	enum { OP_REG, OP_MEM, OP_IMM, OP_XMM, OP_MM, OP_NONE } type;
 	unsigned int bytes;
 	union {
 		unsigned long orig_val;
@@ -213,12 +213,14 @@ struct operand {
 			unsigned seg;
 		} mem;
 		unsigned xmm;
+		unsigned mm;
 	} addr;
 	union {
 		unsigned long val;
 		u64 val64;
 		char valptr[sizeof(unsigned long) + 2];
 		sse128_t vec_val;
+		u64 mm_val;
 	};
 };
 
arch/x86/kvm/emulate.c +99 -4

159 lines not on this page · raw message

Signature · 1 line
1.7.10
author
#5 lore raw

PATCH 5/5 KVM: x86 emulator: implement MMX MOVQ (opcodes 0f 6f, 0f 7f)

In reply to Avi Kivity

Needed by some framebuffer drivers. See

https://bugzilla.kernel.org/show_bug.cgi?id=42779

---
 arch/x86/kvm/emulate.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/emulate.c +1 -1
index 0011b4a..d5729a9 100644
@@ -3488,7 +3488,7 @@ static int check_perm_out(struct x86_emulate_ctxt *ctxt)
 };
 
 static struct gprefix pfx_0f_6f_0f_7f = {
-	N, I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
+	I(Mmx, em_mov), I(Sse | Aligned, em_mov), N, I(Sse | Unaligned, em_mov),
 };
 
 static struct gprefix pfx_vmovntpx = {
Signature · 1 line
1.7.10
#6 lore raw
In reply to Avi Kivity
Quoted 15 lines · Avi Kivity

This patchset implements MMX registers, SSE data alignment, and three instructions:

MOVQ (MMX)
MOVNTPS (SSE)
MOVDQA (SSE)

all used in accessing framebuffers from guests.

Avi Kivity (4):
KVM: x86 emulator: add support for vector alignment
KVM: x86 emulator: implement movntps
KVM: x86 emulator: MMX support
KVM: x86 emulator: implement MMX MOVQ (opcodes 0f 6f, 0f 7f)

Quoted 6 lines

Stefan Hajnoczi (1):
KVM: x86: emulate movdqa

 arch/x86/include/asm/kvm_emulate.h |    4 +-
 arch/x86/kvm/emulate.c             |  148 ++++++++++++++++++++++++++++++++----
 2 files changed, 138 insertions(+), 14 deletions(-)

Applied, thanks.