1#ifndef _LINUX_KERNEL_H
2#define _LINUX_KERNEL_H
3
4
5
6
7#define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
8#define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
9
10#ifdef __KERNEL__
11
12#include <stdarg.h>
13#include <linux/linkage.h>
14#include <linux/stddef.h>
15#include <linux/types.h>
16#include <linux/compiler.h>
17#include <linux/bitops.h>
18#include <linux/log2.h>
19#include <linux/typecheck.h>
20#include <linux/printk.h>
21#include <linux/dynamic_debug.h>
22#include <asm/byteorder.h>
23#include <asm/bug.h>
24
25#define USHRT_MAX ((u16)(~0U))
26#define SHRT_MAX ((s16)(USHRT_MAX>>1))
27#define SHRT_MIN ((s16)(-SHRT_MAX - 1))
28#define INT_MAX ((int)(~0U>>1))
29#define INT_MIN (-INT_MAX - 1)
30#define UINT_MAX (~0U)
31#define LONG_MAX ((long)(~0UL>>1))
32#define LONG_MIN (-LONG_MAX - 1)
33#define ULONG_MAX (~0UL)
34#define LLONG_MAX ((long long)(~0ULL>>1))
35#define LLONG_MIN (-LLONG_MAX - 1)
36#define ULLONG_MAX (~0ULL)
37
38#define STACK_MAGIC 0xdeadbeef
39
40#define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
41#define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
42#define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
43#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
44
45#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
46
47
48
49
50
51
52
53#define __round_mask(x, y) ((__typeof__(x))((y)-1))
54#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
55#define round_down(x, y) ((x) & ~__round_mask(x, y))
56
57#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
58#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
59#define roundup(x, y) ( \
60{ \
61 const typeof(y) __y = y; \
62 (((x) + (__y - 1)) / __y) * __y; \
63} \
64)
65#define rounddown(x, y) ( \
66{ \
67 typeof(x) __x = (x); \
68 __x - (__x % (y)); \
69} \
70)
71#define DIV_ROUND_CLOSEST(x, divisor)( \
72{ \
73 typeof(divisor) __divisor = divisor; \
74 (((x) + ((__divisor) / 2)) / (__divisor)); \
75} \
76)
77
78#define _RET_IP_ (unsigned long)__builtin_return_address(0)
79#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
80
81#ifdef CONFIG_LBDAF
82# include <asm/div64.h>
83# define sector_div(a, b) do_div(a, b)
84#else
85# define sector_div(n, b)( \
86{ \
87 int _res; \
88 _res = (n) % (b); \
89 (n) /= (b); \
90 _res; \
91} \
92)
93#endif
94
95
96
97
98
99
100
101
102
103#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
104
105
106
107
108
109#define lower_32_bits(n) ((u32)(n))
110
111struct completion;
112struct pt_regs;
113struct user;
114
115#ifdef CONFIG_PREEMPT_VOLUNTARY
116extern int _cond_resched(void);
117# define might_resched() _cond_resched()
118#else
119# define might_resched() do { } while (0)
120#endif
121
122#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
123 void __might_sleep(const char *file, int line, int preempt_offset);
124
125
126
127
128
129
130
131
132
133
134# define might_sleep() \
135 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
136#else
137 static inline void __might_sleep(const char *file, int line,
138 int preempt_offset) { }
139# define might_sleep() do { might_resched(); } while (0)
140#endif
141
142#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
143
144#define abs(x) ({ \
145 long __x = (x); \
146 (__x < 0) ? -__x : __x; \
147 })
148
149#define abs64(x) ({ \
150 s64 __x = (x); \
151 (__x < 0) ? -__x : __x; \
152 })
153
154#ifdef CONFIG_PROVE_LOCKING
155void might_fault(void);
156#else
157static inline void might_fault(void)
158{
159 might_sleep();
160}
161#endif
162
163extern struct atomic_notifier_head panic_notifier_list;
164extern long (*panic_blink)(int state);
165NORET_TYPE void panic(const char * fmt, ...)
166 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
167extern void oops_enter(void);
168extern void oops_exit(void);
169void print_oops_end_marker(void);
170extern int oops_may_print(void);
171NORET_TYPE void do_exit(long error_code)
172 ATTRIB_NORET;
173NORET_TYPE void complete_and_exit(struct completion *, long)
174 ATTRIB_NORET;
175extern unsigned long simple_strtoul(const char *,char **,unsigned int);
176extern long simple_strtol(const char *,char **,unsigned int);
177extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
178extern long long simple_strtoll(const char *,char **,unsigned int);
179extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
180extern int __must_check strict_strtol(const char *, unsigned int, long *);
181extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
182extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
183extern int sprintf(char * buf, const char * fmt, ...)
184 __attribute__ ((format (printf, 2, 3)));
185extern int vsprintf(char *buf, const char *, va_list)
186 __attribute__ ((format (printf, 2, 0)));
187extern int snprintf(char * buf, size_t size, const char * fmt, ...)
188 __attribute__ ((format (printf, 3, 4)));
189extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
190 __attribute__ ((format (printf, 3, 0)));
191extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
192 __attribute__ ((format (printf, 3, 4)));
193extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
194 __attribute__ ((format (printf, 3, 0)));
195extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
196 __attribute__ ((format (printf, 2, 3)));
197extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
198
199extern int sscanf(const char *, const char *, ...)
200 __attribute__ ((format (scanf, 2, 3)));
201extern int vsscanf(const char *, const char *, va_list)
202 __attribute__ ((format (scanf, 2, 0)));
203
204extern int get_option(char **str, int *pint);
205extern char *get_options(const char *str, int nints, int *ints);
206extern unsigned long long memparse(const char *ptr, char **retptr);
207
208extern int core_kernel_text(unsigned long addr);
209extern int __kernel_text_address(unsigned long addr);
210extern int kernel_text_address(unsigned long addr);
211extern int func_ptr_is_kernel_text(void *ptr);
212
213struct pid;
214extern struct pid *session_of_pgrp(struct pid *pgrp);
215
216unsigned long int_sqrt(unsigned long);
217
218extern void bust_spinlocks(int yes);
219extern void wake_up_klogd(void);
220extern int oops_in_progress;
221extern int panic_timeout;
222extern int panic_on_oops;
223extern int panic_on_unrecovered_nmi;
224extern int panic_on_io_nmi;
225extern const char *print_tainted(void);
226extern void add_taint(unsigned flag);
227extern int test_taint(unsigned flag);
228extern unsigned long get_taint(void);
229extern int root_mountflags;
230
231
232extern enum system_states {
233 SYSTEM_BOOTING,
234 SYSTEM_RUNNING,
235 SYSTEM_HALT,
236 SYSTEM_POWER_OFF,
237 SYSTEM_RESTART,
238 SYSTEM_SUSPEND_DISK,
239} system_state;
240
241#define TAINT_PROPRIETARY_MODULE 0
242#define TAINT_FORCED_MODULE 1
243#define TAINT_UNSAFE_SMP 2
244#define TAINT_FORCED_RMMOD 3
245#define TAINT_MACHINE_CHECK 4
246#define TAINT_BAD_PAGE 5
247#define TAINT_USER 6
248#define TAINT_DIE 7
249#define TAINT_OVERRIDDEN_ACPI_TABLE 8
250#define TAINT_WARN 9
251#define TAINT_CRAP 10
252#define TAINT_FIRMWARE_WORKAROUND 11
253
254extern const char hex_asc[];
255#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
256#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
257
258static inline char *pack_hex_byte(char *buf, u8 byte)
259{
260 *buf++ = hex_asc_hi(byte);
261 *buf++ = hex_asc_lo(byte);
262 return buf;
263}
264
265extern int hex_to_bin(char ch);
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286#ifdef CONFIG_RING_BUFFER
287void tracing_on(void);
288void tracing_off(void);
289
290void tracing_off_permanent(void);
291int tracing_is_on(void);
292#else
293static inline void tracing_on(void) { }
294static inline void tracing_off(void) { }
295static inline void tracing_off_permanent(void) { }
296static inline int tracing_is_on(void) { return 0; }
297#endif
298
299enum ftrace_dump_mode {
300 DUMP_NONE,
301 DUMP_ALL,
302 DUMP_ORIG,
303};
304
305#ifdef CONFIG_TRACING
306extern void tracing_start(void);
307extern void tracing_stop(void);
308extern void ftrace_off_permanent(void);
309
310static inline void __attribute__ ((format (printf, 1, 2)))
311____trace_printk_check_format(const char *fmt, ...)
312{
313}
314#define __trace_printk_check_format(fmt, args...) \
315do { \
316 if (0) \
317 ____trace_printk_check_format(fmt, ##args); \
318} while (0)
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337#define trace_printk(fmt, args...) \
338do { \
339 __trace_printk_check_format(fmt, ##args); \
340 if (__builtin_constant_p(fmt)) { \
341 static const char *trace_printk_fmt \
342 __attribute__((section("__trace_printk_fmt"))) = \
343 __builtin_constant_p(fmt) ? fmt : NULL; \
344 \
345 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
346 } else \
347 __trace_printk(_THIS_IP_, fmt, ##args); \
348} while (0)
349
350extern int
351__trace_bprintk(unsigned long ip, const char *fmt, ...)
352 __attribute__ ((format (printf, 2, 3)));
353
354extern int
355__trace_printk(unsigned long ip, const char *fmt, ...)
356 __attribute__ ((format (printf, 2, 3)));
357
358extern void trace_dump_stack(void);
359
360
361
362
363
364
365#define ftrace_vprintk(fmt, vargs) \
366do { \
367 if (__builtin_constant_p(fmt)) { \
368 static const char *trace_printk_fmt \
369 __attribute__((section("__trace_printk_fmt"))) = \
370 __builtin_constant_p(fmt) ? fmt : NULL; \
371 \
372 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
373 } else \
374 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
375} while (0)
376
377extern int
378__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
379
380extern int
381__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
382
383extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
384#else
385static inline int
386trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
387
388static inline void tracing_start(void) { }
389static inline void tracing_stop(void) { }
390static inline void ftrace_off_permanent(void) { }
391static inline void trace_dump_stack(void) { }
392static inline int
393trace_printk(const char *fmt, ...)
394{
395 return 0;
396}
397static inline int
398ftrace_vprintk(const char *fmt, va_list ap)
399{
400 return 0;
401}
402static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
403#endif
404
405
406
407
408
409
410#define min(x, y) ({ \
411 typeof(x) _min1 = (x); \
412 typeof(y) _min2 = (y); \
413 (void) (&_min1 == &_min2); \
414 _min1 < _min2 ? _min1 : _min2; })
415
416#define max(x, y) ({ \
417 typeof(x) _max1 = (x); \
418 typeof(y) _max2 = (y); \
419 (void) (&_max1 == &_max2); \
420 _max1 > _max2 ? _max1 : _max2; })
421
422#define min3(x, y, z) ({ \
423 typeof(x) _min1 = (x); \
424 typeof(y) _min2 = (y); \
425 typeof(z) _min3 = (z); \
426 (void) (&_min1 == &_min2); \
427 (void) (&_min1 == &_min3); \
428 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
429 (_min2 < _min3 ? _min2 : _min3); })
430
431#define max3(x, y, z) ({ \
432 typeof(x) _max1 = (x); \
433 typeof(y) _max2 = (y); \
434 typeof(z) _max3 = (z); \
435 (void) (&_max1 == &_max2); \
436 (void) (&_max1 == &_max3); \
437 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
438 (_max2 > _max3 ? _max2 : _max3); })
439
440
441
442
443
444
445#define min_not_zero(x, y) ({ \
446 typeof(x) __x = (x); \
447 typeof(y) __y = (y); \
448 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
449
450
451
452
453
454
455
456
457
458
459#define clamp(val, min, max) ({ \
460 typeof(val) __val = (val); \
461 typeof(min) __min = (min); \
462 typeof(max) __max = (max); \
463 (void) (&__val == &__min); \
464 (void) (&__val == &__max); \
465 __val = __val < __min ? __min: __val; \
466 __val > __max ? __max: __val; })
467
468
469
470
471
472
473
474#define min_t(type, x, y) ({ \
475 type __min1 = (x); \
476 type __min2 = (y); \
477 __min1 < __min2 ? __min1: __min2; })
478
479#define max_t(type, x, y) ({ \
480 type __max1 = (x); \
481 type __max2 = (y); \
482 __max1 > __max2 ? __max1: __max2; })
483
484
485
486
487
488
489
490
491
492
493
494#define clamp_t(type, val, min, max) ({ \
495 type __val = (val); \
496 type __min = (min); \
497 type __max = (max); \
498 __val = __val < __min ? __min: __val; \
499 __val > __max ? __max: __val; })
500
501
502
503
504
505
506
507
508
509
510
511
512#define clamp_val(val, min, max) ({ \
513 typeof(val) __val = (val); \
514 typeof(val) __min = (min); \
515 typeof(val) __max = (max); \
516 __val = __val < __min ? __min: __val; \
517 __val > __max ? __max: __val; })
518
519
520
521
522
523#define swap(a, b) \
524 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
525
526
527
528
529
530
531
532
533#define container_of(ptr, type, member) ({ \
534 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
535 (type *)( (char *)__mptr - offsetof(type,member) );})
536
537struct sysinfo;
538extern int do_sysinfo(struct sysinfo *info);
539
540#endif
541
542#define SI_LOAD_SHIFT 16
543struct sysinfo {
544 long uptime;
545 unsigned long loads[3];
546 unsigned long totalram;
547 unsigned long freeram;
548 unsigned long sharedram;
549 unsigned long bufferram;
550 unsigned long totalswap;
551 unsigned long freeswap;
552 unsigned short procs;
553 unsigned short pad;
554 unsigned long totalhigh;
555 unsigned long freehigh;
556 unsigned int mem_unit;
557 char _f[20-2*sizeof(long)-sizeof(int)];
558};
559
560
561#define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
562
563
564#define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
565
566
567#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
568 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
569
570
571
572
573
574#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
575#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
576
577
578#define __FUNCTION__ (__func__)
579
580
581#ifdef CONFIG_NUMA
582#define NUMA_BUILD 1
583#else
584#define NUMA_BUILD 0
585#endif
586
587
588#ifdef CONFIG_FTRACE_MCOUNT_RECORD
589# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
590#endif
591
592#endif
593