kernel.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. #ifndef _LINUX_KERNEL_H
  2. #define _LINUX_KERNEL_H
  3. /*
  4. * 'kernel.h' contains some often-used function prototypes etc
  5. */
  6. #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
  7. #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
  8. #ifdef __KERNEL__
  9. #include <stdarg.h>
  10. #include <linux/linkage.h>
  11. #include <linux/stddef.h>
  12. #include <linux/types.h>
  13. #include <linux/compiler.h>
  14. #include <linux/bitops.h>
  15. #include <linux/log2.h>
  16. #include <linux/typecheck.h>
  17. #include <linux/dynamic_debug.h>
  18. #include <asm/byteorder.h>
  19. #include <asm/bug.h>
  20. extern const char linux_banner[];
  21. extern const char linux_proc_banner[];
  22. #define USHRT_MAX ((u16)(~0U))
  23. #define SHRT_MAX ((s16)(USHRT_MAX>>1))
  24. #define SHRT_MIN ((s16)(-SHRT_MAX - 1))
  25. #define INT_MAX ((int)(~0U>>1))
  26. #define INT_MIN (-INT_MAX - 1)
  27. #define UINT_MAX (~0U)
  28. #define LONG_MAX ((long)(~0UL>>1))
  29. #define LONG_MIN (-LONG_MAX - 1)
  30. #define ULONG_MAX (~0UL)
  31. #define LLONG_MAX ((long long)(~0ULL>>1))
  32. #define LLONG_MIN (-LLONG_MAX - 1)
  33. #define ULLONG_MAX (~0ULL)
  34. #define STACK_MAGIC 0xdeadbeef
  35. #define ALIGN(x, a) __ALIGN_KERNEL((x), (a))
  36. #define __ALIGN_MASK(x, mask) __ALIGN_KERNEL_MASK((x), (mask))
  37. #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
  38. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  39. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  40. /*
  41. * This looks more complex than it should be. But we need to
  42. * get the type for the ~ right in round_down (it needs to be
  43. * as wide as the result!), and we want to evaluate the macro
  44. * arguments just once each.
  45. */
  46. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  47. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  48. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  49. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  50. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  51. #define roundup(x, y) ( \
  52. { \
  53. const typeof(y) __y = y; \
  54. (((x) + (__y - 1)) / __y) * __y; \
  55. } \
  56. )
  57. #define rounddown(x, y) ( \
  58. { \
  59. typeof(x) __x = (x); \
  60. __x - (__x % (y)); \
  61. } \
  62. )
  63. #define DIV_ROUND_CLOSEST(x, divisor)( \
  64. { \
  65. typeof(divisor) __divisor = divisor; \
  66. (((x) + ((__divisor) / 2)) / (__divisor)); \
  67. } \
  68. )
  69. #define _RET_IP_ (unsigned long)__builtin_return_address(0)
  70. #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
  71. #ifdef CONFIG_LBDAF
  72. # include <asm/div64.h>
  73. # define sector_div(a, b) do_div(a, b)
  74. #else
  75. # define sector_div(n, b)( \
  76. { \
  77. int _res; \
  78. _res = (n) % (b); \
  79. (n) /= (b); \
  80. _res; \
  81. } \
  82. )
  83. #endif
  84. /**
  85. * upper_32_bits - return bits 32-63 of a number
  86. * @n: the number we're accessing
  87. *
  88. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  89. * the "right shift count >= width of type" warning when that quantity is
  90. * 32-bits.
  91. */
  92. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  93. /**
  94. * lower_32_bits - return bits 0-31 of a number
  95. * @n: the number we're accessing
  96. */
  97. #define lower_32_bits(n) ((u32)(n))
  98. #define KERN_EMERG "<0>" /* system is unusable */
  99. #define KERN_ALERT "<1>" /* action must be taken immediately */
  100. #define KERN_CRIT "<2>" /* critical conditions */
  101. #define KERN_ERR "<3>" /* error conditions */
  102. #define KERN_WARNING "<4>" /* warning conditions */
  103. #define KERN_NOTICE "<5>" /* normal but significant condition */
  104. #define KERN_INFO "<6>" /* informational */
  105. #define KERN_DEBUG "<7>" /* debug-level messages */
  106. /* Use the default kernel loglevel */
  107. #define KERN_DEFAULT "<d>"
  108. /*
  109. * Annotation for a "continued" line of log printout (only done after a
  110. * line that had no enclosing \n). Only to be used by core/arch code
  111. * during early bootup (a continued line is not SMP-safe otherwise).
  112. */
  113. #define KERN_CONT "<c>"
  114. extern int console_printk[];
  115. #define console_loglevel (console_printk[0])
  116. #define default_message_loglevel (console_printk[1])
  117. #define minimum_console_loglevel (console_printk[2])
  118. #define default_console_loglevel (console_printk[3])
  119. struct completion;
  120. struct pt_regs;
  121. struct user;
  122. #ifdef CONFIG_PREEMPT_VOLUNTARY
  123. extern int _cond_resched(void);
  124. # define might_resched() _cond_resched()
  125. #else
  126. # define might_resched() do { } while (0)
  127. #endif
  128. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  129. void __might_sleep(const char *file, int line, int preempt_offset);
  130. /**
  131. * might_sleep - annotation for functions that can sleep
  132. *
  133. * this macro will print a stack trace if it is executed in an atomic
  134. * context (spinlock, irq-handler, ...).
  135. *
  136. * This is a useful debugging help to be able to catch problems early and not
  137. * be bitten later when the calling function happens to sleep when it is not
  138. * supposed to.
  139. */
  140. # define might_sleep() \
  141. do { __might_sleep(__FILE__, __LINE__, 0); might_resched(); } while (0)
  142. #else
  143. static inline void __might_sleep(const char *file, int line,
  144. int preempt_offset) { }
  145. # define might_sleep() do { might_resched(); } while (0)
  146. #endif
  147. #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
  148. #define abs(x) ({ \
  149. long __x = (x); \
  150. (__x < 0) ? -__x : __x; \
  151. })
  152. #define abs64(x) ({ \
  153. s64 __x = (x); \
  154. (__x < 0) ? -__x : __x; \
  155. })
  156. #ifdef CONFIG_PROVE_LOCKING
  157. void might_fault(void);
  158. #else
  159. static inline void might_fault(void)
  160. {
  161. might_sleep();
  162. }
  163. #endif
  164. struct va_format {
  165. const char *fmt;
  166. va_list *va;
  167. };
  168. extern struct atomic_notifier_head panic_notifier_list;
  169. extern long (*panic_blink)(int state);
  170. NORET_TYPE void panic(const char * fmt, ...)
  171. __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
  172. extern void oops_enter(void);
  173. extern void oops_exit(void);
  174. void print_oops_end_marker(void);
  175. extern int oops_may_print(void);
  176. NORET_TYPE void do_exit(long error_code)
  177. ATTRIB_NORET;
  178. NORET_TYPE void complete_and_exit(struct completion *, long)
  179. ATTRIB_NORET;
  180. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  181. extern long simple_strtol(const char *,char **,unsigned int);
  182. extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
  183. extern long long simple_strtoll(const char *,char **,unsigned int);
  184. extern int __must_check strict_strtoul(const char *, unsigned int, unsigned long *);
  185. extern int __must_check strict_strtol(const char *, unsigned int, long *);
  186. extern int __must_check strict_strtoull(const char *, unsigned int, unsigned long long *);
  187. extern int __must_check strict_strtoll(const char *, unsigned int, long long *);
  188. extern int sprintf(char * buf, const char * fmt, ...)
  189. __attribute__ ((format (printf, 2, 3)));
  190. extern int vsprintf(char *buf, const char *, va_list)
  191. __attribute__ ((format (printf, 2, 0)));
  192. extern int snprintf(char * buf, size_t size, const char * fmt, ...)
  193. __attribute__ ((format (printf, 3, 4)));
  194. extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  195. __attribute__ ((format (printf, 3, 0)));
  196. extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
  197. __attribute__ ((format (printf, 3, 4)));
  198. extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  199. __attribute__ ((format (printf, 3, 0)));
  200. extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
  201. __attribute__ ((format (printf, 2, 3)));
  202. extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  203. extern int sscanf(const char *, const char *, ...)
  204. __attribute__ ((format (scanf, 2, 3)));
  205. extern int vsscanf(const char *, const char *, va_list)
  206. __attribute__ ((format (scanf, 2, 0)));
  207. extern int get_option(char **str, int *pint);
  208. extern char *get_options(const char *str, int nints, int *ints);
  209. extern unsigned long long memparse(const char *ptr, char **retptr);
  210. extern int core_kernel_text(unsigned long addr);
  211. extern int __kernel_text_address(unsigned long addr);
  212. extern int kernel_text_address(unsigned long addr);
  213. extern int func_ptr_is_kernel_text(void *ptr);
  214. struct pid;
  215. extern struct pid *session_of_pgrp(struct pid *pgrp);
  216. /*
  217. * FW_BUG
  218. * Add this to a message where you are sure the firmware is buggy or behaves
  219. * really stupid or out of spec. Be aware that the responsible BIOS developer
  220. * should be able to fix this issue or at least get a concrete idea of the
  221. * problem by reading your message without the need of looking at the kernel
  222. * code.
  223. *
  224. * Use it for definite and high priority BIOS bugs.
  225. *
  226. * FW_WARN
  227. * Use it for not that clear (e.g. could the kernel messed up things already?)
  228. * and medium priority BIOS bugs.
  229. *
  230. * FW_INFO
  231. * Use this one if you want to tell the user or vendor about something
  232. * suspicious, but generally harmless related to the firmware.
  233. *
  234. * Use it for information or very low priority BIOS bugs.
  235. */
  236. #define FW_BUG "[Firmware Bug]: "
  237. #define FW_WARN "[Firmware Warn]: "
  238. #define FW_INFO "[Firmware Info]: "
  239. /*
  240. * HW_ERR
  241. * Add this to a message for hardware errors, so that user can report
  242. * it to hardware vendor instead of LKML or software vendor.
  243. */
  244. #define HW_ERR "[Hardware Error]: "
  245. #ifdef CONFIG_PRINTK
  246. asmlinkage int vprintk(const char *fmt, va_list args)
  247. __attribute__ ((format (printf, 1, 0)));
  248. asmlinkage int printk(const char * fmt, ...)
  249. __attribute__ ((format (printf, 1, 2))) __cold;
  250. /*
  251. * Please don't use printk_ratelimit(), because it shares ratelimiting state
  252. * with all other unrelated printk_ratelimit() callsites. Instead use
  253. * printk_ratelimited() or plain old __ratelimit().
  254. */
  255. extern int __printk_ratelimit(const char *func);
  256. #define printk_ratelimit() __printk_ratelimit(__func__)
  257. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  258. unsigned int interval_msec);
  259. extern int printk_delay_msec;
  260. /*
  261. * Print a one-time message (analogous to WARN_ONCE() et al):
  262. */
  263. #define printk_once(x...) ({ \
  264. static bool __print_once; \
  265. \
  266. if (!__print_once) { \
  267. __print_once = true; \
  268. printk(x); \
  269. } \
  270. })
  271. void log_buf_kexec_setup(void);
  272. #else
  273. static inline int vprintk(const char *s, va_list args)
  274. __attribute__ ((format (printf, 1, 0)));
  275. static inline int vprintk(const char *s, va_list args) { return 0; }
  276. static inline int printk(const char *s, ...)
  277. __attribute__ ((format (printf, 1, 2)));
  278. static inline int __cold printk(const char *s, ...) { return 0; }
  279. static inline int printk_ratelimit(void) { return 0; }
  280. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
  281. unsigned int interval_msec) \
  282. { return false; }
  283. /* No effect, but we still get type checking even in the !PRINTK case: */
  284. #define printk_once(x...) printk(x)
  285. static inline void log_buf_kexec_setup(void)
  286. {
  287. }
  288. #endif
  289. /*
  290. * Dummy printk for disabled debugging statements to use whilst maintaining
  291. * gcc's format and side-effect checking.
  292. */
  293. static inline __attribute__ ((format (printf, 1, 2)))
  294. int no_printk(const char *s, ...) { return 0; }
  295. extern int printk_needs_cpu(int cpu);
  296. extern void printk_tick(void);
  297. extern void asmlinkage __attribute__((format(printf, 1, 2)))
  298. early_printk(const char *fmt, ...);
  299. unsigned long int_sqrt(unsigned long);
  300. static inline void console_silent(void)
  301. {
  302. console_loglevel = 0;
  303. }
  304. static inline void console_verbose(void)
  305. {
  306. if (console_loglevel)
  307. console_loglevel = 15;
  308. }
  309. extern void bust_spinlocks(int yes);
  310. extern void wake_up_klogd(void);
  311. extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
  312. extern int panic_timeout;
  313. extern int panic_on_oops;
  314. extern int panic_on_unrecovered_nmi;
  315. extern int panic_on_io_nmi;
  316. extern const char *print_tainted(void);
  317. extern void add_taint(unsigned flag);
  318. extern int test_taint(unsigned flag);
  319. extern unsigned long get_taint(void);
  320. extern int root_mountflags;
  321. /* Values used for system_state */
  322. extern enum system_states {
  323. SYSTEM_BOOTING,
  324. SYSTEM_RUNNING,
  325. SYSTEM_HALT,
  326. SYSTEM_POWER_OFF,
  327. SYSTEM_RESTART,
  328. SYSTEM_SUSPEND_DISK,
  329. } system_state;
  330. #define TAINT_PROPRIETARY_MODULE 0
  331. #define TAINT_FORCED_MODULE 1
  332. #define TAINT_UNSAFE_SMP 2
  333. #define TAINT_FORCED_RMMOD 3
  334. #define TAINT_MACHINE_CHECK 4
  335. #define TAINT_BAD_PAGE 5
  336. #define TAINT_USER 6
  337. #define TAINT_DIE 7
  338. #define TAINT_OVERRIDDEN_ACPI_TABLE 8
  339. #define TAINT_WARN 9
  340. #define TAINT_CRAP 10
  341. #define TAINT_FIRMWARE_WORKAROUND 11
  342. extern void dump_stack(void) __cold;
  343. enum {
  344. DUMP_PREFIX_NONE,
  345. DUMP_PREFIX_ADDRESS,
  346. DUMP_PREFIX_OFFSET
  347. };
  348. extern void hex_dump_to_buffer(const void *buf, size_t len,
  349. int rowsize, int groupsize,
  350. char *linebuf, size_t linebuflen, bool ascii);
  351. extern void print_hex_dump(const char *level, const char *prefix_str,
  352. int prefix_type, int rowsize, int groupsize,
  353. const void *buf, size_t len, bool ascii);
  354. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  355. const void *buf, size_t len);
  356. extern const char hex_asc[];
  357. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  358. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  359. static inline char *pack_hex_byte(char *buf, u8 byte)
  360. {
  361. *buf++ = hex_asc_hi(byte);
  362. *buf++ = hex_asc_lo(byte);
  363. return buf;
  364. }
  365. extern int hex_to_bin(char ch);
  366. #ifndef pr_fmt
  367. #define pr_fmt(fmt) fmt
  368. #endif
  369. #define pr_emerg(fmt, ...) \
  370. printk(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  371. #define pr_alert(fmt, ...) \
  372. printk(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  373. #define pr_crit(fmt, ...) \
  374. printk(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  375. #define pr_err(fmt, ...) \
  376. printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  377. #define pr_warning(fmt, ...) \
  378. printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  379. #define pr_warn pr_warning
  380. #define pr_notice(fmt, ...) \
  381. printk(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  382. #define pr_info(fmt, ...) \
  383. printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  384. #define pr_cont(fmt, ...) \
  385. printk(KERN_CONT fmt, ##__VA_ARGS__)
  386. /* pr_devel() should produce zero code unless DEBUG is defined */
  387. #ifdef DEBUG
  388. #define pr_devel(fmt, ...) \
  389. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  390. #else
  391. #define pr_devel(fmt, ...) \
  392. ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
  393. #endif
  394. /* If you are writing a driver, please use dev_dbg instead */
  395. #if defined(DEBUG)
  396. #define pr_debug(fmt, ...) \
  397. printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  398. #elif defined(CONFIG_DYNAMIC_DEBUG)
  399. /* dynamic_pr_debug() uses pr_fmt() internally so we don't need it here */
  400. #define pr_debug(fmt, ...) \
  401. dynamic_pr_debug(fmt, ##__VA_ARGS__)
  402. #else
  403. #define pr_debug(fmt, ...) \
  404. ({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
  405. #endif
  406. /*
  407. * ratelimited messages with local ratelimit_state,
  408. * no local ratelimit_state used in the !PRINTK case
  409. */
  410. #ifdef CONFIG_PRINTK
  411. #define printk_ratelimited(fmt, ...) ({ \
  412. static DEFINE_RATELIMIT_STATE(_rs, \
  413. DEFAULT_RATELIMIT_INTERVAL, \
  414. DEFAULT_RATELIMIT_BURST); \
  415. \
  416. if (__ratelimit(&_rs)) \
  417. printk(fmt, ##__VA_ARGS__); \
  418. })
  419. #else
  420. /* No effect, but we still get type checking even in the !PRINTK case: */
  421. #define printk_ratelimited printk
  422. #endif
  423. #define pr_emerg_ratelimited(fmt, ...) \
  424. printk_ratelimited(KERN_EMERG pr_fmt(fmt), ##__VA_ARGS__)
  425. #define pr_alert_ratelimited(fmt, ...) \
  426. printk_ratelimited(KERN_ALERT pr_fmt(fmt), ##__VA_ARGS__)
  427. #define pr_crit_ratelimited(fmt, ...) \
  428. printk_ratelimited(KERN_CRIT pr_fmt(fmt), ##__VA_ARGS__)
  429. #define pr_err_ratelimited(fmt, ...) \
  430. printk_ratelimited(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
  431. #define pr_warning_ratelimited(fmt, ...) \
  432. printk_ratelimited(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
  433. #define pr_warn_ratelimited pr_warning_ratelimited
  434. #define pr_notice_ratelimited(fmt, ...) \
  435. printk_ratelimited(KERN_NOTICE pr_fmt(fmt), ##__VA_ARGS__)
  436. #define pr_info_ratelimited(fmt, ...) \
  437. printk_ratelimited(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
  438. /* no pr_cont_ratelimited, don't do that... */
  439. /* If you are writing a driver, please use dev_dbg instead */
  440. #if defined(DEBUG)
  441. #define pr_debug_ratelimited(fmt, ...) \
  442. printk_ratelimited(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
  443. #else
  444. #define pr_debug_ratelimited(fmt, ...) \
  445. ({ if (0) printk_ratelimited(KERN_DEBUG pr_fmt(fmt), \
  446. ##__VA_ARGS__); 0; })
  447. #endif
  448. /*
  449. * General tracing related utility functions - trace_printk(),
  450. * tracing_on/tracing_off and tracing_start()/tracing_stop
  451. *
  452. * Use tracing_on/tracing_off when you want to quickly turn on or off
  453. * tracing. It simply enables or disables the recording of the trace events.
  454. * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
  455. * file, which gives a means for the kernel and userspace to interact.
  456. * Place a tracing_off() in the kernel where you want tracing to end.
  457. * From user space, examine the trace, and then echo 1 > tracing_on
  458. * to continue tracing.
  459. *
  460. * tracing_stop/tracing_start has slightly more overhead. It is used
  461. * by things like suspend to ram where disabling the recording of the
  462. * trace is not enough, but tracing must actually stop because things
  463. * like calling smp_processor_id() may crash the system.
  464. *
  465. * Most likely, you want to use tracing_on/tracing_off.
  466. */
  467. #ifdef CONFIG_RING_BUFFER
  468. void tracing_on(void);
  469. void tracing_off(void);
  470. /* trace_off_permanent stops recording with no way to bring it back */
  471. void tracing_off_permanent(void);
  472. int tracing_is_on(void);
  473. #else
  474. static inline void tracing_on(void) { }
  475. static inline void tracing_off(void) { }
  476. static inline void tracing_off_permanent(void) { }
  477. static inline int tracing_is_on(void) { return 0; }
  478. #endif
  479. enum ftrace_dump_mode {
  480. DUMP_NONE,
  481. DUMP_ALL,
  482. DUMP_ORIG,
  483. };
  484. #ifdef CONFIG_TRACING
  485. extern void tracing_start(void);
  486. extern void tracing_stop(void);
  487. extern void ftrace_off_permanent(void);
  488. static inline void __attribute__ ((format (printf, 1, 2)))
  489. ____trace_printk_check_format(const char *fmt, ...)
  490. {
  491. }
  492. #define __trace_printk_check_format(fmt, args...) \
  493. do { \
  494. if (0) \
  495. ____trace_printk_check_format(fmt, ##args); \
  496. } while (0)
  497. /**
  498. * trace_printk - printf formatting in the ftrace buffer
  499. * @fmt: the printf format for printing
  500. *
  501. * Note: __trace_printk is an internal function for trace_printk and
  502. * the @ip is passed in via the trace_printk macro.
  503. *
  504. * This function allows a kernel developer to debug fast path sections
  505. * that printk is not appropriate for. By scattering in various
  506. * printk like tracing in the code, a developer can quickly see
  507. * where problems are occurring.
  508. *
  509. * This is intended as a debugging tool for the developer only.
  510. * Please refrain from leaving trace_printks scattered around in
  511. * your code.
  512. */
  513. #define trace_printk(fmt, args...) \
  514. do { \
  515. __trace_printk_check_format(fmt, ##args); \
  516. if (__builtin_constant_p(fmt)) { \
  517. static const char *trace_printk_fmt \
  518. __attribute__((section("__trace_printk_fmt"))) = \
  519. __builtin_constant_p(fmt) ? fmt : NULL; \
  520. \
  521. __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
  522. } else \
  523. __trace_printk(_THIS_IP_, fmt, ##args); \
  524. } while (0)
  525. extern int
  526. __trace_bprintk(unsigned long ip, const char *fmt, ...)
  527. __attribute__ ((format (printf, 2, 3)));
  528. extern int
  529. __trace_printk(unsigned long ip, const char *fmt, ...)
  530. __attribute__ ((format (printf, 2, 3)));
  531. extern void trace_dump_stack(void);
  532. /*
  533. * The double __builtin_constant_p is because gcc will give us an error
  534. * if we try to allocate the static variable to fmt if it is not a
  535. * constant. Even with the outer if statement.
  536. */
  537. #define ftrace_vprintk(fmt, vargs) \
  538. do { \
  539. if (__builtin_constant_p(fmt)) { \
  540. static const char *trace_printk_fmt \
  541. __attribute__((section("__trace_printk_fmt"))) = \
  542. __builtin_constant_p(fmt) ? fmt : NULL; \
  543. \
  544. __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
  545. } else \
  546. __ftrace_vprintk(_THIS_IP_, fmt, vargs); \
  547. } while (0)
  548. extern int
  549. __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
  550. extern int
  551. __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
  552. extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
  553. #else
  554. static inline int
  555. trace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  556. static inline void tracing_start(void) { }
  557. static inline void tracing_stop(void) { }
  558. static inline void ftrace_off_permanent(void) { }
  559. static inline void trace_dump_stack(void) { }
  560. static inline int
  561. trace_printk(const char *fmt, ...)
  562. {
  563. return 0;
  564. }
  565. static inline int
  566. ftrace_vprintk(const char *fmt, va_list ap)
  567. {
  568. return 0;
  569. }
  570. static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  571. #endif /* CONFIG_TRACING */
  572. /*
  573. * min()/max()/clamp() macros that also do
  574. * strict type-checking.. See the
  575. * "unnecessary" pointer comparison.
  576. */
  577. #define min(x, y) ({ \
  578. typeof(x) _min1 = (x); \
  579. typeof(y) _min2 = (y); \
  580. (void) (&_min1 == &_min2); \
  581. _min1 < _min2 ? _min1 : _min2; })
  582. #define max(x, y) ({ \
  583. typeof(x) _max1 = (x); \
  584. typeof(y) _max2 = (y); \
  585. (void) (&_max1 == &_max2); \
  586. _max1 > _max2 ? _max1 : _max2; })
  587. #define min3(x, y, z) ({ \
  588. typeof(x) _min1 = (x); \
  589. typeof(y) _min2 = (y); \
  590. typeof(z) _min3 = (z); \
  591. (void) (&_min1 == &_min2); \
  592. (void) (&_min1 == &_min3); \
  593. _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
  594. (_min2 < _min3 ? _min2 : _min3); })
  595. #define max3(x, y, z) ({ \
  596. typeof(x) _max1 = (x); \
  597. typeof(y) _max2 = (y); \
  598. typeof(z) _max3 = (z); \
  599. (void) (&_max1 == &_max2); \
  600. (void) (&_max1 == &_max3); \
  601. _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
  602. (_max2 > _max3 ? _max2 : _max3); })
  603. /**
  604. * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  605. * @x: value1
  606. * @y: value2
  607. */
  608. #define min_not_zero(x, y) ({ \
  609. typeof(x) __x = (x); \
  610. typeof(y) __y = (y); \
  611. __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
  612. /**
  613. * clamp - return a value clamped to a given range with strict typechecking
  614. * @val: current value
  615. * @min: minimum allowable value
  616. * @max: maximum allowable value
  617. *
  618. * This macro does strict typechecking of min/max to make sure they are of the
  619. * same type as val. See the unnecessary pointer comparisons.
  620. */
  621. #define clamp(val, min, max) ({ \
  622. typeof(val) __val = (val); \
  623. typeof(min) __min = (min); \
  624. typeof(max) __max = (max); \
  625. (void) (&__val == &__min); \
  626. (void) (&__val == &__max); \
  627. __val = __val < __min ? __min: __val; \
  628. __val > __max ? __max: __val; })
  629. /*
  630. * ..and if you can't take the strict
  631. * types, you can specify one yourself.
  632. *
  633. * Or not use min/max/clamp at all, of course.
  634. */
  635. #define min_t(type, x, y) ({ \
  636. type __min1 = (x); \
  637. type __min2 = (y); \
  638. __min1 < __min2 ? __min1: __min2; })
  639. #define max_t(type, x, y) ({ \
  640. type __max1 = (x); \
  641. type __max2 = (y); \
  642. __max1 > __max2 ? __max1: __max2; })
  643. /**
  644. * clamp_t - return a value clamped to a given range using a given type
  645. * @type: the type of variable to use
  646. * @val: current value
  647. * @min: minimum allowable value
  648. * @max: maximum allowable value
  649. *
  650. * This macro does no typechecking and uses temporary variables of type
  651. * 'type' to make all the comparisons.
  652. */
  653. #define clamp_t(type, val, min, max) ({ \
  654. type __val = (val); \
  655. type __min = (min); \
  656. type __max = (max); \
  657. __val = __val < __min ? __min: __val; \
  658. __val > __max ? __max: __val; })
  659. /**
  660. * clamp_val - return a value clamped to a given range using val's type
  661. * @val: current value
  662. * @min: minimum allowable value
  663. * @max: maximum allowable value
  664. *
  665. * This macro does no typechecking and uses temporary variables of whatever
  666. * type the input argument 'val' is. This is useful when val is an unsigned
  667. * type and min and max are literals that will otherwise be assigned a signed
  668. * integer type.
  669. */
  670. #define clamp_val(val, min, max) ({ \
  671. typeof(val) __val = (val); \
  672. typeof(val) __min = (min); \
  673. typeof(val) __max = (max); \
  674. __val = __val < __min ? __min: __val; \
  675. __val > __max ? __max: __val; })
  676. /*
  677. * swap - swap value of @a and @b
  678. */
  679. #define swap(a, b) \
  680. do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  681. /**
  682. * container_of - cast a member of a structure out to the containing structure
  683. * @ptr: the pointer to the member.
  684. * @type: the type of the container struct this is embedded in.
  685. * @member: the name of the member within the struct.
  686. *
  687. */
  688. #define container_of(ptr, type, member) ({ \
  689. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  690. (type *)( (char *)__mptr - offsetof(type,member) );})
  691. struct sysinfo;
  692. extern int do_sysinfo(struct sysinfo *info);
  693. #endif /* __KERNEL__ */
  694. #define SI_LOAD_SHIFT 16
  695. struct sysinfo {
  696. long uptime; /* Seconds since boot */
  697. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  698. unsigned long totalram; /* Total usable main memory size */
  699. unsigned long freeram; /* Available memory size */
  700. unsigned long sharedram; /* Amount of shared memory */
  701. unsigned long bufferram; /* Memory used by buffers */
  702. unsigned long totalswap; /* Total swap space size */
  703. unsigned long freeswap; /* swap space still available */
  704. unsigned short procs; /* Number of current processes */
  705. unsigned short pad; /* explicit padding for m68k */
  706. unsigned long totalhigh; /* Total high memory size */
  707. unsigned long freehigh; /* Available high memory size */
  708. unsigned int mem_unit; /* Memory unit size in bytes */
  709. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  710. };
  711. /* Force a compilation error if condition is true */
  712. #define BUILD_BUG_ON(condition) ((void)BUILD_BUG_ON_ZERO(condition))
  713. /* Force a compilation error if condition is constant and true */
  714. #define MAYBE_BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
  715. /* Force a compilation error if a constant expression is not a power of 2 */
  716. #define BUILD_BUG_ON_NOT_POWER_OF_2(n) \
  717. BUILD_BUG_ON((n) == 0 || (((n) & ((n) - 1)) != 0))
  718. /* Force a compilation error if condition is true, but also produce a
  719. result (of value 0 and type size_t), so the expression can be used
  720. e.g. in a structure initializer (or where-ever else comma expressions
  721. aren't permitted). */
  722. #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
  723. #define BUILD_BUG_ON_NULL(e) ((void *)sizeof(struct { int:-!!(e); }))
  724. /* Trap pasters of __FUNCTION__ at compile-time */
  725. #define __FUNCTION__ (__func__)
  726. /* This helps us to avoid #ifdef CONFIG_NUMA */
  727. #ifdef CONFIG_NUMA
  728. #define NUMA_BUILD 1
  729. #else
  730. #define NUMA_BUILD 0
  731. #endif
  732. /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
  733. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  734. # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
  735. #endif
  736. #endif