steal_time.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * steal/stolen time test
  4. *
  5. * Copyright (C) 2020, Red Hat, Inc.
  6. */
  7. #define _GNU_SOURCE
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <sched.h>
  11. #include <pthread.h>
  12. #include <linux/kernel.h>
  13. #include <asm/kvm.h>
  14. #include <asm/kvm_para.h>
  15. #include "test_util.h"
  16. #include "kvm_util.h"
  17. #include "processor.h"
  18. #define NR_VCPUS 4
  19. #define ST_GPA_BASE (1 << 30)
  20. static void *st_gva[NR_VCPUS];
  21. static uint64_t guest_stolen_time[NR_VCPUS];
  22. #if defined(__x86_64__)
  23. /* steal_time must have 64-byte alignment */
  24. #define STEAL_TIME_SIZE ((sizeof(struct kvm_steal_time) + 63) & ~63)
  25. static void check_status(struct kvm_steal_time *st)
  26. {
  27. GUEST_ASSERT(!(READ_ONCE(st->version) & 1));
  28. GUEST_ASSERT(READ_ONCE(st->flags) == 0);
  29. GUEST_ASSERT(READ_ONCE(st->preempted) == 0);
  30. }
  31. static void guest_code(int cpu)
  32. {
  33. struct kvm_steal_time *st = st_gva[cpu];
  34. uint32_t version;
  35. GUEST_ASSERT(rdmsr(MSR_KVM_STEAL_TIME) == ((uint64_t)st_gva[cpu] | KVM_MSR_ENABLED));
  36. memset(st, 0, sizeof(*st));
  37. GUEST_SYNC(0);
  38. check_status(st);
  39. WRITE_ONCE(guest_stolen_time[cpu], st->steal);
  40. version = READ_ONCE(st->version);
  41. check_status(st);
  42. GUEST_SYNC(1);
  43. check_status(st);
  44. GUEST_ASSERT(version < READ_ONCE(st->version));
  45. WRITE_ONCE(guest_stolen_time[cpu], st->steal);
  46. check_status(st);
  47. GUEST_DONE();
  48. }
  49. static bool is_steal_time_supported(struct kvm_vcpu *vcpu)
  50. {
  51. return kvm_cpu_has(X86_FEATURE_KVM_STEAL_TIME);
  52. }
  53. static void steal_time_init(struct kvm_vcpu *vcpu, uint32_t i)
  54. {
  55. int ret;
  56. /* ST_GPA_BASE is identity mapped */
  57. st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
  58. sync_global_to_guest(vcpu->vm, st_gva[i]);
  59. ret = _vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME,
  60. (ulong)st_gva[i] | KVM_STEAL_RESERVED_MASK);
  61. TEST_ASSERT(ret == 0, "Bad GPA didn't fail");
  62. vcpu_set_msr(vcpu, MSR_KVM_STEAL_TIME, (ulong)st_gva[i] | KVM_MSR_ENABLED);
  63. }
  64. static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx)
  65. {
  66. struct kvm_steal_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
  67. int i;
  68. pr_info("VCPU%d:\n", vcpu_idx);
  69. pr_info(" steal: %lld\n", st->steal);
  70. pr_info(" version: %d\n", st->version);
  71. pr_info(" flags: %d\n", st->flags);
  72. pr_info(" preempted: %d\n", st->preempted);
  73. pr_info(" u8_pad: ");
  74. for (i = 0; i < 3; ++i)
  75. pr_info("%d", st->u8_pad[i]);
  76. pr_info("\n pad: ");
  77. for (i = 0; i < 11; ++i)
  78. pr_info("%d", st->pad[i]);
  79. pr_info("\n");
  80. }
  81. #elif defined(__aarch64__)
  82. /* PV_TIME_ST must have 64-byte alignment */
  83. #define STEAL_TIME_SIZE ((sizeof(struct st_time) + 63) & ~63)
  84. #define SMCCC_ARCH_FEATURES 0x80000001
  85. #define PV_TIME_FEATURES 0xc5000020
  86. #define PV_TIME_ST 0xc5000021
  87. struct st_time {
  88. uint32_t rev;
  89. uint32_t attr;
  90. uint64_t st_time;
  91. };
  92. static int64_t smccc(uint32_t func, uint64_t arg)
  93. {
  94. struct arm_smccc_res res;
  95. smccc_hvc(func, arg, 0, 0, 0, 0, 0, 0, &res);
  96. return res.a0;
  97. }
  98. static void check_status(struct st_time *st)
  99. {
  100. GUEST_ASSERT(READ_ONCE(st->rev) == 0);
  101. GUEST_ASSERT(READ_ONCE(st->attr) == 0);
  102. }
  103. static void guest_code(int cpu)
  104. {
  105. struct st_time *st;
  106. int64_t status;
  107. status = smccc(SMCCC_ARCH_FEATURES, PV_TIME_FEATURES);
  108. GUEST_ASSERT(status == 0);
  109. status = smccc(PV_TIME_FEATURES, PV_TIME_FEATURES);
  110. GUEST_ASSERT(status == 0);
  111. status = smccc(PV_TIME_FEATURES, PV_TIME_ST);
  112. GUEST_ASSERT(status == 0);
  113. status = smccc(PV_TIME_ST, 0);
  114. GUEST_ASSERT(status != -1);
  115. GUEST_ASSERT(status == (ulong)st_gva[cpu]);
  116. st = (struct st_time *)status;
  117. GUEST_SYNC(0);
  118. check_status(st);
  119. WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
  120. GUEST_SYNC(1);
  121. check_status(st);
  122. WRITE_ONCE(guest_stolen_time[cpu], st->st_time);
  123. GUEST_DONE();
  124. }
  125. static bool is_steal_time_supported(struct kvm_vcpu *vcpu)
  126. {
  127. struct kvm_device_attr dev = {
  128. .group = KVM_ARM_VCPU_PVTIME_CTRL,
  129. .attr = KVM_ARM_VCPU_PVTIME_IPA,
  130. };
  131. return !__vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
  132. }
  133. static void steal_time_init(struct kvm_vcpu *vcpu, uint32_t i)
  134. {
  135. struct kvm_vm *vm = vcpu->vm;
  136. uint64_t st_ipa;
  137. int ret;
  138. struct kvm_device_attr dev = {
  139. .group = KVM_ARM_VCPU_PVTIME_CTRL,
  140. .attr = KVM_ARM_VCPU_PVTIME_IPA,
  141. .addr = (uint64_t)&st_ipa,
  142. };
  143. vcpu_ioctl(vcpu, KVM_HAS_DEVICE_ATTR, &dev);
  144. /* ST_GPA_BASE is identity mapped */
  145. st_gva[i] = (void *)(ST_GPA_BASE + i * STEAL_TIME_SIZE);
  146. sync_global_to_guest(vm, st_gva[i]);
  147. st_ipa = (ulong)st_gva[i] | 1;
  148. ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
  149. TEST_ASSERT(ret == -1 && errno == EINVAL, "Bad IPA didn't report EINVAL");
  150. st_ipa = (ulong)st_gva[i];
  151. vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
  152. ret = __vcpu_ioctl(vcpu, KVM_SET_DEVICE_ATTR, &dev);
  153. TEST_ASSERT(ret == -1 && errno == EEXIST, "Set IPA twice without EEXIST");
  154. }
  155. static void steal_time_dump(struct kvm_vm *vm, uint32_t vcpu_idx)
  156. {
  157. struct st_time *st = addr_gva2hva(vm, (ulong)st_gva[vcpu_idx]);
  158. pr_info("VCPU%d:\n", vcpu_idx);
  159. pr_info(" rev: %d\n", st->rev);
  160. pr_info(" attr: %d\n", st->attr);
  161. pr_info(" st_time: %ld\n", st->st_time);
  162. }
  163. #endif
  164. static void *do_steal_time(void *arg)
  165. {
  166. struct timespec ts, stop;
  167. clock_gettime(CLOCK_MONOTONIC, &ts);
  168. stop = timespec_add_ns(ts, MIN_RUN_DELAY_NS);
  169. while (1) {
  170. clock_gettime(CLOCK_MONOTONIC, &ts);
  171. if (timespec_to_ns(timespec_sub(ts, stop)) >= 0)
  172. break;
  173. }
  174. return NULL;
  175. }
  176. static void run_vcpu(struct kvm_vcpu *vcpu)
  177. {
  178. struct ucall uc;
  179. vcpu_run(vcpu);
  180. switch (get_ucall(vcpu, &uc)) {
  181. case UCALL_SYNC:
  182. case UCALL_DONE:
  183. break;
  184. case UCALL_ABORT:
  185. REPORT_GUEST_ASSERT(uc);
  186. default:
  187. TEST_ASSERT(false, "Unexpected exit: %s",
  188. exit_reason_str(vcpu->run->exit_reason));
  189. }
  190. }
  191. int main(int ac, char **av)
  192. {
  193. struct kvm_vcpu *vcpus[NR_VCPUS];
  194. struct kvm_vm *vm;
  195. pthread_attr_t attr;
  196. pthread_t thread;
  197. cpu_set_t cpuset;
  198. unsigned int gpages;
  199. long stolen_time;
  200. long run_delay;
  201. bool verbose;
  202. int i;
  203. verbose = ac > 1 && (!strncmp(av[1], "-v", 3) || !strncmp(av[1], "--verbose", 10));
  204. /* Set CPU affinity so we can force preemption of the VCPU */
  205. CPU_ZERO(&cpuset);
  206. CPU_SET(0, &cpuset);
  207. pthread_attr_init(&attr);
  208. pthread_attr_setaffinity_np(&attr, sizeof(cpu_set_t), &cpuset);
  209. pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
  210. /* Create a VM and an identity mapped memslot for the steal time structure */
  211. vm = vm_create_with_vcpus(NR_VCPUS, guest_code, vcpus);
  212. gpages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, STEAL_TIME_SIZE * NR_VCPUS);
  213. vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, ST_GPA_BASE, 1, gpages, 0);
  214. virt_map(vm, ST_GPA_BASE, ST_GPA_BASE, gpages);
  215. ucall_init(vm, NULL);
  216. TEST_REQUIRE(is_steal_time_supported(vcpus[0]));
  217. /* Run test on each VCPU */
  218. for (i = 0; i < NR_VCPUS; ++i) {
  219. steal_time_init(vcpus[i], i);
  220. vcpu_args_set(vcpus[i], 1, i);
  221. /* First VCPU run initializes steal-time */
  222. run_vcpu(vcpus[i]);
  223. /* Second VCPU run, expect guest stolen time to be <= run_delay */
  224. run_vcpu(vcpus[i]);
  225. sync_global_from_guest(vm, guest_stolen_time[i]);
  226. stolen_time = guest_stolen_time[i];
  227. run_delay = get_run_delay();
  228. TEST_ASSERT(stolen_time <= run_delay,
  229. "Expected stolen time <= %ld, got %ld",
  230. run_delay, stolen_time);
  231. /* Steal time from the VCPU. The steal time thread has the same CPU affinity as the VCPUs. */
  232. run_delay = get_run_delay();
  233. pthread_create(&thread, &attr, do_steal_time, NULL);
  234. do
  235. sched_yield();
  236. while (get_run_delay() - run_delay < MIN_RUN_DELAY_NS);
  237. pthread_join(thread, NULL);
  238. run_delay = get_run_delay() - run_delay;
  239. TEST_ASSERT(run_delay >= MIN_RUN_DELAY_NS,
  240. "Expected run_delay >= %ld, got %ld",
  241. MIN_RUN_DELAY_NS, run_delay);
  242. /* Run VCPU again to confirm stolen time is consistent with run_delay */
  243. run_vcpu(vcpus[i]);
  244. sync_global_from_guest(vm, guest_stolen_time[i]);
  245. stolen_time = guest_stolen_time[i] - stolen_time;
  246. TEST_ASSERT(stolen_time >= run_delay,
  247. "Expected stolen time >= %ld, got %ld",
  248. run_delay, stolen_time);
  249. if (verbose) {
  250. pr_info("VCPU%d: total-stolen-time=%ld test-stolen-time=%ld", i,
  251. guest_stolen_time[i], stolen_time);
  252. if (stolen_time == run_delay)
  253. pr_info(" (BONUS: guest test-stolen-time even exactly matches test-run_delay)");
  254. pr_info("\n");
  255. steal_time_dump(vm, i);
  256. }
  257. }
  258. return 0;
  259. }