1 /* SPDX-License-Identifier: GPL-2.0 */
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 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
32 * @x: value to repeat
33 *
34 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
35 */
36 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
37
38 /* generic data direction definitions */
39 #define READ 0
40 #define WRITE 1
41
42 /**
43 * ARRAY_SIZE - get the number of elements in array @arr
44 * @arr: array to be sized
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 * upper_32_bits - return bits 32-63 of a number
59 * @n: the number we're accessing
60 *
61 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
62 * the "right shift count >= width of type" warning when that quantity is
63 * 32-bits.
64 */
65 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
66
67 /**
68 * lower_32_bits - return bits 0-31 of a number
69 * @n: the number we're accessing
70 */
71 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
72
73 /**
74 * upper_16_bits - return bits 16-31 of a number
75 * @n: the number we're accessing
76 */
77 #define upper_16_bits(n) ((u16)((n) >> 16))
78
79 /**
80 * lower_16_bits - return bits 0-15 of a number
81 * @n: the number we're accessing
82 */
83 #define lower_16_bits(n) ((u16)((n) & 0xffff))
84
85 struct completion;
86 struct user;
87
88 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
89
90 extern int __cond_resched(void);
91 # define might_resched() __cond_resched()
92
93 #elif defined(CONFIG_PREEMPT_DYNAMIC)
94
95 extern int __cond_resched(void);
96
97 DECLARE_STATIC_CALL(might_resched, __cond_resched);
98
might_resched(void)99 static __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 /* CONFIG_PREEMPT_* */
109
110 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
111 extern void __might_resched(const char *file, int line, unsigned int offsets);
112 extern void __might_sleep(const char *file, int line);
113 extern void __cant_sleep(const char *file, int line, int preempt_offset);
114 extern void __cant_migrate(const char *file, int line);
115
116 /**
117 * might_sleep - annotation for functions that can sleep
118 *
119 * this macro will print a stack trace if it is executed in an atomic
120 * context (spinlock, irq-handler, ...). Additional sections where blocking is
121 * not allowed can be annotated with non_block_start() and non_block_end()
122 * pairs.
123 *
124 * This is a useful debugging help to be able to catch problems early and not
125 * be bitten later when the calling function happens to sleep when it is not
126 * supposed to.
127 */
128 # define might_sleep() \
129 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
130 /**
131 * cant_sleep - annotation for functions that cannot sleep
132 *
133 * this macro will print a stack trace if it is executed with preemption enabled
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 * cant_migrate - annotation for functions that cannot migrate
141 *
142 * Will print a stack trace if executed in code which is migratable
143 */
144 # define cant_migrate() \
145 do { \
146 if (IS_ENABLED(CONFIG_SMP)) \
147 __cant_migrate(__FILE__, __LINE__); \
148 } while (0)
149
150 /**
151 * non_block_start - annotate the start of section where sleeping is prohibited
152 *
153 * This is on behalf of the oom reaper, specifically when it is calling the mmu
154 * notifiers. The problem is that if the notifier were to block on, for example,
155 * mutex_lock() and if the process which holds that mutex were to perform a
156 * sleeping memory allocation, the oom reaper is now blocked on completion of
157 * that memory allocation. Other blocking calls like wait_event() pose similar
158 * issues.
159 */
160 # define non_block_start() (current->non_block_count++)
161 /**
162 * non_block_end - annotate the end of section where sleeping is prohibited
163 *
164 * Closes a section opened by non_block_start().
165 */
166 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
167 #else
__might_resched(const char * file,int line,unsigned int offsets)168 static inline void __might_resched(const char *file, int line,
169 unsigned int offsets) { }
__might_sleep(const char * file,int line)170 static 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__)
184 void __might_fault(const char *file, int line);
185 #else
might_fault(void)186 static inline void might_fault(void) { }
187 #endif
188
189 void do_exit(long error_code) __noreturn;
190 void complete_and_exit(struct completion *, long) __noreturn;
191
192 extern int num_to_str(char *buf, int size,
193 unsigned long long num, unsigned int width);
194
195 /* lib/printf utilities */
196
197 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
198 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
199 extern __printf(3, 4)
200 int snprintf(char *buf, size_t size, const char *fmt, ...);
201 extern __printf(3, 0)
202 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
203 extern __printf(3, 4)
204 int scnprintf(char *buf, size_t size, const char *fmt, ...);
205 extern __printf(3, 0)
206 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
207 extern __printf(2, 3) __malloc
208 char *kasprintf(gfp_t gfp, const char *fmt, ...);
209 extern __printf(2, 0) __malloc
210 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
211 extern __printf(2, 0)
212 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
213
214 extern __scanf(2, 3)
215 int sscanf(const char *, const char *, ...);
216 extern __scanf(2, 0)
217 int vsscanf(const char *, const char *, va_list);
218
219 extern int no_hash_pointers_enable(char *str);
220
221 extern int get_option(char **str, int *pint);
222 extern char *get_options(const char *str, int nints, int *ints);
223 extern unsigned long long memparse(const char *ptr, char **retptr);
224 extern bool parse_option_str(const char *str, const char *option);
225 extern char *next_arg(char *args, char **param, char **val);
226
227 extern int core_kernel_text(unsigned long addr);
228 extern int __kernel_text_address(unsigned long addr);
229 extern int kernel_text_address(unsigned long addr);
230 extern int func_ptr_is_kernel_text(void *ptr);
231
232 extern void bust_spinlocks(int yes);
233
234 extern int root_mountflags;
235
236 extern bool early_boot_irqs_disabled;
237
238 /*
239 * Values used for system_state. Ordering of the states must not be changed
240 * as code checks for <, <=, >, >= STATE.
241 */
242 extern 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
253 extern 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
hex_byte_pack(char * buf,u8 byte)257 static 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
264 extern 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
hex_byte_pack_upper(char * buf,u8 byte)268 static 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
275 extern int hex_to_bin(char ch);
276 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
277 extern char *bin2hex(char *dst, const void *src, size_t count);
278
279 bool mac_pton(const char *s, u8 *mac);
280
281 /*
282 * General tracing related utility functions - trace_printk(),
283 * tracing_on/tracing_off and tracing_start()/tracing_stop
284 *
285 * Use tracing_on/tracing_off when you want to quickly turn on or off
286 * tracing. It simply enables or disables the recording of the trace events.
287 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
288 * file, which gives a means for the kernel and userspace to interact.
289 * Place a tracing_off() in the kernel where you want tracing to end.
290 * From user space, examine the trace, and then echo 1 > tracing_on
291 * to continue tracing.
292 *
293 * tracing_stop/tracing_start has slightly more overhead. It is used
294 * by things like suspend to ram where disabling the recording of the
295 * trace is not enough, but tracing must actually stop because things
296 * like calling smp_processor_id() may crash the system.
297 *
298 * Most likely, you want to use tracing_on/tracing_off.
299 */
300
301 enum ftrace_dump_mode {
302 DUMP_NONE,
303 DUMP_ALL,
304 DUMP_ORIG,
305 };
306
307 #ifdef CONFIG_TRACING
308 void tracing_on(void);
309 void tracing_off(void);
310 int tracing_is_on(void);
311 void tracing_snapshot(void);
312 void tracing_snapshot_alloc(void);
313
314 extern void tracing_start(void);
315 extern void tracing_stop(void);
316
317 static inline __printf(1, 2)
____trace_printk_check_format(const char * fmt,...)318 void ____trace_printk_check_format(const char *fmt, ...)
319 {
320 }
321 #define __trace_printk_check_format(fmt, args...) \
322 do { \
323 if (0) \
324 ____trace_printk_check_format(fmt, ##args); \
325 } while (0)
326
327 /**
328 * trace_printk - printf formatting in the ftrace buffer
329 * @fmt: the printf format for printing
330 *
331 * Note: __trace_printk is an internal function for trace_printk() and
332 * the @ip is passed in via the trace_printk() macro.
333 *
334 * This function allows a kernel developer to debug fast path sections
335 * that printk is not appropriate for. By scattering in various
336 * printk like tracing in the code, a developer can quickly see
337 * where problems are occurring.
338 *
339 * This is intended as a debugging tool for the developer only.
340 * Please refrain from leaving trace_printks scattered around in
341 * your code. (Extra memory is used for special buffers that are
342 * allocated when trace_printk() is used.)
343 *
344 * A little optimization trick is done here. If there's only one
345 * argument, there's no need to scan the string for printf formats.
346 * The trace_puts() will suffice. But how can we take advantage of
347 * using trace_puts() when trace_printk() has only one argument?
348 * By stringifying the args and checking the size we can tell
349 * whether or not there are args. __stringify((__VA_ARGS__)) will
350 * turn into "()\0" with a size of 3 when there are no args, anything
351 * else will be bigger. All we need to do is define a string to this,
352 * and then take its size and compare to 3. If it's bigger, use
353 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
354 * let gcc optimize the rest.
355 */
356
357 #define trace_printk(fmt, ...) \
358 do { \
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...) \
367 do { \
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
380 extern __printf(2, 3)
381 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
382
383 extern __printf(2, 3)
384 int __trace_printk(unsigned long ip, const char *fmt, ...);
385
386 /**
387 * trace_puts - write a string into the ftrace buffer
388 * @str: the string to record
389 *
390 * Note: __trace_bputs is an internal function for trace_puts and
391 * the @ip is passed in via the trace_puts macro.
392 *
393 * This is similar to trace_printk() but is made for those really fast
394 * paths that a developer wants the least amount of "Heisenbug" effects,
395 * where the processing of the print format is still too much.
396 *
397 * This function allows a kernel developer to debug fast path sections
398 * that printk is not appropriate for. By scattering in various
399 * printk like tracing in the code, a developer can quickly see
400 * where problems are occurring.
401 *
402 * This is intended as a debugging tool for the developer only.
403 * Please refrain from leaving trace_puts scattered around in
404 * your code. (Extra memory is used for special buffers that are
405 * allocated when trace_puts() is used.)
406 *
407 * Returns: 0 if nothing was written, positive # if string was.
408 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
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 })
421 extern int __trace_bputs(unsigned long ip, const char *str);
422 extern int __trace_puts(unsigned long ip, const char *str, int size);
423
424 extern void trace_dump_stack(int skip);
425
426 /*
427 * The double __builtin_constant_p is because gcc will give us an error
428 * if we try to allocate the static variable to fmt if it is not a
429 * constant. Even with the outer if statement.
430 */
431 #define ftrace_vprintk(fmt, vargs) \
432 do { \
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
443 extern __printf(2, 0) int
444 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
445
446 extern __printf(2, 0) int
447 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
448
449 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
450 #else
tracing_start(void)451 static inline void tracing_start(void) { }
tracing_stop(void)452 static inline void tracing_stop(void) { }
trace_dump_stack(int skip)453 static inline void trace_dump_stack(int skip) { }
454
tracing_on(void)455 static inline void tracing_on(void) { }
tracing_off(void)456 static inline void tracing_off(void) { }
tracing_is_on(void)457 static inline int tracing_is_on(void) { return 0; }
tracing_snapshot(void)458 static inline void tracing_snapshot(void) { }
tracing_snapshot_alloc(void)459 static inline void tracing_snapshot_alloc(void) { }
460
461 static inline __printf(1, 2)
trace_printk(const char * fmt,...)462 int trace_printk(const char *fmt, ...)
463 {
464 return 0;
465 }
466 static __printf(1, 0) inline int
ftrace_vprintk(const char * fmt,va_list ap)467 ftrace_vprintk(const char *fmt, va_list ap)
468 {
469 return 0;
470 }
ftrace_dump(enum ftrace_dump_mode oops_dump_mode)471 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
472 #endif /* CONFIG_TRACING */
473
474 /* This counts to 12. Any more, it will return 13th argument. */
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 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
482 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
483 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
484 #endif
485
486 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
487 #define VERIFY_OCTAL_PERMISSIONS(perms) \
488 (BUILD_BUG_ON_ZERO((perms) < 0) + \
489 BUILD_BUG_ON_ZERO((perms) > 0777) + \
490 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
491 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
492 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
493 /* USER_WRITABLE >= GROUP_WRITABLE */ \
494 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
495 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
496 BUILD_BUG_ON_ZERO((perms) & 2) + \
497 (perms))
498 #endif
499