summaryrefslogtreecommitdiff
path: root/sys/external/bsd/common/include/linux/kernel.h
blob: 995b4bb645c05395e508b8a8f6609b7fec3cbb0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*	$NetBSD: kernel.h,v 1.46 2022/10/25 23:37:36 riastradh Exp $	*/

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _LINUX_KERNEL_H_
#define _LINUX_KERNEL_H_

#include <sys/cdefs.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>

#include <lib/libkern/libkern.h>

#include <asm/byteorder.h>
#include <asm/div64.h>

#include <linux/bitops.h>
#include <linux/compiler.h>
#include <linux/log2.h>
#include <linux/printk.h>
#include <linux/slab.h>

#define U16_MAX UINT16_MAX
#define U32_MAX UINT32_MAX
#define U64_MAX UINT64_MAX

#define	S16_MAX	INT16_MAX
#define	S16_MIN	INT16_MIN
#define	S32_MAX	INT32_MAX
#define	S32_MIN	INT32_MIN
#define	S64_MAX	INT64_MAX
#define	S64_MIN	INT64_MIN

#define	oops_in_progress	(panicstr != NULL)

#define	IS_BUILTIN(option)	(1) /* Probably... */
#define	IS_ENABLED(option)	(option)
#define	IS_REACHABLE(option)	(option)

#define	might_sleep	ASSERT_SLEEPABLE
#define	might_sleep_if(C) do						      \
{									      \
	if (C)								      \
		might_sleep();						      \
} while (0)

#define	DEFINE_STATIC_KEY_FALSE(N)	bool N __unused = false

/*
 * XXX Linux kludge to work around GCC uninitialized variable warning.
 * Linux does `x = x', which is bollocks.
 */
#define	uninitialized_var(x)	x = 0

#define	typecheck(T, X)	({(1 + 0*sizeof((T *)0 - &(X)));})

/* XXX These will multiply evaluate their arguments.  */
#define	min(X, Y)	MIN(X, Y)
#define	max(X, Y)	MAX(X, Y)

#define	max_t(T, X, Y)	MAX((T)(X), (T)(Y))
#define	min_t(T, X, Y)	MIN((T)(X), (T)(Y))

#define	clamp_t(T, X, MIN, MAX)	min_t(T, max_t(T, X, MIN), MAX)
#define	clamp(X, MN, MX)	MIN(MAX(X, MN), MX)
#define	clamp_val(X, MIN, MAX)	clamp_t(typeof(X), X, MIN, MAX)

#define	min3(X, Y, Z)	MIN(X, MIN(Y, Z))
#define	max3(X, Y, Z)	MAX(X, MAX(Y, Z))

/*
 * Rounding to nearest.
 */
#define	DIV_ROUND_CLOSEST(N, D)						\
	((0 < (N)) ? (((N) + ((D) / 2)) / (D))				\
	    : (((N) - ((D) / 2)) / (D)))

#define	DIV_ROUND_CLOSEST_ULL(N, D)	(((N) + (D)/2)/(D))

/*
 * Rounding to what may or may not be powers of two.
 */
#define	DIV_ROUND_UP(X, N)	(((X) + (N) - 1) / (N))
#define	DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))

#define	DIV_ROUND_DOWN_ULL(X,N)	((unsigned long long)(X) / (N))

/*
 * Rounding to powers of two -- carefully avoiding multiple evaluation
 * of arguments and pitfalls with C integer arithmetic rules.
 */
#define	round_up(X, N)		((((X) - 1) | ((N) - 1)) + 1)
#define	round_down(X, N)	((X) & ~(uintmax_t)((N) - 1))

#define	IS_ALIGNED(X, N)	(((X) & ((N) - 1)) == 0)

#define	ALIGN_DOWN(X, N)	round_down(X, N)

/*
 * These select 32-bit halves of what may be 32- or 64-bit quantities,
 * for which straight 32-bit shifts may be undefined behaviour (and do
 * the wrong thing on most machines: return the input unshifted by
 * ignoring the upper bits of the shift count).
 */
