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