4
votes

I am using KVM-QEMU in Intel platform. And I am wondering how is the PMC/PMU being virtualized in KVM?

My understanding of vPMC is as follows.

Since PMU is a shared physical resource, so hypervisor shall context save/restore PMC configurations and counters during VMExit/VMEnter.

But in my testing, I got confused.

 (In my host Linux, the MSR of IA32_PERF_FIXed_CTRL is set to 0xb0)

First of all, in a launched VM,

I 'wrmsr IA32_PERF_FIXed_CTRL 0x0b', to enable FIXed_CTR0 in VM. Then, I logged the MSR of IA32_PERF_FIXed_CTRL at VMExit, and found FIXed_CTR0 is not enabled.

I think it is because when VMExit happens, it is already in the host context, so the MSR of IA32_PERF_FIXed_CTRL is the one of host, not the host.

But the question is where is that MSR for the VM being saved in host, and when and where the VM's MSR is being restored to CPU during VMEnter???

Hope my question is clear.

Thanks,

1

1 Answers

4
votes

KVM implements a virtual PMU that utilizes Linux's perf subsystem for core PMU operations. It does not expose the host PMU directly to the guest.

When you do wrmsr IA32_PERF_FIXed_CTRL 0x0b, you are not writing to the hardware MSR (Model-specific register), KVM traps the MSR write and makes an update in its representation of the virtual PMU. Look for kvm_set_msr_common() in x86.c, this will ultimately call intel_pmu_set_msr() in pmu_intel.c that does the real magic. Also look at struct kvm_pmu() which is KVM's representation of the PMU.

The MSR doesn't need to be restored like typical VMCS fields because KVM saves guest specific PMU settings in struct kvm_pmu() per vCPU (struct kvm_vcpu).