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
170 /* Values used for system_state */
171 extern enum system_states {
172         SYSTEM_BOOTING,
173         SYSTEM_RUNNING,
174         SYSTEM_HALT,
175         SYSTEM_POWER_OFF,
176         SYSTEM_RESTART,
177 } system_state;
178
179 #define TAINT_PROPRIETARY_MODULE        (1<<0)
180 #define TAINT_FORCED_MODULE             (1<<1)
181 #define TAINT_UNSAFE_SMP                (1<<2)
182 #define TAINT_FORCED_RMMOD              (1<<3)
183 #define TAINT_MACHINE_CHECK             (1<<4)
184 #define TAINT_BAD_PAGE                  (1<<5)
185
186 extern void dump_stack(void);
187
188 #ifdef DEBUG
189 #define pr_debug(fmt,arg...) \
190         printk(KERN_DEBUG fmt,##arg)
191 #else
192 #define pr_debug(fmt,arg...) \
193         do { } while (0)
194 #endif
195
196 #define pr_info(fmt,arg...) \
197         printk(KERN_INFO fmt,##arg)
198
199 /*
200  *      Display an IP address in readable format.
201  */
202
203 #define NIPQUAD(addr) \
204         ((unsigned char *)&addr)[0], \
205         ((unsigned char *)&addr)[1], \
206         ((unsigned char *)&addr)[2], \
207         ((unsigned char *)&addr)[3]
208
209 #define NIP6(addr) \
210         ntohs((addr).s6_addr16[0]), \
211         ntohs((addr).s6_addr16[1]), \
212         ntohs((addr).s6_addr16[2]), \
213         ntohs((addr).s6_addr16[3]), \
214         ntohs((addr).s6_addr16[4]), \
215         ntohs((addr).s6_addr16[5]), \
216         ntohs((addr).s6_addr16[6]), \
217         ntohs((addr).s6_addr16[7])
218
219 #if defined(__LITTLE_ENDIAN)
220 #define HIPQUAD(addr) \
221         ((unsigned char *)&addr)[3], \
222         ((unsigned char *)&addr)[2], \
223         ((unsigned char *)&addr)[1], \
224         ((unsigned char *)&addr)[0]
225 #elif defined(__BIG_ENDIAN)
226 #define HIPQUAD NIPQUAD
227 #else
228 #error "Please fix asm/byteorder.h"
229 #endif /* __LITTLE_ENDIAN */
230
231 /*
232  * min()/max() macros that also do
233  * strict type-checking.. See the
234  * "unnecessary" pointer comparison.
235  */
236 #define min(x,y) ({ \
237         typeof(x) _x = (x);     \
238         typeof(y) _y = (y);     \
239         (void) (&_x == &_y);            \
240         _x < _y ? _x : _y; })
241
242 #define max(x,y) ({ \
243         typeof(x) _x = (x);     \
244         typeof(y) _y = (y);     \
245         (void) (&_x == &_y);            \
246         _x > _y ? _x : _y; })
247
248 /*
249  * ..and if you can't take the strict
250  * types, you can specify one yourself.
251  *
252  * Or not use min/max at all, of course.
253  */
254 #define min_t(type,x,y) \
255         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
256 #define max_t(type,x,y) \
257         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
258
259
260 /**
261  * container_of - cast a member of a structure out to the containing structure
262  *
263  * @ptr:        the pointer to the member.
264  * @type:       the type of the container struct this is embedded in.
265  * @member:     the name of the member within the struct.
266  *
267  */
268 #define container_of(ptr, type, member) ({                      \
269         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
270         (type *)( (char *)__mptr - offsetof(type,member) );})
271
272 /*
273  * Check at compile time that something is of a particular type.
274  * Always evaluates to 1 so you may use it easily in comparisons.
275  */
276 #define typecheck(type,x) \
277 ({      type __dummy; \
278         typeof(x) __dummy2; \
279         (void)(&__dummy == &__dummy2); \
280         1; \
281 })
282
283 #endif /* __KERNEL__ */
284
285 #define SI_LOAD_SHIFT   16
286 struct sysinfo {
287         long uptime;                    /* Seconds since boot */
288         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
289         unsigned long totalram;         /* Total usable main memory size */
290         unsigned long freeram;          /* Available memory size */
291         unsigned long sharedram;        /* Amount of shared memory */
292         unsigned long bufferram;        /* Memory used by buffers */
293         unsigned long totalswap;        /* Total swap space size */
294         unsigned long freeswap;         /* swap space still available */
295         unsigned short procs;           /* Number of current processes */
296         unsigned short pad;             /* explicit padding for m68k */
297         unsigned long totalhigh;        /* Total high memory size */
298         unsigned long freehigh;         /* Available high memory size */
299         unsigned int mem_unit;          /* Memory unit size in bytes */
300         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
301 };
302
303 extern void BUILD_BUG(void);
304 #define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
305
306 #ifdef CONFIG_SYSCTL
307 extern int randomize_va_space;
308 #else
309 #define randomize_va_space 1
310 #endif
311
312 /* Trap pasters of __FUNCTION__ at compile-time */
313 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
314 #define __FUNCTION__ (__func__)
315 #endif
316
317 #endif