kernel.h 16 KB

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