Skip to content
Tutorials Center
My learning

Topics

Lesson 59 of 66 ยท Protection, Security and Virtualization

Virtual Machines and Hypervisors

Learn how virtualization lets many operating systems share one computer: hypervisor types, trap-and-emulate, hardware assistance, paravirtualization, memory virtualization and live migration.

Servers are expensive and, on average, idle most of the time. Virtualization fixes that by letting several complete operating systems run side by side on one physical machine, each believing it owns the hardware. It is the foundation of cloud computing (every cloud server you rent is a virtual machine), of developer tools like VirtualBox and WSL 2, and of strong isolation between tenants.

The core idea fits in one sentence: a virtual machine (VM) is a software-defined computer, and the hypervisor (virtual machine monitor, VMM) is the layer that creates it, schedules it and keeps VMs apart. The OS inside a VM is the guest; the machine and OS it runs on are the host.

Why virtualize?

  • Consolidation: run ten lightly loaded servers on one box.
  • Isolation: a crash or an intrusion inside one VM cannot reach another (the hypervisor is a much smaller attack surface than a full OS).
  • Portability: a VM is a set of files; copy it to another host and it runs.
  • Snapshots and rollback: save a VM's exact state before an upgrade.
  • Testing: try an unfamiliar OS or a risky program safely.
  • Elasticity: start, stop and resize machines on demand.

Types of hypervisors

Type 1 (bare-metal). The hypervisor runs directly on the hardware, like a small OS whose only job is running VMs. Examples: VMware ESXi, Microsoft Hyper-V, Xen and Linux KVM (which turns the Linux kernel itself into a Type 1 hypervisor). Used in data centers because of performance and isolation.

Type 2 (hosted). The hypervisor is an ordinary application on a regular host OS. Examples: VirtualBox, VMware Workstation, Parallels. Convenient for desktops, with extra overhead from the host OS in the path.

flowchart TB
    subgraph T1["Type 1: bare metal"]
      direction TB
      V1["Guest OS A"] --- V2["Guest OS B"]
      V1 --> H1["Hypervisor"]
      V2 --> H1
      H1 --> HW1["Hardware"]
    end
    subgraph T2["Type 2: hosted"]
      direction TB
      G1["Guest OS"] --> H2["Hypervisor app"]
      H2 --> OS["Host OS"]
      OS --> HW2["Hardware"]
    end

The difference is what sits directly on the hardware.

The big problem: privileged instructions

A guest kernel expects to run in the CPU's privileged mode and to execute instructions such as "load a new page table" or "disable interrupts". But if the guest really did that, it could take over the machine. The hypervisor must keep the real privileged mode for itself.

Trap-and-emulate

Run the guest kernel in user mode. When it tries a privileged instruction, the CPU traps into the hypervisor. The hypervisor inspects the instruction, emulates what it should have done to the virtual machine's state, and resumes the guest. Ordinary instructions run at full native speed with no intervention, so most code is not slowed down.

Python Trap-and-emulate in miniature

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Only the few privileged instructions cost a trap. The cost of virtualization is proportional to how often the guest does privileged things (such as I/O or page-table changes), not to how much it computes.

Hardware assistance

Early x86 chips had some sensitive instructions that behaved differently in user mode without trapping, so pure trap-and-emulate failed. Two workarounds appeared:

  • Binary translation: the hypervisor rewrites guest code on the fly, replacing troublesome instructions with safe ones (VMware's early approach).
  • Paravirtualization: the guest kernel is modified to make explicit hypercalls instead of privileged instructions (Xen). Faster, but the guest OS must be changed.

Modern CPUs removed the problem with hardware virtualization extensions (Intel VT-x, AMD-V, ARM virtualization extensions). They add a new mode: the guest kernel runs in its own privileged mode ("non-root", or ring 0 within the guest) and the CPU switches to the hypervisor automatically (a VM exit) only for events the hypervisor must handle.

Virtualizing memory

Each guest believes it has physical memory starting at address 0. Therefore there are three kinds of address:

  1. Guest virtual: what guest programs use.
  2. Guest physical: what the guest OS thinks is RAM.
  3. Host physical: real RAM.

Hardware support called nested paging (Intel EPT, AMD NPT) lets the MMU translate guest-virtual to guest-physical (using the guest's page tables) and then guest-physical to host-physical (using the hypervisor's tables). Without it, hypervisors maintained slow shadow page tables. Memory techniques include ballooning (a driver inside the guest takes memory from the guest so the host can reuse it), memory overcommit (promising more than exists, relying on the fact that VMs rarely use everything) and page sharing (identical pages stored once).

Virtualizing the CPU and I/O

  • CPU scheduling: each VM gets one or more virtual CPUs (vCPUs), which the hypervisor schedules onto real cores. Overcommitting vCPUs is common, and uses the same ideas as CPU scheduling.
  • Device emulation: the hypervisor presents a fake disk or network card. Flexible but slow.
  • Paravirtual devices (virtio): the guest uses a simple, efficient driver that knows it is virtual.
  • Passthrough (SR-IOV, VFIO): a real device or a slice of one is given directly to a VM for near-native speed, using the IOMMU for safety.
Python vCPU overcommit: sharing physical cores

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 8 cores every vCPU runs all the time. With 2 cores each VM gets a fraction, so heavy overcommit means slower guests.

Management features

  • Snapshots: capture disk and memory state; roll back later.
  • Live migration: move a running VM to another host with only a pause of milliseconds. The hypervisor copies memory pages while the VM runs, re-copying pages that changed, then pauses the VM briefly for the last few, and resumes it on the new host.
  • High availability: if a host fails, restart its VMs elsewhere.
  • Cloning and templates: thin copies of a base image.

Other kinds of virtualization

  • Containers share the host kernel, and are lighter (next lesson).
  • Emulation translates a different CPU's instructions (QEMU running ARM on x86): slower than hardware-assisted virtualization.
  • Language VMs such as the JVM run bytecode and are a different idea that shares the name.
  • MicroVMs (Firecracker, used for serverless functions) trim a VM to a few devices and boot in about 100 ms.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What distinguishes a Type 1 hypervisor from a Type 2?

    A bare-metal hypervisor controls the hardware itself. A hosted hypervisor relies on a general-purpose OS underneath.
  2. 2. What happens when a guest kernel executes a privileged instruction under trap-and-emulate?

    The hypervisor keeps real control and applies the effect only to the virtual machine's own state.
  3. 3. What does nested paging (EPT/NPT) accelerate?

    The MMU walks the guest's tables and then the hypervisor's tables, removing the need for slow shadow page tables.
  4. 4. What is paravirtualization?

    The guest knows it is virtualized and asks the hypervisor for privileged services, avoiding costly traps and emulation.

Summary

  • A hypervisor creates and isolates virtual machines. Type 1 runs on hardware, Type 2 on a host OS.
  • Trap-and-emulate, binary translation, paravirtualization and CPU virtualization extensions (VT-x, AMD-V) solve the privileged-instruction problem.
  • Nested paging, ballooning and overcommit virtualize memory; virtio and passthrough speed up I/O.
  • VMs enable consolidation, isolation, snapshots and live migration.

Next: Containers, Namespaces and cgroups