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