KVM and QEMU – do you know the connection?

If you were to ask someone “what is the most popular open source hypervisor” chances are that the answer will be KVM. Indeed KVM (or Kernel-based Virtual Machine) has played a key role in the open source Linux based virtualization environment. However is it really a hypervisor? Moreover, can KVM by itself run virtual machines? We will delve more into such questions in this blog. We will also understand the relationship between KVM and QEMU (Quick EMUlator).

KVM and QEMU – the process ID check

You can use libvirt and the associated Virtual Machine Manager GUI to start a virtual machine. In the GUI, you can choose the ‘Virt Type’ as either KVM or QEMU. I started a VM, once with QEMU as the Virt Type and once with KVM. In both cases, I did a grep of the process ID to see if there was any difference. Note: I used Ubuntu 13.10 for my experiments.

KVM and QEMU - VMM Select Virt Type

 When I started the VM with KVM as the Virt Type the process ID details showed an interesting attribute “accel=kvm” as highlighted below.

QEMU using KVM Accelarator

When I started the same Virtual Machine with QEMU as the Virt Type, the process ID showed “accel=tcg”.

QEMU using TCG Accelarator

Note that in both the cases the same binary is executed to start the VM, namely, qemu-system-x86_64. The main difference is the acceleration type.

KVM and QEMU – understanding hardware acceleration

To understand hardware acceleration, we must understand how Virtual Machine CPU works. In real hardware, the Operating System (OS) translates programs into instructions that are executed by the physical CPU. In a virtual machine, the same thing happens. However, the key difference is that the Virtual CPU is actually emulated (or virtualized) by the hypervisor. Therefore, the hypervisor software has to translate the instructions meant for the Virtual CPU and convert it into instructions for the physical CPU. This translation has a big performance overhead.

To minimize this performance overhead, modern processors support virtualization extensions. Intel support a technology called VT-x and the AMD equivalent is AMD-V. Using these technologies, a slice of physical CPU can be directly mapped to the Virtual CPU. Hence the instructions meant for the Virtual CPU can be directly executed the physical CPU slice. 

KVM is the Linux kernel module that enables this mapping of physical CPU to Virtual CPU. This mapping provides the hardware acceleration for Virtual Machine and boosts its performance. Moreover, QEMU uses this acceleration when Virt Type is chosen as KVM.

Then what is TCG? If your server CPU does not support virtualization extension, then it is the job of the emulator (or hypervisor) to execute the Virtual CPU instruction using translation. QEMU uses TCG or Tiny Code Generator to optimally translate and execute the Virtual CPU instructions on the physical CPU.

KVM and QEMU – Type 1 or Type 2 hypervisor

The web pages of KVM and QEMU clearly show that KVM needs QEMU to provide full hypervisor functionality. By itself, KVM is more of a virtualization infrastructure provider.

KVM and QEMU relationship

QEMU by itself is a Type-2 hypervisor. It intercepts the instructions meant for Virtual CPU and uses the host operating system to get those instructions executed on the physical CPU. When QEMU uses KVM for hardware acceleration, the combination becomes a Type-1 hypervisor. This difference is quite clear from the description on the QEMU website.

KVM and QEMU relationship

KVM and QEMU – the x86 dependency

Since KVM is really a driver for the physical CPU capabilities, it is very tightly associated with the CPU architecture (the x86 architecture). This means that the benefits of hardware acceleration will be available only if the Virtual Machine CPU also uses the same architecture (x86).

If a VM needs to run Power PC CPU but the hypervisor server has an Intel CPU, then KVM will not work. You must use QEMU as the Virt Type and live with the performance overhead.

KVM and QEMU – the conclusion

Based on the discussion above, it is quite clear that QEMU plays a very critical role in Linux based Open Source virtualization solutions. For all practical applications, QEMU needs KVM’s performance boost. However, it is clear that KVM by itself cannot provide the complete virtualization solution. It needs QEMU.

  • Kiran

    good explanation

  • Srikanth A

    “If the architecture of physical CPU and the virtual CPU of the VM are different, then QEMU uses TCG to translate instructions and execute the translated instructions on the physical CPU”. In other words, QEMU doesn’t need KVM for virtualization, so it can act like a Type 1 Hypervisor?

    • LeeThao

      QEMU is type 2 Hypervisor , it need KVM to run act like Type 1

  • Pingback: Infrastructure Engineering()

  • Pingback: KVM & QEMU – do you know the connection between them? | Infrastructure Engineering()

  • LeeThao

    “KVM by itself cannot provide the complete virtualization solution”. KVM is type 1 hypervisor, so which virtualization solution KVM cannot provide

    • Hi Lee,

      You have answered your question already. KVM needs Qemu (emulator) for full hypervisor functionality. QEMU on the other hand is self-sufficient (as a Type 2 hypervisor). KVM is really a Linux kernel module for exploiting VT-extenstions.

  • Paul Mallett

    I feel better already, very clear. All I need now is a walk through the vert-manager gui options, before I try and understand the techy stuff, and my happiness will be complete. Anyone know of one

  • Pingback: Goals for the second half of 2014 | Innervoice Blogs()

  • Pingback: Under the hood, KVM and QEMU working together | CHANGE as a Service()

  • jipan yang

    Really clear explanation, thanks for sharing this!

  • MVM

    very good article

  • Pingback: 20140104 | gumdaeng()

  • ashish

    This is awesome, great insight on the internals, can’t stop myself from reading all your blogs.

  • Raman

    Very useful. Thanks !!

  • Eli Cohen

    Thanks for the clear explanation. So what is the real difference between the virtual CPU and physical CPU? Why can’t any CPU serve both tasks (when the guest OS has the same arch as the hypervisor OS). What are the specific extensions that are required? Also, can elaborate on what exactly does the kvm module do? After all I don’t assume instructions from the guest go through kvm module and then to qemu.

    • Innervoice Admin

      The guest VMs only see Virtual CPU. I may have more than one processor with multiple core as the physical CPU but the VM may only get a slice of it (or all). The virtual CPU is “emulated” by qemu. But if the architecure of vCPU and pCPU is same, then qemu uses KVM to pass the guest instructions directly to physical CPU. So the execution of instruction is accelerated.

  • PravinY

    Now I am clear about kvm role in qemu virtualization. This is really nice documentation. Thanks for the explanation. 🙂 Will go through other posts on blog.

  • Edson Babrosa

    thanks man, very quick understanding.

  • Alex Karnezis

    Great post!

  • Neelam N

    Very clear and helpful