Chinaunix首页 | 论坛 | 博客
  • 博客访问: 570221
  • 博文数量: 201
  • 博客积分: 7734
  • 博客等级: 少将
  • 技术积分: 1994
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-09 19:18
文章分类

全部博文(201)

文章存档

2011年(28)

2010年(173)

分类: LINUX

2011-05-28 22:38:12

  • Xen: fast, runs anywhere, but requires custom OS kernel
  • Qemu without kvm: runs anything anywhere, but slow
  • Qemu-kvm: fast, doesn't require custom kernel, but runs only on specific CPUs
  • When working together, KVM arbitrates access to the CPU and memory, and QEMU emulates the hardware resources (hard disk, video, USB, etc.). When working alone, QEMU emulates both CPU and hardware.
  • QEMU uses emulation; KVM uses processor extensions (HVM) for virtualization.
  • KVM supports hardware virtualization to provide near native performance to the Guest Operating sytems. On the other hand Q-Emu emulates the target operating system.

Qemu:

QEmu is a complete and standalone software on it's own. You use it to emulate machines, it's very flexible and portable. Mainly it works by a special 'recompiler' that transforms binary code written for a given processor into another one (say, to run MIPS code on a PPC mac, or ARM in an x86 PC).

To emulate more than just the processor, Qemu includes a long list of peripheral emulators: disk, network, VGA, PCI, USB, serial/parallel ports, etc.

KQemu:

In the specific case where both source and target are the same architecture (like the common case of x86 on x86), it still has to parse the code to remove any 'privileged instructions' and replace them with context switches. To make it as efficient as possible on x86 Linux, there's a kernel module called KQemu that handles this.

Being a kernel module, KQemu is able to execute most code unchanged, replacing only the lowest-level ring0-only instructions. In that case, userspace Qemu still allocates all the RAM for the emulated machine, and loads the code. the difference is that instead of recompiling the code, it calls KQemu to scan/patch/execute it. All the peripheral hardware emulation is done in Qemu.

This is a lot faster than plain Qemu because most code is unchanged, but still has to transform ring0 code (most of the code in the VM's kernel), so performance still suffers.

KVM:

KVM is a couple of things: first it's a Linux kernel module (now included in mainline) that switches the processor into a new 'guest' state. The guest state has it's own set of ring states, but privileged ring0 instructions fall back to the hypervisor code. Since it's a new processor mode of execution, the code doesn't have to be modified in any way.

Apart from the processor state switching, the kernel module also handles a few low-level parts of the emulation, like the MMU registers (used to handle VM) and some parts of the PCI emulated hardware.

second, KVM is a fork of the Qemu executable. Both teams work actively to keep difference at a minimum, and there are advances in reducing it. Eventually, the goal is that Qemu should work anywhere, and if a KVM kernel module is available, it could be used. But for the foreseeable future, Qemu team focuses on hardware emulation and portability, while KVM folks focus on the kernel module (sometimes moving small parts of the emulation there, if its proven to improve performance), and interfacing with the rest of the userspace code.

The kvm-qemu executable works like normal Qemu: allocates RAM, loads the code, and instead of recompiling it, or calling KQemu, it spawns a thread (this is important); the thread calls the KVM kernel module to switch to guest mode and proceeds to execute the VM code. On a privilege instruction, it switches back to the KVM kernel module, which, if necessary, signals the Qemu thread to handle most of the hardware emulation.

One of the nice things of this architecture is that the guest code is emulated in a posix thread, which you can manage with normal Linux tools. If you want a VM with 2 or 4 cores, kvm-qemu creates 2 or 4 threads, each of them calls the KVM kernel module to start executing. The concurrency (if you have enough real cores) or scheduling (if you don't) is managed by the normal Linux scheduler, keeping code small and surprises limited.

阅读(946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~