1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * arch_timer.c - Tests the arch timer IRQ functionality 4 * 5 * The guest's main thread configures the timer interrupt and waits 6 * for it to fire, with a timeout equal to the timer period. 7 * It asserts that the timeout doesn't exceed the timer period plus 8 * a user configurable error margin(default to 100us) 9 * 10 * On the other hand, upon receipt of an interrupt, the guest's interrupt 11 * handler validates the interrupt by checking if the architectural state 12 * is in compliance with the specifications. 13 * 14 * The test provides command-line options to configure the timer's 15 * period (-p), number of vCPUs (-n), iterations per stage (-i) and timer 16 * interrupt arrival error margin (-e). To stress-test the timer stack 17 * even more, an option to migrate the vCPUs across pCPUs (-m), at a 18 * particular rate, is also provided. 19 * 20 * Copyright (c) 2021, Google LLC. 21 */ 22 #include <stdlib.h> 23 #include <pthread.h> 24 #include <linux/sizes.h> 25 #include <linux/bitmap.h> 26 #include <sys/sysinfo.h> 27 28 #include "timer_test.h" 29 #include "ucall_common.h" 30 31 struct test_args test_args = { 32 .nr_vcpus = NR_VCPUS_DEF, 33 .nr_iter = NR_TEST_ITERS_DEF, 34 .timer_period_ms = TIMER_TEST_PERIOD_MS_DEF, 35 .migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS, 36 .timer_err_margin_us = TIMER_TEST_ERR_MARGIN_US, 37 .reserved = 1, 38 }; 39 40 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS]; 41 struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS]; 42 43 static pthread_t pt_vcpu_run[KVM_MAX_VCPUS]; 44 static unsigned long *vcpu_done_map; 45 static pthread_mutex_t vcpu_done_map_lock; 46 47 static void *test_vcpu_run(void *arg) 48 { 49 unsigned int vcpu_idx = (unsigned long)arg; 50 struct ucall uc; 51 struct kvm_vcpu *vcpu = vcpus[vcpu_idx]; 52 struct kvm_vm *vm = vcpu->vm; 53 struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[vcpu_idx]; 54 55 vcpu_run(vcpu); 56 57 /* Currently, any exit from guest is an indication of completion */ 58 pthread_mutex_lock(&vcpu_done_map_lock); 59 __set_bit(vcpu_idx, vcpu_done_map); 60 pthread_mutex_unlock(&vcpu_done_map_lock); 61 62 switch (get_ucall(vcpu, &uc)) { 63 case UCALL_SYNC: 64 case UCALL_DONE: 65 break; 66 case UCALL_ABORT: 67 sync_global_from_guest(vm, *shared_data); 68 fprintf(stderr, "Guest assert failed, vcpu %u; stage; %u; iter: %u\n", 69 vcpu_idx, shared_data->guest_stage, shared_data->nr_iter); 70 REPORT_GUEST_ASSERT(uc); 71 break; 72 default: 73 TEST_FAIL("Unexpected guest exit"); 74 } 75 76 pr_info("PASS(vCPU-%d).\n", vcpu_idx); 77 78 return NULL; 79 } 80 81 static uint32_t test_get_pcpu(void) 82 { 83 uint32_t pcpu; 84 unsigned int nproc_conf; 85 cpu_set_t online_cpuset; 86 87 nproc_conf = get_nprocs_conf(); 88 sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset); 89 90 /* Randomly find an available pCPU to place a vCPU on */ 91 do { 92 pcpu = rand() % nproc_conf; 93 } while (!CPU_ISSET(pcpu, &online_cpuset)); 94 95 return pcpu; 96 } 97 98 static int test_migrate_vcpu(unsigned int vcpu_idx) 99 { 100 int ret; 101 uint32_t new_pcpu = test_get_pcpu(); 102 103 pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu); 104 105 ret = __pin_task_to_cpu(pt_vcpu_run[vcpu_idx], new_pcpu); 106 107 /* Allow the error where the vCPU thread is already finished */ 108 TEST_ASSERT(ret == 0 || ret == ESRCH, 109 "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d", 110 vcpu_idx, new_pcpu, ret); 111 112 return ret; 113 } 114 115 static void *test_vcpu_migration(void *arg) 116 { 117 unsigned int i, n_done; 118 bool vcpu_done; 119 120 do { 121 usleep(msecs_to_usecs(test_args.migration_freq_ms)); 122 123 for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) { 124 pthread_mutex_lock(&vcpu_done_map_lock); 125 vcpu_done = test_bit(i, vcpu_done_map); 126 pthread_mutex_unlock(&vcpu_done_map_lock); 127 128 if (vcpu_done) { 129 n_done++; 130 continue; 131 } 132 133 test_migrate_vcpu(i); 134 } 135 } while (test_args.nr_vcpus != n_done); 136 137 return NULL; 138 } 139 140 static void test_run(struct kvm_vm *vm) 141 { 142 pthread_t pt_vcpu_migration; 143 unsigned int i; 144 int ret; 145 146 pthread_mutex_init(&vcpu_done_map_lock, NULL); 147 vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus); 148 TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap"); 149 150 for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) { 151 ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run, 152 (void *)(unsigned long)i); 153 TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i); 154 } 155 156 /* Spawn a thread to control the vCPU migrations */ 157 if (test_args.migration_freq_ms) { 158 srand(time(NULL)); 159 160 ret = pthread_create(&pt_vcpu_migration, NULL, 161 test_vcpu_migration, NULL); 162 TEST_ASSERT(!ret, "Failed to create the migration pthread"); 163 } 164 165 166 for (i = 0; i < test_args.nr_vcpus; i++) 167 pthread_join(pt_vcpu_run[i], NULL); 168 169 if (test_args.migration_freq_ms) 170 pthread_join(pt_vcpu_migration, NULL); 171 172 bitmap_free(vcpu_done_map); 173 } 174 175 static void test_print_help(char *name) 176 { 177 pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n" 178 "\t\t [-m migration_freq_ms] [-o counter_offset]\n" 179 "\t\t [-e timer_err_margin_us]\n", name); 180 pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n", 181 NR_VCPUS_DEF, KVM_MAX_VCPUS); 182 pr_info("\t-i: Number of iterations per stage (default: %u)\n", 183 NR_TEST_ITERS_DEF); 184 pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n", 185 TIMER_TEST_PERIOD_MS_DEF); 186 pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n", 187 TIMER_TEST_MIGRATION_FREQ_MS); 188 pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n"); 189 pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n", 190 TIMER_TEST_ERR_MARGIN_US); 191 pr_info("\t-h: print this help screen\n"); 192 } 193 194 static bool parse_args(int argc, char *argv[]) 195 { 196 int opt; 197 198 while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) { 199 switch (opt) { 200 case 'n': 201 test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg); 202 if (test_args.nr_vcpus > KVM_MAX_VCPUS) { 203 pr_info("Max allowed vCPUs: %u\n", 204 KVM_MAX_VCPUS); 205 goto err; 206 } 207 break; 208 case 'i': 209 test_args.nr_iter = atoi_positive("Number of iterations", optarg); 210 break; 211 case 'p': 212 test_args.timer_period_ms = atoi_positive("Periodicity", optarg); 213 break; 214 case 'm': 215 test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg); 216 break; 217 case 'e': 218 test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg); 219 break; 220 case 'o': 221 test_args.counter_offset = strtol(optarg, NULL, 0); 222 test_args.reserved = 0; 223 break; 224 case 'h': 225 default: 226 goto err; 227 } 228 } 229 230 return true; 231 232 err: 233 test_print_help(argv[0]); 234 return false; 235 } 236 237 int main(int argc, char *argv[]) 238 { 239 struct kvm_vm *vm; 240 241 if (!parse_args(argc, argv)) 242 exit(KSFT_SKIP); 243 244 __TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2, 245 "At least two physical CPUs needed for vCPU migration"); 246 247 vm = test_vm_create(); 248 test_run(vm); 249 test_vm_cleanup(vm); 250 251 return 0; 252 } 253