kernel.h 25 KB

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