1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_KERNEL_H
3 #define _LINUX_KERNEL_H
4
5
6 #include <stdarg.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/log2.h>
14 #include <linux/minmax.h>
15 #include <linux/typecheck.h>
16 #include <linux/printk.h>
17 #include <linux/build_bug.h>
18 #include <asm/byteorder.h>
19 #include <asm/div64.h>
20 #include <uapi/linux/kernel.h>
21
22 #define STACK_MAGIC 0xdeadbeef
23
24 /**
25 * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value
26 * @x: value to repeat
27 *
28 * NOTE: @x is not checked for > 0xff; larger values produce odd results.
29 */
30 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
31
32 /* @a is a power of 2 value */
33 #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
34 #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a))
35 #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
36 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
37 #define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a)))
38 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
39
40 /* generic data direction definitions */
41 #define READ 0
42 #define WRITE 1
43
44 /**
45 * ARRAY_SIZE - get the number of elements in array @arr
46 * @arr: array to be sized
47 */
48 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
49
50 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
51
52 #define u64_to_user_ptr(x) ( \
53 { \
54 typecheck(u64, (x)); \
55 (void __user *)(uintptr_t)(x); \
56 } \
57 )
58
59 /*
60 * This looks more complex than it should be. But we need to
61 * get the type for the ~ right in round_down (it needs to be
62 * as wide as the result!), and we want to evaluate the macro
63 * arguments just once each.
64 */
65 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
66 /**
67 * round_up - round up to next specified power of 2
68 * @x: the value to round
69 * @y: multiple to round up to (must be a power of 2)
70 *
71 * Rounds @x up to next multiple of @y (which must be a power of 2).
72 * To perform arbitrary rounding up, use roundup() below.
73 */
74 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
75 /**
76 * round_down - round down to next specified power of 2
77 * @x: the value to round
78 * @y: multiple to round down to (must be a power of 2)
79 *
80 * Rounds @x down to next multiple of @y (which must be a power of 2).
81 * To perform arbitrary rounding down, use rounddown() below.
82 */
83 #define round_down(x, y) ((x) & ~__round_mask(x, y))
84
85 #define typeof_member(T, m) typeof(((T*)0)->m)
86
87 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
88
89 #define DIV_ROUND_DOWN_ULL(ll, d) \
90 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
91
92 #define DIV_ROUND_UP_ULL(ll, d) \
93 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
94
95 #if BITS_PER_LONG == 32
96 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
97 #else
98 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
99 #endif
100
101 /**
102 * roundup - round up to the next specified multiple
103 * @x: the value to up
104 * @y: multiple to round up to
105 *
106 * Rounds @x up to next multiple of @y. If @y will always be a power
107 * of 2, consider using the faster round_up().
108 */
109 #define roundup(x, y) ( \
110 { \
111 typeof(y) __y = y; \
112 (((x) + (__y - 1)) / __y) * __y; \
113 } \
114 )
115 /**
116 * rounddown - round down to next specified multiple
117 * @x: the value to round
118 * @y: multiple to round down to
119 *
120 * Rounds @x down to next multiple of @y. If @y will always be a power
121 * of 2, consider using the faster round_down().
122 */
123 #define rounddown(x, y) ( \
124 { \
125 typeof(x) __x = (x); \
126 __x - (__x % (y)); \
127 } \
128 )
129
130 /*
131 * Divide positive or negative dividend by positive or negative divisor
132 * and round to closest integer. Result is undefined for negative
133 * divisors if the dividend variable type is unsigned and for negative
134 * dividends if the divisor variable type is unsigned.
135 */
136 #define DIV_ROUND_CLOSEST(x, divisor)( \
137 { \
138 typeof(x) __x = x; \
139 typeof(divisor) __d = divisor; \
140 (((typeof(x))-1) > 0 || \
141 ((typeof(divisor))-1) > 0 || \
142 (((__x) > 0) == ((__d) > 0))) ? \
143 (((__x) + ((__d) / 2)) / (__d)) : \
144 (((__x) - ((__d) / 2)) / (__d)); \
145 } \
146 )
147 /*
148 * Same as above but for u64 dividends. divisor must be a 32-bit
149 * number.
150 */
151 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
152 { \
153 typeof(divisor) __d = divisor; \
154 unsigned long long _tmp = (x) + (__d) / 2; \
155 do_div(_tmp, __d); \
156 _tmp; \
157 } \
158 )
159
160 /*
161 * Multiplies an integer by a fraction, while avoiding unnecessary
162 * overflow or loss of precision.
163 */
164 #define mult_frac(x, numer, denom)( \
165 { \
166 typeof(x) quot = (x) / (denom); \
167 typeof(x) rem = (x) % (denom); \
168 (quot * (numer)) + ((rem * (numer)) / (denom)); \
169 } \
170 )
171
172
173 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
174 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
175
176 #define sector_div(a, b) do_div(a, b)
177
178 /**
179 * upper_32_bits - return bits 32-63 of a number
180 * @n: the number we're accessing
181 *
182 * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
183 * the "right shift count >= width of type" warning when that quantity is
184 * 32-bits.
185 */
186 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
187
188 /**
189 * lower_32_bits - return bits 0-31 of a number
190 * @n: the number we're accessing
191 */
192 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
193
194 struct completion;
195 struct pt_regs;
196 struct user;
197
198 #ifdef CONFIG_PREEMPT_VOLUNTARY
199 extern int _cond_resched(void);
200 # define might_resched() _cond_resched()
201 #else
202 # define might_resched() do { } while (0)
203 #endif
204
205 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
206 extern void ___might_sleep(const char *file, int line, int preempt_offset);
207 extern void __might_sleep(const char *file, int line, int preempt_offset);
208 extern void __cant_sleep(const char *file, int line, int preempt_offset);
209
210 /**
211 * might_sleep - annotation for functions that can sleep
212 *
213 * this macro will print a stack trace if it is executed in an atomic
214 * context (spinlock, irq-handler, ...). Additional sections where blocking is
215 * not allowed can be annotated with non_block_start() and non_block_end()
216 * pairs.
217 *
218 * This is a useful debugging help to be able to catch problems early and not
219 * be bitten later when the calling function happens to sleep when it is not
220 * supposed to.
221 */
222 # define might_sleep() \
223 do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
224 /**
225 * cant_sleep - annotation for functions that cannot sleep
226 *
227 * this macro will print a stack trace if it is executed with preemption enabled
228 */
229 # define cant_sleep() \
230 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
231 # define sched_annotate_sleep() (current->task_state_change = 0)
232 /**
233 * non_block_start - annotate the start of section where sleeping is prohibited
234 *
235 * This is on behalf of the oom reaper, specifically when it is calling the mmu
236 * notifiers. The problem is that if the notifier were to block on, for example,
237 * mutex_lock() and if the process which holds that mutex were to perform a
238 * sleeping memory allocation, the oom reaper is now blocked on completion of
239 * that memory allocation. Other blocking calls like wait_event() pose similar
240 * issues.
241 */
242 # define non_block_start() (current->non_block_count++)
243 /**
244 * non_block_end - annotate the end of section where sleeping is prohibited
245 *
246 * Closes a section opened by non_block_start().
247 */
248 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
249 #else
___might_sleep(const char * file,int line,int preempt_offset)250 static inline void ___might_sleep(const char *file, int line,
251 int preempt_offset) { }
__might_sleep(const char * file,int line,int preempt_offset)252 static inline void __might_sleep(const char *file, int line,
253 int preempt_offset) { }
254 # define might_sleep() do { might_resched(); } while (0)
255 # define cant_sleep() do { } while (0)
256 # define sched_annotate_sleep() do { } while (0)
257 # define non_block_start() do { } while (0)
258 # define non_block_end() do { } while (0)
259 #endif
260
261 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
262
263 #ifndef CONFIG_PREEMPT_RT
264 # define cant_migrate() cant_sleep()
265 #else
266 /* Placeholder for now */
267 # define cant_migrate() do { } while (0)
268 #endif
269
270 /**
271 * abs - return absolute value of an argument
272 * @x: the value. If it is unsigned type, it is converted to signed type first.
273 * char is treated as if it was signed (regardless of whether it really is)
274 * but the macro's return type is preserved as char.
275 *
276 * Return: an absolute value of x.
277 */
278 #define abs(x) __abs_choose_expr(x, long long, \
279 __abs_choose_expr(x, long, \
280 __abs_choose_expr(x, int, \
281 __abs_choose_expr(x, short, \
282 __abs_choose_expr(x, char, \
283 __builtin_choose_expr( \
284 __builtin_types_compatible_p(typeof(x), char), \
285 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
286 ((void)0)))))))
287
288 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
289 __builtin_types_compatible_p(typeof(x), signed type) || \
290 __builtin_types_compatible_p(typeof(x), unsigned type), \
291 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
292
293 /**
294 * reciprocal_scale - "scale" a value into range [0, ep_ro)
295 * @val: value
296 * @ep_ro: right open interval endpoint
297 *
298 * Perform a "reciprocal multiplication" in order to "scale" a value into
299 * range [0, @ep_ro), where the upper interval endpoint is right-open.
300 * This is useful, e.g. for accessing a index of an array containing
301 * @ep_ro elements, for example. Think of it as sort of modulus, only that
302 * the result isn't that of modulo. ;) Note that if initial input is a
303 * small value, then result will return 0.
304 *
305 * Return: a result based on @val in interval [0, @ep_ro).
306 */
reciprocal_scale(u32 val,u32 ep_ro)307 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
308 {
309 return (u32)(((u64) val * ep_ro) >> 32);
310 }
311
312 #if defined(CONFIG_MMU) && \
313 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
314 #define might_fault() __might_fault(__FILE__, __LINE__)
315 void __might_fault(const char *file, int line);
316 #else
might_fault(void)317 static inline void might_fault(void) { }
318 #endif
319
320 extern struct atomic_notifier_head panic_notifier_list;
321 extern long (*panic_blink)(int state);
322 __printf(1, 2)
323 void panic(const char *fmt, ...) __noreturn __cold;
324 void nmi_panic(struct pt_regs *regs, const char *msg);
325 void check_panic_on_warn(const char *origin);
326 extern void oops_enter(void);
327 extern void oops_exit(void);
328 extern bool oops_may_print(void);
329 void do_exit(long error_code) __noreturn;
330 void complete_and_exit(struct completion *, long) __noreturn;
331
332 /* Internal, do not use. */
333 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
334 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
335
336 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
337 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
338
339 /**
340 * kstrtoul - convert a string to an unsigned long
341 * @s: The start of the string. The string must be null-terminated, and may also
342 * include a single newline before its terminating null. The first character
343 * may also be a plus sign, but not a minus sign.
344 * @base: The number base to use. The maximum supported base is 16. If base is
345 * given as 0, then the base of the string is automatically detected with the
346 * conventional semantics - If it begins with 0x the number will be parsed as a
347 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
348 * parsed as an octal number. Otherwise it will be parsed as a decimal.
349 * @res: Where to write the result of the conversion on success.
350 *
351 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
352 * Preferred over simple_strtoul(). Return code must be checked.
353 */
kstrtoul(const char * s,unsigned int base,unsigned long * res)354 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
355 {
356 /*
357 * We want to shortcut function call, but
358 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
359 */
360 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
361 __alignof__(unsigned long) == __alignof__(unsigned long long))
362 return kstrtoull(s, base, (unsigned long long *)res);
363 else
364 return _kstrtoul(s, base, res);
365 }
366
367 /**
368 * kstrtol - convert a string to a long
369 * @s: The start of the string. The string must be null-terminated, and may also
370 * include a single newline before its terminating null. The first character
371 * may also be a plus sign or a minus sign.
372 * @base: The number base to use. The maximum supported base is 16. If base is
373 * given as 0, then the base of the string is automatically detected with the
374 * conventional semantics - If it begins with 0x the number will be parsed as a
375 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
376 * parsed as an octal number. Otherwise it will be parsed as a decimal.
377 * @res: Where to write the result of the conversion on success.
378 *
379 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
380 * Preferred over simple_strtol(). Return code must be checked.
381 */
kstrtol(const char * s,unsigned int base,long * res)382 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
383 {
384 /*
385 * We want to shortcut function call, but
386 * __builtin_types_compatible_p(long, long long) = 0.
387 */
388 if (sizeof(long) == sizeof(long long) &&
389 __alignof__(long) == __alignof__(long long))
390 return kstrtoll(s, base, (long long *)res);
391 else
392 return _kstrtol(s, base, res);
393 }
394
395 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
396 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
397
kstrtou64(const char * s,unsigned int base,u64 * res)398 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
399 {
400 return kstrtoull(s, base, res);
401 }
402
kstrtos64(const char * s,unsigned int base,s64 * res)403 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
404 {
405 return kstrtoll(s, base, res);
406 }
407
kstrtou32(const char * s,unsigned int base,u32 * res)408 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
409 {
410 return kstrtouint(s, base, res);
411 }
412
kstrtos32(const char * s,unsigned int base,s32 * res)413 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
414 {
415 return kstrtoint(s, base, res);
416 }
417
418 int __must_check kstrtou16(const char *s, unsigned int base, u16 *res);
419 int __must_check kstrtos16(const char *s, unsigned int base, s16 *res);
420 int __must_check kstrtou8(const char *s, unsigned int base, u8 *res);
421 int __must_check kstrtos8(const char *s, unsigned int base, s8 *res);
422 int __must_check kstrtobool(const char *s, bool *res);
423
424 int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res);
425 int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res);
426 int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res);
427 int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res);
428 int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res);
429 int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res);
430 int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res);
431 int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res);
432 int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res);
433 int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res);
434 int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res);
435
kstrtou64_from_user(const char __user * s,size_t count,unsigned int base,u64 * res)436 static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res)
437 {
438 return kstrtoull_from_user(s, count, base, res);
439 }
440
kstrtos64_from_user(const char __user * s,size_t count,unsigned int base,s64 * res)441 static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res)
442 {
443 return kstrtoll_from_user(s, count, base, res);
444 }
445
kstrtou32_from_user(const char __user * s,size_t count,unsigned int base,u32 * res)446 static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res)
447 {
448 return kstrtouint_from_user(s, count, base, res);
449 }
450
kstrtos32_from_user(const char __user * s,size_t count,unsigned int base,s32 * res)451 static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res)
452 {
453 return kstrtoint_from_user(s, count, base, res);
454 }
455
456 /*
457 * Use kstrto<foo> instead.
458 *
459 * NOTE: simple_strto<foo> does not check for the range overflow and,
460 * depending on the input, may give interesting results.
461 *
462 * Use these functions if and only if you cannot use kstrto<foo>, because
463 * the conversion ends on the first non-digit character, which may be far
464 * beyond the supported range. It might be useful to parse the strings like
465 * 10x50 or 12:21 without altering original string or temporary buffer in use.
466 * Keep in mind above caveat.
467 */
468
469 extern unsigned long simple_strtoul(const char *,char **,unsigned int);
470 extern long simple_strtol(const char *,char **,unsigned int);
471 extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
472 extern long long simple_strtoll(const char *,char **,unsigned int);
473
474 extern int num_to_str(char *buf, int size,
475 unsigned long long num, unsigned int width);
476
477 /* lib/printf utilities */
478
479 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
480 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
481 extern __printf(3, 4)
482 int snprintf(char *buf, size_t size, const char *fmt, ...);
483 extern __printf(3, 0)
484 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
485 extern __printf(3, 4)
486 int scnprintf(char *buf, size_t size, const char *fmt, ...);
487 extern __printf(3, 0)
488 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
489 extern __printf(2, 3) __malloc
490 char *kasprintf(gfp_t gfp, const char *fmt, ...);
491 extern __printf(2, 0) __malloc
492 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
493 extern __printf(2, 0)
494 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
495
496 extern __scanf(2, 3)
497 int sscanf(const char *, const char *, ...);
498 extern __scanf(2, 0)
499 int vsscanf(const char *, const char *, va_list);
500
501 extern int get_option(char **str, int *pint);
502 extern char *get_options(const char *str, int nints, int *ints);
503 extern unsigned long long memparse(const char *ptr, char **retptr);
504 extern bool parse_option_str(const char *str, const char *option);
505 extern char *next_arg(char *args, char **param, char **val);
506
507 extern int core_kernel_text(unsigned long addr);
508 extern int init_kernel_text(unsigned long addr);
509 extern int core_kernel_data(unsigned long addr);
510 extern int __kernel_text_address(unsigned long addr);
511 extern int kernel_text_address(unsigned long addr);
512 extern int func_ptr_is_kernel_text(void *ptr);
513
514 u64 int_pow(u64 base, unsigned int exp);
515 unsigned long int_sqrt(unsigned long);
516
517 #if BITS_PER_LONG < 64
518 u32 int_sqrt64(u64 x);
519 #else
int_sqrt64(u64 x)520 static inline u32 int_sqrt64(u64 x)
521 {
522 return (u32)int_sqrt(x);
523 }
524 #endif
525
526 extern void bust_spinlocks(int yes);
527 extern int panic_timeout;
528 extern unsigned long panic_print;
529 extern int panic_on_oops;
530 extern int panic_on_unrecovered_nmi;
531 extern int panic_on_io_nmi;
532 extern int panic_on_warn;
533 extern unsigned long panic_on_taint;
534 extern bool panic_on_taint_nousertaint;
535 extern int sysctl_panic_on_rcu_stall;
536 extern int sysctl_panic_on_stackoverflow;
537
538 extern bool crash_kexec_post_notifiers;
539
540 /*
541 * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It
542 * holds a CPU number which is executing panic() currently. A value of
543 * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec().
544 */
545 extern atomic_t panic_cpu;
546 #define PANIC_CPU_INVALID -1
547
548 /*
549 * Only to be used by arch init code. If the user over-wrote the default
550 * CONFIG_PANIC_TIMEOUT, honor it.
551 */
set_arch_panic_timeout(int timeout,int arch_default_timeout)552 static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout)
553 {
554 if (panic_timeout == arch_default_timeout)
555 panic_timeout = timeout;
556 }
557 extern const char *print_tainted(void);
558 enum lockdep_ok {
559 LOCKDEP_STILL_OK,
560 LOCKDEP_NOW_UNRELIABLE
561 };
562 extern void add_taint(unsigned flag, enum lockdep_ok);
563 extern int test_taint(unsigned flag);
564 extern unsigned long get_taint(void);
565 extern int root_mountflags;
566
567 extern bool early_boot_irqs_disabled;
568
569 /*
570 * Values used for system_state. Ordering of the states must not be changed
571 * as code checks for <, <=, >, >= STATE.
572 */
573 extern enum system_states {
574 SYSTEM_BOOTING,
575 SYSTEM_SCHEDULING,
576 SYSTEM_RUNNING,
577 SYSTEM_HALT,
578 SYSTEM_POWER_OFF,
579 SYSTEM_RESTART,
580 SYSTEM_SUSPEND,
581 } system_state;
582
583 /* This cannot be an enum because some may be used in assembly source. */
584 #define TAINT_PROPRIETARY_MODULE 0
585 #define TAINT_FORCED_MODULE 1
586 #define TAINT_CPU_OUT_OF_SPEC 2
587 #define TAINT_FORCED_RMMOD 3
588 #define TAINT_MACHINE_CHECK 4
589 #define TAINT_BAD_PAGE 5
590 #define TAINT_USER 6
591 #define TAINT_DIE 7
592 #define TAINT_OVERRIDDEN_ACPI_TABLE 8
593 #define TAINT_WARN 9
594 #define TAINT_CRAP 10
595 #define TAINT_FIRMWARE_WORKAROUND 11
596 #define TAINT_OOT_MODULE 12
597 #define TAINT_UNSIGNED_MODULE 13
598 #define TAINT_SOFTLOCKUP 14
599 #define TAINT_LIVEPATCH 15
600 #define TAINT_AUX 16
601 #define TAINT_RANDSTRUCT 17
602 #define TAINT_FLAGS_COUNT 18
603 #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1)
604
605 struct taint_flag {
606 char c_true; /* character printed when tainted */
607 char c_false; /* character printed when not tainted */
608 bool module; /* also show as a per-module taint flag */
609 };
610
611 extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT];
612
613 extern const char hex_asc[];
614 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
615 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
616
hex_byte_pack(char * buf,u8 byte)617 static inline char *hex_byte_pack(char *buf, u8 byte)
618 {
619 *buf++ = hex_asc_hi(byte);
620 *buf++ = hex_asc_lo(byte);
621 return buf;
622 }
623
624 extern const char hex_asc_upper[];
625 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
626 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
627
hex_byte_pack_upper(char * buf,u8 byte)628 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
629 {
630 *buf++ = hex_asc_upper_hi(byte);
631 *buf++ = hex_asc_upper_lo(byte);
632 return buf;
633 }
634
635 extern int hex_to_bin(unsigned char ch);
636 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
637 extern char *bin2hex(char *dst, const void *src, size_t count);
638
639 bool mac_pton(const char *s, u8 *mac);
640
641 /*
642 * General tracing related utility functions - trace_printk(),
643 * tracing_on/tracing_off and tracing_start()/tracing_stop
644 *
645 * Use tracing_on/tracing_off when you want to quickly turn on or off
646 * tracing. It simply enables or disables the recording of the trace events.
647 * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
648 * file, which gives a means for the kernel and userspace to interact.
649 * Place a tracing_off() in the kernel where you want tracing to end.
650 * From user space, examine the trace, and then echo 1 > tracing_on
651 * to continue tracing.
652 *
653 * tracing_stop/tracing_start has slightly more overhead. It is used
654 * by things like suspend to ram where disabling the recording of the
655 * trace is not enough, but tracing must actually stop because things
656 * like calling smp_processor_id() may crash the system.
657 *
658 * Most likely, you want to use tracing_on/tracing_off.
659 */
660
661 enum ftrace_dump_mode {
662 DUMP_NONE,
663 DUMP_ALL,
664 DUMP_ORIG,
665 };
666
667 #ifdef CONFIG_TRACING
668 void tracing_on(void);
669 void tracing_off(void);
670 int tracing_is_on(void);
671 void tracing_snapshot(void);
672 void tracing_snapshot_alloc(void);
673
674 extern void tracing_start(void);
675 extern void tracing_stop(void);
676
677 static inline __printf(1, 2)
____trace_printk_check_format(const char * fmt,...)678 void ____trace_printk_check_format(const char *fmt, ...)
679 {
680 }
681 #define __trace_printk_check_format(fmt, args...) \
682 do { \
683 if (0) \
684 ____trace_printk_check_format(fmt, ##args); \
685 } while (0)
686
687 /**
688 * trace_printk - printf formatting in the ftrace buffer
689 * @fmt: the printf format for printing
690 *
691 * Note: __trace_printk is an internal function for trace_printk() and
692 * the @ip is passed in via the trace_printk() macro.
693 *
694 * This function allows a kernel developer to debug fast path sections
695 * that printk is not appropriate for. By scattering in various
696 * printk like tracing in the code, a developer can quickly see
697 * where problems are occurring.
698 *
699 * This is intended as a debugging tool for the developer only.
700 * Please refrain from leaving trace_printks scattered around in
701 * your code. (Extra memory is used for special buffers that are
702 * allocated when trace_printk() is used.)
703 *
704 * A little optimization trick is done here. If there's only one
705 * argument, there's no need to scan the string for printf formats.
706 * The trace_puts() will suffice. But how can we take advantage of
707 * using trace_puts() when trace_printk() has only one argument?
708 * By stringifying the args and checking the size we can tell
709 * whether or not there are args. __stringify((__VA_ARGS__)) will
710 * turn into "()\0" with a size of 3 when there are no args, anything
711 * else will be bigger. All we need to do is define a string to this,
712 * and then take its size and compare to 3. If it's bigger, use
713 * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
714 * let gcc optimize the rest.
715 */
716
717 #define trace_printk(fmt, ...) \
718 do { \
719 char _______STR[] = __stringify((__VA_ARGS__)); \
720 if (sizeof(_______STR) > 3) \
721 do_trace_printk(fmt, ##__VA_ARGS__); \
722 else \
723 trace_puts(fmt); \
724 } while (0)
725
726 #define do_trace_printk(fmt, args...) \
727 do { \
728 static const char *trace_printk_fmt __used \
729 __section("__trace_printk_fmt") = \
730 __builtin_constant_p(fmt) ? fmt : NULL; \
731 \
732 __trace_printk_check_format(fmt, ##args); \
733 \
734 if (__builtin_constant_p(fmt)) \
735 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
736 else \
737 __trace_printk(_THIS_IP_, fmt, ##args); \
738 } while (0)
739
740 extern __printf(2, 3)
741 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
742
743 extern __printf(2, 3)
744 int __trace_printk(unsigned long ip, const char *fmt, ...);
745
746 /**
747 * trace_puts - write a string into the ftrace buffer
748 * @str: the string to record
749 *
750 * Note: __trace_bputs is an internal function for trace_puts and
751 * the @ip is passed in via the trace_puts macro.
752 *
753 * This is similar to trace_printk() but is made for those really fast
754 * paths that a developer wants the least amount of "Heisenbug" effects,
755 * where the processing of the print format is still too much.
756 *
757 * This function allows a kernel developer to debug fast path sections
758 * that printk is not appropriate for. By scattering in various
759 * printk like tracing in the code, a developer can quickly see
760 * where problems are occurring.
761 *
762 * This is intended as a debugging tool for the developer only.
763 * Please refrain from leaving trace_puts scattered around in
764 * your code. (Extra memory is used for special buffers that are
765 * allocated when trace_puts() is used.)
766 *
767 * Returns: 0 if nothing was written, positive # if string was.
768 * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
769 */
770
771 #define trace_puts(str) ({ \
772 static const char *trace_printk_fmt __used \
773 __section("__trace_printk_fmt") = \
774 __builtin_constant_p(str) ? str : NULL; \
775 \
776 if (__builtin_constant_p(str)) \
777 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
778 else \
779 __trace_puts(_THIS_IP_, str, strlen(str)); \
780 })
781 extern int __trace_bputs(unsigned long ip, const char *str);
782 extern int __trace_puts(unsigned long ip, const char *str, int size);
783
784 extern void trace_dump_stack(int skip);
785
786 /*
787 * The double __builtin_constant_p is because gcc will give us an error
788 * if we try to allocate the static variable to fmt if it is not a
789 * constant. Even with the outer if statement.
790 */
791 #define ftrace_vprintk(fmt, vargs) \
792 do { \
793 if (__builtin_constant_p(fmt)) { \
794 static const char *trace_printk_fmt __used \
795 __section("__trace_printk_fmt") = \
796 __builtin_constant_p(fmt) ? fmt : NULL; \
797 \
798 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
799 } else \
800 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
801 } while (0)
802
803 extern __printf(2, 0) int
804 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
805
806 extern __printf(2, 0) int
807 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
808
809 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
810 #else
tracing_start(void)811 static inline void tracing_start(void) { }
tracing_stop(void)812 static inline void tracing_stop(void) { }
trace_dump_stack(int skip)813 static inline void trace_dump_stack(int skip) { }
814
tracing_on(void)815 static inline void tracing_on(void) { }
tracing_off(void)816 static inline void tracing_off(void) { }
tracing_is_on(void)817 static inline int tracing_is_on(void) { return 0; }
tracing_snapshot(void)818 static inline void tracing_snapshot(void) { }
tracing_snapshot_alloc(void)819 static inline void tracing_snapshot_alloc(void) { }
820
821 static inline __printf(1, 2)
trace_printk(const char * fmt,...)822 int trace_printk(const char *fmt, ...)
823 {
824 return 0;
825 }
826 static __printf(1, 0) inline int
ftrace_vprintk(const char * fmt,va_list ap)827 ftrace_vprintk(const char *fmt, va_list ap)
828 {
829 return 0;
830 }
ftrace_dump(enum ftrace_dump_mode oops_dump_mode)831 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
832 #endif /* CONFIG_TRACING */
833
834 /* This counts to 12. Any more, it will return 13th argument. */
835 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
836 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
837
838 #define __CONCAT(a, b) a ## b
839 #define CONCATENATE(a, b) __CONCAT(a, b)
840
841 /**
842 * container_of - cast a member of a structure out to the containing structure
843 * @ptr: the pointer to the member.
844 * @type: the type of the container struct this is embedded in.
845 * @member: the name of the member within the struct.
846 *
847 */
848 #define container_of(ptr, type, member) ({ \
849 void *__mptr = (void *)(ptr); \
850 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
851 !__same_type(*(ptr), void), \
852 "pointer type mismatch in container_of()"); \
853 ((type *)(__mptr - offsetof(type, member))); })
854
855 /**
856 * container_of_safe - cast a member of a structure out to the containing structure
857 * @ptr: the pointer to the member.
858 * @type: the type of the container struct this is embedded in.
859 * @member: the name of the member within the struct.
860 *
861 * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged.
862 */
863 #define container_of_safe(ptr, type, member) ({ \
864 void *__mptr = (void *)(ptr); \
865 BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \
866 !__same_type(*(ptr), void), \
867 "pointer type mismatch in container_of()"); \
868 IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
869 ((type *)(__mptr - offsetof(type, member))); })
870
871 /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
872 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
873 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
874 #endif
875
876 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
877 #define VERIFY_OCTAL_PERMISSIONS(perms) \
878 (BUILD_BUG_ON_ZERO((perms) < 0) + \
879 BUILD_BUG_ON_ZERO((perms) > 0777) + \
880 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
881 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
882 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
883 /* USER_WRITABLE >= GROUP_WRITABLE */ \
884 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
885 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
886 BUILD_BUG_ON_ZERO((perms) & 2) + \
887 (perms))
888 #endif
889