18d59ecb2SHans Petter Selasky /*- 28d59ecb2SHans Petter Selasky * Copyright (c) 2010 Isilon Systems, Inc. 38d59ecb2SHans Petter Selasky * Copyright (c) 2010 iX Systems, Inc. 48d59ecb2SHans Petter Selasky * Copyright (c) 2010 Panasas, Inc. 5fe68f570SHans Petter Selasky * Copyright (c) 2013-2016 Mellanox Technologies, Ltd. 68d59ecb2SHans Petter Selasky * Copyright (c) 2014-2015 François Tigeot 78d59ecb2SHans Petter Selasky * All rights reserved. 88d59ecb2SHans Petter Selasky * 98d59ecb2SHans Petter Selasky * Redistribution and use in source and binary forms, with or without 108d59ecb2SHans Petter Selasky * modification, are permitted provided that the following conditions 118d59ecb2SHans Petter Selasky * are met: 128d59ecb2SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright 138d59ecb2SHans Petter Selasky * notice unmodified, this list of conditions, and the following 148d59ecb2SHans Petter Selasky * disclaimer. 158d59ecb2SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright 168d59ecb2SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the 178d59ecb2SHans Petter Selasky * documentation and/or other materials provided with the distribution. 188d59ecb2SHans Petter Selasky * 198d59ecb2SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 208d59ecb2SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 218d59ecb2SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 228d59ecb2SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 238d59ecb2SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 248d59ecb2SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 258d59ecb2SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 268d59ecb2SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 278d59ecb2SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 288d59ecb2SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 298d59ecb2SHans Petter Selasky * 308d59ecb2SHans Petter Selasky * $FreeBSD$ 318d59ecb2SHans Petter Selasky */ 32307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_KERNEL_H_ 33307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_KERNEL_H_ 348d59ecb2SHans Petter Selasky 358d59ecb2SHans Petter Selasky #include <sys/cdefs.h> 368d59ecb2SHans Petter Selasky #include <sys/types.h> 378d59ecb2SHans Petter Selasky #include <sys/systm.h> 388d59ecb2SHans Petter Selasky #include <sys/param.h> 398d59ecb2SHans Petter Selasky #include <sys/libkern.h> 408d59ecb2SHans Petter Selasky #include <sys/stat.h> 418d59ecb2SHans Petter Selasky #include <sys/smp.h> 428d59ecb2SHans Petter Selasky #include <sys/stddef.h> 438d59ecb2SHans Petter Selasky #include <sys/syslog.h> 448f7eee5aSHans Petter Selasky #include <sys/time.h> 458d59ecb2SHans Petter Selasky 468d59ecb2SHans Petter Selasky #include <linux/bitops.h> 478d59ecb2SHans Petter Selasky #include <linux/compiler.h> 4843b4c006SBjoern A. Zeeb #include <linux/stringify.h> 498d59ecb2SHans Petter Selasky #include <linux/errno.h> 501e3db1deSHans Petter Selasky #include <linux/sched.h> 518d59ecb2SHans Petter Selasky #include <linux/types.h> 524b2cb13eSEmmanuel Vadot #include <linux/typecheck.h> 538d59ecb2SHans Petter Selasky #include <linux/jiffies.h> 548d59ecb2SHans Petter Selasky #include <linux/log2.h> 5535555d47SHans Petter Selasky 568d59ecb2SHans Petter Selasky #include <asm/byteorder.h> 574b2cb13eSEmmanuel Vadot #include <asm/cpufeature.h> 584b2cb13eSEmmanuel Vadot #include <asm/processor.h> 5935555d47SHans Petter Selasky #include <asm/uaccess.h> 608d59ecb2SHans Petter Selasky 61aef2a67bSHans Petter Selasky #include <machine/stdarg.h> 62aef2a67bSHans Petter Selasky 638d59ecb2SHans Petter Selasky #define KERN_CONT "" 648d59ecb2SHans Petter Selasky #define KERN_EMERG "<0>" 658d59ecb2SHans Petter Selasky #define KERN_ALERT "<1>" 668d59ecb2SHans Petter Selasky #define KERN_CRIT "<2>" 678d59ecb2SHans Petter Selasky #define KERN_ERR "<3>" 688d59ecb2SHans Petter Selasky #define KERN_WARNING "<4>" 698d59ecb2SHans Petter Selasky #define KERN_NOTICE "<5>" 708d59ecb2SHans Petter Selasky #define KERN_INFO "<6>" 718d59ecb2SHans Petter Selasky #define KERN_DEBUG "<7>" 728d59ecb2SHans Petter Selasky 73abb14a54SHans Petter Selasky #define U8_MAX ((u8)~0U) 74abb14a54SHans Petter Selasky #define S8_MAX ((s8)(U8_MAX >> 1)) 75abb14a54SHans Petter Selasky #define S8_MIN ((s8)(-S8_MAX - 1)) 76abb14a54SHans Petter Selasky #define U16_MAX ((u16)~0U) 77abb14a54SHans Petter Selasky #define S16_MAX ((s16)(U16_MAX >> 1)) 78abb14a54SHans Petter Selasky #define S16_MIN ((s16)(-S16_MAX - 1)) 79abb14a54SHans Petter Selasky #define U32_MAX ((u32)~0U) 80abb14a54SHans Petter Selasky #define S32_MAX ((s32)(U32_MAX >> 1)) 81abb14a54SHans Petter Selasky #define S32_MIN ((s32)(-S32_MAX - 1)) 82abb14a54SHans Petter Selasky #define U64_MAX ((u64)~0ULL) 83abb14a54SHans Petter Selasky #define S64_MAX ((s64)(U64_MAX >> 1)) 84abb14a54SHans Petter Selasky #define S64_MIN ((s64)(-S64_MAX - 1)) 85abb14a54SHans Petter Selasky 86abb14a54SHans Petter Selasky #define S8_C(x) x 87abb14a54SHans Petter Selasky #define U8_C(x) x ## U 88abb14a54SHans Petter Selasky #define S16_C(x) x 89abb14a54SHans Petter Selasky #define U16_C(x) x ## U 90abb14a54SHans Petter Selasky #define S32_C(x) x 91abb14a54SHans Petter Selasky #define U32_C(x) x ## U 92abb14a54SHans Petter Selasky #define S64_C(x) x ## LL 93abb14a54SHans Petter Selasky #define U64_C(x) x ## ULL 94abb14a54SHans Petter Selasky 951082490cSBjoern A. Zeeb /* 961082490cSBjoern A. Zeeb * BUILD_BUG_ON() can happen inside functions where _Static_assert() does not 971082490cSBjoern A. Zeeb * seem to work. Use old-schoold-ish CTASSERT from before commit 981082490cSBjoern A. Zeeb * a3085588a88fa58eb5b1eaae471999e1995a29cf but also make sure we do not 991082490cSBjoern A. Zeeb * end up with an unused typedef or variable. The compiler should optimise 1001082490cSBjoern A. Zeeb * it away entirely. 1011082490cSBjoern A. Zeeb */ 1021082490cSBjoern A. Zeeb #define _O_CTASSERT(x) _O__CTASSERT(x, __LINE__) 1031082490cSBjoern A. Zeeb #define _O__CTASSERT(x, y) _O___CTASSERT(x, y) 1041082490cSBjoern A. Zeeb #define _O___CTASSERT(x, y) while (0) { \ 1051082490cSBjoern A. Zeeb typedef char __assert_line_ ## y[(x) ? 1 : -1]; \ 1061082490cSBjoern A. Zeeb __assert_line_ ## y _x; \ 1071082490cSBjoern A. Zeeb _x[0] = '\0'; \ 1081082490cSBjoern A. Zeeb } 1091082490cSBjoern A. Zeeb 110e9e4ec11SHans Petter Selasky #define BUILD_BUG() do { CTASSERT(0); } while (0) 1116d15ccdeSAlex Richardson #define BUILD_BUG_ON(x) do { _O_CTASSERT(!(x)) } while (0) 112cb564d24SMark Johnston #define BUILD_BUG_ON_MSG(x, msg) BUILD_BUG_ON(x) 113cb564d24SMark Johnston #define BUILD_BUG_ON_NOT_POWER_OF_2(x) BUILD_BUG_ON(!powerof2(x)) 1142460cbb4SHans Petter Selasky #define BUILD_BUG_ON_INVALID(expr) while (0) { (void)(expr); } 11564dab63fSVladimir Kondratyev #define BUILD_BUG_ON_ZERO(x) ((int)sizeof(struct { int:-((x) != 0); })) 116c185f13bSHans Petter Selasky 117abb14a54SHans Petter Selasky #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 118abb14a54SHans Petter Selasky #define BUG_ON(cond) do { \ 119abb14a54SHans Petter Selasky if (cond) { \ 120abb14a54SHans Petter Selasky panic("BUG ON %s failed at %s:%d", \ 121abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 122abb14a54SHans Petter Selasky } \ 123abb14a54SHans Petter Selasky } while (0) 124abb14a54SHans Petter Selasky 1252390a144SHans Petter Selasky extern int linuxkpi_warn_dump_stack; 126abb14a54SHans Petter Selasky #define WARN_ON(cond) ({ \ 127abb14a54SHans Petter Selasky bool __ret = (cond); \ 128abb14a54SHans Petter Selasky if (__ret) { \ 129abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 130abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1312390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 132a164a32bSAndriy Gapon linux_dump_stack(); \ 133abb14a54SHans Petter Selasky } \ 134abb14a54SHans Petter Selasky unlikely(__ret); \ 135abb14a54SHans Petter Selasky }) 136abb14a54SHans Petter Selasky 137abb14a54SHans Petter Selasky #define WARN_ON_SMP(cond) WARN_ON(cond) 138abb14a54SHans Petter Selasky 139abb14a54SHans Petter Selasky #define WARN_ON_ONCE(cond) ({ \ 140abb14a54SHans Petter Selasky static bool __warn_on_once; \ 141abb14a54SHans Petter Selasky bool __ret = (cond); \ 142abb14a54SHans Petter Selasky if (__ret && !__warn_on_once) { \ 143abb14a54SHans Petter Selasky __warn_on_once = 1; \ 144abb14a54SHans Petter Selasky printf("WARNING %s failed at %s:%d\n", \ 145abb14a54SHans Petter Selasky __stringify(cond), __FILE__, __LINE__); \ 1462390a144SHans Petter Selasky if (linuxkpi_warn_dump_stack) \ 147a164a32bSAndriy Gapon linux_dump_stack(); \ 148abb14a54SHans Petter Selasky } \ 149abb14a54SHans Petter Selasky unlikely(__ret); \ 150abb14a54SHans Petter Selasky }) 1518d59ecb2SHans Petter Selasky 152cb564d24SMark Johnston #define oops_in_progress SCHEDULER_STOPPED() 153cb564d24SMark Johnston 1548d59ecb2SHans Petter Selasky #undef ALIGN 1558d59ecb2SHans Petter Selasky #define ALIGN(x, y) roundup2((x), (y)) 15697f2e93aSBjoern A. Zeeb #define ALIGN_DOWN(x, y) rounddown2(x, y) 1578d59ecb2SHans Petter Selasky #undef PTR_ALIGN 1588d59ecb2SHans Petter Selasky #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 159c4e0746eSJohannes Lundberg #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 1608d59ecb2SHans Petter Selasky #define DIV_ROUND_UP(x, n) howmany(x, n) 161bf05cd05SHans Petter Selasky #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 1628d59ecb2SHans Petter Selasky #define DIV_ROUND_UP_ULL(x, n) DIV_ROUND_UP((unsigned long long)(x), (n)) 163c4e0746eSJohannes Lundberg #define DIV_ROUND_DOWN_ULL(x, n) (((unsigned long long)(x) / (n)) * (n)) 1648d59ecb2SHans Petter Selasky #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 1658d59ecb2SHans Petter Selasky 166aef2a67bSHans Petter Selasky #define printk(...) printf(__VA_ARGS__) 167aef2a67bSHans Petter Selasky #define vprintk(f, a) vprintf(f, a) 168aef2a67bSHans Petter Selasky 1690df8bab6SHans Petter Selasky #define asm __asm 1700df8bab6SHans Petter Selasky 171e35079dbSHans Petter Selasky extern void linux_dump_stack(void); 172e35079dbSHans Petter Selasky #define dump_stack() linux_dump_stack() 173e35079dbSHans Petter Selasky 174aef2a67bSHans Petter Selasky struct va_format { 175aef2a67bSHans Petter Selasky const char *fmt; 176aef2a67bSHans Petter Selasky va_list *va; 177aef2a67bSHans Petter Selasky }; 178aef2a67bSHans Petter Selasky 179aef2a67bSHans Petter Selasky static inline int 180aef2a67bSHans Petter Selasky vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 181aef2a67bSHans Petter Selasky { 182aef2a67bSHans Petter Selasky ssize_t ssize = size; 183aef2a67bSHans Petter Selasky int i; 184aef2a67bSHans Petter Selasky 185aef2a67bSHans Petter Selasky i = vsnprintf(buf, size, fmt, args); 186aef2a67bSHans Petter Selasky 187aef2a67bSHans Petter Selasky return ((i >= ssize) ? (ssize - 1) : i); 188aef2a67bSHans Petter Selasky } 189aef2a67bSHans Petter Selasky 190aef2a67bSHans Petter Selasky static inline int 191aef2a67bSHans Petter Selasky scnprintf(char *buf, size_t size, const char *fmt, ...) 192aef2a67bSHans Petter Selasky { 193aef2a67bSHans Petter Selasky va_list args; 194aef2a67bSHans Petter Selasky int i; 195aef2a67bSHans Petter Selasky 196aef2a67bSHans Petter Selasky va_start(args, fmt); 197aef2a67bSHans Petter Selasky i = vscnprintf(buf, size, fmt, args); 198aef2a67bSHans Petter Selasky va_end(args); 199aef2a67bSHans Petter Selasky 200aef2a67bSHans Petter Selasky return (i); 201aef2a67bSHans Petter Selasky } 2028d59ecb2SHans Petter Selasky 2038d59ecb2SHans Petter Selasky /* 2048d59ecb2SHans Petter Selasky * The "pr_debug()" and "pr_devel()" macros should produce zero code 2058d59ecb2SHans Petter Selasky * unless DEBUG is defined: 2068d59ecb2SHans Petter Selasky */ 2078d59ecb2SHans Petter Selasky #ifdef DEBUG 2084580f5eaSHans Petter Selasky extern int linuxkpi_debug; 2098d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 2104580f5eaSHans Petter Selasky do { \ 2114580f5eaSHans Petter Selasky if (linuxkpi_debug) \ 2124580f5eaSHans Petter Selasky log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 2134580f5eaSHans Petter Selasky } while (0) 2148d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 2158d59ecb2SHans Petter Selasky log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 2168d59ecb2SHans Petter Selasky #else 2178d59ecb2SHans Petter Selasky #define pr_debug(fmt, ...) \ 2188d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 2198d59ecb2SHans Petter Selasky #define pr_devel(fmt, ...) \ 2208d59ecb2SHans Petter Selasky ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 2218d59ecb2SHans Petter Selasky #endif 2228d59ecb2SHans Petter Selasky 2238d59ecb2SHans Petter Selasky #ifndef pr_fmt 2248d59ecb2SHans Petter Selasky #define pr_fmt(fmt) fmt 2258d59ecb2SHans Petter Selasky #endif 2268d59ecb2SHans Petter Selasky 2278d59ecb2SHans Petter Selasky /* 2288d59ecb2SHans Petter Selasky * Print a one-time message (analogous to WARN_ONCE() et al): 2298d59ecb2SHans Petter Selasky */ 2308d59ecb2SHans Petter Selasky #define printk_once(...) do { \ 2318d59ecb2SHans Petter Selasky static bool __print_once; \ 2328d59ecb2SHans Petter Selasky \ 2338d59ecb2SHans Petter Selasky if (!__print_once) { \ 2348d59ecb2SHans Petter Selasky __print_once = true; \ 2358d59ecb2SHans Petter Selasky printk(__VA_ARGS__); \ 2368d59ecb2SHans Petter Selasky } \ 2378d59ecb2SHans Petter Selasky } while (0) 2388d59ecb2SHans Petter Selasky 2398d59ecb2SHans Petter Selasky /* 2408d59ecb2SHans Petter Selasky * Log a one-time message (analogous to WARN_ONCE() et al): 2418d59ecb2SHans Petter Selasky */ 2428d59ecb2SHans Petter Selasky #define log_once(level,...) do { \ 2438d59ecb2SHans Petter Selasky static bool __log_once; \ 2448d59ecb2SHans Petter Selasky \ 245abb14a54SHans Petter Selasky if (unlikely(!__log_once)) { \ 2468d59ecb2SHans Petter Selasky __log_once = true; \ 2478d59ecb2SHans Petter Selasky log(level, __VA_ARGS__); \ 2488d59ecb2SHans Petter Selasky } \ 2498d59ecb2SHans Petter Selasky } while (0) 2508d59ecb2SHans Petter Selasky 2518d59ecb2SHans Petter Selasky #define pr_emerg(fmt, ...) \ 2528d59ecb2SHans Petter Selasky log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 2538d59ecb2SHans Petter Selasky #define pr_alert(fmt, ...) \ 2548d59ecb2SHans Petter Selasky log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 2558d59ecb2SHans Petter Selasky #define pr_crit(fmt, ...) \ 2568d59ecb2SHans Petter Selasky log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 2578d59ecb2SHans Petter Selasky #define pr_err(fmt, ...) \ 2588d59ecb2SHans Petter Selasky log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 25905c2d94aSGreg V #define pr_err_once(fmt, ...) \ 26005c2d94aSGreg V log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 2618d59ecb2SHans Petter Selasky #define pr_warning(fmt, ...) \ 2628d59ecb2SHans Petter Selasky log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 263abb14a54SHans Petter Selasky #define pr_warn(...) \ 264abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__) 265abb14a54SHans Petter Selasky #define pr_warn_once(fmt, ...) \ 266abb14a54SHans Petter Selasky log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 2678d59ecb2SHans Petter Selasky #define pr_notice(fmt, ...) \ 2688d59ecb2SHans Petter Selasky log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 2698d59ecb2SHans Petter Selasky #define pr_info(fmt, ...) \ 2708d59ecb2SHans Petter Selasky log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2718d59ecb2SHans Petter Selasky #define pr_info_once(fmt, ...) \ 2728d59ecb2SHans Petter Selasky log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 2738d59ecb2SHans Petter Selasky #define pr_cont(fmt, ...) \ 2748d59ecb2SHans Petter Selasky printk(KERN_CONT fmt, ##__VA_ARGS__) 275e9db3df2SHans Petter Selasky #define pr_warn_ratelimited(...) do { \ 2768f7eee5aSHans Petter Selasky static linux_ratelimit_t __ratelimited; \ 277e9db3df2SHans Petter Selasky if (linux_ratelimited(&__ratelimited)) \ 278e9db3df2SHans Petter Selasky pr_warning(__VA_ARGS__); \ 279e9db3df2SHans Petter Selasky } while (0) 2808d59ecb2SHans Petter Selasky 2818d59ecb2SHans Petter Selasky #ifndef WARN 282abb14a54SHans Petter Selasky #define WARN(condition, ...) ({ \ 283abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 2848d59ecb2SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 285abb14a54SHans Petter Selasky pr_warning(__VA_ARGS__); \ 286abb14a54SHans Petter Selasky unlikely(__ret_warn_on); \ 287abb14a54SHans Petter Selasky }) 288abb14a54SHans Petter Selasky #endif 289abb14a54SHans Petter Selasky 290abb14a54SHans Petter Selasky #ifndef WARN_ONCE 291abb14a54SHans Petter Selasky #define WARN_ONCE(condition, ...) ({ \ 292abb14a54SHans Petter Selasky bool __ret_warn_on = (condition); \ 293abb14a54SHans Petter Selasky if (unlikely(__ret_warn_on)) \ 294abb14a54SHans Petter Selasky pr_warn_once(__VA_ARGS__); \ 2958d59ecb2SHans Petter Selasky unlikely(__ret_warn_on); \ 2968d59ecb2SHans Petter Selasky }) 2978d59ecb2SHans Petter Selasky #endif 2988d59ecb2SHans Petter Selasky 2998d59ecb2SHans Petter Selasky #define container_of(ptr, type, member) \ 3008d59ecb2SHans Petter Selasky ({ \ 301ddad2785SHans Petter Selasky const __typeof(((type *)0)->member) *__p = (ptr); \ 302ddad2785SHans Petter Selasky (type *)((uintptr_t)__p - offsetof(type, member)); \ 3038d59ecb2SHans Petter Selasky }) 3048d59ecb2SHans Petter Selasky 3058d59ecb2SHans Petter Selasky #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 3068d59ecb2SHans Petter Selasky 307ee7c3198SMark Johnston #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 308ee7c3198SMark Johnston 309fee0d486SNeel Chauhan #define _RET_IP_ __builtin_return_address(0) 310fee0d486SNeel Chauhan 311d2f312e0SHans Petter Selasky static inline unsigned long long 312d2f312e0SHans Petter Selasky simple_strtoull(const char *cp, char **endp, unsigned int base) 313d2f312e0SHans Petter Selasky { 314d2f312e0SHans Petter Selasky return (strtouq(cp, endp, base)); 315d2f312e0SHans Petter Selasky } 316d2f312e0SHans Petter Selasky 317d2f312e0SHans Petter Selasky static inline long long 318d2f312e0SHans Petter Selasky simple_strtoll(const char *cp, char **endp, unsigned int base) 319d2f312e0SHans Petter Selasky { 320d2f312e0SHans Petter Selasky return (strtoq(cp, endp, base)); 321d2f312e0SHans Petter Selasky } 322d2f312e0SHans Petter Selasky 323d2f312e0SHans Petter Selasky static inline unsigned long 324d2f312e0SHans Petter Selasky simple_strtoul(const char *cp, char **endp, unsigned int base) 325d2f312e0SHans Petter Selasky { 326d2f312e0SHans Petter Selasky return (strtoul(cp, endp, base)); 327d2f312e0SHans Petter Selasky } 328d2f312e0SHans Petter Selasky 329d2f312e0SHans Petter Selasky static inline long 330d2f312e0SHans Petter Selasky simple_strtol(const char *cp, char **endp, unsigned int base) 331d2f312e0SHans Petter Selasky { 332d2f312e0SHans Petter Selasky return (strtol(cp, endp, base)); 333d2f312e0SHans Petter Selasky } 334d2f312e0SHans Petter Selasky 335d2f312e0SHans Petter Selasky static inline int 336d2f312e0SHans Petter Selasky kstrtoul(const char *cp, unsigned int base, unsigned long *res) 337d2f312e0SHans Petter Selasky { 338d2f312e0SHans Petter Selasky char *end; 339d2f312e0SHans Petter Selasky 340d2f312e0SHans Petter Selasky *res = strtoul(cp, &end, base); 341d2f312e0SHans Petter Selasky 342cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 343cbfc3c73SHans Petter Selasky if (*end == '\n') 344cbfc3c73SHans Petter Selasky end++; 345d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 346d2f312e0SHans Petter Selasky return (-EINVAL); 347d2f312e0SHans Petter Selasky return (0); 348d2f312e0SHans Petter Selasky } 349d2f312e0SHans Petter Selasky 350d2f312e0SHans Petter Selasky static inline int 351d2f312e0SHans Petter Selasky kstrtol(const char *cp, unsigned int base, long *res) 352d2f312e0SHans Petter Selasky { 353d2f312e0SHans Petter Selasky char *end; 354d2f312e0SHans Petter Selasky 355d2f312e0SHans Petter Selasky *res = strtol(cp, &end, base); 356d2f312e0SHans Petter Selasky 357cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 358cbfc3c73SHans Petter Selasky if (*end == '\n') 359cbfc3c73SHans Petter Selasky end++; 360d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 361d2f312e0SHans Petter Selasky return (-EINVAL); 362d2f312e0SHans Petter Selasky return (0); 363d2f312e0SHans Petter Selasky } 364d2f312e0SHans Petter Selasky 365d2f312e0SHans Petter Selasky static inline int 366d2f312e0SHans Petter Selasky kstrtoint(const char *cp, unsigned int base, int *res) 367d2f312e0SHans Petter Selasky { 368d2f312e0SHans Petter Selasky char *end; 369d2f312e0SHans Petter Selasky long temp; 370d2f312e0SHans Petter Selasky 371d2f312e0SHans Petter Selasky *res = temp = strtol(cp, &end, base); 372d2f312e0SHans Petter Selasky 373cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 374cbfc3c73SHans Petter Selasky if (*end == '\n') 375cbfc3c73SHans Petter Selasky end++; 376d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 377d2f312e0SHans Petter Selasky return (-EINVAL); 378d2f312e0SHans Petter Selasky if (temp != (int)temp) 379d2f312e0SHans Petter Selasky return (-ERANGE); 380d2f312e0SHans Petter Selasky return (0); 381d2f312e0SHans Petter Selasky } 382d2f312e0SHans Petter Selasky 383d2f312e0SHans Petter Selasky static inline int 384d2f312e0SHans Petter Selasky kstrtouint(const char *cp, unsigned int base, unsigned int *res) 385d2f312e0SHans Petter Selasky { 386d2f312e0SHans Petter Selasky char *end; 387d2f312e0SHans Petter Selasky unsigned long temp; 388d2f312e0SHans Petter Selasky 389d2f312e0SHans Petter Selasky *res = temp = strtoul(cp, &end, base); 390d2f312e0SHans Petter Selasky 391cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 392cbfc3c73SHans Petter Selasky if (*end == '\n') 393cbfc3c73SHans Petter Selasky end++; 394d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 395d2f312e0SHans Petter Selasky return (-EINVAL); 396d2f312e0SHans Petter Selasky if (temp != (unsigned int)temp) 397d2f312e0SHans Petter Selasky return (-ERANGE); 398d2f312e0SHans Petter Selasky return (0); 399d2f312e0SHans Petter Selasky } 400d2f312e0SHans Petter Selasky 401d2f312e0SHans Petter Selasky static inline int 402b382b785SBjoern A. Zeeb kstrtou8(const char *cp, unsigned int base, u8 *res) 403b382b785SBjoern A. Zeeb { 404b382b785SBjoern A. Zeeb char *end; 405b382b785SBjoern A. Zeeb unsigned long temp; 406b382b785SBjoern A. Zeeb 407b382b785SBjoern A. Zeeb *res = temp = strtoul(cp, &end, base); 408b382b785SBjoern A. Zeeb 409b382b785SBjoern A. Zeeb /* skip newline character, if any */ 410b382b785SBjoern A. Zeeb if (*end == '\n') 411b382b785SBjoern A. Zeeb end++; 412b382b785SBjoern A. Zeeb if (*cp == 0 || *end != 0) 413b382b785SBjoern A. Zeeb return (-EINVAL); 414b382b785SBjoern A. Zeeb if (temp != (u8)temp) 415b382b785SBjoern A. Zeeb return (-ERANGE); 416b382b785SBjoern A. Zeeb return (0); 417b382b785SBjoern A. Zeeb } 418b382b785SBjoern A. Zeeb 419b382b785SBjoern A. Zeeb static inline int 4208e52f22bSEmmanuel Vadot kstrtou16(const char *cp, unsigned int base, u16 *res) 4218e52f22bSEmmanuel Vadot { 4228e52f22bSEmmanuel Vadot char *end; 4238e52f22bSEmmanuel Vadot unsigned long temp; 4248e52f22bSEmmanuel Vadot 4258e52f22bSEmmanuel Vadot *res = temp = strtoul(cp, &end, base); 4268e52f22bSEmmanuel Vadot 4278e52f22bSEmmanuel Vadot /* skip newline character, if any */ 4288e52f22bSEmmanuel Vadot if (*end == '\n') 4298e52f22bSEmmanuel Vadot end++; 4308e52f22bSEmmanuel Vadot if (*cp == 0 || *end != 0) 4318e52f22bSEmmanuel Vadot return (-EINVAL); 4328e52f22bSEmmanuel Vadot if (temp != (u16)temp) 4338e52f22bSEmmanuel Vadot return (-ERANGE); 4348e52f22bSEmmanuel Vadot return (0); 4358e52f22bSEmmanuel Vadot } 4368e52f22bSEmmanuel Vadot 4378e52f22bSEmmanuel Vadot static inline int 438d2f312e0SHans Petter Selasky kstrtou32(const char *cp, unsigned int base, u32 *res) 439d2f312e0SHans Petter Selasky { 440d2f312e0SHans Petter Selasky char *end; 441d2f312e0SHans Petter Selasky unsigned long temp; 442d2f312e0SHans Petter Selasky 443d2f312e0SHans Petter Selasky *res = temp = strtoul(cp, &end, base); 444d2f312e0SHans Petter Selasky 445cbfc3c73SHans Petter Selasky /* skip newline character, if any */ 446cbfc3c73SHans Petter Selasky if (*end == '\n') 447cbfc3c73SHans Petter Selasky end++; 448d2f312e0SHans Petter Selasky if (*cp == 0 || *end != 0) 449d2f312e0SHans Petter Selasky return (-EINVAL); 450d2f312e0SHans Petter Selasky if (temp != (u32)temp) 451d2f312e0SHans Petter Selasky return (-ERANGE); 452d2f312e0SHans Petter Selasky return (0); 453d2f312e0SHans Petter Selasky } 4548d59ecb2SHans Petter Selasky 45535555d47SHans Petter Selasky static inline int 45661f7fe6bSHans Petter Selasky kstrtou64(const char *cp, unsigned int base, u64 *res) 45761f7fe6bSHans Petter Selasky { 45861f7fe6bSHans Petter Selasky char *end; 45961f7fe6bSHans Petter Selasky 46061f7fe6bSHans Petter Selasky *res = strtouq(cp, &end, base); 46161f7fe6bSHans Petter Selasky 46261f7fe6bSHans Petter Selasky /* skip newline character, if any */ 46361f7fe6bSHans Petter Selasky if (*end == '\n') 46461f7fe6bSHans Petter Selasky end++; 46561f7fe6bSHans Petter Selasky if (*cp == 0 || *end != 0) 46661f7fe6bSHans Petter Selasky return (-EINVAL); 46761f7fe6bSHans Petter Selasky return (0); 46861f7fe6bSHans Petter Selasky } 46961f7fe6bSHans Petter Selasky 47061f7fe6bSHans Petter Selasky static inline int 471c427456fSVladimir Kondratyev kstrtoull(const char *cp, unsigned int base, unsigned long long *res) 472c427456fSVladimir Kondratyev { 473c427456fSVladimir Kondratyev return (kstrtou64(cp, base, (u64 *)res)); 474c427456fSVladimir Kondratyev } 475c427456fSVladimir Kondratyev 476c427456fSVladimir Kondratyev static inline int 47735555d47SHans Petter Selasky kstrtobool(const char *s, bool *res) 47835555d47SHans Petter Selasky { 47935555d47SHans Petter Selasky int len; 48035555d47SHans Petter Selasky 48135555d47SHans Petter Selasky if (s == NULL || (len = strlen(s)) == 0 || res == NULL) 48235555d47SHans Petter Selasky return (-EINVAL); 48335555d47SHans Petter Selasky 48435555d47SHans Petter Selasky /* skip newline character, if any */ 48535555d47SHans Petter Selasky if (s[len - 1] == '\n') 48635555d47SHans Petter Selasky len--; 48735555d47SHans Petter Selasky 48835555d47SHans Petter Selasky if (len == 1 && strchr("yY1", s[0]) != NULL) 48935555d47SHans Petter Selasky *res = true; 49035555d47SHans Petter Selasky else if (len == 1 && strchr("nN0", s[0]) != NULL) 49135555d47SHans Petter Selasky *res = false; 49235555d47SHans Petter Selasky else if (strncasecmp("on", s, len) == 0) 49335555d47SHans Petter Selasky *res = true; 49435555d47SHans Petter Selasky else if (strncasecmp("off", s, len) == 0) 49535555d47SHans Petter Selasky *res = false; 49635555d47SHans Petter Selasky else 49735555d47SHans Petter Selasky return (-EINVAL); 49835555d47SHans Petter Selasky 49935555d47SHans Petter Selasky return (0); 50035555d47SHans Petter Selasky } 50135555d47SHans Petter Selasky 50235555d47SHans Petter Selasky static inline int 50335555d47SHans Petter Selasky kstrtobool_from_user(const char __user *s, size_t count, bool *res) 50435555d47SHans Petter Selasky { 50535555d47SHans Petter Selasky char buf[8] = {}; 50635555d47SHans Petter Selasky 50735555d47SHans Petter Selasky if (count > (sizeof(buf) - 1)) 50835555d47SHans Petter Selasky count = (sizeof(buf) - 1); 50935555d47SHans Petter Selasky 51035555d47SHans Petter Selasky if (copy_from_user(buf, s, count)) 51135555d47SHans Petter Selasky return (-EFAULT); 51235555d47SHans Petter Selasky 51335555d47SHans Petter Selasky return (kstrtobool(buf, res)); 51435555d47SHans Petter Selasky } 51535555d47SHans Petter Selasky 516b382b785SBjoern A. Zeeb static inline int 517c840d5ceSBjoern A. Zeeb kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, 518c840d5ceSBjoern A. Zeeb int *p) 519c840d5ceSBjoern A. Zeeb { 520c840d5ceSBjoern A. Zeeb char buf[36] = {}; 521c840d5ceSBjoern A. Zeeb 522c840d5ceSBjoern A. Zeeb if (count > (sizeof(buf) - 1)) 523c840d5ceSBjoern A. Zeeb count = (sizeof(buf) - 1); 524c840d5ceSBjoern A. Zeeb 525c840d5ceSBjoern A. Zeeb if (copy_from_user(buf, s, count)) 526c840d5ceSBjoern A. Zeeb return (-EFAULT); 527c840d5ceSBjoern A. Zeeb 528c840d5ceSBjoern A. Zeeb return (kstrtoint(buf, base, p)); 529c840d5ceSBjoern A. Zeeb } 530c840d5ceSBjoern A. Zeeb 531c840d5ceSBjoern A. Zeeb static inline int 5328e587a5fSEmmanuel Vadot kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, 5338e587a5fSEmmanuel Vadot int *p) 5348e587a5fSEmmanuel Vadot { 5358e587a5fSEmmanuel Vadot char buf[36] = {}; 5368e587a5fSEmmanuel Vadot 5378e587a5fSEmmanuel Vadot if (count > (sizeof(buf) - 1)) 5388e587a5fSEmmanuel Vadot count = (sizeof(buf) - 1); 5398e587a5fSEmmanuel Vadot 5408e587a5fSEmmanuel Vadot if (copy_from_user(buf, s, count)) 5418e587a5fSEmmanuel Vadot return (-EFAULT); 5428e587a5fSEmmanuel Vadot 5438e587a5fSEmmanuel Vadot return (kstrtouint(buf, base, p)); 5448e587a5fSEmmanuel Vadot } 5458e587a5fSEmmanuel Vadot 5468e587a5fSEmmanuel Vadot static inline int 547b382b785SBjoern A. Zeeb kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, 548b382b785SBjoern A. Zeeb u8 *p) 549b382b785SBjoern A. Zeeb { 550b382b785SBjoern A. Zeeb char buf[8] = {}; 551b382b785SBjoern A. Zeeb 552b382b785SBjoern A. Zeeb if (count > (sizeof(buf) - 1)) 553b382b785SBjoern A. Zeeb count = (sizeof(buf) - 1); 554b382b785SBjoern A. Zeeb 555b382b785SBjoern A. Zeeb if (copy_from_user(buf, s, count)) 556b382b785SBjoern A. Zeeb return (-EFAULT); 557b382b785SBjoern A. Zeeb 558b382b785SBjoern A. Zeeb return (kstrtou8(buf, base, p)); 559b382b785SBjoern A. Zeeb } 560b382b785SBjoern A. Zeeb 5618d59ecb2SHans Petter Selasky #define min(x, y) ((x) < (y) ? (x) : (y)) 5628d59ecb2SHans Petter Selasky #define max(x, y) ((x) > (y) ? (x) : (y)) 5638d59ecb2SHans Petter Selasky 5648d59ecb2SHans Petter Selasky #define min3(a, b, c) min(a, min(b,c)) 5658d59ecb2SHans Petter Selasky #define max3(a, b, c) max(a, max(b,c)) 5668d59ecb2SHans Petter Selasky 567abb14a54SHans Petter Selasky #define min_t(type, x, y) ({ \ 568abb14a54SHans Petter Selasky type __min1 = (x); \ 569abb14a54SHans Petter Selasky type __min2 = (y); \ 570abb14a54SHans Petter Selasky __min1 < __min2 ? __min1 : __min2; }) 571abb14a54SHans Petter Selasky 572abb14a54SHans Petter Selasky #define max_t(type, x, y) ({ \ 573abb14a54SHans Petter Selasky type __max1 = (x); \ 574abb14a54SHans Petter Selasky type __max2 = (y); \ 575abb14a54SHans Petter Selasky __max1 > __max2 ? __max1 : __max2; }) 5768d59ecb2SHans Petter Selasky 577355711eaSEmmanuel Vadot #define offsetofend(t, m) \ 578355711eaSEmmanuel Vadot (offsetof(t, m) + sizeof((((t *)0)->m))) 579355711eaSEmmanuel Vadot 5808d59ecb2SHans Petter Selasky #define clamp_t(type, _x, min, max) min_t(type, max_t(type, _x, min), max) 5818d59ecb2SHans Petter Selasky #define clamp(x, lo, hi) min( max(x,lo), hi) 582abb14a54SHans Petter Selasky #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi) 5838d59ecb2SHans Petter Selasky 5848d59ecb2SHans Petter Selasky /* 5858d59ecb2SHans Petter Selasky * This looks more complex than it should be. But we need to 5868d59ecb2SHans Petter Selasky * get the type for the ~ right in round_down (it needs to be 5878d59ecb2SHans Petter Selasky * as wide as the result!), and we want to evaluate the macro 5888d59ecb2SHans Petter Selasky * arguments just once each. 5898d59ecb2SHans Petter Selasky */ 5908d59ecb2SHans Petter Selasky #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 5918d59ecb2SHans Petter Selasky #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 5928d59ecb2SHans Petter Selasky #define round_down(x, y) ((x) & ~__round_mask(x, y)) 5938d59ecb2SHans Petter Selasky 594fe68f570SHans Petter Selasky #define smp_processor_id() PCPU_GET(cpuid) 5958d59ecb2SHans Petter Selasky #define num_possible_cpus() mp_ncpus 5968d59ecb2SHans Petter Selasky #define num_online_cpus() mp_ncpus 59703219fbaSHans Petter Selasky 59803219fbaSHans Petter Selasky #if defined(__i386__) || defined(__amd64__) 59903219fbaSHans Petter Selasky extern bool linux_cpu_has_clflush; 60003219fbaSHans Petter Selasky #define cpu_has_clflush linux_cpu_has_clflush 60103219fbaSHans Petter Selasky #endif 6028d59ecb2SHans Petter Selasky 6038d59ecb2SHans Petter Selasky /* Swap values of a and b */ 6048d59ecb2SHans Petter Selasky #define swap(a, b) do { \ 6058d59ecb2SHans Petter Selasky typeof(a) _swap_tmp = a; \ 6068d59ecb2SHans Petter Selasky a = b; \ 6078d59ecb2SHans Petter Selasky b = _swap_tmp; \ 6088d59ecb2SHans Petter Selasky } while (0) 6098d59ecb2SHans Petter Selasky 6108d59ecb2SHans Petter Selasky #define DIV_ROUND_CLOSEST(x, divisor) (((x) + ((divisor) / 2)) / (divisor)) 6118d59ecb2SHans Petter Selasky 612abb14a54SHans Petter Selasky #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({ \ 613abb14a54SHans Petter Selasky __typeof(divisor) __d = (divisor); \ 614abb14a54SHans Petter Selasky unsigned long long __ret = (x) + (__d) / 2; \ 615abb14a54SHans Petter Selasky __ret /= __d; \ 616abb14a54SHans Petter Selasky __ret; \ 617abb14a54SHans Petter Selasky }) 618abb14a54SHans Petter Selasky 6198d59ecb2SHans Petter Selasky static inline uintmax_t 6208d59ecb2SHans Petter Selasky mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor) 6218d59ecb2SHans Petter Selasky { 6228d59ecb2SHans Petter Selasky uintmax_t q = (x / divisor); 6238d59ecb2SHans Petter Selasky uintmax_t r = (x % divisor); 6248d59ecb2SHans Petter Selasky 6258d59ecb2SHans Petter Selasky return ((q * multiplier) + ((r * multiplier) / divisor)); 6268d59ecb2SHans Petter Selasky } 6278d59ecb2SHans Petter Selasky 6288d59ecb2SHans Petter Selasky static inline int64_t 6298d59ecb2SHans Petter Selasky abs64(int64_t x) 6308d59ecb2SHans Petter Selasky { 6318d59ecb2SHans Petter Selasky return (x < 0 ? -x : x); 6328d59ecb2SHans Petter Selasky } 6338d59ecb2SHans Petter Selasky 6348f7eee5aSHans Petter Selasky typedef struct linux_ratelimit { 6358f7eee5aSHans Petter Selasky struct timeval lasttime; 6368f7eee5aSHans Petter Selasky int counter; 6378f7eee5aSHans Petter Selasky } linux_ratelimit_t; 6388f7eee5aSHans Petter Selasky 6398f7eee5aSHans Petter Selasky static inline bool 6408f7eee5aSHans Petter Selasky linux_ratelimited(linux_ratelimit_t *rl) 6418f7eee5aSHans Petter Selasky { 6428f7eee5aSHans Petter Selasky return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 6438f7eee5aSHans Petter Selasky } 644e9db3df2SHans Petter Selasky 645ef5f8c18SHans Petter Selasky #define struct_size(ptr, field, num) ({ \ 646ef5f8c18SHans Petter Selasky const size_t __size = offsetof(__typeof(*(ptr)), field); \ 647ef5f8c18SHans Petter Selasky const size_t __max = (SIZE_MAX - __size) / sizeof((ptr)->field[0]); \ 648ef5f8c18SHans Petter Selasky ((num) > __max) ? SIZE_MAX : (__size + sizeof((ptr)->field[0]) * (num)); \ 649ef5f8c18SHans Petter Selasky }) 650ef5f8c18SHans Petter Selasky 651d053391cSHans Petter Selasky #define __is_constexpr(x) \ 652d053391cSHans Petter Selasky __builtin_constant_p(x) 653d053391cSHans Petter Selasky 654d326a6c7SHans Petter Selasky /* 655d326a6c7SHans Petter Selasky * The is_signed() macro below returns true if the passed data type is 656d326a6c7SHans Petter Selasky * signed. Else false is returned. 657d326a6c7SHans Petter Selasky */ 658d326a6c7SHans Petter Selasky #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 659d0eed838SHans Petter Selasky 660d326a6c7SHans Petter Selasky /* 661d326a6c7SHans Petter Selasky * The type_max() macro below returns the maxium positive value the 662d326a6c7SHans Petter Selasky * passed data type can hold. 663d326a6c7SHans Petter Selasky */ 664d326a6c7SHans Petter Selasky #define type_max(datatype) ( \ 665d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MAX : UINT64_MAX) : \ 666d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MAX : UINT32_MAX) : \ 667d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MAX : UINT16_MAX) : \ 668d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MAX : UINT8_MAX) \ 669d0eed838SHans Petter Selasky ) 670d0eed838SHans Petter Selasky 671d326a6c7SHans Petter Selasky /* 672d326a6c7SHans Petter Selasky * The type_min() macro below returns the minimum value the passed 673d326a6c7SHans Petter Selasky * data type can hold. For unsigned types the minimum value is always 674d326a6c7SHans Petter Selasky * zero. For signed types it may vary. 675d326a6c7SHans Petter Selasky */ 676d326a6c7SHans Petter Selasky #define type_min(datatype) ( \ 677d326a6c7SHans Petter Selasky (sizeof(datatype) >= 8) ? (is_signed(datatype) ? INT64_MIN : 0) : \ 678d326a6c7SHans Petter Selasky (sizeof(datatype) >= 4) ? (is_signed(datatype) ? INT32_MIN : 0) : \ 679d326a6c7SHans Petter Selasky (sizeof(datatype) >= 2) ? (is_signed(datatype) ? INT16_MIN : 0) : \ 680d326a6c7SHans Petter Selasky (is_signed(datatype) ? INT8_MIN : 0) \ 681d0eed838SHans Petter Selasky ) 682d0eed838SHans Petter Selasky 6830e4e9e8fSEmmanuel Vadot #define TAINT_WARN 0 6840e4e9e8fSEmmanuel Vadot #define test_taint(x) (0) 685*bf27839aSEmmanuel Vadot #define add_taint(x,y) do { \ 686*bf27839aSEmmanuel Vadot } while (0) 6870e4e9e8fSEmmanuel Vadot 688deb9bfbdSBjoern A. Zeeb static inline int 689deb9bfbdSBjoern A. Zeeb _h2b(const char c) 690deb9bfbdSBjoern A. Zeeb { 691deb9bfbdSBjoern A. Zeeb 692deb9bfbdSBjoern A. Zeeb if (c >= '0' && c <= '9') 693deb9bfbdSBjoern A. Zeeb return (c - '0'); 694deb9bfbdSBjoern A. Zeeb if (c >= 'a' && c <= 'f') 695deb9bfbdSBjoern A. Zeeb return (10 + c - 'a'); 696deb9bfbdSBjoern A. Zeeb if (c >= 'A' && c <= 'F') 697deb9bfbdSBjoern A. Zeeb return (10 + c - 'A'); 698deb9bfbdSBjoern A. Zeeb return (-EINVAL); 699deb9bfbdSBjoern A. Zeeb } 700deb9bfbdSBjoern A. Zeeb 701deb9bfbdSBjoern A. Zeeb static inline int 702deb9bfbdSBjoern A. Zeeb hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 703deb9bfbdSBjoern A. Zeeb { 704deb9bfbdSBjoern A. Zeeb int hi4, lo4; 705deb9bfbdSBjoern A. Zeeb 706deb9bfbdSBjoern A. Zeeb while (binlen > 0) { 707deb9bfbdSBjoern A. Zeeb hi4 = _h2b(*hexsrc++); 708deb9bfbdSBjoern A. Zeeb lo4 = _h2b(*hexsrc++); 709deb9bfbdSBjoern A. Zeeb if (hi4 < 0 || lo4 < 0) 710deb9bfbdSBjoern A. Zeeb return (-EINVAL); 711deb9bfbdSBjoern A. Zeeb 712deb9bfbdSBjoern A. Zeeb *bindst++ = (hi4 << 4) | lo4; 713deb9bfbdSBjoern A. Zeeb binlen--; 714deb9bfbdSBjoern A. Zeeb } 715deb9bfbdSBjoern A. Zeeb 716deb9bfbdSBjoern A. Zeeb return (0); 717deb9bfbdSBjoern A. Zeeb } 718deb9bfbdSBjoern A. Zeeb 719c840d5ceSBjoern A. Zeeb #define DECLARE_FLEX_ARRAY(_t, _n) \ 720fab601c5SBjoern A. Zeeb struct { struct { } __dummy_ ## _n; _t _n[0]; } 721c840d5ceSBjoern A. Zeeb 7224abbf816SBjoern A. Zeeb /* 7234abbf816SBjoern A. Zeeb * Checking if an option is defined would be easy if we could do CPP inside CPP. 7244abbf816SBjoern A. Zeeb * The defined case whether -Dxxx or -Dxxx=1 are easy to deal with. In either 7254abbf816SBjoern A. Zeeb * case the defined value is "1". A more general -Dxxx=<c> case will require 7264abbf816SBjoern A. Zeeb * more effort to deal with all possible "true" values. Hope we do not have 7274abbf816SBjoern A. Zeeb * to do this as well. 7284abbf816SBjoern A. Zeeb * The real problem is the undefined case. To avoid this problem we do the 7294abbf816SBjoern A. Zeeb * concat/varargs trick: "yyy" ## xxx can make two arguments if xxx is "1" 7304abbf816SBjoern A. Zeeb * by having a #define for yyy_1 which is "ignore,". 7314abbf816SBjoern A. Zeeb * Otherwise we will just get "yyy". 7324abbf816SBjoern A. Zeeb * Need to be careful about variable substitutions in macros though. 7334abbf816SBjoern A. Zeeb * This way we make a (true, false) problem a (don't care, true, false) or a 7344abbf816SBjoern A. Zeeb * (don't care true, false). Then we can use a variadic macro to only select 7354abbf816SBjoern A. Zeeb * the always well known and defined argument #2. And that seems to be 7364abbf816SBjoern A. Zeeb * exactly what we need. Use 1 for true and 0 for false to also allow 7374abbf816SBjoern A. Zeeb * #if IS_*() checks pre-compiler checks which do not like #if true. 7384abbf816SBjoern A. Zeeb */ 7394abbf816SBjoern A. Zeeb #define ___XAB_1 dontcare, 7404abbf816SBjoern A. Zeeb #define ___IS_XAB(_ignore, _x, ...) (_x) 7414abbf816SBjoern A. Zeeb #define __IS_XAB(_x) ___IS_XAB(_x 1, 0) 7424abbf816SBjoern A. Zeeb #define _IS_XAB(_x) __IS_XAB(__CONCAT(___XAB_, _x)) 7434abbf816SBjoern A. Zeeb 7444abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc=y. */ 7454abbf816SBjoern A. Zeeb #define IS_BUILTIN(_x) _IS_XAB(_x) 7464abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc=m. */ 7474abbf816SBjoern A. Zeeb #define IS_MODULE(_x) _IS_XAB(_x ## _MODULE) 7484abbf816SBjoern A. Zeeb /* This is if CONFIG_ccc is compiled in(=y) or a module(=m). */ 7494abbf816SBjoern A. Zeeb #define IS_ENABLED(_x) (IS_BUILTIN(_x) || IS_MODULE(_x)) 7504abbf816SBjoern A. Zeeb /* 7514abbf816SBjoern A. Zeeb * This is weird case. If the CONFIG_ccc is builtin (=y) this returns true; 7524abbf816SBjoern A. Zeeb * or if the CONFIG_ccc is a module (=m) and the caller is built as a module 7534abbf816SBjoern A. Zeeb * (-DMODULE defined) this returns true, but if the callers is not a module 7544abbf816SBjoern A. Zeeb * (-DMODULE not defined, which means caller is BUILTIN) then it returns 7554abbf816SBjoern A. Zeeb * false. In other words, a module can reach the kernel, a module can reach 7564abbf816SBjoern A. Zeeb * a module, but the kernel cannot reach a module, and code never compiled 7574abbf816SBjoern A. Zeeb * cannot be reached either. 7584abbf816SBjoern A. Zeeb * XXX -- I'd hope the module-to-module case would be handled by a proper 7594abbf816SBjoern A. Zeeb * module dependency definition (MODULE_DEPEND() in FreeBSD). 7604abbf816SBjoern A. Zeeb */ 7614abbf816SBjoern A. Zeeb #define IS_REACHABLE(_x) (IS_BUILTIN(_x) || \ 7624abbf816SBjoern A. Zeeb (IS_MODULE(_x) && IS_BUILTIN(MODULE))) 7634abbf816SBjoern A. Zeeb 764307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 765