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/types.h> 34 #include <sys/systm.h> 35 #include <sys/param.h> 36 #include <sys/libkern.h> 37 #include <sys/stat.h> 38 #include <sys/smp.h> 39 #include <sys/stddef.h> 40 #include <sys/syslog.h> 41 #include <sys/time.h> 42 43 #include <linux/bitops.h> 44 #include <linux/build_bug.h> 45 #include <linux/compiler.h> 46 #include <linux/container_of.h> 47 #include <linux/kstrtox.h> 48 #include <linux/limits.h> 49 #include <linux/math.h> 50 #include <linux/minmax.h> 51 #include <linux/stringify.h> 52 #include <linux/errno.h> 53 #include <linux/types.h> 54 #include <linux/typecheck.h> 55 #include <linux/jiffies.h> 56 #include <linux/log2.h> 57 #include <linux/kconfig.h> 58 #include <linux/instruction_pointer.h> 59 60 #include <asm/byteorder.h> 61 #include <asm/cpufeature.h> 62 #include <asm/processor.h> 63 #include <asm/uaccess.h> 64 65 #include <linux/stdarg.h> 66 67 #define KERN_CONT "" 68 #define KERN_EMERG "<0>" 69 #define KERN_ALERT "<1>" 70 #define KERN_CRIT "<2>" 71 #define KERN_ERR "<3>" 72 #define KERN_WARNING "<4>" 73 #define KERN_NOTICE "<5>" 74 #define KERN_INFO "<6>" 75 #define KERN_DEBUG "<7>" 76 77 #define S8_C(x) x 78 #define U8_C(x) x ## U 79 #define S16_C(x) x 80 #define U16_C(x) x ## U 81 #define S32_C(x) x 82 #define U32_C(x) x ## U 83 #define S64_C(x) x ## LL 84 #define U64_C(x) x ## ULL 85 86 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__) 87 #define BUG_ON(cond) do { \ 88 if (cond) { \ 89 panic("BUG ON %s failed at %s:%d", \ 90 __stringify(cond), __FILE__, __LINE__); \ 91 } \ 92 } while (0) 93 94 extern int linuxkpi_warn_dump_stack; 95 #define WARN_ON(cond) ({ \ 96 bool __ret = (cond); \ 97 if (__ret) { \ 98 printf("WARNING %s failed at %s:%d\n", \ 99 __stringify(cond), __FILE__, __LINE__); \ 100 if (linuxkpi_warn_dump_stack) \ 101 linux_dump_stack(); \ 102 } \ 103 unlikely(__ret); \ 104 }) 105 106 #define WARN_ON_SMP(cond) WARN_ON(cond) 107 108 #define WARN_ON_ONCE(cond) ({ \ 109 static bool __warn_on_once; \ 110 bool __ret = (cond); \ 111 if (__ret && !__warn_on_once) { \ 112 __warn_on_once = 1; \ 113 printf("WARNING %s failed at %s:%d\n", \ 114 __stringify(cond), __FILE__, __LINE__); \ 115 if (linuxkpi_warn_dump_stack) \ 116 linux_dump_stack(); \ 117 } \ 118 unlikely(__ret); \ 119 }) 120 121 #define oops_in_progress SCHEDULER_STOPPED() 122 123 #undef ALIGN 124 #define ALIGN(x, y) roundup2((x), (y)) 125 #define ALIGN_DOWN(x, y) rounddown2(x, y) 126 #undef PTR_ALIGN 127 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a))) 128 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0) 129 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n) 130 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f) 131 132 #define printk(...) printf(__VA_ARGS__) 133 #define vprintk(f, a) vprintf(f, a) 134 135 #define PTR_IF(x, p) ((x) ? (p) : NULL) 136 137 #define asm __asm 138 139 extern void linux_dump_stack(void); 140 #define dump_stack() linux_dump_stack() 141 142 struct va_format { 143 const char *fmt; 144 va_list *va; 145 }; 146 147 static inline int 148 vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 149 { 150 ssize_t ssize = size; 151 int i; 152 153 i = vsnprintf(buf, size, fmt, args); 154 155 return ((i >= ssize) ? (ssize - 1) : i); 156 } 157 158 static inline int 159 scnprintf(char *buf, size_t size, const char *fmt, ...) 160 { 161 va_list args; 162 int i; 163 164 va_start(args, fmt); 165 i = vscnprintf(buf, size, fmt, args); 166 va_end(args); 167 168 return (i); 169 } 170 171 /* 172 * The "pr_debug()" and "pr_devel()" macros should produce zero code 173 * unless DEBUG is defined: 174 */ 175 #ifdef DEBUG 176 extern int linuxkpi_debug; 177 #define pr_debug(fmt, ...) \ 178 do { \ 179 if (linuxkpi_debug) \ 180 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \ 181 } while (0) 182 #define pr_devel(fmt, ...) \ 183 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__) 184 #else 185 #define pr_debug(fmt, ...) \ 186 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; }) 187 #define pr_devel(fmt, ...) \ 188 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; }) 189 #endif 190 191 #ifndef pr_fmt 192 #define pr_fmt(fmt) fmt 193 #endif 194 195 /* 196 * Print a one-time message (analogous to WARN_ONCE() et al): 197 */ 198 #define printk_once(...) do { \ 199 static bool __print_once; \ 200 \ 201 if (!__print_once) { \ 202 __print_once = true; \ 203 printk(__VA_ARGS__); \ 204 } \ 205 } while (0) 206 207 /* 208 * Log a one-time message (analogous to WARN_ONCE() et al): 209 */ 210 #define log_once(level,...) do { \ 211 static bool __log_once; \ 212 \ 213 if (unlikely(!__log_once)) { \ 214 __log_once = true; \ 215 log(level, __VA_ARGS__); \ 216 } \ 217 } while (0) 218 219 #define pr_emerg(fmt, ...) \ 220 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__) 221 #define pr_alert(fmt, ...) \ 222 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__) 223 #define pr_crit(fmt, ...) \ 224 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__) 225 #define pr_err(fmt, ...) \ 226 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 227 #define pr_err_once(fmt, ...) \ 228 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__) 229 #define pr_warning(fmt, ...) \ 230 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 231 #define pr_warn(...) \ 232 pr_warning(__VA_ARGS__) 233 #define pr_warn_once(fmt, ...) \ 234 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__) 235 #define pr_notice(fmt, ...) \ 236 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__) 237 #define pr_info(fmt, ...) \ 238 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 239 #define pr_info_once(fmt, ...) \ 240 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__) 241 #define pr_cont(fmt, ...) \ 242 printk(KERN_CONT fmt, ##__VA_ARGS__) 243 #define pr_warn_ratelimited(...) do { \ 244 static linux_ratelimit_t __ratelimited; \ 245 if (linux_ratelimited(&__ratelimited)) \ 246 pr_warning(__VA_ARGS__); \ 247 } while (0) 248 249 #ifndef WARN 250 #define WARN(condition, ...) ({ \ 251 bool __ret_warn_on = (condition); \ 252 if (unlikely(__ret_warn_on)) \ 253 pr_warning(__VA_ARGS__); \ 254 unlikely(__ret_warn_on); \ 255 }) 256 #endif 257 258 #ifndef WARN_ONCE 259 #define WARN_ONCE(condition, ...) ({ \ 260 bool __ret_warn_on = (condition); \ 261 if (unlikely(__ret_warn_on)) \ 262 pr_warn_once(__VA_ARGS__); \ 263 unlikely(__ret_warn_on); \ 264 }) 265 #endif 266 267 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 268 269 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val)) 270 271 #define offsetofend(t, m) \ 272 (offsetof(t, m) + sizeof((((t *)0)->m))) 273 274 #define smp_processor_id() PCPU_GET(cpuid) 275 #define num_possible_cpus() mp_ncpus 276 #define num_online_cpus() mp_ncpus 277 278 #if defined(__i386__) || defined(__amd64__) 279 extern bool linux_cpu_has_clflush; 280 #define cpu_has_clflush linux_cpu_has_clflush 281 #endif 282 283 typedef struct linux_ratelimit { 284 struct timeval lasttime; 285 int counter; 286 } linux_ratelimit_t; 287 288 static inline bool 289 linux_ratelimited(linux_ratelimit_t *rl) 290 { 291 return (ppsratecheck(&rl->lasttime, &rl->counter, 1)); 292 } 293 294 #define __is_constexpr(x) \ 295 __builtin_constant_p(x) 296 297 /* 298 * The is_signed() macro below returns true if the passed data type is 299 * signed. Else false is returned. 300 */ 301 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0) 302 303 #define TAINT_WARN 0 304 #define test_taint(x) (0) 305 #define add_taint(x,y) do { \ 306 } while (0) 307 308 static inline int 309 _h2b(const char c) 310 { 311 312 if (c >= '0' && c <= '9') 313 return (c - '0'); 314 if (c >= 'a' && c <= 'f') 315 return (10 + c - 'a'); 316 if (c >= 'A' && c <= 'F') 317 return (10 + c - 'A'); 318 return (-EINVAL); 319 } 320 321 static inline int 322 hex2bin(uint8_t *bindst, const char *hexsrc, size_t binlen) 323 { 324 int hi4, lo4; 325 326 while (binlen > 0) { 327 hi4 = _h2b(*hexsrc++); 328 lo4 = _h2b(*hexsrc++); 329 if (hi4 < 0 || lo4 < 0) 330 return (-EINVAL); 331 332 *bindst++ = (hi4 << 4) | lo4; 333 binlen--; 334 } 335 336 return (0); 337 } 338 339 static inline bool 340 mac_pton(const char *macin, uint8_t *macout) 341 { 342 const char *s, *d; 343 uint8_t mac[6], hx, lx; 344 int i; 345 346 if (strlen(macin) < (3 * 6 - 1)) 347 return (false); 348 349 i = 0; 350 s = macin; 351 do { 352 /* Should we also support '-'-delimiters? */ 353 d = strchrnul(s, ':'); 354 hx = lx = 0; 355 while (s < d) { 356 /* Fail on abc:123:xxx:... */ 357 if ((d - s) > 2) 358 return (false); 359 /* We do support non-well-formed strings: 3:45:6:... */ 360 if ((d - s) > 1) { 361 hx = _h2b(*s); 362 if (hx < 0) 363 return (false); 364 s++; 365 } 366 lx = _h2b(*s); 367 if (lx < 0) 368 return (false); 369 s++; 370 } 371 mac[i] = (hx << 4) | lx; 372 i++; 373 if (i >= 6) 374 return (false); 375 } while (d != NULL && *d != '\0'); 376 377 memcpy(macout, mac, 6); 378 return (true); 379 } 380 381 #define DECLARE_FLEX_ARRAY(_t, _n) \ 382 struct { struct { } __dummy_ ## _n; _t _n[0]; } 383 384 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */ 385