1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch_timer.c - Tests the arch timer IRQ functionality
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)
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.
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.
20 * Copyright (c) 2021, Google LLC.
24 #include <linux/sizes.h>
25 #include <linux/bitmap.h>
26 #include <sys/sysinfo.h>
28 #include "timer_test.h"
29 #include "ucall_common.h"
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,
40 struct kvm_vcpu *vcpus[KVM_MAX_VCPUS];
41 struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
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;
47 static void *test_vcpu_run(void *arg)
49 unsigned int vcpu_idx = (unsigned long)arg;
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];
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);
62 switch (get_ucall(vcpu, &uc)) {
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);
73 TEST_FAIL("Unexpected guest exit");
76 pr_info("PASS(vCPU-%d).\n", vcpu_idx);
81 static uint32_t test_get_pcpu(void)
84 unsigned int nproc_conf;
85 cpu_set_t online_cpuset;
87 nproc_conf = get_nprocs_conf();
88 sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);
90 /* Randomly find an available pCPU to place a vCPU on */
92 pcpu = rand() % nproc_conf;
93 } while (!CPU_ISSET(pcpu, &online_cpuset));
98 static int test_migrate_vcpu(unsigned int vcpu_idx)
102 uint32_t new_pcpu = test_get_pcpu();
105 CPU_SET(new_pcpu, &cpuset);
107 pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu_idx, new_pcpu);
109 ret = pthread_setaffinity_np(pt_vcpu_run[vcpu_idx],
110 sizeof(cpuset), &cpuset);
112 /* Allow the error where the vCPU thread is already finished */
113 TEST_ASSERT(ret == 0 || ret == ESRCH,
114 "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d",
115 vcpu_idx, new_pcpu, ret);
120 static void *test_vcpu_migration(void *arg)
122 unsigned int i, n_done;
126 usleep(msecs_to_usecs(test_args.migration_freq_ms));
128 for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
129 pthread_mutex_lock(&vcpu_done_map_lock);
130 vcpu_done = test_bit(i, vcpu_done_map);
131 pthread_mutex_unlock(&vcpu_done_map_lock);
138 test_migrate_vcpu(i);
140 } while (test_args.nr_vcpus != n_done);
145 static void test_run(struct kvm_vm *vm)
147 pthread_t pt_vcpu_migration;
151 pthread_mutex_init(&vcpu_done_map_lock, NULL);
152 vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
153 TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap");
155 for (i = 0; i < (unsigned long)test_args.nr_vcpus; i++) {
156 ret = pthread_create(&pt_vcpu_run[i], NULL, test_vcpu_run,
157 (void *)(unsigned long)i);
158 TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread", i);
161 /* Spawn a thread to control the vCPU migrations */
162 if (test_args.migration_freq_ms) {
165 ret = pthread_create(&pt_vcpu_migration, NULL,
166 test_vcpu_migration, NULL);
167 TEST_ASSERT(!ret, "Failed to create the migration pthread");
171 for (i = 0; i < test_args.nr_vcpus; i++)
172 pthread_join(pt_vcpu_run[i], NULL);
174 if (test_args.migration_freq_ms)
175 pthread_join(pt_vcpu_migration, NULL);
177 bitmap_free(vcpu_done_map);
180 static void test_print_help(char *name)
182 pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n"
183 "\t\t [-m migration_freq_ms] [-o counter_offset]\n"
184 "\t\t [-e timer_err_margin_us]\n", name);
185 pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
186 NR_VCPUS_DEF, KVM_MAX_VCPUS);
187 pr_info("\t-i: Number of iterations per stage (default: %u)\n",
189 pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
190 TIMER_TEST_PERIOD_MS_DEF);
191 pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
192 TIMER_TEST_MIGRATION_FREQ_MS);
193 pr_info("\t-o: Counter offset (in counter cycles, default: 0) [aarch64-only]\n");
194 pr_info("\t-e: Interrupt arrival error margin (in us) of the guest timer (default: %u)\n",
195 TIMER_TEST_ERR_MARGIN_US);
196 pr_info("\t-h: print this help screen\n");
199 static bool parse_args(int argc, char *argv[])
203 while ((opt = getopt(argc, argv, "hn:i:p:m:o:e:")) != -1) {
206 test_args.nr_vcpus = atoi_positive("Number of vCPUs", optarg);
207 if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
208 pr_info("Max allowed vCPUs: %u\n",
214 test_args.nr_iter = atoi_positive("Number of iterations", optarg);
217 test_args.timer_period_ms = atoi_positive("Periodicity", optarg);
220 test_args.migration_freq_ms = atoi_non_negative("Frequency", optarg);
223 test_args.timer_err_margin_us = atoi_non_negative("Error Margin", optarg);
226 test_args.counter_offset = strtol(optarg, NULL, 0);
227 test_args.reserved = 0;
238 test_print_help(argv[0]);
242 int main(int argc, char *argv[])
246 if (!parse_args(argc, argv))
249 __TEST_REQUIRE(!test_args.migration_freq_ms || get_nprocs() >= 2,
250 "At least two physical CPUs needed for vCPU migration");
252 vm = test_vm_create();