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