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 _LINUXKPI_LINUX_KERNEL_H_
33 #define _LINUXKPI_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/stringify.h>
49 #include <linux/errno.h>
50 #include <linux/sched.h>
51 #include <linux/types.h>
52 #include <linux/typecheck.h>
53 #include <linux/jiffies.h>
54 #include <linux/log2.h>
55
56 #include <asm/byteorder.h>
57 #include <asm/cpufeature.h>
58 #include <asm/processor.h>
59 #include <asm/uaccess.h>
60
61 #include <machine/stdarg.h>
62
63 #define KERN_CONT ""
64 #define KERN_EMERG "<0>"
65 #define KERN_ALERT "<1>"
66 #define KERN_CRIT "<2>"
67 #define KERN_ERR "<3>"
68 #define KERN_WARNING "<4>"
69 #define KERN_NOTICE "<5>"
70 #define KERN_INFO "<6>"
71 #define KERN_DEBUG "<7>"
72
73 #define U8_MAX ((u8)~0U)
74 #define S8_MAX ((s8)(U8_MAX >> 1))
75 #define S8_MIN ((s8)(-S8_MAX - 1))
76 #define U16_MAX ((u16)~0U)
77 #define S16_MAX ((s16)(U16_MAX >> 1))
78 #define S16_MIN ((s16)(-S16_MAX - 1))
79 #define U32_MAX ((u32)~0U)
80 #define S32_MAX ((s32)(U32_MAX >> 1))
81 #define S32_MIN ((s32)(-S32_MAX - 1))
82 #define U64_MAX ((u64)~0ULL)
83 #define S64_MAX ((s64)(U64_MAX >> 1))
84 #define S64_MIN ((s64)(-S64_MAX - 1))
85
86 #define S8_C(x) x
87 #define U8_C(x) x ## U
88 #define S16_C(x) x
89 #define U16_C(x) x ## U
90 #define S32_C(x) x
91 #define U32_C(x) x ## U
92 #define S64_C(x) x ## LL
93 #define U64_C(x) x ## ULL
94
95 /*
96 * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not
97 * seem to work. Use old-schoold-ish CTASSERT from before commit
98 * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not
99 * end up with an unused typedef or variable. The compiler should optimise
100 * it away entirely.
101 */
102 #define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__)
103 #define _O__CTASSERT(x, y) _O___CTASSERT(x, y)
104 #define _O___CTASSERT(x, y) while (0) { \
105 typedef char __assert_line_ ## y[(x) ? 1 : -1]; \
106 __assert_line_ ## y _x; \
107 _x[0] = '\0'; \
108 }
109
110 #define BUILD_BUG() do { CTASSERT(0); } while (0)
111 #define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0)
112 #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x)
113 #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x))
114 #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); }
115 #define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); }))
116
117 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__)
118 #define BUG_ON(cond) do { \
119 if (cond) { \
120 panic("BUG ON %s failed at %s:%d", \
121 __stringify(cond), __FILE__, __LINE__); \
122 } \
123 } while (0)
124
125 extern int linuxkpi_warn_dump_stack;
126 #define WARN_ON(cond) ({ \
127 bool __ret = (cond); \
128 if (__ret) { \
129 printf("WARNING %s failed at %s:%d\n", \
130 __stringify(cond), __FILE__, __LINE__); \
131 if (linuxkpi_warn_dump_stack) \
132 linux_dump_stack(); \
133 } \
134 unlikely(__ret); \
135 })
136
137 #define WARN_ON_SMP(cond) WARN_ON(cond)
138
139 #define WARN_ON_ONCE(cond) ({ \
140 static bool __warn_on_once; \
141 bool __ret = (cond); \
142 if (__ret && !__warn_on_once) { \
143 __warn_on_once = 1; \
144 printf("WARNING %s failed at %s:%d\n", \
145 __stringify(cond), __FILE__, __LINE__); \
146 if (linuxkpi_warn_dump_stack) \
147 linux_dump_stack(); \
148 } \
149 unlikely(__ret); \
150 })
151
152 #define oops_in_progress SCHEDULER_STOPPED()
153
154 #undef ALIGN
155 #define ALIGN(x, y) roundup2((x), (y))
156 #define ALIGN_DOWN(x, y) rounddown2(x, y)
157 #undef PTR_ALIGN
158 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
159 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
160 #define DIV_ROUND_UP(x, n) howmany(x, n)
161 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n)
162 #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n))
163 #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n))
164 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f)
165
166 #define printk(...) printf(__VA_ARGS__)
167 #define vprintk(f, a) vprintf(f, a)
168
169 #define asm __asm
170
171 extern void linux_dump_stack(void);
172 #define dump_stack() linux_dump_stack()
173
174 struct va_format {
175 const char *fmt;
176 va_list *va;
177 };
178
179 static inline int
180 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
181 {
182 ssize_t ssize = size;
183 int i;
184
185 i = vsnprintf(buf, size, fmt, args);
186
187 return ((i >= ssize) ? (ssize - 1) : i);
188 }
189
190 static inline int
191 scnprintf(char *buf, size_t size, const char *fmt, ...)
192 {
193 va_list args;
194 int i;
195
196 va_start(args, fmt);
197 i = vscnprintf(buf, size, fmt, args);
198 va_end(args);
199
200 return (i);
201 }
202
203 /*
204 * The "pr_debug()" and "pr_devel()" macros should produce zero code
205 * unless DEBUG is defined:
206 */
207 #ifdef DEBUG
208 extern int linuxkpi_debug;
209 #define pr_debug(fmt, ...) \
210 do { \
211 if (linuxkpi_debug) \
212 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \
213 } while (0)
214 #define pr_devel(fmt, ...) \
215 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
216 #else
217 #define pr_debug(fmt, ...) \
218 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
219 #define pr_devel(fmt, ...) \
220 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
221 #endif
222
223 #ifndef pr_fmt
224 #define pr_fmt(fmt) fmt
225 #endif
226
227 /*
228 * Print a one-time message (analogous to WARN_ONCE() et al):
229 */
230 #define printk_once(...) do { \
231 static bool __print_once; \
232 \
233 if (!__print_once) { \
234 __print_once = true; \
235 printk(__VA_ARGS__); \
236 } \
237 } while (0)
238
239 /*
240 * Log a one-time message (analogous to WARN_ONCE() et al):
241 */
242 #define log_once(level,...) do { \
243 static bool __log_once; \
244 \
245 if (unlikely(!__log_once)) { \
246 __log_once = true; \
247 log(level, __VA_ARGS__); \
248 } \
249 } while (0)
250
251 #define pr_emerg(fmt, ...) \
252 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
253 #define pr_alert(fmt, ...) \
254 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
255 #define pr_crit(fmt, ...) \
256 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
257 #define pr_err(fmt, ...) \
258 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
259 #define pr_err_once(fmt, ...) \
260 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
261 #define pr_warning(fmt, ...) \
262 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
263 #define pr_warn(...) \
264 pr_warning(__VA_ARGS__)
265 #define pr_warn_once(fmt, ...) \
266 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
267 #define pr_notice(fmt, ...) \
268 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
269 #define pr_info(fmt, ...) \
270 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
271 #define pr_info_once(fmt, ...) \
272 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
273 #define pr_cont(fmt, ...) \
274 printk(KERN_CONT fmt, ##__VA_ARGS__)
275 #define pr_warn_ratelimited(...) do { \
276 static linux_ratelimit_t __ratelimited; \
277 if (linux_ratelimited(&__ratelimited)) \
278 pr_warning(__VA_ARGS__); \
279 } while (0)
280
281 #ifndef WARN
282 #define WARN(condition, ...) ({ \
283 bool __ret_warn_on = (condition); \
284 if (unlikely(__ret_warn_on)) \
285 pr_warning(__VA_ARGS__); \
286 unlikely(__ret_warn_on); \
287 })
288 #endif
289
290 #ifndef WARN_ONCE
291 #define WARN_ONCE(condition, ...) ({ \
292 bool __ret_warn_on = (condition); \
293 if (unlikely(__ret_warn_on)) \
294 pr_warn_once(__VA_ARGS__); \
295 unlikely(__ret_warn_on); \
296 })
297 #endif
298
299 #define container_of(ptr, type, member) \
300 ({ \
301 const __typeof(((type *)0)->member) *__p = (ptr); \
302 (type *)((uintptr_t)__p - offsetof(type, member)); \
303 })
304
305 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
306
307 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val))
308
309 #define _RET_IP_ __builtin_return_address(0)
310
311 static inline unsigned long long
312 simple_strtoull(const char *cp, char **endp, unsigned int base)
313 {
314 return (strtouq(cp, endp, base));
315 }
316
317 static inline long long
318 simple_strtoll(const char *cp, char **endp, unsigned int base)
319 {
320 return (strtoq(cp, endp, base));
321 }
322
323 static inline unsigned long
324 simple_strtoul(const char *cp, char **endp, unsigned int base)
325 {
326 return (strtoul(cp, endp, base));
327 }
328
329 static inline long
330 simple_strtol(const char *cp, char **endp, unsigned int base)
331 {
332 return (strtol(cp, endp, base));
333 }
334
335 static inline int
336 kstrtoul(const char *cp, unsigned int base, unsigned long *res)
337 {
338 char *end;
339
340 *res = strtoul(cp, &end, base);
341
342 /* skip newline character, if any */
343 if (*end == '\n')
344 end++;
345 if (*cp == 0 || *end != 0)
346 return (-EINVAL);
347 return (0);
348 }
349
350 static inline int
351 kstrtol(const char *cp, unsigned int base, long *res)
352 {
353 char *end;
354
355 *res = strtol(cp, &end, base);
356
357 /* skip newline character, if any */
358 if (*end == '\n')
359 end++;
360 if (*cp == 0 || *end != 0)
361 return (-EINVAL);
362 return (0);
363 }
364
365 static inline int
366 kstrtoint(const char *cp, unsigned int base, int *res)
367 {
368 char *end;
369 long temp;
370
371 *res = temp = strtol(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 != (int)temp)
379 return (-ERANGE);
380 return (0);
381 }
382
383 static inline int
384 kstrtouint(const char *cp, unsigned int base, unsigned int *res)
385 {
386 char *end;
387 unsigned long temp;
388
389 *res = temp = strtoul(cp, &end, base);
390
391 /* skip newline character, if any */
392 if (*end == '\n')
393 end++;
394 if (*cp == 0 || *end != 0)
395 return (-EINVAL);
396 if (temp != (unsigned int)temp)
397 return (-ERANGE);
398 return (0);
399 }
400
401 static inline int
402 kstrtou8(const char *cp, unsigned int base, u8 *res)
403 {
404 char *end;
405 unsigned long temp;
406
407 *res = temp = strtoul(cp, &end, base);
408
409 /* skip newline character, if any */
410 if (*end == '\n')
411 end++;
412 if (*cp == 0 || *end != 0)
413 return (-EINVAL);
414 if (temp != (u8)temp)
415 return (-ERANGE);
416 return (0);
417 }
418
419 static inline int
420 kstrtou16(const char *cp, unsigned int base, u16 *res)
421 {
422 char *end;
423 unsigned long temp;
424
425 *res = temp = strtoul(cp, &end, base);
426
427 /* skip newline character, if any */
428 if (*end == '\n')
429 end++;
430 if (*cp == 0 || *end != 0)
431 return (-EINVAL);
432 if (temp != (u16)temp)
433 return (-ERANGE);
434 return (0);
435 }
436
437 static inline int
438 kstrtou32(const char *cp, unsigned int base, u32 *res)
439 {
440
441 return (kstrtouint(cp, base, res));
442 }
443
444 static inline int
445 kstrtou64(const char *cp, unsigned int base, u64 *res)
446 {
447 char *end;
448
449 *res = strtouq(cp, &end, base);
450
451 /* skip newline character, if any */
452 if (*end == '\n')
453 end++;
454 if (*cp == 0 || *end != 0)
455 return (-EINVAL);
456 return (0);
457 }
458
459 static inline int
460 kstrtoull(const char *cp, unsigned int base, unsigned long long *res)
461 {
462 return (kstrtou64(cp, base, (u64 *)res));
463 }
464
465 static inline int
466 kstrtobool(const char *s, bool *res)
467 {
468 int len;
469
470 if (s == NULL || (len = strlen(s)) == 0 || res == NULL)
471 return (-EINVAL);
472
473 /* skip newline character, if any */
474 if (s[len - 1] == '\n')
475 len--;
476
477 if (len == 1 && strchr("yY1", s[0]) != NULL)
478 *res = true;
479 else if (len == 1 && strchr("nN0", s[0]) != NULL)
480 *res = false;
481 else if (strncasecmp("on", s, len) == 0)
482 *res = true;
483 else if (strncasecmp("off", s, len) == 0)
484 *res = false;
485 else
486 return (-EINVAL);
487
488 return (0);
489 }
490
491 static inline int
492 kstrtobool_from_user(const char __user *s, size_t count, bool *res)
493 {
494 char buf[8] = {};
495
496 if (count > (sizeof(buf) - 1))
497 count = (sizeof(buf) - 1);
498
499 if (copy_from_user(buf, s, count))
500 return (-EFAULT);
501
502 return (kstrtobool(buf, res));
503 }
504
505 static inline int
506 kstrtoint_from_user(const char __user *s, size_t count, unsigned int base,
507 int *p)
508 {
509 char buf[36] = {};
510
511 if (count > (sizeof(buf) - 1))
512 count = (sizeof(buf) - 1);
513
514 if (copy_from_user(buf, s, count))
515 return (-EFAULT);
516
517 return (kstrtoint(buf, base, p));
518 }
519
520 static inline int
521 kstrtouint_from_user(const char __user *s, size_t count, unsigned int base,
522 unsigned int *p)
523 {
524 char buf[36] = {};
525
526 if (count > (sizeof(buf) - 1))
527 count = (sizeof(buf) - 1);
528
529 if (copy_from_user(buf, s, count))
530 return (-EFAULT);
531
532 return (kstrtouint(buf, base, p));
533 }
534
535 static inline int
536 kstrtou32_from_user(const char __user *s, size_t count, unsigned int base,
537 unsigned int *p)
538 {
539
540 return (kstrtouint_from_user(s, count, base, p));
541 }
542
543 static inline int
544 kstrtou8_from_user(const char __user *s, size_t count, unsigned int base,
545 u8 *p)
546 {
547 char buf[8] = {};
548
549 if (count > (sizeof(buf) - 1))
550 count = (sizeof(buf) - 1);
551
552 if (copy_from_user(buf, s, count))
553 return (-EFAULT);
554
555 return (kstrtou8(buf, base, p));
556 }
557
558 #define min(x, y) ((x) < (y) ? (x) : (y))
559 #define max(x, y) ((x) > (y) ? (x) : (y))
560
561 #define min3(a, b, c) min(a, min(b,c))
562 #define max3(a, b, c) max(a, max(b,c))
563
564 #define min_t(type, x, y) ({ \
565 type __min1 = (x); \
566 type __min2 = (y); \
567 __min1 < __min2 ? __min1 : __min2; })
568
569 #define max_t(type, x, y) ({ \
570 type __max1 = (x); \
571 type __max2 = (y); \
572 __max1 > __max2 ? __max1 : __max2; })
573
574 #define offsetofend(t, m) \
575 (offsetof(t, m) + sizeof((((t *)0)->m)))
576
577 #define typeof_member(s, e) typeof(((s *)0)->e)
578
579 #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max)
580 #define clamp(x, lo, hi) min( max(x,lo), hi)
581 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
582
583 /*
584 * This looks more complex than it should be. But we need to
585 * get the type for the ~ right in round_down (it needs to be
586 * as wide as the result!), and we want to evaluate the macro
587 * arguments just once each.
588 */
589 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
590 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
591 #define round_down(x, y) ((x) & ~__round_mask(x, y))
592
593 #define smp_processor_id() PCPU_GET(cpuid)
594 #define num_possible_cpus() mp_ncpus
595 #define num_online_cpus() mp_ncpus
596
597 #if defined(__i386__) || defined(__amd64__)
598 extern bool linux_cpu_has_clflush;
599 #define cpu_has_clflush linux_cpu_has_clflush
600 #endif
601
602 /* Swap values of a and b */
603 #define swap(a, b) do { \
604 typeof(a) _swap_tmp = a; \
605 a = b; \
606 b = _swap_tmp; \
607 } while (0)
608
609 #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor))
610
611 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \
612 __typeof(divisor) __d = (divisor); \
613 unsigned long long __ret = (x) + (__d) / 2; \
614 __ret /= __d; \
615 __ret; \
616 })
617
618 static inline uintmax_t
619 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
620 {
621 uintmax_t q = (x / divisor);
622 uintmax_t r = (x % divisor);
623
624 return ((q * multiplier) + ((r * multiplier) / divisor));
625 }
626
627 static inline int64_t
628 abs64(int64_t x)
629 {
630 return (x < 0 ? -x : x);
631 }
632
633 typedef struct linux_ratelimit {
634 struct timeval lasttime;
635 int counter;
636 } linux_ratelimit_t;
637
638 static inline bool
639 linux_ratelimited(linux_ratelimit_t *rl)
640 {
641 return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
642 }
643
644 #define struct_size(ptr, field, num) ({ \
645 const size_t __size = offsetof(__typeof(*(ptr)), field); \
646 const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \
647 ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \
648 })
649
650 #define __is_constexpr(x) \
651 __builtin_constant_p(x)
652
653 /*
654 * The is_signed() macro below returns true if the passed data type is
655 * signed. Else false is returned.
656 */
657 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
658
659 /*
660 * The type_max() macro below returns the maxium positive value the
661 * passed data type can hold.
662 */
663 #define type_max(datatype) ( \
664 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \
665 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \
666 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \
667 (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \
668 )
669
670 /*
671 * The type_min() macro below returns the minimum value the passed
672 * data type can hold. For unsigned types the minimum value is always
673 * zero. For signed types it may vary.
674 */
675 #define type_min(datatype) ( \
676 (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \
677 (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \
678 (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \
679 (is_signed(datatype) ? INT8_MIN : 0) \
680 )
681
682 #define TAINT_WARN 0
683 #define test_taint(x) (0)
684 #define add_taint(x,y) do { \
685 } while (0)
686
687 static inline int
688 _h2b(const char c)
689 {
690
691 if (c >= '' && c <= '9')
692 return (c - '');
693 if (c >= 'a' && c <= 'f')
694 return (10 + c - 'a');
695 if (c >= 'A' && c <= 'F')
696 return (10 + c - 'A');
697 return (-EINVAL);
698 }
699
700 static inline int
701 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen)
702 {
703 int hi4, lo4;
704
705 while (binlen > 0) {
706 hi4 = _h2b(*hexsrc++);
707 lo4 = _h2b(*hexsrc++);
708 if (hi4 < 0 || lo4 < 0)
709 return (-EINVAL);
710
711 *bindst++ = (hi4 << 4) | lo4;
712 binlen--;
713 }
714
715 return (0);
716 }
717
718 static inline bool
719 mac_pton(const char *macin, uint8_t *macout)
720 {
721 const char *s, *d;
722 uint8_t mac[6], hx, lx;;
723 int i;
724
725 if (strlen(macin) < (3 * 6 - 1))
726 return (false);
727
728 i = 0;
729 s = macin;
730 do {
731 /* Should we also support '-'-delimiters? */
732 d = strchrnul(s, ':');
733 hx = lx = 0;
734 while (s < d) {
735 /* Fail on abc:123:xxx:... */
736 if ((d - s) > 2)
737 return (false);
738 /* We do support non-well-formed strings: 3:45:6:... */
739 if ((d - s) > 1) {
740 hx = _h2b(*s);
741 if (hx < 0)
742 return (false);
743 s++;
744 }
745 lx = _h2b(*s);
746 if (lx < 0)
747 return (false);
748 s++;
749 }
750 mac[i] = (hx << 4) | lx;
751 i++;
752 if (i >= 6)
753 return (false);
754 } while (d != NULL && *d != '\0');
755
756 memcpy(macout, mac, 6);
757 return (true);
758 }
759
760 #define DECLARE_FLEX_ARRAY(_t, _n) \
761 struct { struct { } __dummy_ ## _n; _t _n[0]; }
762
763 /*
764 * Checking if an option is defined would be easy if we could do CPP inside CPP.
765 * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with. In either
766 * case the defined value is "1". A more general -Dxxx=<c> case will require
767 * more effort to deal with all possible "true" values. Hope we do not have
768 * to do this as well.
769 * The real problem is the undefined case. To avoid this problem we do the
770 * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1"
771 * by having a #define for yyy_1 which is "ignore,".
772 * Otherwise we will just get "yyy".
773 * Need to be careful about variable substitutions in macros though.
774 * This way we make a (true, false) problem a (don't care, true, false) or a
775 * (don't care true, false). Then we can use a variadic macro to only select
776 * the always well known and defined argument #2. And that seems to be
777 * exactly what we need. Use 1 for true and 0 for false to also allow
778 * #if IS_*() checks pre-compiler checks which do not like #if true.
779 */
780 #define ___XAB_1 dontcare,
781 #define ___IS_XAB(_ignore, _x, ...) (_x)
782 #define __IS_XAB(_x) ___IS_XAB(_x 1, 0)
783 #define _IS_XAB(_x) __IS_XAB(__CONCAT(___XAB_, _x))
784
785 /* This is if CONFIG_ccc=y. */
786 #define IS_BUILTIN(_x) _IS_XAB(_x)
787 /* This is if CONFIG_ccc=m. */
788 #define IS_MODULE(_x) _IS_XAB(_x ## _MODULE)
789 /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */
790 #define IS_ENABLED(_x) (IS_BUILTIN(_x) || IS_MODULE(_x))
791 /*
792 * This is weird case. If the CONFIG_ccc is builtin (=y) this returns true;
793 * or if the CONFIG_ccc is a module (=m) and the caller is built as a module
794 * (-DMODULE defined) this returns true, but if the callers is not a module
795 * (-DMODULE not defined, which means caller is BUILTIN) then it returns
796 * false. In other words, a module can reach the kernel, a module can reach
797 * a module, but the kernel cannot reach a module, and code never compiled
798 * cannot be reached either.
799 * XXX -- I'd hope the module-to-module case would be handled by a proper
800 * module dependency definition (MODULE_DEPEND() in FreeBSD).
801 */
802 #define IS_REACHABLE(_x) (IS_BUILTIN(_x) || \
803 (IS_MODULE(_x) && IS_BUILTIN(MODULE)))
804
805 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */
Cache object: d122b1cdb0b1f8a839cf216a9834b6ae
|