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
60
61#define roundup(x, y) ( \
62{ \
63 const typeof(y) __y = y; \
64 (((x) + (__y - 1)) / __y) * __y; \
65} \
66)
67#define rounddown(x, y) ( \
68{ \
69 typeof(x) __x = (x); \
70 __x - (__x % (y)); \
71} \
72)
73#define DIV_ROUND_CLOSEST(x, divisor)( \
74{ \
75 typeof(divisor) __divisor = divisor; \
76 (((x) + ((__divisor) / 2)) / (__divisor)); \
77} \
78)
79
80#define _RET_IP_ (unsigned long)__builtin_return_address(0)
81#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
82
83#ifdef CONFIG_LBDAF
84# include <asm/div64.h>
85# define sector_div(a, b) do_div(a, b)
86#else
87# define sector_div(n, b)( \
88{ \
89 int _res; \
90 _res = (n) % (b); \
91 (n) /= (b); \
92 _res; \
93} \
94)
95#endif
96
97
98
99
100
101
102
103
104
105#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
106
107
108
109
110
111#define lower_32_bits(n) ((u32)(n))
112
113struct completion;
114struct pt_regs;
115struct user;
116
117#ifdef CONFIG_PREEMPT_VOLUNTARY
118extern int _cond_resched(void);
119# define might_resched() _cond_resched()
120#else
121# define might_resched() do { } while (0)
122#endif
123
124#ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
125 void __might_sleep(const char *file, int line, int preempt_offset);
126
127
128
129
130
131
132
133
134
135
136# define might_sleep() \
137 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
138#else
139 static inline void __might_sleep(const char *file, int line,
140 int preempt_offset) { }
141# define might_sleep() do { might_resched(); } while (0)
142#endif
143
144#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
145
146
147
148
149
150
151
152#define abs(x) ({ \
153 long ret; \
154 if (sizeof(x) == sizeof(long)) { \
155 long __x = (x); \
156 ret = (__x < 0) ? -__x : __x; \
157 } else { \
158 int __x = (x); \
159 ret = (__x < 0) ? -__x : __x; \
160 } \
161 ret; \
162 })
163
164#define abs64(x) ({ \
165 s64 __x = (x); \
166 (__x < 0) ? -__x : __x; \
167 })
168
169#ifdef CONFIG_PROVE_LOCKING
170void might_fault(void);
171#else
172static inline void might_fault(void)
173{
174 might_sleep();
175}
176#endif
177
178extern struct atomic_notifier_head panic_notifier_list;
179extern long (*panic_blink)(int state);
180NORET_TYPE void panic(const char * fmt, ...)
181 __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
182extern void oops_enter(void);
183extern void oops_exit(void);
184void print_oops_end_marker(void);
185extern int oops_may_print(void);
186NORET_TYPE void do_exit(long error_code)
187 ATTRIB_NORET;
188NORET_TYPE void complete_and_exit(struct completion *, long)
189 ATTRIB_NORET;
190
191
192int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
193int __must_check _kstrtol(const char *s, unsigned int base, long *res);
194
195int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
196int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
197static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
198{
199
200
201
202
203 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
204 __alignof__(unsigned long) == __alignof__(unsigned long long))
205 return kstrtoull(s, base, (unsigned long long *)res);
206 else
207 return _kstrtoul(s, base, res);
208}
209
210static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
211{
212
213
214
215
216 if (sizeof(long) == sizeof(long long) &&
217 __alignof__(long) == __alignof__(long long))
218 return kstrtoll(s, base, (long long *)res);
219 else
220 return _kstrtol(s, base, res);
221}
222
223int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
224int __must_check kstrtoint(const char *s, unsigned int base, int *res);
225
226static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
227{
228 return kstrtoull(s, base, res);
229}
230
231static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
232{
233 return kstrtoll(s, base, res);
234}
235
236static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
237{
238 return kstrtouint(s, base, res);
239}
240
241static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
242{
243 return kstrtoint(s, base, res);
244}
245
246int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
247int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
248int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
249int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
250
251extern unsigned long simple_strtoul(const char *,char **,unsigned int);
252extern long simple_strtol(const char *,char **,unsigned int);
253extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
254extern long long simple_strtoll(const char *,char **,unsigned int);
255#define strict_strtoul kstrtoul
256#define strict_strtol kstrtol
257#define strict_strtoull kstrtoull
258#define strict_strtoll kstrtoll
259
260extern int sprintf(char * buf, const char * fmt, ...)
261 __attribute__ ((format (printf, 2, 3)));
262extern int vsprintf(char *buf, const char *, va_list)
263 __attribute__ ((format (printf, 2, 0)));
264extern int snprintf(char * buf, size_t size, const char * fmt, ...)
265 __attribute__ ((format (printf, 3, 4)));
266extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
267 __attribute__ ((format (printf, 3, 0)));
268extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
269 __attribute__ ((format (printf, 3, 4)));
270extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
271 __attribute__ ((format (printf, 3, 0)));
272extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
273 __attribute__ ((format (printf, 2, 3)));
274extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
275
276extern int sscanf(const char *, const char *, ...)
277 __attribute__ ((format (scanf, 2, 3)));
278extern int vsscanf(const char *, const char *, va_list)
279 __attribute__ ((format (scanf, 2, 0)));
280
281extern int get_option(char **str, int *pint);
282extern char *get_options(const char *str, int nints, int *ints);
283extern unsigned long long memparse(const char *ptr, char **retptr);
284
285extern int core_kernel_text(unsigned long addr);
286extern int __kernel_text_address(unsigned long addr);
287extern int kernel_text_address(unsigned long addr);
288extern int func_ptr_is_kernel_text(void *ptr);
289
290struct pid;
291extern struct pid *session_of_pgrp(struct pid *pgrp);
292
293unsigned long int_sqrt(unsigned long);
294
295extern void bust_spinlocks(int yes);
296extern void wake_up_klogd(void);
297extern int oops_in_progress;
298extern int panic_timeout;
299extern int panic_on_oops;
300extern int panic_on_unrecovered_nmi;
301extern int panic_on_io_nmi;
302extern const char *print_tainted(void);
303extern void add_taint(unsigned flag);
304extern int test_taint(unsigned flag);
305extern unsigned long get_taint(void);
306extern int root_mountflags;
307
308extern bool early_boot_irqs_disabled;
309
310
311extern enum system_states {
312 SYSTEM_BOOTING,
313 SYSTEM_RUNNING,
314 SYSTEM_HALT,
315 SYSTEM_POWER_OFF,
316 SYSTEM_RESTART,
317 SYSTEM_SUSPEND_DISK,
318} system_state;
319
320#define TAINT_PROPRIETARY_MODULE 0
321#define TAINT_FORCED_MODULE 1
322#define TAINT_UNSAFE_SMP 2
323#define TAINT_FORCED_RMMOD 3
324#define TAINT_MACHINE_CHECK 4
325#define TAINT_BAD_PAGE 5
326#define TAINT_USER 6
327#define TAINT_DIE 7
328#define TAINT_OVERRIDDEN_ACPI_TABLE 8
329#define TAINT_WARN 9
330#define TAINT_CRAP 10
331#define TAINT_FIRMWARE_WORKAROUND 11
332
333extern const char hex_asc[];
334#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
335#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
336
337static inline char *pack_hex_byte(char *buf, u8 byte)
338{
339 *buf++ = hex_asc_hi(byte);
340 *buf++ = hex_asc_lo(byte);
341 return buf;
342}
343
344extern int hex_to_bin(char ch);
345extern void hex2bin(u8 *dst, const char *src, size_t count);
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366#ifdef CONFIG_RING_BUFFER
367void tracing_on(void);
368void tracing_off(void);
369
370void tracing_off_permanent(void);
371int tracing_is_on(void);
372#else
373static inline void tracing_on(void) { }
374static inline void tracing_off(void) { }
375static inline void tracing_off_permanent(void) { }
376static inline int tracing_is_on(void) { return 0; }
377#endif
378
379enum ftrace_dump_mode {
380 DUMP_NONE,
381 DUMP_ALL,
382 DUMP_ORIG,
383};
384
385#ifdef CONFIG_TRACING
386extern void tracing_start(void);
387extern void tracing_stop(void);
388extern void ftrace_off_permanent(void);
389
390static inline void __attribute__ ((format (printf, 1, 2)))
391____trace_printk_check_format(const char *fmt, ...)
392{
393}
394#define __trace_printk_check_format(fmt, args...) \
395do { \
396 if (0) \
397 ____trace_printk_check_format(fmt, ##args); \
398} while (0)
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417#define trace_printk(fmt, args...) \
418do { \
419 __trace_printk_check_format(fmt, ##args); \
420 if (__builtin_constant_p(fmt)) { \
421 static const char *trace_printk_fmt \
422 __attribute__((section("__trace_printk_fmt"))) = \
423 __builtin_constant_p(fmt) ? fmt : NULL; \
424 \
425 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
426 } else \
427 __trace_printk(_THIS_IP_, fmt, ##args); \
428} while (0)
429
430extern int
431__trace_bprintk(unsigned long ip, const char *fmt, ...)
432 __attribute__ ((format (printf, 2, 3)));
433
434extern int
435__trace_printk(unsigned long ip, const char *fmt, ...)
436 __attribute__ ((format (printf, 2, 3)));
437
438extern void trace_dump_stack(void);
439
440
441
442
443
444
445#define ftrace_vprintk(fmt, vargs) \
446do { \
447 if (__builtin_constant_p(fmt)) { \
448 static const char *trace_printk_fmt \
449 __attribute__((section("__trace_printk_fmt"))) = \
450 __builtin_constant_p(fmt) ? fmt : NULL; \
451 \
452 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
453 } else \
454 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
455} while (0)
456
457extern int
458__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
459
460extern int
461__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
462
463extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
464#else
465static inline int
466trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
467
468static inline void tracing_start(void) { }
469static inline void tracing_stop(void) { }
470static inline void ftrace_off_permanent(void) { }
471static inline void trace_dump_stack(void) { }
472static inline int
473trace_printk(const char *fmt, ...)
474{
475 return 0;
476}
477static inline int
478ftrace_vprintk(const char *fmt, va_list ap)
479{
480 return 0;
481}
482static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
483#endif
484
485
486
487
488
489
490#define min(x, y) ({ \
491 typeof(x) _min1 = (x); \
492 typeof(y) _min2 = (y); \
493 (void) (&_min1 == &_min2); \
494 _min1 < _min2 ? _min1 : _min2; })
495
496#define max(x, y) ({ \
497 typeof(x) _max1 = (x); \
498 typeof(y) _max2 = (y); \
499 (void) (&_max1 == &_max2); \
500 _max1 > _max2 ? _max1 : _max2; })
501
502#define min3(x, y, z) ({ \
503 typeof(x) _min1 = (x); \
504 typeof(y) _min2 = (y); \
505 typeof(z) _min3 = (z); \
506 (void) (&_min1 == &_min2); \
507 (void) (&_min1 == &_min3); \
508 _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
509 (_min2 < _min3 ? _min2 : _min3); })
510
511#define max3(x, y, z) ({ \
512 typeof(x) _max1 = (x); \
513 typeof(y) _max2 = (y); \
514 typeof(z) _max3 = (z); \
515 (void) (&_max1 == &_max2); \
516 (void) (&_max1 == &_max3); \
517 _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
518 (_max2 > _max3 ? _max2 : _max3); })
519
520
521
522
523
524
525#define min_not_zero(x, y) ({ \
526 typeof(x) __x = (x); \
527 typeof(y) __y = (y); \
528 __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
529
530
531
532
533
534
535
536
537
538
539#define clamp(val, min, max) ({ \
540 typeof(val) __val = (val); \
541 typeof(min) __min = (min); \
542 typeof(max) __max = (max); \
543 (void) (&__val == &__min); \
544 (void) (&__val == &__max); \
545 __val = __val < __min ? __min: __val; \
546 __val > __max ? __max: __val; })
547
548
549
550
551
552
553
554#define min_t(type, x, y) ({ \
555 type __min1 = (x); \
556 type __min2 = (y); \
557 __min1 < __min2 ? __min1: __min2; })
558
559#define max_t(type, x, y) ({ \
560 type __max1 = (x); \
561 type __max2 = (y); \
562 __max1 > __max2 ? __max1: __max2; })
563
564
565
566
567
568
569
570
571
572
573
574#define clamp_t(type, val, min, max) ({ \
575 type __val = (val); \
576 type __min = (min); \
577 type __max = (max); \
578 __val = __val < __min ? __min: __val; \
579 __val > __max ? __max: __val; })
580
581
582
583
584
585
586
587
588
589
590
591
592#define clamp_val(val, min, max) ({ \
593 typeof(val) __val = (val); \
594 typeof(val) __min = (min); \
595 typeof(val) __max = (max); \
596 __val = __val < __min ? __min: __val; \
597 __val > __max ? __max: __val; })
598
599
600
601
602
603#define swap(a, b) \
604 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
605
606
607
608
609
610
611
612
613#define container_of(ptr, type, member) ({ \
614 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
615 (type *)( (char *)__mptr - offsetof(type,member) );})
616
617struct sysinfo;
618extern int do_sysinfo(struct sysinfo *info);
619
620#endif
621
622#define SI_LOAD_SHIFT 16
623struct sysinfo {
624 long uptime;
625 unsigned long loads[3];
626 unsigned long totalram;
627 unsigned long freeram;
628 unsigned long sharedram;
629 unsigned long bufferram;
630 unsigned long totalswap;
631 unsigned long freeswap;
632 unsigned short procs;
633 unsigned short pad;
634 unsigned long totalhigh;
635 unsigned long freehigh;
636 unsigned int mem_unit;
637 char _f[20-2*sizeof(long)-sizeof(int)];
638};
639
640
641#define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
642 BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
643
644
645
646
647
648#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
649#define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666#ifndef __OPTIMIZE__
667#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
668#else
669extern int __build_bug_on_failed;
670#define BUILD_BUG_ON(condition) \
671 do { \
672 ((void)sizeof(char[1 - 2*!!(condition)])); \
673 if (condition) __build_bug_on_failed = 1; \
674 } while(0)
675#endif
676
677
678#define __FUNCTION__ (__func__)
679
680
681#ifdef CONFIG_NUMA
682#define NUMA_BUILD 1
683#else
684#define NUMA_BUILD 0
685#endif
686
687
688#ifdef CONFIG_COMPACTION
689#define COMPACTION_BUILD 1
690#else
691#define COMPACTION_BUILD 0
692#endif
693
694
695#ifdef CONFIG_FTRACE_MCOUNT_RECORD
696# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
697#endif
698
699#endif
700