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