Execution flow of vCPU

Finally, we are into the vCPU execution flow which helps us to 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 the KVM_RUN arg (#define KVM_RUN_IO(KVMIO, 0x80)). 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 us 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) { if (cpu_can_run(cpu)) { r = kvm_cpu_exec(cpu); } } kvm_cpu_exec ...

Get Mastering KVM Virtualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.