kernel.h revision 1.45 1 /* $NetBSD: kernel.h,v 1.45 2021/12/19 12:02:13 riastradh Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _LINUX_KERNEL_H_
33 #define _LINUX_KERNEL_H_
34
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39
40 #include <lib/libkern/libkern.h>
41
42 #include <asm/byteorder.h>
43 #include <asm/div64.h>
44
45 #include <linux/bitops.h>
46 #include <linux/compiler.h>
47 #include <linux/log2.h>
48 #include <linux/printk.h>
49 #include <linux/slab.h>
50
51 #define U16_MAX UINT16_MAX
52 #define U32_MAX UINT32_MAX
53 #define U64_MAX UINT64_MAX
54
55 #define S16_MAX INT16_MAX
56 #define S32_MAX INT32_MAX
57 #define S64_MAX INT64_MAX
58
59 #define oops_in_progress (panicstr != NULL)
60
61 #define IS_BUILTIN(option) (1) /* Probably... */
62 #define IS_ENABLED(option) (option)
63 #define IS_REACHABLE(option) (option)
64
65 #define might_sleep ASSERT_SLEEPABLE
66 #define might_sleep_if(C) do \
67 { \
68 if (C) \
69 might_sleep(); \
70 } while (0)
71
72 #define DEFINE_STATIC_KEY_FALSE(N) bool N __unused = false
73
74 /*
75 * XXX Linux kludge to work around GCC uninitialized variable warning.
76 * Linux does `x = x', which is bollocks.
77 */
78 #define uninitialized_var(x) x = 0
79
80 #define typecheck(T, X) ({(1 + 0*sizeof((T *)0 - &(X)));})
81
82 /* XXX These will multiply evaluate their arguments. */
83 #define min(X, Y) MIN(X, Y)
84 #define max(X, Y) MAX(X, Y)
85
86 #define max_t(T, X, Y) MAX((T)(X), (T)(Y))
87 #define min_t(T, X, Y) MIN((T)(X), (T)(Y))
88
89 #define clamp_t(T, X, MIN, MAX) min_t(T, max_t(T, X, MIN), MAX)
90 #define clamp(X, MN, MX) MIN(MAX(X, MN), MX)
91 #define clamp_val(X, MIN, MAX) clamp_t(typeof(X), X, MIN, MAX)
92
93 #define min3(X, Y, Z) MIN(X, MIN(Y, Z))
94 #define max3(X, Y, Z) MAX(X, MAX(Y, Z))
95
96 /*
97 * Rounding to nearest.
98 */
99 #define DIV_ROUND_CLOSEST(N, D) \
100 ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \
101 : (((N) - ((D) / 2)) / (D)))
102
103 #define DIV_ROUND_CLOSEST_ULL(N, D) (((N) + (D)/2)/(D))
104
105 /*
106 * Rounding to what may or may not be powers of two.
107 */
108 #define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N))
109 #define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N))
110
111 #define DIV_ROUND_DOWN_ULL(X,N) ((unsigned long long)(X) / (N))
112
113 /*
114 * Rounding to powers of two -- carefully avoiding multiple evaluation
115 * of arguments and pitfalls with C integer arithmetic rules.
116 */
117 #define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
118 #define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
119
120 #define IS_ALIGNED(X, N) (((X) & ((N) - 1)) == 0)
121
122 #define ALIGN_DOWN(X, N) round_down(X, N)
123
124 /*
125 * These select 32-bit halves of what may be 32- or 64-bit quantities,
126 * for which straight 32-bit shifts may be undefined behaviour (and do
127 * the wrong thing on most machines: return the input unshifted by
128 * ignoring the upper bits of the shift count).
129 */
130 #define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
131 #define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
132
133 #define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
134
135 #define __is_constexpr(x) __builtin_constant_p(x)
136
137 #define swap(X, Y) do \
138 { \
139 /* XXX Kludge for type-safety. */ \
140 if (&(X) != &(Y)) { \
141 CTASSERT(sizeof(X) == sizeof(Y)); \
142 /* XXX Can't do this much better without typeof. */ \
143 char __swap_tmp[sizeof(X)]; \
144 (void)memcpy(__swap_tmp, &(X), sizeof(X)); \
145 (void)memcpy(&(X), &(Y), sizeof(X)); \
146 (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \
147 } \
148 } while (0)
149
150 static __inline int64_t
151 abs64(int64_t x)
152 {
153 return (x < 0? (-x) : x);
154 }
155
156 static __inline uintmax_t
157 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
158 {
159 uintmax_t q = (x / divisor);
160 uintmax_t r = (x % divisor);
161
162 return ((q * multiplier) + ((r * multiplier) / divisor));
163 }
164
165 static int panic_timeout __unused = 0;
166
167 static __inline int __printflike(3, 0)
168 vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
169 {
170 int ret;
171
172 ret = vsnprintf(buf, size, fmt, va);
173 if (__predict_false(ret < 0))
174 return ret;
175 if (__predict_false(size == 0))
176 return 0;
177 if (__predict_false(size <= (size_t)ret))
178 return (size - 1);
179
180 return ret;
181 }
182
183 static __inline int __printflike(3, 4)
184 scnprintf(char *buf, size_t size, const char *fmt, ...)
185 {
186 va_list va;
187 int ret;
188
189 va_start(va, fmt);
190 ret = vscnprintf(buf, size, fmt, va);
191 va_end(va);
192
193 return ret;
194 }
195
196 static __inline int
197 kstrtol(const char *s, unsigned base, long *vp)
198 {
199 long long v;
200
201 v = strtoll(s, NULL, base);
202 if (v < LONG_MIN || LONG_MAX < v)
203 return -ERANGE;
204 *vp = v;
205 return 0;
206 }
207
208 static inline long
209 simple_strtol(const char *s, char **endp, unsigned base)
210 {
211 long v;
212
213 *endp = NULL; /* paranoia */
214 v = strtoll(s, endp, base);
215 if (v < LONG_MIN || LONG_MAX < v)
216 return 0;
217 return v;
218 }
219
220 static __inline char * __printflike(2, 0)
221 kvasprintf(gfp_t gfp, const char *fmt, va_list va)
222 {
223 va_list tva;
224 char *str;
225 int len, len1 __diagused;
226
227 va_copy(tva, va);
228 len = vsnprintf(NULL, 0, fmt, tva);
229 va_end(tva);
230 str = kmalloc(len + 1, gfp);
231 if (str == NULL)
232 return NULL;
233 len1 = vsnprintf(str, len + 1, fmt, va);
234 KASSERT(len1 == len);
235
236 return str;
237 }
238
239 static __inline char * __printflike(2, 3)
240 kasprintf(gfp_t gfp, const char *fmt, ...)
241 {
242 va_list va;
243 char *str;
244
245 va_start(va, fmt);
246 str = kvasprintf(gfp, fmt, va);
247 va_end(va);
248
249 return str;
250 }
251
252 static inline void __user *
253 u64_to_user_ptr(uint64_t addr)
254 {
255
256 return (void __user *)(uintptr_t)addr;
257 }
258
259 #define TAINT_MACHINE_CHECK 0
260 #define TAINT_WARN 1
261
262 #define LOCKDEP_STILL_OK 0
263
264 static inline void
265 add_taint(unsigned taint, int lockdep)
266 {
267 }
268
269 #define DFEINE_STATIC_KEY_FALSE(FLAG) \
270 bool FLAG = false
271
272 static inline bool
273 static_branch_likely(const bool *flagp)
274 {
275 return __predict_true(*flagp);
276 }
277
278 static inline int
279 sscanf(const char *fmt, ...)
280 {
281 return 0; /* XXX */
282 }
283
284 #endif /* _LINUX_KERNEL_H_ */
285