]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/testing/selftests/kvm/state_test.c
kvm: selftests: add test for nested state save/restore
[linux.git] / tools / testing / selftests / kvm / state_test.c
1 /*
2  * KVM_GET/SET_* tests
3  *
4  * Copyright (C) 2018, Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  *
8  * Tests for vCPU state save/restore, including nested guest state.
9  */
10 #define _GNU_SOURCE /* for program_invocation_short_name */
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/ioctl.h>
16
17 #include "test_util.h"
18
19 #include "kvm_util.h"
20 #include "x86.h"
21 #include "vmx.h"
22
23 #define VCPU_ID         5
24 #define PORT_SYNC       0x1000
25 #define PORT_ABORT      0x1001
26 #define PORT_DONE       0x1002
27
28 static inline void __exit_to_l0(uint16_t port, uint64_t arg0, uint64_t arg1)
29 {
30         __asm__ __volatile__("in %[port], %%al"
31                              :
32                              : [port]"d"(port), "D"(arg0), "S"(arg1)
33                              : "rax");
34 }
35
36 #define exit_to_l0(_port, _arg0, _arg1) \
37         __exit_to_l0(_port, (uint64_t) (_arg0), (uint64_t) (_arg1))
38
39 #define GUEST_ASSERT(_condition) do { \
40         if (!(_condition)) \
41                 exit_to_l0(PORT_ABORT, "Failed guest assert: " #_condition, __LINE__);\
42 } while (0)
43
44 #define GUEST_SYNC(stage) \
45         exit_to_l0(PORT_SYNC, "hello", stage);
46
47 static bool have_nested_state;
48
49 void l2_guest_code(void)
50 {
51         GUEST_SYNC(5);
52
53         /* Exit to L1 */
54         vmcall();
55
56         GUEST_SYNC(7);
57
58         /* Done, exit to L1 and never come back.  */
59         vmcall();
60 }
61
62 void l1_guest_code(struct vmx_pages *vmx_pages)
63 {
64 #define L2_GUEST_STACK_SIZE 64
65         unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
66
67         GUEST_ASSERT(vmx_pages->vmcs_gpa);
68         GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
69         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
70
71         GUEST_SYNC(3);
72         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
73
74         prepare_vmcs(vmx_pages, l2_guest_code,
75                      &l2_guest_stack[L2_GUEST_STACK_SIZE]);
76
77         GUEST_SYNC(4);
78         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
79         GUEST_ASSERT(!vmlaunch());
80         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
81         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
82
83         /* Check that the launched state is preserved.  */
84         GUEST_ASSERT(vmlaunch());
85
86         GUEST_ASSERT(!vmresume());
87         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
88
89         GUEST_SYNC(6);
90         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
91
92         GUEST_ASSERT(!vmresume());
93         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
94
95         vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);
96
97         GUEST_ASSERT(!vmresume());
98         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
99         GUEST_SYNC(8);
100 }
101
102 void guest_code(struct vmx_pages *vmx_pages)
103 {
104         GUEST_SYNC(1);
105         GUEST_SYNC(2);
106
107         if (vmx_pages)
108                 l1_guest_code(vmx_pages);
109
110         exit_to_l0(PORT_DONE, 0, 0);
111 }
112
113 int main(int argc, char *argv[])
114 {
115         struct vmx_pages *vmx_pages = NULL;
116         vm_vaddr_t vmx_pages_gva = 0;
117
118         struct kvm_regs regs1, regs2;
119         struct kvm_vm *vm;
120         struct kvm_run *run;
121         struct kvm_x86_state *state;
122         int stage;
123
124         struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
125
126         /* Create VM */
127         vm = vm_create_default(VCPU_ID, guest_code);
128         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
129         run = vcpu_state(vm, VCPU_ID);
130
131         vcpu_regs_get(vm, VCPU_ID, &regs1);
132
133         if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
134                 vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
135                 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
136         } else {
137                 printf("will skip nested state checks\n");
138                 vcpu_args_set(vm, VCPU_ID, 1, 0);
139         }
140
141         for (stage = 1;; stage++) {
142                 _vcpu_run(vm, VCPU_ID);
143                 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
144                             "Unexpected exit reason: %u (%s),\n",
145                             run->exit_reason,
146                             exit_reason_str(run->exit_reason));
147
148                 memset(&regs1, 0, sizeof(regs1));
149                 vcpu_regs_get(vm, VCPU_ID, &regs1);
150                 switch (run->io.port) {
151                 case PORT_ABORT:
152                         TEST_ASSERT(false, "%s at %s:%d", (const char *) regs1.rdi,
153                                     __FILE__, regs1.rsi);
154                         /* NOT REACHED */
155                 case PORT_SYNC:
156                         break;
157                 case PORT_DONE:
158                         goto done;
159                 default:
160                         TEST_ASSERT(false, "Unknown port 0x%x.", run->io.port);
161                 }
162
163                 /* PORT_SYNC is handled here.  */
164                 TEST_ASSERT(!strcmp((const char *)regs1.rdi, "hello") &&
165                             regs1.rsi == stage, "Unexpected register values vmexit #%lx, got %lx",
166                             stage, (ulong) regs1.rsi);
167
168                 state = vcpu_save_state(vm, VCPU_ID);
169                 kvm_vm_release(vm);
170
171                 /* Restore state in a new VM.  */
172                 kvm_vm_restart(vm, O_RDWR);
173                 vm_vcpu_add(vm, VCPU_ID, 0, 0);
174                 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
175                 vcpu_load_state(vm, VCPU_ID, state);
176                 run = vcpu_state(vm, VCPU_ID);
177                 free(state);
178
179                 memset(&regs2, 0, sizeof(regs2));
180                 vcpu_regs_get(vm, VCPU_ID, &regs2);
181                 TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
182                             "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
183                             (ulong) regs2.rdi, (ulong) regs2.rsi);
184         }
185
186 done:
187         kvm_vm_free(vm);
188 }