kernel.h 11 KB

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