kernel.h 10 KB

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