test_util.h 6.1 KB

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