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 <linux/log2.h>
17#include <asm/byteorder.h>
18#include <asm/bug.h>
19
20extern const char linux_banner[];
21extern const char linux_proc_banner[];
22
23#define USHORT_MAX ((u16)(~0U))
24#define INT_MAX ((int)(~0U>>1))
25#define INT_MIN (-INT_MAX - 1)
26#define UINT_MAX (~0U)
27#define LONG_MAX ((long)(~0UL>>1))
28#define LONG_MIN (-LONG_MAX - 1)
29#define ULONG_MAX (~0UL)
30#define LLONG_MAX ((long long)(~0ULL>>1))
31#define LLONG_MIN (-LLONG_MAX - 1)
32#define ULLONG_MAX (~0ULL)
33
34#define STACK_MAGIC 0xdeadbeef
35
36#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
37#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
38#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
39#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
40
41#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
42
43#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
44#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
45#define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
46
47#ifdef CONFIG_LBD
48# include <asm/div64.h>
49# define sector_div(a, b) do_div(a, b)
50#else
51# define sector_div(n, b)( \
52{ \
53 int _res; \
54 _res = (n) % (b); \
55 (n) /= (b); \
56 _res; \
57} \
58)
59#endif
60
61
62
63
64
65
66
67
68
69#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
70
71#define KERN_EMERG "<0>"
72#define KERN_ALERT "<1>"
73#define KERN_CRIT "<2>"
74#define KERN_ERR "<3>"
75#define KERN_WARNING "<4>"
76#define KERN_NOTICE "<5>"
77#define KERN_INFO "<6>"
78#define KERN_DEBUG "<7>"
79
80
81
82
83
84
85#define KERN_CONT ""
86
87extern int console_printk[];
88
89#define console_loglevel (console_printk[0])
90#define default_message_loglevel (console_printk[1])
91#define minimum_console_loglevel (console_printk[2])
92#define default_console_loglevel (console_printk[3])
93
94struct completion;
95struct pt_regs;
96struct user;
97
98
99
100
101
102
103
104
105
106
107
108#ifdef CONFIG_PREEMPT_VOLUNTARY
109extern int _cond_resched(void);
110# define might_resched() _cond_resched()
111#else
112# define might_resched() do { } while (0)
113#endif
114
115#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
116 void __might_sleep(char *file, int line);
117# define might_sleep() \
118 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
119#else
120# define might_sleep() do { might_resched(); } while (0)
121#endif
122
123#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
124
125#define abs(x) ({ \
126 int __x = (x); \
127 (__x < 0) ? -__x : __x; \
128 })
129
130extern struct atomic_notifier_head panic_notifier_list;
131extern long (*panic_blink)(long time);
132NORET_TYPE void panic(const char * fmt, ...)
133 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
134extern void oops_enter(void);
135extern void oops_exit(void);
136extern int oops_may_print(void);
137NORET_TYPE void do_exit(long error_code)
138 ATTRIB_NORET;
139NORET_TYPE void complete_and_exit(struct completion *, long)
140 ATTRIB_NORET;
141extern unsigned long simple_strtoul(const char *,char **,unsigned int);
142extern long simple_strtol(const char *,char **,unsigned int);
143extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
144extern long long simple_strtoll(const char *,char **,unsigned int);
145extern int strict_strtoul(const char *, unsigned int, unsigned long *);
146extern int strict_strtol(const char *, unsigned int, long *);
147extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
148extern int strict_strtoll(const char *, unsigned int, long long *);
149extern int sprintf(char * buf, const char * fmt, ...)
150 __attribute__ ((format (printf, 2, 3)));
151extern int vsprintf(char *buf, const char *, va_list)
152 __attribute__ ((format (printf, 2, 0)));
153extern int snprintf(char * buf, size_t size, const char * fmt, ...)
154 __attribute__ ((format (printf, 3, 4)));
155extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
156 __attribute__ ((format (printf, 3, 0)));
157extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
158 __attribute__ ((format (printf, 3, 4)));
159extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
160 __attribute__ ((format (printf, 3, 0)));
161extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
162 __attribute__ ((format (printf, 2, 3)));
163extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
164
165extern int sscanf(const char *, const char *, ...)
166 __attribute__ ((format (scanf, 2, 3)));
167extern int vsscanf(const char *, const char *, va_list)
168 __attribute__ ((format (scanf, 2, 0)));
169
170extern int get_option(char **str, int *pint);
171extern char *get_options(const char *str, int nints, int *ints);
172extern unsigned long long memparse(char *ptr, char **retptr);
173
174extern int core_kernel_text(unsigned long addr);
175extern int __kernel_text_address(unsigned long addr);
176extern int kernel_text_address(unsigned long addr);
177struct pid;
178extern struct pid *session_of_pgrp(struct pid *pgrp);
179
180#ifdef CONFIG_PRINTK
181asmlinkage int vprintk(const char *fmt, va_list args)
182 __attribute__ ((format (printf, 1, 0)));
183asmlinkage int printk(const char * fmt, ...)
184 __attribute__ ((format (printf, 1, 2))) __cold;
185extern int log_buf_get_len(void);
186extern int log_buf_read(int idx);
187extern int log_buf_copy(char *dest, int idx, int len);
188
189extern int printk_ratelimit_jiffies;
190extern int printk_ratelimit_burst;
191extern int printk_ratelimit(void);
192extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
193extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
194 unsigned int interval_msec);
195#else
196static inline int vprintk(const char *s, va_list args)
197 __attribute__ ((format (printf, 1, 0)));
198static inline int vprintk(const char *s, va_list args) { return 0; }
199static inline int printk(const char *s, ...)
200 __attribute__ ((format (printf, 1, 2)));
201static inline int __cold printk(const char *s, ...) { return 0; }
202static inline int log_buf_get_len(void) { return 0; }
203static inline int log_buf_read(int idx) { return 0; }
204static inline int log_buf_copy(char *dest, int idx, int len) { return 0; }
205static inline int printk_ratelimit(void) { return 0; }
206static inline int __printk_ratelimit(int ratelimit_jiffies, \
207 int ratelimit_burst) { return 0; }
208static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
209 unsigned int interval_msec) \
210 { return false; }
211#endif
212
213extern void __attribute__((format(printf, 1, 2)))
214 early_printk(const char *fmt, ...);
215
216unsigned long int_sqrt(unsigned long);
217
218static inline void console_silent(void)
219{
220 console_loglevel = 0;
221}
222
223static inline void console_verbose(void)
224{
225 if (console_loglevel)
226 console_loglevel = 15;
227}
228
229extern void bust_spinlocks(int yes);
230extern void wake_up_klogd(void);
231extern int oops_in_progress;
232extern int panic_timeout;
233extern int panic_on_oops;
234extern int panic_on_unrecovered_nmi;
235extern int tainted;
236extern const char *print_tainted(void);
237extern void add_taint(unsigned);
238extern int root_mountflags;
239
240
241extern enum system_states {
242 SYSTEM_BOOTING,
243 SYSTEM_RUNNING,
244 SYSTEM_HALT,
245 SYSTEM_POWER_OFF,
246 SYSTEM_RESTART,
247 SYSTEM_SUSPEND_DISK,
248} system_state;
249
250#define TAINT_PROPRIETARY_MODULE (1<<0)
251#define TAINT_FORCED_MODULE (1<<1)
252#define TAINT_UNSAFE_SMP (1<<2)
253#define TAINT_FORCED_RMMOD (1<<3)
254#define TAINT_MACHINE_CHECK (1<<4)
255#define TAINT_BAD_PAGE (1<<5)
256#define TAINT_USER (1<<6)
257#define TAINT_DIE (1<<7)
258#define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8)
259
260extern void dump_stack(void) __cold;
261
262enum {
263 DUMP_PREFIX_NONE,
264 DUMP_PREFIX_ADDRESS,
265 DUMP_PREFIX_OFFSET
266};
267extern void hex_dump_to_buffer(const void *buf, size_t len,
268 int rowsize, int groupsize,
269 char *linebuf, size_t linebuflen, bool ascii);
270extern void print_hex_dump(const char *level, const char *prefix_str,
271 int prefix_type, int rowsize, int groupsize,
272 const void *buf, size_t len, bool ascii);
273extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
274 const void *buf, size_t len);
275#define hex_asc(x) "0123456789abcdef"[x]
276
277#define pr_emerg(fmt, arg...) \
278 printk(KERN_EMERG fmt, ##arg)
279#define pr_alert(fmt, arg...) \
280 printk(KERN_ALERT fmt, ##arg)
281#define pr_crit(fmt, arg...) \
282 printk(KERN_CRIT fmt, ##arg)
283#define pr_err(fmt, arg...) \
284 printk(KERN_ERR fmt, ##arg)
285#define pr_warning(fmt, arg...) \
286 printk(KERN_WARNING fmt, ##arg)
287#define pr_notice(fmt, arg...) \
288 printk(KERN_NOTICE fmt, ##arg)
289#define pr_info(fmt, arg...) \
290 printk(KERN_INFO fmt, ##arg)
291
292#ifdef DEBUG
293
294#define pr_debug(fmt, arg...) \
295 printk(KERN_DEBUG fmt, ##arg)
296#else
297static inline int __attribute__ ((format (printf, 1, 2))) pr_debug(const char * fmt, ...)
298{
299 return 0;
300}
301#endif
302
303
304
305
306
307#define NIPQUAD(addr) \
308 ((unsigned char *)&addr)[0], \
309 ((unsigned char *)&addr)[1], \
310 ((unsigned char *)&addr)[2], \
311 ((unsigned char *)&addr)[3]
312#define NIPQUAD_FMT "%u.%u.%u.%u"
313
314#define NIP6(addr) \
315 ntohs((addr).s6_addr16[0]), \
316 ntohs((addr).s6_addr16[1]), \
317 ntohs((addr).s6_addr16[2]), \
318 ntohs((addr).s6_addr16[3]), \
319 ntohs((addr).s6_addr16[4]), \
320 ntohs((addr).s6_addr16[5]), \
321 ntohs((addr).s6_addr16[6]), \
322 ntohs((addr).s6_addr16[7])
323#define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
324#define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"
325
326#if defined(__LITTLE_ENDIAN)
327#define HIPQUAD(addr) \
328 ((unsigned char *)&addr)[3], \
329 ((unsigned char *)&addr)[2], \
330 ((unsigned char *)&addr)[1], \
331 ((unsigned char *)&addr)[0]
332#elif defined(__BIG_ENDIAN)
333#define HIPQUAD NIPQUAD
334#else
335#error "Please fix asm/byteorder.h"
336#endif
337
338
339
340
341
342
343#define min(x,y) ({ \
344 typeof(x) _x = (x); \
345 typeof(y) _y = (y); \
346 (void) (&_x == &_y); \
347 _x < _y ? _x : _y; })
348
349#define max(x,y) ({ \
350 typeof(x) _x = (x); \
351 typeof(y) _y = (y); \
352 (void) (&_x == &_y); \
353 _x > _y ? _x : _y; })
354
355
356
357
358
359
360
361#define min_t(type,x,y) \
362 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
363#define max_t(type,x,y) \
364 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
365
366
367
368
369
370
371
372
373
374#define container_of(ptr, type, member) ({ \
375 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
376 (type *)( (char *)__mptr - offsetof(type,member) );})
377
378
379
380
381
382#define typecheck(type,x) \
383({ type __dummy; \
384 typeof(x) __dummy2; \
385 (void)(&__dummy == &__dummy2); \
386 1; \
387})
388
389
390
391
392
393#define typecheck_fn(type,function) \
394({ typeof(type) __tmp = function; \
395 (void)__tmp; \
396})
397
398struct sysinfo;
399extern int do_sysinfo(struct sysinfo *info);
400
401#endif
402
403#define SI_LOAD_SHIFT 16
404struct sysinfo {
405 long uptime;
406 unsigned long loads[3];
407 unsigned long totalram;
408 unsigned long freeram;
409 unsigned long sharedram;
410 unsigned long bufferram;
411 unsigned long totalswap;
412 unsigned long freeswap;
413 unsigned short procs;
414 unsigned short pad;
415 unsigned long totalhigh;
416 unsigned long freehigh;
417 unsigned int mem_unit;
418 char _f[20-2*sizeof(long)-sizeof(int)];
419};
420
421
422#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
423
424
425
426
427
428#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
429
430
431#define __FUNCTION__ (__func__)
432
433
434#ifdef CONFIG_NUMA
435#define NUMA_BUILD 1
436#else
437#define NUMA_BUILD 0
438#endif
439
440#endif
441