kernel.h 9.6 KB

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