kernel.h 30 KB

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