kernel.h 24 KB

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