I was going to tidy up the entire process of IO processing in the guest VM, KVM, Qemu, and by looking at the data and reading the source code, I had a rough idea of the process of Io in guest KVM. When you want to defragment IO in KVM and QEMU processing, Discovering the process of jumping and interacting between QEMU and KVM is hard to understand, prompting yourself to learn about QEMU and the KVM boot process. (in the code shown in this article, the QEMU version is 1.6.0 and the Linux kernel version is 3.7.10)
To introduce the interaction between QEMU and KVM, let me first describe the interface that KVM provides to the user. KVM is a kernel module that implements a/DEV/KVM character device to interact with the user. Switching between QEMU and KVM can be achieved by invoking a series of IOCTL functions. When you want to create a new virtual machine, first open the/DEV/KVM device and invoke the IOCTL function on it:
[CPP]View Plaincopy
- SYSTEM_FD = open ("/DEV/KVM", Ordwr);
- VM_FD = IOCTL (SYSTEM_FD, KVM_CREATE_VM, 0);
The implementation of the IOCTL function in KVM is the Kvm_dev_ioctl function in Virt/kvm/kvm_main.c, and when the passed parameter is KVM_CREATE_VM, the function creates a VM and returns an FD that enables the operation of the virtual machine.
After the virtual machine is created, you need to create the Vcpus above the virtual machine, and the interface that is called is the IOCTL, only the FD that is returned by the corresponding FD at the time the VM was created.
[CPP]View Plaincopy
- VCPU_FD = IOCTL (VM_FD, VM_CREATE_VCPU, 0)
The implementation of the IOCTL function corresponds to the KVM_VM_IOCTL function in Virt/kvm/kvm_main.c, which is similar to the KVM_CREATE_VM process when the passed parameter is VM_CREATE_VCPU. It creates an Vcpus and returns the FD that can manipulate the Vcpus.
After the Vcpus are created, you can call the IOCTL function above the Vcpus to enter the guest VM.
[CPP]View Plaincopy
- RET = IOCTL (VCPU_FD, Kvm_run, 0);
At this point the IOCTL function corresponds to the implementation of the KVM_VCPU_IOCTL function in Virt/kvm/kvm_main.c, if the passed parameter is Kvm_run, it will eventually call the Vcpu_enter_guest function into the guest VM.
QEMU is a user mode program whose entry is the main function, which is defined in the VL.C file. The main function is long, with two functions associated with KVM initialization: Configure_accelerator () and Machine->init (&args). The Cofigure_accelerator () function chooses which virtualization scheme to apply to a data structure that is accel_list and calls the Accel_list[i].init function. The initialization of Accel_list is as follows, When using a KVM virtualization solution, the accel_list[i].init corresponding function is kvm_init.
[CPP]View Plaincopy
- Static struct {
- Const Char *opt_name;
- Const Char *name;
- Int (*available) (void);
- Int (*init) (void);
- bool *allowed;
- } accel_list[] = {
- { "TCG", "TCG", Tcg_available, Tcg_init, &tcg_allowed},
- { "Xen", "Xen", Xen_available, Xen_init, &xen_allowed},
- { "KVM", "KVM", Kvm_available, Kvm_init, &kvm_allowed},
- { "qtest", "Qtest", Qtest_available, Qtest_init, &qtest_allowed},
- };
The Kvm_init function is defined in the Kvm-all.c file, and its main function is to open the/DEV/KVM device and create a virtual machine.
The Machine->init (&arg) function primarily initializes the hardware device, and calls QEMU_INIT_VCPU to create a thread for each vcpus, and the thread executes a function of QEMU_KVM_CPU_THREAD_FN. From Qemu The function call relationship between main and QEMU_INIT_VCPU involves the assignment of some function pointers the source code comparison is difficult to read, the following is the use of GDB debugging to play its call relationship.
[CPP]View Plaincopy
- #0 qemu_init_vcpu (cpu=0x55555681ea90) at/home/dashu/kvm/qemu/qemu-dev-zwu/cpus.c:1084
- #1 0x0000555555909f1e in X86_cpu_realizefn (Dev=0x55555681ea90, Errp=0x7fffffffd8f8) at/home/dashu/kvm/qemu/ qemu-dev-zwu/target-i386/cpu.c:2399
- #2 0x00005555556c768a in device_set_realized (Obj=0x55555681ea90, Value=true, err=0x7fffffffda88) at HW/CORE/QDEV.C : 699
- #3 0x000055555580b93f in Property_set_bool (Obj=0x55555681ea90, V=0x5555565bab20, Opaque=0x5555565375a0, name= 0x555555a01f88 "realized", errp=0x7fffffffda88) at qom/object.c:1300
- #4 0x000055555580a484 in Object_property_set (Obj=0x55555681ea90, V=0x5555565bab20, name=0x555555a01f88 "realized", errp=0x7fffffffda88) at qom/object.c:788
- #5 0x000055555580bbea in Object_property_set_qobject (Obj=0x55555681ea90, Value=0x555556403e40, name=0x555555a01f88 " Realized ", errp=0x7fffffffda88) at qom/qom-qobject.c:24
- #6 0x000055555580a770 in Object_property_set_bool (Obj=0x55555681ea90, value=true, name=0x555555a01f88 "realized", errp=0x7fffffffda88) at qom/object.c:851
- #7 0x00005555558a7de0 in Pc_new_cpu (cpu_model=0x555555a0200b "Qemu64", Apic_id=0, icc_bridge=0x55555655b2c0, errp= 0X7FFFFFFFDAC8) at/home/dashu/kvm/qemu/qemu-dev-zwu/hw/i386/pc.c:922
- #8 0x00005555558a7fed in Pc_cpus_init (cpu_model=0x555555a0200b "Qemu64", icc_bridge=0x55555655b2c0) at/home/dashu/ kvm/qemu/qemu-dev-zwu/hw/i386/pc.c:978
- #9 0x00005555558a923b in Pc_init1 (system_memory=0x5555562a7240, System_io=0x5555562a7f60, ram_size=1073741824, Boot_ device=0x555555a0248a "CAD", kernel_filename=0x0, Kernel_cmdline=0x5555559f85be "",
- initrd_filename=0x0, cpu_model=0x0, pci_enabled=1, kvmclock_enabled=1) at/home/dashu/kvm/qemu/qemu-dev-zwu/hw/i386 /pc_piix.c:105
- #10 0x00005555558a9a36 in Pc_init_pci (ARGS=0X7FFFFFFFDF10) at/home/dashu/kvm/qemu/qemu-dev-zwu/hw/i386/pc_piix.c : 245
- #11 0x00005555558a9a7f in Pc_init_pci_1_6 (ARGS=0X7FFFFFFFDF10) at/home/dashu/kvm/qemu/qemu-dev-zwu/hw/i386/pc_ piix.c:255
- #12 0x00005555558584fe in Main (argc=10, argv=0x7fffffffe148, envp=0x7fffffffe1a0) at vl.c:4317
The QEMU_KVM_CPU_THREAD_FN function creates the Vcpus and then calls the Kvm_cpu_exec function. The KVM_CPU_EXEC function invokes the IOCTL into the KVM and eventually enters the guest VM.
The above is the process of initializing the KVM for QEMU to call the KVM interface. Later I'll sort out the IO between KVM and Qemu and describe how KVM and qemu work together.
Resources:
1. QEMU-KVM initialization and execution of the client system: http://blog.csdn.net/lux_veritas/article/details/9383643
2. Kernel Virtualization Kvm/qemu----Guest Os,kvm,qemu workflow: http://www.360doc.com/content/12/0619/13/7982302_219186951.shtml
Reprint: http://blog.csdn.net/dashulu/article/details/17074675
The KVM initialization process