projects/linux·blob master · refs

247 lines · 6.7 KiB

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
30static 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
43void __printf(1, 2) print_skip(const char *fmt, ...);
44#define __TEST_REQUIRE(f, fmt, ...) \
45do { \
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
52ssize_t test_write(int fd, const void *buf, size_t count);
53ssize_t test_read(int fd, void *buf, size_t count);
54int test_seq_read(const char *path, char **bufp, size_t *sizep);
55
56void __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) \
64do { \
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
86extern sigjmp_buf expect_sigbus_jmpbuf;
87void expect_sigbus_handler(int signum);
88
89#define TEST_EXPECT_SIGBUS(action) \
90do { \
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
103size_t parse_size(const char *size);
104
105s64 timespec_to_ns(struct timespec ts);
106struct timespec timespec_add_ns(struct timespec ts, s64 ns);
107struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
108struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
109struct timespec timespec_elapsed(struct timespec start);
110struct timespec timespec_div(struct timespec ts, int divisor);
111
112struct kvm_random_state {
113 u32 seed;
114};
115
116extern u32 kvm_random_seed;
117extern struct kvm_random_state kvm_rng;
118
119struct kvm_random_state new_kvm_random_state(u32 seed);
120u32 kvm_random_u32(struct kvm_random_state *state);
121
122static inline bool __kvm_random_bool(struct kvm_random_state *state,
123 u8 percent)
124{
125 return (kvm_random_u32(state) % 100) < percent;
126}
127
128static inline bool kvm_random_bool(struct kvm_random_state *state)
129{
130 return __kvm_random_bool(state, 50);
131}
132
133static 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
138u32 kvm_random_u32_in_range(struct kvm_random_state *state, u32 min, u32 max);
139u64 kvm_random_u64_in_range(struct kvm_random_state *state, u64 min, u64 max);
140
141enum 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
165struct vm_mem_backing_src_alias {
166 const char *name;
167 u32 flag;
168};
169
170#define MIN_RUN_DELAY_NS 200000UL
171
172bool thp_configured(void);
173size_t get_trans_hugepagesz(void);
174size_t get_def_hugetlb_pagesz(void);
175const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(u32 i);
176size_t get_backing_src_pagesz(u32 i);
177bool is_backing_src_hugetlb(u32 i);
178void backing_src_help(const char *flag);
179enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
180long get_run_delay(void);
181bool is_numa_balancing_enabled(void);
182
183/*
184 * Whether or not the given source type is shared memory (as opposed to
185 * anonymous).
186 */
187static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
188{
189 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
190}
191
192static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t)
193{
194 return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM;
195}
196
197/* Aligns x up to the next multiple of size. Size must be a power of 2. */
198static inline u64 align_up(u64 x, u64 size)
199{
200 u64 mask = size - 1;
201
202 TEST_ASSERT(size != 0 && !(size & (size - 1)),
203 "size not a power of 2: %lu", size);
204 return ((x + mask) & ~mask);
205}
206
207static inline u64 align_down(u64 x, u64 size)
208{
209 u64 x_aligned_up = align_up(x, size);
210
211 if (x == x_aligned_up)
212 return x;
213 else
214 return x_aligned_up - size;
215}
216
217static inline void *align_ptr_up(void *x, size_t size)
218{
219 return (void *)align_up((unsigned long)x, size);
220}
221
222int atoi_paranoid(const char *num_str);
223
224static inline u32 atoi_positive(const char *name, const char *num_str)
225{
226 int num = atoi_paranoid(num_str);
227
228 TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
229 return num;
230}
231
232static inline u32 atoi_non_negative(const char *name, const char *num_str)
233{
234 int num = atoi_paranoid(num_str);
235
236 TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
237 return num;
238}
239
240int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
241__printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
242
243char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
244
245char *sys_get_cur_clocksource(void);
246
247#endif /* SELFTEST_KVM_TEST_UTIL_H */