0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef _LINUX_KERNEL_H
0012 #define _LINUX_KERNEL_H
0013
0014 #include <linux/stdarg.h>
0015 #include <linux/align.h>
0016 #include <linux/limits.h>
0017 #include <linux/linkage.h>
0018 #include <linux/stddef.h>
0019 #include <linux/types.h>
0020 #include <linux/compiler.h>
0021 #include <linux/container_of.h>
0022 #include <linux/bitops.h>
0023 #include <linux/kstrtox.h>
0024 #include <linux/log2.h>
0025 #include <linux/math.h>
0026 #include <linux/minmax.h>
0027 #include <linux/typecheck.h>
0028 #include <linux/panic.h>
0029 #include <linux/printk.h>
0030 #include <linux/build_bug.h>
0031 #include <linux/static_call_types.h>
0032 #include <linux/instruction_pointer.h>
0033 #include <asm/byteorder.h>
0034
0035 #include <uapi/linux/kernel.h>
0036
0037 #define STACK_MAGIC 0xdeadbeef
0038
0039
0040
0041
0042
0043
0044
0045 #define REPEAT_BYTE(x) ((~0ul / 0xff) * (x))
0046
0047
0048 #define READ 0
0049 #define WRITE 1
0050
0051
0052
0053
0054
0055 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
0056
0057 #define PTR_IF(cond, ptr) ((cond) ? (ptr) : NULL)
0058
0059 #define u64_to_user_ptr(x) ( \
0060 { \
0061 typecheck(u64, (x)); \
0062 (void __user *)(uintptr_t)(x); \
0063 } \
0064 )
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
0075
0076
0077
0078
0079
0080 #define lower_32_bits(n) ((u32)((n) & 0xffffffff))
0081
0082
0083
0084
0085
0086 #define upper_16_bits(n) ((u16)((n) >> 16))
0087
0088
0089
0090
0091
0092 #define lower_16_bits(n) ((u16)((n) & 0xffff))
0093
0094 struct completion;
0095 struct user;
0096
0097 #ifdef CONFIG_PREEMPT_VOLUNTARY_BUILD
0098
0099 extern int __cond_resched(void);
0100 # define might_resched() __cond_resched()
0101
0102 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_CALL)
0103
0104 extern int __cond_resched(void);
0105
0106 DECLARE_STATIC_CALL(might_resched, __cond_resched);
0107
0108 static __always_inline void might_resched(void)
0109 {
0110 static_call_mod(might_resched)();
0111 }
0112
0113 #elif defined(CONFIG_PREEMPT_DYNAMIC) && defined(CONFIG_HAVE_PREEMPT_DYNAMIC_KEY)
0114
0115 extern int dynamic_might_resched(void);
0116 # define might_resched() dynamic_might_resched()
0117
0118 #else
0119
0120 # define might_resched() do { } while (0)
0121
0122 #endif
0123
0124 #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
0125 extern void __might_resched(const char *file, int line, unsigned int offsets);
0126 extern void __might_sleep(const char *file, int line);
0127 extern void __cant_sleep(const char *file, int line, int preempt_offset);
0128 extern void __cant_migrate(const char *file, int line);
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142 # define might_sleep() \
0143 do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
0144
0145
0146
0147
0148
0149 # define cant_sleep() \
0150 do { __cant_sleep(__FILE__, __LINE__, 0); } while (0)
0151 # define sched_annotate_sleep() (current->task_state_change = 0)
0152
0153
0154
0155
0156
0157
0158 # define cant_migrate() \
0159 do { \
0160 if (IS_ENABLED(CONFIG_SMP)) \
0161 __cant_migrate(__FILE__, __LINE__); \
0162 } while (0)
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174 # define non_block_start() (current->non_block_count++)
0175
0176
0177
0178
0179
0180 # define non_block_end() WARN_ON(current->non_block_count-- == 0)
0181 #else
0182 static inline void __might_resched(const char *file, int line,
0183 unsigned int offsets) { }
0184 static inline void __might_sleep(const char *file, int line) { }
0185 # define might_sleep() do { might_resched(); } while (0)
0186 # define cant_sleep() do { } while (0)
0187 # define cant_migrate() do { } while (0)
0188 # define sched_annotate_sleep() do { } while (0)
0189 # define non_block_start() do { } while (0)
0190 # define non_block_end() do { } while (0)
0191 #endif
0192
0193 #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
0194
0195 #if defined(CONFIG_MMU) && \
0196 (defined(CONFIG_PROVE_LOCKING) || defined(CONFIG_DEBUG_ATOMIC_SLEEP))
0197 #define might_fault() __might_fault(__FILE__, __LINE__)
0198 void __might_fault(const char *file, int line);
0199 #else
0200 static inline void might_fault(void) { }
0201 #endif
0202
0203 void do_exit(long error_code) __noreturn;
0204
0205 extern int num_to_str(char *buf, int size,
0206 unsigned long long num, unsigned int width);
0207
0208
0209
0210 extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
0211 extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
0212 extern __printf(3, 4)
0213 int snprintf(char *buf, size_t size, const char *fmt, ...);
0214 extern __printf(3, 0)
0215 int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
0216 extern __printf(3, 4)
0217 int scnprintf(char *buf, size_t size, const char *fmt, ...);
0218 extern __printf(3, 0)
0219 int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
0220 extern __printf(2, 3) __malloc
0221 char *kasprintf(gfp_t gfp, const char *fmt, ...);
0222 extern __printf(2, 0) __malloc
0223 char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
0224 extern __printf(2, 0)
0225 const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list args);
0226
0227 extern __scanf(2, 3)
0228 int sscanf(const char *, const char *, ...);
0229 extern __scanf(2, 0)
0230 int vsscanf(const char *, const char *, va_list);
0231
0232 extern int no_hash_pointers_enable(char *str);
0233
0234 extern int get_option(char **str, int *pint);
0235 extern char *get_options(const char *str, int nints, int *ints);
0236 extern unsigned long long memparse(const char *ptr, char **retptr);
0237 extern bool parse_option_str(const char *str, const char *option);
0238 extern char *next_arg(char *args, char **param, char **val);
0239
0240 extern int core_kernel_text(unsigned long addr);
0241 extern int __kernel_text_address(unsigned long addr);
0242 extern int kernel_text_address(unsigned long addr);
0243 extern int func_ptr_is_kernel_text(void *ptr);
0244
0245 extern void bust_spinlocks(int yes);
0246
0247 extern int root_mountflags;
0248
0249 extern bool early_boot_irqs_disabled;
0250
0251
0252
0253
0254
0255 extern enum system_states {
0256 SYSTEM_BOOTING,
0257 SYSTEM_SCHEDULING,
0258 SYSTEM_FREEING_INITMEM,
0259 SYSTEM_RUNNING,
0260 SYSTEM_HALT,
0261 SYSTEM_POWER_OFF,
0262 SYSTEM_RESTART,
0263 SYSTEM_SUSPEND,
0264 } system_state;
0265
0266 extern const char hex_asc[];
0267 #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
0268 #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
0269
0270 static inline char *hex_byte_pack(char *buf, u8 byte)
0271 {
0272 *buf++ = hex_asc_hi(byte);
0273 *buf++ = hex_asc_lo(byte);
0274 return buf;
0275 }
0276
0277 extern const char hex_asc_upper[];
0278 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0f)]
0279 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xf0) >> 4]
0280
0281 static inline char *hex_byte_pack_upper(char *buf, u8 byte)
0282 {
0283 *buf++ = hex_asc_upper_hi(byte);
0284 *buf++ = hex_asc_upper_lo(byte);
0285 return buf;
0286 }
0287
0288 extern int hex_to_bin(unsigned char ch);
0289 extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
0290 extern char *bin2hex(char *dst, const void *src, size_t count);
0291
0292 bool mac_pton(const char *s, u8 *mac);
0293
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314 enum ftrace_dump_mode {
0315 DUMP_NONE,
0316 DUMP_ALL,
0317 DUMP_ORIG,
0318 };
0319
0320 #ifdef CONFIG_TRACING
0321 void tracing_on(void);
0322 void tracing_off(void);
0323 int tracing_is_on(void);
0324 void tracing_snapshot(void);
0325 void tracing_snapshot_alloc(void);
0326
0327 extern void tracing_start(void);
0328 extern void tracing_stop(void);
0329
0330 static inline __printf(1, 2)
0331 void ____trace_printk_check_format(const char *fmt, ...)
0332 {
0333 }
0334 #define __trace_printk_check_format(fmt, args...) \
0335 do { \
0336 if (0) \
0337 ____trace_printk_check_format(fmt, ##args); \
0338 } while (0)
0339
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370 #define trace_printk(fmt, ...) \
0371 do { \
0372 char _______STR[] = __stringify((__VA_ARGS__)); \
0373 if (sizeof(_______STR) > 3) \
0374 do_trace_printk(fmt, ##__VA_ARGS__); \
0375 else \
0376 trace_puts(fmt); \
0377 } while (0)
0378
0379 #define do_trace_printk(fmt, args...) \
0380 do { \
0381 static const char *trace_printk_fmt __used \
0382 __section("__trace_printk_fmt") = \
0383 __builtin_constant_p(fmt) ? fmt : NULL; \
0384 \
0385 __trace_printk_check_format(fmt, ##args); \
0386 \
0387 if (__builtin_constant_p(fmt)) \
0388 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
0389 else \
0390 __trace_printk(_THIS_IP_, fmt, ##args); \
0391 } while (0)
0392
0393 extern __printf(2, 3)
0394 int __trace_bprintk(unsigned long ip, const char *fmt, ...);
0395
0396 extern __printf(2, 3)
0397 int __trace_printk(unsigned long ip, const char *fmt, ...);
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424 #define trace_puts(str) ({ \
0425 static const char *trace_printk_fmt __used \
0426 __section("__trace_printk_fmt") = \
0427 __builtin_constant_p(str) ? str : NULL; \
0428 \
0429 if (__builtin_constant_p(str)) \
0430 __trace_bputs(_THIS_IP_, trace_printk_fmt); \
0431 else \
0432 __trace_puts(_THIS_IP_, str, strlen(str)); \
0433 })
0434 extern int __trace_bputs(unsigned long ip, const char *str);
0435 extern int __trace_puts(unsigned long ip, const char *str, int size);
0436
0437 extern void trace_dump_stack(int skip);
0438
0439
0440
0441
0442
0443
0444 #define ftrace_vprintk(fmt, vargs) \
0445 do { \
0446 if (__builtin_constant_p(fmt)) { \
0447 static const char *trace_printk_fmt __used \
0448 __section("__trace_printk_fmt") = \
0449 __builtin_constant_p(fmt) ? fmt : NULL; \
0450 \
0451 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
0452 } else \
0453 __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
0454 } while (0)
0455
0456 extern __printf(2, 0) int
0457 __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
0458
0459 extern __printf(2, 0) int
0460 __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
0461
0462 extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
0463 #else
0464 static inline void tracing_start(void) { }
0465 static inline void tracing_stop(void) { }
0466 static inline void trace_dump_stack(int skip) { }
0467
0468 static inline void tracing_on(void) { }
0469 static inline void tracing_off(void) { }
0470 static inline int tracing_is_on(void) { return 0; }
0471 static inline void tracing_snapshot(void) { }
0472 static inline void tracing_snapshot_alloc(void) { }
0473
0474 static inline __printf(1, 2)
0475 int trace_printk(const char *fmt, ...)
0476 {
0477 return 0;
0478 }
0479 static __printf(1, 0) inline int
0480 ftrace_vprintk(const char *fmt, va_list ap)
0481 {
0482 return 0;
0483 }
0484 static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
0485 #endif
0486
0487
0488 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
0489 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
0490
0491 #define __CONCAT(a, b) a ## b
0492 #define CONCATENATE(a, b) __CONCAT(a, b)
0493
0494
0495 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
0496 # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
0497 #endif
0498
0499
0500 #define VERIFY_OCTAL_PERMISSIONS(perms) \
0501 (BUILD_BUG_ON_ZERO((perms) < 0) + \
0502 BUILD_BUG_ON_ZERO((perms) > 0777) + \
0503 \
0504 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
0505 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
0506 \
0507 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
0508 \
0509 BUILD_BUG_ON_ZERO((perms) & 2) + \
0510 (perms))
0511 #endif