1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018, Red Hat, Inc.
5 * Tests for Enlightened VMCS, including nested guest state.
7 #define _GNU_SOURCE /* for program_invocation_short_name */
12 #include <sys/ioctl.h>
14 #include "test_util.h"
22 void l2_guest_code(void)
28 /* Done, exit to L1 and never come back. */
32 void l1_guest_code(struct vmx_pages *vmx_pages)
34 #define L2_GUEST_STACK_SIZE 64
35 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
37 enable_vp_assist(vmx_pages->vp_assist_gpa, vmx_pages->vp_assist);
39 GUEST_ASSERT(vmx_pages->vmcs_gpa);
40 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
42 GUEST_ASSERT(load_vmcs(vmx_pages));
43 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
46 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
48 prepare_vmcs(vmx_pages, l2_guest_code,
49 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
52 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
53 GUEST_ASSERT(!vmlaunch());
54 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
56 GUEST_ASSERT(!vmresume());
57 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
61 void guest_code(struct vmx_pages *vmx_pages)
67 l1_guest_code(vmx_pages);
72 int main(int argc, char *argv[])
74 vm_vaddr_t vmx_pages_gva = 0;
76 struct kvm_regs regs1, regs2;
79 struct kvm_x86_state *state;
83 struct kvm_enable_cap enable_evmcs_cap = {
84 .cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
85 .args[0] = (unsigned long)&evmcs_ver
89 vm = vm_create_default(VCPU_ID, 0, guest_code);
91 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
93 if (!kvm_check_cap(KVM_CAP_NESTED_STATE) ||
94 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
95 printf("capabilities not available, skipping test\n");
99 vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
101 /* KVM should return supported EVMCS version range */
102 TEST_ASSERT(((evmcs_ver >> 8) >= (evmcs_ver & 0xff)) &&
103 (evmcs_ver & 0xff) > 0,
104 "Incorrect EVMCS version range: %x:%x\n",
105 evmcs_ver & 0xff, evmcs_ver >> 8);
107 run = vcpu_state(vm, VCPU_ID);
109 vcpu_regs_get(vm, VCPU_ID, ®s1);
111 vcpu_alloc_vmx(vm, &vmx_pages_gva);
112 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
114 for (stage = 1;; stage++) {
115 _vcpu_run(vm, VCPU_ID);
116 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
117 "Stage %d: unexpected exit reason: %u (%s),\n",
118 stage, run->exit_reason,
119 exit_reason_str(run->exit_reason));
121 switch (get_ucall(vm, VCPU_ID, &uc)) {
123 TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
124 __FILE__, uc.args[1]);
131 TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
134 /* UCALL_SYNC is handled here. */
135 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
136 uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
137 stage, (ulong)uc.args[1]);
139 state = vcpu_save_state(vm, VCPU_ID);
140 memset(®s1, 0, sizeof(regs1));
141 vcpu_regs_get(vm, VCPU_ID, ®s1);
145 /* Restore state in a new VM. */
146 kvm_vm_restart(vm, O_RDWR);
147 vm_vcpu_add(vm, VCPU_ID);
148 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
149 vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
150 vcpu_load_state(vm, VCPU_ID, state);
151 run = vcpu_state(vm, VCPU_ID);
154 memset(®s2, 0, sizeof(regs2));
155 vcpu_regs_get(vm, VCPU_ID, ®s2);
156 TEST_ASSERT(!memcmp(®s1, ®s2, sizeof(regs2)),
157 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
158 (ulong) regs2.rdi, (ulong) regs2.rsi);