Add support for IBS virtualization (VIBS). VIBS feature allows the guest to collect IBS samples without exiting the guest. There are 2 parts to it [1]. - Virtualizing the IBS register state. - Ensuring the IBS interrupt is handled in the guest without exiting the hypervisor. VIBS requires the use of AVIC or NMI virtualization for delivery of a virtualized interrupt from IBS hardware in the guest [1]. While IBS collects data for IBS fetch/op block for the sampled interval, VIBS hardware signals a VNMI, but the source of VNMI is different in both AVIC enabled/disabled case. - When AVIC is disabled, virtual NMI is HW accelerated. - When AVIC is enabled, virtual NMI is accelerated via AVIC using Extended LVT. The local interrupts are extended to include more LVT registers, to allow additional interrupt sources, like instruction based sampling etc. [3]. Note that, since IBS registers are swap type C [2], the hypervisor is responsible for saving and restoring of IBS host state. Hypervisor does so only when IBS is active on the host to avoid unnecessary rdmsrs/wrmsrs. Hypervisor needs to disable host IBS before saving the state and enter the guest. After a guest exit, the hypervisor needs to restore host IBS state and re-enable IBS. [1]: https://bugzilla.kernel.org/attachment.cgi?id=304653 AMD64 Architecture Programmer’s Manual, Vol 2, Section 15.38 Instruction-Based Sampling Virtualization. [2]: https://bugzilla.kernel.org/attachment.cgi?id=304653 AMD64 Architecture Programmer’s Manual, Vol 2, Appendix B Layout of VMCB, Table B-3 Swap Types. [3]: https://bugzilla.kernel.org/attachment.cgi?id=304653 AMD64 Architecture Programmer’s Manual, Vol 2, Section 16.4.5 Extended Interrupts. Testing done: - Following tests were executed on guest sudo perf record -e ibs_op// -c 100000 -a sudo perf record -e ibs_op// -c 100000 -C 10 sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -a sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -a --raw-samples sudo perf record -e ibs_op/cnt_ctl=1,l3missonly=1/ -c 100000 -a sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -p 1234 sudo perf record -e ibs_op/cnt_ctl=1/ -c 100000 -- ls sudo ./tools/perf/perf record -e ibs_op// -e ibs_fetch// -a --raw-samples -c 100000 sudo perf report sudo perf script sudo perf report -D | grep -P "LdOp 1.*StOp 0" | wc -l sudo perf report -D | grep -P "LdOp 1.*StOp 0.*DcMiss 1" | wc -l sudo perf report -D | grep -P "LdOp 1.*StOp 0.*DcMiss 1.*L2Miss 1" | wc -l sudo perf report -D | grep -B1 -P "LdOp 1.*StOp 0.*DcMiss 1.*L2Miss 1" | grep -P "DataSrc ([02-9]|1[0-2])=" | wc -l - Following Nested guests combinations were tested manually ---------------------------- | Collected IBS Samples in | ---------------------------- | L0 | L1 | L2 | ---------------------------- | Y | Y | Y | | Y | Y | N | | Y | N | Y | | Y | N | N | | N | Y | Y | | N | Y | N | | N | N | Y | ---------------------------- Qemu changes can be found at below location: https://github.com/Kullu14/qemu/tree/qemu_vibs_branch Qemu commandline to enable IBS virtualization: qemu-system-x86_64 -enable-kvm -cpu EPYC-Genoa,+svm,+ibs,+extapic,+extlvt,+vibs \ .. base-commit: 3b2ac85b3d2954b583dc2039825ad76eda8516a9 (kvm_x86 misc) On top of base commit, https://lore.kernel.org/all/20230717041903.85480-1-manali.shukla@xxxxxxx patch is applied, then VIBS patch series is applied Manali Shukla (8): KVM: Add KVM_GET_LAPIC_W_EXTAPIC and KVM_SET_LAPIC_W_EXTAPIC for extapic KVM: x86/cpuid: Add a KVM-only leaf for IBS capabilities KVM: x86: Extend CPUID range to include new leaf perf/x86/amd: Add framework to save/restore host IBS state x86/cpufeatures: Add CPUID feature bit for VIBS in SEV-ES guest KVM: SVM: Add support for IBS virtualization for SEV-ES guests KVM: SVM: Enable IBS virtualization on non SEV-ES and SEV-ES guests KVM: x86: nSVM: Implement support for nested IBS virtualization Santosh Shukla (5): x86/cpufeatures: Add CPUID feature bit for Extended LVT KVM: x86: Add emulation support for Extented LVT registers x86/cpufeatures: Add CPUID feature bit for virtualized IBS KVM: SVM: Extend VMCB area for virtualized IBS registers KVM: SVM: add support for IBS virtualization for non SEV-ES guests Documentation/virt/kvm/api.rst | 23 ++++ arch/x86/events/amd/Makefile | 2 +- arch/x86/events/amd/ibs.c | 23 ++++ arch/x86/events/amd/vibs.c | 101 ++++++++++++++ arch/x86/include/asm/apicdef.h | 14 ++ arch/x86/include/asm/cpufeatures.h | 3 + arch/x86/include/asm/perf_event.h | 27 ++++ arch/x86/include/asm/svm.h | 34 ++++- arch/x86/include/uapi/asm/kvm.h | 5 + arch/x86/kvm/cpuid.c | 11 ++ arch/x86/kvm/governed_features.h | 1 + arch/x86/kvm/lapic.c | 78 ++++++++++- arch/x86/kvm/lapic.h | 7 +- arch/x86/kvm/reverse_cpuid.h | 15 +++ arch/x86/kvm/svm/avic.c | 4 + arch/x86/kvm/svm/nested.c | 23 ++++ arch/x86/kvm/svm/sev.c | 10 ++ arch/x86/kvm/svm/svm.c | 207 +++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 5 +- arch/x86/kvm/x86.c | 24 ++-- include/uapi/linux/kvm.h | 10 ++ 21 files changed, 603 insertions(+), 24 deletions(-) create mode 100644 arch/x86/events/amd/vibs.c -- 2.34.1