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