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