ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 <asm/byteorder.h>
16 #include <asm/bug.h>
17
18 #define INT_MAX         ((int)(~0U>>1))
19 #define INT_MIN         (-INT_MAX - 1)
20 #define UINT_MAX        (~0U)
21 #define LONG_MAX        ((long)(~0UL>>1))
22 #define LONG_MIN        (-LONG_MAX - 1)
23 #define ULONG_MAX       (~0UL)
24
25 #define STACK_MAGIC     0xdeadbeef
26
27 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
28 #define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
29
30 #define KERN_EMERG      "<0>"   /* system is unusable                   */
31 #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
32 #define KERN_CRIT       "<2>"   /* critical conditions                  */
33 #define KERN_ERR        "<3>"   /* error conditions                     */
34 #define KERN_WARNING    "<4>"   /* warning conditions                   */
35 #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
36 #define KERN_INFO       "<6>"   /* informational                        */
37 #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
38
39 extern int console_printk[];
40
41 #define console_loglevel (console_printk[0])
42 #define default_message_loglevel (console_printk[1])
43 #define minimum_console_loglevel (console_printk[2])
44 #define default_console_loglevel (console_printk[3])
45
46 struct completion;
47
48 #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
49 void __might_sleep(char *file, int line);
50 #define might_sleep() __might_sleep(__FILE__, __LINE__)
51 #define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0)
52 #else
53 #define might_sleep() do {} while(0)
54 #define might_sleep_if(cond) do {} while (0)
55 #endif
56
57 extern struct notifier_block *panic_notifier_list;
58 NORET_TYPE void panic(const char * fmt, ...)
59         __attribute__ ((NORET_AND format (printf, 1, 2)));
60 asmlinkage NORET_TYPE void do_exit(long error_code)
61         ATTRIB_NORET;
62 NORET_TYPE void complete_and_exit(struct completion *, long)
63         ATTRIB_NORET;
64 extern int abs(int);
65 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
66 extern long simple_strtol(const char *,char **,unsigned int);
67 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
68 extern long long simple_strtoll(const char *,char **,unsigned int);
69 extern int sprintf(char * buf, const char * fmt, ...)
70         __attribute__ ((format (printf, 2, 3)));
71 extern int vsprintf(char *buf, const char *, va_list);
72 extern int snprintf(char * buf, size_t size, const char * fmt, ...)
73         __attribute__ ((format (printf, 3, 4)));
74 extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
75 extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
76         __attribute__ ((format (printf, 3, 4)));
77 extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
78
79 extern int sscanf(const char *, const char *, ...)
80         __attribute__ ((format (scanf,2,3)));
81 extern int vsscanf(const char *, const char *, va_list);
82
83 extern int get_option(char **str, int *pint);
84 extern char *get_options(const char *str, int nints, int *ints);
85 extern unsigned long long memparse(char *ptr, char **retptr);
86
87 extern int kernel_text_address(unsigned long addr);
88 extern int session_of_pgrp(int pgrp);
89
90 asmlinkage int printk(const char * fmt, ...)
91         __attribute__ ((format (printf, 1, 2)));
92
93 unsigned long int_sqrt(unsigned long);
94
95 extern int printk_ratelimit(void);
96 extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
97
98 static inline void console_silent(void)
99 {
100         console_loglevel = 0;
101 }
102
103 static inline void console_verbose(void)
104 {
105         if (console_loglevel)
106                 console_loglevel = 15;
107 }
108
109 extern void bust_spinlocks(int yes);
110 extern int oops_in_progress;            /* If set, an oops, panic(), BUG() or die() is in progress */
111 extern int panic_on_oops;
112 extern int system_state;                /* See values below */
113 extern int tainted;
114 extern const char *print_tainted(void);
115
116 /* Values used for system_state */
117 #define SYSTEM_BOOTING 0
118 #define SYSTEM_RUNNING 1
119 #define SYSTEM_SHUTDOWN 2
120
121 #define TAINT_PROPRIETARY_MODULE        (1<<0)
122 #define TAINT_FORCED_MODULE             (1<<1)
123 #define TAINT_UNSAFE_SMP                (1<<2)
124 #define TAINT_FORCED_RMMOD              (1<<3)
125
126 extern void dump_stack(void);
127
128 #ifdef DEBUG
129 #define pr_debug(fmt,arg...) \
130         printk(KERN_DEBUG fmt,##arg)
131 #else
132 #define pr_debug(fmt,arg...) \
133         do { } while (0)
134 #endif
135
136 #define pr_info(fmt,arg...) \
137         printk(KERN_INFO fmt,##arg)
138
139 /*
140  *      Display an IP address in readable format.
141  */
142
143 #define NIPQUAD(addr) \
144         ((unsigned char *)&addr)[0], \
145         ((unsigned char *)&addr)[1], \
146         ((unsigned char *)&addr)[2], \
147         ((unsigned char *)&addr)[3]
148
149 #define NIP6(addr) \
150         ntohs((addr).s6_addr16[0]), \
151         ntohs((addr).s6_addr16[1]), \
152         ntohs((addr).s6_addr16[2]), \
153         ntohs((addr).s6_addr16[3]), \
154         ntohs((addr).s6_addr16[4]), \
155         ntohs((addr).s6_addr16[5]), \
156         ntohs((addr).s6_addr16[6]), \
157         ntohs((addr).s6_addr16[7])
158
159 #if defined(__LITTLE_ENDIAN)
160 #define HIPQUAD(addr) \
161         ((unsigned char *)&addr)[3], \
162         ((unsigned char *)&addr)[2], \
163         ((unsigned char *)&addr)[1], \
164         ((unsigned char *)&addr)[0]
165 #elif defined(__BIG_ENDIAN)
166 #define HIPQUAD NIPQUAD
167 #else
168 #error "Please fix asm/byteorder.h"
169 #endif /* __LITTLE_ENDIAN */
170
171 /*
172  * min()/max() macros that also do
173  * strict type-checking.. See the
174  * "unnecessary" pointer comparison.
175  */
176 #define min(x,y) ({ \
177         typeof(x) _x = (x);     \
178         typeof(y) _y = (y);     \
179         (void) (&_x == &_y);            \
180         _x < _y ? _x : _y; })
181
182 #define max(x,y) ({ \
183         typeof(x) _x = (x);     \
184         typeof(y) _y = (y);     \
185         (void) (&_x == &_y);            \
186         _x > _y ? _x : _y; })
187
188 /*
189  * ..and if you can't take the strict
190  * types, you can specify one yourself.
191  *
192  * Or not use min/max at all, of course.
193  */
194 #define min_t(type,x,y) \
195         ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
196 #define max_t(type,x,y) \
197         ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
198
199
200 /**
201  * container_of - cast a member of a structure out to the containing structure
202  *
203  * @ptr:        the pointer to the member.
204  * @type:       the type of the container struct this is embedded in.
205  * @member:     the name of the member within the struct.
206  *
207  */
208 #define container_of(ptr, type, member) ({                      \
209         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
210         (type *)( (char *)__mptr - offsetof(type,member) );})
211
212 /*
213  * Check at compile time that something is of a particular type.
214  * Always evaluates to 1 so you may use it easily in comparisons.
215  */
216 #define typecheck(type,x) \
217 ({      type __dummy; \
218         typeof(x) __dummy2; \
219         (void)(&__dummy == &__dummy2); \
220         1; \
221 })
222
223 #endif /* __KERNEL__ */
224
225 #define SI_LOAD_SHIFT   16
226 struct sysinfo {
227         long uptime;                    /* Seconds since boot */
228         unsigned long loads[3];         /* 1, 5, and 15 minute load averages */
229         unsigned long totalram;         /* Total usable main memory size */
230         unsigned long freeram;          /* Available memory size */
231         unsigned long sharedram;        /* Amount of shared memory */
232         unsigned long bufferram;        /* Memory used by buffers */
233         unsigned long totalswap;        /* Total swap space size */
234         unsigned long freeswap;         /* swap space still available */
235         unsigned short procs;           /* Number of current processes */
236         unsigned short pad;             /* explicit padding for m68k */
237         unsigned long totalhigh;        /* Total high memory size */
238         unsigned long freehigh;         /* Available high memory size */
239         unsigned int mem_unit;          /* Memory unit size in bytes */
240         char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
241 };
242
243 extern void BUILD_BUG(void);
244 #define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
245
246 /* Trap pasters of __FUNCTION__ at compile-time */
247 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
248 #define __FUNCTION__ (__func__)
249 #endif
250
251 #endif