Hypervisors and virtual machines
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A virtual machine is a whole computer made of software: one real machine can run several operating systems at once, each sure it owns the hardware. The trick is a hypervisor that takes kernel mode for itself, moves every guest kernel down to user mode, and catches each privileged thing a guest tries. Follow one write() inside a VM as it bounces through the hypervisor, then see memory get one more map, the CPU switch between VMs, and why the original x86 could not play along. This builds on User mode and kernel mode and on Traps, exceptions and the interrupt table; the Hardware lesson on interrupts and I/O covers devices, and Containers and deployment compares VMs with containers.
Machines inside a machine
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.
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)
اقرأ الدرس خطوة بخطوة
Machines inside a machine
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.
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)Only one boss in kernel mode
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.
who real CPU mode what it believes hypervisor kernel (it knows the truth) guest kernel user kernel guest program user user vCPU = the guest's privileged CPU state, kept in memory: its mode, its trap table, its page table pointer, ...Plain work runs directly
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 1 n = 2 + 3; runs on the real CPU, real user mode traps none memory n = 5 most guest instructions run like this, so plain computation in a VM runs at full speedThe trap lands in the hypervisor
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.
line 2 write(1, "hi\n", 3); trap real mode user -> kernel lands the hypervisor's trap handler (it installed the real trap table) vCPU 1 still user: the guest has not seen it yetPassing it to the guest kernel
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.
earlier guest kernel: set trap table -> trapped hypervisor recorded: handler @ guest address now vCPU 1 mode = kernel (pretend) real mode = user (truth) jump to the guest kernel's write handlerThe device is emulated
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.
guest driver out "hi\n" (one privileged I/O) trap -> hypervisor (one trap) hypervisor emulates a common real device, so the guest's normal driver works real screen hi (3 bytes) then back to the guest kernelReturn-from-trap traps too
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.
guest kernel return-from-trap (iret on x86) -> traps hypervisor vCPU 1 mode = user real return-from-trap into the program result write returns 3 this one write() trapped into the hypervisor 3 times: the syscall, the device I/O and the return-from-trapMemory gets one more map
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.
level table kept by example (OSTEP Fig. B.4) virtual (program's address) page 2 "physical" guest kernel page 2 -> frame 3 machine hypervisor frame 3 -> machine 6 the guest never sees machine addresses; the hypervisor can hand frames to any VMSwitching between machines
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.
timer interrupt -> hypervisor (real kernel mode) save VM 1: registers, PC, vCPU 1 (mode, trap table, ...) load VM 2: registers, PC, vCPU 2 jump to VM 2's PC: here inside guest kernel 2 like a process context switch, plus privileged stateTwo kinds of hypervisor
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.
type 1 on the bare machine e.g. Xen, VMware ESXi type 2 an app on a host OS e.g. VirtualBox KVM part of the Linux kernel; a program such as QEMU asks it for VMs with system calls: #define _POSIX_C_SOURCE 200809L #include <fcntl.h> #include <stdio.h> #include <sys/ioctl.h> #include <unistd.h> #include <linux/kvm.h> int main(void) { int kvm = open("/dev/kvm", O_RDWR | O_CLOEXEC); if (kvm < 0) { perror("open /dev/kvm"); return 1; } int v = ioctl(kvm, KVM_GET_API_VERSION, 0); printf("KVM API version %d\n", v); /* 12 */ close(kvm); return 0; } prints KVM API version 12 (on Linux with KVM)When trap and emulate works
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.
guest kernel 2 popf (tries to turn interrupts off) real CPU user mode: IF unchanged, NO trap hypervisor never told; guest is now wrong Pentium: 17 sensitive instructions that do not trap fixes: binary translation, para-virtualisation, VT-x trap and emulate as a Python simulation: PRIVILEGED = {"set_trap_table", "out", "iret"} vcpu = {"mode": "kernel", "trap_table": None} # the guest's pretend CPU def hypervisor(op, arg): # runs in real kernel mode if op == "syscall": # pass it on to the guest kernel vcpu["mode"] = "kernel" return "jump to guest " + vcpu["trap_table"] if vcpu["mode"] == "user": return "fault: guest user code did " + op if op == "set_trap_table": # remember, do not install vcpu["trap_table"] = arg if op == "out": # emulate the device print("screen:", arg) if op == "iret": vcpu["mode"] = "user" return "emulated " + op def run(op, arg=None): # the real CPU, in user mode if op in PRIVILEGED or op == "syscall": print(op, "traps:", hypervisor(op, arg)) else: print(op, "runs directly") run("set_trap_table", "handler@0x80") # guest kernel boots run("iret") # guest starts its program run("add") # line 1 run("syscall") # line 2: write run("out", "hi") # guest driver run("iret") # back to the program prints: set_trap_table traps: emulated set_trap_table iret traps: emulated iret add runs directly syscall traps: jump to guest handler@0x80 screen: hi out traps: emulated out iret traps: emulated iret
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01In classic trap-and-emulate virtualisation, what real CPU mode does a guest kernel run in?
Only the hypervisor may hold real kernel mode, or it would lose control of the machine. The guest kernel runs deprivileged, and the hypervisor keeps a virtual mode bit that says kernel, so the guest's view stays consistent.
02A program inside a VM makes a system call. Which code runs first?
The real trap table belongs to the hypervisor, so the trap lands there first. The hypervisor recorded the guest's own trap table when the guest tried to install it at boot, so it can jump to the guest's handler.
03The guest's page table maps a program's page 2 to frame 3. Where does the data really live?
Guest 'physical' memory is itself virtual. The hypervisor keeps one more map per VM, from guest frames to machine frames (OSTEP's example: 3 maps to 6).
04Why could the original x86 not be virtualised by trap-and-emulate alone?
Trap-and-emulate needs every sensitive instruction to trap. Robin and Irvine counted seventeen Pentium instructions that do not, POPF among them: in user mode it just leaves the interrupt flag unchanged.
05Which is a type 2 hypervisor?
A type 1 hypervisor runs on the bare machine; a type 2 hypervisor runs as a program on a host OS, such as VirtualBox. Linux with KVM blurs the line: the host kernel itself becomes the hypervisor.
المسرد
- Virtual machine (VM)
- A software copy of a whole computer, with its own operating system (the guest) and programs, running on shared real hardware.
- Hypervisor
- Also called a virtual machine monitor (VMM). The software that runs in real kernel mode and shares the hardware among virtual machines.
- Guest kernel
- The operating system running inside a VM. It believes it controls the machine but runs with reduced privilege.
- Virtual CPU (vCPU)
- The privileged CPU state a hypervisor keeps for each guest, such as its pretend mode bit, trap table and page table pointer.
- Trap and emulate
- Running guest code directly and letting every privileged instruction trap to the hypervisor, which performs its effect on the guest's virtual state.
- Machine memory
- The real physical memory. Guest 'physical' addresses are mapped onto it by the hypervisor.
- Machine switch
- Saving one VM's whole state, including privileged state, and loading another's: a context switch one level down.
- Sensitive instruction
- An instruction that reads or changes the machine's mode, protection or devices. For trap and emulate, each one must trap in user mode.
الملاحظات والمصادر
- 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.
- The page numbers 2, 3 and 6 are the example from OSTEP Figure B.4. Real hypervisors either keep a shadow page table that maps virtual pages straight to machine frames, or use the hardware nested page tables covered in Hardware support for virtualisation.
- Robin and Irvine (2000) found seventeen Pentium instructions that are sensitive but do not trap in user mode, POPF among them. Software hypervisors such as early VMware got around this with binary translation; Intel VT-x and AMD-V later added a new CPU mode for guests.
- The C program uses the Linux KVM interface: open() returns a file descriptor for /dev/kvm and ioctl() with KVM_GET_API_VERSION returns 12. It needs Linux with KVM enabled; elsewhere the open fails and it prints the error.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, appendix B: Virtual Machine Monitors
- MIT 6.1810 (2024), lecture notes: Virtual Machines
- J. S. Robin and C. E. Irvine, Analysis of the Intel Pentium's Ability to Support a Secure Virtual Machine Monitor, USENIX Security 2000
- Linux kernel documentation: the KVM API
- Linux man-pages: ioctl(2)
- Linux man-pages: open(2)
اختبار يتاح بعد الخطوة الأخيرة.