757928199f19a8bfe91abc52adad6f506678fbef
[linux-2.6-microblaze.git] / tools / testing / selftests / kvm / x86_64 / evmcs_test.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018, Red Hat, Inc.
4  *
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>
13
14 #include "test_util.h"
15
16 #include "kvm_util.h"
17
18 #include "vmx.h"
19
20 #define VCPU_ID         5
21
22 void l2_guest_code(void)
23 {
24         GUEST_SYNC(7);
25
26         GUEST_SYNC(8);
27
28         /* Done, exit to L1 and never come back.  */
29         vmcall();
30 }
31
32 void l1_guest_code(struct vmx_pages *vmx_pages)
33 {
34 #define L2_GUEST_STACK_SIZE 64
35         unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
36
37         enable_vp_assist(vmx_pages->vp_assist_gpa, vmx_pages->vp_assist);
38
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);
44
45         GUEST_SYNC(4);
46         GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
47
48         prepare_vmcs(vmx_pages, l2_guest_code,
49                      &l2_guest_stack[L2_GUEST_STACK_SIZE]);
50
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);
57
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);
64 }
65
66 void guest_code(struct vmx_pages *vmx_pages)
67 {
68         GUEST_SYNC(1);
69         GUEST_SYNC(2);
70
71         if (vmx_pages)
72                 l1_guest_code(vmx_pages);
73
74         GUEST_DONE();
75
76         /* Try enlightened vmptrld with an incorrect GPA */
77         evmcs_vmptrld(0xdeadbeef, vmx_pages->enlightened_vmcs);
78         GUEST_ASSERT(vmlaunch());
79 }
80
81 int main(int argc, char *argv[])
82 {
83         vm_vaddr_t vmx_pages_gva = 0;
84
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;
91
92         /* Create VM */
93         vm = vm_create_default(VCPU_ID, 0, guest_code);
94
95         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
96
97         if (!nested_vmx_supported() ||
98             !kvm_check_cap(KVM_CAP_NESTED_STATE) ||
99             !kvm_check_cap(KVM_CAP_HYPERV_ENLIGHTENED_VMCS)) {
100                 print_skip("Enlightened VMCS is unsupported");
101                 exit(KSFT_SKIP);
102         }
103
104         vcpu_enable_evmcs(vm, VCPU_ID);
105
106         run = vcpu_state(vm, VCPU_ID);
107
108         vcpu_regs_get(vm, VCPU_ID, &regs1);
109
110         vcpu_alloc_vmx(vm, &vmx_pages_gva);
111         vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
112
113         for (stage = 1;; stage++) {
114                 _vcpu_run(vm, VCPU_ID);
115                 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
116                             "Stage %d: unexpected exit reason: %u (%s),\n",
117                             stage, run->exit_reason,
118                             exit_reason_str(run->exit_reason));
119
120                 switch (get_ucall(vm, VCPU_ID, &uc)) {
121                 case UCALL_ABORT:
122                         TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0],
123                                   __FILE__, uc.args[1]);
124                         /* NOT REACHED */
125                 case UCALL_SYNC:
126                         break;
127                 case UCALL_DONE:
128                         goto part1_done;
129                 default:
130                         TEST_FAIL("Unknown ucall %lu", uc.cmd);
131                 }
132
133                 /* UCALL_SYNC is handled here.  */
134                 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
135                             uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx",
136                             stage, (ulong)uc.args[1]);
137
138                 state = vcpu_save_state(vm, VCPU_ID);
139                 memset(&regs1, 0, sizeof(regs1));
140                 vcpu_regs_get(vm, VCPU_ID, &regs1);
141
142                 kvm_vm_release(vm);
143
144                 /* Restore state in a new VM.  */
145                 kvm_vm_restart(vm, O_RDWR);
146                 vm_vcpu_add(vm, VCPU_ID);
147                 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
148                 vcpu_enable_evmcs(vm, VCPU_ID);
149                 vcpu_load_state(vm, VCPU_ID, state);
150                 run = vcpu_state(vm, VCPU_ID);
151                 free(state);
152
153                 memset(&regs2, 0, sizeof(regs2));
154                 vcpu_regs_get(vm, VCPU_ID, &regs2);
155                 TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
156                             "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
157                             (ulong) regs2.rdi, (ulong) regs2.rsi);
158         }
159
160 part1_done:
161         _vcpu_run(vm, VCPU_ID);
162         TEST_ASSERT(run->exit_reason == KVM_EXIT_SHUTDOWN,
163                     "Unexpected successful VMEnter with invalid eVMCS pointer!");
164
165         kvm_vm_free(vm);
166 }