Skip to content

Latest commit

 

History

History
271 lines (246 loc) · 17 KB

execution.md

File metadata and controls

271 lines (246 loc) · 17 KB

Three ways to execute a guest

The interpreter, the translation IR and its backends, and hardware acceleration — what each is, what it is measured at, and what it promises. The design notes are under techniques/.

The interpreter is the oracle, always: every other engine is differentially tested against it, and tests/riscv_virt_engines.rs asserts identical state hashes at ten checkpoints across all three plus a snapshot restored across an engine switch.

The translation IR landed first — the architecture-neutral op set, typed SSA blocks, the guest-instruction-boundary markers that make a mid-block fault deliverable at the right PC with the right cycle count, a verifier, liveness and dead-code elimination, and a portable interpreter backend that needs no unsafe and runs on every target including bare metal. Three architectures have frontends now — RISC-V, x86 and AArch64 — and each of those cores takes an engine property with three values: interp, jit (the portable backend), and jit-host (native code). RISC-V takes a fourth, jit-wasm, which lowers each block to a WebAssembly module instead.

AArch64 has a frontend means an AArch64 guest is lowered to host code — which is not the same claim as having an aarch64 code generator. There are three backends: jit::x86 (x86-64 Linux), jit::arm64 (aarch64 Linux, written on an x86-64 machine and executed by the aarch64 JIT workflow's native runner) and jit::wasm (engine = "jit-wasm", a WebAssembly.Module per block). Every number in the table below was taken on x86-64 Linux under jit-host, so it says nothing about the other two. Each row below is the median of interleaved runs of a real Linux boot on that board, and every run in a row finished on one state hash:

Engine RISC-V (riscv-virt, 240 s of guest time) x86 (pc64, 900 s) AArch64 (arm64-virt, 20 s)
interp 122.3 s 276.5 s 18.94 s
jit 103.7 s (1.18×) 184.3 s (1.50×) 10.42 s (1.82×)
jit-host 56.8 s (2.15×) 86.4 s (3.20×) 3.37 s (5.61×)

All three engines produce byte-identical guest output — 653 console lines on the x86 run, ending 900,000 virtual milliseconds in at the same CS:RIP, CR2, CR3, CR4, EFER and flags, having executed the same blocks from the same translations.

ARMv7-M has a frontend too (cpu-arm-v7m-lift; engine = "jit" or "jit-host" on cpu.arm.v7m, and -p engine=… on the STM32L4, F205, F407, F427 and H743 boards). Its host is the interpreter itself: every access, fault and interworking branch is exec's own code, and each instruction boundary closes the interpreter's step (SysTick, DWT_CYCCNT, SEVONPEND, the idle tracker) — so idle skipping, the scheduler's mid-round stops, device stalls and the firewall's pass-through are exact by construction. src/cpu/arm/v7m/liftdiff.rs holds all three engines to one snapshot after every budget over random and directed programs and the built conformance corpus; the L4 idle-skip tests do the same for whole machines. Four things keep that exact and still cheap (#87): a run of boundaries with no access between them is closed with one epilogue (IrHost::stretch, as far as SysTick, the allowance and the idle tracker's back-off allow); an aligned access to plain RAM is served from generated code through a software TLB and then pays the interpreter's cycle, publish and scheduler question; the registers live in a slot array generated code reads and writes in place; and jit::x86 lowers UDIV/SDIV (a zero divisor still leaves the block for the interpreter, which owns CCR.DIV_0_TRP). The FPv4-SP/FPv5-SP instructions and the DSP extension are lifted too, their arithmetic a pure helper call (ir::HELPER_PURE) into the interpreter's own function with FPSCR, Q and GE passed through, so results and cumulative flags are the interpreter's on every host; ExecuteFPCheck (NOCP, lazy state preservation, CONTROL.FPCA) stays the interpreter's, through the block cache's key. Measured under jit-host on x86-64 (benches/v7m_dispatch.rs, run_budget, guest cycles a wall second against the interpreter): ALU loop 4.2×, the same under a 1 kHz SysTick 3.6×, RAM loads and stores 1.5×, calls and returns 1.1×, a device-register poll 1.2×, divides 2.3×, single-precision FP 1.3× (0.64× before it was lifted), a DSP filter loop 2.0× (0.63× before); the portable jit replays every boundary on ir::Interp and is three to four times slower than the interpreter. On benches/l4_status_polls.rs, whose time is in its device models, jit-host runs about 5% fewer host instructions than before this work and its wall clock is within noise of the interpreter's. On the STM32L496 and L4S5, whose core sits behind the firewall's filter, the software TLB fills through the filter's pass-through wherever it publishes a whole page clear (core::space's passage), so the RAM rows there match the memory map's (--bus fw: RAM loads and stores 119 → 157 M cycles a second, calls 114 → 137) and every verdict the firewall still has to give — the call gate, a closed segment, a fetch that may close it — reaches the filter. The interpreter stays the default.

Because the translated engines are a speed knob, the engine property never picks one slower than the interpreter (Engine::effective, #128): jit — the portable backend, a quarter to two fifths of the interpreter's speed — runs the interpreter, and so does jit-host on a build without a host backend that compiles divides and pure helper calls (macOS, Windows, wasm), where it would be that same portable backend. A backend without those two still beats the interpreter on plain arithmetic but measured 0.66–0.74× of it on the divide, DSP and floating-point guests, so it is not used either. ArmV7m::with_engine still takes the engine it is given.

jit::arm64 has the x86 backend's five lift features too — divides, pure helper calls, in-place slot reads, exits that store their own boundary and the self-linked one-block loop — so jit-host is native on aarch64 Linux. On GitHub's arm64 runner (v7m_dispatch --no-step, guest Mcycles per wall second against the interpreter): ALU loop 8.7×, under SysTick 7.7×, divides 2.9×, the DSP loop 3.1×, RAM 2.2×, calls 1.6×, FP 1.7×, a device poll 1.4× (1.2× behind the firewall). macOS still has no host backend (MAP_JIT and pthread_jit_write_protect_np are outside the dependency policy), so there jit-host is the interpreter.

A block now leaves on its tick allowance instead of being refused for a bound it might have exceeded. A block used to have to prove its worst case fitted what remained of the scheduler quantum, so the last part of every quantum admitted nothing — and two independent profiles found that guard, not the unlifted encodings, was the dominant cost of interpretation. With IrHost::spent asked at each instruction boundary, the guard's share is zero:

before after
x86 retired in blocks 97.3% 99.3% (12,248,632 interpreted, was 48,592,900)
AArch64 retired in blocks 97.5% 99.4% (6,012,343 interpreted, was 26,356,169)

What is left on x86 is the exclusion list plus 0.35 M interrupt shadows and pins. On AArch64 it is 6.0 M outside the lifted subset, 1,614 first-sighting lifts and 292 interrupts. 99.8% of compiled RISC-V stores still write guest RAM inline rather than through a call (1,749,886 of 1,753,140). The RISC-V headline fell from 2.28× to 2.15× along the way, because the interpreter it is measured against got 1.27× faster (155.1 s → 122.3 s) and the control moved; the numbers and that argument are in docs/platforms/riscv-virt.md, docs/platforms/pc64.md and docs/platforms/arm64-virt.md.

The wasm backend runs in a browser engine now, and it wins. engine = "jit-wasm" lowers each IR block to a WebAssembly module and executes it, on every target, reaching the same state hash as the interpreter at every checkpoint of tests/riscv_virt_engines.rs. On a native host the only thing that can run one of those modules is a reference wasm interpreter, which makes jit-wasm the slowest of the four engines there and is the point: the translation is executed and hashed everywhere rather than only where a browser is. On wasm32-unknown-unknown the page's own engine compiles them, through three imports and four exports (web/src/jit.js), and the same guest for the same span comes out:

Engine native (reference executor) in V8 (node 26)
interp 1064 ms, 1.00× 922 ms, 1.00×
jit 517 ms, 2.06× 840 ms, 1.10×
jit-host 85 ms, 12.50× — (there is no mmap in a browser)
jit-wasm 1392 ms, 0.76× 613 ms, 1.50×

ROADMAP.md §11.4 expected the backend to win "only on long-running superblocks, and may not win at all"; it wins per basic block, with no chaining and no superblocks. It is well short of jit-host's 12.5×, and docs/techniques/wasm-jit.md says which three mechanisms would move it and why none was worth building before this number existed. Both columns come from one function — benches/wasm_jit_embedder.rs and web/check.mjs §1c call it — so they are one workload rather than two with one name, and the browser column is asserted to hash identically to the interpreter's on every commit.

Two caveats on all of the above. engine is a param on the seven boards that run third-party system software — riscv-virt, arm64-virt, arm64-virt-smp, pc64, q35-linux, q35-linux-smp and q35-uefi — so rsemu run -p engine=jit-host picks it from the command line there. It is still a literal on pc-at, pc-at-smp, pc-apic, q35 and a64-mini, which are therefore still interpreted whatever you pass. And the table above is informative rather than gating: docs/bench-host.md now names the reference host, but these ratios were not taken under the discipline the benchmark harness applies — interleaved sides, a minimum over repetitions, a run-to-run spread beside every figure. One set of numbers in this repository was, and it is the next paragraph.

And rsemu has been measured against something other than itself. Every figure above compares rsemu with rsemu, which can say a change made things faster and can never say whether the result is fast. docs/testing/benchmarks.md is the external comparison phase 8's gate asks for: the same kernel, the same initramfs and the same five workloads under rsemu and under qemu-system-…, timed by markers the guest prints, with QEMU run as a measuring instrument and never read (that is a licence rule, not a preference — see below). The answer is about twenty times QEMU's wall clock on arm64-virt and about a hundred and ten on pc64, against a gate of 2×, and the interesting part is where the distance lives: the code the JIT generates is under a tenth of a boot's host instructions on all three cores — 7.32%, 8.02% and 8.22% where it has been split out. So what the gate measures is the runtime around the generated code: the address space, the deferred-charge replay, the block boundary, the scheduler round. ROADMAP.md phase 8 is that list, in the order the profiles put it.

Hardware acceleration is real, and it is KVM on Linux x86-64. rsemu run q35-linux --media kernel=bzImage --accel kvm boots that same stock Gentoo 6.6.67 kernel to a busybox shell in 2.4 seconds of wall clock against 978 s interpreted — 2,826 seconds of guest time either way — on the board's own default command line: the no_timer_check this paragraph used to carry is gone, and so is the defect it was hiding. 282 of the accelerated run's 346 console lines are byte-identical to the interpreted run's, in the same order, once the printk timestamp is removed; the 62 that differ are the ones that describe the host processor, its mitigations, its XSAVE list and its TLB geometry. On a pure-execution workload the ratio to native is 99.7%, 99.9% and 101.2% across three runs, against a phase-7 gate of 80%.

What made the command line honest is ThreadingMode::Accel, which is now implemented: a scheduler round's elapsed virtual time is read off the host clock rather than counted out of the board's oscillators, and a periodic per-thread timer bounds a guest that takes no exits at all. Before it, virtual time did not advance while a vCPU was inside KVM_RUN, so a kernel calibrating its time-stamp counter against a board timer concluded it was on a 176,273 MHz processor; it now reports 3,992.968 MHz on a 3,993,994 kHz host. That is also what unblocked q35-linux-smp, the two-processor version of the same board, on which the kernel prints smp: Brought up 1 node, 2 CPUs and nproc says 2.

--accel is a host flag rather than a machine-file value, deliberately: engine = "interp" and engine = "jit" are two implementations of the same processor and their state hashes match, while a vCPU answers CPUID from the host's silicon, cannot be replayed, and only exists on Linux/x86-64 — so a board that named it would be a board that does not build on a Mac. The file is used verbatim either way and what is accelerated is the run. It is not reproducible: no state hash, no --record-input. HVF and WHPX are roadmap entries with no code behind them.

Level 3 — user-mode execution — has its proof, on two architectures: a static musl Rust binary, built by scripts/fetch-testdata.sh for riscv64gc-unknown-linux-musl and aarch64-unknown-linux-musl and never committed, runs through musl's own _start and __libc_start_main — AT_PHDR, thread-local storage, a brk heap — reaches main, prints, and exits 0, with no syscall refused. The same hello makes the same twenty-five calls in the same order on both. The run is then replayed with the entropy source replaced by a panicking guard, and produces identical output and an identical tick count.

Threaded std Rust guests run too: clone, futex WAIT/WAKE, set_tid_address and CLONE_CHILD_CLEARTID — which together are the whole of pthread_join — carry four workers hammering one atomic and three threads on a condition variable, written with no knowledge of the emulator. It is also what found the tree's largest correctness defect: the exclusive monitor was core-local, so an AArch64 AtomicU32::fetch_add loop landed 32,038 of 40,000 increments. It lands 40,000 now (see the SMP section above), and the same guest is what proves it.

And a whole C library runs, on both architectures. The same hello, linked against glibc instead of statically against musl and run under that library's own ld.so: a PT_INTERP is read, the interpreter is mapped and entered with no relocations applied, it opens and relocates the objects its DT_NEEDED names, resolves the ifuncs glibc picks its memcpy and strlen with, and transfers control — 59 syscalls on aarch64 and 62 on riscv64. Threaded, it is 205 and 204, and it found something musl never asks for: glibc's pthread_create tries clone3 first and falls back to the five-register clone on -ENOSYS.

Software nobody here wrote runs on it too — three programs picked for what they ask of the ABI rather than for fame, all permissive, all cross-built unmodified at a pinned version by scripts/fetch-testdata.sh and never committed: SQLite 3.45 (public domain), Lua 5.4.7 (MIT) and sbase (MIT). SQLite makes the same ninety-three calls in the same order on both architectures and prints the same rows; sbase's sha256sum, wc and cksum are diffed against the host's over the same bytes, which is what makes it different in kind from a test that only checks a program did not crash. They are what found four holes, every one of them in the consumer's half: mstatus.FS left Off, and missing readv, pread64 and fcntl.

Fifty-two syscall numbers are dispatched — hello makes 25 of them on either architecture and the threaded musl guest 166 — and the hard rule is unchanged: a level-3 guest may be told about itself, and may not be told about the host. What that means has been sharpened rather than relaxed. There is still no --allow flag and none planned, and the property is now mechanical rather than argued: nothing that services a syscall links std, which a CI feature-combination job builds on every commit. A guest reaches files through a stage — a map from guest path to bytes the harness fixes before the guest exists — so a program that is told a path runs and a program that discovers paths does not: ls fails with lstat /work: No such file or directory, because there is no getdents64 and no notion of a directory, and that is the answer rather than a gap. rsemu builds the machine half only; the kernel half is nixvm's (ROADMAP.md §2.1), which is why all of this lives under #[cfg(test)] and none of it is public API. docs/system/usermode-abi.md has the syscall table and the differential traces against a host strace.