Path: blob/master/tools/testing/selftests/kvm/x86/hyperv_evmcs.c
38235 views
// SPDX-License-Identifier: GPL-2.01/*2* Copyright (C) 2018, Red Hat, Inc.3*4* Tests for Enlightened VMCS, including nested guest state.5*/6#include <fcntl.h>7#include <stdio.h>8#include <stdlib.h>9#include <string.h>10#include <sys/ioctl.h>11#include <linux/bitmap.h>1213#include "test_util.h"1415#include "kvm_util.h"1617#include "hyperv.h"18#include "vmx.h"1920static int ud_count;2122static void guest_ud_handler(struct ex_regs *regs)23{24ud_count++;25regs->rip += 3; /* VMLAUNCH */26}2728static void guest_nmi_handler(struct ex_regs *regs)29{30}3132static inline void rdmsr_from_l2(uint32_t msr)33{34/* Currently, L1 doesn't preserve GPRs during vmexits. */35__asm__ __volatile__ ("rdmsr" : : "c"(msr) :36"rax", "rbx", "rdx", "rsi", "rdi", "r8", "r9",37"r10", "r11", "r12", "r13", "r14", "r15");38}3940/* Exit to L1 from L2 with RDMSR instruction */41void l2_guest_code(void)42{43u64 unused;4445GUEST_SYNC(7);4647GUEST_SYNC(8);4849/* Forced exit to L1 upon restore */50GUEST_SYNC(9);5152vmcall();5354/* MSR-Bitmap tests */55rdmsr_from_l2(MSR_FS_BASE); /* intercepted */56rdmsr_from_l2(MSR_FS_BASE); /* intercepted */57rdmsr_from_l2(MSR_GS_BASE); /* not intercepted */58vmcall();59rdmsr_from_l2(MSR_GS_BASE); /* intercepted */6061/* L2 TLB flush tests */62hyperv_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE | HV_HYPERCALL_FAST_BIT, 0x0,63HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES | HV_FLUSH_ALL_PROCESSORS);64rdmsr_from_l2(MSR_FS_BASE);65/*66* Note: hypercall status (RAX) is not preserved correctly by L1 after67* synthetic vmexit, use unchecked version.68*/69__hyperv_hypercall(HVCALL_FLUSH_VIRTUAL_ADDRESS_SPACE | HV_HYPERCALL_FAST_BIT, 0x0,70HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES | HV_FLUSH_ALL_PROCESSORS,71&unused);7273/* Done, exit to L1 and never come back. */74vmcall();75}7677void guest_code(struct vmx_pages *vmx_pages, struct hyperv_test_pages *hv_pages,78vm_vaddr_t hv_hcall_page_gpa)79{80#define L2_GUEST_STACK_SIZE 6481unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];8283wrmsr(HV_X64_MSR_GUEST_OS_ID, HYPERV_LINUX_OS_ID);84wrmsr(HV_X64_MSR_HYPERCALL, hv_hcall_page_gpa);8586x2apic_enable();8788GUEST_SYNC(1);89GUEST_SYNC(2);9091enable_vp_assist(hv_pages->vp_assist_gpa, hv_pages->vp_assist);92evmcs_enable();9394GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));95GUEST_SYNC(3);96GUEST_ASSERT(load_evmcs(hv_pages));97GUEST_ASSERT(vmptrstz() == hv_pages->enlightened_vmcs_gpa);9899GUEST_SYNC(4);100GUEST_ASSERT(vmptrstz() == hv_pages->enlightened_vmcs_gpa);101102prepare_vmcs(vmx_pages, l2_guest_code,103&l2_guest_stack[L2_GUEST_STACK_SIZE]);104105GUEST_SYNC(5);106GUEST_ASSERT(vmptrstz() == hv_pages->enlightened_vmcs_gpa);107current_evmcs->revision_id = -1u;108GUEST_ASSERT(vmlaunch());109current_evmcs->revision_id = EVMCS_VERSION;110GUEST_SYNC(6);111112vmwrite(PIN_BASED_VM_EXEC_CONTROL, vmreadz(PIN_BASED_VM_EXEC_CONTROL) |113PIN_BASED_NMI_EXITING);114115/* L2 TLB flush setup */116current_evmcs->partition_assist_page = hv_pages->partition_assist_gpa;117current_evmcs->hv_enlightenments_control.nested_flush_hypercall = 1;118current_evmcs->hv_vm_id = 1;119current_evmcs->hv_vp_id = 1;120current_vp_assist->nested_control.features.directhypercall = 1;121*(u32 *)(hv_pages->partition_assist) = 0;122123GUEST_ASSERT(!vmlaunch());124GUEST_ASSERT_EQ(vmreadz(VM_EXIT_REASON), EXIT_REASON_EXCEPTION_NMI);125GUEST_ASSERT_EQ((vmreadz(VM_EXIT_INTR_INFO) & 0xff), NMI_VECTOR);126GUEST_ASSERT(vmptrstz() == hv_pages->enlightened_vmcs_gpa);127128/*129* NMI forces L2->L1 exit, resuming L2 and hope that EVMCS is130* up-to-date (RIP points where it should and not at the beginning131* of l2_guest_code(). GUEST_SYNC(9) checkes that.132*/133GUEST_ASSERT(!vmresume());134135GUEST_SYNC(10);136137GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);138current_evmcs->guest_rip += 3; /* vmcall */139140/* Intercept RDMSR 0xc0000100 */141vmwrite(CPU_BASED_VM_EXEC_CONTROL, vmreadz(CPU_BASED_VM_EXEC_CONTROL) |142CPU_BASED_USE_MSR_BITMAPS);143__set_bit(MSR_FS_BASE & 0x1fff, vmx_pages->msr + 0x400);144GUEST_ASSERT(!vmresume());145GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_MSR_READ);146current_evmcs->guest_rip += 2; /* rdmsr */147148/* Enable enlightened MSR bitmap */149current_evmcs->hv_enlightenments_control.msr_bitmap = 1;150GUEST_ASSERT(!vmresume());151GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_MSR_READ);152current_evmcs->guest_rip += 2; /* rdmsr */153154/* Intercept RDMSR 0xc0000101 without telling KVM about it */155__set_bit(MSR_GS_BASE & 0x1fff, vmx_pages->msr + 0x400);156/* Make sure HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP is set */157current_evmcs->hv_clean_fields |= HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;158GUEST_ASSERT(!vmresume());159/* Make sure we don't see EXIT_REASON_MSR_READ here so eMSR bitmap works */160GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);161current_evmcs->guest_rip += 3; /* vmcall */162163/* Now tell KVM we've changed MSR-Bitmap */164current_evmcs->hv_clean_fields &= ~HV_VMX_ENLIGHTENED_CLEAN_FIELD_MSR_BITMAP;165GUEST_ASSERT(!vmresume());166GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_MSR_READ);167current_evmcs->guest_rip += 2; /* rdmsr */168169/*170* L2 TLB flush test. First VMCALL should be handled directly by L0,171* no VMCALL exit expected.172*/173GUEST_ASSERT(!vmresume());174GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_MSR_READ);175current_evmcs->guest_rip += 2; /* rdmsr */176/* Enable synthetic vmexit */177*(u32 *)(hv_pages->partition_assist) = 1;178GUEST_ASSERT(!vmresume());179GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == HV_VMX_SYNTHETIC_EXIT_REASON_TRAP_AFTER_FLUSH);180181GUEST_ASSERT(!vmresume());182GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);183GUEST_SYNC(11);184185/* Try enlightened vmptrld with an incorrect GPA */186evmcs_vmptrld(0xdeadbeef, hv_pages->enlightened_vmcs);187GUEST_ASSERT(vmlaunch());188GUEST_ASSERT(ud_count == 1);189GUEST_DONE();190}191192void inject_nmi(struct kvm_vcpu *vcpu)193{194struct kvm_vcpu_events events;195196vcpu_events_get(vcpu, &events);197198events.nmi.pending = 1;199events.flags |= KVM_VCPUEVENT_VALID_NMI_PENDING;200201vcpu_events_set(vcpu, &events);202}203204static struct kvm_vcpu *save_restore_vm(struct kvm_vm *vm,205struct kvm_vcpu *vcpu)206{207struct kvm_regs regs1, regs2;208struct kvm_x86_state *state;209210state = vcpu_save_state(vcpu);211memset(®s1, 0, sizeof(regs1));212vcpu_regs_get(vcpu, ®s1);213214kvm_vm_release(vm);215216/* Restore state in a new VM. */217vcpu = vm_recreate_with_one_vcpu(vm);218vcpu_set_hv_cpuid(vcpu);219vcpu_enable_evmcs(vcpu);220vcpu_load_state(vcpu, state);221kvm_x86_state_cleanup(state);222223memset(®s2, 0, sizeof(regs2));224vcpu_regs_get(vcpu, ®s2);225TEST_ASSERT(!memcmp(®s1, ®s2, sizeof(regs2)),226"Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",227(ulong) regs2.rdi, (ulong) regs2.rsi);228return vcpu;229}230231int main(int argc, char *argv[])232{233vm_vaddr_t vmx_pages_gva = 0, hv_pages_gva = 0;234vm_vaddr_t hcall_page;235236struct kvm_vcpu *vcpu;237struct kvm_vm *vm;238struct ucall uc;239int stage;240241TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX));242TEST_REQUIRE(kvm_has_cap(KVM_CAP_NESTED_STATE));243TEST_REQUIRE(kvm_has_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS));244TEST_REQUIRE(kvm_hv_cpu_has(HV_X64_NESTED_DIRECT_FLUSH));245246vm = vm_create_with_one_vcpu(&vcpu, guest_code);247248hcall_page = vm_vaddr_alloc_pages(vm, 1);249memset(addr_gva2hva(vm, hcall_page), 0x0, getpagesize());250251vcpu_set_hv_cpuid(vcpu);252vcpu_enable_evmcs(vcpu);253254vcpu_alloc_vmx(vm, &vmx_pages_gva);255vcpu_alloc_hyperv_test_pages(vm, &hv_pages_gva);256vcpu_args_set(vcpu, 3, vmx_pages_gva, hv_pages_gva, addr_gva2gpa(vm, hcall_page));257vcpu_set_msr(vcpu, HV_X64_MSR_VP_INDEX, vcpu->id);258259vm_install_exception_handler(vm, UD_VECTOR, guest_ud_handler);260vm_install_exception_handler(vm, NMI_VECTOR, guest_nmi_handler);261262pr_info("Running L1 which uses EVMCS to run L2\n");263264for (stage = 1;; stage++) {265vcpu_run(vcpu);266TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO);267268switch (get_ucall(vcpu, &uc)) {269case UCALL_ABORT:270REPORT_GUEST_ASSERT(uc);271/* NOT REACHED */272case UCALL_SYNC:273break;274case UCALL_DONE:275goto done;276default:277TEST_FAIL("Unknown ucall %lu", uc.cmd);278}279280/* UCALL_SYNC is handled here. */281TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&282uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",283stage, (ulong)uc.args[1]);284285vcpu = save_restore_vm(vm, vcpu);286287/* Force immediate L2->L1 exit before resuming */288if (stage == 8) {289pr_info("Injecting NMI into L1 before L2 had a chance to run after restore\n");290inject_nmi(vcpu);291}292293/*294* Do KVM_GET_NESTED_STATE/KVM_SET_NESTED_STATE for a freshly295* restored VM (before the first KVM_RUN) to check that296* KVM_STATE_NESTED_EVMCS is not lost.297*/298if (stage == 9) {299pr_info("Trying extra KVM_GET_NESTED_STATE/KVM_SET_NESTED_STATE cycle\n");300vcpu = save_restore_vm(vm, vcpu);301}302}303304done:305kvm_vm_free(vm);306}307308309