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 ssize_t test_write(int fd, const void *buf, size_t count); 53 ssize_t test_read(int fd, void *buf, size_t count); 54 int test_seq_read(const char *path, char **bufp, size_t *sizep); 55 56 void __printf(5, 6) test_assert(bool exp, const char *exp_str, 57 const char *file, unsigned int line, 58 const char *fmt, ...); 59 60 #define TEST_ASSERT(e, fmt, ...) \ 61 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__) 62 63 #define TEST_ASSERT_EQ(a, b) \ 64 do { \ 65 typeof(a) __a = (a); \ 66 typeof(b) __b = (b); \ 67 test_assert(__a == __b, #a " == " #b, __FILE__, __LINE__, \ 68 "%#lx != %#lx (%s != %s)", \ 69 (unsigned long)(__a), (unsigned long)(__b), #a, #b);\ 70 } while (0) 71 72 #define TEST_ASSERT_KVM_EXIT_REASON(vcpu, expected) do { \ 73 __u32 exit_reason = (vcpu)->run->exit_reason; \ 74 \ 75 TEST_ASSERT(exit_reason == (expected), \ 76 "Wanted KVM exit reason: %u (%s), got: %u (%s)", \ 77 (expected), exit_reason_str((expected)), \ 78 exit_reason, exit_reason_str(exit_reason)); \ 79 } while (0) 80 81 #define TEST_FAIL(fmt, ...) do { \ 82 TEST_ASSERT(false, fmt, ##__VA_ARGS__); \ 83 __builtin_unreachable(); \ 84 } while (0) 85 86 extern sigjmp_buf expect_sigbus_jmpbuf; 87 void expect_sigbus_handler(int signum); 88 89 #define TEST_EXPECT_SIGBUS(action) \ 90 do { \ 91 struct sigaction sa_old, sa_new = { \ 92 .sa_handler = expect_sigbus_handler, \ 93 }; \ 94 \ 95 sigaction(SIGBUS, &sa_new, &sa_old); \ 96 if (sigsetjmp(expect_sigbus_jmpbuf, 1) == 0) { \ 97 action; \ 98 TEST_FAIL("'%s' should have triggered SIGBUS", #action); \ 99 } \ 100 sigaction(SIGBUS, &sa_old, NULL); \ 101 } while (0) 102 103 size_t parse_size(const char *size); 104 105 s64 timespec_to_ns(struct timespec ts); 106 struct timespec timespec_add_ns(struct timespec ts, s64 ns); 107 struct timespec timespec_add(struct timespec ts1, struct timespec ts2); 108 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2); 109 struct timespec timespec_elapsed(struct timespec start); 110 struct timespec timespec_div(struct timespec ts, int divisor); 111 112 struct guest_random_state { 113 u32 seed; 114 }; 115 116 extern u32 guest_random_seed; 117 extern struct guest_random_state guest_rng; 118 119 struct guest_random_state new_guest_random_state(u32 seed); 120 u32 guest_random_u32(struct guest_random_state *state); 121 122 static inline bool __guest_random_bool(struct guest_random_state *state, 123 u8 percent) 124 { 125 return (guest_random_u32(state) % 100) < percent; 126 } 127 128 static inline bool guest_random_bool(struct guest_random_state *state) 129 { 130 return __guest_random_bool(state, 50); 131 } 132 133 static inline u64 guest_random_u64(struct guest_random_state *state) 134 { 135 return ((u64)guest_random_u32(state) << 32) | guest_random_u32(state); 136 } 137 138 enum vm_mem_backing_src_type { 139 VM_MEM_SRC_ANONYMOUS, 140 VM_MEM_SRC_ANONYMOUS_THP, 141 VM_MEM_SRC_ANONYMOUS_HUGETLB, 142 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB, 143 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB, 144 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB, 145 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB, 146 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB, 147 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB, 148 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB, 149 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB, 150 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB, 151 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB, 152 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB, 153 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB, 154 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, 155 VM_MEM_SRC_SHMEM, 156 VM_MEM_SRC_SHARED_HUGETLB, 157 NUM_SRC_TYPES, 158 }; 159 160 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS 161 162 struct vm_mem_backing_src_alias { 163 const char *name; 164 u32 flag; 165 }; 166 167 #define MIN_RUN_DELAY_NS 200000UL 168 169 bool thp_configured(void); 170 size_t get_trans_hugepagesz(void); 171 size_t get_def_hugetlb_pagesz(void); 172 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(u32 i); 173 size_t get_backing_src_pagesz(u32 i); 174 bool is_backing_src_hugetlb(u32 i); 175 void backing_src_help(const char *flag); 176 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name); 177 long get_run_delay(void); 178 bool is_numa_balancing_enabled(void); 179 180 /* 181 * Whether or not the given source type is shared memory (as opposed to 182 * anonymous). 183 */ 184 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t) 185 { 186 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED; 187 } 188 189 static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t) 190 { 191 return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM; 192 } 193 194 /* Aligns x up to the next multiple of size. Size must be a power of 2. */ 195 static inline u64 align_up(u64 x, u64 size) 196 { 197 u64 mask = size - 1; 198 199 TEST_ASSERT(size != 0 && !(size & (size - 1)), 200 "size not a power of 2: %lu", size); 201 return ((x + mask) & ~mask); 202 } 203 204 static inline u64 align_down(u64 x, u64 size) 205 { 206 u64 x_aligned_up = align_up(x, size); 207 208 if (x == x_aligned_up) 209 return x; 210 else 211 return x_aligned_up - size; 212 } 213 214 static inline void *align_ptr_up(void *x, size_t size) 215 { 216 return (void *)align_up((unsigned long)x, size); 217 } 218 219 int atoi_paranoid(const char *num_str); 220 221 static inline u32 atoi_positive(const char *name, const char *num_str) 222 { 223 int num = atoi_paranoid(num_str); 224 225 TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str); 226 return num; 227 } 228 229 static inline u32 atoi_non_negative(const char *name, const char *num_str) 230 { 231 int num = atoi_paranoid(num_str); 232 233 TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str); 234 return num; 235 } 236 237 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args); 238 __printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...); 239 240 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1))); 241 242 char *sys_get_cur_clocksource(void); 243 244 #endif /* SELFTEST_KVM_TEST_UTIL_H */ 245