kernel.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 <linux/typecheck.h>
  15. #include <linux/ratelimit.h>
  16. #include <asm/byteorder.h>
  17. #include <asm/bug.h>
  18. extern const char linux_banner[];
  19. extern const char linux_proc_banner[];
  20. #define USHORT_MAX ((u16)(~0U))
  21. #define SHORT_MAX ((s16)(USHORT_MAX>>1))
  22. #define SHORT_MIN (-SHORT_MAX - 1)
  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 PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
  36. #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
  37. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  38. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  39. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  40. #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
  41. #define _RET_IP_ (unsigned long)__builtin_return_address(0)
  42. #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
  43. #ifdef CONFIG_LBD
  44. # include <asm/div64.h>
  45. # define sector_div(a, b) do_div(a, b)
  46. #else
  47. # define sector_div(n, b)( \
  48. { \
  49. int _res; \
  50. _res = (n) % (b); \
  51. (n) /= (b); \
  52. _res; \
  53. } \
  54. )
  55. #endif
  56. /**
  57. * upper_32_bits - return bits 32-63 of a number
  58. * @n: the number we're accessing
  59. *
  60. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  61. * the "right shift count >= width of type" warning when that quantity is
  62. * 32-bits.
  63. */
  64. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  65. #define KERN_EMERG "<0>" /* system is unusable */
  66. #define KERN_ALERT "<1>" /* action must be taken immediately */
  67. #define KERN_CRIT "<2>" /* critical conditions */
  68. #define KERN_ERR "<3>" /* error conditions */
  69. #define KERN_WARNING "<4>" /* warning conditions */
  70. #define KERN_NOTICE "<5>" /* normal but significant condition */
  71. #define KERN_INFO "<6>" /* informational */
  72. #define KERN_DEBUG "<7>" /* debug-level messages */
  73. /*
  74. * Annotation for a "continued" line of log printout (only done after a
  75. * line that had no enclosing \n). Only to be used by core/arch code
  76. * during early bootup (a continued line is not SMP-safe otherwise).
  77. */
  78. #define KERN_CONT ""
  79. extern int console_printk[];
  80. #define console_loglevel (console_printk[0])
  81. #define default_message_loglevel (console_printk[1])
  82. #define minimum_console_loglevel (console_printk[2])
  83. #define default_console_loglevel (console_printk[3])
  84. struct completion;
  85. struct pt_regs;
  86. struct user;
  87. /**
  88. * might_sleep - annotation for functions that can sleep
  89. *
  90. * this macro will print a stack trace if it is executed in an atomic
  91. * context (spinlock, irq-handler, ...).
  92. *
  93. * This is a useful debugging help to be able to catch problems early and not
  94. * be bitten later when the calling function happens to sleep when it is not
  95. * supposed to.
  96. */
  97. #ifdef CONFIG_PREEMPT_VOLUNTARY
  98. extern int _cond_resched(void);
  99. # define might_resched() _cond_resched()
  100. #else
  101. # define might_resched() do { } while (0)
  102. #endif
  103. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  104. void __might_sleep(char *file, int line);
  105. # define might_sleep() \
  106. do { __might_sleep(__FILE__, __LINE__); might_resched(); } while (0)
  107. #else
  108. # define might_sleep() do { might_resched(); } while (0)
  109. #endif
  110. #define might_sleep_if(cond) do { if (cond) might_sleep(); } while (0)
  111. #define abs(x) ({ \
  112. int __x = (x); \
  113. (__x < 0) ? -__x : __x; \
  114. })
  115. extern struct atomic_notifier_head panic_notifier_list;
  116. extern long (*panic_blink)(long time);
  117. NORET_TYPE void panic(const char * fmt, ...)
  118. __attribute__ ((NORET_AND format (printf, 1, 2))) __cold;
  119. extern void oops_enter(void);
  120. extern void oops_exit(void);
  121. extern int oops_may_print(void);
  122. NORET_TYPE void do_exit(long error_code)
  123. ATTRIB_NORET;
  124. NORET_TYPE void complete_and_exit(struct completion *, long)
  125. ATTRIB_NORET;
  126. extern unsigned long simple_strtoul(const char *,char **,unsigned int);
  127. extern long simple_strtol(const char *,char **,unsigned int);
  128. extern unsigned long long simple_strtoull(const char *,char **,unsigned int);
  129. extern long long simple_strtoll(const char *,char **,unsigned int);
  130. extern int strict_strtoul(const char *, unsigned int, unsigned long *);
  131. extern int strict_strtol(const char *, unsigned int, long *);
  132. extern int strict_strtoull(const char *, unsigned int, unsigned long long *);
  133. extern int strict_strtoll(const char *, unsigned int, long long *);
  134. extern int sprintf(char * buf, const char * fmt, ...)
  135. __attribute__ ((format (printf, 2, 3)));
  136. extern int vsprintf(char *buf, const char *, va_list)
  137. __attribute__ ((format (printf, 2, 0)));
  138. extern int snprintf(char * buf, size_t size, const char * fmt, ...)
  139. __attribute__ ((format (printf, 3, 4)));
  140. extern int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  141. __attribute__ ((format (printf, 3, 0)));
  142. extern int scnprintf(char * buf, size_t size, const char * fmt, ...)
  143. __attribute__ ((format (printf, 3, 4)));
  144. extern int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  145. __attribute__ ((format (printf, 3, 0)));
  146. extern char *kasprintf(gfp_t gfp, const char *fmt, ...)
  147. __attribute__ ((format (printf, 2, 3)));
  148. extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  149. extern int sscanf(const char *, const char *, ...)
  150. __attribute__ ((format (scanf, 2, 3)));
  151. extern int vsscanf(const char *, const char *, va_list)
  152. __attribute__ ((format (scanf, 2, 0)));
  153. extern int get_option(char **str, int *pint);
  154. extern char *get_options(const char *str, int nints, int *ints);
  155. extern unsigned long long memparse(const char *ptr, char **retptr);
  156. extern int core_kernel_text(unsigned long addr);
  157. extern int __kernel_text_address(unsigned long addr);
  158. extern int kernel_text_address(unsigned long addr);
  159. struct pid;
  160. extern struct pid *session_of_pgrp(struct pid *pgrp);
  161. #ifdef CONFIG_PRINTK
  162. asmlinkage int vprintk(const char *fmt, va_list args)
  163. __attribute__ ((format (printf, 1, 0)));
  164. asmlinkage int printk(const char * fmt, ...)
  165. __attribute__ ((format (printf, 1, 2))) __cold;
  166. extern struct ratelimit_state printk_ratelimit_state;
  167. extern int printk_ratelimit(void);
  168. extern bool printk_timed_ratelimit(unsigned long *caller_jiffies,
  169. unsigned int interval_msec);
  170. #else
  171. static inline int vprintk(const char *s, va_list args)
  172. __attribute__ ((format (printf, 1, 0)));
  173. static inline int vprintk(const char *s, va_list args) { return 0; }
  174. static inline int printk(const char *s, ...)
  175. __attribute__ ((format (printf, 1, 2)));
  176. static inline int __cold printk(const char *s, ...) { return 0; }
  177. static inline int printk_ratelimit(void) { return 0; }
  178. static inline bool printk_timed_ratelimit(unsigned long *caller_jiffies, \
  179. unsigned int interval_msec) \
  180. { return false; }
  181. #endif
  182. extern void asmlinkage __attribute__((format(printf, 1, 2)))
  183. early_printk(const char *fmt, ...);
  184. unsigned long int_sqrt(unsigned long);
  185. static inline void console_silent(void)
  186. {
  187. console_loglevel = 0;
  188. }
  189. static inline void console_verbose(void)
  190. {
  191. if (console_loglevel)
  192. console_loglevel = 15;
  193. }
  194. extern void bust_spinlocks(int yes);
  195. extern void wake_up_klogd(void);
  196. extern int oops_in_progress; /* If set, an oops, panic(), BUG() or die() is in progress */
  197. extern int panic_timeout;
  198. extern int panic_on_oops;
  199. extern int panic_on_unrecovered_nmi;
  200. extern int tainted;
  201. extern const char *print_tainted(void);
  202. extern void add_taint(unsigned);
  203. extern int root_mountflags;
  204. /* Values used for system_state */
  205. extern enum system_states {
  206. SYSTEM_BOOTING,
  207. SYSTEM_RUNNING,
  208. SYSTEM_HALT,
  209. SYSTEM_POWER_OFF,
  210. SYSTEM_RESTART,
  211. SYSTEM_SUSPEND_DISK,
  212. } system_state;
  213. #define TAINT_PROPRIETARY_MODULE (1<<0)
  214. #define TAINT_FORCED_MODULE (1<<1)
  215. #define TAINT_UNSAFE_SMP (1<<2)
  216. #define TAINT_FORCED_RMMOD (1<<3)
  217. #define TAINT_MACHINE_CHECK (1<<4)
  218. #define TAINT_BAD_PAGE (1<<5)
  219. #define TAINT_USER (1<<6)
  220. #define TAINT_DIE (1<<7)
  221. #define TAINT_OVERRIDDEN_ACPI_TABLE (1<<8)
  222. #define TAINT_WARN (1<<9)
  223. extern void dump_stack(void) __cold;
  224. enum {
  225. DUMP_PREFIX_NONE,
  226. DUMP_PREFIX_ADDRESS,
  227. DUMP_PREFIX_OFFSET
  228. };
  229. extern void hex_dump_to_buffer(const void *buf, size_t len,
  230. int rowsize, int groupsize,
  231. char *linebuf, size_t linebuflen, bool ascii);
  232. extern void print_hex_dump(const char *level, const char *prefix_str,
  233. int prefix_type, int rowsize, int groupsize,
  234. const void *buf, size_t len, bool ascii);
  235. extern void print_hex_dump_bytes(const char *prefix_str, int prefix_type,
  236. const void *buf, size_t len);
  237. extern const char hex_asc[];
  238. #define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
  239. #define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
  240. static inline char *pack_hex_byte(char *buf, u8 byte)
  241. {
  242. *buf++ = hex_asc_hi(byte);
  243. *buf++ = hex_asc_lo(byte);
  244. return buf;
  245. }
  246. #define pr_emerg(fmt, arg...) \
  247. printk(KERN_EMERG fmt, ##arg)
  248. #define pr_alert(fmt, arg...) \
  249. printk(KERN_ALERT fmt, ##arg)
  250. #define pr_crit(fmt, arg...) \
  251. printk(KERN_CRIT fmt, ##arg)
  252. #define pr_err(fmt, arg...) \
  253. printk(KERN_ERR fmt, ##arg)
  254. #define pr_warning(fmt, arg...) \
  255. printk(KERN_WARNING fmt, ##arg)
  256. #define pr_notice(fmt, arg...) \
  257. printk(KERN_NOTICE fmt, ##arg)
  258. #define pr_info(fmt, arg...) \
  259. printk(KERN_INFO fmt, ##arg)
  260. #ifdef DEBUG
  261. /* If you are writing a driver, please use dev_dbg instead */
  262. #define pr_debug(fmt, arg...) \
  263. printk(KERN_DEBUG fmt, ##arg)
  264. #else
  265. #define pr_debug(fmt, arg...) \
  266. ({ if (0) printk(KERN_DEBUG fmt, ##arg); 0; })
  267. #endif
  268. /*
  269. * Display an IP address in readable format.
  270. */
  271. #define NIPQUAD(addr) \
  272. ((unsigned char *)&addr)[0], \
  273. ((unsigned char *)&addr)[1], \
  274. ((unsigned char *)&addr)[2], \
  275. ((unsigned char *)&addr)[3]
  276. #define NIPQUAD_FMT "%u.%u.%u.%u"
  277. #define NIP6(addr) \
  278. ntohs((addr).s6_addr16[0]), \
  279. ntohs((addr).s6_addr16[1]), \
  280. ntohs((addr).s6_addr16[2]), \
  281. ntohs((addr).s6_addr16[3]), \
  282. ntohs((addr).s6_addr16[4]), \
  283. ntohs((addr).s6_addr16[5]), \
  284. ntohs((addr).s6_addr16[6]), \
  285. ntohs((addr).s6_addr16[7])
  286. #define NIP6_FMT "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x"
  287. #define NIP6_SEQFMT "%04x%04x%04x%04x%04x%04x%04x%04x"
  288. #if defined(__LITTLE_ENDIAN)
  289. #define HIPQUAD(addr) \
  290. ((unsigned char *)&addr)[3], \
  291. ((unsigned char *)&addr)[2], \
  292. ((unsigned char *)&addr)[1], \
  293. ((unsigned char *)&addr)[0]
  294. #elif defined(__BIG_ENDIAN)
  295. #define HIPQUAD NIPQUAD
  296. #else
  297. #error "Please fix asm/byteorder.h"
  298. #endif /* __LITTLE_ENDIAN */
  299. /*
  300. * min()/max()/clamp() macros that also do
  301. * strict type-checking.. See the
  302. * "unnecessary" pointer comparison.
  303. */
  304. #define min(x, y) ({ \
  305. typeof(x) _min1 = (x); \
  306. typeof(y) _min2 = (y); \
  307. (void) (&_min1 == &_min2); \
  308. _min1 < _min2 ? _min1 : _min2; })
  309. #define max(x, y) ({ \
  310. typeof(x) _max1 = (x); \
  311. typeof(y) _max2 = (y); \
  312. (void) (&_max1 == &_max2); \
  313. _max1 > _max2 ? _max1 : _max2; })
  314. /**
  315. * clamp - return a value clamped to a given range with strict typechecking
  316. * @val: current value
  317. * @min: minimum allowable value
  318. * @max: maximum allowable value
  319. *
  320. * This macro does strict typechecking of min/max to make sure they are of the
  321. * same type as val. See the unnecessary pointer comparisons.
  322. */
  323. #define clamp(val, min, max) ({ \
  324. typeof(val) __val = (val); \
  325. typeof(min) __min = (min); \
  326. typeof(max) __max = (max); \
  327. (void) (&__val == &__min); \
  328. (void) (&__val == &__max); \
  329. __val = __val < __min ? __min: __val; \
  330. __val > __max ? __max: __val; })
  331. /*
  332. * ..and if you can't take the strict
  333. * types, you can specify one yourself.
  334. *
  335. * Or not use min/max/clamp at all, of course.
  336. */
  337. #define min_t(type, x, y) ({ \
  338. type __min1 = (x); \
  339. type __min2 = (y); \
  340. __min1 < __min2 ? __min1: __min2; })
  341. #define max_t(type, x, y) ({ \
  342. type __max1 = (x); \
  343. type __max2 = (y); \
  344. __max1 > __max2 ? __max1: __max2; })
  345. /**
  346. * clamp_t - return a value clamped to a given range using a given type
  347. * @type: the type of variable to use
  348. * @val: current value
  349. * @min: minimum allowable value
  350. * @max: maximum allowable value
  351. *
  352. * This macro does no typechecking and uses temporary variables of type
  353. * 'type' to make all the comparisons.
  354. */
  355. #define clamp_t(type, val, min, max) ({ \
  356. type __val = (val); \
  357. type __min = (min); \
  358. type __max = (max); \
  359. __val = __val < __min ? __min: __val; \
  360. __val > __max ? __max: __val; })
  361. /**
  362. * clamp_val - return a value clamped to a given range using val's type
  363. * @val: current value
  364. * @min: minimum allowable value
  365. * @max: maximum allowable value
  366. *
  367. * This macro does no typechecking and uses temporary variables of whatever
  368. * type the input argument 'val' is. This is useful when val is an unsigned
  369. * type and min and max are literals that will otherwise be assigned a signed
  370. * integer type.
  371. */
  372. #define clamp_val(val, min, max) ({ \
  373. typeof(val) __val = (val); \
  374. typeof(val) __min = (min); \
  375. typeof(val) __max = (max); \
  376. __val = __val < __min ? __min: __val; \
  377. __val > __max ? __max: __val; })
  378. /**
  379. * container_of - cast a member of a structure out to the containing structure
  380. * @ptr: the pointer to the member.
  381. * @type: the type of the container struct this is embedded in.
  382. * @member: the name of the member within the struct.
  383. *
  384. */
  385. #define container_of(ptr, type, member) ({ \
  386. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  387. (type *)( (char *)__mptr - offsetof(type,member) );})
  388. struct sysinfo;
  389. extern int do_sysinfo(struct sysinfo *info);
  390. #endif /* __KERNEL__ */
  391. #define SI_LOAD_SHIFT 16
  392. struct sysinfo {
  393. long uptime; /* Seconds since boot */
  394. unsigned long loads[3]; /* 1, 5, and 15 minute load averages */
  395. unsigned long totalram; /* Total usable main memory size */
  396. unsigned long freeram; /* Available memory size */
  397. unsigned long sharedram; /* Amount of shared memory */
  398. unsigned long bufferram; /* Memory used by buffers */
  399. unsigned long totalswap; /* Total swap space size */
  400. unsigned long freeswap; /* swap space still available */
  401. unsigned short procs; /* Number of current processes */
  402. unsigned short pad; /* explicit padding for m68k */
  403. unsigned long totalhigh; /* Total high memory size */
  404. unsigned long freehigh; /* Available high memory size */
  405. unsigned int mem_unit; /* Memory unit size in bytes */
  406. char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding: libc5 uses this.. */
  407. };
  408. /* Force a compilation error if condition is true */
  409. #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
  410. /* Force a compilation error if condition is true, but also produce a
  411. result (of value 0 and type size_t), so the expression can be used
  412. e.g. in a structure initializer (or where-ever else comma expressions
  413. aren't permitted). */
  414. #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
  415. /* Trap pasters of __FUNCTION__ at compile-time */
  416. #define __FUNCTION__ (__func__)
  417. /* This helps us to avoid #ifdef CONFIG_NUMA */
  418. #ifdef CONFIG_NUMA
  419. #define NUMA_BUILD 1
  420. #else
  421. #define NUMA_BUILD 0
  422. #endif
  423. #endif