xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kernel.h (revision b4856b8e9d872ca6ee0c4ef43e7047693ef7645b)
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 <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <sys/stat.h>
38 #include <sys/smp.h>
39 #include <sys/stddef.h>
40 #include <sys/syslog.h>
41 #include <sys/time.h>
42 
43 #include <linux/bitops.h>
44 #include <linux/build_bug.h>
45 #include <linux/compiler.h>
46 #include <linux/container_of.h>
47 #include <linux/kstrtox.h>
48 #include <linux/limits.h>
49 #include <linux/math.h>
50 #include <linux/minmax.h>
51 #include <linux/stringify.h>
52 #include <linux/errno.h>
53 #include <linux/types.h>
54 #include <linux/typecheck.h>
55 #include <linux/jiffies.h>
56 #include <linux/log2.h>
57 #include <linux/kconfig.h>
58 
59 #include <asm/byteorder.h>
60 #include <asm/cpufeature.h>
61 #include <asm/processor.h>
62 #include <asm/uaccess.h>
63 
64 #include <linux/stdarg.h>
65 
66 #define KERN_CONT       ""
67 #define	KERN_EMERG	"<0>"
68 #define	KERN_ALERT	"<1>"
69 #define	KERN_CRIT	"<2>"
70 #define	KERN_ERR	"<3>"
71 #define	KERN_WARNING	"<4>"
72 #define	KERN_NOTICE	"<5>"
73 #define	KERN_INFO	"<6>"
74 #define	KERN_DEBUG	"<7>"
75 
76 #define	S8_C(x)  x
77 #define	U8_C(x)  x ## U
78 #define	S16_C(x) x
79 #define	U16_C(x) x ## U
80 #define	S32_C(x) x
81 #define	U32_C(x) x ## U
82 #define	S64_C(x) x ## LL
83 #define	U64_C(x) x ## ULL
84 
85 #define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
86 #define	BUG_ON(cond)		do {				\
87 	if (cond) {						\
88 		panic("BUG ON %s failed at %s:%d",		\
89 		    __stringify(cond), __FILE__, __LINE__);	\
90 	}							\
91 } while (0)
92 
93 extern int linuxkpi_warn_dump_stack;
94 #define	WARN_ON(cond) ({					\
95 	bool __ret = (cond);					\
96 	if (__ret) {						\
97 		printf("WARNING %s failed at %s:%d\n",		\
98 		    __stringify(cond), __FILE__, __LINE__);	\
99 		if (linuxkpi_warn_dump_stack)				\
100 			linux_dump_stack();				\
101 	}								\
102 	unlikely(__ret);						\
103 })
104 
105 #define	WARN_ON_SMP(cond)	WARN_ON(cond)
106 
107 #define	WARN_ON_ONCE(cond) ({					\
108 	static bool __warn_on_once;				\
109 	bool __ret = (cond);					\
110 	if (__ret && !__warn_on_once) {				\
111 		__warn_on_once = 1;				\
112 		printf("WARNING %s failed at %s:%d\n",		\
113 		    __stringify(cond), __FILE__, __LINE__);	\
114 		if (linuxkpi_warn_dump_stack)				\
115 			linux_dump_stack();				\
116 	}								\
117 	unlikely(__ret);						\
118 })
119 
120 #define	oops_in_progress	SCHEDULER_STOPPED()
121 
122 #undef	ALIGN
123 #define	ALIGN(x, y)		roundup2((x), (y))
124 #define	ALIGN_DOWN(x, y)	rounddown2(x, y)
125 #undef PTR_ALIGN
126 #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
127 #define	IS_ALIGNED(x, a)	(((x) & ((__typeof(x))(a) - 1)) == 0)
128 #define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
129 #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
130 
131 #define	printk(...)		printf(__VA_ARGS__)
132 #define	vprintk(f, a)		vprintf(f, a)
133 
134 #define PTR_IF(x, p)		((x) ? (p) : NULL)
135 
136 #define	asm			__asm
137 
138 extern void linux_dump_stack(void);
139 #define	dump_stack()		linux_dump_stack()
140 
141 struct va_format {
142 	const char *fmt;
143 	va_list *va;
144 };
145 
146 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)147 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
148 {
149 	ssize_t ssize = size;
150 	int i;
151 
152 	i = vsnprintf(buf, size, fmt, args);
153 
154 	return ((i >= ssize) ? (ssize - 1) : i);
155 }
156 
157 static inline int
scnprintf(char * buf,size_t size,const char * fmt,...)158 scnprintf(char *buf, size_t size, const char *fmt, ...)
159 {
160 	va_list args;
161 	int i;
162 
163 	va_start(args, fmt);
164 	i = vscnprintf(buf, size, fmt, args);
165 	va_end(args);
166 
167 	return (i);
168 }
169 
170 /*
171  * The "pr_debug()" and "pr_devel()" macros should produce zero code
172  * unless DEBUG is defined:
173  */
174 #ifdef DEBUG
175 extern int linuxkpi_debug;
176 #define pr_debug(fmt, ...)					\
177 	do {							\
178 		if (linuxkpi_debug)				\
179 			log(LOG_DEBUG, fmt, ##__VA_ARGS__);	\
180 	} while (0)
181 #define pr_devel(fmt, ...) \
182 	log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
183 #else
184 #define pr_debug(fmt, ...) \
185 	({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
186 #define pr_devel(fmt, ...) \
187 	({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
188 #endif
189 
190 #ifndef pr_fmt
191 #define pr_fmt(fmt) fmt
192 #endif
193 
194 /*
195  * Print a one-time message (analogous to WARN_ONCE() et al):
196  */
197 #define printk_once(...) do {			\
198 	static bool __print_once;		\
199 						\
200 	if (!__print_once) {			\
201 		__print_once = true;		\
202 		printk(__VA_ARGS__);		\
203 	}					\
204 } while (0)
205 
206 /*
207  * Log a one-time message (analogous to WARN_ONCE() et al):
208  */
209 #define log_once(level,...) do {		\
210 	static bool __log_once;			\
211 						\
212 	if (unlikely(!__log_once)) {		\
213 		__log_once = true;		\
214 		log(level, __VA_ARGS__);	\
215 	}					\
216 } while (0)
217 
218 #define pr_emerg(fmt, ...) \
219 	log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
220 #define pr_alert(fmt, ...) \
221 	log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
222 #define pr_crit(fmt, ...) \
223 	log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
224 #define pr_err(fmt, ...) \
225 	log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
226 #define pr_err_once(fmt, ...) \
227 	log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
228 #define pr_warning(fmt, ...) \
229 	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
230 #define pr_warn(...) \
231 	pr_warning(__VA_ARGS__)
232 #define pr_warn_once(fmt, ...) \
233 	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
234 #define pr_notice(fmt, ...) \
235 	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
236 #define pr_info(fmt, ...) \
237 	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
238 #define pr_info_once(fmt, ...) \
239 	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
240 #define pr_cont(fmt, ...) \
241 	printk(KERN_CONT fmt, ##__VA_ARGS__)
242 #define	pr_warn_ratelimited(...) do {		\
243 	static linux_ratelimit_t __ratelimited;	\
244 	if (linux_ratelimited(&__ratelimited))	\
245 		pr_warning(__VA_ARGS__);	\
246 } while (0)
247 
248 #ifndef WARN
249 #define	WARN(condition, ...) ({			\
250 	bool __ret_warn_on = (condition);	\
251 	if (unlikely(__ret_warn_on))		\
252 		pr_warning(__VA_ARGS__);	\
253 	unlikely(__ret_warn_on);		\
254 })
255 #endif
256 
257 #ifndef WARN_ONCE
258 #define	WARN_ONCE(condition, ...) ({		\
259 	bool __ret_warn_on = (condition);	\
260 	if (unlikely(__ret_warn_on))		\
261 		pr_warn_once(__VA_ARGS__);	\
262 	unlikely(__ret_warn_on);		\
263 })
264 #endif
265 
266 #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
267 
268 #define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
269 
270 #define _RET_IP_		__builtin_return_address(0)
271 
272 #define offsetofend(t, m)	\
273         (offsetof(t, m) + sizeof((((t *)0)->m)))
274 
275 #define	smp_processor_id()	PCPU_GET(cpuid)
276 #define	num_possible_cpus()	mp_ncpus
277 #define	num_online_cpus()	mp_ncpus
278 
279 #if defined(__i386__) || defined(__amd64__)
280 extern bool linux_cpu_has_clflush;
281 #define	cpu_has_clflush		linux_cpu_has_clflush
282 #endif
283 
284 typedef struct linux_ratelimit {
285 	struct timeval lasttime;
286 	int counter;
287 } linux_ratelimit_t;
288 
289 static inline bool
linux_ratelimited(linux_ratelimit_t * rl)290 linux_ratelimited(linux_ratelimit_t *rl)
291 {
292 	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
293 }
294 
295 #define	__is_constexpr(x) \
296 	__builtin_constant_p(x)
297 
298 /*
299  * The is_signed() macro below returns true if the passed data type is
300  * signed. Else false is returned.
301  */
302 #define	is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
303 
304 #define	TAINT_WARN	0
305 #define	test_taint(x)	(0)
306 #define	add_taint(x,y)	do {	\
307 	} while (0)
308 
309 static inline int
_h2b(const char c)310 _h2b(const char c)
311 {
312 
313 	if (c >= '0' && c <= '9')
314 		return (c - '0');
315 	if (c >= 'a' && c <= 'f')
316 		return (10 + c - 'a');
317 	if (c >= 'A' && c <= 'F')
318 		return (10 + c - 'A');
319 	return (-EINVAL);
320 }
321 
322 static inline int
hex2bin(uint8_t * bindst,const char * hexsrc,size_t binlen)323 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
324 {
325 	int hi4, lo4;
326 
327 	while (binlen > 0) {
328 		hi4 = _h2b(*hexsrc++);
329 		lo4 = _h2b(*hexsrc++);
330 		if (hi4 < 0 || lo4 < 0)
331 			return (-EINVAL);
332 
333 		*bindst++ = (hi4 << 4) | lo4;
334 		binlen--;
335 	}
336 
337 	return (0);
338 }
339 
340 static inline bool
mac_pton(const char * macin,uint8_t * macout)341 mac_pton(const char *macin, uint8_t *macout)
342 {
343 	const char *s, *d;
344 	uint8_t mac[6], hx, lx;
345 	int i;
346 
347 	if (strlen(macin) < (3 * 6 - 1))
348 		return (false);
349 
350 	i = 0;
351 	s = macin;
352 	do {
353 		/* Should we also support '-'-delimiters? */
354 		d = strchrnul(s, ':');
355 		hx = lx = 0;
356 		while (s < d) {
357 			/* Fail on abc:123:xxx:... */
358 			if ((d - s) > 2)
359 				return (false);
360 			/* We do support non-well-formed strings: 3:45:6:... */
361 			if ((d - s) > 1) {
362 				hx = _h2b(*s);
363 				if (hx < 0)
364 					return (false);
365 				s++;
366 			}
367 			lx = _h2b(*s);
368 			if (lx < 0)
369 				return (false);
370 			s++;
371 		}
372 		mac[i] = (hx << 4) | lx;
373 		i++;
374 		if (i >= 6)
375 			return (false);
376 	} while (d != NULL && *d != '\0');
377 
378 	memcpy(macout, mac, 6);
379 	return (true);
380 }
381 
382 #define	DECLARE_FLEX_ARRAY(_t, _n)					\
383     struct { struct { } __dummy_ ## _n; _t _n[0]; }
384 
385 #endif	/* _LINUXKPI_LINUX_KERNEL_H_ */
386