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> 171da177e4SLinus Torvalds #include <asm/byteorder.h> 181da177e4SLinus Torvalds #include <asm/bug.h> 191da177e4SLinus Torvalds 203eb3c740SRoman Zippel extern const char linux_banner[]; 213eb3c740SRoman Zippel extern const char linux_proc_banner[]; 223eb3c740SRoman Zippel 2344f564a4SZhang, Yanmin #define USHORT_MAX ((u16)(~0U)) 2444f564a4SZhang, Yanmin #define SHORT_MAX ((s16)(USHORT_MAX>>1)) 2544f564a4SZhang, Yanmin #define SHORT_MIN (-SHORT_MAX - 1) 261da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 271da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 281da177e4SLinus Torvalds #define UINT_MAX (~0U) 291da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 301da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 311da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 32111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 33111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 34111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 371da177e4SLinus Torvalds 382ea58144SLinus Torvalds #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 392ea58144SLinus Torvalds #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 40a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 41f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 422ea58144SLinus Torvalds 43c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 44c5e631cfSRusty Russell 454552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 46930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 47b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 481da177e4SLinus Torvalds 492da96acdSJens Axboe #ifdef CONFIG_LBD 502da96acdSJens Axboe # include <asm/div64.h> 512da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 522da96acdSJens Axboe #else 532da96acdSJens Axboe # define sector_div(n, b)( \ 542da96acdSJens Axboe { \ 552da96acdSJens Axboe int _res; \ 562da96acdSJens Axboe _res = (n) % (b); \ 572da96acdSJens Axboe (n) /= (b); \ 582da96acdSJens Axboe _res; \ 592da96acdSJens Axboe } \ 602da96acdSJens Axboe ) 612da96acdSJens Axboe #endif 622da96acdSJens Axboe 63218e180eSAndrew Morton /** 64218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 65218e180eSAndrew Morton * @n: the number we're accessing 66218e180eSAndrew Morton * 67218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 68218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 69218e180eSAndrew Morton * 32-bits. 70218e180eSAndrew Morton */ 71218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 72218e180eSAndrew Morton 731da177e4SLinus Torvalds #define KERN_EMERG "<0>" /* system is unusable */ 741da177e4SLinus Torvalds #define KERN_ALERT "<1>" /* action must be taken immediately */ 751da177e4SLinus Torvalds #define KERN_CRIT "<2>" /* critical conditions */ 761da177e4SLinus Torvalds #define KERN_ERR "<3>" /* error conditions */ 771da177e4SLinus Torvalds #define KERN_WARNING "<4>" /* warning conditions */ 781da177e4SLinus Torvalds #define KERN_NOTICE "<5>" /* normal but significant condition */ 791da177e4SLinus Torvalds #define KERN_INFO "<6>" /* informational */ 801da177e4SLinus Torvalds #define KERN_DEBUG "<7>" /* debug-level messages */ 811da177e4SLinus Torvalds 8247492527SIngo Molnar /* 8347492527SIngo Molnar * Annotation for a "continued" line of log printout (only done after a 8447492527SIngo Molnar * line that had no enclosing \n). Only to be used by core/arch code 8547492527SIngo Molnar * during early bootup (a continued line is not SMP-safe otherwise). 8647492527SIngo Molnar */ 8747492527SIngo Molnar #define KERN_CONT "" 8847492527SIngo Molnar 891da177e4SLinus Torvalds extern int console_printk[]; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds #define console_loglevel (console_printk[0]) 921da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1]) 931da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2]) 941da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3]) 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds struct completion; 97df2e71fbS[email protected] struct pt_regs; 98df2e71fbS[email protected] struct user; 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /** 1011da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1021da177e4SLinus Torvalds * 1031da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1041da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1051da177e4SLinus Torvalds * 1061da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 107e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1081da177e4SLinus Torvalds * supposed to. 1091da177e4SLinus Torvalds */ 110f8cbd99bSIngo Molnar #ifdef CONFIG_PREEMPT_VOLUNTARY 11102b67cc3SHerbert Xu extern int _cond_resched(void); 11202b67cc3SHerbert Xu # define might_resched() _cond_resched() 1131da177e4SLinus Torvalds #else 114f8cbd99bSIngo Molnar # define might_resched() do { } while (0) 1151da177e4SLinus Torvalds #endif 1161da177e4SLinus Torvalds 117f8cbd99bSIngo Molnar #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 118f8cbd99bSIngo Molnar void __might_sleep(char *file, int line); 119f8cbd99bSIngo Molnar # define might_sleep() \ 120f8cbd99bSIngo Molnar do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 121f8cbd99bSIngo Molnar #else 122f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 123f8cbd99bSIngo Molnar #endif 124f8cbd99bSIngo Molnar 125368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 126f8cbd99bSIngo Molnar 1271da177e4SLinus Torvalds #define abs(x) ({ \ 1281da177e4SLinus Torvalds int __x = (x); \ 1291da177e4SLinus Torvalds (__x < 0) ? -__x : __x; \ 1301da177e4SLinus Torvalds }) 1311da177e4SLinus Torvalds 132e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 1331da177e4SLinus Torvalds extern long (*panic_blink)(long time); 1341da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 135a586df06SAndi Kleen __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 136dd287796SAndrew Morton extern void oops_enter(void); 137dd287796SAndrew Morton extern void oops_exit(void); 138dd287796SAndrew Morton extern int oops_may_print(void); 139ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code) 1401da177e4SLinus Torvalds ATTRIB_NORET; 1411da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long) 1421da177e4SLinus Torvalds ATTRIB_NORET; 1431da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 1441da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 1451da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 1461da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 14706b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *); 14806b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *); 14906b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 15006b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *); 1511da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...) 1521da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 3))); 1531da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list) 1541da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 0))); 1551da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...) 1561da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1571da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1581da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 1591da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 1601da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1611da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 1621da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 163e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 164e905914fSJeremy Fitzhardinge __attribute__ ((format (printf, 2, 3))); 16511443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...) 1681da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 3))); 1691da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list) 1701da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 0))); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 1731da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 1741da177e4SLinus Torvalds extern unsigned long long memparse(char *ptr, char **retptr); 1751da177e4SLinus Torvalds 1765e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 1771da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 1781da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 17904a2e6a5SEric W. Biederman struct pid; 18004a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 1811da177e4SLinus Torvalds 182d59745ceSMatt Mackall #ifdef CONFIG_PRINTK 1831da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args) 1841da177e4SLinus Torvalds __attribute__ ((format (printf, 1, 0))); 1851da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...) 186a586df06SAndi Kleen __attribute__ ((format (printf, 1, 2))) __cold; 1870b15d04aSMike Frysinger extern int log_buf_get_len(void); 1880b15d04aSMike Frysinger extern int log_buf_read(int idx); 1890b15d04aSMike Frysinger extern int log_buf_copy(char *dest, int idx, int len); 1907ef3d2fdSJoe Perches 1917ef3d2fdSJoe Perches extern int printk_ratelimit_jiffies; 1927ef3d2fdSJoe Perches extern int printk_ratelimit_burst; 1937ef3d2fdSJoe Perches extern int printk_ratelimit(void); 1945f97a5a8SDave Young extern int __ratelimit(int ratelimit_jiffies, int ratelimit_burst); 1957ef3d2fdSJoe Perches extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst); 1967ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 1977ef3d2fdSJoe Perches unsigned int interval_msec); 198d59745ceSMatt Mackall #else 199d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) 200d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 0))); 201d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; } 202d59745ceSMatt Mackall static inline int printk(const char *s, ...) 203d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 2))); 204a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; } 2050b15d04aSMike Frysinger static inline int log_buf_get_len(void) { return 0; } 2060b15d04aSMike Frysinger static inline int log_buf_read(int idx) { return 0; } 2070b15d04aSMike Frysinger static inline int log_buf_copy(char *dest, int idx, int len) { return 0; } 2087ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; } 2097ef3d2fdSJoe Perches static inline int __printk_ratelimit(int ratelimit_jiffies, \ 2107ef3d2fdSJoe Perches int ratelimit_burst) { return 0; } 2117ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 2127ef3d2fdSJoe Perches unsigned int interval_msec) \ 2137ef3d2fdSJoe Perches { return false; } 214d59745ceSMatt Mackall #endif 2151da177e4SLinus Torvalds 216076f9776SIngo Molnar extern void __attribute__((format(printf, 1, 2))) 217076f9776SIngo Molnar early_printk(const char *fmt, ...); 218076f9776SIngo Molnar 2191da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds static inline void console_silent(void) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds console_loglevel = 0; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static inline void console_verbose(void) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds if (console_loglevel) 2291da177e4SLinus Torvalds console_loglevel = 15; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 233e3e8a75dSKirill Korotaev extern void wake_up_klogd(void); 2341da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 235aa727107SAdrian Bunk extern int panic_timeout; 2361da177e4SLinus Torvalds extern int panic_on_oops; 2378da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 2381da177e4SLinus Torvalds extern int tainted; 2391da177e4SLinus Torvalds extern const char *print_tainted(void); 2401da177e4SLinus Torvalds extern void add_taint(unsigned); 241b920de1bSDavid Howells extern int root_mountflags; 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /* Values used for system_state */ 2441da177e4SLinus Torvalds extern enum system_states { 2451da177e4SLinus Torvalds SYSTEM_BOOTING, 2461da177e4SLinus Torvalds SYSTEM_RUNNING, 2471da177e4SLinus Torvalds SYSTEM_HALT, 2481da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2491da177e4SLinus Torvalds SYSTEM_RESTART, 250729b4d4cSAlexey Starikovskiy SYSTEM_SUSPEND_DISK, 2511da177e4SLinus Torvalds } system_state; 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds #define TAINT_PROPRIETARY_MODULE (1<<0) 2541da177e4SLinus Torvalds #define TAINT_FORCED_MODULE (1<<1) 2551da177e4SLinus Torvalds #define TAINT_UNSAFE_SMP (1<<2) 2561da177e4SLinus Torvalds #define TAINT_FORCED_RMMOD (1<<3) 2571da177e4SLinus Torvalds #define TAINT_MACHINE_CHECK (1<<4) 2581da177e4SLinus Torvalds #define TAINT_BAD_PAGE (1<<5) 25934f5a398STheodore Ts'o #define TAINT_USER (1<<6) 260bcdcd8e7SPavel Emelianov #define TAINT_DIE (1<<7) 2616ed31e92SÉric Piel #define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8) 26295b570c9SNur Hussein #define TAINT_WARN (1<<9) 2631da177e4SLinus Torvalds 264a586df06SAndi Kleen extern void dump_stack(void) __cold; 2651da177e4SLinus Torvalds 26699eaf3c4SRandy Dunlap enum { 26799eaf3c4SRandy Dunlap DUMP_PREFIX_NONE, 26899eaf3c4SRandy Dunlap DUMP_PREFIX_ADDRESS, 26999eaf3c4SRandy Dunlap DUMP_PREFIX_OFFSET 27099eaf3c4SRandy Dunlap }; 271c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len, 272c7909234SRandy Dunlap int rowsize, int groupsize, 273c7909234SRandy Dunlap char *linebuf, size_t linebuflen, bool ascii); 274c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str, 275c7909234SRandy Dunlap int prefix_type, int rowsize, int groupsize, 2766a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii); 277c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 278eb9a9a56SAlan Stern const void *buf, size_t len); 279*3fc95772SHarvey Harrison 280*3fc95772SHarvey Harrison extern const char hex_asc[]; 281*3fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 282*3fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 283*3fc95772SHarvey Harrison 284*3fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte) 285*3fc95772SHarvey Harrison { 286*3fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 287*3fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 288*3fc95772SHarvey Harrison return buf; 289*3fc95772SHarvey Harrison } 29099eaf3c4SRandy Dunlap 2911f7c8234SEmil Medve #define pr_emerg(fmt, arg...) \ 2921f7c8234SEmil Medve printk(KERN_EMERG fmt, ##arg) 2931f7c8234SEmil Medve #define pr_alert(fmt, arg...) \ 2941f7c8234SEmil Medve printk(KERN_ALERT fmt, ##arg) 2951f7c8234SEmil Medve #define pr_crit(fmt, arg...) \ 2961f7c8234SEmil Medve printk(KERN_CRIT fmt, ##arg) 2971f7c8234SEmil Medve #define pr_err(fmt, arg...) \ 2981f7c8234SEmil Medve printk(KERN_ERR fmt, ##arg) 2991f7c8234SEmil Medve #define pr_warning(fmt, arg...) \ 3001f7c8234SEmil Medve printk(KERN_WARNING fmt, ##arg) 3011f7c8234SEmil Medve #define pr_notice(fmt, arg...) \ 3021f7c8234SEmil Medve printk(KERN_NOTICE fmt, ##arg) 3031f7c8234SEmil Medve #define pr_info(fmt, arg...) \ 3041f7c8234SEmil Medve printk(KERN_INFO fmt, ##arg) 3051f7c8234SEmil Medve 3061da177e4SLinus Torvalds #ifdef DEBUG 3071cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */ 3081da177e4SLinus Torvalds #define pr_debug(fmt, arg...) \ 3091da177e4SLinus Torvalds printk(KERN_DEBUG fmt, ##arg) 3101da177e4SLinus Torvalds #else 3111429db83SJoe Perches #define pr_debug(fmt, arg...) \ 3121429db83SJoe Perches ({ if (0) printk(KERN_DEBUG fmt, ##arg); 0; }) 3131da177e4SLinus Torvalds #endif 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 3161da177e4SLinus Torvalds * Display an IP address in readable format. 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds #define NIPQUAD(addr) \ 3201da177e4SLinus Torvalds ((unsigned char *)&addr)[0], \ 3211da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3221da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3231da177e4SLinus Torvalds ((unsigned char *)&addr)[3] 32446b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u" 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds #define NIP6(addr) \ 3271da177e4SLinus Torvalds ntohs((addr).s6_addr16[0]), \ 3281da177e4SLinus Torvalds ntohs((addr).s6_addr16[1]), \ 3291da177e4SLinus Torvalds ntohs((addr).s6_addr16[2]), \ 3301da177e4SLinus Torvalds ntohs((addr).s6_addr16[3]), \ 3311da177e4SLinus Torvalds ntohs((addr).s6_addr16[4]), \ 3321da177e4SLinus Torvalds ntohs((addr).s6_addr16[5]), \ 3331da177e4SLinus Torvalds ntohs((addr).s6_addr16[6]), \ 3341da177e4SLinus Torvalds ntohs((addr).s6_addr16[7]) 33546b86a2dSJoe Perches #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" 3369343e79aSYOSHIFUJI Hideaki #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x" 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN) 3391da177e4SLinus Torvalds #define HIPQUAD(addr) \ 3401da177e4SLinus Torvalds ((unsigned char *)&addr)[3], \ 3411da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3421da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3431da177e4SLinus Torvalds ((unsigned char *)&addr)[0] 3441da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN) 3451da177e4SLinus Torvalds #define HIPQUAD NIPQUAD 3461da177e4SLinus Torvalds #else 3471da177e4SLinus Torvalds #error "Please fix asm/byteorder.h" 3481da177e4SLinus Torvalds #endif /* __LITTLE_ENDIAN */ 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* 351bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 3521da177e4SLinus Torvalds * strict type-checking.. See the 3531da177e4SLinus Torvalds * "unnecessary" pointer comparison. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds #define min(x, y) ({ \ 356bdf4bbaaSHarvey Harrison typeof(x) _min1 = (x); \ 357bdf4bbaaSHarvey Harrison typeof(y) _min2 = (y); \ 358bdf4bbaaSHarvey Harrison (void) (&_min1 == &_min2); \ 359bdf4bbaaSHarvey Harrison _min1 < _min2 ? _min1 : _min2; }) 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds #define max(x, y) ({ \ 362bdf4bbaaSHarvey Harrison typeof(x) _max1 = (x); \ 363bdf4bbaaSHarvey Harrison typeof(y) _max2 = (y); \ 364bdf4bbaaSHarvey Harrison (void) (&_max1 == &_max2); \ 365bdf4bbaaSHarvey Harrison _max1 > _max2 ? _max1 : _max2; }) 366bdf4bbaaSHarvey Harrison 367bdf4bbaaSHarvey Harrison /** 368bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 369bdf4bbaaSHarvey Harrison * @val: current value 370bdf4bbaaSHarvey Harrison * @min: minimum allowable value 371bdf4bbaaSHarvey Harrison * @max: maximum allowable value 372bdf4bbaaSHarvey Harrison * 373bdf4bbaaSHarvey Harrison * This macro does strict typechecking of min/max to make sure they are of the 374bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 375bdf4bbaaSHarvey Harrison */ 376bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({ \ 377bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 378bdf4bbaaSHarvey Harrison typeof(min) __min = (min); \ 379bdf4bbaaSHarvey Harrison typeof(max) __max = (max); \ 380bdf4bbaaSHarvey Harrison (void) (&__val == &__min); \ 381bdf4bbaaSHarvey Harrison (void) (&__val == &__max); \ 382bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 383bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 3841da177e4SLinus Torvalds 3851da177e4SLinus Torvalds /* 3861da177e4SLinus Torvalds * ..and if you can't take the strict 3871da177e4SLinus Torvalds * types, you can specify one yourself. 3881da177e4SLinus Torvalds * 389bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 3901da177e4SLinus Torvalds */ 391bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({ \ 392bdf4bbaaSHarvey Harrison type __min1 = (x); \ 393bdf4bbaaSHarvey Harrison type __min2 = (y); \ 394bdf4bbaaSHarvey Harrison __min1 < __min2 ? __min1: __min2; }) 3951da177e4SLinus Torvalds 396bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({ \ 397bdf4bbaaSHarvey Harrison type __max1 = (x); \ 398bdf4bbaaSHarvey Harrison type __max2 = (y); \ 399bdf4bbaaSHarvey Harrison __max1 > __max2 ? __max1: __max2; }) 400bdf4bbaaSHarvey Harrison 401bdf4bbaaSHarvey Harrison /** 402bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 403bdf4bbaaSHarvey Harrison * @type: the type of variable to use 404bdf4bbaaSHarvey Harrison * @val: current value 405bdf4bbaaSHarvey Harrison * @min: minimum allowable value 406bdf4bbaaSHarvey Harrison * @max: maximum allowable value 407bdf4bbaaSHarvey Harrison * 408bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 409bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 410bdf4bbaaSHarvey Harrison */ 411bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({ \ 412bdf4bbaaSHarvey Harrison type __val = (val); \ 413bdf4bbaaSHarvey Harrison type __min = (min); \ 414bdf4bbaaSHarvey Harrison type __max = (max); \ 415bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 416bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 417bdf4bbaaSHarvey Harrison 418bdf4bbaaSHarvey Harrison /** 419bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 420bdf4bbaaSHarvey Harrison * @val: current value 421bdf4bbaaSHarvey Harrison * @min: minimum allowable value 422bdf4bbaaSHarvey Harrison * @max: maximum allowable value 423bdf4bbaaSHarvey Harrison * 424bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 425bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 426bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 427bdf4bbaaSHarvey Harrison * integer type. 428bdf4bbaaSHarvey Harrison */ 429bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({ \ 430bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 431bdf4bbaaSHarvey Harrison typeof(val) __min = (min); \ 432bdf4bbaaSHarvey Harrison typeof(val) __max = (max); \ 433bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 434bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /** 4371da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 4381da177e4SLinus Torvalds * @ptr: the pointer to the member. 4391da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 4401da177e4SLinus Torvalds * @member: the name of the member within the struct. 4411da177e4SLinus Torvalds * 4421da177e4SLinus Torvalds */ 4431da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 4441da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 4451da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds /* 4481da177e4SLinus Torvalds * Check at compile time that something is of a particular type. 4491da177e4SLinus Torvalds * Always evaluates to 1 so you may use it easily in comparisons. 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds #define typecheck(type,x) \ 4521da177e4SLinus Torvalds ({ type __dummy; \ 4531da177e4SLinus Torvalds typeof(x) __dummy2; \ 4541da177e4SLinus Torvalds (void)(&__dummy == &__dummy2); \ 4551da177e4SLinus Torvalds 1; \ 4561da177e4SLinus Torvalds }) 4571da177e4SLinus Torvalds 458711a660dSChuck Ebbert /* 459711a660dSChuck Ebbert * Check at compile time that 'function' is a certain type, or is a pointer 460711a660dSChuck Ebbert * to that type (needs to use typedef for the function type.) 461711a660dSChuck Ebbert */ 462711a660dSChuck Ebbert #define typecheck_fn(type,function) \ 463711a660dSChuck Ebbert ({ typeof(type) __tmp = function; \ 464711a660dSChuck Ebbert (void)__tmp; \ 465711a660dSChuck Ebbert }) 466711a660dSChuck Ebbert 467d4d23addSKyle McMartin struct sysinfo; 468d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info); 469d4d23addSKyle McMartin 4701da177e4SLinus Torvalds #endif /* __KERNEL__ */ 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds #define SI_LOAD_SHIFT 16 4731da177e4SLinus Torvalds struct sysinfo { 4741da177e4SLinus Torvalds long uptime; /* Seconds since boot */ 4751da177e4SLinus Torvalds unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 4761da177e4SLinus Torvalds unsigned long totalram; /* Total usable main memory size */ 4771da177e4SLinus Torvalds unsigned long freeram; /* Available memory size */ 4781da177e4SLinus Torvalds unsigned long sharedram; /* Amount of shared memory */ 4791da177e4SLinus Torvalds unsigned long bufferram; /* Memory used by buffers */ 4801da177e4SLinus Torvalds unsigned long totalswap; /* Total swap space size */ 4811da177e4SLinus Torvalds unsigned long freeswap; /* swap space still available */ 4821da177e4SLinus Torvalds unsigned short procs; /* Number of current processes */ 4831da177e4SLinus Torvalds unsigned short pad; /* explicit padding for m68k */ 4841da177e4SLinus Torvalds unsigned long totalhigh; /* Total high memory size */ 4851da177e4SLinus Torvalds unsigned long freehigh; /* Available high memory size */ 4861da177e4SLinus Torvalds unsigned int mem_unit; /* Memory unit size in bytes */ 4871da177e4SLinus Torvalds char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 4881da177e4SLinus Torvalds }; 4891da177e4SLinus Torvalds 490c0398ee6SNikita Danilov /* Force a compilation error if condition is true */ 491921717a2SAndi Kleen #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 4921da177e4SLinus Torvalds 4934552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a 4944552d5dcSJan Beulich result (of value 0 and type size_t), so the expression can be used 4954552d5dcSJan Beulich e.g. in a structure initializer (or where-ever else comma expressions 4964552d5dcSJan Beulich aren't permitted). */ 4974552d5dcSJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 4984552d5dcSJan Beulich 4991da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */ 5001da177e4SLinus Torvalds #define __FUNCTION__ (__func__) 5011da177e4SLinus Torvalds 50208e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */ 50308e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA 50408e0f6a9SChristoph Lameter #define NUMA_BUILD 1 50508e0f6a9SChristoph Lameter #else 50608e0f6a9SChristoph Lameter #define NUMA_BUILD 0 50708e0f6a9SChristoph Lameter #endif 50808e0f6a9SChristoph Lameter 5091da177e4SLinus Torvalds #endif 510