,

Contents ยท Virtualization (KVM, Xen, Hyper-V)


Virtualization models and hardware support

  • Type-1 vs Type-2: bare-metal hypervisor vs hosted (hypervisor as kernel module).
  • Full virtualization: unmodified guests using CPU traps and device emulation.
  • Paravirtualization: guest-aware hypercalls and paravirt drivers to avoid traps/emulation.
  • Hardware extensions: Intel VT-x/AMD-V for non-root mode, EPT/NPT for nested paging, VT-d/IOMMU for device passthrough.
// Address translation layers (conceptual)
// Guest VA -> Guest PA (guest page tables) -> Host PA (EPT/NPT) -> DRAM
function nestedTranslate(guestVA){ /* walk guest then host tables */ }

KVM (Kernel-based Virtual Machine)

  • KVM turns Linux into a type-2 hypervisor using VT-x/AMD-V; userspace (QEMU) provides device emulation.
  • vCPU threads scheduled by Linux; KVM exposes ioctls to run guest code and handle exits (MMIO, CPUID, IO).
  • Memory: userspace maps guest RAM; EPT/NPT maps to host physical; ballooning adjusts footprints.
// Simplified KVM run loop (conceptual)
while (true) {
  const exit = kvmRun(vcpu);
  switch(exit.reason){
    case 'IO': /* emulate */ break;
    case 'MMIO': /* device access */ break;
    case 'HLT': /* idle */ break;
    // ...
  }
}

Xen

  • Microkernel hypervisor with Dom0 (privileged control domain) and DomU (guests).
  • PV (paravirtual) and HVM (hardware virtualized) guest modes; PVH blends PV drivers with HVM CPU.
  • Split drivers: backend in Dom0, frontend in guests; grant tables and event channels for I/O.

Hyper-V

  • Type-1 hypervisor integrated with Windows; partitions instead of domains; enlightened guests via VMBus.
  • Features: Dynamic Memory, SR-IOV, Generation 2 VMs (UEFI), shielded VMs with vTPM.
  • Management: Hyper-V Manager, PowerShell, and integration with SCVMM/Azure Stack.

Virtual devices and paravirtualization

  • Device emulation (QEMU) is flexible but slow; paravirt drivers (virtio, Xen PV, Hyper-V) are faster.
  • Device passthrough with VT-d/IOMMU gives near-native perf; SR-IOV provides virtual functions for NICs.
  • Live migration requires shared storage or pre-copy of RAM and device state; passthrough complicates it.

virtio and vhost

  • virtio: standard paravirt devices with descriptor rings (virtqueues).
  • vhost: kernel acceleration moving virtqueue handling from QEMU to kernel threads.
  • virtio-net/blk/scsi/fs/gpu; modern features: packed rings, multiqueue, mergeable buffers.
// Virtqueue descriptor idea (conceptual)
class VirtqDesc { constructor(addr, len, flags, next) { Object.assign(this, {addr,len,flags,next}); } }

Performance, NUMA, and scheduling

  • Pin vCPUs to pCPUs; align with host NUMA nodes; reserve huge pages for guest RAM to reduce TLB pressure.
  • Use virtio and multiqueue; avoid emulation; passthrough and SR-IOV for critical I/O.
  • Balance overcommit: CPU steal time, memory ballooning, and I/O queueing impact guest latency.

Exercises

  1. Launch a KVM guest with virtio-net/blk and measure throughput vs. e1000 emulation.
  2. Configure SR-IOV on a NIC and attach a VF to a VM; compare latency to virtio-net and emulation.
  3. Perform a live migration; profile downtime and total transfer time with memory dirty rates.
Combine hardware virtualization with paravirt devices and careful NUMA/scheduling to get near bare-metal performance.