~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/kvm/include/test_util.h

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 extern uint32_t guest_random_seed;
 95 extern struct guest_random_state guest_rng;
 96 
 97 struct guest_random_state new_guest_random_state(uint32_t seed);
 98 uint32_t guest_random_u32(struct guest_random_state *state);
 99 
100 static inline bool __guest_random_bool(struct guest_random_state *state,
101                                        uint8_t percent)
102 {
103         return (guest_random_u32(state) % 100) < percent;
104 }
105 
106 static inline bool guest_random_bool(struct guest_random_state *state)
107 {
108         return __guest_random_bool(state, 50);
109 }
110 
111 static inline uint64_t guest_random_u64(struct guest_random_state *state)
112 {
113         return ((uint64_t)guest_random_u32(state) << 32) | guest_random_u32(state);
114 }
115 
116 enum vm_mem_backing_src_type {
117         VM_MEM_SRC_ANONYMOUS,
118         VM_MEM_SRC_ANONYMOUS_THP,
119         VM_MEM_SRC_ANONYMOUS_HUGETLB,
120         VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
121         VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
122         VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
123         VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
124         VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
125         VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
126         VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
127         VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
128         VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
129         VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
130         VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
131         VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
132         VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
133         VM_MEM_SRC_SHMEM,
134         VM_MEM_SRC_SHARED_HUGETLB,
135         NUM_SRC_TYPES,
136 };
137 
138 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
139 
140 struct vm_mem_backing_src_alias {
141         const char *name;
142         uint32_t flag;
143 };
144 
145 #define MIN_RUN_DELAY_NS        200000UL
146 
147 bool thp_configured(void);
148 size_t get_trans_hugepagesz(void);
149 size_t get_def_hugetlb_pagesz(void);
150 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
151 size_t get_backing_src_pagesz(uint32_t i);
152 bool is_backing_src_hugetlb(uint32_t i);
153 void backing_src_help(const char *flag);
154 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
155 long get_run_delay(void);
156 
157 /*
158  * Whether or not the given source type is shared memory (as opposed to
159  * anonymous).
160  */
161 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
162 {
163         return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
164 }
165 
166 static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t)
167 {
168         return t != VM_MEM_SRC_ANONYMOUS && t != VM_MEM_SRC_SHMEM;
169 }
170 
171 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
172 static inline uint64_t align_up(uint64_t x, uint64_t size)
173 {
174         uint64_t mask = size - 1;
175 
176         TEST_ASSERT(size != 0 && !(size & (size - 1)),
177                     "size not a power of 2: %lu", size);
178         return ((x + mask) & ~mask);
179 }
180 
181 static inline uint64_t align_down(uint64_t x, uint64_t size)
182 {
183         uint64_t x_aligned_up = align_up(x, size);
184 
185         if (x == x_aligned_up)
186                 return x;
187         else
188                 return x_aligned_up - size;
189 }
190 
191 static inline void *align_ptr_up(void *x, size_t size)
192 {
193         return (void *)align_up((unsigned long)x, size);
194 }
195 
196 int atoi_paranoid(const char *num_str);
197 
198 static inline uint32_t atoi_positive(const char *name, const char *num_str)
199 {
200         int num = atoi_paranoid(num_str);
201 
202         TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
203         return num;
204 }
205 
206 static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
207 {
208         int num = atoi_paranoid(num_str);
209 
210         TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
211         return num;
212 }
213 
214 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args);
215 __printf(3, 4) int guest_snprintf(char *buf, int n, const char *fmt, ...);
216 
217 char *strdup_printf(const char *fmt, ...) __attribute__((format(printf, 1, 2), nonnull(1)));
218 
219 char *sys_get_cur_clocksource(void);
220 
221 #endif /* SELFTEST_KVM_TEST_UTIL_H */
222 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php