upgrade to fedora-2.6.12-1.1398.FC4 + vserver 2.0.rc7
[linux-2.6.git] / include / linux / kernel.h
1 #ifndef _LINUX_KERNEL_H
2 #define _LINUX_KERNEL_H
3
4 /*
5  * 'kernel.h' contains some often-used function prototypes etc
6  */
7
8 #ifdef __KERNEL__
9
10 #include <stdarg.h>
11 #include <linux/linkage.h>
12 #include <linux/stddef.h>
13 #include <linux/types.h>
14 #include <linux/compiler.h>
15 #include <linux/bitops.h>
16 #include <asm/byteorder.h>
17 #include <asm/bug.h>
18
19 extern const char linux_banner[];
20 extern const char vx_linux_banner[];
21
22 #define INT_MAX         ((int)(~0U>>1))
23 #define INT_MIN         (-INT_MAX - 1)
24 #define UINT_MAX        (~0U)
25 #define LONG_MAX        ((long)(~0UL>>1))
26 #define LONG_MIN        (-LONG_MAX - 1)
27 #define ULONG_MAX       (~0UL)
28
29 #define STACK_MAGIC     0xdeadbeef
30
31 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
32 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
33
34 #define KERN_EMERG      "<0>"   /* system is unusable                   */
35 #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
36 #define KERN_CRIT       "<2>"   /* critical conditions                  */
37 #define KERN_ERR        "<3>"   /* error conditions                     */
38 #define KERN_WARNING    "<4>"   /* warning conditions                   */
39 #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
40 #define KERN_INFO       "<6>"   /* informational                        */
41 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
42
43 extern int console_printk[];
44
45 #define console_loglevel (console_printk[0])
46 #define default_message_loglevel (console_printk[1])
47 #define minimum_console_loglevel (console_printk[2])
48 #define default_console_loglevel (console_printk[3])
49
50 struct completion;
51
52 /**
53  * might_sleep - annotation for functions that can sleep
54  *
55  * this macro will print a stack trace if it is executed in an atomic
56  * context (spinlock, irq-handler, ...).
57  *
58  * This is a useful debugging help to be able to catch problems early and not
59  * be biten later when the calling function happens to sleep when it is not
60  * supposed to.
61  */
62 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
63 #define might_sleep() __might_sleep(__FILE__, __LINE__)
64 #define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0)
65 void __might_sleep(char *file, int line);
66 #else
67 #define might_sleep() do {} while(0)
68 #define might_sleep_if(cond) do {} while (0)
69 #endif
70
71 #define abs(x) ({                               \
72                 int __x = (x);                  \
73                 (__x < 0) ? -__x : __x;         \
74         })
75
76 #define labs(x) ({                              \
77                 long __x = (x);                 \
78                 (__x < 0) ? -__x : __x;         \
79         })
80
81 extern struct notifier_block *panic_notifier_list;
82 extern long (*panic_blink)(long time);
83 NORET_TYPE void panic(const char * fmt, ...)
84         __attribute__ ((NORET_AND format (printf, 1, 2)));
85 fastcall NORET_TYPE void do_exit(long error_code)
86         ATTRIB_NORET;
87 NORET_TYPE void complete_and_exit(struct completion *, long)
88         ATTRIB_NORET;
89 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
90 extern long simple_strtol(const char *,char **,unsigned int);
91 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
92 extern long long simple_strtoll(const char *,char **,unsigned int);
93 extern int sprintf(char * buf, const char * fmt, ...)
94         __attribute__ ((format (printf, 2, 3)));
95 extern int vsprintf(char *buf, const char *, va_list)
96         __attribute__ ((format (printf, 2, 0)));
97 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
98         __attribute__ ((format (printf, 3, 4)));
99 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
100         __attribute__ ((format (printf, 3, 0)));
101 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
102         __attribute__ ((format (printf, 3, 4)));
103 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
104         __attribute__ ((format (printf, 3, 0)));
105
106 extern int sscanf(const char *, const char *, ...)
107         __attribute__ ((format (scanf, 2, 3)));
108 extern int vsscanf(const char *, const char *, va_list)
109         __attribute__ ((format (scanf, 2, 0)));
110
111 extern int get_option(char **str, int *pint);
112 extern char *get_options(const char *str, int nints, int *ints);
113 extern unsigned long long memparse(char *ptr, char **retptr);
114
115 extern int __kernel_text_address(unsigned long addr);
116 extern int kernel_text_address(unsigned long addr);
117 extern int session_of_pgrp(int pgrp);
118
119 #ifdef CONFIG_PRINTK
120 asmlinkage int vprintk(const char *fmt, va_list args)
121         __attribute__ ((format (printf, 1, 0)));
122 asmlinkage int printk(const char * fmt, ...)
123         __attribute__ ((format (printf, 1, 2)));
124 #else
125 static inline int vprintk(const char *s, va_list args)
126         __attribute__ ((format (printf, 1, 0)));
127 static inline int vprintk(const char *s, va_list args) { return 0; }
128 static inline int printk(const char *s, ...)
129         __attribute__ ((format (printf, 1, 2)));
130 static inline int printk(const char *s, ...) { return 0; }
131 #endif
132
133 unsigned long int_sqrt(unsigned long);
134
135 static inline int __attribute_pure__ long_log2(unsigned long x)
136 {
137         int r = 0;
138         for (x >>= 1; x > 0; x >>= 1)
139                 r++;
140         return r;
141 }
142
143 static inline unsigned long __attribute_const__ roundup_pow_of_two(unsigned long x)
144 {
145         return (1UL << fls(x - 1));
146 }
147
148 extern int printk_ratelimit(void);
149 extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
150
151 static inline void console_silent(void)
152 {
153         console_loglevel = 0;
154 }
155
156 static inline void console_verbose(void)
157 {
158         if (console_loglevel)
159                 console_loglevel = 15;
160 }
161
162 extern void bust_spinlocks(int yes);
163 extern int oops_in_progress;            /* If set, an oops, panic(), BUG() or die() is in progress */
164 extern int panic_timeout;
165 extern int panic_on_oops;
166 extern int tainted;
167 extern const char *print_tainted(void);
168 extern void add_taint(unsigned);
169 extern int check_tainted(void);
170
171 #define crashdump_mode()       unlikely(netdump_mode || diskdump_mode)
172
173 struct pt_regs;
174 extern void try_crashdump(struct pt_regs *);
175 extern void (*netdump_func) (struct pt_regs *regs);
176 extern int netdump_mode;
177 extern void (*diskdump_func) (struct pt_regs *regs);
178 extern int diskdump_mode;
179
180 #define crashdump_func()        unlikely(netdump_func || diskdump_func)
181
182 /* Values used for system_state */
183 extern enum system_states {
184         SYSTEM_BOOTING,
185         SYSTEM_RUNNING,
186         SYSTEM_HALT,
187         SYSTEM_POWER_OFF,
188         SYSTEM_RESTART,
189         SYSTEM_DUMPING,
190 } system_state;
191
192 #define TAINT_PROPRIETARY_MODULE        (1<<0)
193 #define TAINT_FORCED_MODULE             (1<<1)
194 #define TAINT_UNSAFE_SMP                (1<<2)
195 #define TAINT_FORCED_RMMOD              (1<<3)
196 #define TAINT_MACHINE_CHECK             (1<<4)
197 #define TAINT_BAD_PAGE                  (1<<5)
198
199 extern void dump_stack(void);
200
201 #ifdef DEBUG
202 #define pr_debug(fmt,arg...) \
203         printk(KERN_DEBUG fmt,##arg)
204 #else
205 #define pr_debug(fmt,arg...) \
206         do { } while (0)
207 #endif
208
209 #define pr_info(fmt,arg...) \
210         printk(KERN_INFO fmt,##arg)
211
212 #define pr_err(fmt,arg...) \
213         printk(KERN_ERR fmt,##arg)
214
215 #define pr_warn(fmt,arg...) \
216         printk(KERN_WARNING fmt,##arg)
217
218 /*
219  *      Display an IP address in readable format.
220  */
221
222 #define NIPQUAD(addr) \
223         ((unsigned char *)&addr)[0], \
224         ((unsigned char *)&addr)[1], \
225         ((unsigned char *)&addr)[2], \
226         ((unsigned char *)&addr)[3]
227
228 #define NIP6(addr) \
229         ntohs((addr).s6_addr16[0]), \
230         ntohs((addr).s6_addr16[1]), \
231         ntohs((addr).s6_addr16[2]), \
232         ntohs((addr).s6_addr16[3]), \
233         ntohs((addr).s6_addr16[4]), \
234         ntohs((addr).s6_addr16[5]), \
235         ntohs((addr).s6_addr16[6]), \
236         ntohs((addr).s6_addr16[7])
237
238 #if defined(__LITTLE_ENDIAN)
239 #define HIPQUAD(addr) \
240         ((unsigned char *)&addr)[3], \
241         ((unsigned char *)&addr)[2], \
242         ((unsigned char *)&addr)[1], \
243         ((unsigned char *)&addr)[0]
244 #elif defined(__BIG_ENDIAN)
245 #define HIPQUAD NIPQUAD
246 #else
247 #error "Please fix asm/byteorder.h"
248 #endif /* __LITTLE_ENDIAN */
249
250 /*
251  * min()/max() macros that also do
252  * strict type-checking.. See the
253  * "unnecessary" pointer comparison.
254  */
255 #define min(x,y) ({ \
256         typeof(x) _x = (x);     \
257         typeof(y) _y = (y);     \
258         (void) (&_x == &_y);            \
259         _x < _y ? _x : _y; })
260
261 #define max(x,y) ({ \
262         typeof(x) _x = (x);     \
263         typeof(y) _y = (y);     \
264         (void) (&_x == &_y);            \
265         _x > _y ? _x : _y; })
266
267 /*
268  * ..and if you can't take the strict
269  * types, you can specify one yourself.
270  *
271  * Or not use min/max at all, of course.
272  */
273 #define min_t(type,x,y) \
274         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
275 #define max_t(type,x,y) \
276         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
277
278
279 /**
280  * container_of - cast a member of a structure out to the containing structure
281  *
282  * @ptr:        the pointer to the member.
283  * @type:       the type of the container struct this is embedded in.
284  * @member:     the name of the member within the struct.
285  *
286  */
287 #define container_of(ptr, type, member) ({                      \
288         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
289         (type *)( (char *)__mptr - offsetof(type,member) );})
290
291 /*
292  * Check at compile time that something is of a particular type.
293  * Always evaluates to 1 so you may use it easily in comparisons.
294  */
295 #define typecheck(type,x) \
296 ({      type __dummy; \
297         typeof(x) __dummy2; \
298         (void)(&__dummy == &__dummy2); \
299         1; \
300 })
301
302 #endif /* __KERNEL__ */
303
304 #define SI_LOAD_SHIFT   16
305 struct sysinfo {
306         long uptime;                    /* Seconds since boot */
307         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
308         unsigned long totalram;         /* Total usable main memory size */
309         unsigned long freeram;          /* Available memory size */
310         unsigned long sharedram;        /* Amount of shared memory */
311         unsigned long bufferram;        /* Memory used by buffers */
312         unsigned long totalswap;        /* Total swap space size */
313         unsigned long freeswap;         /* swap space still available */
314         unsigned short procs;           /* Number of current processes */
315         unsigned short pad;             /* explicit padding for m68k */
316         unsigned long totalhigh;        /* Total high memory size */
317         unsigned long freehigh;         /* Available high memory size */
318         unsigned int mem_unit;          /* Memory unit size in bytes */
319         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
320 };
321
322 extern void BUILD_BUG(void);
323 #define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
324
325 #ifdef CONFIG_SYSCTL
326 extern int randomize_va_space;
327 #else
328 #define randomize_va_space 1
329 #endif
330
331 /* Trap pasters of __FUNCTION__ at compile-time */
332 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
333 #define __FUNCTION__ (__func__)
334 #endif
335
336 #endif