1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch_timer.c - Tests the aarch64 timer IRQ functionality
5 * The test validates both the virtual and physical timer IRQs using
6 * CVAL and TVAL registers. This consitutes the four stages in the test.
7 * The guest's main thread configures the timer interrupt for a stage
8 * and waits for it to fire, with a timeout equal to the timer period.
9 * It asserts that the timeout doesn't exceed the timer period.
11 * On the other hand, upon receipt of an interrupt, the guest's interrupt
12 * handler validates the interrupt by checking if the architectural state
13 * is in compliance with the specifications.
15 * The test provides command-line options to configure the timer's
16 * period (-p), number of vCPUs (-n), and iterations per stage (-i).
17 * To stress-test the timer stack even more, an option to migrate the
18 * vCPUs across pCPUs (-m), at a particular rate, is also provided.
20 * Copyright (c) 2021, Google LLC.
27 #include <linux/kvm.h>
28 #include <linux/sizes.h>
29 #include <linux/bitmap.h>
30 #include <sys/sysinfo.h>
33 #include "processor.h"
35 #include "arch_timer.h"
39 #define NR_VCPUS_DEF 4
40 #define NR_TEST_ITERS_DEF 5
41 #define TIMER_TEST_PERIOD_MS_DEF 10
42 #define TIMER_TEST_ERR_MARGIN_US 100
43 #define TIMER_TEST_MIGRATION_FREQ_MS 2
49 int migration_freq_ms;
52 static struct test_args test_args = {
53 .nr_vcpus = NR_VCPUS_DEF,
54 .nr_iter = NR_TEST_ITERS_DEF,
55 .timer_period_ms = TIMER_TEST_PERIOD_MS_DEF,
56 .migration_freq_ms = TIMER_TEST_MIGRATION_FREQ_MS,
59 #define msecs_to_usecs(msec) ((msec) * 1000LL)
61 #define GICD_BASE_GPA 0x8000000ULL
62 #define GICR_BASE_GPA 0x80A0000ULL
65 GUEST_STAGE_VTIMER_CVAL = 1,
66 GUEST_STAGE_VTIMER_TVAL,
67 GUEST_STAGE_PTIMER_CVAL,
68 GUEST_STAGE_PTIMER_TVAL,
72 /* Shared variables between host and guest */
73 struct test_vcpu_shared_data {
75 enum guest_stage guest_stage;
81 pthread_t pt_vcpu_run;
85 static struct test_vcpu test_vcpu[KVM_MAX_VCPUS];
86 static struct test_vcpu_shared_data vcpu_shared_data[KVM_MAX_VCPUS];
88 static int vtimer_irq, ptimer_irq;
90 static unsigned long *vcpu_done_map;
91 static pthread_mutex_t vcpu_done_map_lock;
94 guest_configure_timer_action(struct test_vcpu_shared_data *shared_data)
96 switch (shared_data->guest_stage) {
97 case GUEST_STAGE_VTIMER_CVAL:
98 timer_set_next_cval_ms(VIRTUAL, test_args.timer_period_ms);
99 shared_data->xcnt = timer_get_cntct(VIRTUAL);
100 timer_set_ctl(VIRTUAL, CTL_ENABLE);
102 case GUEST_STAGE_VTIMER_TVAL:
103 timer_set_next_tval_ms(VIRTUAL, test_args.timer_period_ms);
104 shared_data->xcnt = timer_get_cntct(VIRTUAL);
105 timer_set_ctl(VIRTUAL, CTL_ENABLE);
107 case GUEST_STAGE_PTIMER_CVAL:
108 timer_set_next_cval_ms(PHYSICAL, test_args.timer_period_ms);
109 shared_data->xcnt = timer_get_cntct(PHYSICAL);
110 timer_set_ctl(PHYSICAL, CTL_ENABLE);
112 case GUEST_STAGE_PTIMER_TVAL:
113 timer_set_next_tval_ms(PHYSICAL, test_args.timer_period_ms);
114 shared_data->xcnt = timer_get_cntct(PHYSICAL);
115 timer_set_ctl(PHYSICAL, CTL_ENABLE);
122 static void guest_validate_irq(unsigned int intid,
123 struct test_vcpu_shared_data *shared_data)
125 enum guest_stage stage = shared_data->guest_stage;
126 uint64_t xcnt = 0, xcnt_diff_us, cval = 0;
127 unsigned long xctl = 0;
128 unsigned int timer_irq = 0;
130 if (stage == GUEST_STAGE_VTIMER_CVAL ||
131 stage == GUEST_STAGE_VTIMER_TVAL) {
132 xctl = timer_get_ctl(VIRTUAL);
133 timer_set_ctl(VIRTUAL, CTL_IMASK);
134 xcnt = timer_get_cntct(VIRTUAL);
135 cval = timer_get_cval(VIRTUAL);
136 timer_irq = vtimer_irq;
137 } else if (stage == GUEST_STAGE_PTIMER_CVAL ||
138 stage == GUEST_STAGE_PTIMER_TVAL) {
139 xctl = timer_get_ctl(PHYSICAL);
140 timer_set_ctl(PHYSICAL, CTL_IMASK);
141 xcnt = timer_get_cntct(PHYSICAL);
142 cval = timer_get_cval(PHYSICAL);
143 timer_irq = ptimer_irq;
148 xcnt_diff_us = cycles_to_usec(xcnt - shared_data->xcnt);
150 /* Make sure we are dealing with the correct timer IRQ */
151 GUEST_ASSERT_2(intid == timer_irq, intid, timer_irq);
153 /* Basic 'timer condition met' check */
154 GUEST_ASSERT_3(xcnt >= cval, xcnt, cval, xcnt_diff_us);
155 GUEST_ASSERT_1(xctl & CTL_ISTATUS, xctl);
158 static void guest_irq_handler(struct ex_regs *regs)
160 unsigned int intid = gic_get_and_ack_irq();
161 uint32_t cpu = guest_get_vcpuid();
162 struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
164 guest_validate_irq(intid, shared_data);
166 WRITE_ONCE(shared_data->nr_iter, shared_data->nr_iter + 1);
171 static void guest_run_stage(struct test_vcpu_shared_data *shared_data,
172 enum guest_stage stage)
174 uint32_t irq_iter, config_iter;
176 shared_data->guest_stage = stage;
177 shared_data->nr_iter = 0;
179 for (config_iter = 0; config_iter < test_args.nr_iter; config_iter++) {
180 /* Setup the next interrupt */
181 guest_configure_timer_action(shared_data);
183 /* Setup a timeout for the interrupt to arrive */
184 udelay(msecs_to_usecs(test_args.timer_period_ms) +
185 TIMER_TEST_ERR_MARGIN_US);
187 irq_iter = READ_ONCE(shared_data->nr_iter);
188 GUEST_ASSERT_2(config_iter + 1 == irq_iter,
189 config_iter + 1, irq_iter);
193 static void guest_code(void)
195 uint32_t cpu = guest_get_vcpuid();
196 struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[cpu];
200 gic_init(GIC_V3, test_args.nr_vcpus,
201 (void *)GICD_BASE_GPA, (void *)GICR_BASE_GPA);
203 timer_set_ctl(VIRTUAL, CTL_IMASK);
204 timer_set_ctl(PHYSICAL, CTL_IMASK);
206 gic_irq_enable(vtimer_irq);
207 gic_irq_enable(ptimer_irq);
210 guest_run_stage(shared_data, GUEST_STAGE_VTIMER_CVAL);
211 guest_run_stage(shared_data, GUEST_STAGE_VTIMER_TVAL);
212 guest_run_stage(shared_data, GUEST_STAGE_PTIMER_CVAL);
213 guest_run_stage(shared_data, GUEST_STAGE_PTIMER_TVAL);
218 static void *test_vcpu_run(void *arg)
221 struct test_vcpu *vcpu = arg;
222 struct kvm_vm *vm = vcpu->vm;
223 uint32_t vcpuid = vcpu->vcpuid;
224 struct test_vcpu_shared_data *shared_data = &vcpu_shared_data[vcpuid];
226 vcpu_run(vm, vcpuid);
228 /* Currently, any exit from guest is an indication of completion */
229 pthread_mutex_lock(&vcpu_done_map_lock);
230 set_bit(vcpuid, vcpu_done_map);
231 pthread_mutex_unlock(&vcpu_done_map_lock);
233 switch (get_ucall(vm, vcpuid, &uc)) {
238 sync_global_from_guest(vm, *shared_data);
239 TEST_FAIL("%s at %s:%ld\n\tvalues: %lu, %lu; %lu, vcpu: %u; stage: %u; iter: %u",
240 (const char *)uc.args[0], __FILE__, uc.args[1],
241 uc.args[2], uc.args[3], uc.args[4], vcpuid,
242 shared_data->guest_stage, shared_data->nr_iter);
245 TEST_FAIL("Unexpected guest exit\n");
251 static uint32_t test_get_pcpu(void)
254 unsigned int nproc_conf;
255 cpu_set_t online_cpuset;
257 nproc_conf = get_nprocs_conf();
258 sched_getaffinity(0, sizeof(cpu_set_t), &online_cpuset);
260 /* Randomly find an available pCPU to place a vCPU on */
262 pcpu = rand() % nproc_conf;
263 } while (!CPU_ISSET(pcpu, &online_cpuset));
268 static int test_migrate_vcpu(struct test_vcpu *vcpu)
272 uint32_t new_pcpu = test_get_pcpu();
275 CPU_SET(new_pcpu, &cpuset);
277 pr_debug("Migrating vCPU: %u to pCPU: %u\n", vcpu->vcpuid, new_pcpu);
279 ret = pthread_setaffinity_np(vcpu->pt_vcpu_run,
280 sizeof(cpuset), &cpuset);
282 /* Allow the error where the vCPU thread is already finished */
283 TEST_ASSERT(ret == 0 || ret == ESRCH,
284 "Failed to migrate the vCPU:%u to pCPU: %u; ret: %d\n",
285 vcpu->vcpuid, new_pcpu, ret);
290 static void *test_vcpu_migration(void *arg)
292 unsigned int i, n_done;
296 usleep(msecs_to_usecs(test_args.migration_freq_ms));
298 for (n_done = 0, i = 0; i < test_args.nr_vcpus; i++) {
299 pthread_mutex_lock(&vcpu_done_map_lock);
300 vcpu_done = test_bit(i, vcpu_done_map);
301 pthread_mutex_unlock(&vcpu_done_map_lock);
308 test_migrate_vcpu(&test_vcpu[i]);
310 } while (test_args.nr_vcpus != n_done);
315 static void test_run(struct kvm_vm *vm)
318 pthread_t pt_vcpu_migration;
320 pthread_mutex_init(&vcpu_done_map_lock, NULL);
321 vcpu_done_map = bitmap_zalloc(test_args.nr_vcpus);
322 TEST_ASSERT(vcpu_done_map, "Failed to allocate vcpu done bitmap\n");
324 for (i = 0; i < test_args.nr_vcpus; i++) {
325 ret = pthread_create(&test_vcpu[i].pt_vcpu_run, NULL,
326 test_vcpu_run, &test_vcpu[i]);
327 TEST_ASSERT(!ret, "Failed to create vCPU-%d pthread\n", i);
330 /* Spawn a thread to control the vCPU migrations */
331 if (test_args.migration_freq_ms) {
334 ret = pthread_create(&pt_vcpu_migration, NULL,
335 test_vcpu_migration, NULL);
336 TEST_ASSERT(!ret, "Failed to create the migration pthread\n");
340 for (i = 0; i < test_args.nr_vcpus; i++)
341 pthread_join(test_vcpu[i].pt_vcpu_run, NULL);
343 if (test_args.migration_freq_ms)
344 pthread_join(pt_vcpu_migration, NULL);
346 bitmap_free(vcpu_done_map);
349 static void test_init_timer_irq(struct kvm_vm *vm)
351 /* Timer initid should be same for all the vCPUs, so query only vCPU-0 */
352 int vcpu0_fd = vcpu_get_fd(vm, 0);
354 kvm_device_access(vcpu0_fd, KVM_ARM_VCPU_TIMER_CTRL,
355 KVM_ARM_VCPU_TIMER_IRQ_PTIMER, &ptimer_irq, false);
356 kvm_device_access(vcpu0_fd, KVM_ARM_VCPU_TIMER_CTRL,
357 KVM_ARM_VCPU_TIMER_IRQ_VTIMER, &vtimer_irq, false);
359 sync_global_to_guest(vm, ptimer_irq);
360 sync_global_to_guest(vm, vtimer_irq);
362 pr_debug("ptimer_irq: %d; vtimer_irq: %d\n", ptimer_irq, vtimer_irq);
365 static struct kvm_vm *test_vm_create(void)
370 int nr_vcpus = test_args.nr_vcpus;
372 vm = vm_create_default_with_vcpus(nr_vcpus, 0, 0, guest_code, NULL);
374 vm_init_descriptor_tables(vm);
375 vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
377 for (i = 0; i < nr_vcpus; i++) {
378 vcpu_init_descriptor_tables(vm, i);
380 test_vcpu[i].vcpuid = i;
381 test_vcpu[i].vm = vm;
384 ucall_init(vm, NULL);
385 test_init_timer_irq(vm);
386 ret = vgic_v3_setup(vm, nr_vcpus, 64, GICD_BASE_GPA, GICR_BASE_GPA);
388 print_skip("Failed to create vgic-v3");
392 /* Make all the test's cmdline args visible to the guest */
393 sync_global_to_guest(vm, test_args);
398 static void test_print_help(char *name)
400 pr_info("Usage: %s [-h] [-n nr_vcpus] [-i iterations] [-p timer_period_ms]\n",
402 pr_info("\t-n: Number of vCPUs to configure (default: %u; max: %u)\n",
403 NR_VCPUS_DEF, KVM_MAX_VCPUS);
404 pr_info("\t-i: Number of iterations per stage (default: %u)\n",
406 pr_info("\t-p: Periodicity (in ms) of the guest timer (default: %u)\n",
407 TIMER_TEST_PERIOD_MS_DEF);
408 pr_info("\t-m: Frequency (in ms) of vCPUs to migrate to different pCPU. 0 to turn off (default: %u)\n",
409 TIMER_TEST_MIGRATION_FREQ_MS);
410 pr_info("\t-h: print this help screen\n");
413 static bool parse_args(int argc, char *argv[])
417 while ((opt = getopt(argc, argv, "hn:i:p:m:")) != -1) {
420 test_args.nr_vcpus = atoi(optarg);
421 if (test_args.nr_vcpus <= 0) {
422 pr_info("Positive value needed for -n\n");
424 } else if (test_args.nr_vcpus > KVM_MAX_VCPUS) {
425 pr_info("Max allowed vCPUs: %u\n",
431 test_args.nr_iter = atoi(optarg);
432 if (test_args.nr_iter <= 0) {
433 pr_info("Positive value needed for -i\n");
438 test_args.timer_period_ms = atoi(optarg);
439 if (test_args.timer_period_ms <= 0) {
440 pr_info("Positive value needed for -p\n");
445 test_args.migration_freq_ms = atoi(optarg);
446 if (test_args.migration_freq_ms < 0) {
447 pr_info("0 or positive value needed for -m\n");
460 test_print_help(argv[0]);
464 int main(int argc, char *argv[])
468 /* Tell stdout not to buffer its content */
469 setbuf(stdout, NULL);
471 if (!parse_args(argc, argv))
474 if (test_args.migration_freq_ms && get_nprocs() < 2) {
475 print_skip("At least two physical CPUs needed for vCPU migration");
479 vm = test_vm_create();