xref: /freebsd/sys/compat/linuxkpi/common/include/linux/kernel.h (revision b87850b96e1e1abc3232fbacf8da955eecf97fc3)
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   * $FreeBSD$
31   */
32  #ifndef	_LINUX_KERNEL_H_
33  #define	_LINUX_KERNEL_H_
34  
35  #include <sys/cdefs.h>
36  #include <sys/types.h>
37  #include <sys/systm.h>
38  #include <sys/param.h>
39  #include <sys/libkern.h>
40  #include <sys/stat.h>
41  #include <sys/smp.h>
42  #include <sys/stddef.h>
43  #include <sys/syslog.h>
44  #include <sys/time.h>
45  
46  #include <linux/bitops.h>
47  #include <linux/compiler.h>
48  #include <linux/errno.h>
49  #include <linux/sched.h>
50  #include <linux/types.h>
51  #include <linux/jiffies.h>
52  #include <linux/log2.h>
53  
54  #include <asm/byteorder.h>
55  #include <asm/uaccess.h>
56  
57  #include <machine/stdarg.h>
58  
59  #define KERN_CONT       ""
60  #define	KERN_EMERG	"<0>"
61  #define	KERN_ALERT	"<1>"
62  #define	KERN_CRIT	"<2>"
63  #define	KERN_ERR	"<3>"
64  #define	KERN_WARNING	"<4>"
65  #define	KERN_NOTICE	"<5>"
66  #define	KERN_INFO	"<6>"
67  #define	KERN_DEBUG	"<7>"
68  
69  #define	U8_MAX		((u8)~0U)
70  #define	S8_MAX		((s8)(U8_MAX >> 1))
71  #define	S8_MIN		((s8)(-S8_MAX - 1))
72  #define	U16_MAX		((u16)~0U)
73  #define	S16_MAX		((s16)(U16_MAX >> 1))
74  #define	S16_MIN		((s16)(-S16_MAX - 1))
75  #define	U32_MAX		((u32)~0U)
76  #define	S32_MAX		((s32)(U32_MAX >> 1))
77  #define	S32_MIN		((s32)(-S32_MAX - 1))
78  #define	U64_MAX		((u64)~0ULL)
79  #define	S64_MAX		((s64)(U64_MAX >> 1))
80  #define	S64_MIN		((s64)(-S64_MAX - 1))
81  
82  #define	S8_C(x)  x
83  #define	U8_C(x)  x ## U
84  #define	S16_C(x) x
85  #define	U16_C(x) x ## U
86  #define	S32_C(x) x
87  #define	U32_C(x) x ## U
88  #define	S64_C(x) x ## LL
89  #define	U64_C(x) x ## ULL
90  
91  #define	BUILD_BUG()			do { CTASSERT(0); } while (0)
92  #define	BUILD_BUG_ON(x)			CTASSERT(!(x))
93  #define	BUILD_BUG_ON_MSG(x, msg)	BUILD_BUG_ON(x)
94  #define	BUILD_BUG_ON_NOT_POWER_OF_2(x)	BUILD_BUG_ON(!powerof2(x))
95  #define	BUILD_BUG_ON_INVALID(expr)	while (0) { (void)(expr); }
96  
97  #define	BUG()			panic("BUG at %s:%d", __FILE__, __LINE__)
98  #define	BUG_ON(cond)		do {				\
99  	if (cond) {						\
100  		panic("BUG ON %s failed at %s:%d",		\
101  		    __stringify(cond), __FILE__, __LINE__);	\
102  	}							\
103  } while (0)
104  
105  #define	WARN_ON(cond) ({					\
106        bool __ret = (cond);					\
107        if (__ret) {						\
108  		printf("WARNING %s failed at %s:%d\n",		\
109  		    __stringify(cond), __FILE__, __LINE__);	\
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        }								\
124        unlikely(__ret);						\
125  })
126  
127  #define	oops_in_progress	SCHEDULER_STOPPED()
128  
129  #undef	ALIGN
130  #define	ALIGN(x, y)		roundup2((x), (y))
131  #undef PTR_ALIGN
132  #define	PTR_ALIGN(p, a)		((__typeof(p))ALIGN((uintptr_t)(p), (a)))
133  #define	DIV_ROUND_UP(x, n)	howmany(x, n)
134  #define	__KERNEL_DIV_ROUND_UP(x, n)	howmany(x, n)
135  #define	DIV_ROUND_UP_ULL(x, n)	DIV_ROUND_UP((unsigned long long)(x), (n))
136  #define	FIELD_SIZEOF(t, f)	sizeof(((t *)0)->f)
137  
138  #define	printk(...)		printf(__VA_ARGS__)
139  #define	vprintk(f, a)		vprintf(f, a)
140  
141  #define	asm			__asm
142  
143  extern void linux_dump_stack(void);
144  #define	dump_stack()		linux_dump_stack()
145  
146  struct va_format {
147  	const char *fmt;
148  	va_list *va;
149  };
150  
151  static inline int
152  vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
153  {
154  	ssize_t ssize = size;
155  	int i;
156  
157  	i = vsnprintf(buf, size, fmt, args);
158  
159  	return ((i >= ssize) ? (ssize - 1) : i);
160  }
161  
162  static inline int
163  scnprintf(char *buf, size_t size, const char *fmt, ...)
164  {
165  	va_list args;
166  	int i;
167  
168  	va_start(args, fmt);
169  	i = vscnprintf(buf, size, fmt, args);
170  	va_end(args);
171  
172  	return (i);
173  }
174  
175  /*
176   * The "pr_debug()" and "pr_devel()" macros should produce zero code
177   * unless DEBUG is defined:
178   */
179  #ifdef DEBUG
180  #define pr_debug(fmt, ...) \
181  	log(LOG_DEBUG, fmt, ##__VA_ARGS__)
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_warning(fmt, ...) \
228  	log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
229  #define pr_warn(...) \
230  	pr_warning(__VA_ARGS__)
231  #define pr_warn_once(fmt, ...) \
232  	log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
233  #define pr_notice(fmt, ...) \
234  	log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
235  #define pr_info(fmt, ...) \
236  	log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
237  #define pr_info_once(fmt, ...) \
238  	log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
239  #define pr_cont(fmt, ...) \
240  	printk(KERN_CONT fmt, ##__VA_ARGS__)
241  #define	pr_warn_ratelimited(...) do {		\
242  	static linux_ratelimit_t __ratelimited;	\
243  	if (linux_ratelimited(&__ratelimited))	\
244  		pr_warning(__VA_ARGS__);	\
245  } while (0)
246  
247  #ifndef WARN
248  #define	WARN(condition, ...) ({			\
249  	bool __ret_warn_on = (condition);	\
250  	if (unlikely(__ret_warn_on))		\
251  		pr_warning(__VA_ARGS__);	\
252  	unlikely(__ret_warn_on);		\
253  })
254  #endif
255  
256  #ifndef WARN_ONCE
257  #define	WARN_ONCE(condition, ...) ({		\
258  	bool __ret_warn_on = (condition);	\
259  	if (unlikely(__ret_warn_on))		\
260  		pr_warn_once(__VA_ARGS__);	\
261  	unlikely(__ret_warn_on);		\
262  })
263  #endif
264  
265  #define container_of(ptr, type, member)				\
266  ({								\
267  	const __typeof(((type *)0)->member) *__p = (ptr);	\
268  	(type *)((uintptr_t)__p - offsetof(type, member));	\
269  })
270  
271  #define	ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
272  
273  #define	u64_to_user_ptr(val)	((void *)(uintptr_t)(val))
274  
275  static inline unsigned long long
276  simple_strtoull(const char *cp, char **endp, unsigned int base)
277  {
278  	return (strtouq(cp, endp, base));
279  }
280  
281  static inline long long
282  simple_strtoll(const char *cp, char **endp, unsigned int base)
283  {
284  	return (strtoq(cp, endp, base));
285  }
286  
287  static inline unsigned long
288  simple_strtoul(const char *cp, char **endp, unsigned int base)
289  {
290  	return (strtoul(cp, endp, base));
291  }
292  
293  static inline long
294  simple_strtol(const char *cp, char **endp, unsigned int base)
295  {
296  	return (strtol(cp, endp, base));
297  }
298  
299  static inline int
300  kstrtoul(const char *cp, unsigned int base, unsigned long *res)
301  {
302  	char *end;
303  
304  	*res = strtoul(cp, &end, base);
305  
306  	/* skip newline character, if any */
307  	if (*end == '\n')
308  		end++;
309  	if (*cp == 0 || *end != 0)
310  		return (-EINVAL);
311  	return (0);
312  }
313  
314  static inline int
315  kstrtol(const char *cp, unsigned int base, long *res)
316  {
317  	char *end;
318  
319  	*res = strtol(cp, &end, base);
320  
321  	/* skip newline character, if any */
322  	if (*end == '\n')
323  		end++;
324  	if (*cp == 0 || *end != 0)
325  		return (-EINVAL);
326  	return (0);
327  }
328  
329  static inline int
330  kstrtoint(const char *cp, unsigned int base, int *res)
331  {
332  	char *end;
333  	long temp;
334  
335  	*res = temp = strtol(cp, &end, base);
336  
337  	/* skip newline character, if any */
338  	if (*end == '\n')
339  		end++;
340  	if (*cp == 0 || *end != 0)
341  		return (-EINVAL);
342  	if (temp != (int)temp)
343  		return (-ERANGE);
344  	return (0);
345  }
346  
347  static inline int
348  kstrtouint(const char *cp, unsigned int base, unsigned int *res)
349  {
350  	char *end;
351  	unsigned long temp;
352  
353  	*res = temp = strtoul(cp, &end, base);
354  
355  	/* skip newline character, if any */
356  	if (*end == '\n')
357  		end++;
358  	if (*cp == 0 || *end != 0)
359  		return (-EINVAL);
360  	if (temp != (unsigned int)temp)
361  		return (-ERANGE);
362  	return (0);
363  }
364  
365  static inline int
366  kstrtou32(const char *cp, unsigned int base, u32 *res)
367  {
368  	char *end;
369  	unsigned long temp;
370  
371  	*res = temp = strtoul(cp, &end, base);
372  
373  	/* skip newline character, if any */
374  	if (*end == '\n')
375  		end++;
376  	if (*cp == 0 || *end != 0)
377  		return (-EINVAL);
378  	if (temp != (u32)temp)
379  		return (-ERANGE);
380  	return (0);
381  }
382  
383  static inline int
384  kstrtobool(const char *s, bool *res)
385  {
386  	int len;
387  
388  	if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
389  		return (-EINVAL);
390  
391  	/* skip newline character, if any */
392  	if (s[len - 1] == '\n')
393  		len--;
394  
395  	if (len == 1 && strchr("yY1", s[0]) != NULL)
396  		*res = true;
397  	else if (len == 1 && strchr("nN0", s[0]) != NULL)
398  		*res = false;
399  	else if (strncasecmp("on", s, len) == 0)
400  		*res = true;
401  	else if (strncasecmp("off", s, len) == 0)
402  		*res = false;
403  	else
404  		return (-EINVAL);
405  
406  	return (0);
407  }
408  
409  static inline int
410  kstrtobool_from_user(const char __user *s, size_t count, bool *res)
411  {
412  	char buf[8] = {};
413  
414  	if (count > (sizeof(buf) - 1))
415  		count = (sizeof(buf) - 1);
416  
417  	if (copy_from_user(buf, s, count))
418  		return (-EFAULT);
419  
420  	return (kstrtobool(buf, res));
421  }
422  
423  #define min(x, y)	((x) < (y) ? (x) : (y))
424  #define max(x, y)	((x) > (y) ? (x) : (y))
425  
426  #define min3(a, b, c)	min(a, min(b,c))
427  #define max3(a, b, c)	max(a, max(b,c))
428  
429  #define	min_t(type, x, y) ({			\
430  	type __min1 = (x);			\
431  	type __min2 = (y);			\
432  	__min1 < __min2 ? __min1 : __min2; })
433  
434  #define	max_t(type, x, y) ({			\
435  	type __max1 = (x);			\
436  	type __max2 = (y);			\
437  	__max1 > __max2 ? __max1 : __max2; })
438  
439  #define clamp_t(type, _x, min, max)	min_t(type, max_t(type, _x, min), max)
440  #define clamp(x, lo, hi)		min( max(x,lo), hi)
441  #define	clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
442  
443  /*
444   * This looks more complex than it should be. But we need to
445   * get the type for the ~ right in round_down (it needs to be
446   * as wide as the result!), and we want to evaluate the macro
447   * arguments just once each.
448   */
449  #define __round_mask(x, y) ((__typeof__(x))((y)-1))
450  #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
451  #define round_down(x, y) ((x) & ~__round_mask(x, y))
452  
453  #define	smp_processor_id()	PCPU_GET(cpuid)
454  #define	num_possible_cpus()	mp_ncpus
455  #define	num_online_cpus()	mp_ncpus
456  
457  #if defined(__i386__) || defined(__amd64__)
458  extern bool linux_cpu_has_clflush;
459  #define	cpu_has_clflush		linux_cpu_has_clflush
460  #endif
461  
462  typedef struct pm_message {
463  	int event;
464  } pm_message_t;
465  
466  /* Swap values of a and b */
467  #define swap(a, b) do {			\
468  	typeof(a) _swap_tmp = a;	\
469  	a = b;				\
470  	b = _swap_tmp;			\
471  } while (0)
472  
473  #define	DIV_ROUND_CLOSEST(x, divisor)	(((x) + ((divisor) / 2)) / (divisor))
474  
475  #define	DIV_ROUND_CLOSEST_ULL(x, divisor) ({		\
476  	__typeof(divisor) __d = (divisor);		\
477  	unsigned long long __ret = (x) + (__d) / 2;	\
478  	__ret /= __d;					\
479  	__ret;						\
480  })
481  
482  static inline uintmax_t
483  mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
484  {
485  	uintmax_t q = (x / divisor);
486  	uintmax_t r = (x % divisor);
487  
488  	return ((q * multiplier) + ((r * multiplier) / divisor));
489  }
490  
491  static inline int64_t
492  abs64(int64_t x)
493  {
494  	return (x < 0 ? -x : x);
495  }
496  
497  typedef struct linux_ratelimit {
498  	struct timeval lasttime;
499  	int counter;
500  } linux_ratelimit_t;
501  
502  static inline bool
503  linux_ratelimited(linux_ratelimit_t *rl)
504  {
505  	return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
506  }
507  
508  #endif	/* _LINUX_KERNEL_H_ */
509