写在前面 :本系列解析将作为长期连载,旨在和大家一起交流kvm的相关技术,本人才疏学浅,如果有遗漏或错误之处,还希望各位大牛批评指正~
Irq/nmi window exit从字面来看,可能没有那么好理解,在这里我们将一起探讨一下。
Irq/nmi window的作用基本一致,本文将以irq来举例说明。
我们知道,guest的中断需要我们来注入,但是,guest也可能有无法接收中断的时候,就像host一样,比如执行了cli这种情况。这时我们是不能向guest注入中断的。所以在vmx/svm中,都提供了这样的功能:
当guest可以接受中断的时候,立刻发生exit,告诉vmm可以接受中断了,然后vmm来注入中断。
这就是irq/nmi window的作用所在。
在vmx的vmexit中,exit reason为 7的退出事件即为irq window exit,在svm中,是0x64,他们处理的基本思路是相同的。
下面我们将看看kvm-kmod是如何处理的,这里我们将以vmx为例。
首先我们先来看处理退出事件的handler:
Vmx.c 6378:
[EXIT_REASON_PENDING_INTERRUPT]= handle_interrupt_window
那么,处理函数就是handle_interrupt_window了。
我们再来看 5191行的handle_interrupt_window:
[cpp] view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
- static int handle_interrupt_window(struct kvm_vcpu *vcpu)
- {
- u32 cpu_based_vm_exec_control;
-
- /* clear pending irq */
- cpu_based_vm_exec_control =vmcs_read32(CPU_BASED_VM_EXEC_CONTROL); //读取目前的cpu基础执行控制(我就按字面翻译的,各位看官理解就好~)
- cpu_based_vm_exec_control &=~CPU_BASED_VIRTUAL_INTR_PENDING;//清掉VIRTUAL_INTR_PENDING位,这样就不会在guest可以接收中断的时候发生exit了。为什么要这样做呢?如果不清掉的话,一发生entry之后,如果guest还是可以接中断,就会无线循环下去:entry-可以接中断-exit-entry……
- vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,cpu_based_vm_exec_control);//把控制域写入vmcs。
-
- kvm_make_request(KVM_REQ_EVENT, vcpu);//标记一下,在下次entry之前可以开始注入中断了。
-
- ++vcpu->stat.irq_window_exits; //统计一下irq_window_exit的次数
-
- /*
- * Ifthe user space waits to inject interrupts, exit as soon as
- *possible
- * 就像英文注释所说,如果没有启用在kmod中的虚拟中断芯片、并且qemu需要一个irqwindow、同时没有pending的中断的话,立刻返回qemu,让qemu处理。
- */
- if (!irqchip_in_kernel(vcpu->kvm)&&
- vcpu->run->request_interrupt_window &&
- !kvm_cpu_has_interrupt(vcpu)) {
- vcpu->run->exit_reason =KVM_EXIT_IRQ_WINDOW_OPEN;
- return 0;
- }
- return 1;
- }
我们说过,当处理irqwindow exit之后,我们应该立即向guest注入pending的中断,下面我们将看一下相关的流程:
X86.c 的5960行:
[cpp] view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
- static int vcpu_enter_guest(structkvm_vcpu *vcpu)
- {
- int r;
- bool req_int_win =!irqchip_in_kernel(vcpu->kvm) &&
- vcpu->run->request_interrupt_window; //是否需要irqwindow?
- bool req_immediate_exit = false;
-
- …………
- if (kvm_check_request(KVM_REQ_EVENT, vcpu)|| req_int_win) {
- kvm_apic_accept_events(vcpu);
- if (vcpu->arch.mp_state ==KVM_MP_STATE_INIT_RECEIVED) {
- r = 1;
- goto out; // 如果收到init信号,就去unblock对应的vcpu,让他跑起来。
- }
-
- inject_pending_event(vcpu); //注入pending的中断
-
- /* enable NMI/IRQ window open exits ifneeded */
- //如果需要irq/nmiwindow,则启用irq/nmi window的exit。注意,这里不是一定会enable成功的,如果l2guest在执行l2的guest,并且l1guest处于请求irqwindow的状态,就要将req_immediate_exit,在后面的代码中,会对vcpu所在的cpu发送IPI中断,让它退出l2guest,以便我们可以注入中断。
- if (vcpu->arch.nmi_pending)
- req_immediate_exit =
- kvm_x86_ops->enable_nmi_window(vcpu)!= 0;
- else if(kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
- req_immediate_exit =
- kvm_x86_ops->enable_irq_window(vcpu)!= 0;
-
- ……
- }
下面我们再来看是如何启用irq window exit的:
Vmx.c 的4517行:
[cpp] view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
- static int enable_irq_window(struct kvm_vcpu *vcpu)
- {
- u32 cpu_based_vm_exec_control;
-
- if (is_guest_mode(vcpu) &&nested_exit_on_intr(vcpu))
- /*
- *We get here if vmx_interrupt_allowed() said we can't
- *inject to L1 now because L2 must run. The caller will have
- *to make L2 exit right after entry, so we can inject to L1
- *more promptly.
- * 如果l2guest要运行,我们不能立刻注入中断,必须先退出l2 guest。
- */
- return -EBUSY;
-
- cpu_based_vm_exec_control =vmcs_read32(CPU_BASED_VM_EXEC_CONTROL);
- cpu_based_vm_exec_control |=CPU_BASED_VIRTUAL_INTR_PENDING;//irq window exit位置1
- vmcs_write32(CPU_BASED_VM_EXEC_CONTROL,cpu_based_vm_exec_control); //写入vmcs
- return 0;
- }
总结:
Irq/nmiwindow的含义很形象,我们想注入中断的时候,就等待窗户的开启,一旦开启,中断就能注入了。对应于真实的硬件来说,就像中断控制器等待cpu通知它什么时候可以注入中断,一旦可以,中断就会注入。 |