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