system_counter_offset_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021, Google LLC.
  4. *
  5. * Tests for adjusting the system counter from userspace
  6. */
  7. #include <asm/kvm_para.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include <time.h>
  12. #include "test_util.h"
  13. #include "kvm_util.h"
  14. #include "processor.h"
  15. #ifdef __x86_64__
  16. struct test_case {
  17. uint64_t tsc_offset;
  18. };
  19. static struct test_case test_cases[] = {
  20. { 0 },
  21. { 180 * NSEC_PER_SEC },
  22. { -180 * NSEC_PER_SEC },
  23. };
  24. static void check_preconditions(struct kvm_vcpu *vcpu)
  25. {
  26. __TEST_REQUIRE(!__vcpu_has_device_attr(vcpu, KVM_VCPU_TSC_CTRL,
  27. KVM_VCPU_TSC_OFFSET),
  28. "KVM_VCPU_TSC_OFFSET not supported; skipping test");
  29. }
  30. static void setup_system_counter(struct kvm_vcpu *vcpu, struct test_case *test)
  31. {
  32. vcpu_device_attr_set(vcpu, KVM_VCPU_TSC_CTRL, KVM_VCPU_TSC_OFFSET,
  33. &test->tsc_offset);
  34. }
  35. static uint64_t guest_read_system_counter(struct test_case *test)
  36. {
  37. return rdtsc();
  38. }
  39. static uint64_t host_read_guest_system_counter(struct test_case *test)
  40. {
  41. return rdtsc() + test->tsc_offset;
  42. }
  43. #else /* __x86_64__ */
  44. #error test not implemented for this architecture!
  45. #endif
  46. #define GUEST_SYNC_CLOCK(__stage, __val) \
  47. GUEST_SYNC_ARGS(__stage, __val, 0, 0, 0)
  48. static void guest_main(void)
  49. {
  50. int i;
  51. for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
  52. struct test_case *test = &test_cases[i];
  53. GUEST_SYNC_CLOCK(i, guest_read_system_counter(test));
  54. }
  55. }
  56. static void handle_sync(struct ucall *uc, uint64_t start, uint64_t end)
  57. {
  58. uint64_t obs = uc->args[2];
  59. TEST_ASSERT(start <= obs && obs <= end,
  60. "unexpected system counter value: %"PRIu64" expected range: [%"PRIu64", %"PRIu64"]",
  61. obs, start, end);
  62. pr_info("system counter value: %"PRIu64" expected range [%"PRIu64", %"PRIu64"]\n",
  63. obs, start, end);
  64. }
  65. static void handle_abort(struct ucall *uc)
  66. {
  67. REPORT_GUEST_ASSERT(*uc);
  68. }
  69. static void enter_guest(struct kvm_vcpu *vcpu)
  70. {
  71. uint64_t start, end;
  72. struct ucall uc;
  73. int i;
  74. for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
  75. struct test_case *test = &test_cases[i];
  76. setup_system_counter(vcpu, test);
  77. start = host_read_guest_system_counter(test);
  78. vcpu_run(vcpu);
  79. end = host_read_guest_system_counter(test);
  80. switch (get_ucall(vcpu, &uc)) {
  81. case UCALL_SYNC:
  82. handle_sync(&uc, start, end);
  83. break;
  84. case UCALL_ABORT:
  85. handle_abort(&uc);
  86. return;
  87. default:
  88. TEST_ASSERT(0, "unhandled ucall %ld",
  89. get_ucall(vcpu, &uc));
  90. }
  91. }
  92. }
  93. int main(void)
  94. {
  95. struct kvm_vcpu *vcpu;
  96. struct kvm_vm *vm;
  97. vm = vm_create_with_one_vcpu(&vcpu, guest_main);
  98. check_preconditions(vcpu);
  99. enter_guest(vcpu);
  100. kvm_vm_free(vm);
  101. }