Virtual machines and containers
Popek–Goldberg, trap-and-emulate and VT-x/H, two-level translation, type-1/2, namespaces/cgroups/images.
Why virtualize
The permissions topic ended with a hard limit: different UIDs still share one kernel, one file tree, one process list and one network — and root or a kernel bug crosses every boundary at once. Stronger walls come in two shapes. A virtual machine gives a program a whole computer of its own, kernel included. A container keeps one kernel but gives each group of processes its own view and resource limits.
A VM is a software copy of a computer: virtual CPUs, memory, disks and NICs. Reasons are practical — consolidation of lightly loaded servers, isolation of crashes and compromises, running another OS on the same hardware, and encapsulation (snapshots, copies, live migration). Cloud computing is built on that last property.
The simplest implementation is an emulator: guest registers and memory in arrays, every guest instruction fetched, decoded and carried out in software. Privileged instructions are easy — the emulator updates its own control state — and a guest can even use a different ISA (RISC-V on an x86 laptop). Speed is the problem: one guest add costs dozens of host instructions. Nearly all guest instructions are ordinary arithmetic, loads, stores and branches the host CPU could run itself. The question is when that is safe.
Popek–Goldberg and traps
Key idea (Popek and Goldberg, 1974). Run the guest’s code directly on the processor, but deprivileged, with the hypervisor in the privileged mode. Equivalence, resource control and efficiency hold if every sensitive instruction (one that changes or reveals machine configuration) is also privileged (traps in user mode). Then the hypervisor traps each such instruction, emulates it against a virtual CPU, and lets everything else run at full speed — trap-and-emulate.
On baseline RISC-V, a guest kernel’s csrw satp in U-mode raises an illegal-instruction trap; the hypervisor records the virtual satp and builds the real tables. Classic x86 broke the theorem: POPF and sixteen other instructions were sensitive but not privileged (Robin and Irvine, 2000) — they ran silently wrong. Workarounds: binary translation of guest kernel blocks (VMware), paravirtualization with explicit hypercalls (Xen, and today’s virtio devices), and finally hardware support.
Intel VT-x (2005) and AMD-V (2006) add a guest mode: the guest kernel runs in ring 0 of non-root operation; a VMCS lists which events cause a VM exit. RISC-V’s H extension adds virtualization bit : VS/VU modes for the guest, shadow CSRs (vsstatus, vsatp, …), and fewer traps. Memory needs two translations: guest virtual → guest physical → host physical (). Nested paging (EPT / NPT / G-stage) walks both; on a TLB miss with (Sv39 on Sv39x4) the walker does page-table reads instead of 3; four-level x86-64 gives 24. A TLB hit still costs nothing extra.
Hypervisor types
A type-1 hypervisor runs on the bare metal (ESXi, Xen, Hyper-V). A type-2 hypervisor is an application on a host OS (VirtualBox, VMware Workstation). Linux KVM blurs the line: a kernel module turns Linux into a hypervisor; each VM is a process whose vCPUs are threads. User-space QEMU opens /dev/kvm, supplies guest memory and emulates devices; when guest ISA matches the host, guest code runs natively, otherwise QEMU’s Tiny Code Generator translates blocks — emulator and VMM in one program.
Containers
A container is a group of ordinary host processes with a restricted view and resource caps — no second kernel. Three mechanisms:
- Namespaces (eight kinds on Linux): mount, PID, network, UTS, IPC, user, cgroup, time.
unshare --uts --pid --forkcan make a shell hostnamebox1and PID 1 while the host is unchanged. - cgroups v2: directories under
/sys/fs/cgroup.cpu.max = 50000 100000means 50 ms of CPU per 100 ms period — half a core;memory.max = 64Mis bytes;pids.maxcaps forks. - Layered images on OverlayFS: read-only lower layers shared on disk, a writable upper layer with copy-on-write and whiteouts. A Docker image is such a stack plus metadata; each container gets its own upper layer.
| Process | Container | VM | |
|---|---|---|---|
| Kernel | shared | shared | its own |
| Boundary | permissions | syscall interface | hypervisor |
| Start-up | process | namespaces + mounts | boot a kernel |
A container escape is a host-kernel bug; a VM’s interface is much narrower — which is why clouds often nest containers inside VMs. Next — Networking: how packets move between machines.
The isolation ladder matters for real deployments: permission checks on one kernel, then containers (namespaces + cgroups + images), then virtual machines (guest kernel behind a hypervisor). Public clouds often nest the middle inside the last. Side channels remain: caches, memory bandwidth and the TLB are still shared on a core. Nested paging’s 15 page-table reads on an Sv39/Sv39x4 miss, versus 3 without virtualization, is the concrete tax of that stronger boundary — paid on misses, not on TLB hits.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.