18. The trap-and-emulate story, then hardware

The five modes and three traps Nested boxes. The outer box is M-mode. Inside it, a V=0 box holds HS-mode with U-mode beside it, and a V=1 box holds VS-mode with VU-mode beside it. Three arrows: a VS-mode ecall goes up to HS-mode (when medeleg delegates it, otherwise to M) with exception code 10; a VS-mode sret with hstatus.VTSR set goes up to HS-mode (when medeleg delegates it) with code 22, a virtual instruction exception; a VU-mode ecall goes left to VS-mode with code 8 when hedeleg bit 8 is set. M-mode V=0: two-stage translation off HS-mode the hypervisor U-mode its helpers V=1: two-stage translation on VS-mode the guest kernel VU-mode guest apps VS ecall code 10 VS sret, VTSR=1 code 22, virtual ecall: code 8 hedeleg[8]=1 Popek and Goldberg's theorem, implemented as a mode bit

Why this layer exists

Chapter 7 built isolation from two objects: a page table whose kernel entries lack the U bit, and the privilege mode sret sets on the way down. Those two keep a program out of a kernel. A hypervisor has a harder tenant. It has to run a kernel, code written to own the machine: it writes satp, executes sret, reads the clock, idles with wfi. The hypervisor must let that code run at full speed and still not let it own anything. That needs a third object: a way to run supervisor-level code under a layer that sees every instruction which would touch machine-owned state, or answers it from a private copy.

Popek and Goldberg worked out in 1974 when the hardware that already exists is enough: trap-and-emulate works if every instruction that touches such state traps. The Intel Pentium failed that test on seventeen instructions, which Robin and Irvine counted in 2000. x86 got its third object added later, as VT-x and SVM. The RISC-V hypervisor extension (the H-extension) was designed with it rather than retrofitted, and the RISC-V ISA is where this book writes its hypervisor: the course design (§3, the infrastructure table) names “RISC-V H-extension on QEMU virt” as the written hypervisor ISA, AArch64 EL2 as the second, and x86 VMX as an optional later dialect. That is why hearth is written on RISC-V first.

This chapter reads the H-extension as a claim about instructions: for each instruction a guest kernel executes, one of three things happens, and the table in the Walkthrough says which. The vocabulary defined here (trap-and-emulate, sensitive and privileged instructions, the virtualization mode V, the virtual instruction exception, VMCS and VMCB), together with HS-, VS- and VU-mode, which chapter 3 named and this chapter elaborates, is what chapters 19 through 22 and Part VII use without defining again.

Mechanism

