1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * tools/testing/selftests/kvm/include/test_util.h
5 * Copyright (C) 2018, Google LLC.
8 #ifndef SELFTEST_KVM_TEST_UTIL_H
9 #define SELFTEST_KVM_TEST_UTIL_H
21 #include "kselftest.h"
23 static inline int _no_printf(const char *format, ...) { return 0; }
26 #define pr_debug(...) printf(__VA_ARGS__)
28 #define pr_debug(...) _no_printf(__VA_ARGS__)
31 #define pr_info(...) printf(__VA_ARGS__)
33 #define pr_info(...) _no_printf(__VA_ARGS__)
36 void __printf(1, 2) print_skip(const char *fmt, ...);
37 #define __TEST_REQUIRE(f, fmt, ...) \
40 ksft_exit_skip("- " fmt "\n", ##__VA_ARGS__); \
43 #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f)
45 ssize_t test_write(int fd, const void *buf, size_t count);
46 ssize_t test_read(int fd, void *buf, size_t count);
47 int test_seq_read(const char *path, char **bufp, size_t *sizep);
49 void __printf(5, 6) test_assert(bool exp, const char *exp_str,
50 const char *file, unsigned int line,
51 const char *fmt, ...);
53 #define TEST_ASSERT(e, fmt, ...) \
54 test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
56 #define TEST_ASSERT_EQ(a, b) \
58 typeof(a) __a = (a); \
59 typeof(b) __b = (b); \
60 test_assert(__a == __b, #a " == " #b, __FILE__, __LINE__, \
61 "%#lx != %#lx (%s != %s)", \
62 (unsigned long)(__a), (unsigned long)(__b), #a, #b);\
65 #define TEST_ASSERT_KVM_EXIT_REASON(vcpu, expected) do { \
66 __u32 exit_reason = (vcpu)->run->exit_reason; \
68 TEST_ASSERT(exit_reason == (expected), \
69 "Wanted KVM exit reason: %u (%s), got: %u (%s)", \
70 (expected), exit_reason_str((expected)), \
71 exit_reason, exit_reason_str(exit_reason)); \
74 #define TEST_FAIL(fmt, ...) do { \
75 TEST_ASSERT(false, fmt, ##__VA_ARGS__); \
76 __builtin_unreachable(); \
79 size_t parse_size(const char *size);
81 int64_t timespec_to_ns(struct timespec ts);
82 struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
83 struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
84 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
85 struct timespec timespec_elapsed(struct timespec start);
86 struct timespec timespec_div(struct timespec ts, int divisor);
88 struct guest_random_state {
92 struct guest_random_state new_guest_random_state(uint32_t seed);
93 uint32_t guest_random_u32(struct guest_random_state *state);
95 enum vm_mem_backing_src_type {
97 VM_MEM_SRC_ANONYMOUS_THP,
98 VM_MEM_SRC_ANONYMOUS_HUGETLB,
99 VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
100 VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
101 VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
102 VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
103 VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
104 VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
105 VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
106 VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
107 VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
108 VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
109 VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
110 VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
111 VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
113 VM_MEM_SRC_SHARED_HUGETLB,
117 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
119 struct vm_mem_backing_src_alias {
124 #define MIN_RUN_DELAY_NS 200000UL
126 bool thp_configured(void);
127 size_t get_trans_hugepagesz(void);
128 size_t get_def_hugetlb_pagesz(void);
129 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
130 size_t get_backing_src_pagesz(uint32_t i);
131 bool is_backing_src_hugetlb(uint32_t i);
132 void backing_src_help(const char *flag);
133 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
134 long get_run_delay(void);
137 * Whether or not the given source type is shared memory (as opposed to
140 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
142 return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
145 static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t)
147 return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM;
150 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
151 static inline uint64_t align_up(uint64_t x, uint64_t size)
153 uint64_t mask = size - 1;
155 TEST_ASSERT(size != 0 && !(size & (size - 1)),
156 "size not a power of 2: %lu", size);
157 return ((x + mask) & ~mask);
160 static inline uint64_t align_down(uint64_t x, uint64_t size)
162 uint64_t x_aligned_up = align_up(x, size);
164 if (x == x_aligned_up)
167 return x_aligned_up - size;
170 static inline void *align_ptr_up(void *x, size_t size)
172 return (void *)align_up((unsigned long)x, size);
175 int atoi_paranoid(const char *num_str);
177 static inline uint32_t atoi_positive(const char *name, const char *num_str)
179 int num = atoi_paranoid(num_str);
181 TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
185 static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
187 int num = atoi_paranoid(num_str);
189 TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
193 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
194 __printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
196 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
198 char *sys_get_cur_clocksource(void);
200 #endif /* SELFTEST_KVM_TEST_UTIL_H */