[英]how qemu-kvm create a VM thread internally?
Now I am doing a project on qemu-kvm and linux task scheduler.I know each VCPU is a normal task created by qemu to the linux OS. 现在我正在qemu-kvm和linux task Scheduler上做一个项目。我知道每个VCPU都是qemu创建到Linux OS的正常任务。 Then I try to execute the qemu command to see how the task is created.
然后,我尝试执行qemu命令以查看任务是如何创建的。 I use strace to track all the system calls.
我使用strace跟踪所有系统调用。 There are no things like "fork" or "pthreadcreate".But I have seen this:
没有“ fork”或“ pthreadcreate”之类的东西。但是我已经看到了:
open("/dev/kvm", O_RDWR|O_LARGEFILE) = 3
ioctl(3, KVM_GET_API_VERSION, 0) = 12
ioctl(3, KVM_CHECK_EXTENSION, 0x19) = 0
ioctl(3, KVM_CREATE_VM, 0) = 4
ioctl(3, KVM_CHECK_EXTENSION, 0x4) = 1
ioctl(3, KVM_CHECK_EXTENSION, 0x4) = 1
ioctl(4, KVM_SET_TSS_ADDR, 0xfffbd000) = 0
ioctl(3, KVM_CHECK_EXTENSION, 0x25) = 0
ioctl(3, KVM_CHECK_EXTENSION, 0xb) = 1
ioctl(4, KVM_CREATE_PIT, 0xb) = 0
ioctl(3, KVM_CHECK_EXTENSION, 0xf) = 2
ioctl(3, KVM_CHECK_EXTENSION, 0x3) = 1
ioctl(3, KVM_CHECK_EXTENSION, 0) = 1
ioctl(4, KVM_CREATE_IRQCHIP, 0) = 0
ioctl(3, KVM_CHECK_EXTENSION, 0x1a) = 0
So it looks that it opens the devices /dev/kvm and did some ioctl syscalls. 因此,看起来它打开了设备/ dev / kvm并执行了一些ioctl syscall。 I believe this is the place where the VM thread is actually created.
我相信这是实际创建VM线程的地方。 Right?
对? I am new to the OS stuff and I will appreciate if anyone can give me some clue:> Thanks
我是OS方面的新手,如果有人可以给我一些提示,我将不胜感激:
VCPU is neither a OS thread nor a process. VCPU既不是操作系统线程,也不是进程。 To understand how VCPU works, first we should figure out how guest OS is running on
Intel VT-x
architecture. 要了解VCPU的工作原理,首先我们应该弄清楚来宾操作系统如何在
Intel VT-x
架构上运行。
Intel VT-x
proposed a new mode methodology with two modes: VMX root mode
and VMX non-root mode
, for running host VMM and guest respectively. Intel VT-x
提出了一种具有两种模式的新模式方法: VMX root mode
和VMX non-root mode
,分别用于运行主机VMM和来宾。 Intel VT-x
also contains a new structure: VMCS
, which saves all information both host and guest need. Intel VT-x
还包含一个新结构: VMCS
,可以保存主机和来宾需要的所有信息。 VMCS
is one per guest. VMCS
是每个来宾一个。
KVM is a hardware-assisted hypervisor and leverages Intel VT-x
. KVM是硬件辅助的管理程序,并利用
Intel VT-x
。 The host Linux KVM is running in VMX root mode
. 主机Linux KVM在
VMX root mode
。 When KVM decides to switch CPU mode to run a guest, KVM dumps all current contexts to VMCS
and executes a "VMLAUNCH" instruction. 当KVM决定切换CPU模式以运行客户机时,KVM会将所有当前上下文转储到
VMCS
并执行“ VMLAUNCH”指令。 "VMLAUNCH" will transfer CPU from VMX root mode
to VMX non-root mode
, and load guest context from VMCS, then start or continue to execute guest code. “ VMLAUNCH”会将CPU从
VMX root mode
转移到VMX non-root mode
,并从VMCS加载来宾上下文,然后启动或继续执行来宾代码。
In summary, the guest code is running directly on CPU in VMX non-root mode
. 总之,访客代码以
VMX non-root mode
直接在CPU上VMX non-root mode
。 no software emulation layer for VCPU is needed. 不需要用于VCPU的软件仿真层。 That's why KVM has better performance, and there is no specific thread for guest.
这就是为什么KVM具有更好的性能,并且没有用于来宾的特定线程的原因。
/dev/kvm
is created by kvm.ko
, which is only a KVM interface for QEMU. /dev/kvm
由kvm.ko
创建,它只是QEMU的KVM接口。 Your strace
output showed how QEMU was interacting with KVM and controlling the underlying guests. 您的
strace
输出显示了QEMU如何与KVM交互并控制底层来宾。 You can never find a fork
or clone
system call in KVM. 您永远无法在KVM中找到
fork
或clone
系统调用。
For more KVM detail especially VCPU, you can read KVM code in arch/x86/kvm/vmx.c
for more VCPU implementation detail based on Intel VT-x
. 有关KVM的更多详细信息(尤其是VCPU),您可以阅读
arch/x86/kvm/vmx.c
KVM代码,以获取更多基于Intel VT-x
VCPU实施细节。
Even though a VCPU is an OS object different from a thread or a process, and VCPU objects are created with the KVM_CREATE_VCPU ioctl, QEMU is indeed creating a thread per VCPU. 即使VCPU是不同于线程或进程的OS对象,并且使用KVM_CREATE_VCPU ioctl创建了VCPU对象,QEMU的确为每个VCPU创建了一个线程。 The guest runs (the physical CPU enters VMX non-root mode) when QEMU does KVM_RUN from that thread.
当QEMU从该线程执行KVM_RUN时,guest虚拟机运行(物理CPU进入VMX非根模式)。 KVM_CREATE_VCPU returns a new file descriptor, and that's the fd you'll see in the KVM_RUN ioctl.
KVM_CREATE_VCPU返回一个新的文件描述符,这就是您将在KVM_RUN ioctl中看到的fd。
VCPU threads might be missing from your strace because you did not use the -ff
option. 由于未使用
-ff
选项,因此strace中可能缺少VCPU线程。 -ff
asks strace to also trace other threads than the initial one. -ff
要求strace还跟踪除初始线程之外的其他线程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.