Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how qemu-kvm create a VM thread internally?

Tags:

linux

qemu

kvm

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. Then I try to execute the qemu command to see how the task is created. I use strace to track all the system calls. There are no things like "fork" or "pthreadcreate".But I have seen this:

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. I believe this is the place where the VM thread is actually created. Right? I am new to the OS stuff and I will appreciate if anyone can give me some clue:> Thanks

like image 543
Hao Shen Avatar asked Oct 19 '25 09:10

Hao Shen


1 Answers

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. The guest runs (the physical CPU enters VMX non-root mode) when QEMU does KVM_RUN from that thread. KVM_CREATE_VCPU returns a new file descriptor, and that's the fd you'll see in the KVM_RUN ioctl.

VCPU threads might be missing from your strace because you did not use the -ff option. -ff asks strace to also trace other threads than the initial one.

like image 92
Paolo Bonzini Avatar answered Oct 22 '25 00:10

Paolo Bonzini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!