kernel.h 16 KB

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