kernel.h 24 KB

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