Part 4 of 6 · 2 min read
Three approaches
Think of three ways to let a foreign visitor live in your house. You can translate every sentence the visitor says: slow, but the visitor needs no change. You can teach the visitor your language so that requests are made directly: fast, but the visitor must learn. Or you can give the visitor a private wing with its own door, where most things need no translation: fast, with no teaching. The analogy stops in one place: a translator reads text, while a hypervisor reads machine instructions.
In plain words: Three approaches exist.
| Approach | How the guest runs | Guest changed? | Speed |
|---|---|---|---|
| Full emulation | Every guest instruction is interpreted by software | No | Very slow (10 to 100 times) |
| Paravirtualisation | The guest kernel calls the hypervisor (hypercalls) in place of privileged instructions | Yes | Fast |
| Hardware-assisted | The processor runs the guest directly and traps only on a defined set of events | No | Fast |
Modern systems use hardware assistance for the processor and memory (page 2). They use paravirtual drivers for devices (page 3). So the guest kernel is almost unmodified, but its drivers know they are virtual. A hypercall is a deliberate call from the guest to the hypervisor, like a system call from a program to a kernel.
A worked example. On Linux, the guest can tell that it is a guest. The hypervisor bit in the flags of /proc/cpuinfo is set only inside a VM. On the test machine:
$ lscpu | grep -i -E "hypervisor|virtualization type"
Hypervisor vendor: KVM
Virtualization type: full
$ systemd-detect-virt --vm
kvm
$ systemd-detect-virt --container
docker
The machine is a KVM guest, and inside it the processes run in a Docker-style container (page 4). Both layers are visible from within. The flags vmx (Intel) and svm (AMD) are not shown. They mean "this processor can host guests". The test machine's own hypervisor does not offer them, so it cannot start a guest of its own.
The key lines. The word "full" in Virtualization type: full means the guest runs an unmodified kernel with hardware assistance, as opposed to a paravirtualised guest.
Hardware-assisted virtualisation
Hardware-assisted virtualisation is processor support (Intel VT-x, AMD-V) for running guest code directly in a special mode, in which the hardware traps only the instructions and events that the hypervisor chooses, and saves and restores the guest's processor state itself.
Example: a guest runs ordinary arithmetic at full speed and traps to the hypervisor only for events such as I/O to an emulated device.
Check yourself:
1. What does paravirtualisation require that hardware assistance does not?
2. What does the hypervisor flag in /proc/cpuinfo tell?
3. What do vmx and svm mean?