从KVM中的来宾VM用户空间拦截rdtsc指令 [英] Intercept rdtsc instruction from guest vm userspace in KVM
问题描述
我被标题所困扰,我想通过在VMM中添加
I'm stuck in the problem as the title says.I want to do this in VMM by adding
setup_vmcs_config 函数中vmx.c(arch/x86/kvm)中的 CPU_BASED_RDTSC_EXITING 标志和
the CPU_BASED_RDTSC_EXITING flag in vmx.c(arch/x86/kvm) in setup_vmcs_config function,and
然后自己处理vm_exit(请参阅:邮件列表).问题是我不能
then handle the vm_exit by myself(ref this:mail list).The question is that I cannot
告诉rdtsc的vm_exit是由来宾内核还是来宾vm用户引起的
tell whether the vm_exit of rdtsc is caused by the guest kernel or the guest vm user
空间应用程序,后一个正是我要拦截的内容.我尝试搜索
space application, the latter one is what exactly I want to intercept.I have tried to search
通过qemu-kvm-1.2.0 src找到其他方法来拦截rdtsc指令,我找到了
through qemu-kvm-1.2.0 src to find other ways to intercept the rdtsc instruction,I find
rdtsc线索.我在那里添加了一个printf,但是我什么也没得到.所以我
rdtsc clue in target-i386/translate.c.And I add a printf there, but I got nothing.So I
想知道是否有人可以给我一点突破的指导.非常感谢您!
wonder if anyone could give me some little guidance to break through.Thank you a lot~
推荐答案
经过一番实验,我几乎找到了自己问题的答案.请参见下面的代码:
After some experiment,I almost find the answer to my own question.See the code below:
static int handle_rdtsc(struct kvm_vcpu *vcpu)
{
u64 data;
if (vmx_get_msr(vcpu, MSR_IA32_TSC, &data)) {
kvm_inject_gp(vcpu, 0);
printk("wsh_handle_rdtsc_return\n");
return 1;
}
vcpu->run->exit_reason = 20;
vcpu->arch.regs[VCPU_REGS_RAX] = data & -1u;
vcpu->arch.regs[VCPU_REGS_RDX] = (data >> 32) & -1u;
skip_emulated_instruction(vcpu);
if(vmx_get_cpl(vcpu)>0)
{
printk("wsh_handle_rdtsc,cpl:%d\n",vmx_get_cpl(vcpu));
}
return 1;
}
如上所述,我使用vmx_get_cpl过滤由来宾内核引起的rdtsc vm_exit,事实上,printk总是打印3,因为来宾vm应用程序的特权级别始终为3.欢迎进行任何更正!
As you have seen above,I use vmx_get_cpl to filter those rdtsc vm_exit caused by the guest kernel,and I fact,the printk always print 3,because the privellege level of guest vm applications is always 3.Any corrections is welcomed!
这篇关于从KVM中的来宾VM用户空间拦截rdtsc指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!