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