The idea
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.
Only the hypervisor runs in real kernel mode. Guest kernels are moved down to user mode, yet each assumes it is in kernel mode. The hypervisor keeps a virtual CPU for each guest, with a pretend mode bit.
A system call in a VM
Line 1 is ordinary work, so it runs straight on the real CPU at full speed and stores 5. Neither the guest kernel nor the hypervisor is involved. This is limited direct execution, one level down.
Line 2 calls write(), which runs a trap instruction. The CPU switches to kernel mode and jumps through the real trap table. That table belongs to the hypervisor, not the guest kernel, so the hypervisor wakes up first.
When the guest kernel booted, it tried to install its trap table. That is privileged, so it trapped, and the hypervisor wrote down the address. Now it sets vCPU 1's mode to kernel and jumps there, still in real user mode.
The guest kernel's driver writes the text hi to what it thinks is a screen. Device I/O is privileged, so this one output traps. The hypervisor checks it and shows the text on the real screen for the guest.
To go back, the guest kernel runs return-from-trap. That is privileged, so it bounces into the hypervisor, which sets vCPU 1's mode to user and does the real return. write() gives back 3 bytes written.
Sharing the machine
n lives in the program's page 2. The guest's page table maps it to "physical" frame 3, but that is not real memory. The hypervisor keeps one more table per VM, mapping frame 3 to machine frame 6.
A timer interrupt hands the CPU to the hypervisor. It saves all of VM 1's state, registers and privileged state, then loads VM 2's and jumps back to where VM 2 stopped. This is a machine switch, a context switch one level down.
A type 1 hypervisor runs directly on the hardware, like the one drawn here. A type 2 hypervisor runs as a program on a host operating system. Linux can be both: its KVM module makes the kernel itself a hypervisor.
All of this needs every sensitive instruction to trap in user mode. The original x86 broke that rule: in user mode POPF silently skips the interrupt flag instead of trapping, so the hypervisor never sees it.
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.