From: "Jan Beulich" Subject: [PATCH] x86emul: correct {, F}CMOV and F{, U}COMI{, P} emulation Date: Thu, 13 Oct 2016 00:41:15 -0600 Message-ID: <57FF48AB0200007800116EEC@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part7741D29B.1__=" Return-path: Received: from mail6.bemta3.messagelabs.com ([195.245.230.39]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1buZhh-00037N-Db for xen-devel@lists.xenproject.org; Thu, 13 Oct 2016 06:41:21 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel Cc: Andrew Cooper List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part7741D29B.1__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline The FPU ones need to be executed with guest EFLAGS.{C,P,Z}F in context. We also can't exclude someone wanting to hide the feature from (32-bit) guests. Signed-off-by: Jan Beulich --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -879,6 +879,24 @@ do { put_stub(stub); \ } while (0) =20 +#define emulate_fpu_insn_stub_eflags(bytes...) \ +do { \ + unsigned int nr_ =3D sizeof((uint8_t[]){ bytes }); = \ + struct fpu_insn_ctxt fic_ =3D { .insn_bytes =3D nr_ }; = \ + unsigned long tmp_; \ + memcpy(get_stub(stub), ((uint8_t[]){ bytes, 0xc3 }), nr_ + 1); \ + get_fpu(X86EMUL_FPU_fpu, &fic_); \ + asm volatile ( _PRE_EFLAGS("[eflags]", "[mask]", "[tmp]") \ + "call *%[func];" \ + _POST_EFLAGS("[eflags]", "[mask]", "[tmp]") \ + : [eflags] "+g" (_regs.eflags), \ + [tmp] "=3D&r" (tmp_) = \ + : [func] "rm" (stub.func), \ + [mask] "i" (EFLG_ZF|EFLG_PF|EFLG_CF) ); \ + put_fpu(&fic_); \ + put_stub(stub); \ +} while (0) + static unsigned long _get_rep_prefix( const struct cpu_user_regs *int_regs, int ad_bytes) @@ -1242,6 +1260,7 @@ static bool_t vcpu_has( #define vcpu_must_have(leaf, reg, bit) \ generate_exception_if(!vcpu_has(leaf, reg, bit, ctxt, ops), EXC_UD, = -1) #define vcpu_must_have_fpu() vcpu_must_have(0x00000001, EDX, 0) +#define vcpu_must_have_cmov() vcpu_must_have(0x00000001, EDX, 15) #define vcpu_must_have_mmx() vcpu_must_have(0x00000001, EDX, 23) #define vcpu_must_have_sse() vcpu_must_have(0x00000001, EDX, 25) #define vcpu_must_have_sse2() vcpu_must_have(0x00000001, EDX, 26) @@ -3573,6 +3592,9 @@ x86_emulate( case 0xc8 ... 0xcf: /* fcmove %stN */ case 0xd0 ... 0xd7: /* fcmovbe %stN */ case 0xd8 ... 0xdf: /* fcmovu %stN */ + vcpu_must_have_cmov(); + emulate_fpu_insn_stub_eflags(0xda, modrm); + break; case 0xe9: /* fucompp */ emulate_fpu_insn_stub(0xda, modrm); break; @@ -3621,7 +3643,10 @@ x86_emulate( case 0xc8 ... 0xcf: /* fcmovne %stN */ case 0xd0 ... 0xd7: /* fcmovnbe %stN */ case 0xd8 ... 0xdf: /* fcmovnu %stN */ - emulate_fpu_insn_stub(0xdb, modrm); + case 0xe8 ... 0xef: /* fucomi %stN */ + case 0xf0 ... 0xf7: /* fcomi %stN */ + vcpu_must_have_cmov(); + emulate_fpu_insn_stub_eflags(0xdb, modrm); break; case 0xe2: /* fnclex */ emulate_fpu_insn("fnclex"); @@ -3631,10 +3656,6 @@ x86_emulate( break; case 0xe4: /* fsetpm - 287 only, ignored by 387 */ break; - case 0xe8 ... 0xef: /* fucomi %stN */ - case 0xf0 ... 0xf7: /* fcomi %stN */ - emulate_fpu_insn_stub(0xdb, modrm); - break; default: fail_if(modrm >=3D 0xc0); switch ( modrm_reg & 7 ) @@ -3852,7 +3873,8 @@ x86_emulate( break; case 0xe8 ... 0xef: /* fucomip %stN */ case 0xf0 ... 0xf7: /* fcomip %stN */ - emulate_fpu_insn_stub(0xdf, modrm); + vcpu_must_have_cmov(); + emulate_fpu_insn_stub_eflags(0xdf, modrm); break; default: fail_if(modrm >=3D 0xc0); @@ -4760,6 +4782,7 @@ x86_emulate( } =20 case X86EMUL_OPC(0x0f, 0x40) ... X86EMUL_OPC(0x0f, 0x4f): /* cmovcc = */ + vcpu_must_have_cmov(); dst.val =3D src.val; if ( !test_cc(b, _regs.eflags) ) dst.type =3D OP_NONE; --=__Part7741D29B.1__= Content-Type: text/plain; name="x86emul-check-CPUID-cmov.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86emul-check-CPUID-cmov.patch" x86emul: correct {,F}CMOV and F{,U}COMI{,P} emulation=0A=0AThe FPU ones = need to be executed with guest EFLAGS.{C,P,Z}F in context.=0A=0AWe also = can't exclude someone wanting to hide the feature from (32-bit)=0Aguests.= =0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/xen/arch/x8= 6/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_emulate/x86_emulate.c= =0A@@ -879,6 +879,24 @@ do {=0A put_stub(stub); = \=0A } while (0)=0A =0A+#define emulate_fpu_in= sn_stub_eflags(bytes...) \=0A+do { = \=0A+ unsigned int = nr_ =3D sizeof((uint8_t[]){ bytes }); \=0A+ struct = fpu_insn_ctxt fic_ =3D { .insn_bytes =3D nr_ }; \=0A+ = unsigned long tmp_; \=0A+ = memcpy(get_stub(stub), ((uint8_t[]){ bytes, 0xc3 }), nr_ + 1); = \=0A+ get_fpu(X86EMUL_FPU_fpu, &fic_); = \=0A+ asm volatile ( _PRE_EFLAGS("[eflags]", "[mask]", "[tmp]") = \=0A+ "call *%[func];" = \=0A+ _POST_EFLAGS("[eflags]", "[mask]", "[tmp]") = \=0A+ : [eflags] "+g" (_regs.eflags), = \=0A+ [tmp] "=3D&r" (tmp_) = \=0A+ : [func] "rm" (stub.func), = \=0A+ [mask] "i" (EFLG_ZF|EFLG_PF|EFLG_= CF) ); \=0A+ put_fpu(&fic_); = \=0A+ put_stub(stub); = \=0A+} while (0)=0A+=0A static unsigned long = _get_rep_prefix(=0A const struct cpu_user_regs *int_regs,=0A int = ad_bytes)=0A@@ -1242,6 +1260,7 @@ static bool_t vcpu_has(=0A #define = vcpu_must_have(leaf, reg, bit) \=0A generate_exception_if(!vcpu_has(lea= f, reg, bit, ctxt, ops), EXC_UD, -1)=0A #define vcpu_must_have_fpu() = vcpu_must_have(0x00000001, EDX, 0)=0A+#define vcpu_must_have_cmov() = vcpu_must_have(0x00000001, EDX, 15)=0A #define vcpu_must_have_mmx() = vcpu_must_have(0x00000001, EDX, 23)=0A #define vcpu_must_have_sse() = vcpu_must_have(0x00000001, EDX, 25)=0A #define vcpu_must_have_sse2() = vcpu_must_have(0x00000001, EDX, 26)=0A@@ -3573,6 +3592,9 @@ x86_emulate(=0A= case 0xc8 ... 0xcf: /* fcmove %stN */=0A case 0xd0 ... = 0xd7: /* fcmovbe %stN */=0A case 0xd8 ... 0xdf: /* fcmovu %stN = */=0A+ vcpu_must_have_cmov();=0A+ emulate_fpu_insn_st= ub_eflags(0xda, modrm);=0A+ break;=0A case 0xe9: = /* fucompp */=0A emulate_fpu_insn_stub(0xda, modrm);=0A = break;=0A@@ -3621,7 +3643,10 @@ x86_emulate(=0A case 0xc8 = ... 0xcf: /* fcmovne %stN */=0A case 0xd0 ... 0xd7: /* fcmovnbe = %stN */=0A case 0xd8 ... 0xdf: /* fcmovnu %stN */=0A- = emulate_fpu_insn_stub(0xdb, modrm);=0A+ case 0xe8 ... 0xef: /* = fucomi %stN */=0A+ case 0xf0 ... 0xf7: /* fcomi %stN */=0A+ = vcpu_must_have_cmov();=0A+ emulate_fpu_insn_stub_eflags(0xdb,= modrm);=0A break;=0A case 0xe2: /* fnclex */=0A = emulate_fpu_insn("fnclex");=0A@@ -3631,10 +3656,6 @@ x86_emulate(=0A = break;=0A case 0xe4: /* fsetpm - 287 only, ignored by = 387 */=0A break;=0A- case 0xe8 ... 0xef: /* fucomi %stN = */=0A- case 0xf0 ... 0xf7: /* fcomi %stN */=0A- = emulate_fpu_insn_stub(0xdb, modrm);=0A- break;=0A = default:=0A fail_if(modrm >=3D 0xc0);=0A switch ( = modrm_reg & 7 )=0A@@ -3852,7 +3873,8 @@ x86_emulate(=0A = break;=0A case 0xe8 ... 0xef: /* fucomip %stN */=0A case = 0xf0 ... 0xf7: /* fcomip %stN */=0A- emulate_fpu_insn_stub(0xdf,= modrm);=0A+ vcpu_must_have_cmov();=0A+ emulate_fpu_i= nsn_stub_eflags(0xdf, modrm);=0A break;=0A default:=0A = fail_if(modrm >=3D 0xc0);=0A@@ -4760,6 +4782,7 @@ x86_emulate(= =0A }=0A =0A case X86EMUL_OPC(0x0f, 0x40) ... X86EMUL_OPC(0x0f, = 0x4f): /* cmovcc */=0A+ vcpu_must_have_cmov();=0A dst.val = =3D src.val;=0A if ( !test_cc(b, _regs.eflags) )=0A = dst.type =3D OP_NONE;=0A --=__Part7741D29B.1__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwczovL2xpc3RzLnhlbi5v cmcveGVuLWRldmVsCg== --=__Part7741D29B.1__=--