kernel.h 24 KB

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