kernel.h 11 KB

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