kernel.h 24 KB

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