ucall_common.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2018, Google LLC.
  4. */
  5. #ifndef SELFTEST_KVM_UCALL_COMMON_H
  6. #define SELFTEST_KVM_UCALL_COMMON_H
  7. #include "test_util.h"
  8. #include "ucall.h"
  9. /* Common ucalls */
  10. enum {
  11. UCALL_NONE,
  12. UCALL_SYNC,
  13. UCALL_ABORT,
  14. UCALL_PRINTF,
  15. UCALL_DONE,
  16. UCALL_UNHANDLED,
  17. };
  18. #define UCALL_MAX_ARGS 7
  19. #define UCALL_BUFFER_LEN 1024
  20. struct ucall {
  21. uint64_t cmd;
  22. uint64_t args[UCALL_MAX_ARGS];
  23. char buffer[UCALL_BUFFER_LEN];
  24. /* Host virtual address of this struct. */
  25. struct ucall *hva;
  26. };
  27. void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
  28. void ucall_arch_do_ucall(vm_vaddr_t uc);
  29. void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu);
  30. void ucall(uint64_t cmd, int nargs, ...);
  31. __printf(2, 3) void ucall_fmt(uint64_t cmd, const char *fmt, ...);
  32. __printf(5, 6) void ucall_assert(uint64_t cmd, const char *exp,
  33. const char *file, unsigned int line,
  34. const char *fmt, ...);
  35. uint64_t get_ucall(struct kvm_vcpu *vcpu, struct ucall *uc);
  36. void ucall_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa);
  37. int ucall_nr_pages_required(uint64_t page_size);
  38. /*
  39. * Perform userspace call without any associated data. This bare call avoids
  40. * allocating a ucall struct, which can be useful if the atomic operations in
  41. * the full ucall() are problematic and/or unwanted. Note, this will come out
  42. * as UCALL_NONE on the backend.
  43. */
  44. #define GUEST_UCALL_NONE() ucall_arch_do_ucall((vm_vaddr_t)NULL)
  45. #define GUEST_SYNC_ARGS(stage, arg1, arg2, arg3, arg4) \
  46. ucall(UCALL_SYNC, 6, "hello", stage, arg1, arg2, arg3, arg4)
  47. #define GUEST_SYNC(stage) ucall(UCALL_SYNC, 2, "hello", stage)
  48. #define GUEST_SYNC1(arg0) ucall(UCALL_SYNC, 1, arg0)
  49. #define GUEST_SYNC2(arg0, arg1) ucall(UCALL_SYNC, 2, arg0, arg1)
  50. #define GUEST_SYNC3(arg0, arg1, arg2) \
  51. ucall(UCALL_SYNC, 3, arg0, arg1, arg2)
  52. #define GUEST_SYNC4(arg0, arg1, arg2, arg3) \
  53. ucall(UCALL_SYNC, 4, arg0, arg1, arg2, arg3)
  54. #define GUEST_SYNC5(arg0, arg1, arg2, arg3, arg4) \
  55. ucall(UCALL_SYNC, 5, arg0, arg1, arg2, arg3, arg4)
  56. #define GUEST_SYNC6(arg0, arg1, arg2, arg3, arg4, arg5) \
  57. ucall(UCALL_SYNC, 6, arg0, arg1, arg2, arg3, arg4, arg5)
  58. #define GUEST_PRINTF(_fmt, _args...) ucall_fmt(UCALL_PRINTF, _fmt, ##_args)
  59. #define GUEST_DONE() ucall(UCALL_DONE, 0)
  60. #define REPORT_GUEST_PRINTF(ucall) pr_info("%s", (ucall).buffer)
  61. enum guest_assert_builtin_args {
  62. GUEST_ERROR_STRING,
  63. GUEST_FILE,
  64. GUEST_LINE,
  65. GUEST_ASSERT_BUILTIN_NARGS
  66. };
  67. #define ____GUEST_ASSERT(_condition, _exp, _fmt, _args...) \
  68. do { \
  69. if (!(_condition)) \
  70. ucall_assert(UCALL_ABORT, _exp, __FILE__, __LINE__, _fmt, ##_args); \
  71. } while (0)
  72. #define __GUEST_ASSERT(_condition, _fmt, _args...) \
  73. ____GUEST_ASSERT(_condition, #_condition, _fmt, ##_args)
  74. #define GUEST_ASSERT(_condition) \
  75. __GUEST_ASSERT(_condition, #_condition)
  76. #define GUEST_FAIL(_fmt, _args...) \
  77. ucall_assert(UCALL_ABORT, "Unconditional guest failure", \
  78. __FILE__, __LINE__, _fmt, ##_args)
  79. #define GUEST_ASSERT_EQ(a, b) \
  80. do { \
  81. typeof(a) __a = (a); \
  82. typeof(b) __b = (b); \
  83. ____GUEST_ASSERT(__a == __b, #a " == " #b, "%#lx != %#lx (%s != %s)", \
  84. (unsigned long)(__a), (unsigned long)(__b), #a, #b); \
  85. } while (0)
  86. #define GUEST_ASSERT_NE(a, b) \
  87. do { \
  88. typeof(a) __a = (a); \
  89. typeof(b) __b = (b); \
  90. ____GUEST_ASSERT(__a != __b, #a " != " #b, "%#lx == %#lx (%s == %s)", \
  91. (unsigned long)(__a), (unsigned long)(__b), #a, #b); \
  92. } while (0)
  93. #define REPORT_GUEST_ASSERT(ucall) \
  94. test_assert(false, (const char *)(ucall).args[GUEST_ERROR_STRING], \
  95. (const char *)(ucall).args[GUEST_FILE], \
  96. (ucall).args[GUEST_LINE], "%s", (ucall).buffer)
  97. #endif /* SELFTEST_KVM_UCALL_COMMON_H */