fix_hypercall_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020, Google LLC.
  4. *
  5. * Tests for KVM paravirtual feature disablement
  6. */
  7. #include <asm/kvm_para.h>
  8. #include <linux/kvm_para.h>
  9. #include <linux/stringify.h>
  10. #include <stdint.h>
  11. #include "kvm_test_harness.h"
  12. #include "apic.h"
  13. #include "test_util.h"
  14. #include "kvm_util.h"
  15. #include "processor.h"
  16. /* VMCALL and VMMCALL are both 3-byte opcodes. */
  17. #define HYPERCALL_INSN_SIZE 3
  18. static bool quirk_disabled;
  19. static void guest_ud_handler(struct ex_regs *regs)
  20. {
  21. regs->rax = -EFAULT;
  22. regs->rip += HYPERCALL_INSN_SIZE;
  23. }
  24. static const uint8_t vmx_vmcall[HYPERCALL_INSN_SIZE] = { 0x0f, 0x01, 0xc1 };
  25. static const uint8_t svm_vmmcall[HYPERCALL_INSN_SIZE] = { 0x0f, 0x01, 0xd9 };
  26. extern uint8_t hypercall_insn[HYPERCALL_INSN_SIZE];
  27. static uint64_t do_sched_yield(uint8_t apic_id)
  28. {
  29. uint64_t ret;
  30. asm volatile("hypercall_insn:\n\t"
  31. ".byte 0xcc,0xcc,0xcc\n\t"
  32. : "=a"(ret)
  33. : "a"((uint64_t)KVM_HC_SCHED_YIELD), "b"((uint64_t)apic_id)
  34. : "memory");
  35. return ret;
  36. }
  37. static void guest_main(void)
  38. {
  39. const uint8_t *native_hypercall_insn;
  40. const uint8_t *other_hypercall_insn;
  41. uint64_t ret;
  42. if (host_cpu_is_intel) {
  43. native_hypercall_insn = vmx_vmcall;
  44. other_hypercall_insn = svm_vmmcall;
  45. } else if (host_cpu_is_amd) {
  46. native_hypercall_insn = svm_vmmcall;
  47. other_hypercall_insn = vmx_vmcall;
  48. } else {
  49. GUEST_ASSERT(0);
  50. /* unreachable */
  51. return;
  52. }
  53. memcpy(hypercall_insn, other_hypercall_insn, HYPERCALL_INSN_SIZE);
  54. ret = do_sched_yield(GET_APIC_ID_FIELD(xapic_read_reg(APIC_ID)));
  55. /*
  56. * If the quirk is disabled, verify that guest_ud_handler() "returned"
  57. * -EFAULT and that KVM did NOT patch the hypercall. If the quirk is
  58. * enabled, verify that the hypercall succeeded and that KVM patched in
  59. * the "right" hypercall.
  60. */
  61. if (quirk_disabled) {
  62. GUEST_ASSERT(ret == (uint64_t)-EFAULT);
  63. GUEST_ASSERT(!memcmp(other_hypercall_insn, hypercall_insn,
  64. HYPERCALL_INSN_SIZE));
  65. } else {
  66. GUEST_ASSERT(!ret);
  67. GUEST_ASSERT(!memcmp(native_hypercall_insn, hypercall_insn,
  68. HYPERCALL_INSN_SIZE));
  69. }
  70. GUEST_DONE();
  71. }
  72. KVM_ONE_VCPU_TEST_SUITE(fix_hypercall);
  73. static void enter_guest(struct kvm_vcpu *vcpu)
  74. {
  75. struct kvm_run *run = vcpu->run;
  76. struct ucall uc;
  77. vcpu_run(vcpu);
  78. switch (get_ucall(vcpu, &uc)) {
  79. case UCALL_SYNC:
  80. pr_info("%s: %016lx\n", (const char *)uc.args[2], uc.args[3]);
  81. break;
  82. case UCALL_DONE:
  83. return;
  84. case UCALL_ABORT:
  85. REPORT_GUEST_ASSERT(uc);
  86. default:
  87. TEST_FAIL("Unhandled ucall: %ld\nexit_reason: %u (%s)",
  88. uc.cmd, run->exit_reason, exit_reason_str(run->exit_reason));
  89. }
  90. }
  91. static void test_fix_hypercall(struct kvm_vcpu *vcpu, bool disable_quirk)
  92. {
  93. struct kvm_vm *vm = vcpu->vm;
  94. vm_install_exception_handler(vcpu->vm, UD_VECTOR, guest_ud_handler);
  95. if (disable_quirk)
  96. vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2,
  97. KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
  98. quirk_disabled = disable_quirk;
  99. sync_global_to_guest(vm, quirk_disabled);
  100. virt_pg_map(vm, APIC_DEFAULT_GPA, APIC_DEFAULT_GPA);
  101. enter_guest(vcpu);
  102. }
  103. KVM_ONE_VCPU_TEST(fix_hypercall, enable_quirk, guest_main)
  104. {
  105. test_fix_hypercall(vcpu, false);
  106. }
  107. KVM_ONE_VCPU_TEST(fix_hypercall, disable_quirk, guest_main)
  108. {
  109. test_fix_hypercall(vcpu, true);
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. TEST_REQUIRE(kvm_check_cap(KVM_CAP_DISABLE_QUIRKS2) & KVM_X86_QUIRK_FIX_HYPERCALL_INSN);
  114. return test_harness_run(argc, argv);
  115. }