#define	upper_32_bits(X)	((uint32_t) (((X) >> 16) >> 16))
#define	lower_32_bits(X)	((uint32_t) ((X) & 0xffffffffUL))

#define	ARRAY_SIZE(ARRAY)	__arraycount(ARRAY)

#define	__is_constexpr(x)	__builtin_constant_p(x)

#define	swap(X, Y)	do						\
{									\
	/* XXX Kludge for type-safety.  */				\
	if (&(X) != &(Y)) {						\
		CTASSERT(sizeof(X) == sizeof(Y));			\
		/* XXX Can't do this much better without typeof.  */	\
		char __swap_tmp[sizeof(X)];				\
		(void)memcpy(__swap_tmp, &(X), sizeof(X));		\
		(void)memcpy(&(X), &(Y), sizeof(X));			\
		(void)memcpy(&(Y), __swap_tmp, sizeof(X));		\
	}								\
} while (0)

static __inline int64_t
abs64(int64_t x)
{
	return (x < 0? (-x) : x);
}

static __inline uintmax_t
mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
{
	uintmax_t q = (x / divisor);
	uintmax_t r = (x % divisor);

	return ((q * multiplier) + ((r * multiplier) / divisor));
}

static int panic_timeout __unused = 0;

static __inline int __printflike(3, 0)
vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
{
	int ret;

	ret = vsnprintf(buf, size, fmt, va);
	if (__predict_false(ret < 0))
		return ret;
	if (__predict_false(size == 0))
		return 0;
	if (__predict_false(size <= (size_t)ret))
		return (size - 1);

	return ret;
}

static __inline int __printflike(3, 4)
scnprintf(char *buf, size_t size, const char *fmt, ...)
{
	va_list va;
	int ret;

	va_start(va, fmt);
	ret = vscnprintf(buf, size, fmt, va);
	va_end(va);

	return ret;
}

static __inline int
kstrtol(const char *s, unsigned base, long *vp)
{
	long long v;

	v = strtoll(s, NULL, base);
	if (v < LONG_MIN || LONG_MAX < v)
		return -ERANGE;
	*vp = v;
	return 0;
}

static inline long
simple_strtol(const char *s, char **endp, unsigned base)
{
	long v;

	*endp = NULL;		/* paranoia */
	v = strtoll(s, endp, base);
	if (v < LONG_MIN || LONG_MAX < v)
		return 0;
	return v;
}

static __inline char * __printflike(2, 0)
kvasprintf(gfp_t gfp, const char *fmt, va_list va)
{
	va_list tva;
	char *str;
	int len, len1 __diagused;

	va_copy(tva, va);
	len = vsnprintf(NULL, 0, fmt, tva);
	va_end(tva);
	str = kmalloc(len + 1, gfp);
	if (str == NULL)
		return NULL;
	len1 = vsnprintf(str, len + 1, fmt, va);
	KASSERT(len1 == len);

	return str;
}

static __inline char * __printflike(2, 3)
kasprintf(gfp_t gfp, const char *fmt, ...)
{
	va_list va;
	char *str;

	va_start(va, fmt);
	str = kvasprintf(gfp, fmt, va);
	va_end(va);

	return str;
}

static inline void __user *
u64_to_user_ptr(uint64_t addr)
{

	return (void __user *)(uintptr_t)addr;
}

#define	TAINT_MACHINE_CHECK	0
#define	TAINT_WARN		1

#define	LOCKDEP_STILL_OK	0

static inline void
add_taint(unsigned taint, int lockdep)
{
}

#define	DFEINE_STATIC_KEY_FALSE(FLAG)					      \
	bool FLAG = false

static inline bool
static_branch_likely(const bool *flagp)
{
	return __predict_true(*flagp);
}

static inline int
sscanf(const char *fmt, ...)
{
	return 0;		/* XXX */
}

#endif  /* _LINUX_KERNEL_H_ */