1#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4
5
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#define INT_MAX ((int)(~0U>>1))
20#define INT_MIN (-INT_MAX - 1)
21#define UINT_MAX (~0U)
22#define LONG_MAX ((long)(~0UL>>1))
23#define LONG_MIN (-LONG_MAX - 1)
24#define ULONG_MAX (~0UL)
25
26#define STACK_MAGIC 0xdeadbeef
27
28#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
29#define ALIGN(x,a) (((x)+(a)-1)&~((a)-1))
30
31#define KERN_EMERG "<0>"
32#define KERN_ALERT "<1>"
33#define KERN_CRIT "<2>"
34#define KERN_ERR "<3>"
35#define KERN_WARNING "<4>"
36#define KERN_NOTICE "<5>"
37#define KERN_INFO "<6>"
38#define KERN_DEBUG "<7>"
39
40extern int console_printk[];
41
42#define console_loglevel (console_printk[0])
43#define default_message_loglevel (console_printk[1])
44#define minimum_console_loglevel (console_printk[2])
45#define default_console_loglevel (console_printk[3])
46
47struct completion;
48
49#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
50void __might_sleep(char *file, int line);
51#define might_sleep() __might_sleep(__FILE__, __LINE__)
52#define might_sleep_if(cond) do { if (unlikely(cond)) might_sleep(); } while (0)
53#else
54#define might_sleep() do {} while(0)
55#define might_sleep_if(cond) do {} while (0)
56#endif
57
58#define abs(x) ({ \
59 int __x = (x); \
60 (__x < 0) ? -__x : __x; \
61 })
62
63#define labs(x) ({ \
64 long __x = (x); \
65 (__x < 0) ? -__x : __x; \
66 })
67
68extern struct notifier_block *panic_notifier_list;
69extern long (*panic_blink)(long time);
70NORET_TYPE void panic(const char * fmt, ...)
71 __attribute__ ((NORET_AND format (printf, 1, 2)));
72fastcall NORET_TYPE void do_exit(long error_code)
73 ATTRIB_NORET;
74NORET_TYPE void complete_and_exit(struct completion *, long)
75 ATTRIB_NORET;
76extern unsigned long simple_strtoul(const char *,char **,unsigned int);
77extern long simple_strtol(const char *,char **,unsigned int);
78extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
79extern long long simple_strtoll(const char *,char **,unsigned int);
80extern int sprintf(char * buf, const char * fmt, ...)
81 __attribute__ ((format (printf, 2, 3)));
82extern int vsprintf(char *buf, const char *, va_list);
83extern int snprintf(char * buf, size_t size, const char * fmt, ...)
84 __attribute__ ((format (printf, 3, 4)));
85extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
86extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
87 __attribute__ ((format (printf, 3, 4)));
88extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
89
90extern int sscanf(const char *, const char *, ...)
91 __attribute__ ((format (scanf,2,3)));
92extern int vsscanf(const char *, const char *, va_list);
93
94extern int get_option(char **str, int *pint);
95extern char *get_options(const char *str, int nints, int *ints);
96extern unsigned long long memparse(char *ptr, char **retptr);
97
98extern int __kernel_text_address(unsigned long addr);
99extern int kernel_text_address(unsigned long addr);
100extern int session_of_pgrp(int pgrp);
101
102asmlinkage int vprintk(const char *fmt, va_list args);
103asmlinkage int printk(const char * fmt, ...)
104 __attribute__ ((format (printf, 1, 2)));
105
106unsigned long int_sqrt(unsigned long);
107
108static inline int __attribute_pure__ long_log2(unsigned long x)
109{
110 int r = 0;
111 for (x >>= 1; x > 0; x >>= 1)
112 r++;
113 return r;
114}
115
116static inline unsigned long __attribute_const__ roundup_pow_of_two(unsigned long x)
117{
118 return (1UL << fls(x - 1));
119}
120
121extern int printk_ratelimit(void);
122extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
123
124static inline void console_silent(void)
125{
126 console_loglevel = 0;
127}
128
129static inline void console_verbose(void)
130{
131 if (console_loglevel)
132 console_loglevel = 15;
133}
134
135extern void bust_spinlocks(int yes);
136extern int oops_in_progress;
137extern int panic_on_oops;
138extern int tainted;
139extern const char *print_tainted(void);
140extern void add_taint(unsigned);
141
142
143extern enum system_states {
144 SYSTEM_BOOTING,
145 SYSTEM_RUNNING,
146 SYSTEM_HALT,
147 SYSTEM_POWER_OFF,
148 SYSTEM_RESTART,
149} system_state;
150
151#define TAINT_PROPRIETARY_MODULE (1<<0)
152#define TAINT_FORCED_MODULE (1<<1)
153#define TAINT_UNSAFE_SMP (1<<2)
154#define TAINT_FORCED_RMMOD (1<<3)
155#define TAINT_MACHINE_CHECK (1<<4)
156#define TAINT_BAD_PAGE (1<<5)
157
158extern void dump_stack(void);
159
160#ifdef DEBUG
161#define pr_debug(fmt,arg...) \
162 printk(KERN_DEBUG fmt,##arg)
163#else
164#define pr_debug(fmt,arg...) \
165 do { } while (0)
166#endif
167
168#define pr_info(fmt,arg...) \
169 printk(KERN_INFO fmt,##arg)
170
171
172
173
174
175#define NIPQUAD(addr) \
176 ((unsigned char *)&addr)[0], \
177 ((unsigned char *)&addr)[1], \
178 ((unsigned char *)&addr)[2], \
179 ((unsigned char *)&addr)[3]
180
181#define NIP6(addr) \
182 ntohs((addr).s6_addr16[0]), \
183 ntohs((addr).s6_addr16[1]), \
184 ntohs((addr).s6_addr16[2]), \
185 ntohs((addr).s6_addr16[3]), \
186 ntohs((addr).s6_addr16[4]), \
187 ntohs((addr).s6_addr16[5]), \
188 ntohs((addr).s6_addr16[6]), \
189 ntohs((addr).s6_addr16[7])
190
191#if defined(__LITTLE_ENDIAN)
192#define HIPQUAD(addr) \
193 ((unsigned char *)&addr)[3], \
194 ((unsigned char *)&addr)[2], \
195 ((unsigned char *)&addr)[1], \
196 ((unsigned char *)&addr)[0]
197#elif defined(__BIG_ENDIAN)
198#define HIPQUAD NIPQUAD
199#else
200#error "Please fix asm/byteorder.h"
201#endif
202
203
204
205
206
207
208#define min(x,y) ({ \
209 typeof(x) _x = (x); \
210 typeof(y) _y = (y); \
211 (void) (&_x == &_y); \
212 _x < _y ? _x : _y; })
213
214#define max(x,y) ({ \
215 typeof(x) _x = (x); \
216 typeof(y) _y = (y); \
217 (void) (&_x == &_y); \
218 _x > _y ? _x : _y; })
219
220
221
222
223
224
225
226#define min_t(type,x,y) \
227 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
228#define max_t(type,x,y) \
229 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
230
231
232
233
234
235
236
237
238
239
240#define container_of(ptr, type, member) ({ \
241 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
242 (type *)( (char *)__mptr - offsetof(type,member) );})
243
244
245
246
247
248#define typecheck(type,x) \
249({ type __dummy; \
250 typeof(x) __dummy2; \
251 (void)(&__dummy == &__dummy2); \
252 1; \
253})
254
255#endif
256
257#define SI_LOAD_SHIFT 16
258struct sysinfo {
259 long uptime;
260 unsigned long loads[3];
261 unsigned long totalram;
262 unsigned long freeram;
263 unsigned long sharedram;
264 unsigned long bufferram;
265 unsigned long totalswap;
266 unsigned long freeswap;
267 unsigned short procs;
268 unsigned short pad;
269 unsigned long totalhigh;
270 unsigned long freehigh;
271 unsigned int mem_unit;
272 char _f[20-2*sizeof(long)-sizeof(int)];
273};
274
275extern void BUILD_BUG(void);
276#define BUILD_BUG_ON(condition) do { if (condition) BUILD_BUG(); } while(0)
277
278
279#if __GNUC__ > 2 || __GNUC_MINOR__ >= 95
280#define __FUNCTION__ (__func__)
281#endif
282
283#endif
284