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 8 #ifndef SELFTEST_KVM_TEST_UTIL_H 9 #define SELFTEST_KVM_TEST_UTIL_H 10 11 #include <setjmp.h> 12 #include <signal.h> 13 #include <stdlib.h> 14 #include <stdarg.h> 15 #include <stdbool.h> 16 #include <stdio.h> 17 #include <string.h> 18 #include <inttypes.h> 19 #include <errno.h> 20 #include <unistd.h> 21 #include <fcntl.h> 22 #include "kselftest.h" 23 24 #include <linux/mman.h> 25 #include <linux/types.h> 26 #include <linux/stringify.h> 27 28 #define msecs_to_usecs(msec) ((msec) * 1000ULL) 29 30 static inline __printf(1, 2) int _no_printf(const char *format, ...) { return 0; } 31 32 #ifdef DEBUG 33 #define pr_debug(...) printf(__VA_ARGS__) 34 #else 35 #define pr_debug(...) _no_printf(__VA_ARGS__) 36 #endif 37 #ifndef QUIET 38 #define pr_info(...) printf(__VA_ARGS__) 39 #else 40 #define pr_info(...) _no_printf(__VA_ARGS__) 41 #endif 42 43 void __printf(1, 2) print_skip(const char *fmt, ...); 44 #define __TEST_REQUIRE(f, fmt, ...) \ 45 do { \ 46 if (!(f)) \ 47 ksft_exit_skip("- " fmt "\n", ##__VA_ARGS__); \ 48 } while (0) 49 50 #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f) 51 52 void __printf(5, 6) test_assert(bool exp, const char *exp_str, 53 const char *file, unsigned int line, 54 const char *fmt, ...); 55 56 #define TEST_ASSERT(e, fmt, ...) \ 57 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__) 58 59 #define TEST_ASSERT_EQ(a, b) \ 60 do { \ 61 typeof(a) __a = (a); \ 62 typeof(b) __b = (b); \ 63 test_assert(__a == __b, #a " == " #b, __FILE__, __LINE__, \ 64 "%#lx != %#lx (%s != %s)", \ 65 (unsigned long)(__a), (unsigned long)(__b), #a, #b);\ 66 } while (0) 67 68 #define TEST_ASSERT_KVM_EXIT_REASON(vcpu, expected) do { \ 69 __u32 exit_reason = (vcpu)->run->exit_reason; \ 70 \ 71 TEST_ASSERT(exit_reason == (expected), \ 72 "Wanted KVM exit reason: %u (%s), got: %u (%s)", \ 73 (expected), exit_reason_str((expected)), \ 74 exit_reason, exit_reason_str(exit_reason)); \ 75 } while (0) 76 77 #define TEST_FAIL(fmt, ...) do { \ 78 TEST_ASSERT(false, fmt, ##__VA_ARGS__); \ 79 __builtin_unreachable(); \ 80 } while (0) 81 82 extern __thread sigjmp_buf expect_sigbus_jmpbuf; 83 extern __thread volatile sig_atomic_t expecting_sigbus; 84 void catchall_signal_handler(int signum); 85 86 #define TEST_EXPECT_SIGBUS(action) \ 87 do { \ 88 struct sigaction __sa = {}; \ 89 \ 90 TEST_ASSERT_EQ(sigaction(SIGBUS, NULL, &__sa), 0); \ 91 TEST_ASSERT_EQ(__sa.sa_handler, &catchall_signal_handler); \ 92 \ 93 expecting_sigbus = true; \ 94 if (sigsetjmp(expect_sigbus_jmpbuf, 1) == 0) { \ 95 action; \ 96 TEST_FAIL("'%s' should have triggered SIGBUS", #action);\ 97 } \ 98 expecting_sigbus = false; \ 99 } while (0) 100 101 size_t parse_size(const char *size); 102 103 s64 timespec_to_ns(struct timespec ts); 104 struct timespec timespec_add_ns(struct timespec ts, s64 ns); 105 struct timespec timespec_add(struct timespec ts1, struct timespec ts2); 106 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2); 107 struct timespec timespec_elapsed(struct timespec start); 108 struct timespec timespec_div(struct timespec ts, int divisor); 109 110 struct kvm_random_state { 111 u32 seed; 112 }; 113 114 extern u32 kvm_random_seed; 115 extern struct kvm_random_state kvm_rng; 116 117 extern bool kvm_has_gmem_attributes; 118 119 struct kvm_random_state new_kvm_random_state(u32 seed); 120 u32 kvm_random_u32(struct kvm_random_state *state); 121 122 static inline bool __kvm_random_bool(struct kvm_random_state *state, 123 u8 percent) 124 { 125 return (kvm_random_u32(state) % 100) < percent; 126 } 127 128 static inline bool kvm_random_bool(struct kvm_random_state *state) 129 { 130 return __kvm_random_bool(state, 50); 131 } 132 133 static inline u64 kvm_random_u64(struct kvm_random_state *state) 134 { 135 return ((u64)kvm_random_u32(state) << 32) | kvm_random_u32(state); 136 } 137 138 u32 kvm_random_u32_in_range(struct kvm_random_state *state, u32 min, u32 max); 139 u64 kvm_random_u64_in_range(struct kvm_random_state *state, u64 min, u64 max); 140 141 enum vm_mem_backing_src_type { 142 VM_MEM_SRC_ANONYMOUS, 143 VM_MEM_SRC_ANONYMOUS_THP, 144 VM_MEM_SRC_ANONYMOUS_HUGETLB, 145 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB, 146 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB, 147 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB, 148 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB, 149 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, 150 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB, 151 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB, 152 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB, 153 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB, 154 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB, 155 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB, 156 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, 157 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, 158 VM_MEM_SRC_SHMEM, 159 VM_MEM_SRC_SHARED_HUGETLB, 160 NUM_SRC_TYPES, 161 }; 162 163 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS 164 165 struct vm_mem_backing_src_alias { 166 const char *name; 167 u32 flag; 168 }; 169 170 #define MIN_RUN_DELAY_NS 200000UL 171 172 bool thp_configured(void); 173 size_t get_trans_hugepagesz(void); 174 size_t get_def_hugetlb_pagesz(void); 175 size_t get_free_hugepages(size_t page_size); 176 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(u32 i); 177 size_t get_backing_src_pagesz(u32 i); 178 bool is_backing_src_hugetlb(u32 i); 179 void backing_src_help(const char *flag); 180 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); 181 long get_run_delay(void); 182 bool is_numa_balancing_enabled(void); 183 184 /* 185 * Whether or not the given source type is shared memory (as opposed to 186 * anonymous). 187 */ 188 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t) 189 { 190 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED; 191 } 192 193 static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t) 194 { 195 return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM; 196 } 197 198 /* Aligns x up to the next multiple of size. Size must be a power of 2. */ 199 static inline u64 align_up(u64 x, u64 size) 200 { 201 u64 mask = size - 1; 202 203 TEST_ASSERT(size != 0 && !(size & (size - 1)), 204 "size not a power of 2: %lu", size); 205 return ((x + mask) & ~mask); 206 } 207 208 static inline u64 align_down(u64 x, u64 size) 209 { 210 u64 x_aligned_up = align_up(x, size); 211 212 if (x == x_aligned_up) 213 return x; 214 else 215 return x_aligned_up - size; 216 } 217 218 static inline void *align_ptr_up(void *x, size_t size) 219 { 220 return (void *)align_up((unsigned long)x, size); 221 } 222 223 int atoi_paranoid(const char *num_str); 224 225 static inline u32 atoi_positive(const char *name, const char *num_str) 226 { 227 int num = atoi_paranoid(num_str); 228 229 TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str); 230 return num; 231 } 232 233 static inline u32 atoi_non_negative(const char *name, const char *num_str) 234 { 235 int num = atoi_paranoid(num_str); 236 237 TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str); 238 return num; 239 } 240 241 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args); 242 243 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1))); 244 245 char *sys_get_cur_clocksource(void); 246 247 #endif /* SELFTEST_KVM_TEST_UTIL_H */ 248