OSDN Git Service

kvm: selftests: introduce ucall
[uclinux-h8/linux.git] / tools / testing / selftests / kvm / x86_64 / 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 "processor.h"
21 #include "vmx.h"
22
23 #define VCPU_ID         5
24
25 static bool have_nested_state;
26
27 void l2_guest_code(void)
28 {
29         GUEST_SYNC(5);
30
31         /* Exit to L1 */
32         vmcall();
33
34         /* L1 has now set up a shadow VMCS for us.  */
35         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
36         GUEST_SYNC(9);
37         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
38         GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0fffee));
39         GUEST_SYNC(10);
40         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0fffee);
41         GUEST_ASSERT(!vmwrite(GUEST_RIP, 0xc0ffffee));
42         GUEST_SYNC(11);
43
44         /* Done, exit to L1 and never come back.  */
45         vmcall();
46 }
47
48 void l1_guest_code(struct vmx_pages *vmx_pages)
49 {
50 #define L2_GUEST_STACK_SIZE 64
51         unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
52
53         GUEST_ASSERT(vmx_pages->vmcs_gpa);
54         GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
55         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
56
57         GUEST_SYNC(3);
58         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
59
60         prepare_vmcs(vmx_pages, l2_guest_code,
61                      &l2_guest_stack[L2_GUEST_STACK_SIZE]);
62
63         GUEST_SYNC(4);
64         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
65         GUEST_ASSERT(!vmlaunch());
66         GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa);
67         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
68
69         /* Check that the launched state is preserved.  */
70         GUEST_ASSERT(vmlaunch());
71
72         GUEST_ASSERT(!vmresume());
73         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
74
75         GUEST_SYNC(6);
76         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
77
78         GUEST_ASSERT(!vmresume());
79         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
80
81         vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + 3);
82
83         vmwrite(SECONDARY_VM_EXEC_CONTROL, SECONDARY_EXEC_SHADOW_VMCS);
84         vmwrite(VMCS_LINK_POINTER, vmx_pages->shadow_vmcs_gpa);
85
86         GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
87         GUEST_ASSERT(vmlaunch());
88         GUEST_SYNC(7);
89         GUEST_ASSERT(vmlaunch());
90         GUEST_ASSERT(vmresume());
91
92         vmwrite(GUEST_RIP, 0xc0ffee);
93         GUEST_SYNC(8);
94         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffee);
95
96         GUEST_ASSERT(!vmptrld(vmx_pages->vmcs_gpa));
97         GUEST_ASSERT(!vmresume());
98         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
99
100         GUEST_ASSERT(!vmptrld(vmx_pages->shadow_vmcs_gpa));
101         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
102         GUEST_ASSERT(vmlaunch());
103         GUEST_ASSERT(vmresume());
104         GUEST_SYNC(12);
105         GUEST_ASSERT(vmreadz(GUEST_RIP) == 0xc0ffffee);
106         GUEST_ASSERT(vmlaunch());
107         GUEST_ASSERT(vmresume());
108 }
109
110 void guest_code(struct vmx_pages *vmx_pages)
111 {
112         GUEST_SYNC(1);
113         GUEST_SYNC(2);
114
115         if (vmx_pages)
116                 l1_guest_code(vmx_pages);
117
118         GUEST_DONE();
119 }
120
121 int main(int argc, char *argv[])
122 {
123         struct vmx_pages *vmx_pages = NULL;
124         vm_vaddr_t vmx_pages_gva = 0;
125
126         struct kvm_regs regs1, regs2;
127         struct kvm_vm *vm;
128         struct kvm_run *run;
129         struct kvm_x86_state *state;
130         struct ucall uc;
131         int stage;
132
133         struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
134
135         /* Create VM */
136         vm = vm_create_default(VCPU_ID, 0, guest_code);
137         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
138         run = vcpu_state(vm, VCPU_ID);
139
140         vcpu_regs_get(vm, VCPU_ID, &regs1);
141
142         if (kvm_check_cap(KVM_CAP_NESTED_STATE)) {
143                 vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
144                 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
145         } else {
146                 printf("will skip nested state checks\n");
147                 vcpu_args_set(vm, VCPU_ID, 1, 0);
148         }
149
150         for (stage = 1;; stage++) {
151                 _vcpu_run(vm, VCPU_ID);
152                 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
153                             "Unexpected exit reason: %u (%s),\n",
154                             run->exit_reason,
155                             exit_reason_str(run->exit_reason));
156
157                 memset(&regs1, 0, sizeof(regs1));
158                 vcpu_regs_get(vm, VCPU_ID, &regs1);
159                 switch (get_ucall(vm, VCPU_ID, &uc)) {
160                 case UCALL_ABORT:
161                         TEST_ASSERT(false, "%s at %s:%d", (const char *)uc.args[0],
162                                     __FILE__, uc.args[1]);
163                         /* NOT REACHED */
164                 case UCALL_SYNC:
165                         break;
166                 case UCALL_DONE:
167                         goto done;
168                 default:
169                         TEST_ASSERT(false, "Unknown ucall 0x%x.", uc.cmd);
170                 }
171
172                 /* UCALL_SYNC is handled here.  */
173                 TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") &&
174                             uc.args[1] == stage, "Unexpected register values vmexit #%lx, got %lx",
175                             stage, (ulong)uc.args[1]);
176
177                 state = vcpu_save_state(vm, VCPU_ID);
178                 kvm_vm_release(vm);
179
180                 /* Restore state in a new VM.  */
181                 kvm_vm_restart(vm, O_RDWR);
182                 vm_vcpu_add(vm, VCPU_ID, 0, 0);
183                 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
184                 vcpu_load_state(vm, VCPU_ID, state);
185                 run = vcpu_state(vm, VCPU_ID);
186                 free(state);
187
188                 memset(&regs2, 0, sizeof(regs2));
189                 vcpu_regs_get(vm, VCPU_ID, &regs2);
190                 TEST_ASSERT(!memcmp(&regs1, &regs2, sizeof(regs2)),
191                             "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx",
192                             (ulong) regs2.rdi, (ulong) regs2.rsi);
193         }
194
195 done:
196         kvm_vm_free(vm);
197 }