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 */ 7*a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1) 8*a79ff731SAlexey Dobriyan #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask)) 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #ifdef __KERNEL__ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <stdarg.h> 131da177e4SLinus Torvalds #include <linux/linkage.h> 141da177e4SLinus Torvalds #include <linux/stddef.h> 151da177e4SLinus Torvalds #include <linux/types.h> 161da177e4SLinus Torvalds #include <linux/compiler.h> 171da177e4SLinus Torvalds #include <linux/bitops.h> 18f0d1b0b3SDavid Howells #include <linux/log2.h> 19e0deaff4SAndrew Morton #include <linux/typecheck.h> 20e9d376f0SJason Baron #include <linux/dynamic_debug.h> 211da177e4SLinus Torvalds #include <asm/byteorder.h> 221da177e4SLinus Torvalds #include <asm/bug.h> 231da177e4SLinus Torvalds 243eb3c740SRoman Zippel extern const char linux_banner[]; 253eb3c740SRoman Zippel extern const char linux_proc_banner[]; 263eb3c740SRoman Zippel 2744f564a4SZhang, Yanmin #define USHORT_MAX ((u16)(~0U)) 2844f564a4SZhang, Yanmin #define SHORT_MAX ((s16)(USHORT_MAX>>1)) 2944f564a4SZhang, Yanmin #define SHORT_MIN (-SHORT_MAX - 1) 301da177e4SLinus Torvalds #define INT_MAX ((int)(~0U>>1)) 311da177e4SLinus Torvalds #define INT_MIN (-INT_MAX - 1) 321da177e4SLinus Torvalds #define UINT_MAX (~0U) 331da177e4SLinus Torvalds #define LONG_MAX ((long)(~0UL>>1)) 341da177e4SLinus Torvalds #define LONG_MIN (-LONG_MAX - 1) 351da177e4SLinus Torvalds #define ULONG_MAX (~0UL) 36111ebb6eSOGAWA Hirofumi #define LLONG_MAX ((long long)(~0ULL>>1)) 37111ebb6eSOGAWA Hirofumi #define LLONG_MIN (-LLONG_MAX - 1) 38111ebb6eSOGAWA Hirofumi #define ULLONG_MAX (~0ULL) 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define STACK_MAGIC 0xdeadbeef 411da177e4SLinus Torvalds 42*a79ff731SAlexey Dobriyan #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) 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 489b3be9f9SYinghai Lu /* 499b3be9f9SYinghai Lu * This looks more complex than it should be. But we need to 509b3be9f9SYinghai Lu * get the type for the ~ right in round_down (it needs to be 519b3be9f9SYinghai Lu * as wide as the result!), and we want to evaluate the macro 529b3be9f9SYinghai Lu * arguments just once each. 539b3be9f9SYinghai Lu */ 549b3be9f9SYinghai Lu #define __round_mask(x, y) ((__typeof__(x))((y)-1)) 559b3be9f9SYinghai Lu #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) 569b3be9f9SYinghai Lu #define round_down(x, y) ((x) & ~__round_mask(x, y)) 579b3be9f9SYinghai Lu 584552d5dcSJan Beulich #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f)) 59930631edSSteven Whitehouse #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) 60b4cac1a0SDavid Howells #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 619fe06081SDarrick J. Wong #define DIV_ROUND_CLOSEST(x, divisor)( \ 629fe06081SDarrick J. Wong { \ 639fe06081SDarrick J. Wong typeof(divisor) __divisor = divisor; \ 649fe06081SDarrick J. Wong (((x) + ((__divisor) / 2)) / (__divisor)); \ 659fe06081SDarrick J. Wong } \ 669fe06081SDarrick J. Wong ) 671da177e4SLinus Torvalds 68ca31e146SEduard - Gabriel Munteanu #define _RET_IP_ (unsigned long)__builtin_return_address(0) 69ca31e146SEduard - Gabriel Munteanu #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) 70ca31e146SEduard - Gabriel Munteanu 7190c699a9SBartlomiej Zolnierkiewicz #ifdef CONFIG_LBDAF 722da96acdSJens Axboe # include <asm/div64.h> 732da96acdSJens Axboe # define sector_div(a, b) do_div(a, b) 742da96acdSJens Axboe #else 752da96acdSJens Axboe # define sector_div(n, b)( \ 762da96acdSJens Axboe { \ 772da96acdSJens Axboe int _res; \ 782da96acdSJens Axboe _res = (n) % (b); \ 792da96acdSJens Axboe (n) /= (b); \ 802da96acdSJens Axboe _res; \ 812da96acdSJens Axboe } \ 822da96acdSJens Axboe ) 832da96acdSJens Axboe #endif 842da96acdSJens Axboe 85218e180eSAndrew Morton /** 86218e180eSAndrew Morton * upper_32_bits - return bits 32-63 of a number 87218e180eSAndrew Morton * @n: the number we're accessing 88218e180eSAndrew Morton * 89218e180eSAndrew Morton * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress 90218e180eSAndrew Morton * the "right shift count >= width of type" warning when that quantity is 91218e180eSAndrew Morton * 32-bits. 92218e180eSAndrew Morton */ 93218e180eSAndrew Morton #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) 94218e180eSAndrew Morton 95204b885eSJoerg Roedel /** 96204b885eSJoerg Roedel * lower_32_bits - return bits 0-31 of a number 97204b885eSJoerg Roedel * @n: the number we're accessing 98204b885eSJoerg Roedel */ 99204b885eSJoerg Roedel #define lower_32_bits(n) ((u32)(n)) 100204b885eSJoerg Roedel 1011da177e4SLinus Torvalds #define KERN_EMERG "<0>" /* system is unusable */ 1021da177e4SLinus Torvalds #define KERN_ALERT "<1>" /* action must be taken immediately */ 1031da177e4SLinus Torvalds #define KERN_CRIT "<2>" /* critical conditions */ 1041da177e4SLinus Torvalds #define KERN_ERR "<3>" /* error conditions */ 1051da177e4SLinus Torvalds #define KERN_WARNING "<4>" /* warning conditions */ 1061da177e4SLinus Torvalds #define KERN_NOTICE "<5>" /* normal but significant condition */ 1071da177e4SLinus Torvalds #define KERN_INFO "<6>" /* informational */ 1081da177e4SLinus Torvalds #define KERN_DEBUG "<7>" /* debug-level messages */ 1091da177e4SLinus Torvalds 110e28d7137SLinus Torvalds /* Use the default kernel loglevel */ 111e28d7137SLinus Torvalds #define KERN_DEFAULT "<d>" 11247492527SIngo Molnar /* 11347492527SIngo Molnar * Annotation for a "continued" line of log printout (only done after a 11447492527SIngo Molnar * line that had no enclosing \n). Only to be used by core/arch code 11547492527SIngo Molnar * during early bootup (a continued line is not SMP-safe otherwise). 11647492527SIngo Molnar */ 1175fd29d6cSLinus Torvalds #define KERN_CONT "<c>" 11847492527SIngo Molnar 1191da177e4SLinus Torvalds extern int console_printk[]; 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds #define console_loglevel (console_printk[0]) 1221da177e4SLinus Torvalds #define default_message_loglevel (console_printk[1]) 1231da177e4SLinus Torvalds #define minimum_console_loglevel (console_printk[2]) 1241da177e4SLinus Torvalds #define default_console_loglevel (console_printk[3]) 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds struct completion; 127df2e71fbS[email protected] struct pt_regs; 128df2e71fbS[email protected] struct user; 1291da177e4SLinus Torvalds 130070cb065SUwe Kleine-König #ifdef CONFIG_PREEMPT_VOLUNTARY 131070cb065SUwe Kleine-König extern int _cond_resched(void); 132070cb065SUwe Kleine-König # define might_resched() _cond_resched() 133070cb065SUwe Kleine-König #else 134070cb065SUwe Kleine-König # define might_resched() do { } while (0) 135070cb065SUwe Kleine-König #endif 136070cb065SUwe Kleine-König 1377106a27bSRandy Dunlap #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP 138d894837fSSimon Kagstrom void __might_sleep(const char *file, int line, int preempt_offset); 1391da177e4SLinus Torvalds /** 1401da177e4SLinus Torvalds * might_sleep - annotation for functions that can sleep 1411da177e4SLinus Torvalds * 1421da177e4SLinus Torvalds * this macro will print a stack trace if it is executed in an atomic 1431da177e4SLinus Torvalds * context (spinlock, irq-handler, ...). 1441da177e4SLinus Torvalds * 1451da177e4SLinus Torvalds * This is a useful debugging help to be able to catch problems early and not 146e20ec991SJim Cromie * be bitten later when the calling function happens to sleep when it is not 1471da177e4SLinus Torvalds * supposed to. 1481da177e4SLinus Torvalds */ 149f8cbd99bSIngo Molnar # define might_sleep() \ 150e4aafea2SFrederic Weisbecker do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) 151f8cbd99bSIngo Molnar #else 152d894837fSSimon Kagstrom static inline void __might_sleep(const char *file, int line, 153d894837fSSimon Kagstrom int preempt_offset) { } 154f8cbd99bSIngo Molnar # define might_sleep() do { might_resched(); } while (0) 155f8cbd99bSIngo Molnar #endif 156f8cbd99bSIngo Molnar 157368a5fa1SHua Zhong #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) 158f8cbd99bSIngo Molnar 1591da177e4SLinus Torvalds #define abs(x) ({ \ 160a49c59c0SRolf Eike Beer long __x = (x); \ 1611da177e4SLinus Torvalds (__x < 0) ? -__x : __x; \ 1621da177e4SLinus Torvalds }) 1631da177e4SLinus Torvalds 1643ee1afa3SNick Piggin #ifdef CONFIG_PROVE_LOCKING 1653ee1afa3SNick Piggin void might_fault(void); 1663ee1afa3SNick Piggin #else 1673ee1afa3SNick Piggin static inline void might_fault(void) 1683ee1afa3SNick Piggin { 1693ee1afa3SNick Piggin might_sleep(); 1703ee1afa3SNick Piggin } 1713ee1afa3SNick Piggin #endif 1723ee1afa3SNick Piggin 173e041c683SAlan Stern extern struct atomic_notifier_head panic_notifier_list; 1741da177e4SLinus Torvalds extern long (*panic_blink)(long time); 1751da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 176a586df06SAndi Kleen __attribute__ ((NORET_AND format (printf, 1, 2))) __cold; 177dd287796SAndrew Morton extern void oops_enter(void); 178dd287796SAndrew Morton extern void oops_exit(void); 179dd287796SAndrew Morton extern int oops_may_print(void); 180ec701584SHarvey Harrison NORET_TYPE void do_exit(long error_code) 1811da177e4SLinus Torvalds ATTRIB_NORET; 1821da177e4SLinus Torvalds NORET_TYPE void complete_and_exit(struct completion *, long) 1831da177e4SLinus Torvalds ATTRIB_NORET; 1841da177e4SLinus Torvalds extern unsigned long simple_strtoul(const char *,char **,unsigned int); 1851da177e4SLinus Torvalds extern long simple_strtol(const char *,char **,unsigned int); 1861da177e4SLinus Torvalds extern unsigned long long simple_strtoull(const char *,char **,unsigned int); 1871da177e4SLinus Torvalds extern long long simple_strtoll(const char *,char **,unsigned int); 18806b2a76dSYi Yang extern int strict_strtoul(const char *, unsigned int, unsigned long *); 18906b2a76dSYi Yang extern int strict_strtol(const char *, unsigned int, long *); 19006b2a76dSYi Yang extern int strict_strtoull(const char *, unsigned int, unsigned long long *); 19106b2a76dSYi Yang extern int strict_strtoll(const char *, unsigned int, long long *); 1921da177e4SLinus Torvalds extern int sprintf(char * buf, const char * fmt, ...) 1931da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 3))); 1941da177e4SLinus Torvalds extern int vsprintf(char *buf, const char *, va_list) 1951da177e4SLinus Torvalds __attribute__ ((format (printf, 2, 0))); 1961da177e4SLinus Torvalds extern int snprintf(char * buf, size_t size, const char * fmt, ...) 1971da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 1981da177e4SLinus Torvalds extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 1991da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 2001da177e4SLinus Torvalds extern int scnprintf(char * buf, size_t size, const char * fmt, ...) 2011da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 4))); 2021da177e4SLinus Torvalds extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 2031da177e4SLinus Torvalds __attribute__ ((format (printf, 3, 0))); 204e905914fSJeremy Fitzhardinge extern char *kasprintf(gfp_t gfp, const char *fmt, ...) 205e905914fSJeremy Fitzhardinge __attribute__ ((format (printf, 2, 3))); 20611443ec7SJeremy Fitzhardinge extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds extern int sscanf(const char *, const char *, ...) 2091da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 3))); 2101da177e4SLinus Torvalds extern int vsscanf(const char *, const char *, va_list) 2111da177e4SLinus Torvalds __attribute__ ((format (scanf, 2, 0))); 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds extern int get_option(char **str, int *pint); 2141da177e4SLinus Torvalds extern char *get_options(const char *str, int nints, int *ints); 215d974ae37SJeremy Fitzhardinge extern unsigned long long memparse(const char *ptr, char **retptr); 2161da177e4SLinus Torvalds 2175e376613STrent Piepho extern int core_kernel_text(unsigned long addr); 2181da177e4SLinus Torvalds extern int __kernel_text_address(unsigned long addr); 2191da177e4SLinus Torvalds extern int kernel_text_address(unsigned long addr); 220ab7476cfSArjan van de Ven extern int func_ptr_is_kernel_text(void *ptr); 221ab7476cfSArjan van de Ven 22204a2e6a5SEric W. Biederman struct pid; 22304a2e6a5SEric W. Biederman extern struct pid *session_of_pgrp(struct pid *pgrp); 2241da177e4SLinus Torvalds 225a0ad05c7SThomas Renninger /* 226a0ad05c7SThomas Renninger * FW_BUG 227a0ad05c7SThomas Renninger * Add this to a message where you are sure the firmware is buggy or behaves 228a0ad05c7SThomas Renninger * really stupid or out of spec. Be aware that the responsible BIOS developer 229a0ad05c7SThomas Renninger * should be able to fix this issue or at least get a concrete idea of the 230a0ad05c7SThomas Renninger * problem by reading your message without the need of looking at the kernel 231a0ad05c7SThomas Renninger * code. 232a0ad05c7SThomas Renninger * 233a0ad05c7SThomas Renninger * Use it for definite and high priority BIOS bugs. 234a0ad05c7SThomas Renninger * 235a0ad05c7SThomas Renninger * FW_WARN 236a0ad05c7SThomas Renninger * Use it for not that clear (e.g. could the kernel messed up things already?) 237a0ad05c7SThomas Renninger * and medium priority BIOS bugs. 238a0ad05c7SThomas Renninger * 239a0ad05c7SThomas Renninger * FW_INFO 240a0ad05c7SThomas Renninger * Use this one if you want to tell the user or vendor about something 241a0ad05c7SThomas Renninger * suspicious, but generally harmless related to the firmware. 242a0ad05c7SThomas Renninger * 243a0ad05c7SThomas Renninger * Use it for information or very low priority BIOS bugs. 244a0ad05c7SThomas Renninger */ 245a0ad05c7SThomas Renninger #define FW_BUG "[Firmware Bug]: " 246a0ad05c7SThomas Renninger #define FW_WARN "[Firmware Warn]: " 247a0ad05c7SThomas Renninger #define FW_INFO "[Firmware Info]: " 248a0ad05c7SThomas Renninger 249d59745ceSMatt Mackall #ifdef CONFIG_PRINTK 2501da177e4SLinus Torvalds asmlinkage int vprintk(const char *fmt, va_list args) 2511da177e4SLinus Torvalds __attribute__ ((format (printf, 1, 0))); 2521da177e4SLinus Torvalds asmlinkage int printk(const char * fmt, ...) 253a586df06SAndi Kleen __attribute__ ((format (printf, 1, 2))) __cold; 2547ef3d2fdSJoe Perches 2555c828713SChristian Borntraeger extern int __printk_ratelimit(const char *func); 2565c828713SChristian Borntraeger #define printk_ratelimit() __printk_ratelimit(__func__) 2577ef3d2fdSJoe Perches extern bool printk_timed_ratelimit(unsigned long *caller_jiffies, 2587ef3d2fdSJoe Perches unsigned int interval_msec); 259f036be96SIngo Molnar 260af91322eSDave Young extern int printk_delay_msec; 261af91322eSDave Young 262f036be96SIngo Molnar /* 263f036be96SIngo Molnar * Print a one-time message (analogous to WARN_ONCE() et al): 264f036be96SIngo Molnar */ 265f036be96SIngo Molnar #define printk_once(x...) ({ \ 2660b2749aaSJoe Perches static bool __print_once; \ 267f036be96SIngo Molnar \ 2680b2749aaSJoe Perches if (!__print_once) { \ 2690b2749aaSJoe Perches __print_once = true; \ 270f036be96SIngo Molnar printk(x); \ 271f036be96SIngo Molnar } \ 272f036be96SIngo Molnar }) 273f036be96SIngo Molnar 27404d491abSNeil Horman void log_buf_kexec_setup(void); 275d59745ceSMatt Mackall #else 276d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) 277d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 0))); 278d59745ceSMatt Mackall static inline int vprintk(const char *s, va_list args) { return 0; } 279d59745ceSMatt Mackall static inline int printk(const char *s, ...) 280d59745ceSMatt Mackall __attribute__ ((format (printf, 1, 2))); 281a586df06SAndi Kleen static inline int __cold printk(const char *s, ...) { return 0; } 2827ef3d2fdSJoe Perches static inline int printk_ratelimit(void) { return 0; } 2837ef3d2fdSJoe Perches static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \ 2847ef3d2fdSJoe Perches unsigned int interval_msec) \ 2857ef3d2fdSJoe Perches { return false; } 286f036be96SIngo Molnar 287f036be96SIngo Molnar /* No effect, but we still get type checking even in the !PRINTK case: */ 288f036be96SIngo Molnar #define printk_once(x...) printk(x) 289f036be96SIngo Molnar 29004d491abSNeil Horman static inline void log_buf_kexec_setup(void) 29104d491abSNeil Horman { 29204d491abSNeil Horman } 293d59745ceSMatt Mackall #endif 2941da177e4SLinus Torvalds 295ced9cd40SIngo Molnar extern int printk_needs_cpu(int cpu); 296ced9cd40SIngo Molnar extern void printk_tick(void); 297ced9cd40SIngo Molnar 298e17ba73bSJiri Slaby extern void asmlinkage __attribute__((format(printf, 1, 2))) 299076f9776SIngo Molnar early_printk(const char *fmt, ...); 300076f9776SIngo Molnar 3011da177e4SLinus Torvalds unsigned long int_sqrt(unsigned long); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds static inline void console_silent(void) 3041da177e4SLinus Torvalds { 3051da177e4SLinus Torvalds console_loglevel = 0; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds static inline void console_verbose(void) 3091da177e4SLinus Torvalds { 3101da177e4SLinus Torvalds if (console_loglevel) 3111da177e4SLinus Torvalds console_loglevel = 15; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds extern void bust_spinlocks(int yes); 315e3e8a75dSKirill Korotaev extern void wake_up_klogd(void); 3161da177e4SLinus Torvalds extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */ 317aa727107SAdrian Bunk extern int panic_timeout; 3181da177e4SLinus Torvalds extern int panic_on_oops; 3198da5addaSDon Zickus extern int panic_on_unrecovered_nmi; 3205211a242SKurt Garloff extern int panic_on_io_nmi; 3211da177e4SLinus Torvalds extern const char *print_tainted(void); 32225ddbb18SAndi Kleen extern void add_taint(unsigned flag); 32325ddbb18SAndi Kleen extern int test_taint(unsigned flag); 32425ddbb18SAndi Kleen extern unsigned long get_taint(void); 325b920de1bSDavid Howells extern int root_mountflags; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* Values used for system_state */ 3281da177e4SLinus Torvalds extern enum system_states { 3291da177e4SLinus Torvalds SYSTEM_BOOTING, 3301da177e4SLinus Torvalds SYSTEM_RUNNING, 3311da177e4SLinus Torvalds SYSTEM_HALT, 3321da177e4SLinus Torvalds SYSTEM_POWER_OFF, 3331da177e4SLinus Torvalds SYSTEM_RESTART, 334729b4d4cSAlexey Starikovskiy SYSTEM_SUSPEND_DISK, 3351da177e4SLinus Torvalds } system_state; 3361da177e4SLinus Torvalds 33725ddbb18SAndi Kleen #define TAINT_PROPRIETARY_MODULE 0 33825ddbb18SAndi Kleen #define TAINT_FORCED_MODULE 1 33925ddbb18SAndi Kleen #define TAINT_UNSAFE_SMP 2 34025ddbb18SAndi Kleen #define TAINT_FORCED_RMMOD 3 34125ddbb18SAndi Kleen #define TAINT_MACHINE_CHECK 4 34225ddbb18SAndi Kleen #define TAINT_BAD_PAGE 5 34325ddbb18SAndi Kleen #define TAINT_USER 6 34425ddbb18SAndi Kleen #define TAINT_DIE 7 34525ddbb18SAndi Kleen #define TAINT_OVERRIDDEN_ACPI_TABLE 8 34625ddbb18SAndi Kleen #define TAINT_WARN 9 34726e9a397SLinus Torvalds #define TAINT_CRAP 10 3481da177e4SLinus Torvalds 349a586df06SAndi Kleen extern void dump_stack(void) __cold; 3501da177e4SLinus Torvalds 35199eaf3c4SRandy Dunlap enum { 35299eaf3c4SRandy Dunlap DUMP_PREFIX_NONE, 35399eaf3c4SRandy Dunlap DUMP_PREFIX_ADDRESS, 35499eaf3c4SRandy Dunlap DUMP_PREFIX_OFFSET 35599eaf3c4SRandy Dunlap }; 356c7909234SRandy Dunlap extern void hex_dump_to_buffer(const void *buf, size_t len, 357c7909234SRandy Dunlap int rowsize, int groupsize, 358c7909234SRandy Dunlap char *linebuf, size_t linebuflen, bool ascii); 359c7909234SRandy Dunlap extern void print_hex_dump(const char *level, const char *prefix_str, 360c7909234SRandy Dunlap int prefix_type, int rowsize, int groupsize, 3616a0ed91eSArtem Bityutskiy const void *buf, size_t len, bool ascii); 362c7909234SRandy Dunlap extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type, 363eb9a9a56SAlan Stern const void *buf, size_t len); 3643fc95772SHarvey Harrison 3653fc95772SHarvey Harrison extern const char hex_asc[]; 3663fc95772SHarvey Harrison #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] 3673fc95772SHarvey Harrison #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] 3683fc95772SHarvey Harrison 3693fc95772SHarvey Harrison static inline char *pack_hex_byte(char *buf, u8 byte) 3703fc95772SHarvey Harrison { 3713fc95772SHarvey Harrison *buf++ = hex_asc_hi(byte); 3723fc95772SHarvey Harrison *buf++ = hex_asc_lo(byte); 3733fc95772SHarvey Harrison return buf; 3743fc95772SHarvey Harrison } 37599eaf3c4SRandy Dunlap 376d091c2f5SMartin Schwidefsky #ifndef pr_fmt 377d091c2f5SMartin Schwidefsky #define pr_fmt(fmt) fmt 378d091c2f5SMartin Schwidefsky #endif 379d091c2f5SMartin Schwidefsky 380d091c2f5SMartin Schwidefsky #define pr_emerg(fmt, ...) \ 381d091c2f5SMartin Schwidefsky printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 382d091c2f5SMartin Schwidefsky #define pr_alert(fmt, ...) \ 383d091c2f5SMartin Schwidefsky printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 384d091c2f5SMartin Schwidefsky #define pr_crit(fmt, ...) \ 385d091c2f5SMartin Schwidefsky printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 386d091c2f5SMartin Schwidefsky #define pr_err(fmt, ...) \ 387d091c2f5SMartin Schwidefsky printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 388d091c2f5SMartin Schwidefsky #define pr_warning(fmt, ...) \ 389d091c2f5SMartin Schwidefsky printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 390d091c2f5SMartin Schwidefsky #define pr_notice(fmt, ...) \ 391d091c2f5SMartin Schwidefsky printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 392d091c2f5SMartin Schwidefsky #define pr_info(fmt, ...) \ 393d091c2f5SMartin Schwidefsky printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 394311d0761SCyrill Gorcunov #define pr_cont(fmt, ...) \ 395311d0761SCyrill Gorcunov printk(KERN_CONT fmt, ##__VA_ARGS__) 3961f7c8234SEmil Medve 3974ccb4579SMichael Ellerman /* pr_devel() should produce zero code unless DEBUG is defined */ 3984ccb4579SMichael Ellerman #ifdef DEBUG 3994ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \ 4004ccb4579SMichael Ellerman printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 4014ccb4579SMichael Ellerman #else 4024ccb4579SMichael Ellerman #define pr_devel(fmt, ...) \ 4034ccb4579SMichael Ellerman ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 4044ccb4579SMichael Ellerman #endif 4054ccb4579SMichael Ellerman 4061cc6daf2SPavel Machek /* If you are writing a driver, please use dev_dbg instead */ 407d0d85ff9SCornelia Huck #if defined(DEBUG) 408d0d85ff9SCornelia Huck #define pr_debug(fmt, ...) \ 409d0d85ff9SCornelia Huck printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 410e9d376f0SJason Baron #elif defined(CONFIG_DYNAMIC_DEBUG) 411e6e66b02SGreg Banks /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */ 41200b55864SJoe Perches #define pr_debug(fmt, ...) \ 41300b55864SJoe Perches dynamic_pr_debug(fmt, ##__VA_ARGS__) 4141da177e4SLinus Torvalds #else 415d091c2f5SMartin Schwidefsky #define pr_debug(fmt, ...) \ 416d091c2f5SMartin Schwidefsky ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; }) 4171da177e4SLinus Torvalds #endif 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds /* 4208a64f336SJoe Perches * ratelimited messages with local ratelimit_state, 4218a64f336SJoe Perches * no local ratelimit_state used in the !PRINTK case 4228a64f336SJoe Perches */ 4238a64f336SJoe Perches #ifdef CONFIG_PRINTK 4248a64f336SJoe Perches #define printk_ratelimited(fmt, ...) ({ \ 4258a64f336SJoe Perches static struct ratelimit_state _rs = { \ 4268a64f336SJoe Perches .interval = DEFAULT_RATELIMIT_INTERVAL, \ 4278a64f336SJoe Perches .burst = DEFAULT_RATELIMIT_BURST, \ 4288a64f336SJoe Perches }; \ 4298a64f336SJoe Perches \ 4308a64f336SJoe Perches if (!__ratelimit(&_rs)) \ 4318a64f336SJoe Perches printk(fmt, ##__VA_ARGS__); \ 4328a64f336SJoe Perches }) 4338a64f336SJoe Perches #else 4348a64f336SJoe Perches /* No effect, but we still get type checking even in the !PRINTK case: */ 4358a64f336SJoe Perches #define printk_ratelimited printk 4368a64f336SJoe Perches #endif 4378a64f336SJoe Perches 4388a64f336SJoe Perches #define pr_emerg_ratelimited(fmt, ...) \ 4398a64f336SJoe Perches printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__) 4408a64f336SJoe Perches #define pr_alert_ratelimited(fmt, ...) \ 4418a64f336SJoe Perches printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__) 4428a64f336SJoe Perches #define pr_crit_ratelimited(fmt, ...) \ 4438a64f336SJoe Perches printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__) 4448a64f336SJoe Perches #define pr_err_ratelimited(fmt, ...) \ 4458a64f336SJoe Perches printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__) 4468a64f336SJoe Perches #define pr_warning_ratelimited(fmt, ...) \ 4478a64f336SJoe Perches printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__) 4488a64f336SJoe Perches #define pr_notice_ratelimited(fmt, ...) \ 4498a64f336SJoe Perches printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__) 4508a64f336SJoe Perches #define pr_info_ratelimited(fmt, ...) \ 4518a64f336SJoe Perches printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) 4528a64f336SJoe Perches /* no pr_cont_ratelimited, don't do that... */ 4538a64f336SJoe Perches /* If you are writing a driver, please use dev_dbg instead */ 4548a64f336SJoe Perches #if defined(DEBUG) 4558a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \ 4568a64f336SJoe Perches printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__) 4578a64f336SJoe Perches #else 4588a64f336SJoe Perches #define pr_debug_ratelimited(fmt, ...) \ 4598a64f336SJoe Perches ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \ 4608a64f336SJoe Perches ##__VA_ARGS__); 0; }) 4618a64f336SJoe Perches #endif 4628a64f336SJoe Perches 4638a64f336SJoe Perches /* 464526211bcSIngo Molnar * General tracing related utility functions - trace_printk(), 4652002c258SSteven Rostedt * tracing_on/tracing_off and tracing_start()/tracing_stop 4662002c258SSteven Rostedt * 4672002c258SSteven Rostedt * Use tracing_on/tracing_off when you want to quickly turn on or off 4682002c258SSteven Rostedt * tracing. It simply enables or disables the recording of the trace events. 469156f5a78SGeunSik Lim * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on 4702002c258SSteven Rostedt * file, which gives a means for the kernel and userspace to interact. 4712002c258SSteven Rostedt * Place a tracing_off() in the kernel where you want tracing to end. 4722002c258SSteven Rostedt * From user space, examine the trace, and then echo 1 > tracing_on 4732002c258SSteven Rostedt * to continue tracing. 4742002c258SSteven Rostedt * 4752002c258SSteven Rostedt * tracing_stop/tracing_start has slightly more overhead. It is used 4762002c258SSteven Rostedt * by things like suspend to ram where disabling the recording of the 4772002c258SSteven Rostedt * trace is not enough, but tracing must actually stop because things 4782002c258SSteven Rostedt * like calling smp_processor_id() may crash the system. 4792002c258SSteven Rostedt * 4802002c258SSteven Rostedt * Most likely, you want to use tracing_on/tracing_off. 481526211bcSIngo Molnar */ 4822002c258SSteven Rostedt #ifdef CONFIG_RING_BUFFER 4832002c258SSteven Rostedt void tracing_on(void); 4842002c258SSteven Rostedt void tracing_off(void); 4852002c258SSteven Rostedt /* trace_off_permanent stops recording with no way to bring it back */ 4862002c258SSteven Rostedt void tracing_off_permanent(void); 4872002c258SSteven Rostedt int tracing_is_on(void); 4882002c258SSteven Rostedt #else 4892002c258SSteven Rostedt static inline void tracing_on(void) { } 4902002c258SSteven Rostedt static inline void tracing_off(void) { } 4912002c258SSteven Rostedt static inline void tracing_off_permanent(void) { } 4922002c258SSteven Rostedt static inline int tracing_is_on(void) { return 0; } 4932002c258SSteven Rostedt #endif 494526211bcSIngo Molnar #ifdef CONFIG_TRACING 495526211bcSIngo Molnar extern void tracing_start(void); 496526211bcSIngo Molnar extern void tracing_stop(void); 497526211bcSIngo Molnar extern void ftrace_off_permanent(void); 498526211bcSIngo Molnar 499526211bcSIngo Molnar extern void 500526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3); 501526211bcSIngo Molnar 502769b0441SFrederic Weisbecker static inline void __attribute__ ((format (printf, 1, 2))) 503769b0441SFrederic Weisbecker ____trace_printk_check_format(const char *fmt, ...) 504769b0441SFrederic Weisbecker { 505769b0441SFrederic Weisbecker } 506769b0441SFrederic Weisbecker #define __trace_printk_check_format(fmt, args...) \ 507769b0441SFrederic Weisbecker do { \ 508769b0441SFrederic Weisbecker if (0) \ 509769b0441SFrederic Weisbecker ____trace_printk_check_format(fmt, ##args); \ 510769b0441SFrederic Weisbecker } while (0) 511769b0441SFrederic Weisbecker 512526211bcSIngo Molnar /** 513526211bcSIngo Molnar * trace_printk - printf formatting in the ftrace buffer 514526211bcSIngo Molnar * @fmt: the printf format for printing 515526211bcSIngo Molnar * 516526211bcSIngo Molnar * Note: __trace_printk is an internal function for trace_printk and 517526211bcSIngo Molnar * the @ip is passed in via the trace_printk macro. 518526211bcSIngo Molnar * 519526211bcSIngo Molnar * This function allows a kernel developer to debug fast path sections 520526211bcSIngo Molnar * that printk is not appropriate for. By scattering in various 521526211bcSIngo Molnar * printk like tracing in the code, a developer can quickly see 522526211bcSIngo Molnar * where problems are occurring. 523526211bcSIngo Molnar * 524526211bcSIngo Molnar * This is intended as a debugging tool for the developer only. 525526211bcSIngo Molnar * Please refrain from leaving trace_printks scattered around in 526526211bcSIngo Molnar * your code. 527526211bcSIngo Molnar */ 528769b0441SFrederic Weisbecker 529769b0441SFrederic Weisbecker #define trace_printk(fmt, args...) \ 530769b0441SFrederic Weisbecker do { \ 531769b0441SFrederic Weisbecker __trace_printk_check_format(fmt, ##args); \ 53248ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 53348ead020SFrederic Weisbecker static const char *trace_printk_fmt \ 53448ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 53548ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 53648ead020SFrederic Weisbecker \ 53748ead020SFrederic Weisbecker __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ 53848ead020SFrederic Weisbecker } else \ 53948ead020SFrederic Weisbecker __trace_printk(_THIS_IP_, fmt, ##args); \ 540769b0441SFrederic Weisbecker } while (0) 541769b0441SFrederic Weisbecker 542526211bcSIngo Molnar extern int 54348ead020SFrederic Weisbecker __trace_bprintk(unsigned long ip, const char *fmt, ...) 54448ead020SFrederic Weisbecker __attribute__ ((format (printf, 2, 3))); 54548ead020SFrederic Weisbecker 54648ead020SFrederic Weisbecker extern int 547526211bcSIngo Molnar __trace_printk(unsigned long ip, const char *fmt, ...) 548526211bcSIngo Molnar __attribute__ ((format (printf, 2, 3))); 549769b0441SFrederic Weisbecker 55003889384SSteven Rostedt extern void trace_dump_stack(void); 55103889384SSteven Rostedt 55248ead020SFrederic Weisbecker /* 55348ead020SFrederic Weisbecker * The double __builtin_constant_p is because gcc will give us an error 55448ead020SFrederic Weisbecker * if we try to allocate the static variable to fmt if it is not a 55548ead020SFrederic Weisbecker * constant. Even with the outer if statement. 55648ead020SFrederic Weisbecker */ 557769b0441SFrederic Weisbecker #define ftrace_vprintk(fmt, vargs) \ 558769b0441SFrederic Weisbecker do { \ 55948ead020SFrederic Weisbecker if (__builtin_constant_p(fmt)) { \ 560769b0441SFrederic Weisbecker static const char *trace_printk_fmt \ 56148ead020SFrederic Weisbecker __attribute__((section("__trace_printk_fmt"))) = \ 56248ead020SFrederic Weisbecker __builtin_constant_p(fmt) ? fmt : NULL; \ 5637bffc23eSIngo Molnar \ 56448ead020SFrederic Weisbecker __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ 56548ead020SFrederic Weisbecker } else \ 56648ead020SFrederic Weisbecker __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ 567769b0441SFrederic Weisbecker } while (0) 568769b0441SFrederic Weisbecker 569526211bcSIngo Molnar extern int 57048ead020SFrederic Weisbecker __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); 57148ead020SFrederic Weisbecker 57248ead020SFrederic Weisbecker extern int 573526211bcSIngo Molnar __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); 574769b0441SFrederic Weisbecker 575526211bcSIngo Molnar extern void ftrace_dump(void); 576526211bcSIngo Molnar #else 577526211bcSIngo Molnar static inline void 578526211bcSIngo Molnar ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { } 579526211bcSIngo Molnar static inline int 580526211bcSIngo Molnar trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2))); 581526211bcSIngo Molnar 582526211bcSIngo Molnar static inline void tracing_start(void) { } 583526211bcSIngo Molnar static inline void tracing_stop(void) { } 584526211bcSIngo Molnar static inline void ftrace_off_permanent(void) { } 585e36c5458SSteven Rostedt static inline void trace_dump_stack(void) { } 586526211bcSIngo Molnar static inline int 587526211bcSIngo Molnar trace_printk(const char *fmt, ...) 588526211bcSIngo Molnar { 589526211bcSIngo Molnar return 0; 590526211bcSIngo Molnar } 591526211bcSIngo Molnar static inline int 592526211bcSIngo Molnar ftrace_vprintk(const char *fmt, va_list ap) 593526211bcSIngo Molnar { 594526211bcSIngo Molnar return 0; 595526211bcSIngo Molnar } 596526211bcSIngo Molnar static inline void ftrace_dump(void) { } 597769b0441SFrederic Weisbecker #endif /* CONFIG_TRACING */ 598526211bcSIngo Molnar 599526211bcSIngo Molnar /* 6001da177e4SLinus Torvalds * Display an IP address in readable format. 6011da177e4SLinus Torvalds */ 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds #define NIPQUAD(addr) \ 6041da177e4SLinus Torvalds ((unsigned char *)&addr)[0], \ 6051da177e4SLinus Torvalds ((unsigned char *)&addr)[1], \ 6061da177e4SLinus Torvalds ((unsigned char *)&addr)[2], \ 6071da177e4SLinus Torvalds ((unsigned char *)&addr)[3] 60846b86a2dSJoe Perches #define NIPQUAD_FMT "%u.%u.%u.%u" 6091da177e4SLinus Torvalds 6101da177e4SLinus Torvalds /* 611bdf4bbaaSHarvey Harrison * min()/max()/clamp() macros that also do 6121da177e4SLinus Torvalds * strict type-checking.. See the 6131da177e4SLinus Torvalds * "unnecessary" pointer comparison. 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds #define min(x, y) ({ \ 616bdf4bbaaSHarvey Harrison typeof(x) _min1 = (x); \ 617bdf4bbaaSHarvey Harrison typeof(y) _min2 = (y); \ 618bdf4bbaaSHarvey Harrison (void) (&_min1 == &_min2); \ 619bdf4bbaaSHarvey Harrison _min1 < _min2 ? _min1 : _min2; }) 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds #define max(x, y) ({ \ 622bdf4bbaaSHarvey Harrison typeof(x) _max1 = (x); \ 623bdf4bbaaSHarvey Harrison typeof(y) _max2 = (y); \ 624bdf4bbaaSHarvey Harrison (void) (&_max1 == &_max2); \ 625bdf4bbaaSHarvey Harrison _max1 > _max2 ? _max1 : _max2; }) 626bdf4bbaaSHarvey Harrison 627bdf4bbaaSHarvey Harrison /** 628bdf4bbaaSHarvey Harrison * clamp - return a value clamped to a given range with strict typechecking 629bdf4bbaaSHarvey Harrison * @val: current value 630bdf4bbaaSHarvey Harrison * @min: minimum allowable value 631bdf4bbaaSHarvey Harrison * @max: maximum allowable value 632bdf4bbaaSHarvey Harrison * 633bdf4bbaaSHarvey Harrison * This macro does strict typechecking of min/max to make sure they are of the 634bdf4bbaaSHarvey Harrison * same type as val. See the unnecessary pointer comparisons. 635bdf4bbaaSHarvey Harrison */ 636bdf4bbaaSHarvey Harrison #define clamp(val, min, max) ({ \ 637bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 638bdf4bbaaSHarvey Harrison typeof(min) __min = (min); \ 639bdf4bbaaSHarvey Harrison typeof(max) __max = (max); \ 640bdf4bbaaSHarvey Harrison (void) (&__val == &__min); \ 641bdf4bbaaSHarvey Harrison (void) (&__val == &__max); \ 642bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 643bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* 6461da177e4SLinus Torvalds * ..and if you can't take the strict 6471da177e4SLinus Torvalds * types, you can specify one yourself. 6481da177e4SLinus Torvalds * 649bdf4bbaaSHarvey Harrison * Or not use min/max/clamp at all, of course. 6501da177e4SLinus Torvalds */ 651bdf4bbaaSHarvey Harrison #define min_t(type, x, y) ({ \ 652bdf4bbaaSHarvey Harrison type __min1 = (x); \ 653bdf4bbaaSHarvey Harrison type __min2 = (y); \ 654bdf4bbaaSHarvey Harrison __min1 < __min2 ? __min1: __min2; }) 6551da177e4SLinus Torvalds 656bdf4bbaaSHarvey Harrison #define max_t(type, x, y) ({ \ 657bdf4bbaaSHarvey Harrison type __max1 = (x); \ 658bdf4bbaaSHarvey Harrison type __max2 = (y); \ 659bdf4bbaaSHarvey Harrison __max1 > __max2 ? __max1: __max2; }) 660bdf4bbaaSHarvey Harrison 661bdf4bbaaSHarvey Harrison /** 662bdf4bbaaSHarvey Harrison * clamp_t - return a value clamped to a given range using a given type 663bdf4bbaaSHarvey Harrison * @type: the type of variable to use 664bdf4bbaaSHarvey Harrison * @val: current value 665bdf4bbaaSHarvey Harrison * @min: minimum allowable value 666bdf4bbaaSHarvey Harrison * @max: maximum allowable value 667bdf4bbaaSHarvey Harrison * 668bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of type 669bdf4bbaaSHarvey Harrison * 'type' to make all the comparisons. 670bdf4bbaaSHarvey Harrison */ 671bdf4bbaaSHarvey Harrison #define clamp_t(type, val, min, max) ({ \ 672bdf4bbaaSHarvey Harrison type __val = (val); \ 673bdf4bbaaSHarvey Harrison type __min = (min); \ 674bdf4bbaaSHarvey Harrison type __max = (max); \ 675bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 676bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 677bdf4bbaaSHarvey Harrison 678bdf4bbaaSHarvey Harrison /** 679bdf4bbaaSHarvey Harrison * clamp_val - return a value clamped to a given range using val's type 680bdf4bbaaSHarvey Harrison * @val: current value 681bdf4bbaaSHarvey Harrison * @min: minimum allowable value 682bdf4bbaaSHarvey Harrison * @max: maximum allowable value 683bdf4bbaaSHarvey Harrison * 684bdf4bbaaSHarvey Harrison * This macro does no typechecking and uses temporary variables of whatever 685bdf4bbaaSHarvey Harrison * type the input argument 'val' is. This is useful when val is an unsigned 686bdf4bbaaSHarvey Harrison * type and min and max are literals that will otherwise be assigned a signed 687bdf4bbaaSHarvey Harrison * integer type. 688bdf4bbaaSHarvey Harrison */ 689bdf4bbaaSHarvey Harrison #define clamp_val(val, min, max) ({ \ 690bdf4bbaaSHarvey Harrison typeof(val) __val = (val); \ 691bdf4bbaaSHarvey Harrison typeof(val) __min = (min); \ 692bdf4bbaaSHarvey Harrison typeof(val) __max = (max); \ 693bdf4bbaaSHarvey Harrison __val = __val < __min ? __min: __val; \ 694bdf4bbaaSHarvey Harrison __val > __max ? __max: __val; }) 6951da177e4SLinus Torvalds 69691f68b73SWu Fengguang 69791f68b73SWu Fengguang /* 69891f68b73SWu Fengguang * swap - swap value of @a and @b 69991f68b73SWu Fengguang */ 700ac7b9004SPeter Zijlstra #define swap(a, b) \ 701ac7b9004SPeter Zijlstra do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0) 70291f68b73SWu Fengguang 7031da177e4SLinus Torvalds /** 7041da177e4SLinus Torvalds * container_of - cast a member of a structure out to the containing structure 7051da177e4SLinus Torvalds * @ptr: the pointer to the member. 7061da177e4SLinus Torvalds * @type: the type of the container struct this is embedded in. 7071da177e4SLinus Torvalds * @member: the name of the member within the struct. 7081da177e4SLinus Torvalds * 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds #define container_of(ptr, type, member) ({ \ 7111da177e4SLinus Torvalds const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 7121da177e4SLinus Torvalds (type *)( (char *)__mptr - offsetof(type,member) );}) 7131da177e4SLinus Torvalds 714d4d23addSKyle McMartin struct sysinfo; 715d4d23addSKyle McMartin extern int do_sysinfo(struct sysinfo *info); 716d4d23addSKyle McMartin 7171da177e4SLinus Torvalds #endif /* __KERNEL__ */ 7181da177e4SLinus Torvalds 719c01226c3SArnd Bergmann #ifndef __EXPORTED_HEADERS__ 720c01226c3SArnd Bergmann #ifndef __KERNEL__ 721c01226c3SArnd Bergmann #warning Attempt to use kernel headers from user space, see http://kernelnewbies.org/KernelHeaders 722c01226c3SArnd Bergmann #endif /* __KERNEL__ */ 723c01226c3SArnd Bergmann #endif /* __EXPORTED_HEADERS__ */ 724c01226c3SArnd Bergmann 7251da177e4SLinus Torvalds #define SI_LOAD_SHIFT 16 7261da177e4SLinus Torvalds struct sysinfo { 7271da177e4SLinus Torvalds long uptime; /* Seconds since boot */ 7281da177e4SLinus Torvalds unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ 7291da177e4SLinus Torvalds unsigned long totalram; /* Total usable main memory size */ 7301da177e4SLinus Torvalds unsigned long freeram; /* Available memory size */ 7311da177e4SLinus Torvalds unsigned long sharedram; /* Amount of shared memory */ 7321da177e4SLinus Torvalds unsigned long bufferram; /* Memory used by buffers */ 7331da177e4SLinus Torvalds unsigned long totalswap; /* Total swap space size */ 7341da177e4SLinus Torvalds unsigned long freeswap; /* swap space still available */ 7351da177e4SLinus Torvalds unsigned short procs; /* Number of current processes */ 7361da177e4SLinus Torvalds unsigned short pad; /* explicit padding for m68k */ 7371da177e4SLinus Torvalds unsigned long totalhigh; /* Total high memory size */ 7381da177e4SLinus Torvalds unsigned long freehigh; /* Available high memory size */ 7391da177e4SLinus Torvalds unsigned int mem_unit; /* Memory unit size in bytes */ 7401da177e4SLinus Torvalds char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */ 7411da177e4SLinus Torvalds }; 7421da177e4SLinus Torvalds 743c0398ee6SNikita Danilov /* Force a compilation error if condition is true */ 7448c87df45SJan Beulich #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition)) 7458c87df45SJan Beulich 7468c87df45SJan Beulich /* Force a compilation error if condition is constant and true */ 7478c87df45SJan Beulich #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) 7481da177e4SLinus Torvalds 749cc8ef6ebSRoland Dreier /* Force a compilation error if a constant expression is not a power of 2 */ 750cc8ef6ebSRoland Dreier #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \ 751cc8ef6ebSRoland Dreier BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0)) 752cc8ef6ebSRoland Dreier 7534552d5dcSJan Beulich /* Force a compilation error if condition is true, but also produce a 7544552d5dcSJan Beulich result (of value 0 and type size_t), so the expression can be used 7554552d5dcSJan Beulich e.g. in a structure initializer (or where-ever else comma expressions 7564552d5dcSJan Beulich aren't permitted). */ 7578c87df45SJan Beulich #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) 7588c87df45SJan Beulich #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); })) 7594552d5dcSJan Beulich 7601da177e4SLinus Torvalds /* Trap pasters of __FUNCTION__ at compile-time */ 7611da177e4SLinus Torvalds #define __FUNCTION__ (__func__) 7621da177e4SLinus Torvalds 76308e0f6a9SChristoph Lameter /* This helps us to avoid #ifdef CONFIG_NUMA */ 76408e0f6a9SChristoph Lameter #ifdef CONFIG_NUMA 76508e0f6a9SChristoph Lameter #define NUMA_BUILD 1 76608e0f6a9SChristoph Lameter #else 76708e0f6a9SChristoph Lameter #define NUMA_BUILD 0 76808e0f6a9SChristoph Lameter #endif 76908e0f6a9SChristoph Lameter 77029e71abfSSteven Rostedt /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ 77129e71abfSSteven Rostedt #ifdef CONFIG_FTRACE_MCOUNT_RECORD 77229e71abfSSteven Rostedt # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD 77329e71abfSSteven Rostedt #endif 77429e71abfSSteven Rostedt 7751da177e4SLinus Torvalds #endif 776