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