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