kernel.h 16 KB

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