kernel.h 34 KB

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