kernel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <asm/byteorder.h>
  15. #include <asm/bug.h>
  16. extern const char linux_banner[];
  17. extern const char linux_proc_banner[];
  18. #define INT_MAX ((int)(~0U>>1))
  19. #define INT_MIN (-INT_MAX - 1)
  20. #define UINT_MAX (~0U)
  21. #define LONG_MAX ((long)(~0UL>>1))
  22. #define LONG_MIN (-LONG_MAX - 1)
  23. #define ULONG_MAX (~0UL)
  24. #define LLONG_MAX ((long long)(~0ULL>>1))
  25. #define LLONG_MIN (-LLONG_MAX - 1)
  26. #define ULLONG_MAX (~0ULL)
  27. #define STACK_MAGIC 0xdeadbeef
  28. #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
  29. #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
  30. #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
  31. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  32. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  33. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  34. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  35. #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
  36. #ifdef CONFIG_LBD
  37. # include <asm/div64.h>
  38. # define sector_div(a, b) do_div(a, b)
  39. #else
  40. # define sector_div(n, b)( \
  41. { \
  42. int _res; \
  43. _res = (n) % (b); \
  44. (n) /= (b); \
  45. _res; \
  46. } \
  47. )
  48. #endif
  49. /**
  50. * upper_32_bits - return bits 32-63 of a number
  51. * @n: the number we're accessing
  52. *
  53. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  54. * the "right shift count >= width of type" warning when that quantity is
  55. * 32-bits.
  56. */
  57. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  58. #define KERN_EMERG "<0>" /* system is unusable */
  59. #define KERN_ALERT "<1>" /* action must be taken immediately */
  60. #define KERN_CRIT "<2>" /* critical conditions */
  61. #define KERN_ERR "<3>" /* error conditions */
  62. #define KERN_WARNING "<4>" /* warning conditions */
  63. #define KERN_NOTICE "<5>" /* normal but significant condition */
  64. #define KERN_INFO "<6>" /* informational */
  65. #define KERN_DEBUG "<7>" /* debug-level messages */
  66. /*
  67. * Annotation for a "continued" line of log printout (only done after a
  68. * line that had no enclosing \n). Only to be used by core/arch code
  69. * during early bootup (a continued line is not SMP-safe otherwise).
  70. */
  71. #define KERN_CONT ""
  72. extern int console_printk[];
  73. #define console_loglevel (console_printk[0])
  74. #define default_message_loglevel (console_printk[1])
  75. #define minimum_console_loglevel (console_printk[2])
  76. #define default_console_loglevel (console_printk[3])
  77. struct completion;
  78. struct pt_regs;
  79. struct user;
  80. /**
  81. * might_sleep - annotation for functions that can sleep
  82. *
  83. * this macro will print a stack trace if it is executed in an atomic
  84. * context (spinlock, irq-handler, ...).
  85. *
  86. * This is a useful debugging help to be able to catch problems early and not
  87. * be bitten later when the calling function happens to sleep when it is not
  88. * supposed to.
  89. */
  90. #ifdef CONFIG_PREEMPT_VOLUNTARY
  91. extern int _cond_resched(void);
  92. # define might_resched() _cond_resched()
  93. #else
  94. # define might_resched() do { } while (0)
  95. #endif
  96. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  97. void __might_sleep(char *file, int line);
  98. # define might_sleep() \
  99. do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
  100. #else
  101. # define might_sleep() do { might_resched(); } while (0)
  102. #endif
  103. #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
  104. #define abs(x) ({ \
  105. int __x = (x); \
  106. (__x < 0) ? -__x : __x; \
  107. })
  108. extern struct atomic_notifier_head panic_notifier_list;
  109. extern long (*panic_blink)(long time);
  110. NORET_TYPE void panic(const char * fmt, ...)
  111. __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
  112. extern void oops_enter(void);
  113. extern void oops_exit(void);
  114. extern int oops_may_print(void);
  115. NORET_TYPE void do_exit(long error_code)
  116. ATTRIB_NORET;
  117. NORET_TYPE void complete_and_exit(struct completion *, long)
  118. ATTRIB_NORET;
  119. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  120. extern long simple_strtol(const char *,char **,unsigned int);
  121. extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
  122. extern long long simple_strtoll(const char *,char **,unsigned int);
  123. extern int strict_strtoul(const char *, unsigned int, unsigned long *);
  124. extern int strict_strtol(const char *, unsigned int, long *);
  125. extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
  126. extern int strict_strtoll(const char *, unsigned int, long long *);
  127. extern int sprintf(char * buf, const char * fmt, ...)
  128. __attribute__ ((format (printf, 2, 3)));
  129. extern int vsprintf(char *buf, const char *, va_list)
  130. __attribute__ ((format (printf, 2, 0)));
  131. extern int snprintf(char * buf, size_t size, const char * fmt, ...)
  132. __attribute__ ((format (printf, 3, 4)));
  133. extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  134. __attribute__ ((format (printf, 3, 0)));
  135. extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
  136. __attribute__ ((format (printf, 3, 4)));
  137. extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  138. __attribute__ ((format (printf, 3, 0)));
  139. extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
  140. __attribute__ ((format (printf, 2, 3)));
  141. extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  142. extern int sscanf(const char *, const char *, ...)
  143. __attribute__ ((format (scanf, 2, 3)));
  144. extern int vsscanf(const char *, const char *, va_list)
  145. __attribute__ ((format (scanf, 2, 0)));
  146. extern int get_option(char **str, int *pint);
  147. extern char *get_options(const char *str, int nints, int *ints);
  148. extern unsigned long long memparse(char *ptr, char **retptr);
  149. extern int core_kernel_text(unsigned long addr);
  150. extern int __kernel_text_address(unsigned long addr);
  151. extern int kernel_text_address(unsigned long addr);
  152. struct pid;
  153. extern struct pid *session_of_pgrp(struct pid *pgrp);
  154. #ifdef CONFIG_PRINTK
  155. asmlinkage int vprintk(const char *fmt, va_list args)
  156. __attribute__ ((format (printf, 1, 0)));
  157. asmlinkage int printk(const char * fmt, ...)
  158. __attribute__ ((format (printf, 1, 2))) __cold;
  159. extern int log_buf_get_len(void);
  160. extern int log_buf_read(int idx);
  161. extern int log_buf_copy(char *dest, int idx, int len);
  162. extern int printk_ratelimit_jiffies;
  163. extern int printk_ratelimit_burst;
  164. extern int printk_ratelimit(void);
  165. extern int __printk_ratelimit(int ratelimit_jiffies, int ratelimit_burst);
  166. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  167. unsigned int interval_msec);
  168. #else
  169. static inline int vprintk(const char *s, va_list args)
  170. __attribute__ ((format (printf, 1, 0)));
  171. static inline int vprintk(const char *s, va_list args) { return 0; }
  172. static inline int printk(const char *s, ...)
  173. __attribute__ ((format (printf, 1, 2)));
  174. static inline int __cold printk(const char *s, ...) { return 0; }
  175. static inline int log_buf_get_len(void) { return 0; }
  176. static inline int log_buf_read(int idx) { return 0; }
  177. static inline int log_buf_copy(char *dest, int idx, int len) { return 0; }
  178. static inline int printk_ratelimit(void) { return 0; }
  179. static inline int __printk_ratelimit(int ratelimit_jiffies, \
  180. int ratelimit_burst) { return 0; }
  181. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
  182. unsigned int interval_msec) \
  183. { return false; }
  184. #endif
  185. extern void __attribute__((format(printf, 1, 2)))
  186. early_printk(const char *fmt, ...);
  187. unsigned long int_sqrt(unsigned long);
  188. static inline void console_silent(void)
  189. {
  190. console_loglevel = 0;
  191. }
  192. static inline void console_verbose(void)
  193. {
  194. if (console_loglevel)
  195. console_loglevel = 15;
  196. }
  197. extern void bust_spinlocks(int yes);
  198. extern void wake_up_klogd(void);
  199. extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
  200. extern int panic_timeout;
  201. extern int panic_on_oops;
  202. extern int panic_on_unrecovered_nmi;
  203. extern int tainted;
  204. extern const char *print_tainted(void);
  205. extern void add_taint(unsigned);
  206. extern int root_mountflags;
  207. /* Values used for system_state */
  208. extern enum system_states {
  209. SYSTEM_BOOTING,
  210. SYSTEM_RUNNING,
  211. SYSTEM_HALT,
  212. SYSTEM_POWER_OFF,
  213. SYSTEM_RESTART,
  214. SYSTEM_SUSPEND_DISK,
  215. } system_state;
  216. #define TAINT_PROPRIETARY_MODULE (1<<0)
  217. #define TAINT_FORCED_MODULE (1<<1)
  218. #define TAINT_UNSAFE_SMP (1<<2)
  219. #define TAINT_FORCED_RMMOD (1<<3)
  220. #define TAINT_MACHINE_CHECK (1<<4)
  221. #define TAINT_BAD_PAGE (1<<5)
  222. #define TAINT_USER (1<<6)
  223. #define TAINT_DIE (1<<7)
  224. #define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8)
  225. extern void dump_stack(void) __cold;
  226. enum {
  227. DUMP_PREFIX_NONE,
  228. DUMP_PREFIX_ADDRESS,
  229. DUMP_PREFIX_OFFSET
  230. };
  231. extern void hex_dump_to_buffer(const void *buf, size_t len,
  232. int rowsize, int groupsize,
  233. char *linebuf, size_t linebuflen, bool ascii);
  234. extern void print_hex_dump(const char *level, const char *prefix_str,
  235. int prefix_type, int rowsize, int groupsize,
  236. const void *buf, size_t len, bool ascii);
  237. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  238. const void *buf, size_t len);
  239. #define hex_asc(x) "0123456789abcdef"[x]
  240. #define pr_emerg(fmt, arg...) \
  241. printk(KERN_EMERG fmt, ##arg)
  242. #define pr_alert(fmt, arg...) \
  243. printk(KERN_ALERT fmt, ##arg)
  244. #define pr_crit(fmt, arg...) \
  245. printk(KERN_CRIT fmt, ##arg)
  246. #define pr_err(fmt, arg...) \
  247. printk(KERN_ERR fmt, ##arg)
  248. #define pr_warning(fmt, arg...) \
  249. printk(KERN_WARNING fmt, ##arg)
  250. #define pr_notice(fmt, arg...) \
  251. printk(KERN_NOTICE fmt, ##arg)
  252. #define pr_info(fmt, arg...) \
  253. printk(KERN_INFO fmt, ##arg)
  254. #ifdef DEBUG
  255. /* If you are writing a driver, please use dev_dbg instead */
  256. #define pr_debug(fmt, arg...) \
  257. printk(KERN_DEBUG fmt, ##arg)
  258. #else
  259. static inline int __attribute__ ((format (printf, 1, 2))) pr_debug(const char * fmt, ...)
  260. {
  261. return 0;
  262. }
  263. #endif
  264. /*
  265. * Display an IP address in readable format.
  266. */
  267. #define NIPQUAD(addr) \
  268. ((unsigned char *)&addr)[0], \
  269. ((unsigned char *)&addr)[1], \
  270. ((unsigned char *)&addr)[2], \
  271. ((unsigned char *)&addr)[3]
  272. #define NIPQUAD_FMT "%u.%u.%u.%u"
  273. #define NIP6(addr) \
  274. ntohs((addr).s6_addr16[0]), \
  275. ntohs((addr).s6_addr16[1]), \
  276. ntohs((addr).s6_addr16[2]), \
  277. ntohs((addr).s6_addr16[3]), \
  278. ntohs((addr).s6_addr16[4]), \
  279. ntohs((addr).s6_addr16[5]), \
  280. ntohs((addr).s6_addr16[6]), \
  281. ntohs((addr).s6_addr16[7])
  282. #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
  283. #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"
  284. #if defined(__LITTLE_ENDIAN)
  285. #define HIPQUAD(addr) \
  286. ((unsigned char *)&addr)[3], \
  287. ((unsigned char *)&addr)[2], \
  288. ((unsigned char *)&addr)[1], \
  289. ((unsigned char *)&addr)[0]
  290. #elif defined(__BIG_ENDIAN)
  291. #define HIPQUAD NIPQUAD
  292. #else
  293. #error "Please fix asm/byteorder.h"
  294. #endif /* __LITTLE_ENDIAN */
  295. /*
  296. * min()/max() macros that also do
  297. * strict type-checking.. See the
  298. * "unnecessary" pointer comparison.
  299. */
  300. #define min(x,y) ({ \
  301. typeof(x) _x = (x); \
  302. typeof(y) _y = (y); \
  303. (void) (&_x == &_y); \
  304. _x < _y ? _x : _y; })
  305. #define max(x,y) ({ \
  306. typeof(x) _x = (x); \
  307. typeof(y) _y = (y); \
  308. (void) (&_x == &_y); \
  309. _x > _y ? _x : _y; })
  310. /*
  311. * ..and if you can't take the strict
  312. * types, you can specify one yourself.
  313. *
  314. * Or not use min/max at all, of course.
  315. */
  316. #define min_t(type,x,y) \
  317. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  318. #define max_t(type,x,y) \
  319. ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
  320. /**
  321. * container_of - cast a member of a structure out to the containing structure
  322. * @ptr: the pointer to the member.
  323. * @type: the type of the container struct this is embedded in.
  324. * @member: the name of the member within the struct.
  325. *
  326. */
  327. #define container_of(ptr, type, member) ({ \
  328. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  329. (type *)( (char *)__mptr - offsetof(type,member) );})
  330. /*
  331. * Check at compile time that something is of a particular type.
  332. * Always evaluates to 1 so you may use it easily in comparisons.
  333. */
  334. #define typecheck(type,x) \
  335. ({ type __dummy; \
  336. typeof(x) __dummy2; \
  337. (void)(&__dummy == &__dummy2); \
  338. 1; \
  339. })
  340. /*
  341. * Check at compile time that 'function' is a certain type, or is a pointer
  342. * to that type (needs to use typedef for the function type.)
  343. */
  344. #define typecheck_fn(type,function) \
  345. ({ typeof(type) __tmp = function; \
  346. (void)__tmp; \
  347. })
  348. struct sysinfo;
  349. extern int do_sysinfo(struct sysinfo *info);
  350. #endif /* __KERNEL__ */
  351. #define SI_LOAD_SHIFT 16
  352. struct sysinfo {
  353. long uptime; /* Seconds since boot */
  354. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  355. unsigned long totalram; /* Total usable main memory size */
  356. unsigned long freeram; /* Available memory size */
  357. unsigned long sharedram; /* Amount of shared memory */
  358. unsigned long bufferram; /* Memory used by buffers */
  359. unsigned long totalswap; /* Total swap space size */
  360. unsigned long freeswap; /* swap space still available */
  361. unsigned short procs; /* Number of current processes */
  362. unsigned short pad; /* explicit padding for m68k */
  363. unsigned long totalhigh; /* Total high memory size */
  364. unsigned long freehigh; /* Available high memory size */
  365. unsigned int mem_unit; /* Memory unit size in bytes */
  366. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  367. };
  368. /* Force a compilation error if condition is true */
  369. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  370. /* Force a compilation error if condition is true, but also produce a
  371. result (of value 0 and type size_t), so the expression can be used
  372. e.g. in a structure initializer (or where-ever else comma expressions
  373. aren't permitted). */
  374. #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
  375. /* Trap pasters of __FUNCTION__ at compile-time */
  376. #define __FUNCTION__ (__func__)
  377. /* This helps us to avoid #ifdef CONFIG_NUMA */
  378. #ifdef CONFIG_NUMA
  379. #define NUMA_BUILD 1
  380. #else
  381. #define NUMA_BUILD 0
  382. #endif
  383. #endif