kernel.h 11 KB

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