kernel.h 32 KB

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