11da177e4SLinus Torvalds #ifndef _LINUX_KERNEL_H 21da177e4SLinus Torvalds #define _LINUX_KERNEL_H 31da177e4SLinus Torvalds 41da177e4SLinus Torvalds /* 51da177e4SLinus Torvalds * 'kernel.h' contains some often-used function prototypes etc 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #ifdef __KERNEL__ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <stdarg.h> 111da177e4SLinus Torvalds #include <linux/linkage.h> 121da177e4SLinus Torvalds #include <linux/stddef.h> 131da177e4SLinus Torvalds #include <linux/types.h> 141da177e4SLinus Torvalds #include <linux/compiler.h> 151da177e4SLinus Torvalds #include <linux/bitops.h> 16f0d1b0b3SDavid Howells #include <linux/log2.h> 17e0deaff4SAndrew Morton #include <linux/typecheck.h> 18717115e1SDave Young #include <linux/ratelimit.h> 19346e15beSJason Baron #include <linux/dynamic_printk.h> 201da177e4SLinus Torvalds #include <asm/byteorder.h> 211da177e4SLinus Torvalds #include <asm/bug.h> 221da177e4SLinus Torvalds 233eb3c740SRoman Zippel extern const char linux_banner[]; 243eb3c740SRoman Zippel extern const char linux_proc_banner[]; 253eb3c740SRoman Zippel 2644f564a4SZhang, Yanmin #define USHORT_MAX ((u16)(~0U)) 2744f564a4SZhang, Yanmin #define SHORT_MAX ((s16)(USHORT_MAX>>1)) 2844f564a4SZhang, Yanmin #define SHORT_MIN (-SHORT_MAX - 1) 291da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 301da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 311da177e4SLinus Torvalds #define UINT_MAX (~0U) 321da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 331da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 341da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 35111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 36111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 37111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 401da177e4SLinus Torvalds 412ea58144SLinus Torvalds #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 422ea58144SLinus Torvalds #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 43a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 44f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 452ea58144SLinus Torvalds 46c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 47c5e631cfSRusty Russell 484552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 49930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 50b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 519fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)( \ 529fe06081SDarrick J. Wong { \ 539fe06081SDarrick J. Wong typeof(divisor) __divisor = divisor; \ 549fe06081SDarrick J. Wong (((x) + ((__divisor) / 2)) / (__divisor)); \ 559fe06081SDarrick J. Wong } \ 569fe06081SDarrick J. Wong ) 571da177e4SLinus Torvalds 58ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 59ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 60ca31e146SEduard - Gabriel Munteanu 612da96acdSJens Axboe #ifdef CONFIG_LBD 622da96acdSJens Axboe # include <asm/div64.h> 632da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 642da96acdSJens Axboe #else 652da96acdSJens Axboe # define sector_div(n, b)( \ 662da96acdSJens Axboe { \ 672da96acdSJens Axboe int _res; \ 682da96acdSJens Axboe _res = (n) % (b); \ 692da96acdSJens Axboe (n) /= (b); \ 702da96acdSJens Axboe _res; \ 712da96acdSJens Axboe } \ 722da96acdSJens Axboe ) 732da96acdSJens Axboe #endif 742da96acdSJens Axboe 75218e180eSAndrew Morton /** 76218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 77218e180eSAndrew Morton * @n: the number we're accessing 78218e180eSAndrew Morton * 79218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 80218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 81218e180eSAndrew Morton * 32-bits. 82218e180eSAndrew Morton */ 83218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 84218e180eSAndrew Morton 85204b885eSJoerg Roedel /** 86204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 87204b885eSJoerg Roedel * @n: the number we're accessing 88204b885eSJoerg Roedel */ 89204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n)) 90204b885eSJoerg Roedel 911da177e4SLinus Torvalds #define KERN_EMERG "<0>" /* system is unusable */ 921da177e4SLinus Torvalds #define KERN_ALERT "<1>" /* action must be taken immediately */ 931da177e4SLinus Torvalds #define KERN_CRIT "<2>" /* critical conditions */ 941da177e4SLinus Torvalds #define KERN_ERR "<3>" /* error conditions */ 951da177e4SLinus Torvalds #define KERN_WARNING "<4>" /* warning conditions */ 961da177e4SLinus Torvalds #define KERN_NOTICE "<5>" /* normal but significant condition */ 971da177e4SLinus Torvalds #define KERN_INFO "<6>" /* informational */ 981da177e4SLinus Torvalds #define KERN_DEBUG "<7>" /* debug-level messages */ 991da177e4SLinus Torvalds 10047492527SIngo Molnar /* 10147492527SIngo Molnar * Annotation for a "continued" line of log printout (only done after a 10247492527SIngo Molnar * line that had no enclosing \n). Only to be used by core/arch code 10347492527SIngo Molnar * during early bootup (a continued line is not SMP-safe otherwise). 10447492527SIngo Molnar */ 10547492527SIngo Molnar #define KERN_CONT "" 10647492527SIngo Molnar 1071da177e4SLinus Torvalds extern int console_printk[]; 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds #define console_loglevel (console_printk[0]) 1101da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1]) 1111da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2]) 1121da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3]) 1131da177e4SLinus Torvalds 1141da177e4SLinus Torvalds struct completion; 115df2e71fbS[email protected] struct pt_regs; 116df2e71fbS[email protected] struct user; 1171da177e4SLinus Torvalds 118070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 119070cb065SUwe Kleine-König extern int _cond_resched(void); 120070cb065SUwe Kleine-König # define might_resched() _cond_resched() 121070cb065SUwe Kleine-König #else 122070cb065SUwe Kleine-König # define might_resched() do { } while (0) 123070cb065SUwe Kleine-König #endif 124070cb065SUwe Kleine-König 1257106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 1267106a27bSRandy Dunlap void __might_sleep(char *file, int line); 1271da177e4SLinus Torvalds /** 1281da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1291da177e4SLinus Torvalds * 1301da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1311da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1321da177e4SLinus Torvalds * 1331da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 134e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1351da177e4SLinus Torvalds * supposed to. 1361da177e4SLinus Torvalds */ 137f8cbd99bSIngo Molnar # define might_sleep() \ 138f8cbd99bSIngo Molnar do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 139f8cbd99bSIngo Molnar #else 140f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 141f8cbd99bSIngo Molnar #endif 142f8cbd99bSIngo Molnar 143368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 144f8cbd99bSIngo Molnar 1451da177e4SLinus Torvalds #define abs(x) ({ \ 1461da177e4SLinus Torvalds int __x = (x); \ 1471da177e4SLinus Torvalds (__x < 0) ? -__x : __x; \ 1481da177e4SLinus Torvalds }) 1491da177e4SLinus Torvalds 1503ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 1513ee1afa3SNick Piggin void might_fault(void); 1523ee1afa3SNick Piggin #else 1533ee1afa3SNick Piggin static inline void might_fault(void) 1543ee1afa3SNick Piggin { 1553ee1afa3SNick Piggin might_sleep(); 1563ee1afa3SNick Piggin } 1573ee1afa3SNick Piggin #endif 1583ee1afa3SNick Piggin 159e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 1601da177e4SLinus Torvalds extern long (*panic_blink)(long time); 1611da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 162a586df06SAndi Kleen __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 163dd287796SAndrew Morton extern void oops_enter(void); 164dd287796SAndrew Morton extern void oops_exit(void); 165dd287796SAndrew Morton extern int oops_may_print(void); 166ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code) 1671da177e4SLinus Torvalds ATTRIB_NORET; 1681da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long) 1691da177e4SLinus Torvalds ATTRIB_NORET; 1701da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 1711da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 1721da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 1731da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 17406b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *); 17506b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *); 17606b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 17706b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *); 1781da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...) 1791da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 3))); 1801da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list) 1811da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 0))); 1821da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...) 1831da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1841da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1851da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 1861da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 1871da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1881da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 1891da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 190e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 191e905914fSJeremy Fitzhardinge __attribute__ ((format (printf, 2, 3))); 19211443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...) 1951da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 3))); 1961da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list) 1971da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 0))); 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 2001da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 201d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 2021da177e4SLinus Torvalds 2035e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 2041da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 2051da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 206ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 207ab7476cfSArjan van de Ven 20804a2e6a5SEric W. Biederman struct pid; 20904a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 2101da177e4SLinus Torvalds 211a0ad05c7SThomas Renninger /* 212a0ad05c7SThomas Renninger * FW_BUG 213a0ad05c7SThomas Renninger * Add this to a message where you are sure the firmware is buggy or behaves 214a0ad05c7SThomas Renninger * really stupid or out of spec. Be aware that the responsible BIOS developer 215a0ad05c7SThomas Renninger * should be able to fix this issue or at least get a concrete idea of the 216a0ad05c7SThomas Renninger * problem by reading your message without the need of looking at the kernel 217a0ad05c7SThomas Renninger * code. 218a0ad05c7SThomas Renninger * 219a0ad05c7SThomas Renninger * Use it for definite and high priority BIOS bugs. 220a0ad05c7SThomas Renninger * 221a0ad05c7SThomas Renninger * FW_WARN 222a0ad05c7SThomas Renninger * Use it for not that clear (e.g. could the kernel messed up things already?) 223a0ad05c7SThomas Renninger * and medium priority BIOS bugs. 224a0ad05c7SThomas Renninger * 225a0ad05c7SThomas Renninger * FW_INFO 226a0ad05c7SThomas Renninger * Use this one if you want to tell the user or vendor about something 227a0ad05c7SThomas Renninger * suspicious, but generally harmless related to the firmware. 228a0ad05c7SThomas Renninger * 229a0ad05c7SThomas Renninger * Use it for information or very low priority BIOS bugs. 230a0ad05c7SThomas Renninger */ 231a0ad05c7SThomas Renninger #define FW_BUG "[Firmware Bug]: " 232a0ad05c7SThomas Renninger #define FW_WARN "[Firmware Warn]: " 233a0ad05c7SThomas Renninger #define FW_INFO "[Firmware Info]: " 234a0ad05c7SThomas Renninger 235d59745ceSMatt Mackall #ifdef CONFIG_PRINTK 2361da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args) 2371da177e4SLinus Torvalds __attribute__ ((format (printf, 1, 0))); 2381da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...) 239a586df06SAndi Kleen __attribute__ ((format (printf, 1, 2))) __cold; 2407ef3d2fdSJoe Perches 241717115e1SDave Young extern struct ratelimit_state printk_ratelimit_state; 2427ef3d2fdSJoe Perches extern int printk_ratelimit(void); 2437ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 2447ef3d2fdSJoe Perches unsigned int interval_msec); 245d59745ceSMatt Mackall #else 246d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) 247d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 0))); 248d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; } 249d59745ceSMatt Mackall static inline int printk(const char *s, ...) 250d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 2))); 251a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; } 2527ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; } 2537ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 2547ef3d2fdSJoe Perches unsigned int interval_msec) \ 2557ef3d2fdSJoe Perches { return false; } 256d59745ceSMatt Mackall #endif 2571da177e4SLinus Torvalds 258ced9cd40SIngo Molnar extern int printk_needs_cpu(int cpu); 259ced9cd40SIngo Molnar extern void printk_tick(void); 260ced9cd40SIngo Molnar 261e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2))) 262076f9776SIngo Molnar early_printk(const char *fmt, ...); 263076f9776SIngo Molnar 2641da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds static inline void console_silent(void) 2671da177e4SLinus Torvalds { 2681da177e4SLinus Torvalds console_loglevel = 0; 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2711da177e4SLinus Torvalds static inline void console_verbose(void) 2721da177e4SLinus Torvalds { 2731da177e4SLinus Torvalds if (console_loglevel) 2741da177e4SLinus Torvalds console_loglevel = 15; 2751da177e4SLinus Torvalds } 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 278e3e8a75dSKirill Korotaev extern void wake_up_klogd(void); 2791da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 280aa727107SAdrian Bunk extern int panic_timeout; 2811da177e4SLinus Torvalds extern int panic_on_oops; 2828da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 2831da177e4SLinus Torvalds extern const char *print_tainted(void); 28425ddbb18SAndi Kleen extern void add_taint(unsigned flag); 28525ddbb18SAndi Kleen extern int test_taint(unsigned flag); 28625ddbb18SAndi Kleen extern unsigned long get_taint(void); 287b920de1bSDavid Howells extern int root_mountflags; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds /* Values used for system_state */ 2901da177e4SLinus Torvalds extern enum system_states { 2911da177e4SLinus Torvalds SYSTEM_BOOTING, 2921da177e4SLinus Torvalds SYSTEM_RUNNING, 2931da177e4SLinus Torvalds SYSTEM_HALT, 2941da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2951da177e4SLinus Torvalds SYSTEM_RESTART, 296729b4d4cSAlexey Starikovskiy SYSTEM_SUSPEND_DISK, 2971da177e4SLinus Torvalds } system_state; 2981da177e4SLinus Torvalds 29925ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE 0 30025ddbb18SAndi Kleen #define TAINT_FORCED_MODULE 1 30125ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP 2 30225ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD 3 30325ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK 4 30425ddbb18SAndi Kleen #define TAINT_BAD_PAGE 5 30525ddbb18SAndi Kleen #define TAINT_USER 6 30625ddbb18SAndi Kleen #define TAINT_DIE 7 30725ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE 8 30825ddbb18SAndi Kleen #define TAINT_WARN 9 30926e9a397SLinus Torvalds #define TAINT_CRAP 10 3101da177e4SLinus Torvalds 311a586df06SAndi Kleen extern void dump_stack(void) __cold; 3121da177e4SLinus Torvalds 31399eaf3c4SRandy Dunlap enum { 31499eaf3c4SRandy Dunlap DUMP_PREFIX_NONE, 31599eaf3c4SRandy Dunlap DUMP_PREFIX_ADDRESS, 31699eaf3c4SRandy Dunlap DUMP_PREFIX_OFFSET 31799eaf3c4SRandy Dunlap }; 318c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len, 319c7909234SRandy Dunlap int rowsize, int groupsize, 320c7909234SRandy Dunlap char *linebuf, size_t linebuflen, bool ascii); 321c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str, 322c7909234SRandy Dunlap int prefix_type, int rowsize, int groupsize, 3236a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii); 324c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 325eb9a9a56SAlan Stern const void *buf, size_t len); 3263fc95772SHarvey Harrison 3273fc95772SHarvey Harrison extern const char hex_asc[]; 3283fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 3293fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 3303fc95772SHarvey Harrison 3313fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte) 3323fc95772SHarvey Harrison { 3333fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 3343fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 3353fc95772SHarvey Harrison return buf; 3363fc95772SHarvey Harrison } 33799eaf3c4SRandy Dunlap 338d091c2f5SMartin Schwidefsky #ifndef pr_fmt 339d091c2f5SMartin Schwidefsky #define pr_fmt(fmt) fmt 340d091c2f5SMartin Schwidefsky #endif 341d091c2f5SMartin Schwidefsky 342d091c2f5SMartin Schwidefsky #define pr_emerg(fmt, ...) \ 343d091c2f5SMartin Schwidefsky printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 344d091c2f5SMartin Schwidefsky #define pr_alert(fmt, ...) \ 345d091c2f5SMartin Schwidefsky printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 346d091c2f5SMartin Schwidefsky #define pr_crit(fmt, ...) \ 347d091c2f5SMartin Schwidefsky printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 348d091c2f5SMartin Schwidefsky #define pr_err(fmt, ...) \ 349d091c2f5SMartin Schwidefsky printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 350d091c2f5SMartin Schwidefsky #define pr_warning(fmt, ...) \ 351d091c2f5SMartin Schwidefsky printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 352d091c2f5SMartin Schwidefsky #define pr_notice(fmt, ...) \ 353d091c2f5SMartin Schwidefsky printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 354d091c2f5SMartin Schwidefsky #define pr_info(fmt, ...) \ 355d091c2f5SMartin Schwidefsky printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 3561f7c8234SEmil Medve 3571cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */ 358d0d85ff9SCornelia Huck #if defined(DEBUG) 359d0d85ff9SCornelia Huck #define pr_debug(fmt, ...) \ 360d0d85ff9SCornelia Huck printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 361d0d85ff9SCornelia Huck #elif defined(CONFIG_DYNAMIC_PRINTK_DEBUG) 362346e15beSJason Baron #define pr_debug(fmt, ...) do { \ 363d091c2f5SMartin Schwidefsky dynamic_pr_debug(pr_fmt(fmt), ##__VA_ARGS__); \ 364346e15beSJason Baron } while (0) 3651da177e4SLinus Torvalds #else 366d091c2f5SMartin Schwidefsky #define pr_debug(fmt, ...) \ 367d091c2f5SMartin Schwidefsky ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 3681da177e4SLinus Torvalds #endif 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /* 371*526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 372*526211bcSIngo Molnar * tracing_start()/tracing_stop: 373*526211bcSIngo Molnar */ 374*526211bcSIngo Molnar #ifdef CONFIG_TRACING 375*526211bcSIngo Molnar extern void tracing_start(void); 376*526211bcSIngo Molnar extern void tracing_stop(void); 377*526211bcSIngo Molnar extern void ftrace_off_permanent(void); 378*526211bcSIngo Molnar 379*526211bcSIngo Molnar extern void 380*526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3); 381*526211bcSIngo Molnar 382*526211bcSIngo Molnar /** 383*526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 384*526211bcSIngo Molnar * @fmt: the printf format for printing 385*526211bcSIngo Molnar * 386*526211bcSIngo Molnar * Note: __trace_printk is an internal function for trace_printk and 387*526211bcSIngo Molnar * the @ip is passed in via the trace_printk macro. 388*526211bcSIngo Molnar * 389*526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 390*526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 391*526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 392*526211bcSIngo Molnar * where problems are occurring. 393*526211bcSIngo Molnar * 394*526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 395*526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 396*526211bcSIngo Molnar * your code. 397*526211bcSIngo Molnar */ 398*526211bcSIngo Molnar # define trace_printk(fmt...) __trace_printk(_THIS_IP_, fmt) 399*526211bcSIngo Molnar extern int 400*526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...) 401*526211bcSIngo Molnar __attribute__ ((format (printf, 2, 3))); 402*526211bcSIngo Molnar # define ftrace_vprintk(fmt, ap) __trace_printk(_THIS_IP_, fmt, ap) 403*526211bcSIngo Molnar extern int 404*526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 405*526211bcSIngo Molnar extern void ftrace_dump(void); 406*526211bcSIngo Molnar #else 407*526211bcSIngo Molnar static inline void 408*526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { } 409*526211bcSIngo Molnar static inline int 410*526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); 411*526211bcSIngo Molnar 412*526211bcSIngo Molnar static inline void tracing_start(void) { } 413*526211bcSIngo Molnar static inline void tracing_stop(void) { } 414*526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { } 415*526211bcSIngo Molnar static inline int 416*526211bcSIngo Molnar trace_printk(const char *fmt, ...) 417*526211bcSIngo Molnar { 418*526211bcSIngo Molnar return 0; 419*526211bcSIngo Molnar } 420*526211bcSIngo Molnar static inline int 421*526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 422*526211bcSIngo Molnar { 423*526211bcSIngo Molnar return 0; 424*526211bcSIngo Molnar } 425*526211bcSIngo Molnar static inline void ftrace_dump(void) { } 426*526211bcSIngo Molnar #endif 427*526211bcSIngo Molnar 428*526211bcSIngo Molnar /* 4291da177e4SLinus Torvalds * Display an IP address in readable format. 4301da177e4SLinus Torvalds */ 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds #define NIPQUAD(addr) \ 4331da177e4SLinus Torvalds ((unsigned char *)&addr)[0], \ 4341da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 4351da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 4361da177e4SLinus Torvalds ((unsigned char *)&addr)[3] 43746b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u" 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN) 4401da177e4SLinus Torvalds #define HIPQUAD(addr) \ 4411da177e4SLinus Torvalds ((unsigned char *)&addr)[3], \ 4421da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 4431da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 4441da177e4SLinus Torvalds ((unsigned char *)&addr)[0] 4451da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN) 4461da177e4SLinus Torvalds #define HIPQUAD NIPQUAD 4471da177e4SLinus Torvalds #else 4481da177e4SLinus Torvalds #error "Please fix asm/byteorder.h" 4491da177e4SLinus Torvalds #endif /* __LITTLE_ENDIAN */ 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds /* 452bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 4531da177e4SLinus Torvalds * strict type-checking.. See the 4541da177e4SLinus Torvalds * "unnecessary" pointer comparison. 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds #define min(x, y) ({ \ 457bdf4bbaaSHarvey Harrison typeof(x) _min1 = (x); \ 458bdf4bbaaSHarvey Harrison typeof(y) _min2 = (y); \ 459bdf4bbaaSHarvey Harrison (void) (&_min1 == &_min2); \ 460bdf4bbaaSHarvey Harrison _min1 < _min2 ? _min1 : _min2; }) 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds #define max(x, y) ({ \ 463bdf4bbaaSHarvey Harrison typeof(x) _max1 = (x); \ 464bdf4bbaaSHarvey Harrison typeof(y) _max2 = (y); \ 465bdf4bbaaSHarvey Harrison (void) (&_max1 == &_max2); \ 466bdf4bbaaSHarvey Harrison _max1 > _max2 ? _max1 : _max2; }) 467bdf4bbaaSHarvey Harrison 468bdf4bbaaSHarvey Harrison /** 469bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 470bdf4bbaaSHarvey Harrison * @val: current value 471bdf4bbaaSHarvey Harrison * @min: minimum allowable value 472bdf4bbaaSHarvey Harrison * @max: maximum allowable value 473bdf4bbaaSHarvey Harrison * 474bdf4bbaaSHarvey Harrison * This macro does strict typechecking of min/max to make sure they are of the 475bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 476bdf4bbaaSHarvey Harrison */ 477bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({ \ 478bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 479bdf4bbaaSHarvey Harrison typeof(min) __min = (min); \ 480bdf4bbaaSHarvey Harrison typeof(max) __max = (max); \ 481bdf4bbaaSHarvey Harrison (void) (&__val == &__min); \ 482bdf4bbaaSHarvey Harrison (void) (&__val == &__max); \ 483bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 484bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /* 4871da177e4SLinus Torvalds * ..and if you can't take the strict 4881da177e4SLinus Torvalds * types, you can specify one yourself. 4891da177e4SLinus Torvalds * 490bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 4911da177e4SLinus Torvalds */ 492bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({ \ 493bdf4bbaaSHarvey Harrison type __min1 = (x); \ 494bdf4bbaaSHarvey Harrison type __min2 = (y); \ 495bdf4bbaaSHarvey Harrison __min1 < __min2 ? __min1: __min2; }) 4961da177e4SLinus Torvalds 497bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({ \ 498bdf4bbaaSHarvey Harrison type __max1 = (x); \ 499bdf4bbaaSHarvey Harrison type __max2 = (y); \ 500bdf4bbaaSHarvey Harrison __max1 > __max2 ? __max1: __max2; }) 501bdf4bbaaSHarvey Harrison 502bdf4bbaaSHarvey Harrison /** 503bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 504bdf4bbaaSHarvey Harrison * @type: the type of variable to use 505bdf4bbaaSHarvey Harrison * @val: current value 506bdf4bbaaSHarvey Harrison * @min: minimum allowable value 507bdf4bbaaSHarvey Harrison * @max: maximum allowable value 508bdf4bbaaSHarvey Harrison * 509bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 510bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 511bdf4bbaaSHarvey Harrison */ 512bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({ \ 513bdf4bbaaSHarvey Harrison type __val = (val); \ 514bdf4bbaaSHarvey Harrison type __min = (min); \ 515bdf4bbaaSHarvey Harrison type __max = (max); \ 516bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 517bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 518bdf4bbaaSHarvey Harrison 519bdf4bbaaSHarvey Harrison /** 520bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 521bdf4bbaaSHarvey Harrison * @val: current value 522bdf4bbaaSHarvey Harrison * @min: minimum allowable value 523bdf4bbaaSHarvey Harrison * @max: maximum allowable value 524bdf4bbaaSHarvey Harrison * 525bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 526bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 527bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 528bdf4bbaaSHarvey Harrison * integer type. 529bdf4bbaaSHarvey Harrison */ 530bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({ \ 531bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 532bdf4bbaaSHarvey Harrison typeof(val) __min = (min); \ 533bdf4bbaaSHarvey Harrison typeof(val) __max = (max); \ 534bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 535bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 5361da177e4SLinus Torvalds 53791f68b73SWu Fengguang 53891f68b73SWu Fengguang /* 53991f68b73SWu Fengguang * swap - swap value of @a and @b 54091f68b73SWu Fengguang */ 541ac7b9004SPeter Zijlstra #define swap(a, b) \ 542ac7b9004SPeter Zijlstra do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 54391f68b73SWu Fengguang 5441da177e4SLinus Torvalds /** 5451da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 5461da177e4SLinus Torvalds * @ptr: the pointer to the member. 5471da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 5481da177e4SLinus Torvalds * @member: the name of the member within the struct. 5491da177e4SLinus Torvalds * 5501da177e4SLinus Torvalds */ 5511da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 5521da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 5531da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 5541da177e4SLinus Torvalds 555d4d23addSKyle McMartin struct sysinfo; 556d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info); 557d4d23addSKyle McMartin 5581da177e4SLinus Torvalds #endif /* __KERNEL__ */ 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds #define SI_LOAD_SHIFT 16 5611da177e4SLinus Torvalds struct sysinfo { 5621da177e4SLinus Torvalds long uptime; /* Seconds since boot */ 5631da177e4SLinus Torvalds unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 5641da177e4SLinus Torvalds unsigned long totalram; /* Total usable main memory size */ 5651da177e4SLinus Torvalds unsigned long freeram; /* Available memory size */ 5661da177e4SLinus Torvalds unsigned long sharedram; /* Amount of shared memory */ 5671da177e4SLinus Torvalds unsigned long bufferram; /* Memory used by buffers */ 5681da177e4SLinus Torvalds unsigned long totalswap; /* Total swap space size */ 5691da177e4SLinus Torvalds unsigned long freeswap; /* swap space still available */ 5701da177e4SLinus Torvalds unsigned short procs; /* Number of current processes */ 5711da177e4SLinus Torvalds unsigned short pad; /* explicit padding for m68k */ 5721da177e4SLinus Torvalds unsigned long totalhigh; /* Total high memory size */ 5731da177e4SLinus Torvalds unsigned long freehigh; /* Available high memory size */ 5741da177e4SLinus Torvalds unsigned int mem_unit; /* Memory unit size in bytes */ 5751da177e4SLinus Torvalds char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 5761da177e4SLinus Torvalds }; 5771da177e4SLinus Torvalds 578c0398ee6SNikita Danilov /* Force a compilation error if condition is true */ 579921717a2SAndi Kleen #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 5801da177e4SLinus Torvalds 5814552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a 5824552d5dcSJan Beulich result (of value 0 and type size_t), so the expression can be used 5834552d5dcSJan Beulich e.g. in a structure initializer (or where-ever else comma expressions 5844552d5dcSJan Beulich aren't permitted). */ 5854552d5dcSJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 5864552d5dcSJan Beulich 5871da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */ 5881da177e4SLinus Torvalds #define __FUNCTION__ (__func__) 5891da177e4SLinus Torvalds 59008e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */ 59108e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA 59208e0f6a9SChristoph Lameter #define NUMA_BUILD 1 59308e0f6a9SChristoph Lameter #else 59408e0f6a9SChristoph Lameter #define NUMA_BUILD 0 59508e0f6a9SChristoph Lameter #endif 59608e0f6a9SChristoph Lameter 59729e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 59829e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD 59929e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 60029e71abfSSteven Rostedt #endif 60129e71abfSSteven Rostedt 6021da177e4SLinus Torvalds #endif 603