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