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> 191da177e4SLinus Torvalds #include <asm/byteorder.h> 201da177e4SLinus Torvalds #include <asm/bug.h> 211da177e4SLinus Torvalds 223eb3c740SRoman Zippel extern const char linux_banner[]; 233eb3c740SRoman Zippel extern const char linux_proc_banner[]; 243eb3c740SRoman Zippel 2544f564a4SZhang, Yanmin #define USHORT_MAX ((u16)(~0U)) 2644f564a4SZhang, Yanmin #define SHORT_MAX ((s16)(USHORT_MAX>>1)) 2744f564a4SZhang, Yanmin #define SHORT_MIN (-SHORT_MAX - 1) 281da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 291da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 301da177e4SLinus Torvalds #define UINT_MAX (~0U) 311da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 321da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 331da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 34111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 35111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 36111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 391da177e4SLinus Torvalds 402ea58144SLinus Torvalds #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 412ea58144SLinus Torvalds #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 42a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 43f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 442ea58144SLinus Torvalds 45c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 46c5e631cfSRusty Russell 474552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 48930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 49b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 501da177e4SLinus Torvalds 51ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 52ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 53ca31e146SEduard - Gabriel Munteanu 542da96acdSJens Axboe #ifdef CONFIG_LBD 552da96acdSJens Axboe # include <asm/div64.h> 562da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 572da96acdSJens Axboe #else 582da96acdSJens Axboe # define sector_div(n, b)( \ 592da96acdSJens Axboe { \ 602da96acdSJens Axboe int _res; \ 612da96acdSJens Axboe _res = (n) % (b); \ 622da96acdSJens Axboe (n) /= (b); \ 632da96acdSJens Axboe _res; \ 642da96acdSJens Axboe } \ 652da96acdSJens Axboe ) 662da96acdSJens Axboe #endif 672da96acdSJens Axboe 68218e180eSAndrew Morton /** 69218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 70218e180eSAndrew Morton * @n: the number we're accessing 71218e180eSAndrew Morton * 72218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 73218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 74218e180eSAndrew Morton * 32-bits. 75218e180eSAndrew Morton */ 76218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 77218e180eSAndrew Morton 78204b885eSJoerg Roedel /** 79204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 80204b885eSJoerg Roedel * @n: the number we're accessing 81204b885eSJoerg Roedel */ 82204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n)) 83204b885eSJoerg Roedel 841da177e4SLinus Torvalds #define KERN_EMERG "<0>" /* system is unusable */ 851da177e4SLinus Torvalds #define KERN_ALERT "<1>" /* action must be taken immediately */ 861da177e4SLinus Torvalds #define KERN_CRIT "<2>" /* critical conditions */ 871da177e4SLinus Torvalds #define KERN_ERR "<3>" /* error conditions */ 881da177e4SLinus Torvalds #define KERN_WARNING "<4>" /* warning conditions */ 891da177e4SLinus Torvalds #define KERN_NOTICE "<5>" /* normal but significant condition */ 901da177e4SLinus Torvalds #define KERN_INFO "<6>" /* informational */ 911da177e4SLinus Torvalds #define KERN_DEBUG "<7>" /* debug-level messages */ 921da177e4SLinus Torvalds 9347492527SIngo Molnar /* 9447492527SIngo Molnar * Annotation for a "continued" line of log printout (only done after a 9547492527SIngo Molnar * line that had no enclosing \n). Only to be used by core/arch code 9647492527SIngo Molnar * during early bootup (a continued line is not SMP-safe otherwise). 9747492527SIngo Molnar */ 9847492527SIngo Molnar #define KERN_CONT "" 9947492527SIngo Molnar 1001da177e4SLinus Torvalds extern int console_printk[]; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds #define console_loglevel (console_printk[0]) 1031da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1]) 1041da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2]) 1051da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3]) 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds struct completion; 108df2e71fbS[email protected] struct pt_regs; 109df2e71fbS[email protected] struct user; 1101da177e4SLinus Torvalds 111070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 112070cb065SUwe Kleine-König extern int _cond_resched(void); 113070cb065SUwe Kleine-König # define might_resched() _cond_resched() 114070cb065SUwe Kleine-König #else 115070cb065SUwe Kleine-König # define might_resched() do { } while (0) 116070cb065SUwe Kleine-König #endif 117070cb065SUwe Kleine-König 1181da177e4SLinus Torvalds /** 1191da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1201da177e4SLinus Torvalds * 1211da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1221da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1231da177e4SLinus Torvalds * 1241da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 125e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1261da177e4SLinus Torvalds * supposed to. 1271da177e4SLinus Torvalds */ 128f8cbd99bSIngo Molnar #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 129f8cbd99bSIngo Molnar void __might_sleep(char *file, int line); 130f8cbd99bSIngo Molnar # define might_sleep() \ 131f8cbd99bSIngo Molnar do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 132f8cbd99bSIngo Molnar #else 133f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 134f8cbd99bSIngo Molnar #endif 135f8cbd99bSIngo Molnar 136368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 137f8cbd99bSIngo Molnar 1381da177e4SLinus Torvalds #define abs(x) ({ \ 1391da177e4SLinus Torvalds int __x = (x); \ 1401da177e4SLinus Torvalds (__x < 0) ? -__x : __x; \ 1411da177e4SLinus Torvalds }) 1421da177e4SLinus Torvalds 143*3ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 144*3ee1afa3SNick Piggin void might_fault(void); 145*3ee1afa3SNick Piggin #else 146*3ee1afa3SNick Piggin static inline void might_fault(void) 147*3ee1afa3SNick Piggin { 148*3ee1afa3SNick Piggin might_sleep(); 149*3ee1afa3SNick Piggin } 150*3ee1afa3SNick Piggin #endif 151*3ee1afa3SNick Piggin 152e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 1531da177e4SLinus Torvalds extern long (*panic_blink)(long time); 1541da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 155a586df06SAndi Kleen __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 156dd287796SAndrew Morton extern void oops_enter(void); 157dd287796SAndrew Morton extern void oops_exit(void); 158dd287796SAndrew Morton extern int oops_may_print(void); 159ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code) 1601da177e4SLinus Torvalds ATTRIB_NORET; 1611da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long) 1621da177e4SLinus Torvalds ATTRIB_NORET; 1631da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 1641da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 1651da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 1661da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 16706b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *); 16806b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *); 16906b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 17006b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *); 1711da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...) 1721da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 3))); 1731da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list) 1741da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 0))); 1751da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...) 1761da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1771da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1781da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 1791da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 1801da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1811da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 1821da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 183e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 184e905914fSJeremy Fitzhardinge __attribute__ ((format (printf, 2, 3))); 18511443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...) 1881da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 3))); 1891da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list) 1901da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 0))); 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 1931da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 1941da177e4SLinus Torvalds extern unsigned long long memparse(char *ptr, char **retptr); 1951da177e4SLinus Torvalds 1965e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 1971da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 1981da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 19904a2e6a5SEric W. Biederman struct pid; 20004a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 2011da177e4SLinus Torvalds 202d59745ceSMatt Mackall #ifdef CONFIG_PRINTK 2031da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args) 2041da177e4SLinus Torvalds __attribute__ ((format (printf, 1, 0))); 2051da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...) 206a586df06SAndi Kleen __attribute__ ((format (printf, 1, 2))) __cold; 2077ef3d2fdSJoe Perches 208717115e1SDave Young extern struct ratelimit_state printk_ratelimit_state; 2097ef3d2fdSJoe Perches extern int printk_ratelimit(void); 2107ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 2117ef3d2fdSJoe Perches unsigned int interval_msec); 212d59745ceSMatt Mackall #else 213d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) 214d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 0))); 215d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; } 216d59745ceSMatt Mackall static inline int printk(const char *s, ...) 217d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 2))); 218a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; } 2197ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; } 2207ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 2217ef3d2fdSJoe Perches unsigned int interval_msec) \ 2227ef3d2fdSJoe Perches { return false; } 223d59745ceSMatt Mackall #endif 2241da177e4SLinus Torvalds 225e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2))) 226076f9776SIngo Molnar early_printk(const char *fmt, ...); 227076f9776SIngo Molnar 2281da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static inline void console_silent(void) 2311da177e4SLinus Torvalds { 2321da177e4SLinus Torvalds console_loglevel = 0; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds static inline void console_verbose(void) 2361da177e4SLinus Torvalds { 2371da177e4SLinus Torvalds if (console_loglevel) 2381da177e4SLinus Torvalds console_loglevel = 15; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 242e3e8a75dSKirill Korotaev extern void wake_up_klogd(void); 2431da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 244aa727107SAdrian Bunk extern int panic_timeout; 2451da177e4SLinus Torvalds extern int panic_on_oops; 2468da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 2471da177e4SLinus Torvalds extern int tainted; 2481da177e4SLinus Torvalds extern const char *print_tainted(void); 2491da177e4SLinus Torvalds extern void add_taint(unsigned); 250b920de1bSDavid Howells extern int root_mountflags; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* Values used for system_state */ 2531da177e4SLinus Torvalds extern enum system_states { 2541da177e4SLinus Torvalds SYSTEM_BOOTING, 2551da177e4SLinus Torvalds SYSTEM_RUNNING, 2561da177e4SLinus Torvalds SYSTEM_HALT, 2571da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2581da177e4SLinus Torvalds SYSTEM_RESTART, 259729b4d4cSAlexey Starikovskiy SYSTEM_SUSPEND_DISK, 2601da177e4SLinus Torvalds } system_state; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds #define TAINT_PROPRIETARY_MODULE (1<<0) 2631da177e4SLinus Torvalds #define TAINT_FORCED_MODULE (1<<1) 2641da177e4SLinus Torvalds #define TAINT_UNSAFE_SMP (1<<2) 2651da177e4SLinus Torvalds #define TAINT_FORCED_RMMOD (1<<3) 2661da177e4SLinus Torvalds #define TAINT_MACHINE_CHECK (1<<4) 2671da177e4SLinus Torvalds #define TAINT_BAD_PAGE (1<<5) 26834f5a398STheodore Ts'o #define TAINT_USER (1<<6) 269bcdcd8e7SPavel Emelianov #define TAINT_DIE (1<<7) 2706ed31e92SÉric Piel #define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8) 27195b570c9SNur Hussein #define TAINT_WARN (1<<9) 2721da177e4SLinus Torvalds 273a586df06SAndi Kleen extern void dump_stack(void) __cold; 2741da177e4SLinus Torvalds 27599eaf3c4SRandy Dunlap enum { 27699eaf3c4SRandy Dunlap DUMP_PREFIX_NONE, 27799eaf3c4SRandy Dunlap DUMP_PREFIX_ADDRESS, 27899eaf3c4SRandy Dunlap DUMP_PREFIX_OFFSET 27999eaf3c4SRandy Dunlap }; 280c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len, 281c7909234SRandy Dunlap int rowsize, int groupsize, 282c7909234SRandy Dunlap char *linebuf, size_t linebuflen, bool ascii); 283c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str, 284c7909234SRandy Dunlap int prefix_type, int rowsize, int groupsize, 2856a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii); 286c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 287eb9a9a56SAlan Stern const void *buf, size_t len); 2883fc95772SHarvey Harrison 2893fc95772SHarvey Harrison extern const char hex_asc[]; 2903fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 2913fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 2923fc95772SHarvey Harrison 2933fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte) 2943fc95772SHarvey Harrison { 2953fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 2963fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 2973fc95772SHarvey Harrison return buf; 2983fc95772SHarvey Harrison } 29999eaf3c4SRandy Dunlap 3001f7c8234SEmil Medve #define pr_emerg(fmt, arg...) \ 3011f7c8234SEmil Medve printk(KERN_EMERG fmt, ##arg) 3021f7c8234SEmil Medve #define pr_alert(fmt, arg...) \ 3031f7c8234SEmil Medve printk(KERN_ALERT fmt, ##arg) 3041f7c8234SEmil Medve #define pr_crit(fmt, arg...) \ 3051f7c8234SEmil Medve printk(KERN_CRIT fmt, ##arg) 3061f7c8234SEmil Medve #define pr_err(fmt, arg...) \ 3071f7c8234SEmil Medve printk(KERN_ERR fmt, ##arg) 3081f7c8234SEmil Medve #define pr_warning(fmt, arg...) \ 3091f7c8234SEmil Medve printk(KERN_WARNING fmt, ##arg) 3101f7c8234SEmil Medve #define pr_notice(fmt, arg...) \ 3111f7c8234SEmil Medve printk(KERN_NOTICE fmt, ##arg) 3121f7c8234SEmil Medve #define pr_info(fmt, arg...) \ 3131f7c8234SEmil Medve printk(KERN_INFO fmt, ##arg) 3141f7c8234SEmil Medve 3151da177e4SLinus Torvalds #ifdef DEBUG 3161cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */ 3171da177e4SLinus Torvalds #define pr_debug(fmt, arg...) \ 3181da177e4SLinus Torvalds printk(KERN_DEBUG fmt, ##arg) 3191da177e4SLinus Torvalds #else 3201429db83SJoe Perches #define pr_debug(fmt, arg...) \ 3211429db83SJoe Perches ({ if (0) printk(KERN_DEBUG fmt, ##arg); 0; }) 3221da177e4SLinus Torvalds #endif 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* 3251da177e4SLinus Torvalds * Display an IP address in readable format. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds #define NIPQUAD(addr) \ 3291da177e4SLinus Torvalds ((unsigned char *)&addr)[0], \ 3301da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3311da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3321da177e4SLinus Torvalds ((unsigned char *)&addr)[3] 33346b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u" 3341da177e4SLinus Torvalds 3351da177e4SLinus Torvalds #define NIP6(addr) \ 3361da177e4SLinus Torvalds ntohs((addr).s6_addr16[0]), \ 3371da177e4SLinus Torvalds ntohs((addr).s6_addr16[1]), \ 3381da177e4SLinus Torvalds ntohs((addr).s6_addr16[2]), \ 3391da177e4SLinus Torvalds ntohs((addr).s6_addr16[3]), \ 3401da177e4SLinus Torvalds ntohs((addr).s6_addr16[4]), \ 3411da177e4SLinus Torvalds ntohs((addr).s6_addr16[5]), \ 3421da177e4SLinus Torvalds ntohs((addr).s6_addr16[6]), \ 3431da177e4SLinus Torvalds ntohs((addr).s6_addr16[7]) 34446b86a2dSJoe Perches #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" 3459343e79aSYOSHIFUJI Hideaki #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x" 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN) 3481da177e4SLinus Torvalds #define HIPQUAD(addr) \ 3491da177e4SLinus Torvalds ((unsigned char *)&addr)[3], \ 3501da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3511da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3521da177e4SLinus Torvalds ((unsigned char *)&addr)[0] 3531da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN) 3541da177e4SLinus Torvalds #define HIPQUAD NIPQUAD 3551da177e4SLinus Torvalds #else 3561da177e4SLinus Torvalds #error "Please fix asm/byteorder.h" 3571da177e4SLinus Torvalds #endif /* __LITTLE_ENDIAN */ 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* 360bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 3611da177e4SLinus Torvalds * strict type-checking.. See the 3621da177e4SLinus Torvalds * "unnecessary" pointer comparison. 3631da177e4SLinus Torvalds */ 3641da177e4SLinus Torvalds #define min(x, y) ({ \ 365bdf4bbaaSHarvey Harrison typeof(x) _min1 = (x); \ 366bdf4bbaaSHarvey Harrison typeof(y) _min2 = (y); \ 367bdf4bbaaSHarvey Harrison (void) (&_min1 == &_min2); \ 368bdf4bbaaSHarvey Harrison _min1 < _min2 ? _min1 : _min2; }) 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds #define max(x, y) ({ \ 371bdf4bbaaSHarvey Harrison typeof(x) _max1 = (x); \ 372bdf4bbaaSHarvey Harrison typeof(y) _max2 = (y); \ 373bdf4bbaaSHarvey Harrison (void) (&_max1 == &_max2); \ 374bdf4bbaaSHarvey Harrison _max1 > _max2 ? _max1 : _max2; }) 375bdf4bbaaSHarvey Harrison 376bdf4bbaaSHarvey Harrison /** 377bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 378bdf4bbaaSHarvey Harrison * @val: current value 379bdf4bbaaSHarvey Harrison * @min: minimum allowable value 380bdf4bbaaSHarvey Harrison * @max: maximum allowable value 381bdf4bbaaSHarvey Harrison * 382bdf4bbaaSHarvey Harrison * This macro does strict typechecking of min/max to make sure they are of the 383bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 384bdf4bbaaSHarvey Harrison */ 385bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({ \ 386bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 387bdf4bbaaSHarvey Harrison typeof(min) __min = (min); \ 388bdf4bbaaSHarvey Harrison typeof(max) __max = (max); \ 389bdf4bbaaSHarvey Harrison (void) (&__val == &__min); \ 390bdf4bbaaSHarvey Harrison (void) (&__val == &__max); \ 391bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 392bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 3931da177e4SLinus Torvalds 3941da177e4SLinus Torvalds /* 3951da177e4SLinus Torvalds * ..and if you can't take the strict 3961da177e4SLinus Torvalds * types, you can specify one yourself. 3971da177e4SLinus Torvalds * 398bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 3991da177e4SLinus Torvalds */ 400bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({ \ 401bdf4bbaaSHarvey Harrison type __min1 = (x); \ 402bdf4bbaaSHarvey Harrison type __min2 = (y); \ 403bdf4bbaaSHarvey Harrison __min1 < __min2 ? __min1: __min2; }) 4041da177e4SLinus Torvalds 405bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({ \ 406bdf4bbaaSHarvey Harrison type __max1 = (x); \ 407bdf4bbaaSHarvey Harrison type __max2 = (y); \ 408bdf4bbaaSHarvey Harrison __max1 > __max2 ? __max1: __max2; }) 409bdf4bbaaSHarvey Harrison 410bdf4bbaaSHarvey Harrison /** 411bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 412bdf4bbaaSHarvey Harrison * @type: the type of variable to use 413bdf4bbaaSHarvey Harrison * @val: current value 414bdf4bbaaSHarvey Harrison * @min: minimum allowable value 415bdf4bbaaSHarvey Harrison * @max: maximum allowable value 416bdf4bbaaSHarvey Harrison * 417bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 418bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 419bdf4bbaaSHarvey Harrison */ 420bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({ \ 421bdf4bbaaSHarvey Harrison type __val = (val); \ 422bdf4bbaaSHarvey Harrison type __min = (min); \ 423bdf4bbaaSHarvey Harrison type __max = (max); \ 424bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 425bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 426bdf4bbaaSHarvey Harrison 427bdf4bbaaSHarvey Harrison /** 428bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 429bdf4bbaaSHarvey Harrison * @val: current value 430bdf4bbaaSHarvey Harrison * @min: minimum allowable value 431bdf4bbaaSHarvey Harrison * @max: maximum allowable value 432bdf4bbaaSHarvey Harrison * 433bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 434bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 435bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 436bdf4bbaaSHarvey Harrison * integer type. 437bdf4bbaaSHarvey Harrison */ 438bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({ \ 439bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 440bdf4bbaaSHarvey Harrison typeof(val) __min = (min); \ 441bdf4bbaaSHarvey Harrison typeof(val) __max = (max); \ 442bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 443bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds /** 4461da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 4471da177e4SLinus Torvalds * @ptr: the pointer to the member. 4481da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 4491da177e4SLinus Torvalds * @member: the name of the member within the struct. 4501da177e4SLinus Torvalds * 4511da177e4SLinus Torvalds */ 4521da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 4531da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 4541da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 4551da177e4SLinus Torvalds 456d4d23addSKyle McMartin struct sysinfo; 457d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info); 458d4d23addSKyle McMartin 4591da177e4SLinus Torvalds #endif /* __KERNEL__ */ 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds #define SI_LOAD_SHIFT 16 4621da177e4SLinus Torvalds struct sysinfo { 4631da177e4SLinus Torvalds long uptime; /* Seconds since boot */ 4641da177e4SLinus Torvalds unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 4651da177e4SLinus Torvalds unsigned long totalram; /* Total usable main memory size */ 4661da177e4SLinus Torvalds unsigned long freeram; /* Available memory size */ 4671da177e4SLinus Torvalds unsigned long sharedram; /* Amount of shared memory */ 4681da177e4SLinus Torvalds unsigned long bufferram; /* Memory used by buffers */ 4691da177e4SLinus Torvalds unsigned long totalswap; /* Total swap space size */ 4701da177e4SLinus Torvalds unsigned long freeswap; /* swap space still available */ 4711da177e4SLinus Torvalds unsigned short procs; /* Number of current processes */ 4721da177e4SLinus Torvalds unsigned short pad; /* explicit padding for m68k */ 4731da177e4SLinus Torvalds unsigned long totalhigh; /* Total high memory size */ 4741da177e4SLinus Torvalds unsigned long freehigh; /* Available high memory size */ 4751da177e4SLinus Torvalds unsigned int mem_unit; /* Memory unit size in bytes */ 4761da177e4SLinus Torvalds char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 4771da177e4SLinus Torvalds }; 4781da177e4SLinus Torvalds 479c0398ee6SNikita Danilov /* Force a compilation error if condition is true */ 480921717a2SAndi Kleen #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 4811da177e4SLinus Torvalds 4824552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a 4834552d5dcSJan Beulich result (of value 0 and type size_t), so the expression can be used 4844552d5dcSJan Beulich e.g. in a structure initializer (or where-ever else comma expressions 4854552d5dcSJan Beulich aren't permitted). */ 4864552d5dcSJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 4874552d5dcSJan Beulich 4881da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */ 4891da177e4SLinus Torvalds #define __FUNCTION__ (__func__) 4901da177e4SLinus Torvalds 49108e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */ 49208e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA 49308e0f6a9SChristoph Lameter #define NUMA_BUILD 1 49408e0f6a9SChristoph Lameter #else 49508e0f6a9SChristoph Lameter #define NUMA_BUILD 0 49608e0f6a9SChristoph Lameter #endif 49708e0f6a9SChristoph Lameter 4981da177e4SLinus Torvalds #endif 499