1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2018, Google LLC.
9 * According to the SDM, "if an execution of WRMSR to the
10 * IA32_TIME_STAMP_COUNTER MSR adds (or subtracts) value X from the TSC,
11 * the logical processor also adds (or subtracts) value X from the
12 * IA32_TSC_ADJUST MSR.
14 * Note that when L1 doesn't intercept writes to IA32_TSC, a
15 * WRMSR(IA32_TSC) from L2 sets L1's TSC value, not L2's perceived TSC
18 * This test verifies that this unusual case is handled correctly.
21 #include "test_util.h"
23 #include "processor.h"
27 #include <sys/ioctl.h>
29 #include "kselftest.h"
31 #ifndef MSR_IA32_TSC_ADJUST
32 #define MSR_IA32_TSC_ADJUST 0x3b
35 #define PAGE_SIZE 4096
38 #define TSC_ADJUST_VALUE (1ll << 32)
39 #define TSC_OFFSET_VALUE -(1ll << 48)
55 struct kvm_single_msr {
56 struct kvm_msrs header;
57 struct kvm_msr_entry entry;
58 } __attribute__((packed));
60 /* The virtual machine object. */
61 static struct kvm_vm *vm;
63 static void check_ia32_tsc_adjust(int64_t max)
67 adjust = rdmsr(MSR_IA32_TSC_ADJUST);
69 GUEST_ASSERT(adjust <= max);
72 static void l2_guest_code(void)
74 uint64_t l1_tsc = rdtsc() - TSC_OFFSET_VALUE;
76 wrmsr(MSR_IA32_TSC, l1_tsc - TSC_ADJUST_VALUE);
77 check_ia32_tsc_adjust(-2 * TSC_ADJUST_VALUE);
80 __asm__ __volatile__("vmcall");
83 static void l1_guest_code(struct vmx_pages *vmx_pages)
85 #define L2_GUEST_STACK_SIZE 64
86 unsigned long l2_guest_stack[L2_GUEST_STACK_SIZE];
90 GUEST_ASSERT(rdtsc() < TSC_ADJUST_VALUE);
91 wrmsr(MSR_IA32_TSC, rdtsc() - TSC_ADJUST_VALUE);
92 check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
94 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages));
95 GUEST_ASSERT(load_vmcs(vmx_pages));
97 /* Prepare the VMCS for L2 execution. */
98 prepare_vmcs(vmx_pages, l2_guest_code,
99 &l2_guest_stack[L2_GUEST_STACK_SIZE]);
100 control = vmreadz(CPU_BASED_VM_EXEC_CONTROL);
101 control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETTING;
102 vmwrite(CPU_BASED_VM_EXEC_CONTROL, control);
103 vmwrite(TSC_OFFSET, TSC_OFFSET_VALUE);
105 /* Jump into L2. First, test failure to load guest CR3. */
106 save_cr3 = vmreadz(GUEST_CR3);
107 vmwrite(GUEST_CR3, -1ull);
108 GUEST_ASSERT(!vmlaunch());
109 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) ==
110 (EXIT_REASON_FAILED_VMENTRY | EXIT_REASON_INVALID_STATE));
111 check_ia32_tsc_adjust(-1 * TSC_ADJUST_VALUE);
112 vmwrite(GUEST_CR3, save_cr3);
114 GUEST_ASSERT(!vmlaunch());
115 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL);
117 check_ia32_tsc_adjust(-2 * TSC_ADJUST_VALUE);
122 static void report(int64_t val)
124 pr_info("IA32_TSC_ADJUST is %ld (%lld * TSC_ADJUST_VALUE + %lld).\n",
125 val, val / TSC_ADJUST_VALUE, val % TSC_ADJUST_VALUE);
128 int main(int argc, char *argv[])
130 vm_vaddr_t vmx_pages_gva;
132 nested_vmx_check_supported();
134 vm = vm_create_default(VCPU_ID, 0, (void *) l1_guest_code);
135 vcpu_set_cpuid(vm, VCPU_ID, kvm_get_supported_cpuid());
137 /* Allocate VMX pages and shared descriptors (vmx_pages). */
138 vcpu_alloc_vmx(vm, &vmx_pages_gva);
139 vcpu_args_set(vm, VCPU_ID, 1, vmx_pages_gva);
142 volatile struct kvm_run *run = vcpu_state(vm, VCPU_ID);
145 vcpu_run(vm, VCPU_ID);
146 TEST_ASSERT(run->exit_reason == KVM_EXIT_IO,
147 "Got exit_reason other than KVM_EXIT_IO: %u (%s)\n",
149 exit_reason_str(run->exit_reason));
151 switch (get_ucall(vm, VCPU_ID, &uc)) {
153 TEST_FAIL("%s", (const char *)uc.args[0]);
161 TEST_FAIL("Unknown ucall %lu", uc.cmd);