1 #ifndef _LINUX_KERNEL_H 2 #define _LINUX_KERNEL_H 3 4 /* 5 * 'kernel.h' contains some often-used function prototypes etc 6 */ 7 8 #ifdef __KERNEL__ 9 10 #include <stdarg.h> 11 #include <linux/linkage.h> 12 #include <linux/stddef.h> 13 #include <linux/types.h> 14 #include <linux/compiler.h> 15 #include <linux/bitops.h> 16 #include <linux/log2.h> 17 #include <linux/typecheck.h> 18 #include <linux/dynamic_debug.h> 19 #include <asm/byteorder.h> 20 #include <asm/bug.h> 21 22 extern const char linux_banner[]; 23 extern const char linux_proc_banner[]; 24 25 #define USHORT_MAX ((u16)(~0U)) 26 #define SHORT_MAX ((s16)(USHORT_MAX>>1)) 27 #define SHORT_MIN (-SHORT_MAX - 1) 28 #define INT_MAX ((int)(~0U>>1)) 29 #define INT_MIN (-INT_MAX - 1) 30 #define UINT_MAX (~0U) 31 #define LONG_MAX ((long)(~0UL>>1)) 32 #define LONG_MIN (-LONG_MAX - 1) 33 #define ULONG_MAX (~0UL) 34 #define LLONG_MAX ((long long)(~0ULL>>1)) 35 #define LLONG_MIN (-LLONG_MAX - 1) 36 #define ULLONG_MAX (~0ULL) 37 38 #define STACK_MAGIC 0xdeadbeef 39 40 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 41 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 42 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 43 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 44 45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 46 47 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 48 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 49 #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 50 #define DIV_ROUND_CLOSEST(x, divisor)( \ 51 { \ 52 typeof(divisor) __divisor = divisor; \ 53 (((x) + ((__divisor) / 2)) / (__divisor)); \ 54 } \ 55 ) 56 57 #define _RET_IP_ (unsigned long)__builtin_return_address(0) 58 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 59 60 #ifdef CONFIG_LBDAF 61 # include <asm/div64.h> 62 # define sector_div(a, b) do_div(a, b) 63 #else 64 # define sector_div(n, b)( \ 65 { \ 66 int _res; \ 67 _res = (n) % (b); \ 68 (n) /= (b); \ 69 _res; \ 70 } \ 71 ) 72 #endif 73 74 /** 75 * upper_32_bits - return bits 32-63 of a number 76 * @n: the number we're accessing 77 * 78 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 79 * the "right shift count >= width of type" warning when that quantity is 80 * 32-bits. 81 */ 82 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 83 84 /** 85 * lower_32_bits - return bits 0-31 of a number 86 * @n: the number we're accessing 87 */ 88 #define lower_32_bits(n) ((u32)(n)) 89 90 #define KERN_EMERG "<0>" /* system is unusable */ 91 #define KERN_ALERT "<1>" /* action must be taken immediately */ 92 #define KERN_CRIT "<2>" /* critical conditions */ 93 #define KERN_ERR "<3>" /* error conditions */ 94 #define KERN_WARNING "<4>" /* warning conditions */ 95 #define KERN_NOTICE "<5>" /* normal but significant condition */ 96 #define KERN_INFO "<6>" /* informational */ 97 #define KERN_DEBUG "<7>" /* debug-level messages */ 98 99 /* Use the default kernel loglevel */ 100 #define KERN_DEFAULT "<d>" 101 /* 102 * Annotation for a "continued" line of log printout (only done after a 103 * line that had no enclosing \n). Only to be used by core/arch code 104 * during early bootup (a continued line is not SMP-safe otherwise). 105 */ 106 #define KERN_CONT "<c>" 107 108 extern int console_printk[]; 109 110 #define console_loglevel (console_printk[0]) 111 #define default_message_loglevel (console_printk[1]) 112 #define minimum_console_loglevel (console_printk[2]) 113 #define default_console_loglevel (console_printk[3]) 114 115 struct completion; 116 struct pt_regs; 117 struct user; 118 119 #ifdef CONFIG_PREEMPT_VOLUNTARY 120 extern int _cond_resched(void); 121 # define might_resched() _cond_resched() 122 #else 123 # define might_resched() do { } while (0) 124 #endif 125 126 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 127 void __might_sleep(const char *file, int line, int preempt_offset); 128 /** 129 * might_sleep - annotation for functions that can sleep 130 * 131 * this macro will print a stack trace if it is executed in an atomic 132 * context (spinlock, irq-handler, ...). 133 * 134 * This is a useful debugging help to be able to catch problems early and not 135 * be bitten later when the calling function happens to sleep when it is not 136 * supposed to. 137 */ 138 # define might_sleep() \ 139 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 140 #else 141 static inline void __might_sleep(const char *file, int line, 142 int preempt_offset) { } 143 # define might_sleep() do { might_resched(); } while (0) 144 #endif 145 146 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 147 148 #define abs(x) ({ \ 149 long __x = (x); \ 150 (__x < 0) ? -__x : __x; \ 151 }) 152 153 #ifdef CONFIG_PROVE_LOCKING 154 void might_fault(void); 155 #else 156 static inline void might_fault(void) 157 { 158 might_sleep(); 159 } 160 #endif 161 162 extern struct atomic_notifier_head panic_notifier_list; 163 extern long (*panic_blink)(long time); 164 NORET_TYPE void panic(const char * fmt, ...) 165 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 166 extern void oops_enter(void); 167 extern void oops_exit(void); 168 extern int oops_may_print(void); 169 NORET_TYPE void do_exit(long error_code) 170 ATTRIB_NORET; 171 NORET_TYPE void complete_and_exit(struct completion *, long) 172 ATTRIB_NORET; 173 extern unsigned long simple_strtoul(const char *,char **,unsigned int); 174 extern long simple_strtol(const char *,char **,unsigned int); 175 extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 176 extern long long simple_strtoll(const char *,char **,unsigned int); 177 extern int strict_strtoul(const char *, unsigned int, unsigned long *); 178 extern int strict_strtol(const char *, unsigned int, long *); 179 extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 180 extern int strict_strtoll(const char *, unsigned int, long long *); 181 extern int sprintf(char * buf, const char * fmt, ...) 182 __attribute__ ((format (printf, 2, 3))); 183 extern int vsprintf(char *buf, const char *, va_list) 184 __attribute__ ((format (printf, 2, 0))); 185 extern int snprintf(char * buf, size_t size, const char * fmt, ...) 186 __attribute__ ((format (printf, 3, 4))); 187 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 188 __attribute__ ((format (printf, 3, 0))); 189 extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 190 __attribute__ ((format (printf, 3, 4))); 191 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 192 __attribute__ ((format (printf, 3, 0))); 193 extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 194 __attribute__ ((format (printf, 2, 3))); 195 extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 196 197 extern int sscanf(const char *, const char *, ...) 198 __attribute__ ((format (scanf, 2, 3))); 199 extern int vsscanf(const char *, const char *, va_list) 200 __attribute__ ((format (scanf, 2, 0))); 201 202 extern int get_option(char **str, int *pint); 203 extern char *get_options(const char *str, int nints, int *ints); 204 extern unsigned long long memparse(const char *ptr, char **retptr); 205 206 extern int core_kernel_text(unsigned long addr); 207 extern int __kernel_text_address(unsigned long addr); 208 extern int kernel_text_address(unsigned long addr); 209 extern int func_ptr_is_kernel_text(void *ptr); 210 211 struct pid; 212 extern struct pid *session_of_pgrp(struct pid *pgrp); 213 214 /* 215 * FW_BUG 216 * Add this to a message where you are sure the firmware is buggy or behaves 217 * really stupid or out of spec. Be aware that the responsible BIOS developer 218 * should be able to fix this issue or at least get a concrete idea of the 219 * problem by reading your message without the need of looking at the kernel 220 * code. 221 * 222 * Use it for definite and high priority BIOS bugs. 223 * 224 * FW_WARN 225 * Use it for not that clear (e.g. could the kernel messed up things already?) 226 * and medium priority BIOS bugs. 227 * 228 * FW_INFO 229 * Use this one if you want to tell the user or vendor about something 230 * suspicious, but generally harmless related to the firmware. 231 * 232 * Use it for information or very low priority BIOS bugs. 233 */ 234 #define FW_BUG "[Firmware Bug]: " 235 #define FW_WARN "[Firmware Warn]: " 236 #define FW_INFO "[Firmware Info]: " 237 238 #ifdef CONFIG_PRINTK 239 asmlinkage int vprintk(const char *fmt, va_list args) 240 __attribute__ ((format (printf, 1, 0))); 241 asmlinkage int printk(const char * fmt, ...) 242 __attribute__ ((format (printf, 1, 2))) __cold; 243 244 extern int __printk_ratelimit(const char *func); 245 #define printk_ratelimit() __printk_ratelimit(__func__) 246 extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 247 unsigned int interval_msec); 248 249 extern int printk_delay_msec; 250 251 /* 252 * Print a one-time message (analogous to WARN_ONCE() et al): 253 */ 254 #define printk_once(x...) ({ \ 255 static bool __print_once; \ 256 \ 257 if (!__print_once) { \ 258 __print_once = true; \ 259 printk(x); \ 260 } \ 261 }) 262 263 void log_buf_kexec_setup(void); 264 #else 265 static inline int vprintk(const char *s, va_list args) 266 __attribute__ ((format (printf, 1, 0))); 267 static inline int vprintk(const char *s, va_list args) { return 0; } 268 static inline int printk(const char *s, ...) 269 __attribute__ ((format (printf, 1, 2))); 270 static inline int __cold printk(const char *s, ...) { return 0; } 271 static inline int printk_ratelimit(void) { return 0; } 272 static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 273 unsigned int interval_msec) \ 274 { return false; } 275 276 /* No effect, but we still get type checking even in the !PRINTK case: */ 277 #define printk_once(x...) printk(x) 278 279 static inline void log_buf_kexec_setup(void) 280 { 281 } 282 #endif 283 284 extern int printk_needs_cpu(int cpu); 285 extern void printk_tick(void); 286 287 extern void asmlinkage __attribute__((format(printf, 1, 2))) 288 early_printk(const char *fmt, ...); 289 290 unsigned long int_sqrt(unsigned long); 291 292 static inline void console_silent(void) 293 { 294 console_loglevel = 0; 295 } 296 297 static inline void console_verbose(void) 298 { 299 if (console_loglevel) 300 console_loglevel = 15; 301 } 302 303 extern void bust_spinlocks(int yes); 304 extern void wake_up_klogd(void); 305 extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 306 extern int panic_timeout; 307 extern int panic_on_oops; 308 extern int panic_on_unrecovered_nmi; 309 extern int panic_on_io_nmi; 310 extern const char *print_tainted(void); 311 extern void add_taint(unsigned flag); 312 extern int test_taint(unsigned flag); 313 extern unsigned long get_taint(void); 314 extern int root_mountflags; 315 316 /* Values used for system_state */ 317 extern enum system_states { 318 SYSTEM_BOOTING, 319 SYSTEM_RUNNING, 320 SYSTEM_HALT, 321 SYSTEM_POWER_OFF, 322 SYSTEM_RESTART, 323 SYSTEM_SUSPEND_DISK, 324 } system_state; 325 326 #define TAINT_PROPRIETARY_MODULE 0 327 #define TAINT_FORCED_MODULE 1 328 #define TAINT_UNSAFE_SMP 2 329 #define TAINT_FORCED_RMMOD 3 330 #define TAINT_MACHINE_CHECK 4 331 #define TAINT_BAD_PAGE 5 332 #define TAINT_USER 6 333 #define TAINT_DIE 7 334 #define TAINT_OVERRIDDEN_ACPI_TABLE 8 335 #define TAINT_WARN 9 336 #define TAINT_CRAP 10 337 338 extern void dump_stack(void) __cold; 339 340 enum { 341 DUMP_PREFIX_NONE, 342 DUMP_PREFIX_ADDRESS, 343 DUMP_PREFIX_OFFSET 344 }; 345 extern void hex_dump_to_buffer(const void *buf, size_t len, 346 int rowsize, int groupsize, 347 char *linebuf, size_t linebuflen, bool ascii); 348 extern void print_hex_dump(const char *level, const char *prefix_str, 349 int prefix_type, int rowsize, int groupsize, 350 const void *buf, size_t len, bool ascii); 351 extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 352 const void *buf, size_t len); 353 354 extern const char hex_asc[]; 355 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 356 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 357 358 static inline char *pack_hex_byte(char *buf, u8 byte) 359 { 360 *buf++ = hex_asc_hi(byte); 361 *buf++ = hex_asc_lo(byte); 362 return buf; 363 } 364 365 #ifndef pr_fmt 366 #define pr_fmt(fmt) fmt 367 #endif 368 369 #define pr_emerg(fmt, ...) \ 370 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 371 #define pr_alert(fmt, ...) \ 372 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 373 #define pr_crit(fmt, ...) \ 374 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 375 #define pr_err(fmt, ...) \ 376 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 377 #define pr_warning(fmt, ...) \ 378 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 379 #define pr_notice(fmt, ...) \ 380 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 381 #define pr_info(fmt, ...) \ 382 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 383 #define pr_cont(fmt, ...) \ 384 printk(KERN_CONT fmt, ##__VA_ARGS__) 385 386 /* pr_devel() should produce zero code unless DEBUG is defined */ 387 #ifdef DEBUG 388 #define pr_devel(fmt, ...) \ 389 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 390 #else 391 #define pr_devel(fmt, ...) \ 392 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 393 #endif 394 395 /* If you are writing a driver, please use dev_dbg instead */ 396 #if defined(DEBUG) 397 #define pr_debug(fmt, ...) \ 398 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 399 #elif defined(CONFIG_DYNAMIC_DEBUG) 400 /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */ 401 #define pr_debug(fmt, ...) \ 402 dynamic_pr_debug(fmt, ##__VA_ARGS__) 403 #else 404 #define pr_debug(fmt, ...) \ 405 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 406 #endif 407 408 /* 409 * ratelimited messages with local ratelimit_state, 410 * no local ratelimit_state used in the !PRINTK case 411 */ 412 #ifdef CONFIG_PRINTK 413 #define printk_ratelimited(fmt, ...) ({ \ 414 static struct ratelimit_state _rs = { \ 415 .interval = DEFAULT_RATELIMIT_INTERVAL, \ 416 .burst = DEFAULT_RATELIMIT_BURST, \ 417 }; \ 418 \ 419 if (!__ratelimit(&_rs)) \ 420 printk(fmt, ##__VA_ARGS__); \ 421 }) 422 #else 423 /* No effect, but we still get type checking even in the !PRINTK case: */ 424 #define printk_ratelimited printk 425 #endif 426 427 #define pr_emerg_ratelimited(fmt, ...) \ 428 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 429 #define pr_alert_ratelimited(fmt, ...) \ 430 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 431 #define pr_crit_ratelimited(fmt, ...) \ 432 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 433 #define pr_err_ratelimited(fmt, ...) \ 434 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 435 #define pr_warning_ratelimited(fmt, ...) \ 436 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 437 #define pr_notice_ratelimited(fmt, ...) \ 438 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 439 #define pr_info_ratelimited(fmt, ...) \ 440 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 441 /* no pr_cont_ratelimited, don't do that... */ 442 /* If you are writing a driver, please use dev_dbg instead */ 443 #if defined(DEBUG) 444 #define pr_debug_ratelimited(fmt, ...) \ 445 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 446 #else 447 #define pr_debug_ratelimited(fmt, ...) \ 448 ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \ 449 ##__VA_ARGS__); 0; }) 450 #endif 451 452 /* 453 * General tracing related utility functions - trace_printk(), 454 * tracing_on/tracing_off and tracing_start()/tracing_stop 455 * 456 * Use tracing_on/tracing_off when you want to quickly turn on or off 457 * tracing. It simply enables or disables the recording of the trace events. 458 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 459 * file, which gives a means for the kernel and userspace to interact. 460 * Place a tracing_off() in the kernel where you want tracing to end. 461 * From user space, examine the trace, and then echo 1 > tracing_on 462 * to continue tracing. 463 * 464 * tracing_stop/tracing_start has slightly more overhead. It is used 465 * by things like suspend to ram where disabling the recording of the 466 * trace is not enough, but tracing must actually stop because things 467 * like calling smp_processor_id() may crash the system. 468 * 469 * Most likely, you want to use tracing_on/tracing_off. 470 */ 471 #ifdef CONFIG_RING_BUFFER 472 void tracing_on(void); 473 void tracing_off(void); 474 /* trace_off_permanent stops recording with no way to bring it back */ 475 void tracing_off_permanent(void); 476 int tracing_is_on(void); 477 #else 478 static inline void tracing_on(void) { } 479 static inline void tracing_off(void) { } 480 static inline void tracing_off_permanent(void) { } 481 static inline int tracing_is_on(void) { return 0; } 482 #endif 483 #ifdef CONFIG_TRACING 484 extern void tracing_start(void); 485 extern void tracing_stop(void); 486 extern void ftrace_off_permanent(void); 487 488 extern void 489 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3); 490 491 static inline void __attribute__ ((format (printf, 1, 2))) 492 ____trace_printk_check_format(const char *fmt, ...) 493 { 494 } 495 #define __trace_printk_check_format(fmt, args...) \ 496 do { \ 497 if (0) \ 498 ____trace_printk_check_format(fmt, ##args); \ 499 } while (0) 500 501 /** 502 * trace_printk - printf formatting in the ftrace buffer 503 * @fmt: the printf format for printing 504 * 505 * Note: __trace_printk is an internal function for trace_printk and 506 * the @ip is passed in via the trace_printk macro. 507 * 508 * This function allows a kernel developer to debug fast path sections 509 * that printk is not appropriate for. By scattering in various 510 * printk like tracing in the code, a developer can quickly see 511 * where problems are occurring. 512 * 513 * This is intended as a debugging tool for the developer only. 514 * Please refrain from leaving trace_printks scattered around in 515 * your code. 516 */ 517 518 #define trace_printk(fmt, args...) \ 519 do { \ 520 __trace_printk_check_format(fmt, ##args); \ 521 if (__builtin_constant_p(fmt)) { \ 522 static const char *trace_printk_fmt \ 523 __attribute__((section("__trace_printk_fmt"))) = \ 524 __builtin_constant_p(fmt) ? fmt : NULL; \ 525 \ 526 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 527 } else \ 528 __trace_printk(_THIS_IP_, fmt, ##args); \ 529 } while (0) 530 531 extern int 532 __trace_bprintk(unsigned long ip, const char *fmt, ...) 533 __attribute__ ((format (printf, 2, 3))); 534 535 extern int 536 __trace_printk(unsigned long ip, const char *fmt, ...) 537 __attribute__ ((format (printf, 2, 3))); 538 539 extern void trace_dump_stack(void); 540 541 /* 542 * The double __builtin_constant_p is because gcc will give us an error 543 * if we try to allocate the static variable to fmt if it is not a 544 * constant. Even with the outer if statement. 545 */ 546 #define ftrace_vprintk(fmt, vargs) \ 547 do { \ 548 if (__builtin_constant_p(fmt)) { \ 549 static const char *trace_printk_fmt \ 550 __attribute__((section("__trace_printk_fmt"))) = \ 551 __builtin_constant_p(fmt) ? fmt : NULL; \ 552 \ 553 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 554 } else \ 555 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 556 } while (0) 557 558 extern int 559 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 560 561 extern int 562 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 563 564 extern void ftrace_dump(void); 565 #else 566 static inline void 567 ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { } 568 static inline int 569 trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); 570 571 static inline void tracing_start(void) { } 572 static inline void tracing_stop(void) { } 573 static inline void ftrace_off_permanent(void) { } 574 static inline void trace_dump_stack(void) { } 575 static inline int 576 trace_printk(const char *fmt, ...) 577 { 578 return 0; 579 } 580 static inline int 581 ftrace_vprintk(const char *fmt, va_list ap) 582 { 583 return 0; 584 } 585 static inline void ftrace_dump(void) { } 586 #endif /* CONFIG_TRACING */ 587 588 /* 589 * Display an IP address in readable format. 590 */ 591 592 #define NIPQUAD(addr) \ 593 ((unsigned char *)&addr)[0], \ 594 ((unsigned char *)&addr)[1], \ 595 ((unsigned char *)&addr)[2], \ 596 ((unsigned char *)&addr)[3] 597 #define NIPQUAD_FMT "%u.%u.%u.%u" 598 599 /* 600 * min()/max()/clamp() macros that also do 601 * strict type-checking.. See the 602 * "unnecessary" pointer comparison. 603 */ 604 #define min(x, y) ({ \ 605 typeof(x) _min1 = (x); \ 606 typeof(y) _min2 = (y); \ 607 (void) (&_min1 == &_min2); \ 608 _min1 < _min2 ? _min1 : _min2; }) 609 610 #define max(x, y) ({ \ 611 typeof(x) _max1 = (x); \ 612 typeof(y) _max2 = (y); \ 613 (void) (&_max1 == &_max2); \ 614 _max1 > _max2 ? _max1 : _max2; }) 615 616 /** 617 * clamp - return a value clamped to a given range with strict typechecking 618 * @val: current value 619 * @min: minimum allowable value 620 * @max: maximum allowable value 621 * 622 * This macro does strict typechecking of min/max to make sure they are of the 623 * same type as val. See the unnecessary pointer comparisons. 624 */ 625 #define clamp(val, min, max) ({ \ 626 typeof(val) __val = (val); \ 627 typeof(min) __min = (min); \ 628 typeof(max) __max = (max); \ 629 (void) (&__val == &__min); \ 630 (void) (&__val == &__max); \ 631 __val = __val < __min ? __min: __val; \ 632 __val > __max ? __max: __val; }) 633 634 /* 635 * ..and if you can't take the strict 636 * types, you can specify one yourself. 637 * 638 * Or not use min/max/clamp at all, of course. 639 */ 640 #define min_t(type, x, y) ({ \ 641 type __min1 = (x); \ 642 type __min2 = (y); \ 643 __min1 < __min2 ? __min1: __min2; }) 644 645 #define max_t(type, x, y) ({ \ 646 type __max1 = (x); \ 647 type __max2 = (y); \ 648 __max1 > __max2 ? __max1: __max2; }) 649 650 /** 651 * clamp_t - return a value clamped to a given range using a given type 652 * @type: the type of variable to use 653 * @val: current value 654 * @min: minimum allowable value 655 * @max: maximum allowable value 656 * 657 * This macro does no typechecking and uses temporary variables of type 658 * 'type' to make all the comparisons. 659 */ 660 #define clamp_t(type, val, min, max) ({ \ 661 type __val = (val); \ 662 type __min = (min); \ 663 type __max = (max); \ 664 __val = __val < __min ? __min: __val; \ 665 __val > __max ? __max: __val; }) 666 667 /** 668 * clamp_val - return a value clamped to a given range using val's type 669 * @val: current value 670 * @min: minimum allowable value 671 * @max: maximum allowable value 672 * 673 * This macro does no typechecking and uses temporary variables of whatever 674 * type the input argument 'val' is. This is useful when val is an unsigned 675 * type and min and max are literals that will otherwise be assigned a signed 676 * integer type. 677 */ 678 #define clamp_val(val, min, max) ({ \ 679 typeof(val) __val = (val); \ 680 typeof(val) __min = (min); \ 681 typeof(val) __max = (max); \ 682 __val = __val < __min ? __min: __val; \ 683 __val > __max ? __max: __val; }) 684 685 686 /* 687 * swap - swap value of @a and @b 688 */ 689 #define swap(a, b) \ 690 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 691 692 /** 693 * container_of - cast a member of a structure out to the containing structure 694 * @ptr: the pointer to the member. 695 * @type: the type of the container struct this is embedded in. 696 * @member: the name of the member within the struct. 697 * 698 */ 699 #define container_of(ptr, type, member) ({ \ 700 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 701 (type *)( (char *)__mptr - offsetof(type,member) );}) 702 703 struct sysinfo; 704 extern int do_sysinfo(struct sysinfo *info); 705 706 #endif /* __KERNEL__ */ 707 708 #ifndef __EXPORTED_HEADERS__ 709 #ifndef __KERNEL__ 710 #warning Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders 711 #endif /* __KERNEL__ */ 712 #endif /* __EXPORTED_HEADERS__ */ 713 714 #define SI_LOAD_SHIFT 16 715 struct sysinfo { 716 long uptime; /* Seconds since boot */ 717 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 718 unsigned long totalram; /* Total usable main memory size */ 719 unsigned long freeram; /* Available memory size */ 720 unsigned long sharedram; /* Amount of shared memory */ 721 unsigned long bufferram; /* Memory used by buffers */ 722 unsigned long totalswap; /* Total swap space size */ 723 unsigned long freeswap; /* swap space still available */ 724 unsigned short procs; /* Number of current processes */ 725 unsigned short pad; /* explicit padding for m68k */ 726 unsigned long totalhigh; /* Total high memory size */ 727 unsigned long freehigh; /* Available high memory size */ 728 unsigned int mem_unit; /* Memory unit size in bytes */ 729 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 730 }; 731 732 /* Force a compilation error if condition is true */ 733 #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) 734 735 /* Force a compilation error if condition is constant and true */ 736 #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) 737 738 /* Force a compilation error if a constant expression is not a power of 2 */ 739 #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ 740 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) 741 742 /* Force a compilation error if condition is true, but also produce a 743 result (of value 0 and type size_t), so the expression can be used 744 e.g. in a structure initializer (or where-ever else comma expressions 745 aren't permitted). */ 746 #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 747 #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 748 749 /* Trap pasters of __FUNCTION__ at compile-time */ 750 #define __FUNCTION__ (__func__) 751 752 /* This helps us to avoid #ifdef CONFIG_NUMA */ 753 #ifdef CONFIG_NUMA 754 #define NUMA_BUILD 1 755 #else 756 #define NUMA_BUILD 0 757 #endif 758 759 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 760 #ifdef CONFIG_FTRACE_MCOUNT_RECORD 761 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 762 #endif 763 764 #endif 765