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 
_no_printf(const char * format,...)23 static inline int _no_printf(const char *format, ...) { return 0; }
24 
25 #ifdef DEBUG
26 #define pr_debug(...) printf(__VA_ARGS__)
27 #else
28 #define pr_debug(...) _no_printf(__VA_ARGS__)
29 #endif
30 #ifndef QUIET
31 #define pr_info(...) printf(__VA_ARGS__)
32 #else
33 #define pr_info(...) _no_printf(__VA_ARGS__)
34 #endif
35 
36 void print_skip(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
37 #define __TEST_REQUIRE(f, fmt, ...)				\
38 do {								\
39 	if (!(f))						\
40 		ksft_exit_skip("- " fmt "\n", ##__VA_ARGS__);	\
41 } while (0)
42 
43 #define TEST_REQUIRE(f) __TEST_REQUIRE(f, "Requirement not met: %s", #f)
44 
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);
48 
49 void test_assert(bool exp, const char *exp_str,
50 		 const char *file, unsigned int line, const char *fmt, ...)
51 		__attribute__((format(printf, 5, 6)));
52 
53 #define TEST_ASSERT(e, fmt, ...) \
54 	test_assert((e), #e, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
55 
56 #define ASSERT_EQ(a, b) do { \
57 	typeof(a) __a = (a); \
58 	typeof(b) __b = (b); \
59 	TEST_ASSERT(__a == __b, \
60 		    "ASSERT_EQ(%s, %s) failed.\n" \
61 		    "\t%s is %#lx\n" \
62 		    "\t%s is %#lx", \
63 		    #a, #b, #a, (unsigned long) __a, #b, (unsigned long) __b); \
64 } while (0)
65 
66 #define TEST_FAIL(fmt, ...) do { \
67 	TEST_ASSERT(false, fmt, ##__VA_ARGS__); \
68 	__builtin_unreachable(); \
69 } while (0)
70 
71 size_t parse_size(const char *size);
72 
73 int64_t timespec_to_ns(struct timespec ts);
74 struct timespec timespec_add_ns(struct timespec ts, int64_t ns);
75 struct timespec timespec_add(struct timespec ts1, struct timespec ts2);
76 struct timespec timespec_sub(struct timespec ts1, struct timespec ts2);
77 struct timespec timespec_elapsed(struct timespec start);
78 struct timespec timespec_div(struct timespec ts, int divisor);
79 
80 enum vm_mem_backing_src_type {
81 	VM_MEM_SRC_ANONYMOUS,
82 	VM_MEM_SRC_ANONYMOUS_THP,
83 	VM_MEM_SRC_ANONYMOUS_HUGETLB,
84 	VM_MEM_SRC_ANONYMOUS_HUGETLB_16KB,
85 	VM_MEM_SRC_ANONYMOUS_HUGETLB_64KB,
86 	VM_MEM_SRC_ANONYMOUS_HUGETLB_512KB,
87 	VM_MEM_SRC_ANONYMOUS_HUGETLB_1MB,
88 	VM_MEM_SRC_ANONYMOUS_HUGETLB_2MB,
89 	VM_MEM_SRC_ANONYMOUS_HUGETLB_8MB,
90 	VM_MEM_SRC_ANONYMOUS_HUGETLB_16MB,
91 	VM_MEM_SRC_ANONYMOUS_HUGETLB_32MB,
92 	VM_MEM_SRC_ANONYMOUS_HUGETLB_256MB,
93 	VM_MEM_SRC_ANONYMOUS_HUGETLB_512MB,
94 	VM_MEM_SRC_ANONYMOUS_HUGETLB_1GB,
95 	VM_MEM_SRC_ANONYMOUS_HUGETLB_2GB,
96 	VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB,
97 	VM_MEM_SRC_SHMEM,
98 	VM_MEM_SRC_SHARED_HUGETLB,
99 	NUM_SRC_TYPES,
100 };
101 
102 #define DEFAULT_VM_MEM_SRC VM_MEM_SRC_ANONYMOUS
103 
104 struct vm_mem_backing_src_alias {
105 	const char *name;
106 	uint32_t flag;
107 };
108 
109 #define MIN_RUN_DELAY_NS	200000UL
110 
111 bool thp_configured(void);
112 size_t get_trans_hugepagesz(void);
113 size_t get_def_hugetlb_pagesz(void);
114 const struct vm_mem_backing_src_alias *vm_mem_backing_src_alias(uint32_t i);
115 size_t get_backing_src_pagesz(uint32_t i);
116 bool is_backing_src_hugetlb(uint32_t i);
117 void backing_src_help(const char *flag);
118 enum vm_mem_backing_src_type parse_backing_src_type(const char *type_name);
119 long get_run_delay(void);
120 
121 /*
122  * Whether or not the given source type is shared memory (as opposed to
123  * anonymous).
124  */
backing_src_is_shared(enum vm_mem_backing_src_type t)125 static inline bool backing_src_is_shared(enum vm_mem_backing_src_type t)
126 {
127 	return vm_mem_backing_src_alias(t)->flag & MAP_SHARED;
128 }
129 
130 /* Aligns x up to the next multiple of size. Size must be a power of 2. */
align_up(uint64_t x,uint64_t size)131 static inline uint64_t align_up(uint64_t x, uint64_t size)
132 {
133 	uint64_t mask = size - 1;
134 
135 	TEST_ASSERT(size != 0 && !(size & (size - 1)),
136 		    "size not a power of 2: %lu", size);
137 	return ((x + mask) & ~mask);
138 }
139 
align_down(uint64_t x,uint64_t size)140 static inline uint64_t align_down(uint64_t x, uint64_t size)
141 {
142 	uint64_t x_aligned_up = align_up(x, size);
143 
144 	if (x == x_aligned_up)
145 		return x;
146 	else
147 		return x_aligned_up - size;
148 }
149 
align_ptr_up(void * x,size_t size)150 static inline void *align_ptr_up(void *x, size_t size)
151 {
152 	return (void *)align_up((unsigned long)x, size);
153 }
154 
155 #endif /* SELFTEST_KVM_TEST_UTIL_H */
156