kvm-machine

Implements the KVM machine layer for VM creation, vCPU execution, and VM exit handling in Rust.

Updated Aug 18, 2026
One-click install
npx skills add https://github.com/doctorspider42/entangled-destop --skill kvm-machine-doctorspider42
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kvm-machine
Source: https://github.com/doctorspider42/entangled-destop/tree/main/.agents/skills/kvm-machine
Command: npx skills add https://github.com/doctorspider42/entangled-destop --skill kvm-machine-doctorspider42

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building a virtual machine monitor from scratch on Linux KVM involves dozens of subtle kernel API rules — memory slot registration, IRQ chip ordering, vCPU reset state, snapshot restore order — where mistakes cause silent corruption or triple faults. This Skill encodes the proven implementation order, safety rules, and hard-won pitfalls for the Entangled Desktop KVM machine layer so work on vmm-core and machine-x86 stays correct. ## Core Features & Use Cases - VM and vCPU bring-up guidance: Covers create_vm, KVM_SET_USER_MEMORY_REGION from the E820 map, in-kernel IRQ chip and PIT2 creation before vCPUs, CPUID filtering, and long-mode register setup per the Linux boot protocol. - KVM_RUN loop and lifecycle rules: Explicit VM exit matching, clean shutdown via KVM_CAP_IMMEDIATE_EXIT, crash reporting through VmState, and leak-free create/destroy cycles. - Advanced hypervisor mechanics: Documents vCPU in-place reset ordering (ADR-0005), snapshot/restore of vCPU and in-kernel IRQ chips (ADR-0006), dirty page logging limitations, and virtio shared-memory window slot management. - Use Case: When adding a new KVM capability or debugging a guest that triple-faults after reboot, load this Skill to get the exact ioctl ordering and safety constraints instead of rediscovering them from kernel source. ## Quick Start Load this skill before modifying any code in crates/vmm-core or crates/machine-x86 that touches kvm-ioctls or kvm-bindings.

Frequently Asked Questions about kvm-machine

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a KVM virtual machine in Rust?

Open /dev/kvm, validate API version 12 and required capabilities, call create_vm, then register each RAM region with KVM_SET_USER_MEMORY_REGION backed by vm-memory's GuestMemoryMmap. Create the in-kernel IRQ chip and PIT2 before creating any vCPUs.

How do I set up a KVM vCPU for 64-bit Linux boot?

Filter KVM_GET_SUPPORTED_CPUID results, then set sregs for long mode: flat GDT descriptors, CR0.PE|PG, CR4.PAE, EFER.LME|LMA with identity-mapped page tables. Set regs with rip at the kernel entry, rsi pointing to the zero page, and rflags equal to 2.

Why does a rebooted KVM guest triple-fault after reset?

A stale APIC timer from the previous kernel fires before the new boot installs an IDT. Reset must clear vCPU events, restore a power-on LAPIC page, rewrite sregs and regs, then set MP_STATE before re-entering KVM_RUN.

Does KVM dirty page logging track writes made by the VMM process?

No, KVM dirty logging only tracks guest writes through second-level page tables. Host writes like boot image copies or virtio completions never set dirty bits, so it cannot be used alone for incremental snapshots.

What is the correct restore order for a KVM vCPU snapshot?

Restore mp_state first, then KVM_SET_LAPIC, sregs, regs, XCRS, XSAVE, MSRs, debug registers, and finally KVM_SET_VCPU_EVENTS. In-kernel IRQ chips and PIT state restore after devices, with the IOAPIC last.

Why must the KVM IRQ chip be created before vCPUs?

KVM_CREATE_PIT2 fails if vCPUs already exist, so the IRQ chip and PIT must be created immediately after VM creation. This ordering is a kernel requirement, not a design choice.