kernel.h revision 1.27 1 /* $NetBSD: kernel.h,v 1.27 2021/12/19 00:31:35 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 #include <sys/endian.h>
40
41 #include <lib/libkern/libkern.h>
42
43 #include <asm/byteorder.h>
44 #include <asm/div64.h>
45
46 #include <linux/bitops.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 oops_in_progress (panicstr != NULL)
56
57 #if BYTE_ORDER == BIG_ENDIAN
58 #define __BIG_ENDIAN _BIG_ENDIAN
59 #else
60 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
61 #endif
62
63 #define IS_BUILTIN(option) (1) /* Probably... */
64 #define IS_ENABLED(option) (option)
65 #define IS_REACHABLE(option) (option)
66
67 #define __printf __printflike
68 #define __user
69 #if __GNUC_PREREQ__(4,0) /* not sure when but this will work */
70 #define __must_check __attribute__((warn_unused_result))
71 #else
72 #define __must_check /* nothing */
73 #endif
74 #define __always_unused __unused
75 #define noinline __noinline
76
77 #define barrier() __insn_barrier()
78 #define likely(X) __predict_true(X)
79 #define unlikely(X) __predict_false(X)
80
81 /*
82 * XXX Linux kludge to work around GCC uninitialized variable warning.
83 * Linux does `x = x', which is bollocks.
84 */
85 #define uninitialized_var(x) x = 0
86
87 /* XXX These will multiply evaluate their arguments. */
88 #define min(X, Y) MIN(X, Y)
89 #define max(X, Y) MAX(X, Y)
90
91 #define max_t(T, X, Y) MAX(X, Y)
92 #define min_t(T, X, Y) MIN(X, Y)
93
94 #define clamp_t(T, X, MIN, MAX) min_t(T, max_t(T, X, MIN), MAX)
95 #define clamp(X, MN, MX) MIN(MAX(X, MN), MX)
96
97 #define min3(X, Y, Z) MIN(X, MIN(Y, Z))
98 #define max3(X, Y, Z) MAX(X, MAX(Y, Z))
99
100 /*
101 * Rounding to nearest.
102 */
103 #define DIV_ROUND_CLOSEST(N, D) \
104 ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \
105 : (((N) - ((D) / 2)) / (D)))
106
107 #define DIV_ROUND_CLOSEST_ULL(N, D) (((N) + (D)/2)/(D))
108
109 /*
110 * Rounding to what may or may not be powers of two.
111 */
112 #define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N))
113 #define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N))
114
115 /*
116 * Rounding to powers of two -- carefully avoiding multiple evaluation
117 * of arguments and pitfalls with C integer arithmetic rules.
118 */
119 #define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
120 #define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
121
122 #define IS_ALIGNED(X, N) (((X) & ((N) - 1)) == 0)
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 swap(X, Y) do \
136 { \
137 /* XXX Kludge for type-safety. */ \
138 if (&(X) != &(Y)) { \
139 CTASSERT(sizeof(X) == sizeof(Y)); \
140 /* XXX Can't do this much better without typeof. */ \
141 char __swap_tmp[sizeof(X)]; \
142 (void)memcpy(__swap_tmp, &(X), sizeof(X)); \
143 (void)memcpy(&(X), &(Y), sizeof(X)); \
144 (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \
145 } \
146 } while (0)
147
148 #define ACCESS_ONCE(X) ({ \
149 typeof(X) __access_once_tmp = (X); \
150 __insn_barrier(); \
151 __access_once_tmp; \
152 })
153
154 static __inline int64_t
155 abs64(int64_t x)
156 {
157 return (x < 0? (-x) : x);
158 }
159
160 static __inline uintmax_t
161 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
162 {
163 uintmax_t q = (x / divisor);
164 uintmax_t r = (x % divisor);
165
166 return ((q * multiplier) + ((r * multiplier) / divisor));
167 }
168
169 static int panic_timeout __unused = 0;
170
171 static __inline int __printflike(3, 0)
172 vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
173 {
174 int ret;
175
176 ret = vsnprintf(buf, size, fmt, va);
177 if (__predict_false(ret < 0))
178 return ret;
179 if (__predict_false(size == 0))
180 return 0;
181 if (__predict_false(size <= (size_t)ret))
182 return (size - 1);
183
184 return ret;
185 }
186
187 static __inline int __printflike(3, 4)
188 scnprintf(char *buf, size_t size, const char *fmt, ...)
189 {
190 va_list va;
191 int ret;
192
193 va_start(va, fmt);
194 ret = vscnprintf(buf, size, fmt, va);
195 va_end(va);
196
197 return ret;
198 }
199
200 static __inline int
201 kstrtol(const char *s, unsigned base, long *vp)
202 {
203 long long v;
204
205 v = strtoll(s, NULL, base);
206 if (v < LONG_MIN || LONG_MAX < v)
207 return -ERANGE;
208 *vp = v;
209 return 0;
210 }
211
212 static inline long
213 simple_strtol(const char *s, char **endp, unsigned base)
214 {
215 long v;
216
217 *endp = NULL; /* paranoia */
218 v = strtoll(s, endp, base);
219 if (v < LONG_MIN || LONG_MAX < v)
220 return 0;
221 return v;
222 }
223
224 static __inline char * __printflike(2, 0)
225 kvasprintf(gfp_t gfp, const char *fmt, va_list va)
226 {
227 va_list tva;
228 char *str;
229 int len, len1 __diagused;
230
231 va_copy(tva, va);
232 len = vsnprintf(NULL, 0, fmt, tva);
233 va_end(tva);
234 str = kmalloc(len + 1, gfp);
235 if (str == NULL)
236 return NULL;
237 len1 = vsnprintf(str, len + 1, fmt, va);
238 KASSERT(len1 == len);
239
240 return str;
241 }
242
243 static __inline char * __printflike(2, 3)
244 kasprintf(gfp_t gfp, const char *fmt, ...)
245 {
246 va_list va;
247 char *str;
248
249 va_start(va, fmt);
250 str = kvasprintf(gfp, fmt, va);
251 va_end(va);
252
253 return str;
254 }
255
256 #endif /* _LINUX_KERNEL_H_ */
257