VM 11n = 2 + 3;2write(1, "hi\n", 3);OS 1VM 2OS 2HYPERVISORvCPU 1uservCPU 2userCPUMODEuserRUNSVM 1frame 4frame 5frame 6frame 7hin=5hi3p2f3m6popf
Back: two virtual machines, each with a guest kernelMiddle: the hypervisor and each guest's virtual CPU; front: the real CPU, memory and screenTokens: a system call, device output and return bouncing through the hypervisorTwo virtual machines at the back, each with a guest kernel; the hypervisor in the middle, holding each guest's virtual CPU state; the real CPU, memory and screen in front. Line 1 runs straight on the CPU. Line 2's system call lands in the hypervisor, which passes it to the guest kernel, emulates its device output and catches its return-from-trap. Memory gets one more map, a timer switches to VM 2, and a popf slips past without trapping. Every trap through the hypervisor costs time, so system calls and device I/O are slower in a VM, while plain computation runs at full speed. Para-virtualised guests and virtual devices (such as virtio on Linux) cut the number of traps.

The idea

  1. A virtual machine (VM) is a software copy of a whole computer. A hypervisor, also called a virtual machine monitor, runs on the real hardware and shares it among VMs. Each VM runs its own operating system, the guest.

A system call in a VM

Sharing the machine

What happens

;; step 1/11 · machines
real hardware   one CPU, one memory, one screen
hypervisor      shares them out, like a kernel for kernels
VM 1, VM 2      each: a guest kernel + its programs

the goal: each guest believes it controls the machine
(OSTEP calls this transparency)

The lesson draws one real mode bit with two values. Real CPUs have more levels (x86-64 has rings 0 to 3, RISC-V has machine, supervisor and user modes), and Disco on MIPS ran guest kernels in the in-between supervisor mode. The idea is the same: the guest kernel is not in the most privileged mode. Every trap through the hypervisor costs time, so system calls and device I/O are slower in a VM, while plain computation runs at full speed. Para-virtualised guests and virtual devices (such as virtio on Linux) cut the number of traps.