Chapter 00 · Part 0
Two kernels, one Mac: what a virtual machine is
Course progress: chapter 00 is 1 of the 26 chapters written so far (numbering starts at 00)
Concepts you'll need
- Core, taught here: virtual machine, host, guest, virtual machine monitor (VMM), hypervisor; the problem ledger; emulation; trap-and-emulate; sensitive versus privileged instructions and Popek and Goldberg's condition; binary translation; paravirtualization; hardware assist (the idea); a second translation stage (the idea; chapter 05); emulated versus paravirtual devices (the idea; chapters 09 and 15).
- Core, from earlier: none; this is the first chapter.
- Primers (read first):
os-basics(process, user and kernel mode, trap, privileged instruction, system call, page table, interrupt, firmware). - Pointers: QEMU's translator; x86 and
POPF; Popek and Goldberg's three properties; the timeline's systems; KVM. - Forward definitions: VM exit and the exception levels EL0 to EL3 (chapter 02); Virtualization.framework (chapters 01 and 23); the doctor's lines about the IPA (intermediate physical address), EL2 and entitlements (chapter 01).
Suppose you run Linux in a virtual machine app on your Mac. Linux believes it owns the processor, the memory and every device. So does macOS, underneath it. They cannot both be right, so one of them is being fooled.
There is an easy way to fool Linux: write a program that pretends to be a processor, reading Linux's instructions one at a time and doing what each says. It works, and it runs many times slower than the real chip. Yet the apps people use run Linux at nearly full speed. So the real puzzle is this: how does the fooling stay fast?
This chapter answers that by building the course's problem ledger: everything a kernel assumes it owns. Every later chapter closes one of its rows. You work each classic answer through on a toy machine, then check that your Mac is ready.
Objectives: by the end you can
- Explain, with an example, what a virtual machine, host, guest, VMM and hypervisor are (including that most writing uses "hypervisor" for the last two), and give two reasons to want a virtual machine.
- Predict, for the thought experiment, at least three things that break, in your own words, then place each in a ledger row (coarse answers count; the rows you missed are part of the lesson, not a failure).
- Run the same toy guest in
toy-vmmunder interpretation, trap-and-emulate, binary translation, paravirtualization and hardware assist; record for each the VMM interventions per 1,000 guest instructions and whether the console output matches bare metal; explain every difference. - State Popek and Goldberg's condition in one sentence, name the toy instruction set's sensitive but unprivileged instruction, and explain from it why a processor with such instructions needs binary translation or paravirtualization.
- Place the five strategies on a timeline with one real system each (QEMU, IBM VM/370, VMware, Xen, Intel VT-x or Arm's EL2), saying which problem each solved and what it cost.
- Run
scripts/setup.shandscripts/doctor.shand say whathv_vm_create: OKmeans in this chapter's terms.
If a lab does not load. The toy-vmm and syscall-stepper labs run in your browser. Without them, objective 3 still works on paper: trace each mode through the toy program listed below, and count which instructions the VMM handles. The primer's system-call diagram stands in for the stepper.
Before you start
Get the code first. You need the Xcode command line tools (xcode-select --install) and rustup, which installs Rust. Then clone the repository and run its setup script, which checks your Mac and installs the exact Rust version the course uses:
git clone https://github.com/Indicogni/macvmm.gitcd macvmmscripts/setup.shIt ends with setup: OK. Now, from the repository root, run:
scripts/learner initscripts/learner start 00The first run compiles a helper, so it may be silent for a minute. init creates learner/, where your VMM grows from chapter 01 on; start 00 copies this chapter's journal to learner/journal/00.md, "your journal" below, which never leaves your Mac. In a fresh clone:
scripts/learner init && scripts/learner start 00learner: readylearner: 1 file copied, 0 already presentlearner: chapter 00 at level worked"Level worked" means this chapter gives you everything; later chapters hand you less.
If you have a virtual machine app (UTM, Parallels Desktop or VMware Fusion), boot anything and open Activity Monitor: the whole guest computer is one or a few ordinary processes. Or see it with this course's program:
bash guest-as-process.sh== the last lines Linux printed on its console ==[ 0.113263] Run /init as init processmacvmm-initramfs: ready 6.18.60-0-virt/ # == the same moment, seen from macOS == PID %CPU RSS UCOMM29876 0.0 241152 macvmm threads in that process: 6macvmm exit code: 5macvmm: timeout: the guest was still running after --timeout 8sLinux waits at its prompt, / # , and macOS sees one process, macvmm. Idle Linux costs macOS almost no processor time (0.0 %CPU); the six threads include one per virtual processor. Exit code 5 means macvmm stopped the guest itself, because our script asked for --timeout 8 (8 seconds); Linux had not crashed. This is our run: you cannot repeat it yet, because it needs a Linux kernel the course downloads later. Your own VMM reaches this prompt in chapter 14.
The words: guest, host, VMM, hypervisor
The illusion of a whole computer, made for an operating system that is not really in charge of one, is a virtual machine (VM). The operating system inside it is the guest; the one that really runs the machine, macOS for us, is the host. The program that keeps the illusion going, the macvmm process above, is the virtual machine monitor (VMM). People want virtual machines to isolate untrusted software, run another operating system without rebooting, pack many servers onto one machine, and crash a test kernel instead of their laptop.
One more word, with a warning. Most of the world uses hypervisor and VMM for the same thing: you will read "QEMU is a hypervisor" and "KVM is a hypervisor" (two programs that run virtual machines; Asides below place them), and both are fine.
This course must tell apart two pieces, so it uses the word narrowly. The hypervisor is the part that controls the processor's support for virtual machines, at a processor level more privileged than the kernel's (the level above kernel mode that the os-basics primer set aside; "Hardware assist" below says where it came from). On a Mac it is a part of macOS. The VMM is the program that builds the machine around the guest; on a Mac, ours. When you read that a program "is a hypervisor", translate it as "is a VMM, or a VMM plus its privileged part".
A thought experiment: run a kernel as an app
A Linux kernel is a file of machine code, like any program. So copy it into an ordinary process's memory, mark that memory executable, and call its first instruction as if it were a function. What breaks?
Write at least three predictions under ## Predict in your journal before you open the answer. If nothing comes, try these prompts:
- When a kernel starts, what does it set up before any app runs? Think: memory, a console, a disk.
- What does it need from the hardware while it runs? Think: a key press, a clock tick.
- What does it do that an app may not? Think of the
os-basicsprimer's user and kernel modes.
Predict A kernel runs as an ordinary app. What breaks? Write three guesses before you open this.
Coarse answers count: "it can't talk to the disk" is a full answer. Write each guess's row number next to it. A row you missed is not a failure; it is a need that is easy to miss.
| Row | The kernel assumes it owns... | As an app, it breaks because... | Closed in chapter |
|---|---|---|---|
| 1 | the processor's privileged state | user mode cannot run kernel-only instructions | 02, 06 |
| 2 | physical memory and its page tables | a process sees only its own addresses | 04, 05 |
| 3 | devices at fixed addresses | nothing answers there | 05, 07, 09 |
| 4 | a way to power off, reboot, or start other processors | there is no firmware to ask | 08, 20 |
| 5 | a description of the machine | it cannot discover the hardware | 10 |
| 6 | interrupts from devices | none reach a process | 11, 12 |
| 7 | an alarm clock, to take turns and sleep | no timer is wired to it | 11, 12 |
| 8 | cheap bulk input and output | every device access that needs a helper program costs a slow round trip | 03, 15 to 19 |
| 9 | several processors | a thread is not a processor | 20 |
| 10 | a boot chain that loads it | nothing loads it | 13, 14, 21 |
The problem ledger. "Breaks" means: in an ordinary process, with no help.
This chapter explains rows 1 to 3, 6 and 7; row 4 needs firmware, the program a processor runs first, and the rest wait for their chapters. Row 1 bites hardest, so we start there.
Strategy 1: interpret every instruction
The easy answer from the opening: keep a software copy of the whole machine (registers, memory, a mode bit) and loop: fetch the guest's next instruction, decode it, and do what it says to the copy. That program is an emulator. It is correct for any guest on any host, even an Arm guest on an Intel-based computer, and slow: tens of host instructions per guest instruction. QEMU, the best-known emulator, began this way.
Time to meet the toy machine:
- four registers,
r0tor3, and a program counter (pc, the address of the next instruction); - a mode bit: the primer's "user mode" or "kernel mode";
- a base-and-limit register: in user mode every address the program uses is added to the base, and an address past the limit traps; kernel mode uses addresses as they are. This is the toy's whole page table;
- an interrupt-enable bit, switched off by
CLIand on bySTI; - 256 words of memory; the console sits at
0xF0to0xFF, and a store to0xF0prints a character; - ordinary instructions (
LI, load a number;ADD;LD;ST;JNZ, jump if not zero);SYSCALL, an app asking its kernel (it enters kernel mode at address0x10, saving the return address inr3); the privilegedSETBASE,IRET r3(back to user mode, at the address inr3),CLIandSTI;GETMODE, which reads the current mode; and, in mode G only,HCALL, which asks the VMM directly.
Its guest is a toy kernel and app. Each line is marked P (privileged), S (sensitive but unprivileged: you will meet the word shortly), C (a console store) or T (traps by design: a system call):
addr instruction mark what it does (addresses in hex)--- toy kernel, loaded at 0x00 ---00 SETBASE 0x20, 0x1F P the app's memory: 32 words from 0x2001 STI P interrupts on02 GETMODE r1 S r1 = 1 in kernel mode, 0 in user mode03 JNZ r1, 07 kernel mode? skip the panic04 LI r0, '!' panic: ...05 ST r0, [0xF0] C ...print "!"...06 JNZ r0, 06 ...and spin here forever07 LI r3, 0x00 the app starts at its address 008 IRET r3 P enter the app, in user mode10 ST r0, [0xF0] C system-call handler: print r011 IRET r3 P back to the app--- toy app, at 0x20 (it sees it as 0x00) ---00 LI r0, '1' the first character01 LI r1, -3 a counter that climbs to 002 LI r2, 103 SYSCALL T ask the kernel to print r004 ADD r0, r2 next character05 ADD r1, r2 count up06 JNZ r1, 03 not 0 yet? print again07 JNZ r2, 07 done: spin here (r2 is never 0)The kernel never runs CLI. On bare metal (mode A) the console prints 123, the reference, after 23 instructions; after 27 the app reaches its spin loop and stays there for the rest of the 1,000-instruction budget.
Toy VMM
Run one toy guest under eight strategies, from bare metal to hardware assist, and count who executes each instruction: the CPU or the VMM.
Runs in browserOpen full page: Toy VMMMode B is the thought experiment: in user mode with no VMM, the first instruction, SETBASE, is privileged, so the process dies (row 1). Mode C is the emulator: the VMM acts on all 1,000 instructions. The lab's timings use an invented cost model (1 ns per native instruction, 1 µs per VMM intervention); chapter 03 measures the real cost.
Aside
Without the processor's help, QEMU is a cleverer emulator: it translates each block of guest instructions into host code once and reuses it (Bellard, 2005). With the help described under "Hardware assist", it becomes a VMM like ours.
Strategy 2: deprivilege, trap, emulate, resume
Recall the system call from the os-basics primer: an app in user mode asks, the processor switches to kernel mode and jumps to the kernel's handler, and the kernel returns to the app. Step through it again:
System call stepper
Single-step an app's svc from user mode into the kernel and back, then compare it with a page fault, predicting which of mode, PC, saved return address and reason change at each step.
Runs in browserOpen full page: System call stepperNow apply that picture one level over. Run the guest kernel natively, but in user mode. Most of its instructions (adding, loading, branching) are harmless and run at full speed.
When it reaches for something only a kernel may do, the processor traps, as in the primer, but into the VMM, which in this classic design runs in kernel mode. (On a Mac that job is split between macOS and our program; chapter 01 shows how.) The VMM updates a virtual copy of the state the guest believed it changed (its mode, its base register) and resumes the guest after the instruction. This is trap-and-emulate: deprivilege, trap, emulate, resume.
Devices get the same treatment. In mode D the VMM gives the guest base 0x40 and limit 0xAF, so the console range is past the limit and each console store traps; the VMM plays the console. Interrupts and the clock too: CLI and STI trap, so the VMM keeps a virtual interrupt-enable bit and delivers virtual interrupts, clock ticks included, only while it is on.
Memory is harder, because a real guest kernel writes its own page tables. The classic answer was shadow page tables: a real set the VMM keeps, mapping the guest's addresses straight to host memory, kept in step by write-protecting the guest's own tables, so the guest's writes to them fault into the VMM. In the toy this is one addition: the guest's SETBASE traps, the VMM remembers the guest's base (0x20) as a virtual value, and when the guest enters its app it loads the real register with the guest's base plus its own (0x60).
Predict In mode D, the toy guest runs 1,000 instructions. How many does the VMM execute on its behalf: (a) all 1,000, since it is running the guest; (b) about 1,000 plus some overhead; (c) a handful?
Write your guess and one sentence of reasoning under ## Predict in your journal, below your ledger guesses, then follow mode D through the listing.
The answer is (c). The VMM acts only on what traps: the privileged instructions (SETBASE, STI, both IRETs), the console stores, and GETMODE (remember that one). It also acts on the app's system calls: the processor sends every trap to real kernel mode, which is now the VMM, so the VMM must pass each one on to the guest kernel. From the listing: four traps to start (lines 00, 01, 02 and 08), then three per character (SYSCALL, the store, IRET), 13 in all. The other 987 run unseen.
If you chose (a), you held this chapter's misconception, "a VMM emulates the processor": that is an emulator, mode C. If you chose (b): the VMM is not even running between traps.
When trapping is not enough
Mode D quietly cheated: it made GETMODE trap, which the toy's processor does not do. Nor did the mode-reading instructions of x86, the instruction set of the Intel and AMD processors in 1990s personal computers. The failure it hid: the guest kernel asks the processor "am I in kernel mode?"; the processor, really running it in user mode, answers "no"; nothing traps, so the VMM never gets the chance to lie; and the guest panics.
Now the names. An instruction is sensitive if it reads or changes machine state the VMM must control, such as the current mode. It is privileged if it traps in user mode and runs normally in kernel mode. In 1974, Gerald Popek and Robert Goldberg proved when trap-and-emulate works: a processor can be virtualized this way if every sensitive instruction is privileged. The toy has one sensitive instruction that is not privileged: GETMODE.
Deliberate breakage 1: mode E, the sneaky instruction. Mode E is mode D with GETMODE behaving as the toy's processor really does.
Predict In mode E, will the guest notice that it is not really in kernel mode?
Write your guess in your journal next to your mode D guess, then follow mode E one instruction at a time, counting traps, and stop at GETMODE.
It notices. SETBASE traps (one trap); STI traps (two). GETMODE is not privileged, so the processor runs it natively and writes 0, "user", into r1: the count stays at two, line 03 does not jump, and the kernel falls into its panic code. Its ! (a third trap, for the console store) is the only output.
What you saw: ! instead of 123. The misconception this exposes: "if something goes wrong, the VMM gets told". The tool that finds it: step mode's "who executes it" column, where GETMODE shows the processor, not the VMM, and the trap counter does not move.
The cure is to give the VMM a turn before GETMODE answers. Making it privileged (a toggle in the lab) is the fix Popek and Goldberg ask of processor designers; two more need no new processor.
Aside
Popek and Goldberg ask three things of a VMM: equivalence, resource control and efficiency; mode C fails efficiency and mode E equivalence. x86 had seventeen sensitive unprivileged instructions (Robin and Irvine, 2000). One, POPF, restores a saved word of flags, including the interrupt-enable bit (the toy's CLI/STI bit); in user mode it silently leaves that bit unchanged, so a deprivileged guest kernel that turns interrupts off this way believes it did, and no trap tells the VMM.
Two ways around a broken instruction set
Binary translation scans each short run of guest code right before it executes and rewrites the sensitive instructions into calls into the VMM; the rest is copied and cached. VMware built its first product this way in 1999. In mode F the rewritten GETMODE reaches the VMM, which answers "kernel". The cost is the translator: a large, hard program, time to translate each new block, and memory for the cache.
The other way is to change the guest. Paravirtualization edits the guest kernel's source so it asks the VMM directly, with a hypercall (a deliberate call to the VMM, as a system call asks the kernel). Xen took this road in 2003.
In mode G the modified guest has HCALL at line 02 instead of GETMODE. The cost is the edit: a guest whose source you cannot change cannot run this way. The idea survives in power-off hypercalls (chapter 08) and devices designed for virtual machines (chapter 15).
Hardware assist
The lasting fix came from the processor makers: a level above the kernel, whose software chooses what traps (on a Mac, macOS's part makes most of these choices for us; chapter 01). The guest kernel runs in a real kernel mode, so GETMODE answers "kernel" truthfully, and every operation the VMM asked to watch stops and hands over: hardware-assisted virtualization. Intel shipped its version, VT-x, in 2005.
Arm calls its privilege levels exception levels, EL0 (apps) to EL3: a kernel runs at EL1, and the level that supervises kernels is EL2; chapter 02 meets them properly. EL2 has been defined in 64-bit Arm's instruction set architecture, the rulebook every Arm chip follows, since its first version, ARMv8-A. It is optional for chip makers; every Apple silicon chip has it.
It was not free: every stop is a round trip to the VMM, and with memory still shadowed, the first hardware-assisted VMMs were often slower than binary translation (Adams and Agesen, 2006). The fix for memory: the hardware translates addresses twice, through the guest's own tables, then through a second translation stage that the VMM owns, so the guest edits its tables with no traps and still reaches only the memory it was given. Chapter 05 builds ours.
Devices stay the VMM's job: an emulated device imitates real hardware register by register, so an unmodified driver works, at a trap per access (chapter 09); a paravirtual device is designed for virtual machines and needs far fewer (chapter 15).
Mode H gives the toy a third, more privileged mode. The VMM chooses what traps: here, only the console range. The guest sets its own base natively, and the VMM's second base register, applied after it, keeps the guest in its memory: the toy's second translation stage. Trap base writes too, and the output is the same, with more interventions. The interrupt switch needs no trap: the hardware keeps a virtual interrupt mask for the guest. Leave the console range unchosen and the VMM maps plain memory there: the prints never trap, so the VMM, the only thing that can reach your screen, never sees them, and the console stays empty.
All eight modes, now that you have predicted:
| Mode | Strategy | When the VMM acts | Output as in mode A? |
|---|---|---|---|
| A | bare metal | never: there is no VMM | it is the reference |
| B | a process, no VMM | never | no: killed at SETBASE |
| C | interpret | on every instruction | yes |
| D | trap-and-emulate | on each trap | yes (GETMODE made to trap) |
| E | trap-and-emulate, real GETMODE | on each trap | no: the guest panics |
| F | binary translation | on each trap and each new block | yes |
| G | paravirtualization | on each trap and each hypercall | yes |
| H | hardware assist | only on what the VMM chose to trap | yes |
Try it
Ask macOS whether this processor offers the hardware-assisted level: run sysctl kern.hv_support. A 1 means yes. Ours:
sysctl kern.hv_supportkern.hv_support: 1The strategies on one screen:
1960s IBM CP-40, CP-67 trap-and-emulate on mainframes (Creasy 1981)1972 IBM VM/370 the same, as a product (Creasy 1981)1974 Popek and Goldberg when trap-and-emulate works (Popek and Goldberg 1974)1999 VMware Workstation binary translation on x86 (Bugnion et al. 2012)2000 Robin and Irvine x86 has 17 sensitive, unprivileged (USENIX Security 2000)2002 Denali the word "paravirtualization" (Whitaker et al. 2002)2003 Xen paravirtualization of Linux (Barham et al. 2003)2005 QEMU paper fast emulation by block translation (Bellard 2005)2005 Intel VT-x a hardware level above the kernel (Adams and Agesen 2006)2006 AMD-V the same, from AMD (Adams and Agesen 2006)2007 AMD, then Intel second translation stage, 2007-2008 (vendor manuals)2010 ARMv7-A extensions Arm's first virtualization level (Arm, Cortex-A15 launch)2011 ARMv8-A EL2 in 64-bit Arm (Arm, ARMv8 launch)Each strategy arrived to fix the cost or the hole of the one before.
That makes five strategies, each with one real system: emulation (QEMU), trap-and-emulate (IBM's mainframes, in the timeline's first rows, with VM/370 the product), binary translation (VMware), paravirtualization (Xen) and hardware assist (Intel VT-x, or Arm's EL2).
Aside
You do not need to remember these names. On Linux, KVM, a part of the kernel, offers VT-x and EL2 to ordinary programs (KVM documentation): the job chapter 01 finds macOS doing.
What is left for a VMM
The answer to the opening puzzle: the fooling stays fast because the real processor runs almost every guest instruction itself, and stops only for what the VMM asked to see.
Before reading on, write under Q3 in your journal what the VMM still did in mode H.
Running the guest's instructions is not on the list. The toy showed four jobs: set up the guest's memory (the second base register), answer its device accesses (the console), answer its requests (hypercalls such as mode G's HCALL, which a real guest uses to power off), and decide what each stop means. A real machine adds two: set the guest's first registers, and describe the machine to it. That list is the course.
The moment the guest stops and the VMM runs is a VM exit (chapter 02; never a process's exit code). The privileged level decides what stops the guest; the VMM decides what to do about it. On a Mac, who owns that level? Not us; chapter 01 says who. (Apple also ships a ready-made virtual machine library, Virtualization.framework; we leave it until chapter 23, and chapter 01 says why.)
Security
Every job on that list handles values the guest chose, and a hostile guest sends whatever breaks the VMM. From chapter 03 on: check every guest-chosen number before you use it.
Is this Mac ready?
From the repository root:
scripts/setup.shscripts/doctor.shsetup.sh checks the tools this course needs and ends with setup: OK. Ours (your versions will differ):
scripts/setup.shsetup: host: arm64setup: macOS: 27.2.1setup: clang: Apple clang version 21.0.0 (clang-2100.4.1.1)setup: rust: rustc 1.99.0 (b940084d7 2026-09-28) with rustfmt, clippy, llvm-tools, aarch64-unknown-nonesetup: optional: dtc present (/opt/homebrew/bin/dtc)setup: optional: e2fsprogs present (1.47.4, /opt/homebrew/opt/e2fsprogs/sbin/mkfs.ext4)setup: optional: xz present (xz (XZ Utils) 5.8.4)setup: optional: shellcheck present (0.11.0)setup: OKdoctor.sh builds a tiny probe program, attaches the one permission macOS requires before a program may create a virtual machine (an entitlement, carried in the program's signature; chapter 01 and its primer explain both), and asks macOS to create one. Three of its lines (max IPA, EL2 supported, entitlements) will mean nothing to you yet, as expected; check that the last line says DOCTOR OK. Chapter 01 explains the others.
scripts/doctor.shdoctor: hv_vm_create: OKdoctor: max IPA: 40 bits (default 36)doctor: EL2 supported: yesdoctor: codesign entitlements: com.apple.security.hypervisor onlyDOCTOR OKhv_vm_create: OK means this Mac has the hardware-assisted level, and macOS let this process use it to create a virtual machine. EL2 supported may say no on older Apple chips; that is normal and passes, because it asks a different question, answered in chapter 01. If doctor stops before DOCTOR OK, it prints a hint; the common cause is a Mac that is itself a virtual machine, such as some cloud Macs. scripts/learner hint 00 gives more.
Checkpoint
First check your journal: ## Predict holds your ledger guesses with their rows and your mode D and E guesses, and Q1 to Q5 are answered (Q5 is the open question below). Then, from the repository root:
Checkpoint
Needs a MacRun this from the repository root:
scripts/checkpoint.sh 00You should see, as the last line:
CHECKPOINT 00 PASSIt runs setup.sh and doctor.sh, compares their lines (where Macs differ, any value passes), and checks that every journal heading has an answer. A pass prints the ideas a strong answer contains. Ours, with a filled journal:
scripts/checkpoint.sh 00checkpoint: implementation <tree>/learner[INFO] contract tests none for this chapter[PASS] runs on your Mac setup: 2/2 serial markers, exit 0 in 0.46s[PASS] runs on your Mac doctor: 5/5 serial markers, exit 0 in 0.23s[INFO] journal 5/5 answered, predict recorded; rubric printed below # Rubric 00: Two kernels, one Mac: what a virtual machine is Ideas a strong answer contains. Compare, do not copy: an answer in your own words that reaches the same mechanism is a full answer. ## Predict - Lists at least three things that break, in any words, written before the ledger was shown: coarse answers such as "it can't use the disk" or "it can't run kernel-only instructions" are full answers. - Puts a ledger row next to each guess (for example: kernel-only instructions, row 1; the disk or console, row 3; the clock, row 7), and notes which rows nobody guessed. - Commits to one answer for mode D before running it, with a reason. - Afterwards records what mode D showed: (c), only the instructions that trap (the privileged ones, `GETMODE` made to trap, the console stores and the app's system calls, which the VMM passes on to the guest kernel: 13 from the listing); and names "the VMM runs every instruction" as the emulator's behaviour (mode C), not a VMM's. - For mode E, records that the guest did notice: `GETMODE` answered "user" and the toy kernel panicked, with no trap. ## Q1 - Compares the three: an intervention for every instruction under interpretation, only the trapping ones under trap-and-emulate, only what the VMM chose to trap under hardware assist (from the listing: 1,000, 13 and 4 with the interrupt switch and console chosen; or the lab's counts). - Names the deciding number as the VMM interventions (traps) per guest instruction, not the number of guest instructions. - Explains why: each intervention costs far more than a native instruction (about a thousand times in the lab's stated toy model), so the fraction of instructions that need the VMM sets the slowdown. - Notes that the toy cost model is invented, and that the real cost of one stop is measured later, on the Mac. ## Q2 - Names `GETMODE`: sensitive (it reads the mode the VMM must control) but not privileged (it does not trap in user mode). - Explains that the VMM never ran at that moment: no trap means no turn, so it could not substitute the virtual mode "kernel". - Explains why afterwards is too late: the wrong answer is already in a register and the guest has branched on it; the VMM cannot tell, from outside, which later behaviour came from that answer. - Connects it to Popek and Goldberg's condition: every sensitive instruction must be privileged for trap-and-emulate to work. ## Q3 - Separates what the toy showed from what a real machine adds (the first registers, the description of the machine). - Set up the guest's memory (in the toy, the second base register: the second translation stage). - Set the first registers, so the guest starts at the right place. - Describe the machine to the guest. - Answer device accesses (the console range it chose to trap). - Answer requests and decide what each stop means. - Does not list "running the guest's instructions": under hardware assist the processor does that. (A common wrong answer is "it translates the guest's instructions"; that is emulation.) ## Q4 - A table close to: emulation, such as QEMU, which translates blocks rather than interpreting one instruction at a time (any guest, slow); trap-and-emulate on IBM mainframes, CP-67 and VM/370 (fast, but needs every sensitive instruction privileged); VMware's binary translation (unmodified guests on x86; a large, hard translator, translation time and a code cache); Xen's paravirtualization (no translator; a modified guest); Intel VT-x or Arm EL2 (unmodified guests natively; at first every stop was an expensive round trip and memory was still shadowed, until the second translation stage). - Paravirtualization's cost: the guest must be modified, so a guest whose source you cannot change (or cannot rebuild) cannot run; the VMM and guest must also agree on the hypercall interface. - Survival: hypercalls a guest makes on purpose to ask for a service, such as powering off (chapter 08), and devices designed for virtual machines, which talk to the VMM through shared memory (chapter 15). ## Q5 - Interpretation still works unchanged: the emulator answers the instruction in software. - Binary translation still works: the translator rewrites the new instruction like `GETMODE`. - Paravirtualization still works for guests that are edited not to use it. - Plain trap-and-emulate breaks: the instruction is sensitive and unprivileged, the exact failure of mode E. - Hardware assist is fine only if the guest runs in a real kernel mode where the answer is true, or if the more privileged level can be told to trap it. - The request to the designers: make it trap when run deprivileged, or let the level above choose to trap it (a configurable trap), so every sensitive instruction gives the VMM a turn.CHECKPOINT 00 PASSEach result line is one of three kinds of check: contract lines test your own code (none yet), run lines are runs on your Mac, and the journal line is your journal. "5/5 serial markers" means all five expected lines were found in the script's output.
You can now name the parts of a virtual machine, you have the ledger, and this Mac can create one. Chapter 01 starts from here plus your learner/ workspace.
What's next
On a Mac, the privileged level that hardware assist added belongs to macOS, not to us. Chapter 01 draws the real stack, shows what that level hands to an ordinary program, and has your own Rust code ask it for a virtual machine: the same hv_vm_create the doctor called in C.
Open question
A new processor adds one instruction that reads the current privilege level and does not trap. Which strategies still work unchanged, and what would you ask the processor's designers for?
Spec hunt
- Popek and Goldberg, "Formal Requirements for Virtualizable Third Generation Architectures", 1974 (doi:10.1145/361011.361073), Theorem 1 and the definitions before it: how do they define a sensitive instruction, and which toy instructions fit?
- Robin and Irvine, USENIX Security 2000, the list of sensitive unprivileged instructions: which read state, like
GETMODE, and which change it, likePOPF? - The Arm Architecture Reference Manual (document DDI 0487, free from Arm's developer site), the introduction to virtualization in part D: what can software at EL2 choose to trap?