kernel.h 12 KB

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