3

I write some function in my OS kernel to issue the performance monitoring interrupt (PMI) on instructions counter overflow. It works well on my machine (Intel core i5). But when I run it on qemu using

qemu-system-x86_64 -enable-kvm -cpu host -m 256 -serial mon:stdio -cdrom var/run/hypervisor.iso

the interrupt does never fire. Is there anything I am missing? Does it require any special configuration to get the PMI fired on qemu? I recall that instruction counting works well in qemu. msr registers to activate PMI

Msr::write(Msr::MSR_PERF_GLOBAL_CTRL, 0x700000003);
Msr::write(Msr::MSR_PERF_FIXED_CTRL, 0xa);
Msr::write(Msr::IA32_PERFEVTSEL0, 0x004100c5);

Program the PMI for 0x1000 instructions

Msr::write(Msr::IA32_PERF_GLOBAL_OVF_CTRL, 1ull << 32);
Msr::write(Msr::MSR_PERF_FIXED_CTR0, 0xFFFFEFFF | 0xFFFF00000000);
Mahouk
  • 902
  • 9
  • 28
  • in this thread http://kvm.vger.kernel.narkive.com/PCuzsRwf/vt-x-and-performance-counter-interrupt-in-kvm-mode, we read that Performance counter interrupt virtualization may be integrated in KVM, but the thread is too old now to reply and I don't know if this feature is now fully implemented in kvm. – Mahouk Feb 10 '17 at 13:58

1 Answers1

2

From the discussion here, you can read that kvm updates the virtual counter only when you write into PERF_FIXED_CTR_CTRL (0x38d).

So, to get the pmi, you better set this register at last position, after setting the counter value of course.

For the sake of completeness, instructions must be issued in this order:

; set counter value.
mov edx, 0xffff
mov eax, 0xffff0000
mov ecx, 0x309
wrmsr

; set counting mode
xor edx, edx
mov eax, 0xa
mov ecx, 0x38d
wrmsr
fourpastmidnight
  • 4,032
  • 1
  • 35
  • 48
Godo
  • 48
  • 5