Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
1da177e4 LT |
2 | #ifndef _LINUX_KERNEL_H |
3 | #define _LINUX_KERNEL_H | |
4 | ||
1da177e4 | 5 | #include <stdarg.h> |
54d50897 | 6 | #include <linux/limits.h> |
1da177e4 LT |
7 | #include <linux/linkage.h> |
8 | #include <linux/stddef.h> | |
9 | #include <linux/types.h> | |
10 | #include <linux/compiler.h> | |
11 | #include <linux/bitops.h> | |
f0d1b0b3 | 12 | #include <linux/log2.h> |
aa6159ab | 13 | #include <linux/math.h> |
b296a6d5 | 14 | #include <linux/minmax.h> |
e0deaff4 | 15 | #include <linux/typecheck.h> |
968ab183 | 16 | #include <linux/printk.h> |
c7acec71 | 17 | #include <linux/build_bug.h> |
b965f1dd | 18 | #include <linux/static_call_types.h> |
1da177e4 | 19 | #include <asm/byteorder.h> |
aa6159ab | 20 | |
607ca46e | 21 | #include <uapi/linux/kernel.h> |
1da177e4 | 22 | |
1da177e4 LT |
23 | #define STACK_MAGIC 0xdeadbeef |
24 | ||
e8c97af0 RD |
25 | /** |
26 | * REPEAT_BYTE - repeat the value @x multiple times as an unsigned long value | |
27 | * @x: value to repeat | |
28 | * | |
29 | * NOTE: @x is not checked for > 0xff; larger values produce odd results. | |
30 | */ | |
44696908 DM |
31 | #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x)) |
32 | ||
3ca45a46 | 33 | /* @a is a power of 2 value */ |
a79ff731 | 34 | #define ALIGN(x, a) __ALIGN_KERNEL((x), (a)) |
ed067d4a | 35 | #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a) - 1), (a)) |
9f93ff5b | 36 | #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask)) |
a83308e6 | 37 | #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a))) |
229f5879 | 38 | #define PTR_ALIGN_DOWN(p, a) ((typeof(p))ALIGN_DOWN((unsigned long)(p), (a))) |
f10db627 | 39 | #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0) |
2ea58144 | 40 | |
d3849953 CH |
41 | /* generic data direction definitions */ |
42 | #define READ 0 | |
43 | #define WRITE 1 | |
44 | ||
e8c97af0 RD |
45 | /** |
46 | * ARRAY_SIZE - get the number of elements in array @arr | |
47 | * @arr: array to be sized | |
48 | */ | |
c5e631cf RR |
49 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) |
50 | ||
3ed605bc GP |
51 | #define u64_to_user_ptr(x) ( \ |
52 | { \ | |
a0fe2c64 JH |
53 | typecheck(u64, (x)); \ |
54 | (void __user *)(uintptr_t)(x); \ | |
3ed605bc GP |
55 | } \ |
56 | ) | |
57 | ||
ce251e0e AD |
58 | #define typeof_member(T, m) typeof(((T*)0)->m) |
59 | ||
ca31e146 EGM |
60 | #define _RET_IP_ (unsigned long)__builtin_return_address(0) |
61 | #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) | |
62 | ||
218e180e AM |
63 | /** |
64 | * upper_32_bits - return bits 32-63 of a number | |
65 | * @n: the number we're accessing | |
66 | * | |
67 | * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress | |
68 | * the "right shift count >= width of type" warning when that quantity is | |
69 | * 32-bits. | |
70 | */ | |
71 | #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16)) | |
72 | ||
204b885e JR |
73 | /** |
74 | * lower_32_bits - return bits 0-31 of a number | |
75 | * @n: the number we're accessing | |
76 | */ | |
ef91bb19 | 77 | #define lower_32_bits(n) ((u32)((n) & 0xffffffff)) |
204b885e | 78 | |
1da177e4 | 79 | struct completion; |
df2e71fb | 80 | struct pt_regs; |
81 | struct user; | |
1da177e4 | 82 | |
070cb065 | 83 | #ifdef CONFIG_PREEMPT_VOLUNTARY |
b965f1dd PZI |
84 | |
85 | extern int __cond_resched(void); | |
86 | # define might_resched() __cond_resched() | |
87 | ||
88 | #elif defined(CONFIG_PREEMPT_DYNAMIC) | |
89 | ||
90 | extern int __cond_resched(void); | |
91 | ||
92 | DECLARE_STATIC_CALL(might_resched, __cond_resched); | |
93 | ||
94 | static __always_inline void might_resched(void) | |
95 | { | |
ef72661e | 96 | static_call_mod(might_resched)(); |
b965f1dd PZI |
97 | } |
98 | ||
070cb065 | 99 | #else |
b965f1dd | 100 | |
070cb065 | 101 | # define might_resched() do { } while (0) |
b965f1dd PZI |
102 | |
103 | #endif /* CONFIG_PREEMPT_* */ | |
070cb065 | 104 | |
d902db1e | 105 | #ifdef CONFIG_DEBUG_ATOMIC_SLEEP |
568f1967 PZ |
106 | extern void ___might_sleep(const char *file, int line, int preempt_offset); |
107 | extern void __might_sleep(const char *file, int line, int preempt_offset); | |
108 | extern void __cant_sleep(const char *file, int line, int preempt_offset); | |
74d862b6 | 109 | extern void __cant_migrate(const char *file, int line); |
568f1967 | 110 | |
1da177e4 LT |
111 | /** |
112 | * might_sleep - annotation for functions that can sleep | |
113 | * | |
114 | * this macro will print a stack trace if it is executed in an atomic | |
312364f3 DV |
115 | * context (spinlock, irq-handler, ...). Additional sections where blocking is |
116 | * not allowed can be annotated with non_block_start() and non_block_end() | |
117 | * pairs. | |
1da177e4 LT |
118 | * |
119 | * This is a useful debugging help to be able to catch problems early and not | |
e20ec991 | 120 | * be bitten later when the calling function happens to sleep when it is not |
1da177e4 LT |
121 | * supposed to. |
122 | */ | |
f8cbd99b | 123 | # define might_sleep() \ |
e4aafea2 | 124 | do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0) |
568f1967 PZ |
125 | /** |
126 | * cant_sleep - annotation for functions that cannot sleep | |
127 | * | |
128 | * this macro will print a stack trace if it is executed with preemption enabled | |
129 | */ | |
130 | # define cant_sleep() \ | |
131 | do { __cant_sleep(__FILE__, __LINE__, 0); } while (0) | |
00845eb9 | 132 | # define sched_annotate_sleep() (current->task_state_change = 0) |
74d862b6 TG |
133 | |
134 | /** | |
135 | * cant_migrate - annotation for functions that cannot migrate | |
136 | * | |
137 | * Will print a stack trace if executed in code which is migratable | |
138 | */ | |
139 | # define cant_migrate() \ | |
140 | do { \ | |
141 | if (IS_ENABLED(CONFIG_SMP)) \ | |
142 | __cant_migrate(__FILE__, __LINE__); \ | |
143 | } while (0) | |
144 | ||
312364f3 DV |
145 | /** |
146 | * non_block_start - annotate the start of section where sleeping is prohibited | |
147 | * | |
148 | * This is on behalf of the oom reaper, specifically when it is calling the mmu | |
149 | * notifiers. The problem is that if the notifier were to block on, for example, | |
150 | * mutex_lock() and if the process which holds that mutex were to perform a | |
151 | * sleeping memory allocation, the oom reaper is now blocked on completion of | |
152 | * that memory allocation. Other blocking calls like wait_event() pose similar | |
153 | * issues. | |
154 | */ | |
155 | # define non_block_start() (current->non_block_count++) | |
156 | /** | |
157 | * non_block_end - annotate the end of section where sleeping is prohibited | |
158 | * | |
159 | * Closes a section opened by non_block_start(). | |
160 | */ | |
161 | # define non_block_end() WARN_ON(current->non_block_count-- == 0) | |
1da177e4 | 162 | #else |
3427445a PZ |
163 | static inline void ___might_sleep(const char *file, int line, |
164 | int preempt_offset) { } | |
d894837f SK |
165 | static inline void __might_sleep(const char *file, int line, |
166 | int preempt_offset) { } | |
f8cbd99b | 167 | # define might_sleep() do { might_resched(); } while (0) |
568f1967 | 168 | # define cant_sleep() do { } while (0) |
74d862b6 | 169 | # define cant_migrate() do { } while (0) |
1029a2b5 | 170 | # define sched_annotate_sleep() do { } while (0) |
312364f3 DV |
171 | # define non_block_start() do { } while (0) |
172 | # define non_block_end() do { } while (0) | |
1da177e4 LT |
173 | #endif |
174 | ||
368a5fa1 | 175 | #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0) |
f8cbd99b | 176 | |
386e7906 AL |
177 | #if defined(CONFIG_MMU) && \ |
178 | (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP)) | |
9ec23531 DH |
179 | #define might_fault() __might_fault(__FILE__, __LINE__) |
180 | void __might_fault(const char *file, int line); | |
3ee1afa3 | 181 | #else |
662bbcb2 | 182 | static inline void might_fault(void) { } |
3ee1afa3 NP |
183 | #endif |
184 | ||
e041c683 | 185 | extern struct atomic_notifier_head panic_notifier_list; |
c7ff0d9c | 186 | extern long (*panic_blink)(int state); |
9402c95f | 187 | __printf(1, 2) |
9af6528e | 188 | void panic(const char *fmt, ...) __noreturn __cold; |
ebc41f20 | 189 | void nmi_panic(struct pt_regs *regs, const char *msg); |
dd287796 AM |
190 | extern void oops_enter(void); |
191 | extern void oops_exit(void); | |
79076e12 | 192 | extern bool oops_may_print(void); |
9af6528e PZ |
193 | void do_exit(long error_code) __noreturn; |
194 | void complete_and_exit(struct completion *, long) __noreturn; | |
33ee3b2e AD |
195 | |
196 | /* Internal, do not use. */ | |
197 | int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res); | |
198 | int __must_check _kstrtol(const char *s, unsigned int base, long *res); | |
199 | ||
200 | int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res); | |
201 | int __must_check kstrtoll(const char *s, unsigned int base, long long *res); | |
4c925d60 EZ |
202 | |
203 | /** | |
204 | * kstrtoul - convert a string to an unsigned long | |
205 | * @s: The start of the string. The string must be null-terminated, and may also | |
206 | * include a single newline before its terminating null. The first character | |
207 | * may also be a plus sign, but not a minus sign. | |
208 | * @base: The number base to use. The maximum supported base is 16. If base is | |
209 | * given as 0, then the base of the string is automatically detected with the | |
210 | * conventional semantics - If it begins with 0x the number will be parsed as a | |
211 | * hexadecimal (case insensitive), if it otherwise begins with 0, it will be | |
212 | * parsed as an octal number. Otherwise it will be parsed as a decimal. | |
213 | * @res: Where to write the result of the conversion on success. | |
214 | * | |
215 | * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. | |
ef0f2685 | 216 | * Preferred over simple_strtoul(). Return code must be checked. |
4c925d60 | 217 | */ |
33ee3b2e AD |
218 | static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res) |
219 | { | |
220 | /* | |
221 | * We want to shortcut function call, but | |
222 | * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0. | |
223 | */ | |
224 | if (sizeof(unsigned long) == sizeof(unsigned long long) && | |
225 | __alignof__(unsigned long) == __alignof__(unsigned long long)) | |
226 | return kstrtoull(s, base, (unsigned long long *)res); | |
227 | else | |
228 | return _kstrtoul(s, base, res); | |
229 | } | |
230 | ||
4c925d60 EZ |
231 | /** |
232 | * kstrtol - convert a string to a long | |
233 | * @s: The start of the string. The string must be null-terminated, and may also | |
234 | * include a single newline before its terminating null. The first character | |
235 | * may also be a plus sign or a minus sign. | |
236 | * @base: The number base to use. The maximum supported base is 16. If base is | |
237 | * given as 0, then the base of the string is automatically detected with the | |
238 | * conventional semantics - If it begins with 0x the number will be parsed as a | |
239 | * hexadecimal (case insensitive), if it otherwise begins with 0, it will be | |
240 | * parsed as an octal number. Otherwise it will be parsed as a decimal. | |
241 | * @res: Where to write the result of the conversion on success. | |
242 | * | |
243 | * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error. | |
ef0f2685 | 244 | * Preferred over simple_strtol(). Return code must be checked. |
4c925d60 | 245 | */ |
33ee3b2e AD |
246 | static inline int __must_check kstrtol(const char *s, unsigned int base, long *res) |
247 | { | |
248 | /* | |
249 | * We want to shortcut function call, but | |
250 | * __builtin_types_compatible_p(long, long long) = 0. | |
251 | */ | |
252 | if (sizeof(long) == sizeof(long long) && | |
253 | __alignof__(long) == __alignof__(long long)) | |
254 | return kstrtoll(s, base, (long long *)res); | |
255 | else | |
256 | return _kstrtol(s, base, res); | |
257 | } | |
258 | ||
259 | int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res); | |
260 | int __must_check kstrtoint(const char *s, unsigned int base, int *res); | |
261 | ||
262 | static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res) | |
263 | { | |
264 | return kstrtoull(s, base, res); | |
265 | } | |
266 | ||
267 | static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res) | |
268 | { | |
269 | return kstrtoll(s, base, res); | |
270 | } | |
271 | ||
272 | static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res) | |
273 | { | |
274 | return kstrtouint(s, base, res); | |
275 | } | |
276 | ||
277 | static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res) | |
278 | { | |
279 | return kstrtoint(s, base, res); | |
280 | } | |
281 | ||
282 | int __must_check kstrtou16(const char *s, unsigned int base, u16 *res); | |
283 | int __must_check kstrtos16(const char *s, unsigned int base, s16 *res); | |
284 | int __must_check kstrtou8(const char *s, unsigned int base, u8 *res); | |
285 | int __must_check kstrtos8(const char *s, unsigned int base, s8 *res); | |
ef951599 | 286 | int __must_check kstrtobool(const char *s, bool *res); |
33ee3b2e | 287 | |
c196e32a AD |
288 | int __must_check kstrtoull_from_user(const char __user *s, size_t count, unsigned int base, unsigned long long *res); |
289 | int __must_check kstrtoll_from_user(const char __user *s, size_t count, unsigned int base, long long *res); | |
290 | int __must_check kstrtoul_from_user(const char __user *s, size_t count, unsigned int base, unsigned long *res); | |
291 | int __must_check kstrtol_from_user(const char __user *s, size_t count, unsigned int base, long *res); | |
292 | int __must_check kstrtouint_from_user(const char __user *s, size_t count, unsigned int base, unsigned int *res); | |
293 | int __must_check kstrtoint_from_user(const char __user *s, size_t count, unsigned int base, int *res); | |
294 | int __must_check kstrtou16_from_user(const char __user *s, size_t count, unsigned int base, u16 *res); | |
295 | int __must_check kstrtos16_from_user(const char __user *s, size_t count, unsigned int base, s16 *res); | |
296 | int __must_check kstrtou8_from_user(const char __user *s, size_t count, unsigned int base, u8 *res); | |
297 | int __must_check kstrtos8_from_user(const char __user *s, size_t count, unsigned int base, s8 *res); | |
ef951599 | 298 | int __must_check kstrtobool_from_user(const char __user *s, size_t count, bool *res); |
c196e32a AD |
299 | |
300 | static inline int __must_check kstrtou64_from_user(const char __user *s, size_t count, unsigned int base, u64 *res) | |
301 | { | |
302 | return kstrtoull_from_user(s, count, base, res); | |
303 | } | |
304 | ||
305 | static inline int __must_check kstrtos64_from_user(const char __user *s, size_t count, unsigned int base, s64 *res) | |
306 | { | |
307 | return kstrtoll_from_user(s, count, base, res); | |
308 | } | |
309 | ||
310 | static inline int __must_check kstrtou32_from_user(const char __user *s, size_t count, unsigned int base, u32 *res) | |
311 | { | |
312 | return kstrtouint_from_user(s, count, base, res); | |
313 | } | |
314 | ||
315 | static inline int __must_check kstrtos32_from_user(const char __user *s, size_t count, unsigned int base, s32 *res) | |
316 | { | |
317 | return kstrtoint_from_user(s, count, base, res); | |
318 | } | |
319 | ||
885e68e8 AS |
320 | /* |
321 | * Use kstrto<foo> instead. | |
322 | * | |
323 | * NOTE: simple_strto<foo> does not check for the range overflow and, | |
324 | * depending on the input, may give interesting results. | |
325 | * | |
326 | * Use these functions if and only if you cannot use kstrto<foo>, because | |
327 | * the conversion ends on the first non-digit character, which may be far | |
328 | * beyond the supported range. It might be useful to parse the strings like | |
329 | * 10x50 or 12:21 without altering original string or temporary buffer in use. | |
330 | * Keep in mind above caveat. | |
331 | */ | |
67d0a075 | 332 | |
1da177e4 LT |
333 | extern unsigned long simple_strtoul(const char *,char **,unsigned int); |
334 | extern long simple_strtol(const char *,char **,unsigned int); | |
335 | extern unsigned long long simple_strtoull(const char *,char **,unsigned int); | |
336 | extern long long simple_strtoll(const char *,char **,unsigned int); | |
33ee3b2e | 337 | |
d1be35cb AV |
338 | extern int num_to_str(char *buf, int size, |
339 | unsigned long long num, unsigned int width); | |
1ac101a5 | 340 | |
67d0a075 JP |
341 | /* lib/printf utilities */ |
342 | ||
b9075fa9 JP |
343 | extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...); |
344 | extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list); | |
345 | extern __printf(3, 4) | |
346 | int snprintf(char *buf, size_t size, const char *fmt, ...); | |
347 | extern __printf(3, 0) | |
348 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args); | |
349 | extern __printf(3, 4) | |
350 | int scnprintf(char *buf, size_t size, const char *fmt, ...); | |
351 | extern __printf(3, 0) | |
352 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); | |
48a27055 | 353 | extern __printf(2, 3) __malloc |
b9075fa9 | 354 | char *kasprintf(gfp_t gfp, const char *fmt, ...); |
48a27055 | 355 | extern __printf(2, 0) __malloc |
8db14860 | 356 | char *kvasprintf(gfp_t gfp, const char *fmt, va_list args); |
0a9df786 RV |
357 | extern __printf(2, 0) |
358 | const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args); | |
1da177e4 | 359 | |
6061d949 JP |
360 | extern __scanf(2, 3) |
361 | int sscanf(const char *, const char *, ...); | |
362 | extern __scanf(2, 0) | |
363 | int vsscanf(const char *, const char *, va_list); | |
1da177e4 LT |
364 | |
365 | extern int get_option(char **str, int *pint); | |
366 | extern char *get_options(const char *str, int nints, int *ints); | |
d974ae37 | 367 | extern unsigned long long memparse(const char *ptr, char **retptr); |
6ccc72b8 | 368 | extern bool parse_option_str(const char *str, const char *option); |
f51b17c8 | 369 | extern char *next_arg(char *args, char **param, char **val); |
1da177e4 | 370 | |
5e376613 | 371 | extern int core_kernel_text(unsigned long addr); |
9fbcc57a | 372 | extern int init_kernel_text(unsigned long addr); |
cdbe61bf | 373 | extern int core_kernel_data(unsigned long addr); |
1da177e4 LT |
374 | extern int __kernel_text_address(unsigned long addr); |
375 | extern int kernel_text_address(unsigned long addr); | |
ab7476cf | 376 | extern int func_ptr_is_kernel_text(void *ptr); |
ab7476cf | 377 | |
60c958d8 GP |
378 | #ifdef CONFIG_SMP |
379 | extern unsigned int sysctl_oops_all_cpu_backtrace; | |
380 | #else | |
381 | #define sysctl_oops_all_cpu_backtrace 0 | |
382 | #endif /* CONFIG_SMP */ | |
383 | ||
1da177e4 | 384 | extern void bust_spinlocks(int yes); |
aa727107 | 385 | extern int panic_timeout; |
81c9d43f | 386 | extern unsigned long panic_print; |
1da177e4 | 387 | extern int panic_on_oops; |
8da5adda | 388 | extern int panic_on_unrecovered_nmi; |
5211a242 | 389 | extern int panic_on_io_nmi; |
9e3961a0 | 390 | extern int panic_on_warn; |
db38d5c1 RA |
391 | extern unsigned long panic_on_taint; |
392 | extern bool panic_on_taint_nousertaint; | |
088e9d25 | 393 | extern int sysctl_panic_on_rcu_stall; |
dfe56404 | 394 | extern int sysctl_max_rcu_stall_to_panic; |
55af7796 | 395 | extern int sysctl_panic_on_stackoverflow; |
5375b708 HD |
396 | |
397 | extern bool crash_kexec_post_notifiers; | |
398 | ||
1717f209 HK |
399 | /* |
400 | * panic_cpu is used for synchronizing panic() and crash_kexec() execution. It | |
401 | * holds a CPU number which is executing panic() currently. A value of | |
402 | * PANIC_CPU_INVALID means no CPU has entered panic() or crash_kexec(). | |
403 | */ | |
404 | extern atomic_t panic_cpu; | |
405 | #define PANIC_CPU_INVALID -1 | |
406 | ||
5800dc3c JB |
407 | /* |
408 | * Only to be used by arch init code. If the user over-wrote the default | |
409 | * CONFIG_PANIC_TIMEOUT, honor it. | |
410 | */ | |
411 | static inline void set_arch_panic_timeout(int timeout, int arch_default_timeout) | |
412 | { | |
413 | if (panic_timeout == arch_default_timeout) | |
414 | panic_timeout = timeout; | |
415 | } | |
1da177e4 | 416 | extern const char *print_tainted(void); |
373d4d09 RR |
417 | enum lockdep_ok { |
418 | LOCKDEP_STILL_OK, | |
419 | LOCKDEP_NOW_UNRELIABLE | |
420 | }; | |
421 | extern void add_taint(unsigned flag, enum lockdep_ok); | |
25ddbb18 AK |
422 | extern int test_taint(unsigned flag); |
423 | extern unsigned long get_taint(void); | |
b920de1b | 424 | extern int root_mountflags; |
1da177e4 | 425 | |
2ce802f6 TH |
426 | extern bool early_boot_irqs_disabled; |
427 | ||
69a78ff2 TG |
428 | /* |
429 | * Values used for system_state. Ordering of the states must not be changed | |
430 | * as code checks for <, <=, >, >= STATE. | |
431 | */ | |
1da177e4 LT |
432 | extern enum system_states { |
433 | SYSTEM_BOOTING, | |
69a78ff2 | 434 | SYSTEM_SCHEDULING, |
1da177e4 LT |
435 | SYSTEM_RUNNING, |
436 | SYSTEM_HALT, | |
437 | SYSTEM_POWER_OFF, | |
438 | SYSTEM_RESTART, | |
c1a957d1 | 439 | SYSTEM_SUSPEND, |
1da177e4 LT |
440 | } system_state; |
441 | ||
47d4b263 | 442 | /* This cannot be an enum because some may be used in assembly source. */ |
25ddbb18 AK |
443 | #define TAINT_PROPRIETARY_MODULE 0 |
444 | #define TAINT_FORCED_MODULE 1 | |
8c90487c | 445 | #define TAINT_CPU_OUT_OF_SPEC 2 |
25ddbb18 AK |
446 | #define TAINT_FORCED_RMMOD 3 |
447 | #define TAINT_MACHINE_CHECK 4 | |
448 | #define TAINT_BAD_PAGE 5 | |
449 | #define TAINT_USER 6 | |
450 | #define TAINT_DIE 7 | |
451 | #define TAINT_OVERRIDDEN_ACPI_TABLE 8 | |
452 | #define TAINT_WARN 9 | |
26e9a397 | 453 | #define TAINT_CRAP 10 |
92946bc7 | 454 | #define TAINT_FIRMWARE_WORKAROUND 11 |
2449b8ba | 455 | #define TAINT_OOT_MODULE 12 |
66cc69e3 | 456 | #define TAINT_UNSIGNED_MODULE 13 |
69361eef | 457 | #define TAINT_SOFTLOCKUP 14 |
c5f45465 | 458 | #define TAINT_LIVEPATCH 15 |
4efb442c | 459 | #define TAINT_AUX 16 |
bc4f2f54 KC |
460 | #define TAINT_RANDSTRUCT 17 |
461 | #define TAINT_FLAGS_COUNT 18 | |
db38d5c1 | 462 | #define TAINT_FLAGS_MAX ((1UL << TAINT_FLAGS_COUNT) - 1) |
7fd8329b PM |
463 | |
464 | struct taint_flag { | |
5eb7c0d0 LF |
465 | char c_true; /* character printed when tainted */ |
466 | char c_false; /* character printed when not tainted */ | |
7fd8329b PM |
467 | bool module; /* also show as a per-module taint flag */ |
468 | }; | |
469 | ||
470 | extern const struct taint_flag taint_flags[TAINT_FLAGS_COUNT]; | |
1da177e4 | 471 | |
3fc95772 HH |
472 | extern const char hex_asc[]; |
473 | #define hex_asc_lo(x) hex_asc[((x) & 0x0f)] | |
474 | #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4] | |
475 | ||
55036ba7 | 476 | static inline char *hex_byte_pack(char *buf, u8 byte) |
3fc95772 HH |
477 | { |
478 | *buf++ = hex_asc_hi(byte); | |
479 | *buf++ = hex_asc_lo(byte); | |
480 | return buf; | |
481 | } | |
99eaf3c4 | 482 | |
c26d436c AN |
483 | extern const char hex_asc_upper[]; |
484 | #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)] | |
485 | #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4] | |
486 | ||
487 | static inline char *hex_byte_pack_upper(char *buf, u8 byte) | |
488 | { | |
489 | *buf++ = hex_asc_upper_hi(byte); | |
490 | *buf++ = hex_asc_upper_lo(byte); | |
491 | return buf; | |
492 | } | |
493 | ||
90378889 | 494 | extern int hex_to_bin(char ch); |
b7804983 | 495 | extern int __must_check hex2bin(u8 *dst, const char *src, size_t count); |
53d91c5c | 496 | extern char *bin2hex(char *dst, const void *src, size_t count); |
90378889 | 497 | |
a69f5edb | 498 | bool mac_pton(const char *s, u8 *mac); |
4cd5773a | 499 | |
526211bc IM |
500 | /* |
501 | * General tracing related utility functions - trace_printk(), | |
2002c258 SR |
502 | * tracing_on/tracing_off and tracing_start()/tracing_stop |
503 | * | |
504 | * Use tracing_on/tracing_off when you want to quickly turn on or off | |
505 | * tracing. It simply enables or disables the recording of the trace events. | |
156f5a78 | 506 | * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on |
2002c258 SR |
507 | * file, which gives a means for the kernel and userspace to interact. |
508 | * Place a tracing_off() in the kernel where you want tracing to end. | |
509 | * From user space, examine the trace, and then echo 1 > tracing_on | |
510 | * to continue tracing. | |
511 | * | |
512 | * tracing_stop/tracing_start has slightly more overhead. It is used | |
513 | * by things like suspend to ram where disabling the recording of the | |
514 | * trace is not enough, but tracing must actually stop because things | |
515 | * like calling smp_processor_id() may crash the system. | |
516 | * | |
517 | * Most likely, you want to use tracing_on/tracing_off. | |
526211bc | 518 | */ |
cecbca96 FW |
519 | |
520 | enum ftrace_dump_mode { | |
521 | DUMP_NONE, | |
522 | DUMP_ALL, | |
523 | DUMP_ORIG, | |
524 | }; | |
525 | ||
526211bc | 526 | #ifdef CONFIG_TRACING |
93d68e52 SR |
527 | void tracing_on(void); |
528 | void tracing_off(void); | |
529 | int tracing_is_on(void); | |
ad909e21 SRRH |
530 | void tracing_snapshot(void); |
531 | void tracing_snapshot_alloc(void); | |
93d68e52 | 532 | |
526211bc IM |
533 | extern void tracing_start(void); |
534 | extern void tracing_stop(void); | |
526211bc | 535 | |
b9075fa9 JP |
536 | static inline __printf(1, 2) |
537 | void ____trace_printk_check_format(const char *fmt, ...) | |
769b0441 FW |
538 | { |
539 | } | |
540 | #define __trace_printk_check_format(fmt, args...) \ | |
541 | do { \ | |
542 | if (0) \ | |
543 | ____trace_printk_check_format(fmt, ##args); \ | |
544 | } while (0) | |
545 | ||
526211bc IM |
546 | /** |
547 | * trace_printk - printf formatting in the ftrace buffer | |
548 | * @fmt: the printf format for printing | |
549 | * | |
e8c97af0 RD |
550 | * Note: __trace_printk is an internal function for trace_printk() and |
551 | * the @ip is passed in via the trace_printk() macro. | |
526211bc IM |
552 | * |
553 | * This function allows a kernel developer to debug fast path sections | |
554 | * that printk is not appropriate for. By scattering in various | |
555 | * printk like tracing in the code, a developer can quickly see | |
556 | * where problems are occurring. | |
557 | * | |
558 | * This is intended as a debugging tool for the developer only. | |
559 | * Please refrain from leaving trace_printks scattered around in | |
09ae7234 | 560 | * your code. (Extra memory is used for special buffers that are |
e8c97af0 | 561 | * allocated when trace_printk() is used.) |
9d3c752c | 562 | * |
8730662d | 563 | * A little optimization trick is done here. If there's only one |
9d3c752c SRRH |
564 | * argument, there's no need to scan the string for printf formats. |
565 | * The trace_puts() will suffice. But how can we take advantage of | |
566 | * using trace_puts() when trace_printk() has only one argument? | |
567 | * By stringifying the args and checking the size we can tell | |
568 | * whether or not there are args. __stringify((__VA_ARGS__)) will | |
569 | * turn into "()\0" with a size of 3 when there are no args, anything | |
570 | * else will be bigger. All we need to do is define a string to this, | |
571 | * and then take its size and compare to 3. If it's bigger, use | |
572 | * do_trace_printk() otherwise, optimize it to trace_puts(). Then just | |
573 | * let gcc optimize the rest. | |
526211bc | 574 | */ |
769b0441 | 575 | |
9d3c752c SRRH |
576 | #define trace_printk(fmt, ...) \ |
577 | do { \ | |
578 | char _______STR[] = __stringify((__VA_ARGS__)); \ | |
579 | if (sizeof(_______STR) > 3) \ | |
580 | do_trace_printk(fmt, ##__VA_ARGS__); \ | |
581 | else \ | |
582 | trace_puts(fmt); \ | |
583 | } while (0) | |
584 | ||
585 | #define do_trace_printk(fmt, args...) \ | |
769b0441 | 586 | do { \ |
3debb0a9 | 587 | static const char *trace_printk_fmt __used \ |
33def849 | 588 | __section("__trace_printk_fmt") = \ |
07d777fe SR |
589 | __builtin_constant_p(fmt) ? fmt : NULL; \ |
590 | \ | |
769b0441 | 591 | __trace_printk_check_format(fmt, ##args); \ |
48ead020 | 592 | \ |
07d777fe | 593 | if (__builtin_constant_p(fmt)) \ |
48ead020 | 594 | __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \ |
07d777fe SR |
595 | else \ |
596 | __trace_printk(_THIS_IP_, fmt, ##args); \ | |
769b0441 FW |
597 | } while (0) |
598 | ||
b9075fa9 JP |
599 | extern __printf(2, 3) |
600 | int __trace_bprintk(unsigned long ip, const char *fmt, ...); | |
48ead020 | 601 | |
b9075fa9 JP |
602 | extern __printf(2, 3) |
603 | int __trace_printk(unsigned long ip, const char *fmt, ...); | |
769b0441 | 604 | |
09ae7234 SRRH |
605 | /** |
606 | * trace_puts - write a string into the ftrace buffer | |
607 | * @str: the string to record | |
608 | * | |
609 | * Note: __trace_bputs is an internal function for trace_puts and | |
610 | * the @ip is passed in via the trace_puts macro. | |
611 | * | |
612 | * This is similar to trace_printk() but is made for those really fast | |
e8c97af0 | 613 | * paths that a developer wants the least amount of "Heisenbug" effects, |
09ae7234 SRRH |
614 | * where the processing of the print format is still too much. |
615 | * | |
616 | * This function allows a kernel developer to debug fast path sections | |
617 | * that printk is not appropriate for. By scattering in various | |
618 | * printk like tracing in the code, a developer can quickly see | |
619 | * where problems are occurring. | |
620 | * | |
621 | * This is intended as a debugging tool for the developer only. | |
622 | * Please refrain from leaving trace_puts scattered around in | |
623 | * your code. (Extra memory is used for special buffers that are | |
e8c97af0 | 624 | * allocated when trace_puts() is used.) |
09ae7234 SRRH |
625 | * |
626 | * Returns: 0 if nothing was written, positive # if string was. | |
627 | * (1 when __trace_bputs is used, strlen(str) when __trace_puts is used) | |
628 | */ | |
629 | ||
09ae7234 | 630 | #define trace_puts(str) ({ \ |
3debb0a9 | 631 | static const char *trace_printk_fmt __used \ |
33def849 | 632 | __section("__trace_printk_fmt") = \ |
09ae7234 SRRH |
633 | __builtin_constant_p(str) ? str : NULL; \ |
634 | \ | |
635 | if (__builtin_constant_p(str)) \ | |
636 | __trace_bputs(_THIS_IP_, trace_printk_fmt); \ | |
637 | else \ | |
638 | __trace_puts(_THIS_IP_, str, strlen(str)); \ | |
639 | }) | |
bcf312cf SR |
640 | extern int __trace_bputs(unsigned long ip, const char *str); |
641 | extern int __trace_puts(unsigned long ip, const char *str, int size); | |
09ae7234 | 642 | |
c142be8e | 643 | extern void trace_dump_stack(int skip); |
03889384 | 644 | |
48ead020 FW |
645 | /* |
646 | * The double __builtin_constant_p is because gcc will give us an error | |
647 | * if we try to allocate the static variable to fmt if it is not a | |
648 | * constant. Even with the outer if statement. | |
649 | */ | |
769b0441 FW |
650 | #define ftrace_vprintk(fmt, vargs) \ |
651 | do { \ | |
48ead020 | 652 | if (__builtin_constant_p(fmt)) { \ |
3debb0a9 | 653 | static const char *trace_printk_fmt __used \ |
33def849 | 654 | __section("__trace_printk_fmt") = \ |
48ead020 | 655 | __builtin_constant_p(fmt) ? fmt : NULL; \ |
7bffc23e | 656 | \ |
48ead020 FW |
657 | __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \ |
658 | } else \ | |
659 | __ftrace_vprintk(_THIS_IP_, fmt, vargs); \ | |
769b0441 FW |
660 | } while (0) |
661 | ||
8db14860 | 662 | extern __printf(2, 0) int |
48ead020 FW |
663 | __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap); |
664 | ||
8db14860 | 665 | extern __printf(2, 0) int |
526211bc | 666 | __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap); |
769b0441 | 667 | |
cecbca96 | 668 | extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode); |
526211bc | 669 | #else |
526211bc IM |
670 | static inline void tracing_start(void) { } |
671 | static inline void tracing_stop(void) { } | |
e67bc51e | 672 | static inline void trace_dump_stack(int skip) { } |
93d68e52 SR |
673 | |
674 | static inline void tracing_on(void) { } | |
675 | static inline void tracing_off(void) { } | |
676 | static inline int tracing_is_on(void) { return 0; } | |
ad909e21 SRRH |
677 | static inline void tracing_snapshot(void) { } |
678 | static inline void tracing_snapshot_alloc(void) { } | |
93d68e52 | 679 | |
60efc15a MH |
680 | static inline __printf(1, 2) |
681 | int trace_printk(const char *fmt, ...) | |
526211bc IM |
682 | { |
683 | return 0; | |
684 | } | |
8db14860 | 685 | static __printf(1, 0) inline int |
526211bc IM |
686 | ftrace_vprintk(const char *fmt, va_list ap) |
687 | { | |
688 | return 0; | |
689 | } | |
cecbca96 | 690 | static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { } |
769b0441 | 691 | #endif /* CONFIG_TRACING */ |
526211bc | 692 | |
cf14f27f AS |
693 | /* This counts to 12. Any more, it will return 13th argument. */ |
694 | #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n | |
695 | #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) | |
696 | ||
697 | #define __CONCAT(a, b) a ## b | |
698 | #define CONCATENATE(a, b) __CONCAT(a, b) | |
699 | ||
1da177e4 LT |
700 | /** |
701 | * container_of - cast a member of a structure out to the containing structure | |
1da177e4 LT |
702 | * @ptr: the pointer to the member. |
703 | * @type: the type of the container struct this is embedded in. | |
704 | * @member: the name of the member within the struct. | |
705 | * | |
706 | */ | |
c7acec71 IA |
707 | #define container_of(ptr, type, member) ({ \ |
708 | void *__mptr = (void *)(ptr); \ | |
709 | BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ | |
710 | !__same_type(*(ptr), void), \ | |
711 | "pointer type mismatch in container_of()"); \ | |
712 | ((type *)(__mptr - offsetof(type, member))); }) | |
1da177e4 | 713 | |
05e6557b N |
714 | /** |
715 | * container_of_safe - cast a member of a structure out to the containing structure | |
716 | * @ptr: the pointer to the member. | |
717 | * @type: the type of the container struct this is embedded in. | |
718 | * @member: the name of the member within the struct. | |
719 | * | |
720 | * If IS_ERR_OR_NULL(ptr), ptr is returned unchanged. | |
721 | */ | |
722 | #define container_of_safe(ptr, type, member) ({ \ | |
723 | void *__mptr = (void *)(ptr); \ | |
724 | BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) && \ | |
725 | !__same_type(*(ptr), void), \ | |
726 | "pointer type mismatch in container_of()"); \ | |
227abcc6 | 727 | IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \ |
05e6557b N |
728 | ((type *)(__mptr - offsetof(type, member))); }) |
729 | ||
b9d4f426 AL |
730 | /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */ |
731 | #ifdef CONFIG_FTRACE_MCOUNT_RECORD | |
732 | # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD | |
733 | #endif | |
9d00f92f | 734 | |
58f86cc8 | 735 | /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */ |
28b8d0c8 GCM |
736 | #define VERIFY_OCTAL_PERMISSIONS(perms) \ |
737 | (BUILD_BUG_ON_ZERO((perms) < 0) + \ | |
738 | BUILD_BUG_ON_ZERO((perms) > 0777) + \ | |
739 | /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \ | |
740 | BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \ | |
741 | BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \ | |
742 | /* USER_WRITABLE >= GROUP_WRITABLE */ \ | |
743 | BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \ | |
744 | /* OTHER_WRITABLE? Generally considered a bad idea. */ \ | |
745 | BUILD_BUG_ON_ZERO((perms) & 2) + \ | |
58f86cc8 | 746 | (perms)) |
1da177e4 | 747 | #endif |