x86/emulator: add feature checks for newer instructions 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 := test_x86_emulator .PHONY: all all: $(TARGET) +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 . 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 $@ $< 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 +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 */ +#include #include /* 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; } +#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 = X86EMUL_OKAY; + + fail_if(!ops->cpuid); + rc = ops->cpuid(&eax, &ebx, &ecx, &edx, ctxt); + if ( rc == X86EMUL_OKAY ) + { + switch ( reg ) + { + case EAX: reg = eax; break; + case EBX: reg = ebx; break; + case ECX: reg = ecx; break; + case EDX: reg = edx; break; + default: BUG(); + } + if ( !(reg & (1U << bit)) ) + rc = ~X86EMUL_OKAY; + } + + done: + return rc == 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 = 4; dst = ea; dst.type = 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 = 8; dst = ea; dst.type = 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 = 2; dst = ea; dst.type = OP_MEM; @@ -4141,6 +4188,7 @@ x86_emulate( case 0xc3: /* movnti */ /* Ignore the non-temporal hint for now. */ + vcpu_must_have_sse2(); generate_exception_if(dst.bytes <= 2, EXC_UD, -1); dst.val = src.val; break; @@ -4151,6 +4199,8 @@ x86_emulate( generate_exception_if((modrm_reg & 7) != 1, EXC_UD, -1); generate_exception_if(ea.type != OP_MEM, EXC_UD, -1); + if ( op_bytes == 8 ) + vcpu_must_have_cx16(); op_bytes *= 2; /* Get actual old value. */