From: "Jan Beulich" Subject: [PATCH 1/2] x86/emulator: add feature checks for newer instructions Date: Wed, 16 Nov 2011 13:59:51 +0000 Message-ID: <4EC3CFE702000078000614F6@nat28.tlf.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartE3CC63C7.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: "xen-devel@lists.xensource.com" 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. --=__PartE3CC63C7.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Certain instructions were introduced only after the i686 or original x86-64 architecture, so we should not try to emulate them if the guest is not seeing the respective feature enabled (or, worse, if the underlying hardware doesn't support them). This affects fisttp, movnti, and cmpxchg16b. Also add a "run" target to the test code makefile, to simplify building and running. Signed-off-by: Jan Beulich --- a/tools/tests/x86_emulator/Makefile +++ b/tools/tests/x86_emulator/Makefile @@ -7,6 +7,9 @@ TARGET :=3D test_x86_emulator .PHONY: all all: $(TARGET) =20 +run: $(TARGET) + ./$(TARGET) + .PHONY: blowfish.h blowfish.h: rm -f blowfish.bin @@ -38,7 +41,7 @@ x86_emulate: [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xen/arch/x86/x86_emulate = . =20 x86_emulate.o: x86_emulate.c x86_emulate - $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include -c -o $@ $< + $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include -imacros = asm-x86/cpufeature.h -c -o $@ $< =20 test_x86_emulator.o: test_x86_emulator.c blowfish.h x86_emulate $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include -c -o $@ $< --- a/tools/tests/x86_emulator/x86_emulate.c +++ b/tools/tests/x86_emulator/x86_emulate.c @@ -1,7 +1,13 @@ +#include #include #include +#include #include #include =20 +typedef bool bool_t; + +#define BUG() abort() + #include "x86_emulate/x86_emulate.h" #include "x86_emulate/x86_emulate.c" --- a/xen/arch/x86/x86_emulate.c +++ b/xen/arch/x86/x86_emulate.c @@ -9,6 +9,7 @@ * Keir Fraser */ =20 +#include #include =20 /* Avoid namespace pollution. */ --- a/xen/arch/x86/x86_emulate/x86_emulate.c +++ b/xen/arch/x86/x86_emulate/x86_emulate.c @@ -932,6 +932,50 @@ static int ioport_access_check( return ops->inject_hw_exception(EXC_GP, 0, ctxt) ? : X86EMUL_EXCEPTION= ; } =20 +#define EAX 0 +#define ECX 1 +#define EDX 2 +#define EBX 3 + +static bool_t vcpu_has( + unsigned int eax, + unsigned int ecx, + unsigned int reg, + unsigned int bit, + struct x86_emulate_ctxt *ctxt, + const struct x86_emulate_ops *ops) +{ + unsigned int edx, ebx; + int rc =3D X86EMUL_OKAY; + + fail_if(!ops->cpuid); + rc =3D ops->cpuid(&eax, &ebx, &ecx, &edx, ctxt); + if ( rc =3D=3D X86EMUL_OKAY ) + { + switch ( reg ) + { + case EAX: reg =3D eax; break; + case EBX: reg =3D ebx; break; + case ECX: reg =3D ecx; break; + case EDX: reg =3D edx; break; + default: BUG(); + } + if ( !(reg & (1U << bit)) ) + rc =3D ~X86EMUL_OKAY; + } + + done: + return rc =3D=3D X86EMUL_OKAY; +} + +#define vcpu_must_have(leaf, subleaf, reg, feature) \ + generate_exception_if(!vcpu_has(leaf, subleaf, reg, \ + X86_FEATURE_##feature % 32, \ + ctxt, ops), EXC_UD, -1) +#define vcpu_must_have_sse2() vcpu_must_have(1, 0, EDX, XMM2) +#define vcpu_must_have_sse3() vcpu_must_have(1, 0, ECX, XMM3) +#define vcpu_must_have_cx16() vcpu_must_have(1, 0, ECX, CX16) + static int in_realmode( struct x86_emulate_ctxt *ctxt, @@ -2738,6 +2782,7 @@ x86_emulate( emulate_fpu_insn_memsrc("fildl", src.val); break; case 1: /* fisttp m32i */ + vcpu_must_have_sse3(); ea.bytes =3D 4; dst =3D ea; dst.type =3D OP_MEM; @@ -2846,6 +2891,7 @@ x86_emulate( emulate_fpu_insn_memsrc("fldl", src.val); break; case 1: /* fisttp m64i */ + vcpu_must_have_sse3(); ea.bytes =3D 8; dst =3D ea; dst.type =3D OP_MEM; @@ -2953,6 +2999,7 @@ x86_emulate( emulate_fpu_insn_memsrc("filds", src.val); break; case 1: /* fisttp m16i */ + vcpu_must_have_sse3(); ea.bytes =3D 2; dst =3D ea; dst.type =3D OP_MEM; @@ -4141,6 +4188,7 @@ x86_emulate( =20 case 0xc3: /* movnti */ /* Ignore the non-temporal hint for now. */ + vcpu_must_have_sse2(); generate_exception_if(dst.bytes <=3D 2, EXC_UD, -1); dst.val =3D src.val; break; @@ -4151,6 +4199,8 @@ x86_emulate( =20 generate_exception_if((modrm_reg & 7) !=3D 1, EXC_UD, -1); generate_exception_if(ea.type !=3D OP_MEM, EXC_UD, -1); + if ( op_bytes =3D=3D 8 ) + vcpu_must_have_cx16(); op_bytes *=3D 2; =20 /* Get actual old value. */ --=__PartE3CC63C7.0__= Content-Type: text/plain; name="x86-emul-feature-checks.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-emul-feature-checks.patch" x86/emulator: add feature checks for newer instructions=0A=0ACertain = instructions were introduced only after the i686 or original=0Ax86-64 = architecture, so we should not try to emulate them if the guest=0Ais not = seeing the respective feature enabled (or, worse, if the=0Aunderlying = hardware doesn't support them). This affects fisttp, movnti,=0Aand = cmpxchg16b.=0A=0AAlso add a "run" target to the test code makefile, to = simplify building=0Aand running.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- a/tools/tests/x86_emulator/Makefile=0A+++ b/tools/tests/= x86_emulator/Makefile=0A@@ -7,6 +7,9 @@ TARGET :=3D test_x86_emulator=0A = .PHONY: all=0A all: $(TARGET)=0A =0A+run: $(TARGET)=0A+ ./$(TARGET)=0A+=0A = .PHONY: blowfish.h=0A blowfish.h:=0A rm -f blowfish.bin=0A@@ -38,7 = +41,7 @@ x86_emulate:=0A [ -L x86_emulate ] || ln -sf $(XEN_ROOT)/xe= n/arch/x86/x86_emulate .=0A =0A x86_emulate.o: x86_emulate.c x86_emulate=0A= - $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include -c -o $@ $<=0A+ = $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include -imacros asm-x86/cpufeatu= re.h -c -o $@ $<=0A =0A test_x86_emulator.o: test_x86_emulator.c blowfish.h= x86_emulate=0A $(HOSTCC) $(HOSTCFLAGS) -I$(XEN_ROOT)/xen/include = -c -o $@ $<=0A--- a/tools/tests/x86_emulator/x86_emulate.c=0A+++ b/tools/te= sts/x86_emulator/x86_emulate.c=0A@@ -1,7 +1,13 @@=0A+#include = =0A #include =0A #include =0A+#include =0A = #include =0A #include =0A =0A+typedef bool = bool_t;=0A+=0A+#define BUG() abort()=0A+=0A #include "x86_emulate/x86_emula= te.h"=0A #include "x86_emulate/x86_emulate.c"=0A--- a/xen/arch/x86/x86_emul= ate.c=0A+++ b/xen/arch/x86/x86_emulate.c=0A@@ -9,6 +9,7 @@=0A * Keir = Fraser =0A */=0A =0A+#include =0A = #include =0A =0A /* Avoid namespace pollution. */=0A--- = a/xen/arch/x86/x86_emulate/x86_emulate.c=0A+++ b/xen/arch/x86/x86_emulate/x= 86_emulate.c=0A@@ -932,6 +932,50 @@ static int ioport_access_check(=0A = return ops->inject_hw_exception(EXC_GP, 0, ctxt) ? : X86EMUL_EXCEPTION;=0A = }=0A =0A+#define EAX 0=0A+#define ECX 1=0A+#define EDX 2=0A+#define EBX = 3=0A+=0A+static bool_t vcpu_has(=0A+ unsigned int eax,=0A+ unsigned = int ecx,=0A+ unsigned int reg,=0A+ unsigned int bit,=0A+ struct = x86_emulate_ctxt *ctxt,=0A+ const struct x86_emulate_ops *ops)=0A+{=0A+ = unsigned int edx, ebx;=0A+ int rc =3D X86EMUL_OKAY;=0A+=0A+ = fail_if(!ops->cpuid);=0A+ rc =3D ops->cpuid(&eax, &ebx, &ecx, &edx, = ctxt);=0A+ if ( rc =3D=3D X86EMUL_OKAY )=0A+ {=0A+ switch ( = reg )=0A+ {=0A+ case EAX: reg =3D eax; break;=0A+ = case EBX: reg =3D ebx; break;=0A+ case ECX: reg =3D ecx; break;=0A+ = case EDX: reg =3D edx; break;=0A+ default: BUG();=0A+ = }=0A+ if ( !(reg & (1U << bit)) )=0A+ rc =3D ~X86EMUL_OKA= Y;=0A+ }=0A+=0A+ done:=0A+ return rc =3D=3D X86EMUL_OKAY;=0A+}=0A+=0A= +#define vcpu_must_have(leaf, subleaf, reg, feature) \=0A+ generate_exce= ption_if(!vcpu_has(leaf, subleaf, reg, \=0A+ = X86_FEATURE_##feature % 32, \=0A+ = ctxt, ops), EXC_UD, -1)=0A+#define vcpu_must_have_sse2() vcpu_must_have(1, = 0, EDX, XMM2)=0A+#define vcpu_must_have_sse3() vcpu_must_have(1, 0, ECX, = XMM3)=0A+#define vcpu_must_have_cx16() vcpu_must_have(1, 0, ECX, = CX16)=0A+=0A static int=0A in_realmode(=0A struct x86_emulate_ctxt = *ctxt,=0A@@ -2738,6 +2782,7 @@ x86_emulate(=0A emulate_fpu_= insn_memsrc("fildl", src.val);=0A break;=0A = case 1: /* fisttp m32i */=0A+ vcpu_must_have_sse3();=0A = ea.bytes =3D 4;=0A dst =3D ea;=0A = dst.type =3D OP_MEM;=0A@@ -2846,6 +2891,7 @@ x86_emulate(=0A = emulate_fpu_insn_memsrc("fldl", src.val);=0A = break;=0A case 1: /* fisttp m64i */=0A+ = vcpu_must_have_sse3();=0A ea.bytes =3D 8;=0A = dst =3D ea;=0A dst.type =3D OP_MEM;=0A@@ -2953,6 +2999,7 = @@ x86_emulate(=0A emulate_fpu_insn_memsrc("filds", = src.val);=0A break;=0A case 1: /* fisttp m16i = */=0A+ vcpu_must_have_sse3();=0A ea.bytes = =3D 2;=0A dst =3D ea;=0A dst.type =3D = OP_MEM;=0A@@ -4141,6 +4188,7 @@ x86_emulate(=0A =0A case 0xc3: /* = movnti */=0A /* Ignore the non-temporal hint for now. */=0A+ = vcpu_must_have_sse2();=0A generate_exception_if(dst.bytes <=3D 2, = EXC_UD, -1);=0A dst.val =3D src.val;=0A break;=0A@@ = -4151,6 +4199,8 @@ x86_emulate(=0A =0A generate_exception_if((modrm= _reg & 7) !=3D 1, EXC_UD, -1);=0A generate_exception_if(ea.type = !=3D OP_MEM, EXC_UD, -1);=0A+ if ( op_bytes =3D=3D 8 )=0A+ = vcpu_must_have_cx16();=0A op_bytes *=3D 2;=0A =0A /* Get = actual old value. */=0A --=__PartE3CC63C7.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__PartE3CC63C7.0__=--