WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / kvm / x86_64 / evmcs_test.c
blob37b8a78f6b74a6b1290d6d492139006b0c03de7d
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018, Red Hat, Inc.
5 * Tests for Enlightened VMCS, including nested guest state.
6 */
7 #define _GNU_SOURCE /* for program_invocation_short_name */
8 #include <fcntl.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/ioctl.h>
14 #include "test_util.h"
16 #include "kvm_util.h"
18 #include "vmx.h"
20 #define VCPU_ID 5
22 void l2_guest_code(void)
24 GUEST_SYNC(7);
26 GUEST_SYNC(8);
28 /* Done, exit to L1 and never come back. */
29 vmcall();
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));
41 GUEST_SYNC(3);
42 GUEST_ASSERT(load_vmcs(vmx_pages));
43 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
45 GUEST_SYNC(4);
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]);
51 GUEST_SYNC(5);
52 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
53 current_evmcs->revision_id = -1u;
54 GUEST_ASSERT(vmlaunch());
55 current_evmcs->revision_id = EVMCS_VERSION;
56 GUEST_SYNC(6);
58 GUEST_ASSERT(!vmlaunch());
59 GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
60 GUEST_SYNC(9);
61 GUEST_ASSERT(!vmresume());
62 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
63 GUEST_SYNC(10);
66 void guest_code(struct vmx_pages *vmx_pages)
68 GUEST_SYNC(1);
69 GUEST_SYNC(2);
71 if (vmx_pages)
72 l1_guest_code(vmx_pages);
74 GUEST_DONE();
76 /* Try enlightened vmptrld with an incorrect GPA */
77 evmcs_vmptrld(0xdeadbeef, vmx_pages->enlightened_vmcs);
78 GUEST_ASSERT(vmlaunch());
81 int main(int argc, char *argv[])
83 vm_vaddr_t vmx_pages_gva = 0;
85 struct kvm_regs regs1, regs2;
86 struct kvm_vm *vm;
87 struct kvm_run *run;
88 struct kvm_x86_state *state;
89 struct ucall uc;
90 int stage;
92 /* Create VM */
93 vm = vm_create_default(VCPU_ID, 0, guest_code);
95 if (!nested_vmx_supported() ||
96 !kvm_check_cap(KVM_CAP_NESTED_STATE) ||
97 !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
98 print_skip("Enlightened VMCS is unsupported");
99 exit(KSFT_SKIP);
102 vcpu_enable_evmcs(vm, VCPU_ID);
104 run = vcpu_state(vm, VCPU_ID);
106 vcpu_regs_get(vm, VCPU_ID, &regs1);
108 vcpu_alloc_vmx(vm, &vmx_pages_gva);
109 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
111 for (stage = 1;; stage++) {
112 _vcpu_run(vm, VCPU_ID);
113 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
114 "Stage %d: unexpected exit reason: %u (%s),\n",
115 stage, run->exit_reason,
116 exit_reason_str(run->exit_reason));
118 switch (get_ucall(vm, VCPU_ID, &uc)) {
119 case UCALL_ABORT:
120 TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0],
121 __FILE__, uc.args[1]);
122 /* NOT REACHED */
123 case UCALL_SYNC:
124 break;
125 case UCALL_DONE:
126 goto part1_done;
127 default:
128 TEST_FAIL("Unknown ucall %lu", uc.cmd);
131 /* UCALL_SYNC is handled here. */
132 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
133 uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
134 stage, (ulong)uc.args[1]);
136 state = vcpu_save_state(vm, VCPU_ID);
137 memset(&regs1, 0, sizeof(regs1));
138 vcpu_regs_get(vm, VCPU_ID, &regs1);
140 kvm_vm_release(vm);
142 /* Restore state in a new VM. */
143 kvm_vm_restart(vm, O_RDWR);
144 vm_vcpu_add(vm, VCPU_ID);
145 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
146 vcpu_enable_evmcs(vm, VCPU_ID);
147 vcpu_load_state(vm, VCPU_ID, state);
148 run = vcpu_state(vm, VCPU_ID);
149 free(state);
151 memset(&regs2, 0, sizeof(regs2));
152 vcpu_regs_get(vm, VCPU_ID, &regs2);
153 TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
154 "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
155 (ulong) regs2.rdi, (ulong) regs2.rsi);
158 part1_done:
159 _vcpu_run(vm, VCPU_ID);
160 TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
161 "Unexpected successful VMEnter with invalid eVMCS pointer!");
163 kvm_vm_free(vm);