A Deep Dive into Virtualization, Linux KVM and Low-Level Systems Engineering
Preface
- Who This Book Is For
- How This Book Is Organized
- The Central Thesis
- Conventions
- Building the Project
Part I: Foundations of Virtualization
Chapter 1: The Case for MicroVMs
- From Bare Metal to Serverless: The Demand for Isolation
- Containers, VMs, Unikernels and the MicroVM Sweet Spot
- What Makes a MicroVM a MicroVM
- Historical Context: Xen, KVM and the Rise of Firecracker
- The Promise of This Book
Chapter 2: Privilege Levels and CPU Virtualization
- The Problem: Protecting the Hypervisor
- x86 Privilege Levels and Ring 0
- Intel VT-x: VMX Root and Non-Root Operation
- AMD-V: SVM and Its Differences from VMX
- The VMCS and VMCB: Per-Virtual-CPU State
- VM Entry and VM Exit: The Fundamental Primitive
- Summary
Chapter 3: Memory Virtualization
- The Problem: Two Operating Systems, One Memory
- Guest Virtual to Guest Physical: The Guest’s Own MMU
- EPT and NPT: Second-Level Address Translation
- Memory Regions and KVM Memory Slots
- Huge Pages and Memory Performance
- Copy-on-Write and Memory Overcommit
- Summary
Chapter 4: I/O Virtualization and virtio
- The Problem: Devices in a Virtual Machine
- Port I/O and Memory-Mapped I/O
- Device Emulation: Why QEMU Is Slow
- Paravirtualization and the virtio Philosophy
- The virtio Feature Bit Negotiation Protocol
- virtio Ranges, Device Types and Modern virtio
- Summary
Chapter 5: Interrupts, Timers and the APIC
- The Problem: Delivering Events to the Guest
- The x86 Interrupt Architecture: PIC to APIC
- Virtualizing Interrupts: Posted Interrupts and Virtual Wire Mode
- The KVM PIC and IOAPIC Emulation
- Timers: TSC, PIT, HPET and the virtio Clock
- Interrupt Routing and MSI/MSI-X
- Summary
Part II: Linux KVM in Depth
Chapter 6: The KVM Architecture
- KVM as a Linux Kernel Module
- The /dev/kvm Interface and File Descriptor Model
- Kernel vs Userspace Responsibilities
- KVM Architecture on x86: VMX and SVM Modules
- Kernel Versions, Capabilities and Feature Detection
- Summary
Chapter 7: The KVM Userspace API
- The KVM ioctl Taxonomy: /dev/kvm, VM, vCPU, Device
- Creating VMs and vCPUs: KVM_CREATE_VM, KVM_CREATE_VCPU
- Register Access: KVM_GET_REGS, KVM_GET_SREGS, KVM_GET_LAPIC
- Interrupt Injection: KVM_IRQFD, KVM_IRQ_LINE
- Signal Handling, Scheduling and vCPU Threads
- Summary
Chapter 8: KVM Memory Management and MMU
- Memory Slots and KVM_SET_USER_MEMORY_REGION
- Memory Mapping with mmap and KVM
- The KVM Shadow Page Table (EPT/NPT) Management
- Huge Pages in KVM: 2MB and 1GB Pages
- Memory Hotplug and Dynamic Memory
- Summary
Part III: Building the MicroVM in Rust
Chapter 9: Project Architecture and Rust for Systems
- Project Layout and Cargo Configuration
- Rust for Systems Programming: Ownership, Lifetimes and Unsafe
- Error Handling Strategies in Systems Code
- The VMM Module Map
Chapter 10: Creating the Virtual Machine
- Opening /dev/kvm and Checking Capabilities
- Allocating Guest Memory with mmap
- Registering Memory Slots with KVM
- Verifying the VM State
- Summary
Chapter 11: Creating and Running vCPUs
- Creating vCPU Threads and File Descriptors
- Initializing CPU State: GPRs, Segment Registers, MSRs
- The vCPU Run Loop and KVM_RUN
- Handling VM Exits in Rust
- Multi-vCPU Synchronization and Shutdown
- Summary
Chapter 12: Booting a Linux Kernel
- The Linux Kernel Boot Protocol
- Loading the Kernel into Guest Memory
- Entering the Kernel: Long-Mode CPU State
- First Boot: Getting a Kernel to Run
- Summary
Chapter 13: The Device Framework and Serial Console
- The VMM Device Trait and Abstraction
- Port I/O Handling: The KVM_EXIT_IO Path
- Implementing a 16550 UART for Console Output
- Memory-Mapped I/O Regions in Rust
- Device Registration and Address Spaces
- Summary
Chapter 14: Virtio Foundations
- The virtio Transport Model
- Descriptor Rings, Available Rings and Used Rings
- Feature Negotiation in Practice
- Implementing VirtQueue in Rust
Chapter 15: Virtio Block Device
- The virtio-blk Configuration and Headers
- Request Types: Read, Write, Flush, GetID
- The Block Device Backend: File I/O
- Integrating Virtio-Blk with the Kernel Command Line
- Performance Considerations and AIO
- Summary
Chapter 16: Virtio Networking
- The virtio-net Configuration and Packet Headers
- Network Backends: TAP Devices and Network Namespaces
- Packet Flow: Guest to Host and Back
- Multi-Queue Networking and RSS
- Multi-Queue Networking and RSS
- Network Performance and Buffering
- Wiring the Net Device into the VMM
- Summary
Chapter 17: Interrupt Delivery and the Event Loop
- Interrupts in a Virtual Machine
- KVM_IRQFD and EventFD-Based Interrupts
- Building the VMM Event Loop with epoll
- Multiplexing vCPU, Device and Control Events
- The Virtio Interrupt Signaling Path
- Timer and Clock Virtualization
- Summary
Part IV: Advanced Engineering
Chapter 18: Concurrency, Async I/O and Performance
- Concurrency in Virtual Machine Monitors
- Async I/O: epoll vs io_uring
- vCPU Affinity, Pinning and NUMA
- Profiling and Performance Benchmarking
- Summary
Chapter 19: Memory Management Deep Dive
- Guest Memory Layout and the Physical Address Space
- Huge Pages for the Guest: 2MB and 1GB
- Copy-on-Write Cloning for Fast Startup
- Memory Hotplug and Dynamic Sizing
- Ballooning and Memory Reclaim
- Summary
Chapter 20: Security Engineering for MicroVMs
- The MicroVM Threat Model: Untrusted Guests
- Attack Surfaces: KVM, Devices, Memory, vCPU
- Process Sandboxing: Namespaces, Capabilities, chroot
- seccomp-BPF Filters: Restricting System Calls
- cgroups and Resource Isolation
- Guest Escape Risks and Mitigations
- Summary
Part V: Production and Landscape
Chapter 21: Production VMM Architecture
- From Prototype to Production
- API Server and Instance Lifecycle Management
- Configuration, Validation and Defaults
- Structured Logging, Metrics and Tracing
- Graceful Shutdown and Crash Recovery
- Summary
Chapter 22: Snapshots, Checkpoints and Restore
- What Makes a VM Snapshot
- Device State Serialization in Rust
- Memory Snapshotting and CoW
- Restore: Reconstructing a Running VM
- Limitations and Open Problems
- Summary
Chapter 23: The VMM Landscape: Firecracker, Cloud Hypervisor, crosvm and QEMU
- Why Study Other VMMs
- Firecracker: The Security-Focused MicroVM
- Cloud Hypervisor: The Full-Featured MicroVM
- crosvm: The Chrome OS VMM
- QEMU: The General-Purpose Emulator
- Comparative Analysis
- Lessons Learned from the Landscape
- Summary
Chapter 24: Future Directions and Conclusion
- Emerging Technologies in Virtualization
- Conclusion: The Art and Science of Building a MicroVM
- Final Thoughts
References
- Official Documentation and Specifications
- Research Papers and Technical Articles
- Source Code Repositories
- Hardware Manuals and Datasheets
- Online Resources
Appendix A: Complete Project Structure
- Cargo.toml
Appendix B: KVM ioctl Quick Reference
Appendix C: Boot Parameters Reference
- setup_header (at offset 0x1F1 in the bzImage header)
- screen_info (at offset 0 in boot_params)
- e820_mem_map (setup_data, type 1)
Appendix D: virtio Register Map
- virtio-blk Configuration
- virtio-net Configuration
Appendix E: Glossary
Appendix F: Index
- A
- B
- C
- D
- E
- F
- G
- H
- I
- K
- L
- M
- N
- P
- Q
- R
- S
- T
- U
- V
- W
- X
- Y
- Z