test_util.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * tools/testing/selftests/kvm/include/test_util.h
  4. *
  5. * Copyright (C) 2018, Google LLC.
  6. */
  7. #ifndef SELFTEST_KVM_TEST_UTIL_H
  8. #define SELFTEST_KVM_TEST_UTIL_H
  9. #include <setjmp.h>
  10. #include <signal.h>
  11. #include <stdlib.h>
  12. #include <stdarg.h>
  13. #include <stdbool.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <inttypes.h>
  17. #include <errno.h>
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <sys/mman.h>
  21. #include "kselftest.h"
  22. #define msecs_to_usecs(msec) ((msec) * 1000ULL)
  23. static inline __printf(1, 2) int _no_printf(const char *format, ...) { return 0; }
  24. #ifdef DEBUG
  25. #define pr_debug(...) printf(__VA_ARGS__)
  26. #else
  27. #define pr_debug(...) _no_printf(__VA_ARGS__)
  28. #endif
  29. #ifndef QUIET
  30. #define pr_info(...) printf(__VA_ARGS__)
  31. #else
  32. #define pr_info(...) _no_printf(__VA_ARGS__)
  33. #endif
  34. void __printf(1, 2) print_skip(const char *fmt, ...);
  35. #define __TEST_REQUIRE(f, fmt, ...) \
  36. do { \
  37. if (!(f)) \
  38. ksft_exit_skip("- " fmt "\n", ##__VA_ARGS__); \
  39. } while (0)
  40. #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f)
  41. ssize_t test_write(int fd, const void *buf, size_t count);
  42. ssize_t test_read(int fd, void *buf, size_t count);
  43. int test_seq_read(const char *path, char **bufp, size_t *sizep);
  44. void __printf(5, 6) test_assert(bool exp, const char *exp_str,
  45. const char *file, unsigned int line,
  46. const char *fmt, ...);
  47. #define TEST_ASSERT(e, fmt, ...) \
  48. test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
  49. #define TEST_ASSERT_EQ(a, b) \
  50. do { \
  51. typeof(a) __a = (a); \
  52. typeof(b) __b = (b); \
  53. test_assert(__a == __b, #a " == " #b, __FILE__, __LINE__, \
  54. "%#lx != %#lx (%s != %s)", \
  55. (unsigned long)(__a), (unsigned long)(__b), #a, #b);\
  56. } while (0)
  57. #define TEST_ASSERT_KVM_EXIT_REASON(vcpu, expected) do { \
  58. __u32 exit_reason = (vcpu)->run->exit_reason; \
  59. \
  60. TEST_ASSERT(exit_reason == (expected), \
  61. "Wanted KVM exit reason: %u (%s), got: %u (%s)", \
  62. (expected), exit_reason_str((expected)), \
  63. exit_reason, exit_reason_str(exit_reason)); \
  64. } while (0)
  65. #define TEST_FAIL(fmt, ...) do { \
  66. TEST_ASSERT(false, fmt, ##__VA_ARGS__); \
  67. __builtin_unreachable(); \
  68. } while (0)
  69. extern sigjmp_buf expect_sigbus_jmpbuf;
  70. void expect_sigbus_handler(int signum);
  71. #define TEST_EXPECT_SIGBUS(action) \
  72. do { \
  73. struct sigaction sa_old, sa_new = { \
  74. .sa_handler = expect_sigbus_handler, \
  75. }; \
  76. \
  77. sigaction(SIGBUS, &sa_new, &sa_old); \
  78. if (sigsetjmp(expect_sigbus_jmpbuf, 1) == 0) { \
  79. action; \
  80. TEST_FAIL("'%s' should have triggered SIGBUS", #action); \
  81. } \
  82. sigaction(SIGBUS, &sa_old, NULL); \
  83. } while (0)
  84. size_t parse_size(const char *size);
  85. int64_t timespec_to_ns(struct timespec ts);
  86. struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
  87. struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
  88. struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
  89. struct timespec timespec_elapsed(struct timespec start);
  90. struct timespec timespec_div(struct timespec ts, int divisor);
  91. struct guest_random_state {
  92. uint32_t seed;
  93. };
  94. extern uint32_t guest_random_seed;
  95. extern struct guest_random_state guest_rng;
  96. struct guest_random_state new_guest_random_state(uint32_t seed);
  97. uint32_t guest_random_u32(struct guest_random_state *state);
  98. static inline bool __guest_random_bool(struct guest_random_state *state,
  99. uint8_t percent)
  100. {
  101. return (guest_random_u32(state) % 100) < percent;
  102. }
  103. static inline bool guest_random_bool(struct guest_random_state *state)
  104. {
  105. return __guest_random_bool(state, 50);
  106. }
  107. static inline uint64_t guest_random_u64(struct guest_random_state *state)
  108. {
  109. return ((uint64_t)guest_random_u32(state) << 32) | guest_random_u32(state);
  110. }
  111. enum vm_mem_backing_src_type {
  112. VM_MEM_SRC_ANONYMOUS,
  113. VM_MEM_SRC_ANONYMOUS_THP,
  114. VM_MEM_SRC_ANONYMOUS_HUGETLB,
  115. VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
  116. VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
  117. VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
  118. VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
  119. VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
  120. VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
  121. VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
  122. VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
  123. VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
  124. VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
  125. VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
  126. VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
  127. VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
  128. VM_MEM_SRC_SHMEM,
  129. VM_MEM_SRC_SHARED_HUGETLB,
  130. NUM_SRC_TYPES,
  131. };
  132. #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
  133. struct vm_mem_backing_src_alias {
  134. const char *name;
  135. uint32_t flag;
  136. };
  137. #define MIN_RUN_DELAY_NS 200000UL
  138. bool thp_configured(void);
  139. size_t get_trans_hugepagesz(void);
  140. size_t get_def_hugetlb_pagesz(void);
  141. const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
  142. size_t get_backing_src_pagesz(uint32_t i);
  143. bool is_backing_src_hugetlb(uint32_t i);
  144. void backing_src_help(const char *flag);
  145. enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
  146. long get_run_delay(void);
  147. bool is_numa_balancing_enabled(void);
  148. /*
  149. * Whether or not the given source type is shared memory (as opposed to
  150. * anonymous).
  151. */
  152. static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
  153. {
  154. return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
  155. }
  156. static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t)
  157. {
  158. return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM;
  159. }
  160. /* Aligns x up to the next multiple of size. Size must be a power of 2. */
  161. static inline uint64_t align_up(uint64_t x, uint64_t size)
  162. {
  163. uint64_t mask = size - 1;
  164. TEST_ASSERT(size != 0 && !(size & (size - 1)),
  165. "size not a power of 2: %lu", size);
  166. return ((x + mask) & ~mask);
  167. }
  168. static inline uint64_t align_down(uint64_t x, uint64_t size)
  169. {
  170. uint64_t x_aligned_up = align_up(x, size);
  171. if (x == x_aligned_up)
  172. return x;
  173. else
  174. return x_aligned_up - size;
  175. }
  176. static inline void *align_ptr_up(void *x, size_t size)
  177. {
  178. return (void *)align_up((unsigned long)x, size);
  179. }
  180. int atoi_paranoid(const char *num_str);
  181. static inline uint32_t atoi_positive(const char *name, const char *num_str)
  182. {
  183. int num = atoi_paranoid(num_str);
  184. TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
  185. return num;
  186. }
  187. static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
  188. {
  189. int num = atoi_paranoid(num_str);
  190. TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
  191. return num;
  192. }
  193. int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
  194. __printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
  195. char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
  196. char *sys_get_cur_clocksource(void);
  197. #endif /* SELFTEST_KVM_TEST_UTIL_H */