1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vmx_nested_tsc_scaling_test 4 * 5 * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. 6 * 7 * This test case verifies that nested TSC scaling behaves as expected when 8 * both L1 and L2 are scaled using different ratios. For this test we scale 9 * L1 down and scale L2 up. 10 */ 11 12 #include <time.h> 13 14 #include "kvm_util.h" 15 #include "vmx.h" 16 #include "svm_util.h" 17 #include "kselftest.h" 18 19 /* L2 is scaled up (from L1's perspective) by this factor */ 20 #define L2_SCALE_FACTOR 4ULL 21 22 #define TSC_OFFSET_L2 ((u64)-33125236320908) 23 #define TSC_MULTIPLIER_L2 (L2_SCALE_FACTOR << 48) 24 25 enum { USLEEP, UCHECK_L1, UCHECK_L2 }; 26 #define GUEST_SLEEP(sec) ucall(UCALL_SYNC, 2, USLEEP, sec) 27 #define GUEST_CHECK(level, freq) ucall(UCALL_SYNC, 2, level, freq) 28 29 30 /* 31 * This function checks whether the "actual" TSC frequency of a guest matches 32 * its expected frequency. In order to account for delays in taking the TSC 33 * measurements, a difference of 1% between the actual and the expected value 34 * is tolerated. 35 */ 36 static void compare_tsc_freq(u64 actual, u64 expected) 37 { 38 u64 tolerance, thresh_low, thresh_high; 39 40 tolerance = expected / 100; 41 thresh_low = expected - tolerance; 42 thresh_high = expected + tolerance; 43 44 TEST_ASSERT(thresh_low < actual, 45 "TSC freq is expected to be between %"PRIu64" and %"PRIu64 46 " but it actually is %"PRIu64, 47 thresh_low, thresh_high, actual); 48 TEST_ASSERT(thresh_high > actual, 49 "TSC freq is expected to be between %"PRIu64" and %"PRIu64 50 " but it actually is %"PRIu64, 51 thresh_low, thresh_high, actual); 52 } 53 54 static void check_tsc_freq(int level) 55 { 56 u64 tsc_start, tsc_end, tsc_freq; 57 58 /* 59 * Reading the TSC twice with about a second's difference should give 60 * us an approximation of the TSC frequency from the guest's 61 * perspective. Now, this won't be completely accurate, but it should 62 * be good enough for the purposes of this test. 63 */ 64 tsc_start = rdmsr(MSR_IA32_TSC); 65 GUEST_SLEEP(1); 66 tsc_end = rdmsr(MSR_IA32_TSC); 67 68 tsc_freq = tsc_end - tsc_start; 69 70 GUEST_CHECK(level, tsc_freq); 71 } 72 73 static void l2_guest_code(void) 74 { 75 check_tsc_freq(UCHECK_L2); 76 77 /* exit to L1 */ 78 __asm__ __volatile__("vmcall"); 79 } 80 81 static void l1_svm_code(struct svm_test_data *svm) 82 { 83 /* check that L1's frequency looks alright before launching L2 */ 84 check_tsc_freq(UCHECK_L1); 85 86 generic_svm_setup(svm, l2_guest_code); 87 88 /* enable TSC scaling for L2 */ 89 wrmsr(MSR_AMD64_TSC_RATIO, L2_SCALE_FACTOR << 32); 90 91 /* launch L2 */ 92 run_guest(svm->vmcb, svm->vmcb_gpa); 93 GUEST_ASSERT(svm->vmcb->control.exit_code == SVM_EXIT_VMMCALL); 94 95 /* check that L1's frequency still looks good */ 96 check_tsc_freq(UCHECK_L1); 97 98 GUEST_DONE(); 99 } 100 101 static void l1_vmx_code(struct vmx_pages *vmx_pages) 102 { 103 u32 control; 104 105 /* check that L1's frequency looks alright before launching L2 */ 106 check_tsc_freq(UCHECK_L1); 107 108 GUEST_ASSERT(prepare_for_vmx_operation(vmx_pages)); 109 GUEST_ASSERT(load_vmcs(vmx_pages)); 110 111 /* prepare the VMCS for L2 execution */ 112 prepare_vmcs(vmx_pages, l2_guest_code); 113 114 /* enable TSC offsetting and TSC scaling for L2 */ 115 control = vmreadz(CPU_BASED_VM_EXEC_CONTROL); 116 control |= CPU_BASED_USE_MSR_BITMAPS | CPU_BASED_USE_TSC_OFFSETTING; 117 vmwrite(CPU_BASED_VM_EXEC_CONTROL, control); 118 119 control = vmreadz(SECONDARY_VM_EXEC_CONTROL); 120 control |= SECONDARY_EXEC_TSC_SCALING; 121 vmwrite(SECONDARY_VM_EXEC_CONTROL, control); 122 123 vmwrite(TSC_OFFSET, TSC_OFFSET_L2); 124 vmwrite(TSC_MULTIPLIER, TSC_MULTIPLIER_L2); 125 vmwrite(TSC_MULTIPLIER_HIGH, TSC_MULTIPLIER_L2 >> 32); 126 127 /* launch L2 */ 128 GUEST_ASSERT(!vmlaunch()); 129 GUEST_ASSERT(vmreadz(VM_EXIT_REASON) == EXIT_REASON_VMCALL); 130 131 /* check that L1's frequency still looks good */ 132 check_tsc_freq(UCHECK_L1); 133 134 GUEST_DONE(); 135 } 136 137 static void l1_guest_code(void *data) 138 { 139 if (this_cpu_has(X86_FEATURE_VMX)) 140 l1_vmx_code(data); 141 else 142 l1_svm_code(data); 143 } 144 145 int main(int argc, char *argv[]) 146 { 147 struct kvm_vcpu *vcpu; 148 struct kvm_vm *vm; 149 gva_t guest_gva = 0; 150 151 u64 tsc_start, tsc_end; 152 u64 tsc_khz; 153 u64 l1_scale_factor; 154 u64 l0_tsc_freq = 0; 155 u64 l1_tsc_freq = 0; 156 u64 l2_tsc_freq = 0; 157 158 TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_VMX) || 159 kvm_cpu_has(X86_FEATURE_SVM)); 160 TEST_REQUIRE(kvm_has_cap(KVM_CAP_TSC_CONTROL)); 161 TEST_REQUIRE(sys_clocksource_is_based_on_tsc()); 162 163 /* 164 * We set L1's scale factor to be a random number from 2 to 10. 165 * Ideally we would do the same for L2's factor but that one is 166 * referenced by both main() and l1_guest_code() and using a global 167 * variable does not work. 168 */ 169 srand(time(NULL)); 170 l1_scale_factor = (rand() % 9) + 2; 171 printf("L1's scale down factor is: %"PRIu64"\n", l1_scale_factor); 172 printf("L2's scale up factor is: %llu\n", L2_SCALE_FACTOR); 173 174 tsc_start = rdtsc(); 175 sleep(1); 176 tsc_end = rdtsc(); 177 178 l0_tsc_freq = tsc_end - tsc_start; 179 printf("real TSC frequency is around: %"PRIu64"\n", l0_tsc_freq); 180 181 vm = vm_create_with_one_vcpu(&vcpu, l1_guest_code); 182 183 if (kvm_cpu_has(X86_FEATURE_VMX)) 184 vcpu_alloc_vmx(vm, &guest_gva); 185 else 186 vcpu_alloc_svm(vm, &guest_gva); 187 188 vcpu_args_set(vcpu, 1, guest_gva); 189 190 tsc_khz = __vcpu_ioctl(vcpu, KVM_GET_TSC_KHZ, NULL); 191 TEST_ASSERT(tsc_khz != -1, "vcpu ioctl KVM_GET_TSC_KHZ failed"); 192 193 /* scale down L1's TSC frequency */ 194 vcpu_ioctl(vcpu, KVM_SET_TSC_KHZ, (void *) (tsc_khz / l1_scale_factor)); 195 196 for (;;) { 197 struct ucall uc; 198 199 vcpu_run(vcpu); 200 TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); 201 202 switch (get_ucall(vcpu, &uc)) { 203 case UCALL_ABORT: 204 REPORT_GUEST_ASSERT(uc); 205 case UCALL_SYNC: 206 switch (uc.args[0]) { 207 case USLEEP: 208 sleep(uc.args[1]); 209 break; 210 case UCHECK_L1: 211 l1_tsc_freq = uc.args[1]; 212 printf("L1's TSC frequency is around: %"PRIu64 213 "\n", l1_tsc_freq); 214 215 compare_tsc_freq(l1_tsc_freq, 216 l0_tsc_freq / l1_scale_factor); 217 break; 218 case UCHECK_L2: 219 l2_tsc_freq = uc.args[1]; 220 printf("L2's TSC frequency is around: %"PRIu64 221 "\n", l2_tsc_freq); 222 223 compare_tsc_freq(l2_tsc_freq, 224 l1_tsc_freq * L2_SCALE_FACTOR); 225 break; 226 } 227 break; 228 case UCALL_DONE: 229 goto done; 230 default: 231 TEST_FAIL("Unknown ucall %lu", uc.cmd); 232 } 233 } 234 235 done: 236 kvm_vm_free(vm); 237 return 0; 238 } 239