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 231da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 241da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 251da177e4SLinus Torvalds #define UINT_MAX (~0U) 261da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 271da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 281da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 29111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 30111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 31111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 341da177e4SLinus Torvalds 352ea58144SLinus Torvalds #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1) 362ea58144SLinus Torvalds #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask)) 37a83308e6SMatthew Wilcox #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) 38f10db627SHerbert Xu #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) 392ea58144SLinus Torvalds 40c5e631cfSRusty Russell #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) 41c5e631cfSRusty Russell 424552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 43930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 44b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 451da177e4SLinus Torvalds 462da96acdSJens Axboe #ifdef CONFIG_LBD 472da96acdSJens Axboe # include <asm/div64.h> 482da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 492da96acdSJens Axboe #else 502da96acdSJens Axboe # define sector_div(n, b)( \ 512da96acdSJens Axboe { \ 522da96acdSJens Axboe int _res; \ 532da96acdSJens Axboe _res = (n) % (b); \ 542da96acdSJens Axboe (n) /= (b); \ 552da96acdSJens Axboe _res; \ 562da96acdSJens Axboe } \ 572da96acdSJens Axboe ) 582da96acdSJens Axboe #endif 592da96acdSJens Axboe 60218e180eSAndrew Morton /** 61218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 62218e180eSAndrew Morton * @n: the number we're accessing 63218e180eSAndrew Morton * 64218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 65218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 66218e180eSAndrew Morton * 32-bits. 67218e180eSAndrew Morton */ 68218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 69218e180eSAndrew Morton 701da177e4SLinus Torvalds #define KERN_EMERG "<0>" /* system is unusable */ 711da177e4SLinus Torvalds #define KERN_ALERT "<1>" /* action must be taken immediately */ 721da177e4SLinus Torvalds #define KERN_CRIT "<2>" /* critical conditions */ 731da177e4SLinus Torvalds #define KERN_ERR "<3>" /* error conditions */ 741da177e4SLinus Torvalds #define KERN_WARNING "<4>" /* warning conditions */ 751da177e4SLinus Torvalds #define KERN_NOTICE "<5>" /* normal but significant condition */ 761da177e4SLinus Torvalds #define KERN_INFO "<6>" /* informational */ 771da177e4SLinus Torvalds #define KERN_DEBUG "<7>" /* debug-level messages */ 781da177e4SLinus Torvalds 7947492527SIngo Molnar /* 8047492527SIngo Molnar * Annotation for a "continued" line of log printout (only done after a 8147492527SIngo Molnar * line that had no enclosing \n). Only to be used by core/arch code 8247492527SIngo Molnar * during early bootup (a continued line is not SMP-safe otherwise). 8347492527SIngo Molnar */ 8447492527SIngo Molnar #define KERN_CONT "" 8547492527SIngo Molnar 861da177e4SLinus Torvalds extern int console_printk[]; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds #define console_loglevel (console_printk[0]) 891da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1]) 901da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2]) 911da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3]) 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds struct completion; 94df2e71fbS[email protected] struct pt_regs; 95df2e71fbS[email protected] struct user; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds /** 981da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 991da177e4SLinus Torvalds * 1001da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1011da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1021da177e4SLinus Torvalds * 1031da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 104e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1051da177e4SLinus Torvalds * supposed to. 1061da177e4SLinus Torvalds */ 107f8cbd99bSIngo Molnar #ifdef CONFIG_PREEMPT_VOLUNTARY 10802b67cc3SHerbert Xu extern int _cond_resched(void); 10902b67cc3SHerbert Xu # define might_resched() _cond_resched() 1101da177e4SLinus Torvalds #else 111f8cbd99bSIngo Molnar # define might_resched() do { } while (0) 1121da177e4SLinus Torvalds #endif 1131da177e4SLinus Torvalds 114f8cbd99bSIngo Molnar #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 115f8cbd99bSIngo Molnar void __might_sleep(char *file, int line); 116f8cbd99bSIngo Molnar # define might_sleep() \ 117f8cbd99bSIngo Molnar do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0) 118f8cbd99bSIngo Molnar #else 119f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 120f8cbd99bSIngo Molnar #endif 121f8cbd99bSIngo Molnar 122368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 123f8cbd99bSIngo Molnar 1241da177e4SLinus Torvalds #define abs(x) ({ \ 1251da177e4SLinus Torvalds int __x = (x); \ 1261da177e4SLinus Torvalds (__x < 0) ? -__x : __x; \ 1271da177e4SLinus Torvalds }) 1281da177e4SLinus Torvalds 129e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 1301da177e4SLinus Torvalds extern long (*panic_blink)(long time); 1311da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 132a586df06SAndi Kleen __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 133dd287796SAndrew Morton extern void oops_enter(void); 134dd287796SAndrew Morton extern void oops_exit(void); 135dd287796SAndrew Morton extern int oops_may_print(void); 136ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code) 1371da177e4SLinus Torvalds ATTRIB_NORET; 1381da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long) 1391da177e4SLinus Torvalds ATTRIB_NORET; 1401da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 1411da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 1421da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 1431da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 144*06b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *); 145*06b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *); 146*06b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 147*06b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *); 1481da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...) 1491da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 3))); 1501da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list) 1511da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 0))); 1521da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...) 1531da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1541da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1551da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 1561da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 1571da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1581da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 1591da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 160e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 161e905914fSJeremy Fitzhardinge __attribute__ ((format (printf, 2, 3))); 16211443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...) 1651da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 3))); 1661da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list) 1671da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 0))); 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 1701da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 1711da177e4SLinus Torvalds extern unsigned long long memparse(char *ptr, char **retptr); 1721da177e4SLinus Torvalds 1735e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 1741da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 1751da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 17604a2e6a5SEric W. Biederman struct pid; 17704a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 1781da177e4SLinus Torvalds 179d59745ceSMatt Mackall #ifdef CONFIG_PRINTK 1801da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args) 1811da177e4SLinus Torvalds __attribute__ ((format (printf, 1, 0))); 1821da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...) 183a586df06SAndi Kleen __attribute__ ((format (printf, 1, 2))) __cold; 1840b15d04aSMike Frysinger extern int log_buf_get_len(void); 1850b15d04aSMike Frysinger extern int log_buf_read(int idx); 1860b15d04aSMike Frysinger extern int log_buf_copy(char *dest, int idx, int len); 1877ef3d2fdSJoe Perches 1887ef3d2fdSJoe Perches extern int printk_ratelimit_jiffies; 1897ef3d2fdSJoe Perches extern int printk_ratelimit_burst; 1907ef3d2fdSJoe Perches extern int printk_ratelimit(void); 1917ef3d2fdSJoe Perches extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst); 1927ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 1937ef3d2fdSJoe Perches unsigned int interval_msec); 194d59745ceSMatt Mackall #else 195d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) 196d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 0))); 197d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; } 198d59745ceSMatt Mackall static inline int printk(const char *s, ...) 199d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 2))); 200a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; } 2010b15d04aSMike Frysinger static inline int log_buf_get_len(void) { return 0; } 2020b15d04aSMike Frysinger static inline int log_buf_read(int idx) { return 0; } 2030b15d04aSMike Frysinger static inline int log_buf_copy(char *dest, int idx, int len) { return 0; } 2047ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; } 2057ef3d2fdSJoe Perches static inline int __printk_ratelimit(int ratelimit_jiffies, \ 2067ef3d2fdSJoe Perches int ratelimit_burst) { return 0; } 2077ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 2087ef3d2fdSJoe Perches unsigned int interval_msec) \ 2097ef3d2fdSJoe Perches { return false; } 210d59745ceSMatt Mackall #endif 2111da177e4SLinus Torvalds 212076f9776SIngo Molnar extern void __attribute__((format(printf, 1, 2))) 213076f9776SIngo Molnar early_printk(const char *fmt, ...); 214076f9776SIngo Molnar 2151da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds static inline void console_silent(void) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds console_loglevel = 0; 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static inline void console_verbose(void) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds if (console_loglevel) 2251da177e4SLinus Torvalds console_loglevel = 15; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 229e3e8a75dSKirill Korotaev extern void wake_up_klogd(void); 2301da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 231aa727107SAdrian Bunk extern int panic_timeout; 2321da177e4SLinus Torvalds extern int panic_on_oops; 2338da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 2341da177e4SLinus Torvalds extern int tainted; 2351da177e4SLinus Torvalds extern const char *print_tainted(void); 2361da177e4SLinus Torvalds extern void add_taint(unsigned); 237b920de1bSDavid Howells extern int root_mountflags; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds /* Values used for system_state */ 2401da177e4SLinus Torvalds extern enum system_states { 2411da177e4SLinus Torvalds SYSTEM_BOOTING, 2421da177e4SLinus Torvalds SYSTEM_RUNNING, 2431da177e4SLinus Torvalds SYSTEM_HALT, 2441da177e4SLinus Torvalds SYSTEM_POWER_OFF, 2451da177e4SLinus Torvalds SYSTEM_RESTART, 246729b4d4cSAlexey Starikovskiy SYSTEM_SUSPEND_DISK, 2471da177e4SLinus Torvalds } system_state; 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds #define TAINT_PROPRIETARY_MODULE (1<<0) 2501da177e4SLinus Torvalds #define TAINT_FORCED_MODULE (1<<1) 2511da177e4SLinus Torvalds #define TAINT_UNSAFE_SMP (1<<2) 2521da177e4SLinus Torvalds #define TAINT_FORCED_RMMOD (1<<3) 2531da177e4SLinus Torvalds #define TAINT_MACHINE_CHECK (1<<4) 2541da177e4SLinus Torvalds #define TAINT_BAD_PAGE (1<<5) 25534f5a398STheodore Ts'o #define TAINT_USER (1<<6) 256bcdcd8e7SPavel Emelianov #define TAINT_DIE (1<<7) 2576ed31e92SÉric Piel #define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8) 2581da177e4SLinus Torvalds 259a586df06SAndi Kleen extern void dump_stack(void) __cold; 2601da177e4SLinus Torvalds 26199eaf3c4SRandy Dunlap enum { 26299eaf3c4SRandy Dunlap DUMP_PREFIX_NONE, 26399eaf3c4SRandy Dunlap DUMP_PREFIX_ADDRESS, 26499eaf3c4SRandy Dunlap DUMP_PREFIX_OFFSET 26599eaf3c4SRandy Dunlap }; 266c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len, 267c7909234SRandy Dunlap int rowsize, int groupsize, 268c7909234SRandy Dunlap char *linebuf, size_t linebuflen, bool ascii); 269c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str, 270c7909234SRandy Dunlap int prefix_type, int rowsize, int groupsize, 2716a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii); 272c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 273eb9a9a56SAlan Stern const void *buf, size_t len); 27499eaf3c4SRandy Dunlap #define hex_asc(x) "0123456789abcdef"[x] 27599eaf3c4SRandy Dunlap 2761f7c8234SEmil Medve #define pr_emerg(fmt, arg...) \ 2771f7c8234SEmil Medve printk(KERN_EMERG fmt, ##arg) 2781f7c8234SEmil Medve #define pr_alert(fmt, arg...) \ 2791f7c8234SEmil Medve printk(KERN_ALERT fmt, ##arg) 2801f7c8234SEmil Medve #define pr_crit(fmt, arg...) \ 2811f7c8234SEmil Medve printk(KERN_CRIT fmt, ##arg) 2821f7c8234SEmil Medve #define pr_err(fmt, arg...) \ 2831f7c8234SEmil Medve printk(KERN_ERR fmt, ##arg) 2841f7c8234SEmil Medve #define pr_warning(fmt, arg...) \ 2851f7c8234SEmil Medve printk(KERN_WARNING fmt, ##arg) 2861f7c8234SEmil Medve #define pr_notice(fmt, arg...) \ 2871f7c8234SEmil Medve printk(KERN_NOTICE fmt, ##arg) 2881f7c8234SEmil Medve #define pr_info(fmt, arg...) \ 2891f7c8234SEmil Medve printk(KERN_INFO fmt, ##arg) 2901f7c8234SEmil Medve 2911da177e4SLinus Torvalds #ifdef DEBUG 2921cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */ 2931da177e4SLinus Torvalds #define pr_debug(fmt, arg...) \ 2941da177e4SLinus Torvalds printk(KERN_DEBUG fmt, ##arg) 2951da177e4SLinus Torvalds #else 2968b2a1fd1SZach Brown static inline int __attribute__ ((format (printf, 1, 2))) pr_debug(const char * fmt, ...) 2978b2a1fd1SZach Brown { 2988b2a1fd1SZach Brown return 0; 2998b2a1fd1SZach Brown } 3001da177e4SLinus Torvalds #endif 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds /* 3031da177e4SLinus Torvalds * Display an IP address in readable format. 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds #define NIPQUAD(addr) \ 3071da177e4SLinus Torvalds ((unsigned char *)&addr)[0], \ 3081da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3091da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3101da177e4SLinus Torvalds ((unsigned char *)&addr)[3] 31146b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u" 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds #define NIP6(addr) \ 3141da177e4SLinus Torvalds ntohs((addr).s6_addr16[0]), \ 3151da177e4SLinus Torvalds ntohs((addr).s6_addr16[1]), \ 3161da177e4SLinus Torvalds ntohs((addr).s6_addr16[2]), \ 3171da177e4SLinus Torvalds ntohs((addr).s6_addr16[3]), \ 3181da177e4SLinus Torvalds ntohs((addr).s6_addr16[4]), \ 3191da177e4SLinus Torvalds ntohs((addr).s6_addr16[5]), \ 3201da177e4SLinus Torvalds ntohs((addr).s6_addr16[6]), \ 3211da177e4SLinus Torvalds ntohs((addr).s6_addr16[7]) 32246b86a2dSJoe Perches #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x" 3239343e79aSYOSHIFUJI Hideaki #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x" 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds #if defined(__LITTLE_ENDIAN) 3261da177e4SLinus Torvalds #define HIPQUAD(addr) \ 3271da177e4SLinus Torvalds ((unsigned char *)&addr)[3], \ 3281da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 3291da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 3301da177e4SLinus Torvalds ((unsigned char *)&addr)[0] 3311da177e4SLinus Torvalds #elif defined(__BIG_ENDIAN) 3321da177e4SLinus Torvalds #define HIPQUAD NIPQUAD 3331da177e4SLinus Torvalds #else 3341da177e4SLinus Torvalds #error "Please fix asm/byteorder.h" 3351da177e4SLinus Torvalds #endif /* __LITTLE_ENDIAN */ 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds /* 3381da177e4SLinus Torvalds * min()/max() macros that also do 3391da177e4SLinus Torvalds * strict type-checking.. See the 3401da177e4SLinus Torvalds * "unnecessary" pointer comparison. 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds #define min(x,y) ({ \ 3431da177e4SLinus Torvalds typeof(x) _x = (x); \ 3441da177e4SLinus Torvalds typeof(y) _y = (y); \ 3451da177e4SLinus Torvalds (void) (&_x == &_y); \ 3461da177e4SLinus Torvalds _x < _y ? _x : _y; }) 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds #define max(x,y) ({ \ 3491da177e4SLinus Torvalds typeof(x) _x = (x); \ 3501da177e4SLinus Torvalds typeof(y) _y = (y); \ 3511da177e4SLinus Torvalds (void) (&_x == &_y); \ 3521da177e4SLinus Torvalds _x > _y ? _x : _y; }) 3531da177e4SLinus Torvalds 3541da177e4SLinus Torvalds /* 3551da177e4SLinus Torvalds * ..and if you can't take the strict 3561da177e4SLinus Torvalds * types, you can specify one yourself. 3571da177e4SLinus Torvalds * 3581da177e4SLinus Torvalds * Or not use min/max at all, of course. 3591da177e4SLinus Torvalds */ 3601da177e4SLinus Torvalds #define min_t(type,x,y) \ 3611da177e4SLinus Torvalds ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; }) 3621da177e4SLinus Torvalds #define max_t(type,x,y) \ 3631da177e4SLinus Torvalds ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; }) 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /** 3671da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 3681da177e4SLinus Torvalds * @ptr: the pointer to the member. 3691da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 3701da177e4SLinus Torvalds * @member: the name of the member within the struct. 3711da177e4SLinus Torvalds * 3721da177e4SLinus Torvalds */ 3731da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 3741da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 3751da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 3761da177e4SLinus Torvalds 3771da177e4SLinus Torvalds /* 3781da177e4SLinus Torvalds * Check at compile time that something is of a particular type. 3791da177e4SLinus Torvalds * Always evaluates to 1 so you may use it easily in comparisons. 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds #define typecheck(type,x) \ 3821da177e4SLinus Torvalds ({ type __dummy; \ 3831da177e4SLinus Torvalds typeof(x) __dummy2; \ 3841da177e4SLinus Torvalds (void)(&__dummy == &__dummy2); \ 3851da177e4SLinus Torvalds 1; \ 3861da177e4SLinus Torvalds }) 3871da177e4SLinus Torvalds 388711a660dSChuck Ebbert /* 389711a660dSChuck Ebbert * Check at compile time that 'function' is a certain type, or is a pointer 390711a660dSChuck Ebbert * to that type (needs to use typedef for the function type.) 391711a660dSChuck Ebbert */ 392711a660dSChuck Ebbert #define typecheck_fn(type,function) \ 393711a660dSChuck Ebbert ({ typeof(type) __tmp = function; \ 394711a660dSChuck Ebbert (void)__tmp; \ 395711a660dSChuck Ebbert }) 396711a660dSChuck Ebbert 397d4d23addSKyle McMartin struct sysinfo; 398d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info); 399d4d23addSKyle McMartin 4001da177e4SLinus Torvalds #endif /* __KERNEL__ */ 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds #define SI_LOAD_SHIFT 16 4031da177e4SLinus Torvalds struct sysinfo { 4041da177e4SLinus Torvalds long uptime; /* Seconds since boot */ 4051da177e4SLinus Torvalds unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 4061da177e4SLinus Torvalds unsigned long totalram; /* Total usable main memory size */ 4071da177e4SLinus Torvalds unsigned long freeram; /* Available memory size */ 4081da177e4SLinus Torvalds unsigned long sharedram; /* Amount of shared memory */ 4091da177e4SLinus Torvalds unsigned long bufferram; /* Memory used by buffers */ 4101da177e4SLinus Torvalds unsigned long totalswap; /* Total swap space size */ 4111da177e4SLinus Torvalds unsigned long freeswap; /* swap space still available */ 4121da177e4SLinus Torvalds unsigned short procs; /* Number of current processes */ 4131da177e4SLinus Torvalds unsigned short pad; /* explicit padding for m68k */ 4141da177e4SLinus Torvalds unsigned long totalhigh; /* Total high memory size */ 4151da177e4SLinus Torvalds unsigned long freehigh; /* Available high memory size */ 4161da177e4SLinus Torvalds unsigned int mem_unit; /* Memory unit size in bytes */ 4171da177e4SLinus Torvalds char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 4181da177e4SLinus Torvalds }; 4191da177e4SLinus Torvalds 420c0398ee6SNikita Danilov /* Force a compilation error if condition is true */ 421921717a2SAndi Kleen #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 4221da177e4SLinus Torvalds 4234552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a 4244552d5dcSJan Beulich result (of value 0 and type size_t), so the expression can be used 4254552d5dcSJan Beulich e.g. in a structure initializer (or where-ever else comma expressions 4264552d5dcSJan Beulich aren't permitted). */ 4274552d5dcSJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) 4284552d5dcSJan Beulich 4291da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */ 4301da177e4SLinus Torvalds #define __FUNCTION__ (__func__) 4311da177e4SLinus Torvalds 43208e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */ 43308e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA 43408e0f6a9SChristoph Lameter #define NUMA_BUILD 1 43508e0f6a9SChristoph Lameter #else 43608e0f6a9SChristoph Lameter #define NUMA_BUILD 0 43708e0f6a9SChristoph Lameter #endif 43808e0f6a9SChristoph Lameter 4391da177e4SLinus Torvalds #endif 440