Merge tag 'perf-core-for-mingo-5.4-20190822' of git://git.kernel.org/pub/scm/linux...
[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(6);
25
26         GUEST_SYNC(7);
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         GUEST_ASSERT(!vmlaunch());
54         GUEST_ASSERT(vmptrstz() == vmx_pages->enlightened_vmcs_gpa);
55         GUEST_SYNC(8);
56         GUEST_ASSERT(!vmresume());
57         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
58         GUEST_SYNC(9);
59 }
60
61 void guest_code(struct vmx_pages *vmx_pages)
62 {
63         GUEST_SYNC(1);
64         GUEST_SYNC(2);
65
66         if (vmx_pages)
67                 l1_guest_code(vmx_pages);
68
69         GUEST_DONE();
70 }
71
72 int main(int argc, char *argv[])
73 {
74         vm_vaddr_t vmx_pages_gva = 0;
75
76         struct kvm_regs regs1, regs2;
77         struct kvm_vm *vm;
78         struct kvm_run *run;
79         struct kvm_x86_state *state;
80         struct ucall uc;
81         int stage;
82         uint16_t evmcs_ver;
83         struct kvm_enable_cap enable_evmcs_cap = {
84                 .cap = KVM_CAP_HYPERV_ENLIGHTENED_VMCS,
85                  .args[0] = (unsigned long)&evmcs_ver
86         };
87
88         /* Create VM */
89         vm = vm_create_default(VCPU_ID, 0, guest_code);
90
91         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
92
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");
96                 exit(KSFT_SKIP);
97         }
98
99         vcpu_ioctl(vm, VCPU_ID, KVM_ENABLE_CAP, &enable_evmcs_cap);
100
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);
106
107         run = vcpu_state(vm, VCPU_ID);
108
109         vcpu_regs_get(vm, VCPU_ID, &regs1);
110
111         vcpu_alloc_vmx(vm, &vmx_pages_gva);
112         vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
113
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));
120
121                 switch (get_ucall(vm, VCPU_ID, &uc)) {
122                 case UCALL_ABORT:
123                         TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
124                                     __FILE__, uc.args[1]);
125                         /* NOT REACHED */
126                 case UCALL_SYNC:
127                         break;
128                 case UCALL_DONE:
129                         goto done;
130                 default:
131                         TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
132                 }
133
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]);
138
139                 state = vcpu_save_state(vm, VCPU_ID);
140                 memset(&regs1, 0, sizeof(regs1));
141                 vcpu_regs_get(vm, VCPU_ID, &regs1);
142
143                 kvm_vm_release(vm);
144
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);
152                 free(state);
153
154                 memset(&regs2, 0, sizeof(regs2));
155                 vcpu_regs_get(vm, VCPU_ID, &regs2);
156                 TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
157                             "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
158                             (ulong) regs2.rdi, (ulong) regs2.rsi);
159         }
160
161 done:
162         kvm_vm_free(vm);
163 }