x86emul: support RDPID
authorJan Beulich <jbeulich@suse.com>
Thu, 19 Jan 2017 09:33:29 +0000 (10:33 +0100)
committerJan Beulich <jbeulich@suse.com>
Thu, 19 Jan 2017 09:33:29 +0000 (10:33 +0100)
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
tools/tests/x86_emulator/test_x86_emulator.c
tools/tests/x86_emulator/x86_emulate.c
xen/arch/x86/x86_emulate/x86_emulate.c
xen/include/public/arch-x86/cpufeatureset.h

index 5abecf4ba3dcfc935a62b2275919dad37d6b9780..245d1c69d666d60dbb7b5db7dd595630e0de2034 100644 (file)
@@ -158,6 +158,11 @@ static int read_msr(
     case 0xc0000080: /* EFER */
         *val = ctxt->addr_size > 32 ? 0x500 /* LME|LMA */ : 0;
         return X86EMUL_OKAY;
+
+    case 0xc0000103: /* TSC_AUX */
+#define TSC_AUX_VALUE 0xCACACACA
+        *val = TSC_AUX_VALUE;
+        return X86EMUL_OKAY;
     }
 
     return X86EMUL_UNHANDLEABLE;
@@ -1472,6 +1477,16 @@ int main(int argc, char **argv)
     else
         printf("skipped\n");
 
+    printf("%-40s", "Testing rdpid %ecx...");
+    instr[0] = 0xF3; instr[1] = 0x0f; instr[2] = 0xC7; instr[3] = 0xf9;
+    regs.eip = (unsigned long)&instr[0];
+    rc = x86_emulate(&ctxt, &emulops);
+    if ( (rc != X86EMUL_OKAY) ||
+         (regs.ecx != TSC_AUX_VALUE) ||
+         (regs.eip != (unsigned long)&instr[4]) )
+        goto fail;
+    printf("okay\n");
+
     printf("%-40s", "Testing movq %mm3,(%ecx)...");
     if ( stack_exec && cpu_has_mmx )
     {
index 73411f863d41a67d27e4aadfa55868261f63ffe3..615326259bca13d407376c252aa6b0c613f4ebef 100644 (file)
@@ -60,9 +60,15 @@ int emul_test_cpuid(
     if ( leaf == 1 )
         res->c |= 1U << 22;
 
-    /* The emulator doesn't itself use ADCX/ADOX, so we can always run the test. */
+    /*
+     * The emulator doesn't itself use ADCX/ADOX/RDPID, so we can always run
+     * the respective tests.
+     */
     if ( leaf == 7 && subleaf == 0 )
+    {
         res->b |= 1U << 19;
+        res->c |= 1U << 22;
+    }
 
     return X86EMUL_OKAY;
 }
index 4f5383720557987215734a27676112e1073e80c9..6533d060014e1775ef9c5158fc87d5096baaf4fd 100644 (file)
@@ -1367,6 +1367,7 @@ static bool vcpu_has(
 #define vcpu_has_smap()        vcpu_has(         7, EBX, 20, ctxt, ops)
 #define vcpu_has_clflushopt()  vcpu_has(         7, EBX, 23, ctxt, ops)
 #define vcpu_has_clwb()        vcpu_has(         7, EBX, 24, ctxt, ops)
+#define vcpu_has_rdpid()       vcpu_has(         7, ECX, 22, ctxt, ops)
 
 #define vcpu_must_have(feat) \
     generate_exception_if(!vcpu_has_##feat(), EXC_UD)
@@ -5800,8 +5801,23 @@ x86_emulate(
                 break;
 #endif
 
+            case 7: /* rdseed / rdpid */
+                if ( repe_prefix() ) /* rdpid */
+                {
+                    uint64_t tsc_aux;
+
+                    generate_exception_if(ea.type != OP_REG, EXC_UD);
+                    vcpu_must_have(rdpid);
+                    fail_if(!ops->read_msr);
+                    if ( (rc = ops->read_msr(MSR_TSC_AUX, &tsc_aux,
+                                             ctxt)) != X86EMUL_OKAY )
+                        goto done;
+                    dst = ea;
+                    dst.val = tsc_aux;
+                    dst.bytes = 4;
+                    break;
+                }
 #ifdef HAVE_GAS_RDSEED
-            case 7: /* rdseed */
                 generate_exception_if(rep_prefix(), EXC_UD);
                 host_and_vcpu_must_have(rdseed);
                 dst = ea;
index c9b38e5a5742e58c57edc76db26bbfbfc79c9489..306200b8c24b6f8cb3af9e30156ebbe45536763a 100644 (file)
@@ -227,6 +227,7 @@ XEN_CPUFEATURE(AVX512VBMI,    6*32+ 1) /*A  AVX-512 Vector Byte Manipulation Ins
 XEN_CPUFEATURE(PKU,           6*32+ 3) /*H  Protection Keys for Userspace */
 XEN_CPUFEATURE(OSPKE,         6*32+ 4) /*!  OS Protection Keys Enable */
 XEN_CPUFEATURE(AVX512_VPOPCNTDQ, 6*32+14) /*A  POPCNT for vectors of DW/QW */
+XEN_CPUFEATURE(RDPID,         6*32+22) /*A  RDPID instruction */
 
 /* AMD-defined CPU features, CPUID level 0x80000007.edx, word 7 */
 XEN_CPUFEATURE(ITSC,          7*32+ 8) /*   Invariant TSC */