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