Every section number below is from the pages as fetched on 2026-10-01 UTC: the privileged spec, hypervisor chapter, v20260120 (https://docs.riscv.org/reference/isa/v20260120/priv/hypervisor.html, fetched 04:45Z), and its machine chapter (…/priv/machine.html, same fetch). Quotes are verbatim. Section numbers in the machine chapter are written 2.1.x as that page numbers them. “§14.1” alone means the hypervisor chapter’s introduction, the text before §14.1.1.

Trap-and-emulate, sensitive, privileged

Trap-and-emulate is the technique: run the guest’s instructions directly on the real hart, at a privilege level below the hypervisor’s. When the guest executes something that would change or reveal machine state the hypervisor owns, the hart traps to the hypervisor, which emulates the instruction’s effect on the guest’s virtual copy of that state and resumes the guest after it. Almost every guest instruction is an add or a load and never traps; the cost is paid only where an instruction is about the machine.

Popek and Goldberg give the vocabulary, for a deliberately simplified machine with a supervisor/user mode and a relocation register (“Formal Requirements for Virtualizable Third Generation Architectures”, CACM 17(7), July 1974, pp. 412–421; page numbers below are the journal’s). An instruction is privileged if it traps when executed in user mode and does not trap in supervisor mode. The paper is explicit that the trap is the definition: an instruction that is merely a no-op in user mode “should not be called a privileged instruction; maybe ‘user mode NOP’ would be more accurate” (p. 415). An instruction is sensitive if it is control sensitive, meaning it attempts to change the resources available or “affects the processor mode without going through the memory trap sequence” (p. 415), or behavior sensitive, meaning the effect of its execution depends on the mode or on the relocation register (p. 416). Their basic theorem is Theorem 1 (p. 417):

For any conventional third generation computer, a virtual machine monitor may be constructed if the set of sensitive instructions for that computer is a subset of the set of privileged instructions.

Read it as a bug report waiting to happen. A sensitive instruction that is not privileged is a hole: the guest kernel executes it, it acts on the real hart, and the hypervisor never gets a trap. The formal definitions are for the paper’s simplified machine; this book uses sensitive in the looser sense that fits a real ISA: an instruction whose effect or result depends on, or changes, state the hypervisor owns.

What x86 did

Robin and Irvine (“Analysis of the Intel Pentium’s Ability to Support a Secure Virtual Machine Monitor”, 9th USENIX Security Symposium, 2000) applied the theorem to a real ISA. They analysed the Pentium’s “approximately 250 instructions” (abstract) and found that “seventeen instructions violate” their Requirement 3 (§3), their form of the theorem’s condition. The paper’s own summary of the consequence (§2.2) is the cost model of the whole subject: when a VM runs in user mode and a sensitive instruction is privileged, it traps and the VMM emulates it; “However, if sensitive, non-privileged instructions exist, it may be necessary for the VMM to examine all instructions before execution to force a trap to the VMM”. The standing example is popf, which pops a word into the flags register. Executed in a guest kernel that the VMM runs at user level, without enough privilege to change the interrupt flag, “an exception is not generated” (§3.1.3): the flag stays as it was and the guest never learns. The VMM never sees the instruction at all. sgdt, sidt, sldt and smsw (§3.1) fail the other way, reading machine state without a trap.

The fix was in hardware. Intel’s VT-x and AMD’s SVM add a second operating state in which guest code runs (the Intel manual’s name is VMX non-root operation) and a memory-resident structure that holds the guest’s processor state: the VMCS (virtual-machine control structure) on Intel, the VMCB (virtual-machine control block) on AMD. Of the Intel one, the SDM says only what this book needs: “VM entries load processor state from these fields and VM exits store processor state into these fields” (Vol. 3C §27.4). That is a trap frame the hardware fills in: the same object as chapter 5’s struct trapframe, except that the hardware, not the entry stub, writes it. The SDM chapters are named in Further reading; nothing else in this book depends on them.

The H-extension: modes and V

Chapter 3 quoted the extension’s opening sentence of purpose (§14.1) and named HS-, VS- and VU-mode; this chapter says what separates them. The extension changes S-mode into HS-mode, “hypervisor-extended supervisor mode”, and adds a second S-mode and U-mode for guests:

HS-mode acts the same as S-mode, but with additional instructions and CSRs that control the new stage of address translation and support hosting a guest OS in virtual S-mode (VS-mode). Regular S-mode operating systems can execute without modification either in HS-mode or as VS-mode guests. (§14.1)

The S-mode ember has run in since chapter 3 is HS-mode on a hart that has the H-extension; mret picks it by MPP=S with mstatus.MPV clear (§14.1.6.4, Table 9). drop_to_s does not touch MPV, and the machine chapter (§2.1.4) does not specify its reset value, so the book relies on QEMU’s reset value, and chapter 19’s boot into HS depends on it: a hypervisor CSR write that does not trap is the proof that V was 0. What separates HS from VS is a new piece of hart state, the virtualization mode V, which §14.1.1 says tells whether the hart is executing in a guest:

When V=1, the hart is either in virtual S-mode (VS-mode), or in virtual U-mode (VU-mode) atop a guest OS running in VS-mode. When V=0, the hart is either in M-mode, in HS-mode, or in U-mode atop an OS running in HS-mode. The virtualization mode also indicates whether two-stage address translation is active (V=1) or inactive (V=0). (§14.1.1)

So there are five modes: M, HS and U with V=0, and VS and VU with V=1 (the figure draws them). “HS-mode is more privileged than VS-mode, and VS-mode is more privileged than VU-mode” (§14.1.1). Two-stage translation, the second half of that paragraph, is chapter 19; here it only says V=1 is where it applies.

hstatus: the guest’s control panel

hstatus is HS-mode’s register for tracking and controlling the exception behaviour of a VS-mode guest (§14.1.2.1). The fields this chapter needs, with their bit positions on RV64 as the pinned Linux tree defines them (arch/riscv/include/asm/csr.h:144–152 at v7.2.8, the tree chapter 15 builds):

Field Bit What it controls
SPV 7 Set by the hart on a trap into HS to the value of V at the time of the trap
SPVP 8 Set to the guest privilege (VS or VU) only when V was 1, otherwise unchanged; sets the effective privilege of the hlv/hsv accesses
HU 9 Whether the hlv/hlvx/hsv instructions may also run in U-mode
VTVM 20 Makes satp accesses and sfence.vma from VS trap
VTW 21 Makes wfi from VS trap
VTSR 22 Makes sret from VS trap

SPV is the hypervisor’s entry switch as well as the trap’s record: “the SPV bit in hstatus is set to the value of the virtualization mode V at the time of the trap. When an SRET instruction is executed when V=0, V is set to SPV.” To start or resume a guest, HS sets SPV to 1 and sstatus.SPP to 1, writes the guest’s program counter into sepc, and executes sret; the hart lands at that address in VS-mode with V=1 (§14.1.6.4, Table 10). The same instruction chapter 7 used to enter U enters a guest. SPVP is the guest-side counterpart of sstatus.SPP: when V was 1, “bit SPVP (Supervisor Previous Virtual Privilege) is set to the nominal privilege mode at the time of the trap, the same as sstatus.SPP”, and it also sets the effective privilege of the virtual-machine load and store instructions. Without it, “even with HU=1, U-mode could not access virtual machine memory at VS-level, because to enter U-mode using SRET always leaves SPP=0”. HU is for a hypervisor split into a small HS part and a larger U part: “The HU bit allows a portion of a hypervisor to be run in U-mode for greater protection against software bugs, while still retaining access to a virtual machine’s memory.” hstatus has more fields (GVA, VGEIN, VSBE, VSXL); GVA is chapter 19’s.

The three trap bits are one idea:

The hstatus fields VTSR, VTW, and VTVM are defined analogously to the mstatus fields TSR, TW, and TVM, but affect execution only in VS-mode, and cause virtual-instruction exceptions instead of illegal-instruction exceptions. (§14.1.2.1)

mstatus.TSR, TW and TVM already let M-mode make S-mode’s sret, wfi and satp/sfence.vma trap (§2.1.1.6.6; chapter 6 quotes the specification’s use for the wfi lever, a world switch to another guest). The H-extension gives HS the same three levers over VS, without going through M.

VS CSRs: the copy

A guest kernel is unmodified supervisor code: it writes sstatus, stvec, satp. If those writes reached the hart’s real sstatus, stvec and satp, they would overwrite the hypervisor’s own. The extension’s answer is a second set of registers, chosen by V:

Furthermore, several virtual supervisor CSRs (VS CSRs) are replicas of the normal supervisor CSRs. For example, vsstatus is the VS CSR that duplicates the usual sstatus CSR.

When V=1, the VS CSRs substitute for the corresponding supervisor CSRs, taking over all functions of the usual supervisor CSRs except as specified otherwise. Instructions that normally read or modify a supervisor CSR shall instead access the corresponding VS CSR. (§14.1.2)

The replicas are vsstatus, vsip/vsie, vstvec, vsscratch, vsepc, vscause, vstval and vsatp (§14.1.2.11–§14.1.2.18), plus vstimecmp with the Sstc extension (§14.1.2.19). The real ones are not lost: while V=1 they “retain their values but do not affect the behavior of the machine unless specifically documented to do so” (§14.1.2). A few supervisor CSRs have no replica (senvcfg, scounteren, scontext), and for those “Hypervisor software is expected to manually swap the contents of these registers as needed.” The guest’s csrw satp is an ordinary instruction that happens to write vsatp. This is the sense in which the guest’s state is virtualised rather than trapped: no exit, no emulation, the hart addresses a different register.

An access in the other direction does trap. “When V=1, an attempt to read or write a VS CSR directly by its own separate CSR address causes a virtual-instruction exception” (§14.1.2), and the same holds for the H-level CSRs: the guest cannot read hstatus to find out it is a guest.

The three outcomes, and the virtual instruction exception

A guest instruction that touches machine-owned state therefore ends one of three ways:

  1. It runs on the VS copy (or, for cycle and wfi, on the real hart with no trap). sret with VTSR=0 (§14.1.6.4: “When executed in VS-mode (i.e., V=1), SRET sets the privilege mode according to Table 11, in vsstatus sets SPP=0, SIE=SPIE, and SPIE=1, and lastly sets pc=vsepc”), the CSR accesses above, time and cycle reads when hcounteren and mcounteren allow them.
  2. It traps to the hypervisor as a virtual instruction exception.
  3. It traps as its ordinary cause, the same exception a non-virtualised hart would raise: ecall is the case that matters.

The virtual instruction exception is exception code 22. It is how the hardware tells HS that the instruction would have been valid in HS-mode but was stopped because V=1, which for a VS-mode guest kernel usually means one it is entitled to execute:

When V=1, a virtual-instruction exception (code 22) is normally raised instead of an illegal-instruction exception if the attempted instruction is HS-qualified but is prevented from executing when V=1 either due to insufficient privilege or because the instruction is expressly disabled by a supervisor or hypervisor CSR such as scounteren or hcounteren. An instruction is HS-qualified if it would be valid to execute in HS-mode (for some values of the instruction’s register operands), assuming fields TSR and TVM of CSR mstatus are both zero. (§14.1.6.1)

The split matters because the two exceptions call for different handlers. A virtual instruction exception is a trap the hypervisor can emulate (the spec: “It is not unusual that hypervisors must emulate the instructions that raise virtual-instruction exceptions”): the guest did something legal for a kernel, and the hypervisor supplies the effect. “When not emulating the trapping instruction, a hypervisor should convert a virtual-instruction trap into an illegal-instruction exception for the guest virtual machine” (§14.1.6.1), so it can still be handed to the guest as an error. The spec adds a performance reason: “Machine level is expected ordinarily to delegate virtual-instruction traps directly to HS-level, whereas illegal-instruction traps are likely to be processed first in M-mode before being conditionally delegated (by software) to HS-level.”

Exception codes and delegation

The extension adds codes to mcause and scause: “Codes are added for VS-level interrupts (interrupts 2, 6, 10), for supervisor-level guest external interrupts (interrupt 12), for virtual-instruction exceptions (exception 22), and for guest-page faults (exceptions 20, 21, 23). Furthermore, environment calls from VS-mode are assigned cause 10, whereas those from HS-mode or S-mode use cause 9 as usual.” (§14.1.6.1, Table 7). Five of the codes in that table matter in this part (chapter 3’s exercise (d) decodes them from a probe):

Code Table 7 name Where it is used
10 Environment call from VS-mode this chapter, chapter 19
20 Instruction guest-page fault chapter 19
21 Load guest-page fault chapter 19
22 Virtual instruction this chapter
23 Store/AMO guest-page fault chapter 19

The guest-page faults belong to the second translation stage; chapter 19 builds that stage and defines them. They are listed here because a hypervisor’s trap handler reads the code to tell all five apart.

Delegation is a chain, and the extension adds a link. “When a trap occurs in VS-mode or VU-mode, it goes to M-mode, unless delegated by medeleg or mideleg, in which case it goes to HS-mode, unless further delegated by hedeleg or hideleg, in which case it goes to VS-mode.” (§14.1.6.2). Which bits of hedeleg can be set is fixed by §14.1.2.2, Table 2: bit 8 (environment call from U-mode or VU-mode) is writable, and bits 9, 10 and 11 (environment calls from HS, VS and M) and bits 20–23 (the guest-page faults and the virtual instruction exception) are read-only zero. Two consequences. A guest never receives a virtual instruction exception or a guest-page fault. Those are HS’s when medeleg delegates them, otherwise M’s. And a VS-mode ecall, code 10, can never be delegated to the guest. The spec gives the reason the code exists, “HS-mode and VS-mode ECALLs use different cause values so they can be delegated separately” (§14.1.6.1), so that M can delegate one to HS and not the other; the reason the hypervisor cannot pass it on follows from what ecall is, a request to the layer above, which for VS is HS: “An HS-mode hypervisor is expected to implement the SBI for its VS-mode guest” (§14.1). The SBI, the Supervisor Binary Interface, is the calling convention by which supervisor code asks the layer below it for a service; chapter 19’s guest makes one such call. A VU-mode ecall is a different matter: it is code 8, the guest kernel’s own system call, and hedeleg[8] lets HS pass it straight to the guest without a world switch. The figure’s three arrows are these cases; the two upward arrows reach HS when medeleg delegates codes 10 and 22 to it, and M otherwise.

Placing this on chapter 14’s axes

Chapter 14’s three axes, for the object this chapter describes. What is shared: the hart, and, from chapter 19, the memory it translates; most of the guest’s S-level registers are not shared, because each has a VS replica. Who is trusted: HS-mode and M-mode. The hypervisor depends on the guest kernel for nothing: the guest’s S-mode is not part of the hypervisor’s TCB, which is the point of the part. Where the copy is: the VS CSRs are the guest’s registers, held by the hart while V=1, and everything else (the guest’s general-purpose registers) is saved into a trap frame by HS’s entry code, the way chapter 5’s trap.S saves them for ember. On x86 the VMCS and VMCB are where the copy lives; on RISC-V it is a set of CSRs and a frame HS writes itself.

Walkthrough

There is no code. The table is the lab’s answer key: ten rows, each an instruction (or class) a guest kernel executes in VS-mode, what an unmodified S-mode kernel would see, and which of the three outcomes the H-extension gives it, with the spec section that says so. “Virtual instruction” is exception code 22.

Instruction in VS-mode Unmodified S-mode semantics With V=1 Spec
sret Returns from a trap: mode from sstatus.SPP, pc from sepc (chapter 7) Copy if hstatus.VTSR=0: pops vsstatus, pc=vsepc. Virtual instruction if VTSR=1. §14.1.6.4; §14.1.2.1
wfi Stalls the hart until an interrupt may need service; may be a NOP (chapter 6) Runs if VTW=0 and mstatus.TW=0. If VTW=1 and mstatus.TW=0: virtual instruction, unless it completes in an implementation-specific bounded time. If mstatus.TW=1: ordinary illegal instruction (code 2), likewise unless it completes in bounded time §14.1.2.1; §14.1.6.1; §2.1.1.6.6
csrr/csrw satp Reads or writes the S-stage translation root (chapter 4) Copy if VTVM=0: accesses vsatp; a write with an unsupported MODE is ignored. Virtual instruction if VTVM=1 §14.1.2.18; §14.1.2.1
sfence.vma Orders and flushes address-translation caches for satp (chapter 4) Copy if VTVM=0: applies to the current satp, which at V=1 is vsatp. Virtual instruction if VTVM=1 §14.1.3.2; §14.1.2.1
sstatus, sie, sip, stvec, sscratch, sepc, scause, stval The S-level register Copy: the instruction reads or writes the vs* replica; HS’s own value is untouched §14.1.2; §14.1.2.11–§14.1.2.17
csrr time, csrr cycle Readable when the mcounteren bit is set, else illegal instruction Both bits set: cycle runs on the real counter, and time is a copy that returns time + htimedelta. hcounteren bit clear and mcounteren bit set: virtual instruction. mcounteren bit clear: ordinary illegal instruction §14.1.2.6; §14.1.2.7; §14.1.6.1; §2.1.1.11
ecall A request to the layer above: from S-mode it is code 9 Ordinary: code 10, always; hedeleg[10] is read-only zero, so it cannot reach VS. Lands in HS if medeleg[10] is set, else in M §14.1.6.1, Table 7; §14.1.2.2, Table 2; §14.1.6.2
hlv.*, hlvx.*, hsv.* Do not exist; illegal instruction Virtual instruction at V=1. In HS they are the hypervisor’s way to read and write guest memory §14.1.3.1; §14.1.6.1
hfence.vvma, hfence.gvma Do not exist; illegal instruction Virtual instruction at V=1; neither mstatus.TVM nor hstatus.VTVM makes hfence.vvma trap in HS §14.1.3.2; §14.1.6.1
csrr/csrw of vsatp, hstatus, hedeleg … by their own addresses Do not exist; illegal instruction Virtual instruction at V=1: the guest cannot touch the replicas or the control CSRs directly §14.1.2; §14.1.6.1

Three things the table shows that no row says alone.

First, the conditions sit in two places. VTSR, VTW, VTVM, hcounteren and hedeleg are HS’s; mstatus.TW and mcounteren are M’s. HS decides what is virtual instruction versus copy; M’s bits decide whether the outcome becomes an ordinary illegal instruction instead. The spec closes the loop with one sentence: “Because TSR and TVM in mstatus are intended to impact only S-mode (HS-mode), they are ignored for determining exceptions in VS-mode” (§14.1.6.1). TW is not in that sentence, which is why wfi has the extra case above.

Second, VU-mode is stricter than VS. The spec’s list (§14.1.6.1) has VU-mode sret and sfence instructions, supervisor CSR accesses that HS-mode could make, and wfi with mstatus.TW=0 all raise virtual-instruction exceptions, and bit 22 of hedeleg is read-only zero, so they are never the guest’s (HS’s when medeleg delegates code 22, otherwise M’s): a guest’s user code can never run a supervisor instruction.

Third, the three outcomes cover every row, and this is the check of the theorem for this table: no row is sensitive and silent. The spec does not state Popek and Goldberg’s condition as a goal; the table is the reader’s evidence for the instructions it lists, not a proof about the whole ISA.

Exercises

(a) Why does popf defeat trap-and-emulate on a 1990s Pentium, in Popek and Goldberg’s terms? Name the two properties it has and the one it lacks.

Answer

Robin and Irvine §3.1.3: executed in a VM running at user level with insufficient privilege, popf raises no exception, and the interrupt-flag bits are left unchanged, so a guest kernel’s attempt to re-enable interrupts is silently dropped and the VMM never sees the instruction. It is sensitive: its effect on the flags depends on the privilege level the VM runs at (behavior sensitive, Popek and Goldberg p. 416), and, in this chapter’s looser sense of the word, it changes state the monitor owns. It lacks the third property: it is not privileged. Popek and Goldberg’s definition requires the user-mode execution to trap, and their p. 415 names the case that does not, “user mode NOP”. Theorem 1 (p. 417) needs sensitive ⊆ privileged, and popf is a counterexample to it. The hardware fix did not make popf trap: the guest’s flags live in a field (Intel’s guest-state area holds the guest RFLAGS, SDM Vol. 3C §27.4.1), and the guest’s popf writes the guest’s copy.

(b) Which hstatus bit makes wfi trap from VS-mode, what is the exception code, and which other CSR bit changes the code?

Answer

VTW, bit 21 (csr.h:145 at v7.2.8). With VTW=1 and mstatus.TW=0, a VS-mode wfi raises a virtual-instruction exception, code 22 (§14.1.6.1), unless it completes within an implementation-specific, bounded time (§14.1.2.1). With mstatus.TW=1 the answer changes to code 2, an illegal-instruction exception: §2.1.1.6.6 says that when TW=1, a wfi executed in “any less-privileged mode” that does not complete in bounded time causes one, and §14.1.2.1 states VTW’s effect only “assuming mstatus.TW=0”. So mstatus.TW sits above VTW, and the code that arrives in HS depends on a bit the hypervisor may not own.

(c) A VS-mode ecall lands in HS with code 10. What does a VU-mode ecall do when hedeleg[8]=0, and when hedeleg[8]=1, and what must be true of medeleg in both cases? Which hstatus and vsstatus fields change?

Answer

A VU-mode ecall is code 8 (Table 7, “Environment call from U-mode or VU-mode”). In both cases medeleg[8] must be set; otherwise the trap goes to M, because the chain starts at medeleg (§14.1.6.2). Chapter 5’s trap_init_m value (medeleg bits 2, 3, 8, 12, 13 and 15) sets it, the same bit chapter 7 uses to take a user ecall in S. With hedeleg[8]=0 the trap goes to HS: V becomes 0, hstatus.SPV=1 and sstatus.SPP=0 (Table 10, VU-mode), SPVP is set to the same value as SPP, 0, and sepc, scause, stval and the two hypervisor trap registers are written (§14.1.6.2): htval, which holds a faulting guest-physical address and is zero for an ecall (§14.1.2.8), and htinst, zero or a transformed copy of the trapping instruction (§14.1.2.9). With hedeleg[8]=1 the trap goes to VS: vsstatus.SPP=0 (Table 11), vsepc, vscause and vstval are written, and, in the spec’s words, “hstatus and the HS-level sstatus are not modified, and the virtualization mode V remains 1”. The second case is the one that costs HS nothing: the guest kernel sees an ordinary system call, as it would on bare metal. The corresponding VS ecall has no such case because hedeleg[10] is read-only zero (Table 2).

(d) Name the VS CSR that replaces satp and the hstatus bit that makes the guest’s accesses to satp trap. What happens to HS’s own satp while the guest runs?

Answer

vsatp (§14.1.2.18) and VTVM, bit 20 (§14.1.2.1; csr.h:146). With VTVM=1 the same bit also makes sfence.vma and sinval.vma trap, with code 22. HS’s own satp is not overwritten and not consulted: the supervisor CSRs that have VS replicas “retain their values but do not affect the behavior of the machine unless specifically documented to do so” (§14.1.2), so HS does not have to change its own satp to run a guest. HS may still choose to trap the guest’s satp writes. The reason is the one §2.1.1.6.6 gives for the machine-level TVM bit: trapping them “provides the hooks necessary to lazily populate shadow page tables”, the software technique that a second translation stage replaces.

(e) The VMCS is a trap frame. List the fields of ember’s struct trapframe (chapter 5), and say which three VMCS guest-state fields they correspond to.

Answer

struct trapframe in ember/trap.h has 35 fields, 280 bytes (chapter 5): the 31 general-purpose registers ra, sp, gp, tp, t0–t6, s0–s11, a0–a7, and the four CSRs sepc, scause, stval, sstatus. Intel SDM Vol. 3C §27.4.1 lists the guest-state area’s register fields: control registers CR0, CR3 and CR4, DR7, “RSP, RIP, and RFLAGS”, the segment and descriptor-table registers, and some MSRs. Three line up: sepc with RIP, sp with RSP, sstatus with RFLAGS (RFLAGS holds the interrupt-enable flag, as sstatus.SIE does). The list has no other general-purpose register, so on VMX the guest’s remaining GPRs are the VMM’s to save in software, as ember’s entry stub saves the other 30 here. scause and stval have no guest-state counterpart: they record why the guest stopped, and the VMCS keeps that elsewhere than the guest-state area (the exit information fields; this chapter does not read those). On RISC-V the guest-side copy of all four is the VS CSRs, which the hart already holds.

(f) On chapter 14’s axes, what does the H-extension add to the TCB of a hypervisor?

Answer

Nothing outside HS-mode and M-mode. The hypervisor is HS plus the firmware below it; the guest’s S-mode is below HS in the order §14.1.1 gives (“HS-mode is more privileged than VS-mode”), and the hardware routes every trap in V=1 up the chain in §14.1.6.2. Table 2 keeps that chain closed in HS’s favour (the Mechanism section’s delegation paragraph): chapter 5’s medeleg delegates neither code 10 nor 20–23, and chapter 19’s gated branch adds them, so in this book they are HS’s. What is added to the TCB is configuration, not code: every hstatus, hedeleg and medeleg value HS or M writes decides who sees a trap. A hedeleg[8] set by mistake delivers a guest’s user ecall to the guest kernel when the hypervisor meant to intercept it. The guest’s own S-mode, a whole kernel in a conventional design, falls out of the TCB entirely, and that is the point of Part IV: chapter 14’s unikernel was trusted as a whole image for its own isolation, and under hearth no part of it is trusted by the thing that hosts it.

Lab

A reading lab: nothing boots. labs/ch18/README.md has an empty table with the same ten rows as the Walkthrough, in the same order. For each row, fill in the three columns: what an unmodified S-mode kernel sees, which of the three outcomes (copy, virtual instruction, ordinary) the H-extension gives at V=1 and under which hstatus, mstatus or counter-enable bits, and the section of the privileged spec that says so. Read the spec pages from the links in Further reading; the Walkthrough’s table is the answer key, and a disagreement between your table and the spec’s text is a finding to report, not to paper over. Chapter 19 is the first lab that boots a guest on this hart.

Further reading

results matching ""

    No results matching ""