xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kernel.h (revision 5180fce1cf7ec7430ccf2adb88358602e5ccee1e)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * Copyright (c) 2014-2015 François Tigeot
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice unmodified, this list of conditions, and the following
14  *    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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #ifndef	_LINUXKPI_LINUX_KERNEL_H_
31 #define	_LINUXKPI_LINUX_KERNEL_H_
32 
33 #include "opt_linuxkpi.h"
34 
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/param.h>
38 #include <sys/libkern.h>
39 #include <sys/stat.h>
40 #include <sys/smp.h>
41 #include <sys/stddef.h>
42 #include <sys/syslog.h>
43 #include <sys/time.h>
44 
45 #include <linux/array_size.h>
46 #include <linux/bitops.h>
47 #include <linux/build_bug.h>
48 #include <linux/compiler.h>
49 #include <linux/container_of.h>
50 #include <linux/kstrtox.h>
51 #include <linux/limits.h>
52 #include <linux/math.h>
53 #include <linux/minmax.h>
54 #include <linux/stringify.h>
55 #include <linux/errno.h>
56 #include <linux/types.h>
57 #include <linux/typecheck.h>
58 #include <linux/jiffies.h>
59 #include <linux/log2.h>
60 #include <linux/kconfig.h>
61 #include <linux/instruction_pointer.h>
62 #include <linux/hex.h>
63 #include <linux/wordpart.h>
64 
65 #include <asm/byteorder.h>
66 #include <asm/cpufeature.h>
67 #include <asm/processor.h>
68 #include <asm/uaccess.h>
69 
70 #include <linux/stdarg.h>
71 
72 #define	KERNEL_VERSION(a,b,c)						\
73     (((a) << 16) + ((b) << 8) + ((c) > 255 ? 255 : (c)))
74 
75 #define KERN_CONT       ""
76 #define	KERN_EMERG	"<0>"
77 #define	KERN_ALERT	"<1>"
78 #define	KERN_CRIT	"<2>"
79 #define	KERN_ERR	"<3>"
80 #define	KERN_WARNING	"<4>"
81 #define	KERN_NOTICE	"<5>"
82 #define	KERN_INFO	"<6>"
83 #define	KERN_DEBUG	"<7>"
84 
85 #define	S8_C(x)  x
86 #define	U8_C(x)  x ## U
87 #define	S16_C(x) x
88 #define	U16_C(x) x ## U
89 #define	S32_C(x) x
90 #define	U32_C(x) x ## U
91 #define	S64_C(x) x ## LL
92 #define	U64_C(x) x ## ULL
93 
94 #define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
95 #define	BUG_ON(cond)		do {				\
96 	if (cond) {						\
97 		panic("BUG ON %s failed at %s:%d",		\
98 		    __stringify(cond), __FILE__, __LINE__);	\
99 	}							\
100 } while (0)
101 
102 extern int linuxkpi_warn_dump_stack;
103 #define	WARN_ON(cond) ({					\
104 	bool __ret = (cond);					\
105 	if (__ret) {						\
106 		printf("WARNING %s failed at %s:%d\n",		\
107 		    __stringify(cond), __FILE__, __LINE__);	\
108 		if (linuxkpi_warn_dump_stack)				\
109 			linux_dump_stack();				\
110 	}								\
111 	unlikely(__ret);						\
112 })
113 
114 #define	WARN_ON_SMP(cond)	WARN_ON(cond)
115 
116 #define	WARN_ON_ONCE(cond) ({					\
117 	static bool __warn_on_once;				\
118 	bool __ret = (cond);					\
119 	if (__ret && !__warn_on_once) {				\
120 		__warn_on_once = 1;				\
121 		printf("WARNING %s failed at %s:%d\n",		\
122 		    __stringify(cond), __FILE__, __LINE__);	\
123 		if (linuxkpi_warn_dump_stack)				\
124 			linux_dump_stack();				\
125 	}								\
126 	unlikely(__ret);						\
127 })
128 
129 #define	oops_in_progress	SCHEDULER_STOPPED()
130 
131 #undef	ALIGN
132 #define	ALIGN(x, y)		roundup2((x), (y))
133 #define	ALIGN_DOWN(x, y)	rounddown2(x, y)
134 #undef PTR_ALIGN
135 #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
136 #define	IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
137 #define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
138 #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
139 
140 #define	printk(...)		printf(__VA_ARGS__)
141 #define	vprintk(f, a)		vprintf(f, a)
142 
143 #define PTR_IF(x, p)		((x) ? (p) : NULL)
144 
145 #define	asm			__asm
146 
147 extern void linux_dump_stack(void);
148 #define	dump_stack()		linux_dump_stack()
149 
150 struct va_format {
151 	const char *fmt;
152 	va_list *va;
153 };
154 
155 static inline int
156 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
157 {
158 	ssize_t ssize = size;
159 	int i;
160 
161 	i = vsnprintf(buf, size, fmt, args);
162 
163 	return ((i >= ssize) ? (ssize - 1) : i);
164 }
165 
166 static inline int
167 scnprintf(char *buf, size_t size, const char *fmt, ...)
168 {
169 	va_list args;
170 	int i;
171 
172 	va_start(args, fmt);
173 	i = vscnprintf(buf, size, fmt, args);
174 	va_end(args);
175 
176 	return (i);
177 }
178 
179 /*
180  * The "pr_debug()" and "pr_devel()" macros should produce zero code
181  * unless LINUXKPI_DEBUG is defined:
182  */
183 #ifdef LINUXKPI_DEBUG
184 extern int linuxkpi_debug;
185 #define pr_debug(fmt, ...)					\
186 	do {							\
187 		if (linuxkpi_debug)				\
188 			log(LOG_DEBUG, fmt, ##__VA_ARGS__);	\
189 	} while (0)
190 #define pr_devel(fmt, ...) \
191 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
192 #else
193 #define pr_debug(fmt, ...) \
194 	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
195 #define pr_devel(fmt, ...) \
196 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
197 #endif
198 
199 #ifndef pr_fmt
200 #define pr_fmt(fmt) fmt
201 #endif
202 
203 /*
204  * Print a one-time message (analogous to WARN_ONCE() et al):
205  */
206 #define printk_once(...) do {			\
207 	static bool __print_once;		\
208 						\
209 	if (!__print_once) {			\
210 		__print_once = true;		\
211 		printk(__VA_ARGS__);		\
212 	}					\
213 } while (0)
214 
215 /*
216  * Log a one-time message (analogous to WARN_ONCE() et al):
217  */
218 #define log_once(level,...) do {		\
219 	static bool __log_once;			\
220 						\
221 	if (unlikely(!__log_once)) {		\
222 		__log_once = true;		\
223 		log(level, __VA_ARGS__);	\
224 	}					\
225 } while (0)
226 
227 #define pr_emerg(fmt, ...) \
228 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
229 #define pr_alert(fmt, ...) \
230 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
231 #define pr_crit(fmt, ...) \
232 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
233 #define pr_err(fmt, ...) \
234 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
235 #define pr_err_once(fmt, ...) \
236 	log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
237 #define pr_warning(fmt, ...) \
238 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
239 #define pr_warn(...) \
240 	pr_warning(__VA_ARGS__)
241 #define pr_warn_once(fmt, ...) \
242 	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
243 #define pr_notice(fmt, ...) \
244 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
245 #define pr_info(fmt, ...) \
246 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
247 #define pr_info_once(fmt, ...) \
248 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
249 #define pr_cont(fmt, ...) \
250 	printk(KERN_CONT fmt, ##__VA_ARGS__)
251 #define	pr_warn_ratelimited(...) do {		\
252 	static linux_ratelimit_t __ratelimited;	\
253 	if (linux_ratelimited(&__ratelimited))	\
254 		pr_warning(__VA_ARGS__);	\
255 } while (0)
256 
257 #ifndef WARN
258 #define	WARN(condition, ...) ({			\
259 	bool __ret_warn_on = (condition);	\
260 	if (unlikely(__ret_warn_on))		\
261 		pr_warning(__VA_ARGS__);	\
262 	unlikely(__ret_warn_on);		\
263 })
264 #endif
265 
266 #ifndef WARN_ONCE
267 #define	WARN_ONCE(condition, ...) ({		\
268 	bool __ret_warn_on = (condition);	\
269 	if (unlikely(__ret_warn_on))		\
270 		pr_warn_once(__VA_ARGS__);	\
271 	unlikely(__ret_warn_on);		\
272 })
273 #endif
274 
275 #define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
276 
277 #define offsetofend(t, m)	\
278         (offsetof(t, m) + sizeof((((t *)0)->m)))
279 
280 #define	smp_processor_id()	PCPU_GET(cpuid)
281 #define	num_possible_cpus()	mp_ncpus
282 #define	num_online_cpus()	mp_ncpus
283 
284 #if defined(__i386__) || defined(__amd64__)
285 extern bool linux_cpu_has_clflush;
286 #define	cpu_has_clflush		linux_cpu_has_clflush
287 #endif
288 
289 typedef struct linux_ratelimit {
290 	struct timeval lasttime;
291 	int counter;
292 } linux_ratelimit_t;
293 
294 static inline bool
295 linux_ratelimited(linux_ratelimit_t *rl)
296 {
297 	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
298 }
299 
300 #define	__is_constexpr(x) \
301 	__builtin_constant_p(x)
302 
303 /*
304  * The is_signed() macro below returns true if the passed data type is
305  * signed. Else false is returned.
306  */
307 #define	is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
308 
309 #define	TAINT_WARN	0
310 #define	test_taint(x)	(0)
311 #define	add_taint(x,y)	do {	\
312 	} while (0)
313 
314 static inline bool
315 mac_pton(const char *macin, uint8_t *macout)
316 {
317 	const char *s, *d;
318 	uint8_t mac[6], hx, lx;
319 	int i;
320 
321 	if (strlen(macin) < (3 * 6 - 1))
322 		return (false);
323 
324 	i = 0;
325 	s = macin;
326 	do {
327 		/* Should we also support '-'-delimiters? */
328 		d = strchrnul(s, ':');
329 		hx = lx = 0;
330 		while (s < d) {
331 			/* Fail on abc:123:xxx:... */
332 			if ((d - s) > 2)
333 				return (false);
334 			/* We do support non-well-formed strings: 3:45:6:... */
335 			if ((d - s) > 1) {
336 				hx = _h2b(*s);
337 				if (hx < 0)
338 					return (false);
339 				s++;
340 			}
341 			lx = _h2b(*s);
342 			if (lx < 0)
343 				return (false);
344 			s++;
345 		}
346 		mac[i] = (hx << 4) | lx;
347 		i++;
348 		if (i >= 6)
349 			return (false);
350 	} while (d != NULL && *d != '\0');
351 
352 	memcpy(macout, mac, 6);
353 	return (true);
354 }
355 
356 #define	DECLARE_FLEX_ARRAY(_t, _n)					\
357     struct { struct { } __dummy_ ## _n; _t _n[0]; }
358 
359 /*
360  * The following functions/macros are debug/diagnostics tools. They default to
361  * no-ops, except `might_sleep()` which uses `WITNESS_WARN()` on FreeBSD.
362  */
363 static inline void
364 __might_resched(const char *file, int line, unsigned int offsets)
365 {
366 }
367 
368 static inline void
369 __might_sleep(const char *file, int line)
370 {
371 }
372 
373 static inline void
374 might_fault(void)
375 {
376 }
377 
378 #define	might_sleep()							\
379 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")
380 
381 #define	might_sleep_if(cond) do { \
382 	if (cond) { might_sleep(); } \
383 } while (0)
384 
385 #define	might_resched()		do { } while (0)
386 #define	cant_sleep()		do { } while (0)
387 #define	cant_migrate()		do { } while (0)
388 #define	sched_annotate_sleep()	do { } while (0)
389 #define	non_block_start()	do { } while (0)
390 #define	non_block_end()		do { } while (0)
391 
392 extern enum system_states {
393 	SYSTEM_BOOTING,
394 	SYSTEM_SCHEDULING,
395 	SYSTEM_FREEING_INITMEM,
396 	SYSTEM_RUNNING,
397 	SYSTEM_HALT,
398 	SYSTEM_POWER_OFF,
399 	SYSTEM_RESTART,
400 	SYSTEM_SUSPEND,
401 } system_state;
402 
403 #endif	/* _LINUXKPI_LINUX_KERNEL_H_ */
404