1
2#ifndef _LINUX_KERNEL_H
3#define _LINUX_KERNEL_H
4
5#include <linux/stdarg.h>
6#include <linux/align.h>
7#include <linux/limits.h>
8#include <linux/linkage.h>
9#include <linux/stddef.h>
10#include <linux/types.h>
11#include <linux/compiler.h>
12#include <linux/bitops.h>
13#include <linux/kstrtox.h>
14#include <linux/log2.h>
15#include <linux/math.h>
16#include <linux/minmax.h>
17#include <linux/typecheck.h>
18#include <linux/panic.h>
19#include <linux/printk.h>
20#include <linux/build_bug.h>
21#include <linux/static_call_types.h>
22#include <asm/byteorder.h>
23
24#include <uapi/linux/kernel.h>
25
26#define STACK_MAGIC 0xdeadbeef
27
28
29
30
31
32
33
34#define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
35
36
37#define READ 0
38#define WRITE 1
39
40
41
42
43
44#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
45
46#define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
47
48#define u64_to_user_ptr(x) ( \
49{ \
50 typecheck(u64, (x)); \
51 (void __user *)(uintptr_t)(x); \
52} \
53)
54
55#define typeof_member(T, m) typeof(((T*)0)->m)
56
57#define _RET_IP_ (unsigned long)__builtin_return_address(0)
58#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
59
60
61
62
63
64
65
66
67
68#define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
69
70
71
72
73
74#define lower_32_bits(n) ((u32)((n) & 0xffffffff))
75
76
77
78
79
80#define upper_16_bits(n) ((u16)((n) >> 16))
81
82
83
84
85
86#define lower_16_bits(n) ((u16)((n) & 0xffff))
87
88struct completion;
89struct user;
90
91#ifdef CONFIG_PREEMPT_VOLUNTARY
92
93extern int __cond_resched(void);
94# define might_resched() __cond_resched()
95
96#elif defined(CONFIG_PREEMPT_DYNAMIC)
97
98extern int __cond_resched(void);
99
100DECLARE_STATIC_CALL(might_resched, __cond_resched);
101
102static __always_inline void might_resched(void)
103{
104 static_call_mod(might_resched)();
105}
106
107#else
108
109# define might_resched() do { } while (0)
110
111#endif
112
113#ifdef CONFIG_DEBUG_ATOMIC_SLEEP
114extern void ___might_sleep(const char *file, int line, int preempt_offset);
115extern void __might_sleep(const char *file, int line, int preempt_offset);
116extern void __cant_sleep(const char *file, int line, int preempt_offset);
117extern void __cant_migrate(const char *file, int line);
118
119
120
121
122
123
124
125
126
127
128
129
130
131# define might_sleep() \
132 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
133
134
135
136
137
138# define cant_sleep() \
139 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
140# define sched_annotate_sleep() (current->task_state_change = 0)
141
142
143
144
145
146
147# define cant_migrate() \
148 do { \
149 if (IS_ENABLED(CONFIG_SMP)) \
150 __cant_migrate(__FILE__, __LINE__); \
151 } while (0)
152
153
154
155
156
157
158
159
160
161
162
163# define non_block_start() (current->non_block_count++)
164
165
166
167
168
169# define non_block_end() WARN_ON(current->non_block_count-- == 0)
170#else
171 static inline void ___might_sleep(const char *file, int line,
172 int preempt_offset) { }
173 static inline void __might_sleep(const char *file, int line,
174 int preempt_offset) { }
175# define might_sleep() do { might_resched(); } while (0)
176# define cant_sleep() do { } while (0)
177# define cant_migrate() do { } while (0)
178# define sched_annotate_sleep() do { } while (0)
179# define non_block_start() do { } while (0)
180# define non_block_end() do { } while (0)
181#endif
182
183#define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
184
185#if defined(CONFIG_MMU) && \
186 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
187#define might_fault() __might_fault(__FILE__, __LINE__)
188void __might_fault(const char *file, int line);
189#else
190static inline void might_fault(void) { }
191#endif
192
193void do_exit(long error_code) __noreturn;
194void complete_and_exit(struct completion *, long) __noreturn;
195
196extern int num_to_str(char *buf, int size,
197 unsigned long long num, unsigned int width);
198
199
200
201extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
202extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
203extern __printf(3, 4)
204int snprintf(char *buf, size_t size, const char *fmt, ...);
205extern __printf(3, 0)
206int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
207extern __printf(3, 4)
208int scnprintf(char *buf, size_t size, const char *fmt, ...);
209extern __printf(3, 0)
210int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
211extern __printf(2, 3) __malloc
212char *kasprintf(gfp_t gfp, const char *fmt, ...);
213extern __printf(2, 0) __malloc
214char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
215extern __printf(2, 0)
216const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
217
218extern __scanf(2, 3)
219int sscanf(const char *, const char *, ...);
220extern __scanf(2, 0)
221int vsscanf(const char *, const char *, va_list);
222
223extern int no_hash_pointers_enable(char *str);
224
225extern int get_option(char **str, int *pint);
226extern char *get_options(const char *str, int nints, int *ints);
227extern unsigned long long memparse(const char *ptr, char **retptr);
228extern bool parse_option_str(const char *str, const char *option);
229extern char *next_arg(char *args, char **param, char **val);
230
231extern int core_kernel_text(unsigned long addr);
232extern int init_kernel_text(unsigned long addr);
233extern int core_kernel_data(unsigned long addr);
234extern int __kernel_text_address(unsigned long addr);
235extern int kernel_text_address(unsigned long addr);
236extern int func_ptr_is_kernel_text(void *ptr);
237
238extern void bust_spinlocks(int yes);
239
240extern int root_mountflags;
241
242extern bool early_boot_irqs_disabled;
243
244
245
246
247
248extern enum system_states {
249 SYSTEM_BOOTING,
250 SYSTEM_SCHEDULING,
251 SYSTEM_RUNNING,
252 SYSTEM_HALT,
253 SYSTEM_POWER_OFF,
254 SYSTEM_RESTART,
255 SYSTEM_SUSPEND,
256} system_state;
257
258extern const char hex_asc[];
259#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
260#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
261
262static inline char *hex_byte_pack(char *buf, u8 byte)
263{
264 *buf++ = hex_asc_hi(byte);
265 *buf++ = hex_asc_lo(byte);
266 return buf;
267}
268
269extern const char hex_asc_upper[];
270#define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
271#define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
272
273static inline char *hex_byte_pack_upper(char *buf, u8 byte)
274{
275 *buf++ = hex_asc_upper_hi(byte);
276 *buf++ = hex_asc_upper_lo(byte);
277 return buf;
278}
279
280extern int hex_to_bin(char ch);
281extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
282extern char *bin2hex(char *dst, const void *src, size_t count);
283
284bool mac_pton(const char *s, u8 *mac);
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306enum ftrace_dump_mode {
307 DUMP_NONE,
308 DUMP_ALL,
309 DUMP_ORIG,
310};
311
312#ifdef CONFIG_TRACING
313void tracing_on(void);
314void tracing_off(void);
315int tracing_is_on(void);
316void tracing_snapshot(void);
317void tracing_snapshot_alloc(void);
318
319extern void tracing_start(void);
320extern void tracing_stop(void);
321
322static inline __printf(1, 2)
323void ____trace_printk_check_format(const char *fmt, ...)
324{
325}
326#define __trace_printk_check_format(fmt, args...) \
327do { \
328 if (0) \
329 ____trace_printk_check_format(fmt, ##args); \
330} while (0)
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362#define trace_printk(fmt, ...) \
363do { \
364 char _______STR[] = __stringify((__VA_ARGS__)); \
365 if (sizeof(_______STR) > 3) \
366 do_trace_printk(fmt, ##__VA_ARGS__); \
367 else \
368 trace_puts(fmt); \
369} while (0)
370
371#define do_trace_printk(fmt, args...) \
372do { \
373 static const char *trace_printk_fmt __used \
374 __section("__trace_printk_fmt") = \
375 __builtin_constant_p(fmt) ? fmt : NULL; \
376 \
377 __trace_printk_check_format(fmt, ##args); \
378 \
379 if (__builtin_constant_p(fmt)) \
380 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
381 else \
382 __trace_printk(_THIS_IP_, fmt, ##args); \
383} while (0)
384
385extern __printf(2, 3)
386int __trace_bprintk(unsigned long ip, const char *fmt, ...);
387
388extern __printf(2, 3)
389int __trace_printk(unsigned long ip, const char *fmt, ...);
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416#define trace_puts(str) ({ \
417 static const char *trace_printk_fmt __used \
418 __section("__trace_printk_fmt") = \
419 __builtin_constant_p(str) ? str : NULL; \
420 \
421 if (__builtin_constant_p(str)) \
422 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
423 else \
424 __trace_puts(_THIS_IP_, str, strlen(str)); \
425})
426extern int __trace_bputs(unsigned long ip, const char *str);
427extern int __trace_puts(unsigned long ip, const char *str, int size);
428
429extern void trace_dump_stack(int skip);
430
431
432
433
434
435
436#define ftrace_vprintk(fmt, vargs) \
437do { \
438 if (__builtin_constant_p(fmt)) { \
439 static const char *trace_printk_fmt __used \
440 __section("__trace_printk_fmt") = \
441 __builtin_constant_p(fmt) ? fmt : NULL; \
442 \
443 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
444 } else \
445 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
446} while (0)
447
448extern __printf(2, 0) int
449__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
450
451extern __printf(2, 0) int
452__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
453
454extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
455#else
456static inline void tracing_start(void) { }
457static inline void tracing_stop(void) { }
458static inline void trace_dump_stack(int skip) { }
459
460static inline void tracing_on(void) { }
461static inline void tracing_off(void) { }
462static inline int tracing_is_on(void) { return 0; }
463static inline void tracing_snapshot(void) { }
464static inline void tracing_snapshot_alloc(void) { }
465
466static inline __printf(1, 2)
467int trace_printk(const char *fmt, ...)
468{
469 return 0;
470}
471static __printf(1, 0) inline int
472ftrace_vprintk(const char *fmt, va_list ap)
473{
474 return 0;
475}
476static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
477#endif
478
479
480#define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
481#define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
482
483#define __CONCAT(a, b) a ## b
484#define CONCATENATE(a, b) __CONCAT(a, b)
485
486
487
488
489
490
491
492
493#define container_of(ptr, type, member) ({ \
494 void *__mptr = (void *)(ptr); \
495 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
496 !__same_type(*(ptr), void), \
497 "pointer type mismatch in container_of()"); \
498 ((type *)(__mptr - offsetof(type, member))); })
499
500
501
502
503
504
505
506
507
508#define container_of_safe(ptr, type, member) ({ \
509 void *__mptr = (void *)(ptr); \
510 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
511 !__same_type(*(ptr), void), \
512 "pointer type mismatch in container_of()"); \
513 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
514 ((type *)(__mptr - offsetof(type, member))); })
515
516
517#ifdef CONFIG_FTRACE_MCOUNT_RECORD
518# define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
519#endif
520
521
522#define VERIFY_OCTAL_PERMISSIONS(perms) \
523 (BUILD_BUG_ON_ZERO((perms) < 0) + \
524 BUILD_BUG_ON_ZERO((perms) > 0777) + \
525 \
526 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
527 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
528 \
529 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
530 \
531 BUILD_BUG_ON_ZERO((perms) & 2) + \
532 (perms))
533#endif
534