Execution flow of vCPU
Finally, we are into the vCPU execution flow, which helps us put everything together and understand what happens under the hood.
I hope you didn't forget that the QEMU creates a POSIX thread for a vCPU of the guest and ioctl()
, which is responsible for running a CPU and has KVM_RUN arg (#define KVM_RUN _IO(KVMIO, 0x80))
. The vCPU thread executes ioctl(.., KVM_RUN, ...)
to run the guest code. As these are POSIX threads, the Linux kernel can schedule these threads as with any other process/thread in the system.
Let's see how it all works:
Qemu-kvm User Space: kvm_init_vcpu () Â Â Â Â kvm_arch_init_vcpu() Â Â Â Â Â Â Â qemu_init_vcpu() Â Â Â Â Â Â Â Â Â Â qemu_kvm_start_vcpu() Â Â Â Â Â Â Â Â Â Â Â Â Â qemu_kvm_cpu_thread_fn() Â Â Â Â while (1) { Â Â Â Â Â Â Â Â ...