1 // SPDX-License-Identifier: GPL-2.0
3 * steal/stolen time test
5 * Copyright (C) 2020, Red Hat, Inc.
12 #include <linux/kernel.h>
13 #include <sys/syscall.h>
15 #include <asm/kvm_para.h>
17 #include "test_util.h"
19 #include "processor.h"
22 #define ST_GPA_BASE (1 << 30)
23 #define MIN_RUN_DELAY_NS 200000UL
25 static void *st_gva[NR_VCPUS];
26 static uint64_t guest_stolen_time[NR_VCPUS];
28 #if defined(__x86_64__)
30 /* steal_time must have 64-byte alignment */
31 #define STEAL_TIME_SIZE ((sizeof(struct kvm_steal_time) + 63) & ~63)
33 static void check_status(struct kvm_steal_time *st)
35 GUEST_ASSERT(!(READ_ONCE(st->version) & 1));
36 GUEST_ASSERT(READ_ONCE(st->flags) == 0);
37 GUEST_ASSERT(READ_ONCE(st->preempted) == 0);
40 static void guest_code(int cpu)
42 struct kvm_steal_time *st = st_gva[cpu];
45 GUEST_ASSERT(rdmsr(MSR_KVM_STEAL_TIME) == ((uint64_t)st_gva[cpu] | KVM_MSR_ENABLED));
47 memset(st, 0, sizeof(*st));
51 WRITE_ONCE(guest_stolen_time[cpu], st->steal);
52 version = READ_ONCE(st->version);
57 GUEST_ASSERT(version < READ_ONCE(st->version));
58 WRITE_ONCE(guest_stolen_time[cpu], st->steal);
63 static void steal_time_init(struct kvm_vm *vm)
67 if (!(kvm_get_supported_cpuid_entry(KVM_CPUID_FEATURES)->eax &
68 KVM_FEATURE_STEAL_TIME)) {
69 print_skip("steal-time not supported");
73 for (i = 0; i < NR_VCPUS; ++i) {
76 /* ST_GPA_BASE is identity mapped */
77 st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
78 sync_global_to_guest(vm, st_gva[i]);
80 ret = _vcpu_set_msr(vm, i, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_STEAL_RESERVED_MASK);
81 TEST_ASSERT(ret == 0, "Bad GPA didn't fail");
83 vcpu_set_msr(vm, i, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_MSR_ENABLED);
87 static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpuid)
89 struct kvm_steal_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpuid]);
92 pr_info("VCPU%d:\n", vcpuid);
93 pr_info(" steal: %lld\n", st->steal);
94 pr_info(" version: %d\n", st->version);
95 pr_info(" flags: %d\n", st->flags);
96 pr_info(" preempted: %d\n", st->preempted);
98 for (i = 0; i < 3; ++i)
99 pr_info("%d", st->u8_pad[i]);
101 for (i = 0; i < 11; ++i)
102 pr_info("%d", st->pad[i]);
106 #elif defined(__aarch64__)
108 /* PV_TIME_ST must have 64-byte alignment */
109 #define STEAL_TIME_SIZE ((sizeof(struct st_time) + 63) & ~63)
111 #define SMCCC_ARCH_FEATURES 0x80000001
112 #define PV_TIME_FEATURES 0xc5000020
113 #define PV_TIME_ST 0xc5000021
121 static int64_t smccc(uint32_t func, uint32_t arg)
130 : "=r" (ret) : "r" (func), "r" (arg) :
131 "x0", "x1", "x2", "x3");
136 static void check_status(struct st_time *st)
138 GUEST_ASSERT(READ_ONCE(st->rev) == 0);
139 GUEST_ASSERT(READ_ONCE(st->attr) == 0);
142 static void guest_code(int cpu)
147 status = smccc(SMCCC_ARCH_FEATURES, PV_TIME_FEATURES);
148 GUEST_ASSERT(status == 0);
149 status = smccc(PV_TIME_FEATURES, PV_TIME_FEATURES);
150 GUEST_ASSERT(status == 0);
151 status = smccc(PV_TIME_FEATURES, PV_TIME_ST);
152 GUEST_ASSERT(status == 0);
154 status = smccc(PV_TIME_ST, 0);
155 GUEST_ASSERT(status != -1);
156 GUEST_ASSERT(status == (ulong)st_gva[cpu]);
158 st = (struct st_time *)status;
162 WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
166 WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
170 static void steal_time_init(struct kvm_vm *vm)
172 struct kvm_device_attr dev = {
173 .group = KVM_ARM_VCPU_PVTIME_CTRL,
174 .attr = KVM_ARM_VCPU_PVTIME_IPA,
178 ret = _vcpu_ioctl(vm, 0, KVM_HAS_DEVICE_ATTR, &dev);
179 if (ret != 0 && errno == ENXIO) {
180 print_skip("steal-time not supported");
184 for (i = 0; i < NR_VCPUS; ++i) {
187 vcpu_ioctl(vm, i, KVM_HAS_DEVICE_ATTR, &dev);
189 dev.addr = (uint64_t)&st_ipa;
191 /* ST_GPA_BASE is identity mapped */
192 st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
193 sync_global_to_guest(vm, st_gva[i]);
195 st_ipa = (ulong)st_gva[i] | 1;
196 ret = _vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
197 TEST_ASSERT(ret == -1 && errno == EINVAL, "Bad IPA didn't report EINVAL");
199 st_ipa = (ulong)st_gva[i];
200 vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
202 ret = _vcpu_ioctl(vm, i, KVM_SET_DEVICE_ATTR, &dev);
203 TEST_ASSERT(ret == -1 && errno == EEXIST, "Set IPA twice without EEXIST");
208 static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpuid)
210 struct st_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpuid]);
212 pr_info("VCPU%d:\n", vcpuid);
213 pr_info(" rev: %d\n", st->rev);
214 pr_info(" attr: %d\n", st->attr);
215 pr_info(" st_time: %ld\n", st->st_time);
220 static long get_run_delay(void)
226 sprintf(path, "/proc/%ld/schedstat", syscall(SYS_gettid));
227 fp = fopen(path, "r");
228 fscanf(fp, "%ld %ld ", &val[0], &val[1]);
234 static void *do_steal_time(void *arg)
236 struct timespec ts, stop;
238 clock_gettime(CLOCK_MONOTONIC, &ts);
239 stop = timespec_add_ns(ts, MIN_RUN_DELAY_NS);
242 clock_gettime(CLOCK_MONOTONIC, &ts);
243 if (timespec_to_ns(timespec_sub(ts, stop)) >= 0)
250 static void run_vcpu(struct kvm_vm *vm, uint32_t vcpuid)
254 vcpu_args_set(vm, vcpuid, 1, vcpuid);
256 vcpu_ioctl(vm, vcpuid, KVM_RUN, NULL);
258 switch (get_ucall(vm, vcpuid, &uc)) {
263 TEST_ASSERT(false, "%s at %s:%ld", (const char *)uc.args[0],
264 __FILE__, uc.args[1]);
266 TEST_ASSERT(false, "Unexpected exit: %s",
267 exit_reason_str(vcpu_state(vm, vcpuid)->exit_reason));
271 int main(int ac, char **av)
283 verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
285 /* Set CPU affinity so we can force preemption of the VCPU */
288 pthread_attr_init(&attr);
289 pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
290 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
292 /* Create a one VCPU guest and an identity mapped memslot for the steal time structure */
293 vm = vm_create_default(0, 0, guest_code);
294 gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
295 vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
296 virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
297 ucall_init(vm, NULL);
299 /* Add the rest of the VCPUs */
300 for (i = 1; i < NR_VCPUS; ++i)
301 vm_vcpu_add_default(vm, i, guest_code);
305 /* Run test on each VCPU */
306 for (i = 0; i < NR_VCPUS; ++i) {
307 /* First VCPU run initializes steal-time */
310 /* Second VCPU run, expect guest stolen time to be <= run_delay */
312 sync_global_from_guest(vm, guest_stolen_time[i]);
313 stolen_time = guest_stolen_time[i];
314 run_delay = get_run_delay();
315 TEST_ASSERT(stolen_time <= run_delay,
316 "Expected stolen time <= %ld, got %ld",
317 run_delay, stolen_time);
319 /* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
320 run_delay = get_run_delay();
321 pthread_create(&thread, &attr, do_steal_time, NULL);
324 while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);
325 pthread_join(thread, NULL);
326 run_delay = get_run_delay() - run_delay;
327 TEST_ASSERT(run_delay >= MIN_RUN_DELAY_NS,
328 "Expected run_delay >= %ld, got %ld",
329 MIN_RUN_DELAY_NS, run_delay);
331 /* Run VCPU again to confirm stolen time is consistent with run_delay */
333 sync_global_from_guest(vm, guest_stolen_time[i]);
334 stolen_time = guest_stolen_time[i] - stolen_time;
335 TEST_ASSERT(stolen_time >= run_delay,
336 "Expected stolen time >= %ld, got %ld",
337 run_delay, stolen_time);
340 pr_info("VCPU%d: total-stolen-time=%ld test-stolen-time=%ld", i,
341 guest_stolen_time[i], stolen_time);
342 if (stolen_time == run_delay)
343 pr_info(" (BONUS: guest test-stolen-time even exactly matches test-run_delay)");
345 steal_time_dump(vm, i);