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