xref: /netbsd-src/sys/external/bsd/common/include/linux/kernel.h (revision 6db1fce331d5ce421d621fa8f9d1903397399498)
1*6db1fce3Sriastradh /*	$NetBSD: kernel.h,v 1.46 2022/10/25 23:37:36 riastradh Exp $	*/
2d13ab4c3Sskrll 
3d13ab4c3Sskrll /*-
4d13ab4c3Sskrll  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5d13ab4c3Sskrll  * All rights reserved.
6d13ab4c3Sskrll  *
7d13ab4c3Sskrll  * This code is derived from software contributed to The NetBSD Foundation
8d13ab4c3Sskrll  * by Taylor R. Campbell.
9d13ab4c3Sskrll  *
10d13ab4c3Sskrll  * Redistribution and use in source and binary forms, with or without
11d13ab4c3Sskrll  * modification, are permitted provided that the following conditions
12d13ab4c3Sskrll  * are met:
13d13ab4c3Sskrll  * 1. Redistributions of source code must retain the above copyright
14d13ab4c3Sskrll  *    notice, this list of conditions and the following disclaimer.
15d13ab4c3Sskrll  * 2. Redistributions in binary form must reproduce the above copyright
16d13ab4c3Sskrll  *    notice, this list of conditions and the following disclaimer in the
17d13ab4c3Sskrll  *    documentation and/or other materials provided with the distribution.
18d13ab4c3Sskrll  *
19d13ab4c3Sskrll  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20d13ab4c3Sskrll  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21d13ab4c3Sskrll  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22d13ab4c3Sskrll  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23d13ab4c3Sskrll  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24d13ab4c3Sskrll  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25d13ab4c3Sskrll  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26d13ab4c3Sskrll  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27d13ab4c3Sskrll  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28d13ab4c3Sskrll  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29d13ab4c3Sskrll  * POSSIBILITY OF SUCH DAMAGE.
30d13ab4c3Sskrll  */
31d13ab4c3Sskrll 
32d13ab4c3Sskrll #ifndef _LINUX_KERNEL_H_
33d13ab4c3Sskrll #define _LINUX_KERNEL_H_
34d13ab4c3Sskrll 
35d13ab4c3Sskrll #include <sys/cdefs.h>
36d13ab4c3Sskrll #include <sys/types.h>
37d13ab4c3Sskrll #include <sys/param.h>
3877b5597aSriastradh #include <sys/systm.h>
3977b5597aSriastradh 
407fd36dbcSriastradh #include <lib/libkern/libkern.h>
41d22e7791Smaya 
42d22e7791Smaya #include <asm/byteorder.h>
43d22e7791Smaya #include <asm/div64.h>
44d22e7791Smaya 
45922889e3Sriastradh #include <linux/bitops.h>
46ea45a958Sriastradh #include <linux/compiler.h>
47d22e7791Smaya #include <linux/log2.h>
48916aa4c1Sriastradh #include <linux/printk.h>
49d8b42109Sriastradh #include <linux/slab.h>
507fd36dbcSriastradh 
5110b48c2fSriastradh #define U16_MAX UINT16_MAX
5210b48c2fSriastradh #define U32_MAX UINT32_MAX
5310b48c2fSriastradh #define U64_MAX UINT64_MAX
5410b48c2fSriastradh 
55e7fd312eSriastradh #define	S16_MAX	INT16_MAX
56*6db1fce3Sriastradh #define	S16_MIN	INT16_MIN
57e7fd312eSriastradh #define	S32_MAX	INT32_MAX
58*6db1fce3Sriastradh #define	S32_MIN	INT32_MIN
59e7fd312eSriastradh #define	S64_MAX	INT64_MAX
60*6db1fce3Sriastradh #define	S64_MIN	INT64_MIN
61e7fd312eSriastradh 
6277b5597aSriastradh #define	oops_in_progress	(panicstr != NULL)
6377b5597aSriastradh 
645e742034Sriastradh #define	IS_BUILTIN(option)	(1) /* Probably... */
651618c793Sriastradh #define	IS_ENABLED(option)	(option)
661618c793Sriastradh #define	IS_REACHABLE(option)	(option)
67d13ab4c3Sskrll 
68291c6e14Sriastradh #define	might_sleep	ASSERT_SLEEPABLE
698837f05dSriastradh #define	might_sleep_if(C) do						      \
708837f05dSriastradh {									      \
718837f05dSriastradh 	if (C)								      \
728837f05dSriastradh 		might_sleep();						      \
738837f05dSriastradh } while (0)
74291c6e14Sriastradh 
75a799bc66Sriastradh #define	DEFINE_STATIC_KEY_FALSE(N)	bool N __unused = false
76a799bc66Sriastradh 
77f2bbabefSriastradh /*
78f2bbabefSriastradh  * XXX Linux kludge to work around GCC uninitialized variable warning.
79f2bbabefSriastradh  * Linux does `x = x', which is bollocks.
80f2bbabefSriastradh  */
81f2bbabefSriastradh #define	uninitialized_var(x)	x = 0
82d13ab4c3Sskrll 
831de3f401Sriastradh #define	typecheck(T, X)	({(1 + 0*sizeof((T *)0 - &(X)));})
841de3f401Sriastradh 
85d13ab4c3Sskrll /* XXX These will multiply evaluate their arguments.  */
868554c0aaSriastradh #define	min(X, Y)	MIN(X, Y)
878554c0aaSriastradh #define	max(X, Y)	MAX(X, Y)
888554c0aaSriastradh 
893636dd71Sriastradh #define	max_t(T, X, Y)	MAX((T)(X), (T)(Y))
903636dd71Sriastradh #define	min_t(T, X, Y)	MIN((T)(X), (T)(Y))
91d13ab4c3Sskrll 
9277b5597aSriastradh #define	clamp_t(T, X, MIN, MAX)	min_t(T, max_t(T, X, MIN), MAX)
939a2add20Sriastradh #define	clamp(X, MN, MX)	MIN(MAX(X, MN), MX)
944560925bSriastradh #define	clamp_val(X, MIN, MAX)	clamp_t(typeof(X), X, MIN, MAX)
9577b5597aSriastradh 
964b028f11Sriastradh #define	min3(X, Y, Z)	MIN(X, MIN(Y, Z))
974b028f11Sriastradh #define	max3(X, Y, Z)	MAX(X, MAX(Y, Z))
984b028f11Sriastradh 
99d13ab4c3Sskrll /*
100f2bbabefSriastradh  * Rounding to nearest.
101f2bbabefSriastradh  */
102f2bbabefSriastradh #define	DIV_ROUND_CLOSEST(N, D)						\
103f2bbabefSriastradh 	((0 < (N)) ? (((N) + ((D) / 2)) / (D))				\
104f2bbabefSriastradh 	    : (((N) - ((D) / 2)) / (D)))
105f2bbabefSriastradh 
10641005c9bSriastradh #define	DIV_ROUND_CLOSEST_ULL(N, D)	(((N) + (D)/2)/(D))
10741005c9bSriastradh 
108f2bbabefSriastradh /*
109d13ab4c3Sskrll  * Rounding to what may or may not be powers of two.
110d13ab4c3Sskrll  */
111d13ab4c3Sskrll #define	DIV_ROUND_UP(X, N)	(((X) + (N) - 1) / (N))
11277b5597aSriastradh #define	DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))
113d13ab4c3Sskrll 
1141ac5f62eSriastradh #define	DIV_ROUND_DOWN_ULL(X,N)	((unsigned long long)(X) / (N))
1151ac5f62eSriastradh 
116d13ab4c3Sskrll /*
117d13ab4c3Sskrll  * Rounding to powers of two -- carefully avoiding multiple evaluation
118d13ab4c3Sskrll  * of arguments and pitfalls with C integer arithmetic rules.
119d13ab4c3Sskrll  */
120d13ab4c3Sskrll #define	round_up(X, N)		((((X) - 1) | ((N) - 1)) + 1)
121d13ab4c3Sskrll #define	round_down(X, N)	((X) & ~(uintmax_t)((N) - 1))
122d13ab4c3Sskrll 
123b81c64e1Sriastradh #define	IS_ALIGNED(X, N)	(((X) & ((N) - 1)) == 0)
124b81c64e1Sriastradh 
125d89b1820Sriastradh #define	ALIGN_DOWN(X, N)	round_down(X, N)
126d89b1820Sriastradh 
127d13ab4c3Sskrll /*
128d13ab4c3Sskrll  * These select 32-bit halves of what may be 32- or 64-bit quantities,
129d13ab4c3Sskrll  * for which straight 32-bit shifts may be undefined behaviour (and do
130d13ab4c3Sskrll  * the wrong thing on most machines: return the input unshifted by
131d13ab4c3Sskrll  * ignoring the upper bits of the shift count).
132d13ab4c3Sskrll  */
133d13ab4c3Sskrll #define	upper_32_bits(X)	((uint32_t) (((X) >> 16) >> 16))
134d13ab4c3Sskrll #define	lower_32_bits(X)	((uint32_t) ((X) & 0xffffffffUL))
135d13ab4c3Sskrll 
136d13ab4c3Sskrll #define	ARRAY_SIZE(ARRAY)	__arraycount(ARRAY)
137d13ab4c3Sskrll 
138c586a418Sriastradh #define	__is_constexpr(x)	__builtin_constant_p(x)
139c586a418Sriastradh 
140d13ab4c3Sskrll #define	swap(X, Y)	do						\
141d13ab4c3Sskrll {									\
142d13ab4c3Sskrll 	/* XXX Kludge for type-safety.  */				\
143d13ab4c3Sskrll 	if (&(X) != &(Y)) {						\
144d13ab4c3Sskrll 		CTASSERT(sizeof(X) == sizeof(Y));			\
145d13ab4c3Sskrll 		/* XXX Can't do this much better without typeof.  */	\
146d13ab4c3Sskrll 		char __swap_tmp[sizeof(X)];				\
147d13ab4c3Sskrll 		(void)memcpy(__swap_tmp, &(X), sizeof(X));		\
148d13ab4c3Sskrll 		(void)memcpy(&(X), &(Y), sizeof(X));			\
149d13ab4c3Sskrll 		(void)memcpy(&(Y), __swap_tmp, sizeof(X));		\
150d13ab4c3Sskrll 	}								\
151d13ab4c3Sskrll } while (0)
152d13ab4c3Sskrll 
15334c0ab9fSchristos static __inline int64_t
abs64(int64_t x)154d13ab4c3Sskrll abs64(int64_t x)
155d13ab4c3Sskrll {
156d13ab4c3Sskrll 	return (x < 0? (-x) : x);
157d13ab4c3Sskrll }
158d13ab4c3Sskrll 
15934c0ab9fSchristos static __inline uintmax_t
mult_frac(uintmax_t x,uintmax_t multiplier,uintmax_t divisor)16077b5597aSriastradh mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
16177b5597aSriastradh {
16277b5597aSriastradh 	uintmax_t q = (x / divisor);
16377b5597aSriastradh 	uintmax_t r = (x % divisor);
16477b5597aSriastradh 
16577b5597aSriastradh 	return ((q * multiplier) + ((r * multiplier) / divisor));
16677b5597aSriastradh }
16777b5597aSriastradh 
168f2bbabefSriastradh static int panic_timeout __unused = 0;
169f2bbabefSriastradh 
17034c0ab9fSchristos static __inline int __printflike(3, 0)
vscnprintf(char * buf,size_t size,const char * fmt,va_list va)17177b5597aSriastradh vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
17277b5597aSriastradh {
17377b5597aSriastradh 	int ret;
17477b5597aSriastradh 
17577b5597aSriastradh 	ret = vsnprintf(buf, size, fmt, va);
17677b5597aSriastradh 	if (__predict_false(ret < 0))
17777b5597aSriastradh 		return ret;
17877b5597aSriastradh 	if (__predict_false(size == 0))
17977b5597aSriastradh 		return 0;
1802c08c520Smrg 	if (__predict_false(size <= (size_t)ret))
18177b5597aSriastradh 		return (size - 1);
18277b5597aSriastradh 
18377b5597aSriastradh 	return ret;
18477b5597aSriastradh }
18577b5597aSriastradh 
18634c0ab9fSchristos static __inline int __printflike(3, 4)
scnprintf(char * buf,size_t size,const char * fmt,...)18777b5597aSriastradh scnprintf(char *buf, size_t size, const char *fmt, ...)
18877b5597aSriastradh {
18977b5597aSriastradh 	va_list va;
19077b5597aSriastradh 	int ret;
19177b5597aSriastradh 
19277b5597aSriastradh 	va_start(va, fmt);
19377b5597aSriastradh 	ret = vscnprintf(buf, size, fmt, va);
19477b5597aSriastradh 	va_end(va);
19577b5597aSriastradh 
19677b5597aSriastradh 	return ret;
19777b5597aSriastradh }
19877b5597aSriastradh 
19934c0ab9fSchristos static __inline int
kstrtol(const char * s,unsigned base,long * vp)200389d09bfSriastradh kstrtol(const char *s, unsigned base, long *vp)
201389d09bfSriastradh {
202389d09bfSriastradh 	long long v;
203389d09bfSriastradh 
204389d09bfSriastradh 	v = strtoll(s, NULL, base);
205389d09bfSriastradh 	if (v < LONG_MIN || LONG_MAX < v)
206389d09bfSriastradh 		return -ERANGE;
207389d09bfSriastradh 	*vp = v;
208389d09bfSriastradh 	return 0;
209389d09bfSriastradh }
210389d09bfSriastradh 
21184d717f3Sriastradh static inline long
simple_strtol(const char * s,char ** endp,unsigned base)21284d717f3Sriastradh simple_strtol(const char *s, char **endp, unsigned base)
21384d717f3Sriastradh {
21484d717f3Sriastradh 	long v;
21584d717f3Sriastradh 
21684d717f3Sriastradh 	*endp = NULL;		/* paranoia */
21784d717f3Sriastradh 	v = strtoll(s, endp, base);
21884d717f3Sriastradh 	if (v < LONG_MIN || LONG_MAX < v)
21984d717f3Sriastradh 		return 0;
22084d717f3Sriastradh 	return v;
22184d717f3Sriastradh }
22284d717f3Sriastradh 
22334c0ab9fSchristos static __inline char * __printflike(2, 0)
kvasprintf(gfp_t gfp,const char * fmt,va_list va)2241c2c92c6Sriastradh kvasprintf(gfp_t gfp, const char *fmt, va_list va)
2251c2c92c6Sriastradh {
226a589054dSriastradh 	va_list tva;
2271c2c92c6Sriastradh 	char *str;
2281c2c92c6Sriastradh 	int len, len1 __diagused;
2291c2c92c6Sriastradh 
230a589054dSriastradh 	va_copy(tva, va);
231a589054dSriastradh 	len = vsnprintf(NULL, 0, fmt, tva);
232a589054dSriastradh 	va_end(tva);
2331c2c92c6Sriastradh 	str = kmalloc(len + 1, gfp);
2341c2c92c6Sriastradh 	if (str == NULL)
2351c2c92c6Sriastradh 		return NULL;
2361c2c92c6Sriastradh 	len1 = vsnprintf(str, len + 1, fmt, va);
2371c2c92c6Sriastradh 	KASSERT(len1 == len);
2381c2c92c6Sriastradh 
2391c2c92c6Sriastradh 	return str;
2401c2c92c6Sriastradh }
2411c2c92c6Sriastradh 
24234c0ab9fSchristos static __inline char * __printflike(2, 3)
kasprintf(gfp_t gfp,const char * fmt,...)243d8b42109Sriastradh kasprintf(gfp_t gfp, const char *fmt, ...)
244d8b42109Sriastradh {
245d8b42109Sriastradh 	va_list va;
246d8b42109Sriastradh 	char *str;
247d8b42109Sriastradh 
248d8b42109Sriastradh 	va_start(va, fmt);
2491c2c92c6Sriastradh 	str = kvasprintf(gfp, fmt, va);
250d8b42109Sriastradh 	va_end(va);
251d8b42109Sriastradh 
252d8b42109Sriastradh 	return str;
253d8b42109Sriastradh }
254d8b42109Sriastradh 
2555a4265dcSriastradh static inline void __user *
u64_to_user_ptr(uint64_t addr)2565a4265dcSriastradh u64_to_user_ptr(uint64_t addr)
2575a4265dcSriastradh {
2585a4265dcSriastradh 
2595a4265dcSriastradh 	return (void __user *)(uintptr_t)addr;
2605a4265dcSriastradh }
2615a4265dcSriastradh 
262291c6e14Sriastradh #define	TAINT_MACHINE_CHECK	0
263291c6e14Sriastradh #define	TAINT_WARN		1
264291c6e14Sriastradh 
265291c6e14Sriastradh #define	LOCKDEP_STILL_OK	0
266291c6e14Sriastradh 
267291c6e14Sriastradh static inline void
add_taint(unsigned taint,int lockdep)268291c6e14Sriastradh add_taint(unsigned taint, int lockdep)
269291c6e14Sriastradh {
270291c6e14Sriastradh }
271291c6e14Sriastradh 
2729c351cd5Sriastradh #define	DFEINE_STATIC_KEY_FALSE(FLAG)					      \
2739c351cd5Sriastradh 	bool FLAG = false
2749c351cd5Sriastradh 
2759c351cd5Sriastradh static inline bool
static_branch_likely(const bool * flagp)2769c351cd5Sriastradh static_branch_likely(const bool *flagp)
2779c351cd5Sriastradh {
2789c351cd5Sriastradh 	return __predict_true(*flagp);
2799c351cd5Sriastradh }
2809c351cd5Sriastradh 
281f59d6b01Sriastradh static inline int
sscanf(const char * fmt,...)282f59d6b01Sriastradh sscanf(const char *fmt, ...)
283f59d6b01Sriastradh {
284f59d6b01Sriastradh 	return 0;		/* XXX */
285f59d6b01Sriastradh }
286f59d6b01Sriastradh 
287d13ab4c3Sskrll #endif  /* _LINUX_KERNEL_H_ */
288