whp: implement safe wrappers for VM, vCPU, and instruction emulation#665
whp: implement safe wrappers for VM, vCPU, and instruction emulation#665lstocchi wants to merge 1 commit intocontainers:mainfrom
Conversation
d3579ef to
71a27dd
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a Windows Hypervisor Platform (WHP) backend for libkrun, implementing core functionality for VM partition management, vCPU execution, memory mapping, and instruction emulation. It also includes a mechanism for detecting host TSC frequency using CPUID or the Windows High-Resolution Performance Counter. Feedback was provided to improve the robustness of the CPUID-based frequency detection by verifying the maximum supported leaf before accessing specific leaves, ensuring better compatibility across various CPU architectures.
This commit introduces the foundational Rust abstractions for the Windows Hypervisor Platform (WHP) backend. It encapsulates the raw C FFI bindings into safe, idiomatic Rust structs (`WhpVm`, `WhpVcpu`, and `WhpEmulator`). Key features include: - Partition & vCPU Management: Safe wrappers for partition configuration, CPUID masking, memory mapping, and `WHvRunVirtualProcessor` exit routing. - High-Performance Register I/O: Introduces const-generic helpers (`get_registers<N>` and `set_registers<N>`) to guarantee stack-allocated, zero-overhead register reads/writes on the VM-exit hot path. - Robust TSC Calibration: Implements accurate host TSC frequency detection via CPUID 0x15/0x16, with a reliable `QueryPerformanceCounter` (QPC) calibration fallback for AMD/older hardware. - Emulation: Integrates WHP's built-in x86 emulator for MMIO and PIO handling. - Undocumented WHP Workarounds: Implements the `clear_halt_suspend` workaround using `WHvRegisterInternalActivityState`. This ports a known QEMU/crosvm fix that manually clears the `HaltSuspend` bit, preventing vCPUs from freezing when interrupts are injected via `WHvRequestInterrupt` while in a HLT state. Signed-off-by: lstocchi <lstocchi@redhat.com>
|
GitHub is broken so I can't tag the specific line in the code ("internal error") anyways here: // CPUID 0x15 — TSC / Core Crystal Clock (Intel SDM)
// TSC frequency in Hz = ECX * (EBX / EAX).
// We use a 1 Hz crystal with EBX = tsc_freq_hz to avoid rounding.
cpuid_results.push(WHV_X64_CPUID_RESULT {
Function: 0x15,
Reserved: [0; 3],
Eax: 1,
Ebx: tsc_freq_hz as u32,
Ecx: 1,
Edx: 0,
});I ran this PR through Claude Opus 4.6 and GPT-5.4 — both independently flagged
Footnotes
|
This commit introduces the foundational Rust abstractions for the Windows Hypervisor Platform (WHP) backend. I took inspiration from the hvf crate (you'll find
WhpVm,WhpVcpu, andWhpEmulator).Key features include:
WHvRunVirtualProcessorexit routing.get_registers<N>andset_registers<N>) to guarantee stack-allocated, zero-overhead register reads/writes on the VM-exit hot path.QueryPerformanceCounter(QPC) calibration fallback for AMD/older hardware.clear_halt_suspendworkaround usingWHvRegisterInternalActivityState. This ports a known QEMU/crosvm fix that manually clears theHaltSuspendbit, preventing vCPUs from freezing when interrupts are injected viaWHvRequestInterruptwhile in a HLT state.In a follow-up PR i'll add the hyper-v enlightements as this is already a big one.