|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * VMX-preemption timer test |
| 4 | + * |
| 5 | + * Copyright (C) 2020, Google, LLC. |
| 6 | + * |
| 7 | + * Test to ensure the VM-Enter after migration doesn't |
| 8 | + * incorrectly restarts the timer with the full timer |
| 9 | + * value instead of partially decayed timer value |
| 10 | + * |
| 11 | + */ |
| 12 | +#define _GNU_SOURCE /* for program_invocation_short_name */ |
| 13 | +#include <fcntl.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | +#include <sys/ioctl.h> |
| 18 | + |
| 19 | +#include "test_util.h" |
| 20 | + |
| 21 | +#include "kvm_util.h" |
| 22 | +#include "processor.h" |
| 23 | +#include "vmx.h" |
| 24 | + |
| 25 | +#define VCPU_ID 5 |
| 26 | +#define PREEMPTION_TIMER_VALUE 100000000ull |
| 27 | +#define PREEMPTION_TIMER_VALUE_THRESHOLD1 80000000ull |
| 28 | + |
| 29 | +u32 vmx_pt_rate; |
| 30 | +bool l2_save_restore_done; |
| 31 | +static u64 l2_vmx_pt_start; |
| 32 | +volatile u64 l2_vmx_pt_finish; |
| 33 | + |
| 34 | +void l2_guest_code(void) |
| 35 | +{ |
| 36 | + u64 vmx_pt_delta; |
| 37 | + |
| 38 | + vmcall(); |
| 39 | + l2_vmx_pt_start = (rdtsc() >> vmx_pt_rate) << vmx_pt_rate; |
| 40 | + |
| 41 | + /* |
| 42 | + * Wait until the 1st threshold has passed |
| 43 | + */ |
| 44 | + do { |
| 45 | + l2_vmx_pt_finish = rdtsc(); |
| 46 | + vmx_pt_delta = (l2_vmx_pt_finish - l2_vmx_pt_start) >> |
| 47 | + vmx_pt_rate; |
| 48 | + } while (vmx_pt_delta < PREEMPTION_TIMER_VALUE_THRESHOLD1); |
| 49 | + |
| 50 | + /* |
| 51 | + * Force L2 through Save and Restore cycle |
| 52 | + */ |
| 53 | + GUEST_SYNC(1); |
| 54 | + |
| 55 | + l2_save_restore_done = 1; |
| 56 | + |
| 57 | + /* |
| 58 | + * Now wait for the preemption timer to fire and |
| 59 | + * exit to L1 |
| 60 | + */ |
| 61 | + while ((l2_vmx_pt_finish = rdtsc())) |
| 62 | + ; |
| 63 | +} |
| 64 | + |
| 65 | +void l1_guest_code(struct vmx_pages *vmx_pages) |
| 66 | +{ |
| 67 | +#define L2_GUEST_STACK_SIZE 64 |
| 68 | + unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE]; |
| 69 | + u64 l1_vmx_pt_start; |
| 70 | + u64 l1_vmx_pt_finish; |
| 71 | + u64 l1_tsc_deadline, l2_tsc_deadline; |
| 72 | + |
| 73 | + GUEST_ASSERT(vmx_pages->vmcs_gpa); |
| 74 | + GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages)); |
| 75 | + GUEST_ASSERT(load_vmcs(vmx_pages)); |
| 76 | + GUEST_ASSERT(vmptrstz() == vmx_pages->vmcs_gpa); |
| 77 | + |
| 78 | + prepare_vmcs(vmx_pages, l2_guest_code, |
| 79 | + &l2_guest_stack[L2_GUEST_STACK_SIZE]); |
| 80 | + |
| 81 | + /* |
| 82 | + * Check for Preemption timer support |
| 83 | + */ |
| 84 | + basic.val = rdmsr(MSR_IA32_VMX_BASIC); |
| 85 | + ctrl_pin_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_PINBASED_CTLS |
| 86 | + : MSR_IA32_VMX_PINBASED_CTLS); |
| 87 | + ctrl_exit_rev.val = rdmsr(basic.ctrl ? MSR_IA32_VMX_TRUE_EXIT_CTLS |
| 88 | + : MSR_IA32_VMX_EXIT_CTLS); |
| 89 | + |
| 90 | + if (!(ctrl_pin_rev.clr & PIN_BASED_VMX_PREEMPTION_TIMER) || |
| 91 | + !(ctrl_exit_rev.clr & VM_EXIT_SAVE_VMX_PREEMPTION_TIMER)) |
| 92 | + return; |
| 93 | + |
| 94 | + GUEST_ASSERT(!vmlaunch()); |
| 95 | + GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); |
| 96 | + vmwrite(GUEST_RIP, vmreadz(GUEST_RIP) + vmreadz(VM_EXIT_INSTRUCTION_LEN)); |
| 97 | + |
| 98 | + /* |
| 99 | + * Turn on PIN control and resume the guest |
| 100 | + */ |
| 101 | + GUEST_ASSERT(!vmwrite(PIN_BASED_VM_EXEC_CONTROL, |
| 102 | + vmreadz(PIN_BASED_VM_EXEC_CONTROL) | |
| 103 | + PIN_BASED_VMX_PREEMPTION_TIMER)); |
| 104 | + |
| 105 | + GUEST_ASSERT(!vmwrite(VMX_PREEMPTION_TIMER_VALUE, |
| 106 | + PREEMPTION_TIMER_VALUE)); |
| 107 | + |
| 108 | + vmx_pt_rate = rdmsr(MSR_IA32_VMX_MISC) & 0x1F; |
| 109 | + |
| 110 | + l2_save_restore_done = 0; |
| 111 | + |
| 112 | + l1_vmx_pt_start = (rdtsc() >> vmx_pt_rate) << vmx_pt_rate; |
| 113 | + |
| 114 | + GUEST_ASSERT(!vmresume()); |
| 115 | + |
| 116 | + l1_vmx_pt_finish = rdtsc(); |
| 117 | + |
| 118 | + /* |
| 119 | + * Ensure exit from L2 happens after L2 goes through |
| 120 | + * save and restore |
| 121 | + */ |
| 122 | + GUEST_ASSERT(l2_save_restore_done); |
| 123 | + |
| 124 | + /* |
| 125 | + * Ensure the exit from L2 is due to preemption timer expiry |
| 126 | + */ |
| 127 | + GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_PREEMPTION_TIMER); |
| 128 | + |
| 129 | + l1_tsc_deadline = l1_vmx_pt_start + |
| 130 | + (PREEMPTION_TIMER_VALUE << vmx_pt_rate); |
| 131 | + |
| 132 | + l2_tsc_deadline = l2_vmx_pt_start + |
| 133 | + (PREEMPTION_TIMER_VALUE << vmx_pt_rate); |
| 134 | + |
| 135 | + /* |
| 136 | + * Sync with the host and pass the l1|l2 pt_expiry_finish times and |
| 137 | + * tsc deadlines so that host can verify they are as expected |
| 138 | + */ |
| 139 | + GUEST_SYNC_ARGS(2, l1_vmx_pt_finish, l1_tsc_deadline, |
| 140 | + l2_vmx_pt_finish, l2_tsc_deadline); |
| 141 | +} |
| 142 | + |
| 143 | +void guest_code(struct vmx_pages *vmx_pages) |
| 144 | +{ |
| 145 | + if (vmx_pages) |
| 146 | + l1_guest_code(vmx_pages); |
| 147 | + |
| 148 | + GUEST_DONE(); |
| 149 | +} |
| 150 | + |
| 151 | +int main(int argc, char *argv[]) |
| 152 | +{ |
| 153 | + vm_vaddr_t vmx_pages_gva = 0; |
| 154 | + |
| 155 | + struct kvm_regs regs1, regs2; |
| 156 | + struct kvm_vm *vm; |
| 157 | + struct kvm_run *run; |
| 158 | + struct kvm_x86_state *state; |
| 159 | + struct ucall uc; |
| 160 | + int stage; |
| 161 | + |
| 162 | + /* |
| 163 | + * AMD currently does not implement any VMX features, so for now we |
| 164 | + * just early out. |
| 165 | + */ |
| 166 | + nested_vmx_check_supported(); |
| 167 | + |
| 168 | + /* Create VM */ |
| 169 | + vm = vm_create_default(VCPU_ID, 0, guest_code); |
| 170 | + vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); |
| 171 | + run = vcpu_state(vm, VCPU_ID); |
| 172 | + |
| 173 | + vcpu_regs_get(vm, VCPU_ID, ®s1); |
| 174 | + |
| 175 | + if (kvm_check_cap(KVM_CAP_NESTED_STATE)) { |
| 176 | + vcpu_alloc_vmx(vm, &vmx_pages_gva); |
| 177 | + vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva); |
| 178 | + } else { |
| 179 | + pr_info("will skip vmx preemption timer checks\n"); |
| 180 | + goto done; |
| 181 | + } |
| 182 | + |
| 183 | + for (stage = 1;; stage++) { |
| 184 | + _vcpu_run(vm, VCPU_ID); |
| 185 | + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, |
| 186 | + "Stage %d: unexpected exit reason: %u (%s),\n", |
| 187 | + stage, run->exit_reason, |
| 188 | + exit_reason_str(run->exit_reason)); |
| 189 | + |
| 190 | + switch (get_ucall(vm, VCPU_ID, &uc)) { |
| 191 | + case UCALL_ABORT: |
| 192 | + TEST_FAIL("%s at %s:%ld", (const char *)uc.args[0], |
| 193 | + __FILE__, uc.args[1]); |
| 194 | + /* NOT REACHED */ |
| 195 | + case UCALL_SYNC: |
| 196 | + break; |
| 197 | + case UCALL_DONE: |
| 198 | + goto done; |
| 199 | + default: |
| 200 | + TEST_FAIL("Unknown ucall %lu", uc.cmd); |
| 201 | + } |
| 202 | + |
| 203 | + /* UCALL_SYNC is handled here. */ |
| 204 | + TEST_ASSERT(!strcmp((const char *)uc.args[0], "hello") && |
| 205 | + uc.args[1] == stage, "Stage %d: Unexpected register values vmexit, got %lx", |
| 206 | + stage, (ulong)uc.args[1]); |
| 207 | + /* |
| 208 | + * If this stage 2 then we should verify the vmx pt expiry |
| 209 | + * is as expected. |
| 210 | + * From L1's perspective verify Preemption timer hasn't |
| 211 | + * expired too early. |
| 212 | + * From L2's perspective verify Preemption timer hasn't |
| 213 | + * expired too late. |
| 214 | + */ |
| 215 | + if (stage == 2) { |
| 216 | + |
| 217 | + pr_info("Stage %d: L1 PT expiry TSC (%lu) , L1 TSC deadline (%lu)\n", |
| 218 | + stage, uc.args[2], uc.args[3]); |
| 219 | + |
| 220 | + pr_info("Stage %d: L2 PT expiry TSC (%lu) , L2 TSC deadline (%lu)\n", |
| 221 | + stage, uc.args[4], uc.args[5]); |
| 222 | + |
| 223 | + TEST_ASSERT(uc.args[2] >= uc.args[3], |
| 224 | + "Stage %d: L1 PT expiry TSC (%lu) < L1 TSC deadline (%lu)", |
| 225 | + stage, uc.args[2], uc.args[3]); |
| 226 | + |
| 227 | + TEST_ASSERT(uc.args[4] < uc.args[5], |
| 228 | + "Stage %d: L2 PT expiry TSC (%lu) > L2 TSC deadline (%lu)", |
| 229 | + stage, uc.args[4], uc.args[5]); |
| 230 | + } |
| 231 | + |
| 232 | + state = vcpu_save_state(vm, VCPU_ID); |
| 233 | + memset(®s1, 0, sizeof(regs1)); |
| 234 | + vcpu_regs_get(vm, VCPU_ID, ®s1); |
| 235 | + |
| 236 | + kvm_vm_release(vm); |
| 237 | + |
| 238 | + /* Restore state in a new VM. */ |
| 239 | + kvm_vm_restart(vm, O_RDWR); |
| 240 | + vm_vcpu_add(vm, VCPU_ID); |
| 241 | + vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid()); |
| 242 | + vcpu_load_state(vm, VCPU_ID, state); |
| 243 | + run = vcpu_state(vm, VCPU_ID); |
| 244 | + free(state); |
| 245 | + |
| 246 | + memset(®s2, 0, sizeof(regs2)); |
| 247 | + vcpu_regs_get(vm, VCPU_ID, ®s2); |
| 248 | + TEST_ASSERT(!memcmp(®s1, ®s2, sizeof(regs2)), |
| 249 | + "Unexpected register values after vcpu_load_state; rdi: %lx rsi: %lx", |
| 250 | + (ulong) regs2.rdi, (ulong) regs2.rsi); |
| 251 | + } |
| 252 | + |
| 253 | +done: |
| 254 | + kvm_vm_free(vm); |
| 255 | +} |
0 commit comments