JustKernel

Ray Of Hope

QEMU + VCPU architecture.

Whenever a new CPU is added to the QEMU, call flow goes like this.
Realize the x86 Device : target-i386/cpu.c:x86_cpu_realizefn -> cpus.c:qemu_init_vcpu() and
target-i386/cpu.c:x86_cpu_realizefn -> xcc->parent_Realizefn().

qemu_init_vcpu checks if the kvm is enabled and it calls cpus.c: qemu_kvm_start_vcpu() .

qemu_kvm_start_vcpu allocates the memory for the thread, specify its halting condition and calls qemu_thread_create with the callback function of qemu_kvm_cpu_thread_fn.

qemu_thread_create calls pthread_create() to create a new thread i.e VCPU i.e a new thread in userspace.

qemu_kvm_cpu_thread_fn (callback is called when a new thread is created by QEMU in userspace) : calls kvm_init_vcpu to initialize the vcpu kernel mode contexts. Set the signal masks for the vcpu which it will listen. Start a unending while loop that will continuously wait for events for this cpu.

kvm_init_vcpu: checks if the vcpu with ID vcpu is already available in the parked list of cpus. If its available then there is no need to create a new VCPU in kernel. Else, issue kvm_vm_ioctl(KVM_CREATE_VCPU) to kvm module in kernel and get an fd corresponding to the usermode vcpu or thread. Then issue kvm_ioctl(KVM_GET_VCPU_MMAP_SIZE)i.e get the kernel memory associated with kernel fd and map that memory to userspace. Further then call kvm_arch_init_vcpu.

kvm_arch_init_vcpu: initialize the registers associated with the newly created virtual cpu. (Just like registers are associated with the normal physical CPUs like CR3, CR0 etc).

Thanks
Anshul Makkar
mailto: anshul_makkar@justkernel.com

Tags: ,


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.