kernel.h 9.7 KB

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