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