kvm: selftests: actually use all of lib/vmx.c
[sfrench/cifs-2.6.git] / tools / testing / selftests / kvm / vmx_tsc_adjust_test.c
1 /*
2  * gtests/tests/vmx_tsc_adjust_test.c
3  *
4  * Copyright (C) 2018, Google LLC.
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2.
7  *
8  *
9  * IA32_TSC_ADJUST test
10  *
11  * According to the SDM, "if an execution of WRMSR to the
12  * IA32_TIME_STAMP_COUNTER MSR adds (or subtracts) value X from the TSC,
13  * the logical processor also adds (or subtracts) value X from the
14  * IA32_TSC_ADJUST MSR.
15  *
16  * Note that when L1 doesn't intercept writes to IA32_TSC, a
17  * WRMSR(IA32_TSC) from L2 sets L1's TSC value, not L2's perceived TSC
18  * value.
19  *
20  * This test verifies that this unusual case is handled correctly.
21  */
22
23 #include "test_util.h"
24 #include "kvm_util.h"
25 #include "x86.h"
26 #include "vmx.h"
27
28 #include <string.h>
29 #include <sys/ioctl.h>
30
31 #include "../kselftest.h"
32
33 #ifndef MSR_IA32_TSC_ADJUST
34 #define MSR_IA32_TSC_ADJUST 0x3b
35 #endif
36
37 #define PAGE_SIZE       4096
38 #define VCPU_ID         5
39
40 #define TSC_ADJUST_VALUE (1ll << 32)
41 #define TSC_OFFSET_VALUE -(1ll << 48)
42
43 enum {
44         PORT_ABORT = 0x1000,
45         PORT_REPORT,
46         PORT_DONE,
47 };
48
49 enum {
50         VMXON_PAGE = 0,
51         VMCS_PAGE,
52         MSR_BITMAP_PAGE,
53
54         NUM_VMX_PAGES,
55 };
56
57 struct kvm_single_msr {
58         struct kvm_msrs header;
59         struct kvm_msr_entry entry;
60 } __attribute__((packed));
61
62 /* The virtual machine object. */
63 static struct kvm_vm *vm;
64
65 #define exit_to_l0(_port, _arg) do_exit_to_l0(_port, (unsigned long) (_arg))
66 static void do_exit_to_l0(uint16_t port, unsigned long arg)
67 {
68         __asm__ __volatile__("in %[port], %%al"
69                 :
70                 : [port]"d"(port), "D"(arg)
71                 : "rax");
72 }
73
74
75 #define GUEST_ASSERT(_condition) do {                                        \
76         if (!(_condition))                                                   \
77                 exit_to_l0(PORT_ABORT, "Failed guest assert: " #_condition); \
78 } while (0)
79
80 static void check_ia32_tsc_adjust(int64_t max)
81 {
82         int64_t adjust;
83
84         adjust = rdmsr(MSR_IA32_TSC_ADJUST);
85         exit_to_l0(PORT_REPORT, adjust);
86         GUEST_ASSERT(adjust <= max);
87 }
88
89 static void l2_guest_code(void)
90 {
91         uint64_t l1_tsc = rdtsc() - TSC_OFFSET_VALUE;
92
93         wrmsr(MSR_IA32_TSC, l1_tsc - TSC_ADJUST_VALUE);
94         check_ia32_tsc_adjust(-2 * TSC_ADJUST_VALUE);
95
96         /* Exit to L1 */
97         __asm__ __volatile__("vmcall");
98 }
99
100 static void l1_guest_code(struct vmx_pages *vmx_pages)
101 {
102 #define L2_GUEST_STACK_SIZE 64
103         unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
104         uint32_t control;
105         uintptr_t save_cr3;
106
107         GUEST_ASSERT(rdtsc() < TSC_ADJUST_VALUE);
108         wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
109         check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
110
111         GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
112
113         /* Prepare the VMCS for L2 execution. */
114         prepare_vmcs(vmx_pages, l2_guest_code,
115                      &l2_guest_stack[L2_GUEST_STACK_SIZE]);
116         control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
117         control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETING;
118         vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
119         vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
120
121         /* Jump into L2.  First, test failure to load guest CR3.  */
122         save_cr3 = vmreadz(GUEST_CR3);
123         vmwrite(GUEST_CR3, -1ull);
124         GUEST_ASSERT(!vmlaunch());
125         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) ==
126                      (EXIT_REASON_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE));
127         check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
128         vmwrite(GUEST_CR3, save_cr3);
129
130         GUEST_ASSERT(!vmlaunch());
131         GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
132
133         check_ia32_tsc_adjust(-2 * TSC_ADJUST_VALUE);
134
135         exit_to_l0(PORT_DONE, 0);
136 }
137
138 void report(int64_t val)
139 {
140         printf("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n",
141                val, val / TSC_ADJUST_VALUE, val % TSC_ADJUST_VALUE);
142 }
143
144 int main(int argc, char *argv[])
145 {
146         struct vmx_pages *vmx_pages;
147         vm_vaddr_t vmx_pages_gva;
148         struct kvm_cpuid_entry2 *entry = kvm_get_supported_cpuid_entry(1);
149
150         if (!(entry->ecx & CPUID_VMX)) {
151                 fprintf(stderr, "nested VMX not enabled, skipping test\n");
152                 exit(KSFT_SKIP);
153         }
154
155         vm = vm_create_default(VCPU_ID, (void *) l1_guest_code);
156         vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
157
158         /* Allocate VMX pages and shared descriptors (vmx_pages). */
159         vmx_pages = vcpu_alloc_vmx(vm, &vmx_pages_gva);
160         vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
161
162         for (;;) {
163                 volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
164                 struct kvm_regs regs;
165
166                 vcpu_run(vm, VCPU_ID);
167                 vcpu_regs_get(vm, VCPU_ID, &regs);
168                 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
169                             "Got exit_reason other than KVM_EXIT_IO: %u (%s), rip=%lx\n",
170                             run->exit_reason,
171                             exit_reason_str(run->exit_reason), regs.rip);
172
173                 switch (run->io.port) {
174                 case PORT_ABORT:
175                         TEST_ASSERT(false, "%s", (const char *) regs.rdi);
176                         /* NOT REACHED */
177                 case PORT_REPORT:
178                         report(regs.rdi);
179                         break;
180                 case PORT_DONE:
181                         goto done;
182                 default:
183                         TEST_ASSERT(false, "Unknown port 0x%x.", run->io.port);
184                 }
185         }
186
187         kvm_vm_free(vm);
188 done:
189         return 0;
190 }