Pass through the LBR stack to the guest when the guest lbr feature is
enabled. This makes the guest have direct accesses to the lbr stack.

Signed-off-by: Like Xu <like...@intel.com>
Signed-off-by: Wei Wang <wei.w.w...@intel.com>
Cc: Paolo Bonzini <pbonz...@redhat.com>
Cc: Andi Kleen <a...@linux.intel.com>
---
 arch/x86/kvm/vmx.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 1d26f3c..7a62c1c 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -7847,6 +7847,38 @@ static void vmx_enable_tdp(void)
        kvm_enable_tdp();
 }
 
+static int vmx_passthrough_lbr_msrs(struct kvm *kvm,
+                                    unsigned long *msr_bitmap)
+{
+       int i;
+       struct perf_lbr_stack lbr_stack;
+
+       if (perf_get_lbr_stack(&lbr_stack) < 0) {
+               pr_err("Failed to pass through the lbr stack\n");
+               return -ENOENT;
+       }
+
+       vmx_disable_intercept_for_msr(msr_bitmap, MSR_LBR_SELECT,
+                                     MSR_TYPE_RW);
+       vmx_disable_intercept_for_msr(msr_bitmap, lbr_stack.lbr_tos,
+                                     MSR_TYPE_RW);
+
+       for (i = 0; i < lbr_stack.lbr_nr; i++) {
+               vmx_disable_intercept_for_msr(msr_bitmap,
+                                             lbr_stack.lbr_from + i,
+                                             MSR_TYPE_RW);
+               vmx_disable_intercept_for_msr(msr_bitmap,
+                                             lbr_stack.lbr_to + i,
+                                             MSR_TYPE_RW);
+               if (lbr_stack.lbr_info)
+                       vmx_disable_intercept_for_msr(msr_bitmap,
+                                                     lbr_stack.lbr_info + i,
+                                                     MSR_TYPE_RW);
+       }
+
+       return 0;
+}
+
 static __init int hardware_setup(void)
 {
        unsigned long host_bndcfgs;
@@ -10998,6 +11030,12 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm 
*kvm, unsigned int id)
        vmx_disable_intercept_for_msr(msr_bitmap, MSR_IA32_SYSENTER_EIP, 
MSR_TYPE_RW);
        vmx->msr_bitmap_mode = 0;
 
+       if (kvm->arch.guest_lbr) {
+               err = vmx_passthrough_lbr_msrs(kvm, msr_bitmap);
+               if (err < 0)
+                       goto free_vmcs;
+       }
+
        vmx->loaded_vmcs = &vmx->vmcs01;
        cpu = get_cpu();
        vmx_vcpu_load(&vmx->vcpu, cpu);
-- 
2.7.4

Reply via email to