kernel.h 9.0 KB

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