blob: b526947bdf489f508c6905b2009005b2220a8643 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4/*
5 * 'kernel.h' contains some often-used function prototypes etc
6 */
Alexey Dobriyana79ff732010-04-13 11:21:46 +02007#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#ifdef __KERNEL__
11
12#include <stdarg.h>
13#include <linux/linkage.h>
14#include <linux/stddef.h>
15#include <linux/types.h>
16#include <linux/compiler.h>
17#include <linux/bitops.h>
David Howellsf0d1b0b2006-12-08 02:37:49 -080018#include <linux/log2.h>
Andrew Mortone0deaff42008-07-25 01:45:24 -070019#include <linux/typecheck.h>
Jason Barone9d376f2009-02-05 11:51:38 -050020#include <linux/dynamic_debug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/byteorder.h>
22#include <asm/bug.h>
23
Roman Zippel3eb3c742007-01-10 14:45:28 +010024extern const char linux_banner[];
25extern const char linux_proc_banner[];
26
Alexey Dobriyan4be929b2010-05-24 14:33:03 -070027#define USHRT_MAX ((u16)(~0U))
28#define SHRT_MAX ((s16)(USHRT_MAX>>1))
29#define SHRT_MIN ((s16)(-SHRT_MAX - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define INT_MAX ((int)(~0U>>1))
31#define INT_MIN (-INT_MAX - 1)
32#define UINT_MAX (~0U)
33#define LONG_MAX ((long)(~0UL>>1))
34#define LONG_MIN (-LONG_MAX - 1)
35#define ULONG_MAX (~0UL)
OGAWA Hirofumi111ebb62006-06-23 02:03:26 -070036#define LLONG_MAX ((long long)(~0ULL>>1))
37#define LLONG_MIN (-LLONG_MAX - 1)
38#define ULLONG_MAX (~0ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define STACK_MAGIC 0xdeadbeef
41
Alexey Dobriyana79ff732010-04-13 11:21:46 +020042#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
Alexey Dobriyan9f93ff52010-04-13 14:09:15 +020043#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
Matthew Wilcoxa83308e2007-09-11 15:23:47 -070044#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
Herbert Xuf10db622008-02-06 01:37:05 -080045#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
Linus Torvalds2ea58142006-11-26 19:05:22 -080046
Rusty Russellc5e631cf2007-05-06 14:51:05 -070047#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
48
Yinghai Lu9b3be9f2010-02-10 01:20:29 -080049/*
50 * This looks more complex than it should be. But we need to
51 * get the type for the ~ right in round_down (it needs to be
52 * as wide as the result!), and we want to evaluate the macro
53 * arguments just once each.
54 */
55#define __round_mask(x, y) ((__typeof__(x))((y)-1))
56#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
57#define round_down(x, y) ((x) & ~__round_mask(x, y))
58
Jan Beulich4552d5d2006-06-26 13:57:28 +020059#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
Steven Whitehouse930631e2006-09-25 23:32:40 -070060#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
Eric Parisb28efd52010-10-13 17:50:08 -040061#define roundup(x, y) ( \
62{ \
Tetsuo Handa6070bf32010-11-08 11:20:49 +090063 const typeof(y) __y = y; \
Eric Parisb28efd52010-10-13 17:50:08 -040064 (((x) + (__y - 1)) / __y) * __y; \
65} \
66)
Eric Paris686a0f32010-10-13 17:50:02 -040067#define rounddown(x, y) ( \
68{ \
69 typeof(x) __x = (x); \
70 __x - (__x % (y)); \
71} \
72)
Darrick J. Wong9fe06082009-01-06 14:40:51 -080073#define DIV_ROUND_CLOSEST(x, divisor)( \
74{ \
75 typeof(divisor) __divisor = divisor; \
76 (((x) + ((__divisor) / 2)) / (__divisor)); \
77} \
78)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Eduard - Gabriel Munteanuca31e142008-07-05 12:14:23 +030080#define _RET_IP_ (unsigned long)__builtin_return_address(0)
81#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
82
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +020083#ifdef CONFIG_LBDAF
Jens Axboe2da96ac2007-10-12 12:40:38 +020084# include <asm/div64.h>
85# define sector_div(a, b) do_div(a, b)
86#else
87# define sector_div(n, b)( \
88{ \
89 int _res; \
90 _res = (n) % (b); \
91 (n) /= (b); \
92 _res; \
93} \
94)
95#endif
96
Andrew Morton218e1802007-05-10 03:15:18 -070097/**
98 * upper_32_bits - return bits 32-63 of a number
99 * @n: the number we're accessing
100 *
101 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
102 * the "right shift count >= width of type" warning when that quantity is
103 * 32-bits.
104 */
105#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
106
Joerg Roedel204b8852008-07-29 22:33:42 -0700107/**
108 * lower_32_bits - return bits 0-31 of a number
109 * @n: the number we're accessing
110 */
111#define lower_32_bits(n) ((u32)(n))
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define KERN_EMERG "<0>" /* system is unusable */
114#define KERN_ALERT "<1>" /* action must be taken immediately */
115#define KERN_CRIT "<2>" /* critical conditions */
116#define KERN_ERR "<3>" /* error conditions */
117#define KERN_WARNING "<4>" /* warning conditions */
118#define KERN_NOTICE "<5>" /* normal but significant condition */
119#define KERN_INFO "<6>" /* informational */
120#define KERN_DEBUG "<7>" /* debug-level messages */
121
Linus Torvaldse28d7132009-06-16 11:02:28 -0700122/* Use the default kernel loglevel */
123#define KERN_DEFAULT "<d>"
Ingo Molnar47492522007-10-16 23:30:29 -0700124/*
125 * Annotation for a "continued" line of log printout (only done after a
126 * line that had no enclosing \n). Only to be used by core/arch code
127 * during early bootup (a continued line is not SMP-safe otherwise).
128 */
Linus Torvalds5fd29d62009-06-16 10:57:02 -0700129#define KERN_CONT "<c>"
Ingo Molnar47492522007-10-16 23:30:29 -0700130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131extern int console_printk[];
132
133#define console_loglevel (console_printk[0])
134#define default_message_loglevel (console_printk[1])
135#define minimum_console_loglevel (console_printk[2])
136#define default_console_loglevel (console_printk[3])
137
138struct completion;
akpm@osdl.orgdf2e71f2006-01-09 20:51:37 -0800139struct pt_regs;
140struct user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Uwe Kleine-König070cb062008-08-12 15:08:59 -0700142#ifdef CONFIG_PREEMPT_VOLUNTARY
143extern int _cond_resched(void);
144# define might_resched() _cond_resched()
145#else
146# define might_resched() do { } while (0)
147#endif
148
Randy Dunlap7106a272008-10-29 14:01:15 -0700149#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
Simon Kagstromd8948372009-12-23 11:08:18 +0100150 void __might_sleep(const char *file, int line, int preempt_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/**
152 * might_sleep - annotation for functions that can sleep
153 *
154 * this macro will print a stack trace if it is executed in an atomic
155 * context (spinlock, irq-handler, ...).
156 *
157 * This is a useful debugging help to be able to catch problems early and not
Jim Cromiee20ec992006-11-30 04:46:13 +0100158 * be bitten later when the calling function happens to sleep when it is not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 * supposed to.
160 */
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700161# define might_sleep() \
Frederic Weisbeckere4aafea2009-07-16 15:44:29 +0200162 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700163#else
Simon Kagstromd8948372009-12-23 11:08:18 +0100164 static inline void __might_sleep(const char *file, int line,
165 int preempt_offset) { }
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700166# define might_sleep() do { might_resched(); } while (0)
167#endif
168
Hua Zhong368a5fa2006-06-23 02:05:42 -0700169#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
Ingo Molnarf8cbd992005-06-25 14:57:39 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171#define abs(x) ({ \
Rolf Eike Beera49c59c2009-09-22 16:44:03 -0700172 long __x = (x); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 (__x < 0) ? -__x : __x; \
174 })
175
Brian Behlendorf658716d2010-10-26 14:23:10 -0700176#define abs64(x) ({ \
177 s64 __x = (x); \
178 (__x < 0) ? -__x : __x; \
179 })
180
Nick Piggin3ee1afa2008-09-10 13:37:17 +0200181#ifdef CONFIG_PROVE_LOCKING
182void might_fault(void);
183#else
184static inline void might_fault(void)
185{
186 might_sleep();
187}
188#endif
189
Joe Perches7db6f5f2010-06-27 01:02:33 +0000190struct va_format {
191 const char *fmt;
192 va_list *va;
193};
194
Alan Sterne041c682006-03-27 01:16:30 -0800195extern struct atomic_notifier_head panic_notifier_list;
TAMUKI Shoichic7ff0d92010-08-10 18:03:28 -0700196extern long (*panic_blink)(int state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197NORET_TYPE void panic(const char * fmt, ...)
Andi Kleena586df02007-07-21 17:10:00 +0200198 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
Andrew Mortondd287792006-03-23 03:00:57 -0800199extern void oops_enter(void);
200extern void oops_exit(void);
Anton Blanchard863a6042010-08-10 18:03:30 -0700201void print_oops_end_marker(void);
Andrew Mortondd287792006-03-23 03:00:57 -0800202extern int oops_may_print(void);
Harvey Harrisonec701582008-02-08 04:19:55 -0800203NORET_TYPE void do_exit(long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 ATTRIB_NORET;
205NORET_TYPE void complete_and_exit(struct completion *, long)
206 ATTRIB_NORET;
207extern unsigned long simple_strtoul(const char *,char **,unsigned int);
208extern long simple_strtol(const char *,char **,unsigned int);
209extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
210extern long long simple_strtoll(const char *,char **,unsigned int);
Andrew Mortona55621f2010-10-26 14:22:25 -0700211extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
212extern int __must_check strict_strtol(const char *, unsigned int, long *);
213extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
214extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215extern int sprintf(char * buf, const char * fmt, ...)
216 __attribute__ ((format (printf, 2, 3)));
217extern int vsprintf(char *buf, const char *, va_list)
218 __attribute__ ((format (printf, 2, 0)));
219extern int snprintf(char * buf, size_t size, const char * fmt, ...)
220 __attribute__ ((format (printf, 3, 4)));
221extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
222 __attribute__ ((format (printf, 3, 0)));
223extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
224 __attribute__ ((format (printf, 3, 4)));
225extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
226 __attribute__ ((format (printf, 3, 0)));
Jeremy Fitzhardingee9059142006-06-25 05:49:17 -0700227extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
228 __attribute__ ((format (printf, 2, 3)));
Jeremy Fitzhardinge11443ec2007-04-30 15:09:56 -0700229extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231extern int sscanf(const char *, const char *, ...)
232 __attribute__ ((format (scanf, 2, 3)));
233extern int vsscanf(const char *, const char *, va_list)
234 __attribute__ ((format (scanf, 2, 0)));
235
236extern int get_option(char **str, int *pint);
237extern char *get_options(const char *str, int nints, int *ints);
Jeremy Fitzhardinged974ae32008-07-24 16:27:46 -0700238extern unsigned long long memparse(const char *ptr, char **retptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Trent Piepho5e376612006-05-15 09:44:06 -0700240extern int core_kernel_text(unsigned long addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241extern int __kernel_text_address(unsigned long addr);
242extern int kernel_text_address(unsigned long addr);
Arjan van de Venab7476c2008-08-15 15:29:38 -0700243extern int func_ptr_is_kernel_text(void *ptr);
Arjan van de Venab7476c2008-08-15 15:29:38 -0700244
Eric W. Biederman04a2e6a2007-02-12 00:52:56 -0800245struct pid;
246extern struct pid *session_of_pgrp(struct pid *pgrp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Thomas Renningera0ad05c2008-09-01 14:27:02 +0200248/*
249 * FW_BUG
250 * Add this to a message where you are sure the firmware is buggy or behaves
251 * really stupid or out of spec. Be aware that the responsible BIOS developer
252 * should be able to fix this issue or at least get a concrete idea of the
253 * problem by reading your message without the need of looking at the kernel
254 * code.
255 *
256 * Use it for definite and high priority BIOS bugs.
257 *
258 * FW_WARN
259 * Use it for not that clear (e.g. could the kernel messed up things already?)
260 * and medium priority BIOS bugs.
261 *
262 * FW_INFO
263 * Use this one if you want to tell the user or vendor about something
264 * suspicious, but generally harmless related to the firmware.
265 *
266 * Use it for information or very low priority BIOS bugs.
267 */
268#define FW_BUG "[Firmware Bug]: "
269#define FW_WARN "[Firmware Warn]: "
270#define FW_INFO "[Firmware Info]: "
271
Huang Yingc6de9f02010-05-31 16:48:09 +0800272/*
273 * HW_ERR
274 * Add this to a message for hardware errors, so that user can report
275 * it to hardware vendor instead of LKML or software vendor.
276 */
277#define HW_ERR "[Hardware Error]: "
278
Matt Mackalld59745c2005-05-01 08:59:02 -0700279#ifdef CONFIG_PRINTK
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280asmlinkage int vprintk(const char *fmt, va_list args)
281 __attribute__ ((format (printf, 1, 0)));
282asmlinkage int printk(const char * fmt, ...)
Andi Kleena586df02007-07-21 17:10:00 +0200283 __attribute__ ((format (printf, 1, 2))) __cold;
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800284
Andrew Morton77006a02010-10-26 14:22:49 -0700285/*
286 * Please don't use printk_ratelimit(), because it shares ratelimiting state
287 * with all other unrelated printk_ratelimit() callsites. Instead use
288 * printk_ratelimited() or plain old __ratelimit().
289 */
Christian Borntraeger5c828712009-10-23 14:58:11 +0200290extern int __printk_ratelimit(const char *func);
291#define printk_ratelimit() __printk_ratelimit(__func__)
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800292extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
293 unsigned int interval_msec);
Ingo Molnarf036be92009-02-05 13:45:43 +0100294
Dave Youngaf913222009-09-22 16:43:33 -0700295extern int printk_delay_msec;
296
Ingo Molnarf036be92009-02-05 13:45:43 +0100297/*
298 * Print a one-time message (analogous to WARN_ONCE() et al):
299 */
300#define printk_once(x...) ({ \
Joe Perches0b2749a2009-12-14 18:00:19 -0800301 static bool __print_once; \
Ingo Molnarf036be92009-02-05 13:45:43 +0100302 \
Joe Perches0b2749a2009-12-14 18:00:19 -0800303 if (!__print_once) { \
304 __print_once = true; \
Ingo Molnarf036be92009-02-05 13:45:43 +0100305 printk(x); \
306 } \
307})
308
Neil Horman04d491a2009-04-02 16:58:57 -0700309void log_buf_kexec_setup(void);
Matt Mackalld59745c2005-05-01 08:59:02 -0700310#else
311static inline int vprintk(const char *s, va_list args)
312 __attribute__ ((format (printf, 1, 0)));
313static inline int vprintk(const char *s, va_list args) { return 0; }
314static inline int printk(const char *s, ...)
315 __attribute__ ((format (printf, 1, 2)));
Andi Kleena586df02007-07-21 17:10:00 +0200316static inline int __cold printk(const char *s, ...) { return 0; }
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800317static inline int printk_ratelimit(void) { return 0; }
Joe Perches7ef3d2f2008-02-08 04:21:25 -0800318static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
319 unsigned int interval_msec) \
320 { return false; }
Ingo Molnarf036be92009-02-05 13:45:43 +0100321
322/* No effect, but we still get type checking even in the !PRINTK case: */
323#define printk_once(x...) printk(x)
324
Neil Horman04d491a2009-04-02 16:58:57 -0700325static inline void log_buf_kexec_setup(void)
326{
327}
Matt Mackalld59745c2005-05-01 08:59:02 -0700328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
David Howells12fdff32010-08-12 16:54:57 +0100330/*
331 * Dummy printk for disabled debugging statements to use whilst maintaining
332 * gcc's format and side-effect checking.
333 */
334static inline __attribute__ ((format (printf, 1, 2)))
335int no_printk(const char *s, ...) { return 0; }
336
Ingo Molnarced9cd42008-08-11 14:38:12 +0200337extern int printk_needs_cpu(int cpu);
338extern void printk_tick(void);
339
Jiri Slabye17ba732008-05-12 15:44:40 +0200340extern void asmlinkage __attribute__((format(printf, 1, 2)))
Ingo Molnar076f9772008-01-30 13:33:06 +0100341 early_printk(const char *fmt, ...);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343unsigned long int_sqrt(unsigned long);
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static inline void console_silent(void)
346{
347 console_loglevel = 0;
348}
349
350static inline void console_verbose(void)
351{
352 if (console_loglevel)
353 console_loglevel = 15;
354}
355
356extern void bust_spinlocks(int yes);
Kirill Korotaeve3e8a752007-02-10 01:46:19 -0800357extern void wake_up_klogd(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
Adrian Bunkaa727102006-04-10 22:53:59 -0700359extern int panic_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360extern int panic_on_oops;
Don Zickus8da5add2006-09-26 10:52:27 +0200361extern int panic_on_unrecovered_nmi;
Kurt Garloff5211a242009-06-24 14:32:11 -0700362extern int panic_on_io_nmi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363extern const char *print_tainted(void);
Andi Kleen25ddbb12008-10-15 22:01:41 -0700364extern void add_taint(unsigned flag);
365extern int test_taint(unsigned flag);
366extern unsigned long get_taint(void);
David Howellsb920de12008-02-08 04:19:31 -0800367extern int root_mountflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369/* Values used for system_state */
370extern enum system_states {
371 SYSTEM_BOOTING,
372 SYSTEM_RUNNING,
373 SYSTEM_HALT,
374 SYSTEM_POWER_OFF,
375 SYSTEM_RESTART,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500376 SYSTEM_SUSPEND_DISK,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377} system_state;
378
Andi Kleen25ddbb12008-10-15 22:01:41 -0700379#define TAINT_PROPRIETARY_MODULE 0
380#define TAINT_FORCED_MODULE 1
381#define TAINT_UNSAFE_SMP 2
382#define TAINT_FORCED_RMMOD 3
383#define TAINT_MACHINE_CHECK 4
384#define TAINT_BAD_PAGE 5
385#define TAINT_USER 6
386#define TAINT_DIE 7
387#define TAINT_OVERRIDDEN_ACPI_TABLE 8
388#define TAINT_WARN 9
Linus Torvalds26e9a392008-10-17 09:50:12 -0700389#define TAINT_CRAP 10
Ben Hutchings92946bc2010-04-03 19:36:42 +0100390#define TAINT_FIRMWARE_WORKAROUND 11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Andi Kleena586df02007-07-21 17:10:00 +0200392extern void dump_stack(void) __cold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Randy Dunlap99eaf3c2007-05-10 22:22:39 -0700394enum {
395 DUMP_PREFIX_NONE,
396 DUMP_PREFIX_ADDRESS,
397 DUMP_PREFIX_OFFSET
398};
Randy Dunlapc7909232007-06-08 13:47:04 -0700399extern void hex_dump_to_buffer(const void *buf, size_t len,
400 int rowsize, int groupsize,
401 char *linebuf, size_t linebuflen, bool ascii);
402extern void print_hex_dump(const char *level, const char *prefix_str,
403 int prefix_type, int rowsize, int groupsize,
Artem Bityutskiy6a0ed912007-08-07 23:43:14 +0300404 const void *buf, size_t len, bool ascii);
Randy Dunlapc7909232007-06-08 13:47:04 -0700405extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
Alan Sterneb9a9a52007-08-10 13:01:07 -0700406 const void *buf, size_t len);
Harvey Harrison3fc95772008-05-14 16:05:49 -0700407
408extern const char hex_asc[];
409#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
410#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
411
412static inline char *pack_hex_byte(char *buf, u8 byte)
413{
414 *buf++ = hex_asc_hi(byte);
415 *buf++ = hex_asc_lo(byte);
416 return buf;
417}
Randy Dunlap99eaf3c2007-05-10 22:22:39 -0700418
Andy Shevchenko903788892010-05-24 14:33:23 -0700419extern int hex_to_bin(char ch);
420
Martin Schwidefskyd091c2f2008-11-12 21:16:43 +0100421#ifndef pr_fmt
422#define pr_fmt(fmt) fmt
423#endif
424
425#define pr_emerg(fmt, ...) \
426 printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
427#define pr_alert(fmt, ...) \
428 printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
429#define pr_crit(fmt, ...) \
430 printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
431#define pr_err(fmt, ...) \
432 printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
433#define pr_warning(fmt, ...) \
434 printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Joe Perchesfc62f2f2010-05-24 14:33:08 -0700435#define pr_warn pr_warning
Martin Schwidefskyd091c2f2008-11-12 21:16:43 +0100436#define pr_notice(fmt, ...) \
437 printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
438#define pr_info(fmt, ...) \
439 printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
Cyrill Gorcunov311d0762009-03-31 15:23:51 -0700440#define pr_cont(fmt, ...) \
441 printk(KERN_CONT fmt, ##__VA_ARGS__)
Emil Medve1f7c8232007-10-16 23:29:48 -0700442
Michael Ellerman4ccb4572009-04-09 14:48:24 -0700443/* pr_devel() should produce zero code unless DEBUG is defined */
444#ifdef DEBUG
445#define pr_devel(fmt, ...) \
446 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
447#else
448#define pr_devel(fmt, ...) \
449 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
450#endif
451
Pavel Machek1cc6daf2006-08-08 01:37:15 +0200452/* If you are writing a driver, please use dev_dbg instead */
Cornelia Huckd0d85ff2008-12-04 16:55:47 +0100453#if defined(DEBUG)
454#define pr_debug(fmt, ...) \
455 printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
Jason Barone9d376f2009-02-05 11:51:38 -0500456#elif defined(CONFIG_DYNAMIC_DEBUG)
Greg Bankse6e66b02009-03-11 21:07:28 +1100457/* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
Joe Perches00b55862009-12-14 18:00:14 -0800458#define pr_debug(fmt, ...) \
459 dynamic_pr_debug(fmt, ##__VA_ARGS__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#else
Martin Schwidefskyd091c2f2008-11-12 21:16:43 +0100461#define pr_debug(fmt, ...) \
462 ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463#endif
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
Joe Perches8a64f332009-12-14 18:00:25 -0800466 * ratelimited messages with local ratelimit_state,
467 * no local ratelimit_state used in the !PRINTK case
468 */
469#ifdef CONFIG_PRINTK
OGAWA Hirofumid8521fc2010-05-24 14:33:11 -0700470#define printk_ratelimited(fmt, ...) ({ \
471 static DEFINE_RATELIMIT_STATE(_rs, \
472 DEFAULT_RATELIMIT_INTERVAL, \
473 DEFAULT_RATELIMIT_BURST); \
474 \
475 if (__ratelimit(&_rs)) \
476 printk(fmt, ##__VA_ARGS__); \
Joe Perches8a64f332009-12-14 18:00:25 -0800477})
478#else
479/* No effect, but we still get type checking even in the !PRINTK case: */
480#define printk_ratelimited printk
481#endif
482
483#define pr_emerg_ratelimited(fmt, ...) \
484 printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
485#define pr_alert_ratelimited(fmt, ...) \
486 printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
487#define pr_crit_ratelimited(fmt, ...) \
488 printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
489#define pr_err_ratelimited(fmt, ...) \
490 printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
491#define pr_warning_ratelimited(fmt, ...) \
492 printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
Joe Perchesfc62f2f2010-05-24 14:33:08 -0700493#define pr_warn_ratelimited pr_warning_ratelimited
Joe Perches8a64f332009-12-14 18:00:25 -0800494#define pr_notice_ratelimited(fmt, ...) \
495 printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
496#define pr_info_ratelimited(fmt, ...) \
497 printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
498/* no pr_cont_ratelimited, don't do that... */
499/* If you are writing a driver, please use dev_dbg instead */
500#if defined(DEBUG)
501#define pr_debug_ratelimited(fmt, ...) \
502 printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
503#else
504#define pr_debug_ratelimited(fmt, ...) \
505 ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
506 ##__VA_ARGS__); 0; })
507#endif
508
509/*
Ingo Molnar526211b2009-03-05 10:28:45 +0100510 * General tracing related utility functions - trace_printk(),
Steven Rostedt2002c252009-03-05 10:35:56 -0500511 * tracing_on/tracing_off and tracing_start()/tracing_stop
512 *
513 * Use tracing_on/tracing_off when you want to quickly turn on or off
514 * tracing. It simply enables or disables the recording of the trace events.
GeunSik Lim156f5a72009-06-02 15:01:37 +0900515 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
Steven Rostedt2002c252009-03-05 10:35:56 -0500516 * file, which gives a means for the kernel and userspace to interact.
517 * Place a tracing_off() in the kernel where you want tracing to end.
518 * From user space, examine the trace, and then echo 1 > tracing_on
519 * to continue tracing.
520 *
521 * tracing_stop/tracing_start has slightly more overhead. It is used
522 * by things like suspend to ram where disabling the recording of the
523 * trace is not enough, but tracing must actually stop because things
524 * like calling smp_processor_id() may crash the system.
525 *
526 * Most likely, you want to use tracing_on/tracing_off.
Ingo Molnar526211b2009-03-05 10:28:45 +0100527 */
Steven Rostedt2002c252009-03-05 10:35:56 -0500528#ifdef CONFIG_RING_BUFFER
529void tracing_on(void);
530void tracing_off(void);
531/* trace_off_permanent stops recording with no way to bring it back */
532void tracing_off_permanent(void);
533int tracing_is_on(void);
534#else
535static inline void tracing_on(void) { }
536static inline void tracing_off(void) { }
537static inline void tracing_off_permanent(void) { }
538static inline int tracing_is_on(void) { return 0; }
539#endif
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200540
541enum ftrace_dump_mode {
542 DUMP_NONE,
543 DUMP_ALL,
544 DUMP_ORIG,
545};
546
Ingo Molnar526211b2009-03-05 10:28:45 +0100547#ifdef CONFIG_TRACING
548extern void tracing_start(void);
549extern void tracing_stop(void);
550extern void ftrace_off_permanent(void);
551
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100552static inline void __attribute__ ((format (printf, 1, 2)))
553____trace_printk_check_format(const char *fmt, ...)
554{
555}
556#define __trace_printk_check_format(fmt, args...) \
557do { \
558 if (0) \
559 ____trace_printk_check_format(fmt, ##args); \
560} while (0)
561
Ingo Molnar526211b2009-03-05 10:28:45 +0100562/**
563 * trace_printk - printf formatting in the ftrace buffer
564 * @fmt: the printf format for printing
565 *
566 * Note: __trace_printk is an internal function for trace_printk and
567 * the @ip is passed in via the trace_printk macro.
568 *
569 * This function allows a kernel developer to debug fast path sections
570 * that printk is not appropriate for. By scattering in various
571 * printk like tracing in the code, a developer can quickly see
572 * where problems are occurring.
573 *
574 * This is intended as a debugging tool for the developer only.
575 * Please refrain from leaving trace_printks scattered around in
576 * your code.
577 */
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100578
579#define trace_printk(fmt, args...) \
580do { \
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100581 __trace_printk_check_format(fmt, ##args); \
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100582 if (__builtin_constant_p(fmt)) { \
583 static const char *trace_printk_fmt \
584 __attribute__((section("__trace_printk_fmt"))) = \
585 __builtin_constant_p(fmt) ? fmt : NULL; \
586 \
587 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
588 } else \
589 __trace_printk(_THIS_IP_, fmt, ##args); \
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100590} while (0)
591
Ingo Molnar526211b2009-03-05 10:28:45 +0100592extern int
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100593__trace_bprintk(unsigned long ip, const char *fmt, ...)
594 __attribute__ ((format (printf, 2, 3)));
595
596extern int
Ingo Molnar526211b2009-03-05 10:28:45 +0100597__trace_printk(unsigned long ip, const char *fmt, ...)
598 __attribute__ ((format (printf, 2, 3)));
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100599
Steven Rostedt03889382009-12-11 09:48:22 -0500600extern void trace_dump_stack(void);
601
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100602/*
603 * The double __builtin_constant_p is because gcc will give us an error
604 * if we try to allocate the static variable to fmt if it is not a
605 * constant. Even with the outer if statement.
606 */
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100607#define ftrace_vprintk(fmt, vargs) \
608do { \
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100609 if (__builtin_constant_p(fmt)) { \
610 static const char *trace_printk_fmt \
611 __attribute__((section("__trace_printk_fmt"))) = \
612 __builtin_constant_p(fmt) ? fmt : NULL; \
Ingo Molnar7bffc232009-03-09 10:11:36 +0100613 \
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100614 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
615 } else \
616 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100617} while (0)
618
Ingo Molnar526211b2009-03-05 10:28:45 +0100619extern int
Frederic Weisbecker48ead022009-03-12 18:24:49 +0100620__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
621
622extern int
Ingo Molnar526211b2009-03-05 10:28:45 +0100623__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100624
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200625extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
Ingo Molnar526211b2009-03-05 10:28:45 +0100626#else
Ingo Molnar526211b2009-03-05 10:28:45 +0100627static inline int
628trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
629
630static inline void tracing_start(void) { }
631static inline void tracing_stop(void) { }
632static inline void ftrace_off_permanent(void) { }
Steven Rostedte36c5452009-12-14 15:58:33 -0500633static inline void trace_dump_stack(void) { }
Ingo Molnar526211b2009-03-05 10:28:45 +0100634static inline int
635trace_printk(const char *fmt, ...)
636{
637 return 0;
638}
639static inline int
640ftrace_vprintk(const char *fmt, va_list ap)
641{
642 return 0;
643}
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200644static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100645#endif /* CONFIG_TRACING */
Ingo Molnar526211b2009-03-05 10:28:45 +0100646
647/*
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700648 * min()/max()/clamp() macros that also do
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 * strict type-checking.. See the
650 * "unnecessary" pointer comparison.
651 */
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700652#define min(x, y) ({ \
653 typeof(x) _min1 = (x); \
654 typeof(y) _min2 = (y); \
655 (void) (&_min1 == &_min2); \
656 _min1 < _min2 ? _min1 : _min2; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700658#define max(x, y) ({ \
659 typeof(x) _max1 = (x); \
660 typeof(y) _max2 = (y); \
661 (void) (&_max1 == &_max2); \
662 _max1 > _max2 ? _max1 : _max2; })
663
Hagen Paul Pfeiferf27c85c2010-10-26 14:22:21 -0700664#define min3(x, y, z) ({ \
665 typeof(x) _min1 = (x); \
666 typeof(y) _min2 = (y); \
667 typeof(z) _min3 = (z); \
668 (void) (&_min1 == &_min2); \
669 (void) (&_min1 == &_min3); \
670 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
671 (_min2 < _min3 ? _min2 : _min3); })
672
673#define max3(x, y, z) ({ \
674 typeof(x) _max1 = (x); \
675 typeof(y) _max2 = (y); \
676 typeof(z) _max3 = (z); \
677 (void) (&_max1 == &_max2); \
678 (void) (&_max1 == &_max3); \
679 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
680 (_max2 > _max3 ? _max2 : _max3); })
681
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700682/**
Martin K. Petersenc8bf1332010-09-10 20:07:38 +0200683 * min_not_zero - return the minimum that is _not_ zero, unless both are zero
684 * @x: value1
685 * @y: value2
686 */
687#define min_not_zero(x, y) ({ \
688 typeof(x) __x = (x); \
689 typeof(y) __y = (y); \
690 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
691
692/**
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700693 * clamp - return a value clamped to a given range with strict typechecking
694 * @val: current value
695 * @min: minimum allowable value
696 * @max: maximum allowable value
697 *
698 * This macro does strict typechecking of min/max to make sure they are of the
699 * same type as val. See the unnecessary pointer comparisons.
700 */
701#define clamp(val, min, max) ({ \
702 typeof(val) __val = (val); \
703 typeof(min) __min = (min); \
704 typeof(max) __max = (max); \
705 (void) (&__val == &__min); \
706 (void) (&__val == &__max); \
707 __val = __val < __min ? __min: __val; \
708 __val > __max ? __max: __val; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
710/*
711 * ..and if you can't take the strict
712 * types, you can specify one yourself.
713 *
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700714 * Or not use min/max/clamp at all, of course.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700716#define min_t(type, x, y) ({ \
717 type __min1 = (x); \
718 type __min2 = (y); \
719 __min1 < __min2 ? __min1: __min2; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Harvey Harrisonbdf4bba2008-04-30 00:54:55 -0700721#define max_t(type, x, y) ({ \
722 type __max1 = (x); \
723 type __max2 = (y); \
724 __max1 > __max2 ? __max1: __max2; })
725
726/**
727 * clamp_t - return a value clamped to a given range using a given type
728 * @type: the type of variable to use
729 * @val: current value
730 * @min: minimum allowable value
731 * @max: maximum allowable value
732 *
733 * This macro does no typechecking and uses temporary variables of type
734 * 'type' to make all the comparisons.
735 */
736#define clamp_t(type, val, min, max) ({ \
737 type __val = (val); \
738 type __min = (min); \
739 type __max = (max); \
740 __val = __val < __min ? __min: __val; \
741 __val > __max ? __max: __val; })
742
743/**
744 * clamp_val - return a value clamped to a given range using val's type
745 * @val: current value
746 * @min: minimum allowable value
747 * @max: maximum allowable value
748 *
749 * This macro does no typechecking and uses temporary variables of whatever
750 * type the input argument 'val' is. This is useful when val is an unsigned
751 * type and min and max are literals that will otherwise be assigned a signed
752 * integer type.
753 */
754#define clamp_val(val, min, max) ({ \
755 typeof(val) __val = (val); \
756 typeof(val) __min = (min); \
757 typeof(val) __max = (max); \
758 __val = __val < __min ? __min: __val; \
759 __val > __max ? __max: __val; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Wu Fengguang91f68b72009-01-07 18:09:12 -0800761
762/*
763 * swap - swap value of @a and @b
764 */
Peter Zijlstraac7b9002009-02-04 15:11:59 -0800765#define swap(a, b) \
766 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
Wu Fengguang91f68b72009-01-07 18:09:12 -0800767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768/**
769 * container_of - cast a member of a structure out to the containing structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * @ptr: the pointer to the member.
771 * @type: the type of the container struct this is embedded in.
772 * @member: the name of the member within the struct.
773 *
774 */
775#define container_of(ptr, type, member) ({ \
Daniel Walker78db2ad2007-05-12 16:28:35 -0700776 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
777 (type *)( (char *)__mptr - offsetof(type,member) );})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Kyle McMartind4d23ad2007-02-10 01:46:00 -0800779struct sysinfo;
780extern int do_sysinfo(struct sysinfo *info);
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#endif /* __KERNEL__ */
783
784#define SI_LOAD_SHIFT 16
785struct sysinfo {
786 long uptime; /* Seconds since boot */
787 unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
788 unsigned long totalram; /* Total usable main memory size */
789 unsigned long freeram; /* Available memory size */
790 unsigned long sharedram; /* Amount of shared memory */
791 unsigned long bufferram; /* Memory used by buffers */
792 unsigned long totalswap; /* Total swap space size */
793 unsigned long freeswap; /* swap space still available */
794 unsigned short procs; /* Number of current processes */
795 unsigned short pad; /* explicit padding for m68k */
796 unsigned long totalhigh; /* Total high memory size */
797 unsigned long freehigh; /* Available high memory size */
798 unsigned int mem_unit; /* Memory unit size in bytes */
799 char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
800};
801
Nikita Danilovc0398ee2005-10-30 15:03:10 -0800802/* Force a compilation error if condition is true */
Jan Beulich8c87df42009-09-22 16:43:52 -0700803#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
804
805/* Force a compilation error if condition is constant and true */
806#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Roland Dreiercc8ef6e2010-01-15 17:01:22 -0800808/* Force a compilation error if a constant expression is not a power of 2 */
809#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
810 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
811
Jan Beulich4552d5d2006-06-26 13:57:28 +0200812/* Force a compilation error if condition is true, but also produce a
813 result (of value 0 and type size_t), so the expression can be used
814 e.g. in a structure initializer (or where-ever else comma expressions
815 aren't permitted). */
Jan Beulich8c87df42009-09-22 16:43:52 -0700816#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
817#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
Jan Beulich4552d5d2006-06-26 13:57:28 +0200818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/* Trap pasters of __FUNCTION__ at compile-time */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820#define __FUNCTION__ (__func__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Christoph Lameter08e0f6a2006-09-27 01:50:06 -0700822/* This helps us to avoid #ifdef CONFIG_NUMA */
823#ifdef CONFIG_NUMA
824#define NUMA_BUILD 1
825#else
826#define NUMA_BUILD 0
827#endif
828
Steven Rostedt29e71ab2008-08-14 15:45:10 -0400829/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
830#ifdef CONFIG_FTRACE_MCOUNT_RECORD
831# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
832#endif
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834#endif