KVM: x86: Add Intel Processor Trace cpuid emulation
authorChao Peng <chao.p.peng@linux.intel.com>
Wed, 24 Oct 2018 08:05:11 +0000 (16:05 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Fri, 21 Dec 2018 10:28:35 +0000 (11:28 +0100)
Expose Intel Processor Trace to guest only when
the PT works in Host-Guest mode.

Signed-off-by: Chao Peng <chao.p.peng@linux.intel.com>
Signed-off-by: Luwei Kang <luwei.kang@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/include/asm/kvm_host.h
arch/x86/kvm/cpuid.c
arch/x86/kvm/svm.c
arch/x86/kvm/vmx/vmx.c

index bca77c25a19a0ceb5d7d982ce1a6e8542d83c741..ffb8a853e0d426489f0a4120d3a4d3e2ec5cb808 100644 (file)
@@ -1105,6 +1105,7 @@ struct kvm_x86_ops {
        bool (*mpx_supported)(void);
        bool (*xsaves_supported)(void);
        bool (*umip_emulated)(void);
+       bool (*pt_supported)(void);
 
        int (*check_nested_events)(struct kvm_vcpu *vcpu, bool external_intr);
        void (*request_immediate_exit)(struct kvm_vcpu *vcpu);
index be5e64c61d8f73b42cbc2fcec10c6184297c6f2d..c731e87baef580ec5776bf67161d9e894ea1dbc2 100644 (file)
@@ -334,6 +334,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
        unsigned f_mpx = kvm_mpx_supported() ? F(MPX) : 0;
        unsigned f_xsaves = kvm_x86_ops->xsaves_supported() ? F(XSAVES) : 0;
        unsigned f_umip = kvm_x86_ops->umip_emulated() ? F(UMIP) : 0;
+       unsigned f_intel_pt = kvm_x86_ops->pt_supported() ? F(INTEL_PT) : 0;
 
        /* cpuid 1.edx */
        const u32 kvm_cpuid_1_edx_x86_features =
@@ -392,7 +393,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
                F(BMI2) | F(ERMS) | f_invpcid | F(RTM) | f_mpx | F(RDSEED) |
                F(ADX) | F(SMAP) | F(AVX512IFMA) | F(AVX512F) | F(AVX512PF) |
                F(AVX512ER) | F(AVX512CD) | F(CLFLUSHOPT) | F(CLWB) | F(AVX512DQ) |
-               F(SHA_NI) | F(AVX512BW) | F(AVX512VL);
+               F(SHA_NI) | F(AVX512BW) | F(AVX512VL) | f_intel_pt;
 
        /* cpuid 0xD.1.eax */
        const u32 kvm_cpuid_D_1_eax_x86_features =
@@ -423,7 +424,7 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
 
        switch (function) {
        case 0:
-               entry->eax = min(entry->eax, (u32)0xd);
+               entry->eax = min(entry->eax, (u32)(f_intel_pt ? 0x14 : 0xd));
                break;
        case 1:
                entry->edx &= kvm_cpuid_1_edx_x86_features;
@@ -600,6 +601,23 @@ static inline int __do_cpuid_ent(struct kvm_cpuid_entry2 *entry, u32 function,
                }
                break;
        }
+       /* Intel PT */
+       case 0x14: {
+               int t, times = entry->eax;
+
+               if (!f_intel_pt)
+                       break;
+
+               entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+               for (t = 1; t <= times; ++t) {
+                       if (*nent >= maxnent)
+                               goto out;
+                       do_cpuid_1_ent(&entry[t], function, t);
+                       entry[t].flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX;
+                       ++*nent;
+               }
+               break;
+       }
        case KVM_CPUID_SIGNATURE: {
                static const char signature[12] = "KVMKVMKVM\0\0";
                const u32 *sigptr = (const u32 *)signature;
index 1a64844dcdfb38781d92fd2b3097c20c7c7224a4..47a9a45a2f70da42c7c37f1725f05ef2c13abe74 100644 (file)
@@ -5947,6 +5947,11 @@ static bool svm_umip_emulated(void)
        return false;
 }
 
+static bool svm_pt_supported(void)
+{
+       return false;
+}
+
 static bool svm_has_wbinvd_exit(void)
 {
        return true;
@@ -7188,6 +7193,7 @@ static struct kvm_x86_ops svm_x86_ops __ro_after_init = {
        .mpx_supported = svm_mpx_supported,
        .xsaves_supported = svm_xsaves_supported,
        .umip_emulated = svm_umip_emulated,
+       .pt_supported = svm_pt_supported,
 
        .set_supported_cpuid = svm_set_supported_cpuid,
 
index 338977e6f5529c211411dd01145ce327e6185f97..f5b88b578bbee07f92431c933baafc30d27aeaff 100644 (file)
@@ -5886,6 +5886,11 @@ static bool vmx_has_emulated_msr(int index)
        }
 }
 
+static bool vmx_pt_supported(void)
+{
+       return pt_mode == PT_MODE_HOST_GUEST;
+}
+
 static void vmx_recover_nmi_blocking(struct vcpu_vmx *vmx)
 {
        u32 exit_intr_info;
@@ -7399,6 +7404,7 @@ static struct kvm_x86_ops vmx_x86_ops __ro_after_init = {
        .mpx_supported = vmx_mpx_supported,
        .xsaves_supported = vmx_xsaves_supported,
        .umip_emulated = vmx_umip_emulated,
+       .pt_supported = vmx_pt_supported,
 
        .request_immediate_exit = vmx_request_immediate_exit,