Subversion Repositories Kolibri OS

Rev

Rev 5270 | Rev 6082 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef _LINUX_KERNEL_H
  2. #define _LINUX_KERNEL_H
  3.  
  4.  
  5. #include <stdarg.h>
  6. #include <linux/linkage.h>
  7. #include <linux/stddef.h>
  8. #include <linux/types.h>
  9. #include <linux/compiler.h>
  10. #include <linux/bitops.h>
  11. #include <linux/log2.h>
  12. #include <linux/typecheck.h>
  13. #include <linux/printk.h>
  14. #include <asm/byteorder.h>
  15. #include <uapi/linux/kernel.h>
  16.  
  17. #define USHRT_MAX       ((u16)(~0U))
  18. #define SHRT_MAX        ((s16)(USHRT_MAX>>1))
  19. #define SHRT_MIN        ((s16)(-SHRT_MAX - 1))
  20. #define INT_MAX     ((int)(~0U>>1))
  21. #define INT_MIN     (-INT_MAX - 1)
  22. #define UINT_MAX    (~0U)
  23. #define LONG_MAX    ((long)(~0UL>>1))
  24. #define LONG_MIN    (-LONG_MAX - 1)
  25. #define ULONG_MAX   (~0UL)
  26. #define LLONG_MAX   ((long long)(~0ULL>>1))
  27. #define LLONG_MIN   (-LLONG_MAX - 1)
  28. #define ULLONG_MAX  (~0ULL)
  29. #define SIZE_MAX        (~(size_t)0)
  30.  
  31. #define U8_MAX          ((u8)~0U)
  32. #define S8_MAX          ((s8)(U8_MAX>>1))
  33. #define S8_MIN          ((s8)(-S8_MAX - 1))
  34. #define U16_MAX         ((u16)~0U)
  35. #define S16_MAX         ((s16)(U16_MAX>>1))
  36. #define S16_MIN         ((s16)(-S16_MAX - 1))
  37. #define U32_MAX         ((u32)~0U)
  38. #define S32_MAX         ((s32)(U32_MAX>>1))
  39. #define S32_MIN         ((s32)(-S32_MAX - 1))
  40. #define U64_MAX         ((u64)~0ULL)
  41. #define S64_MAX         ((s64)(U64_MAX>>1))
  42. #define S64_MIN         ((s64)(-S64_MAX - 1))
  43.  
  44. #define STACK_MAGIC     0xdeadbeef
  45.  
  46. #define REPEAT_BYTE(x)  ((~0ul / 0xff) * (x))
  47.  
  48. #define ALIGN(x, a)             __ALIGN_KERNEL((x), (a))
  49. #define __ALIGN_MASK(x, mask)   __ALIGN_KERNEL_MASK((x), (mask))
  50. #define PTR_ALIGN(p, a)     ((typeof(p))ALIGN((unsigned long)(p), (a)))
  51. #define IS_ALIGNED(x, a)        (((x) & ((typeof(x))(a) - 1)) == 0)
  52.  
  53. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
  54.  
  55. /*
  56.  * This looks more complex than it should be. But we need to
  57.  * get the type for the ~ right in round_down (it needs to be
  58.  * as wide as the result!), and we want to evaluate the macro
  59.  * arguments just once each.
  60.  */
  61. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  62. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  63. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  64.  
  65. #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
  66. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  67. #define DIV_ROUND_UP_ULL(ll,d) \
  68.         ({ unsigned long long _tmp = (ll)+(d)-1; do_div(_tmp, d); _tmp; })
  69.  
  70. #if BITS_PER_LONG == 32
  71. # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
  72. #else
  73. # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
  74. #endif
  75.  
  76. /* The `const' in roundup() prevents gcc-3.3 from calling __divdi3 */
  77. #define roundup(x, y) (                                 \
  78. {                                                       \
  79.         const typeof(y) __y = y;                        \
  80.         (((x) + (__y - 1)) / __y) * __y;                \
  81. }                                                       \
  82. )
  83. #define rounddown(x, y) (                               \
  84. {                                                       \
  85.         typeof(x) __x = (x);                            \
  86.         __x - (__x % (y));                              \
  87. }                                                       \
  88. )
  89.  
  90. /*
  91.  * Divide positive or negative dividend by positive divisor and round
  92.  * to closest integer. Result is undefined for negative divisors and
  93.  * for negative dividends if the divisor variable type is unsigned.
  94.  */
  95. #define DIV_ROUND_CLOSEST(x, divisor)(                  \
  96. {                                                       \
  97.         typeof(x) __x = x;                              \
  98.         typeof(divisor) __d = divisor;                  \
  99.         (((typeof(x))-1) > 0 ||                         \
  100.          ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
  101.                 (((__x) + ((__d) / 2)) / (__d)) :       \
  102.                 (((__x) - ((__d) / 2)) / (__d));        \
  103. }                                                       \
  104. )
  105.  
  106. /*
  107.  * Multiplies an integer by a fraction, while avoiding unnecessary
  108.  * overflow or loss of precision.
  109.  */
  110. #define mult_frac(x, numer, denom)(                     \
  111. {                                                       \
  112.         typeof(x) quot = (x) / (denom);                 \
  113.         typeof(x) rem  = (x) % (denom);                 \
  114.         (quot * (numer)) + ((rem * (numer)) / (denom)); \
  115. }                                                       \
  116. )
  117.  
  118.  
  119. #define _RET_IP_                (unsigned long)__builtin_return_address(0)
  120. #define _THIS_IP_  ({ __label__ __here; __here: (unsigned long)&&__here; })
  121.  
  122. #ifdef CONFIG_LBDAF
  123. # include <asm/div64.h>
  124. # define sector_div(a, b) do_div(a, b)
  125. #else
  126. # define sector_div(n, b)( \
  127. { \
  128.         int _res; \
  129.         _res = (n) % (b); \
  130.         (n) /= (b); \
  131.         _res; \
  132. } \
  133. )
  134. #endif
  135.  
  136. /**
  137.  * upper_32_bits - return bits 32-63 of a number
  138.  * @n: the number we're accessing
  139.  *
  140.  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
  141.  * the "right shift count >= width of type" warning when that quantity is
  142.  * 32-bits.
  143.  */
  144. #define upper_32_bits(n) ((u32)(((n) >> 16) >> 16))
  145.  
  146. /**
  147.  * lower_32_bits - return bits 0-31 of a number
  148.  * @n: the number we're accessing
  149.  */
  150. #define lower_32_bits(n) ((u32)(n))
  151.  
  152.  
  153. /*
  154.  * abs() handles unsigned and signed longs, ints, shorts and chars.  For all
  155.  * input types abs() returns a signed long.
  156.  * abs() should not be used for 64-bit types (s64, u64, long long) - use abs64()
  157.  * for those.
  158.  */
  159. #define abs(x) ({                                               \
  160.                 long ret;                                       \
  161.                 if (sizeof(x) == sizeof(long)) {                \
  162.                         long __x = (x);                         \
  163.                         ret = (__x < 0) ? -__x : __x;           \
  164.                 } else {                                        \
  165.                         int __x = (x);                          \
  166.                         ret = (__x < 0) ? -__x : __x;           \
  167.                 }                                               \
  168.                 ret;                                            \
  169.         })
  170.  
  171. #define abs64(x) ({                             \
  172.                 s64 __x = (x);                  \
  173.                 (__x < 0) ? -__x : __x;         \
  174.         })
  175.  
  176. #define KERN_EMERG      "<0>"   /* system is unusable                   */
  177. #define KERN_ALERT      "<1>"   /* action must be taken immediately     */
  178. #define KERN_CRIT       "<2>"   /* critical conditions                  */
  179. #define KERN_ERR        "<3>"   /* error conditions                     */
  180. #define KERN_WARNING    "<4>"   /* warning conditions                   */
  181. #define KERN_NOTICE     "<5>"   /* normal but significant condition     */
  182. #define KERN_INFO       "<6>"   /* informational                        */
  183. #define KERN_DEBUG      "<7>"   /* debug-level messages                 */
  184. extern __printf(2, 3) int sprintf(char *buf, const char * fmt, ...);
  185. extern __printf(2, 0) int vsprintf(char *buf, const char *, va_list);
  186. extern __printf(3, 4)
  187. int snprintf(char *buf, size_t size, const char *fmt, ...);
  188. extern __printf(3, 0)
  189. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
  190. extern __printf(3, 4)
  191. int scnprintf(char *buf, size_t size, const char *fmt, ...);
  192. extern __printf(3, 0)
  193. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
  194. extern __printf(2, 3)
  195. char *kasprintf(gfp_t gfp, const char *fmt, ...);
  196. extern char *kvasprintf(gfp_t gfp, const char *fmt, va_list args);
  197. enum lockdep_ok {
  198.         LOCKDEP_STILL_OK,
  199.         LOCKDEP_NOW_UNRELIABLE
  200. };
  201. extern void add_taint(unsigned flag, enum lockdep_ok);
  202. extern int test_taint(unsigned flag);
  203. extern unsigned long get_taint(void);
  204. extern int root_mountflags;
  205.  
  206. extern bool early_boot_irqs_disabled;
  207.  
  208. /* Values used for system_state */
  209. extern enum system_states {
  210.         SYSTEM_BOOTING,
  211.         SYSTEM_RUNNING,
  212.         SYSTEM_HALT,
  213.         SYSTEM_POWER_OFF,
  214.         SYSTEM_RESTART,
  215. } system_state;
  216.  
  217. #define TAINT_PROPRIETARY_MODULE        0
  218. #define TAINT_FORCED_MODULE             1
  219. #define TAINT_CPU_OUT_OF_SPEC           2
  220. #define TAINT_FORCED_RMMOD              3
  221. #define TAINT_MACHINE_CHECK             4
  222. #define TAINT_BAD_PAGE                  5
  223. #define TAINT_USER                      6
  224. #define TAINT_DIE                       7
  225. #define TAINT_OVERRIDDEN_ACPI_TABLE     8
  226. #define TAINT_WARN                      9
  227. #define TAINT_CRAP                      10
  228. #define TAINT_FIRMWARE_WORKAROUND       11
  229. #define TAINT_OOT_MODULE                12
  230. #define TAINT_UNSIGNED_MODULE           13
  231. #define TAINT_SOFTLOCKUP                14
  232.  
  233. extern const char hex_asc[];
  234. #define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
  235. #define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
  236.  
  237. static inline char *hex_byte_pack(char *buf, u8 byte)
  238. {
  239.         *buf++ = hex_asc_hi(byte);
  240.         *buf++ = hex_asc_lo(byte);
  241.         return buf;
  242. }
  243.  
  244. extern const char hex_asc_upper[];
  245. #define hex_asc_upper_lo(x)     hex_asc_upper[((x) & 0x0f)]
  246. #define hex_asc_upper_hi(x)     hex_asc_upper[((x) & 0xf0) >> 4]
  247.  
  248. static inline char *hex_byte_pack_upper(char *buf, u8 byte)
  249. {
  250.         *buf++ = hex_asc_upper_hi(byte);
  251.         *buf++ = hex_asc_upper_lo(byte);
  252.         return buf;
  253. }
  254.  
  255. extern int hex_to_bin(char ch);
  256. extern int __must_check hex2bin(u8 *dst, const char *src, size_t count);
  257. extern char *bin2hex(char *dst, const void *src, size_t count);
  258.  
  259. bool mac_pton(const char *s, u8 *mac);
  260.  
  261. /*
  262.  * General tracing related utility functions - trace_printk(),
  263.  * tracing_on/tracing_off and tracing_start()/tracing_stop
  264.  *
  265.  * Use tracing_on/tracing_off when you want to quickly turn on or off
  266.  * tracing. It simply enables or disables the recording of the trace events.
  267.  * This also corresponds to the user space /sys/kernel/debug/tracing/tracing_on
  268.  * file, which gives a means for the kernel and userspace to interact.
  269.  * Place a tracing_off() in the kernel where you want tracing to end.
  270.  * From user space, examine the trace, and then echo 1 > tracing_on
  271.  * to continue tracing.
  272.  *
  273.  * tracing_stop/tracing_start has slightly more overhead. It is used
  274.  * by things like suspend to ram where disabling the recording of the
  275.  * trace is not enough, but tracing must actually stop because things
  276.  * like calling smp_processor_id() may crash the system.
  277.  *
  278.  * Most likely, you want to use tracing_on/tracing_off.
  279.  */
  280. #ifdef CONFIG_RING_BUFFER
  281. /* trace_off_permanent stops recording with no way to bring it back */
  282. void tracing_off_permanent(void);
  283. #else
  284. static inline void tracing_off_permanent(void) { }
  285. #endif
  286.  
  287. enum ftrace_dump_mode {
  288.         DUMP_NONE,
  289.         DUMP_ALL,
  290.         DUMP_ORIG,
  291. };
  292.  
  293. #ifdef CONFIG_TRACING
  294. void tracing_on(void);
  295. void tracing_off(void);
  296. int tracing_is_on(void);
  297. void tracing_snapshot(void);
  298. void tracing_snapshot_alloc(void);
  299.  
  300. extern void tracing_start(void);
  301. extern void tracing_stop(void);
  302.  
  303. static inline __printf(1, 2)
  304. void ____trace_printk_check_format(const char *fmt, ...)
  305. {
  306. }
  307. #define __trace_printk_check_format(fmt, args...)                       \
  308. do {                                                                    \
  309.         if (0)                                                          \
  310.                 ____trace_printk_check_format(fmt, ##args);             \
  311. } while (0)
  312.  
  313. /**
  314.  * trace_printk - printf formatting in the ftrace buffer
  315.  * @fmt: the printf format for printing
  316.  *
  317.  * Note: __trace_printk is an internal function for trace_printk and
  318.  *       the @ip is passed in via the trace_printk macro.
  319.  *
  320.  * This function allows a kernel developer to debug fast path sections
  321.  * that printk is not appropriate for. By scattering in various
  322.  * printk like tracing in the code, a developer can quickly see
  323.  * where problems are occurring.
  324.  *
  325.  * This is intended as a debugging tool for the developer only.
  326.  * Please refrain from leaving trace_printks scattered around in
  327.  * your code. (Extra memory is used for special buffers that are
  328.  * allocated when trace_printk() is used)
  329.  *
  330.  * A little optization trick is done here. If there's only one
  331.  * argument, there's no need to scan the string for printf formats.
  332.  * The trace_puts() will suffice. But how can we take advantage of
  333.  * using trace_puts() when trace_printk() has only one argument?
  334.  * By stringifying the args and checking the size we can tell
  335.  * whether or not there are args. __stringify((__VA_ARGS__)) will
  336.  * turn into "()\0" with a size of 3 when there are no args, anything
  337.  * else will be bigger. All we need to do is define a string to this,
  338.  * and then take its size and compare to 3. If it's bigger, use
  339.  * do_trace_printk() otherwise, optimize it to trace_puts(). Then just
  340.  * let gcc optimize the rest.
  341.  */
  342.  
  343. #define trace_printk(fmt, ...)                          \
  344. do {                                                    \
  345.         char _______STR[] = __stringify((__VA_ARGS__)); \
  346.         if (sizeof(_______STR) > 3)                     \
  347.                 do_trace_printk(fmt, ##__VA_ARGS__);    \
  348.         else                                            \
  349.                 trace_puts(fmt);                        \
  350. } while (0)
  351.  
  352. #define do_trace_printk(fmt, args...)                                   \
  353. do {                                                                    \
  354.         static const char *trace_printk_fmt                             \
  355.                 __attribute__((section("__trace_printk_fmt"))) =        \
  356.                 __builtin_constant_p(fmt) ? fmt : NULL;                 \
  357.                                                                         \
  358.         __trace_printk_check_format(fmt, ##args);                       \
  359.                                                                         \
  360.         if (__builtin_constant_p(fmt))                                  \
  361.                 __trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args);   \
  362.         else                                                            \
  363.                 __trace_printk(_THIS_IP_, fmt, ##args);                 \
  364. } while (0)
  365.  
  366. extern __printf(2, 3)
  367. int __trace_bprintk(unsigned long ip, const char *fmt, ...);
  368.  
  369. extern __printf(2, 3)
  370. int __trace_printk(unsigned long ip, const char *fmt, ...);
  371.  
  372. /**
  373.  * trace_puts - write a string into the ftrace buffer
  374.  * @str: the string to record
  375.  *
  376.  * Note: __trace_bputs is an internal function for trace_puts and
  377.  *       the @ip is passed in via the trace_puts macro.
  378.  *
  379.  * This is similar to trace_printk() but is made for those really fast
  380.  * paths that a developer wants the least amount of "Heisenbug" affects,
  381.  * where the processing of the print format is still too much.
  382.  *
  383.  * This function allows a kernel developer to debug fast path sections
  384.  * that printk is not appropriate for. By scattering in various
  385.  * printk like tracing in the code, a developer can quickly see
  386.  * where problems are occurring.
  387.  *
  388.  * This is intended as a debugging tool for the developer only.
  389.  * Please refrain from leaving trace_puts scattered around in
  390.  * your code. (Extra memory is used for special buffers that are
  391.  * allocated when trace_puts() is used)
  392.  *
  393.  * Returns: 0 if nothing was written, positive # if string was.
  394.  *  (1 when __trace_bputs is used, strlen(str) when __trace_puts is used)
  395.  */
  396.  
  397. #define trace_puts(str) ({                                              \
  398.         static const char *trace_printk_fmt                             \
  399.                 __attribute__((section("__trace_printk_fmt"))) =        \
  400.                 __builtin_constant_p(str) ? str : NULL;                 \
  401.                                                                         \
  402.         if (__builtin_constant_p(str))                                  \
  403.                 __trace_bputs(_THIS_IP_, trace_printk_fmt);             \
  404.         else                                                            \
  405.                 __trace_puts(_THIS_IP_, str, strlen(str));              \
  406. })
  407. extern int __trace_bputs(unsigned long ip, const char *str);
  408. extern int __trace_puts(unsigned long ip, const char *str, int size);
  409.  
  410. extern void trace_dump_stack(int skip);
  411.  
  412. /*
  413.  * The double __builtin_constant_p is because gcc will give us an error
  414.  * if we try to allocate the static variable to fmt if it is not a
  415.  * constant. Even with the outer if statement.
  416.  */
  417. #define ftrace_vprintk(fmt, vargs)                                      \
  418. do {                                                                    \
  419.         if (__builtin_constant_p(fmt)) {                                \
  420.                 static const char *trace_printk_fmt                     \
  421.                   __attribute__((section("__trace_printk_fmt"))) =      \
  422.                         __builtin_constant_p(fmt) ? fmt : NULL;         \
  423.                                                                         \
  424.                 __ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs);  \
  425.         } else                                                          \
  426.                 __ftrace_vprintk(_THIS_IP_, fmt, vargs);                \
  427. } while (0)
  428.  
  429. extern int
  430. __ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
  431.  
  432. extern int
  433. __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
  434.  
  435. extern void ftrace_dump(enum ftrace_dump_mode oops_dump_mode);
  436. #else
  437. static inline void tracing_start(void) { }
  438. static inline void tracing_stop(void) { }
  439. static inline void trace_dump_stack(int skip) { }
  440.  
  441. static inline void tracing_on(void) { }
  442. static inline void tracing_off(void) { }
  443. static inline int tracing_is_on(void) { return 0; }
  444. static inline void tracing_snapshot(void) { }
  445. static inline void tracing_snapshot_alloc(void) { }
  446.  
  447. static inline __printf(1, 2)
  448. int trace_printk(const char *fmt, ...)
  449. {
  450.         return 0;
  451. }
  452. static inline int
  453. ftrace_vprintk(const char *fmt, va_list ap)
  454. {
  455.         return 0;
  456. }
  457. static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
  458. #endif /* CONFIG_TRACING */
  459.  
  460. /*
  461.  * min()/max()/clamp() macros that also do
  462.  * strict type-checking.. See the
  463.  * "unnecessary" pointer comparison.
  464.  */
  465. #define min(x, y) ({                \
  466.     typeof(x) _min1 = (x);          \
  467.     typeof(y) _min2 = (y);          \
  468.     (void) (&_min1 == &_min2);      \
  469.     _min1 < _min2 ? _min1 : _min2; })
  470.  
  471. #define max(x, y) ({                \
  472.     typeof(x) _max1 = (x);          \
  473.     typeof(y) _max2 = (y);          \
  474.     (void) (&_max1 == &_max2);      \
  475.     _max1 > _max2 ? _max1 : _max2; })
  476.  
  477. #define min3(x, y, z) min((typeof(x))min(x, y), z)
  478. #define max3(x, y, z) max((typeof(x))max(x, y), z)
  479.  
  480. /**
  481.  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
  482.  * @x: value1
  483.  * @y: value2
  484.  */
  485. #define min_not_zero(x, y) ({                   \
  486.         typeof(x) __x = (x);                    \
  487.         typeof(y) __y = (y);                    \
  488.         __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
  489.  
  490. /**
  491.  * clamp - return a value clamped to a given range with strict typechecking
  492.  * @val: current value
  493.  * @lo: lowest allowable value
  494.  * @hi: highest allowable value
  495.  *
  496.  * This macro does strict typechecking of lo/hi to make sure they are of the
  497.  * same type as val.  See the unnecessary pointer comparisons.
  498.  */
  499. #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
  500.  
  501. /*
  502.  * ..and if you can't take the strict
  503.  * types, you can specify one yourself.
  504.  *
  505.  * Or not use min/max/clamp at all, of course.
  506.  */
  507. #define min_t(type, x, y) ({            \
  508.     type __min1 = (x);          \
  509.     type __min2 = (y);          \
  510.     __min1 < __min2 ? __min1: __min2; })
  511.  
  512. #define max_t(type, x, y) ({            \
  513.     type __max1 = (x);          \
  514.     type __max2 = (y);          \
  515.     __max1 > __max2 ? __max1: __max2; })
  516.  
  517. /**
  518.  * clamp_t - return a value clamped to a given range using a given type
  519.  * @type: the type of variable to use
  520.  * @val: current value
  521.  * @lo: minimum allowable value
  522.  * @hi: maximum allowable value
  523.  *
  524.  * This macro does no typechecking and uses temporary variables of type
  525.  * 'type' to make all the comparisons.
  526.  */
  527. #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
  528.  
  529. /**
  530.  * clamp_val - return a value clamped to a given range using val's type
  531.  * @val: current value
  532.  * @lo: minimum allowable value
  533.  * @hi: maximum allowable value
  534.  *
  535.  * This macro does no typechecking and uses temporary variables of whatever
  536.  * type the input argument 'val' is.  This is useful when val is an unsigned
  537.  * type and min and max are literals that will otherwise be assigned a signed
  538.  * integer type.
  539.  */
  540. #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
  541.  
  542.  
  543. /*
  544.  * swap - swap value of @a and @b
  545.  */
  546. #define swap(a, b) \
  547.         do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  548.  
  549. /**
  550.  * container_of - cast a member of a structure out to the containing structure
  551.  * @ptr:    the pointer to the member.
  552.  * @type:   the type of the container struct this is embedded in.
  553.  * @member: the name of the member within the struct.
  554.  *
  555.  */
  556. #define container_of(ptr, type, member) ({          \
  557.     const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
  558.     (type *)( (char *)__mptr - offsetof(type,member) );})
  559.  
  560. /* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
  561. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  562. # define REBUILD_DUE_TO_FTRACE_MCOUNT_RECORD
  563. #endif
  564.  
  565. /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
  566. #define VERIFY_OCTAL_PERMISSIONS(perms)                                 \
  567.         (BUILD_BUG_ON_ZERO((perms) < 0) +                               \
  568.          BUILD_BUG_ON_ZERO((perms) > 0777) +                            \
  569.          /* User perms >= group perms >= other perms */                 \
  570.          BUILD_BUG_ON_ZERO(((perms) >> 6) < (((perms) >> 3) & 7)) +     \
  571.          BUILD_BUG_ON_ZERO((((perms) >> 3) & 7) < ((perms) & 7)) +      \
  572.          /* Other writable?  Generally considered a bad idea. */        \
  573.          BUILD_BUG_ON_ZERO((perms) & 2) +                               \
  574.          (perms))
  575.  
  576.  
  577. void free (void *ptr);
  578.  
  579.  
  580. typedef unsigned long   pgprotval_t;
  581.  
  582.  
  583. struct file
  584. {
  585.     struct page  **pages;         /* physical memory backend */
  586.     unsigned int   count;
  587.     unsigned int   allocated;
  588.     void           *vma;
  589. };
  590.  
  591. struct vm_area_struct {};
  592. struct address_space {};
  593.  
  594. struct device
  595. {
  596.     struct device   *parent;
  597.     void            *driver_data;
  598. };
  599.  
  600. static inline void dev_set_drvdata(struct device *dev, void *data)
  601. {
  602.     dev->driver_data = data;
  603. }
  604.  
  605. static inline void *dev_get_drvdata(struct device *dev)
  606. {
  607.     return dev->driver_data;
  608. }
  609.  
  610.  
  611. #define in_dbg_master() (0)
  612.  
  613. #define HZ 100
  614.  
  615. struct tvec_base;
  616.  
  617. struct timer_list {
  618.          struct list_head entry;
  619.          unsigned long expires;
  620.  
  621.          void (*function)(unsigned long);
  622.          unsigned long data;
  623.          u32  handle;
  624. };
  625.  
  626. #define setup_timer(_timer, _fn, _data)                                 \
  627.         do {                                                            \
  628.                 (_timer)->function = (_fn);                             \
  629.                 (_timer)->data = (_data);                               \
  630.                 (_timer)->handle = 0;                                   \
  631.         } while (0)
  632.  
  633. int del_timer(struct timer_list *timer);
  634.  
  635. # define del_timer_sync(t)              del_timer(t)
  636.  
  637.  
  638. #define build_mmio_read(name, size, type, reg, barrier)     \
  639. static inline type name(const volatile void __iomem *addr)  \
  640. { type ret; asm volatile("mov" size " %1,%0":reg (ret)      \
  641. :"m" (*(volatile type __force *)addr) barrier); return ret; }
  642.  
  643. #define build_mmio_write(name, size, type, reg, barrier) \
  644. static inline void name(type val, volatile void __iomem *addr) \
  645. { asm volatile("mov" size " %0,%1": :reg (val), \
  646. "m" (*(volatile type __force *)addr) barrier); }
  647.  
  648. build_mmio_read(readb, "b", unsigned char, "=q", :"memory")
  649. build_mmio_read(readw, "w", unsigned short, "=r", :"memory")
  650. build_mmio_read(readl, "l", unsigned int, "=r", :"memory")
  651.  
  652. build_mmio_read(__readb, "b", unsigned char, "=q", )
  653. build_mmio_read(__readw, "w", unsigned short, "=r", )
  654. build_mmio_read(__readl, "l", unsigned int, "=r", )
  655.  
  656. build_mmio_write(writeb, "b", unsigned char, "q", :"memory")
  657. build_mmio_write(writew, "w", unsigned short, "r", :"memory")
  658. build_mmio_write(writel, "l", unsigned int, "r", :"memory")
  659.  
  660. build_mmio_write(__writeb, "b", unsigned char, "q", )
  661. build_mmio_write(__writew, "w", unsigned short, "r", )
  662. build_mmio_write(__writel, "l", unsigned int, "r", )
  663.  
  664. #define readb_relaxed(a) __readb(a)
  665. #define readw_relaxed(a) __readw(a)
  666. #define readl_relaxed(a) __readl(a)
  667. #define __raw_readb __readb
  668. #define __raw_readw __readw
  669. #define __raw_readl __readl
  670.  
  671. #define __raw_writeb __writeb
  672. #define __raw_writew __writew
  673. #define __raw_writel __writel
  674.  
  675. #define swap(a, b) \
  676.         do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
  677.  
  678.  
  679. #define mmiowb() barrier()
  680.  
  681. #define dev_err(dev, format, arg...)            \
  682.         printk("Error %s " format, __func__ , ## arg)
  683.  
  684. #define dev_warn(dev, format, arg...)            \
  685.         printk("Warning %s " format, __func__ , ## arg)
  686.  
  687. #define dev_info(dev, format, arg...)       \
  688.         printk("Info %s " format , __func__, ## arg)
  689.  
  690. struct page
  691. {
  692.     unsigned int addr;
  693. };
  694.  
  695. #define page_to_phys(page)    ((dma_addr_t)(page))
  696.  
  697. struct vm_fault {
  698.     unsigned int flags;             /* FAULT_FLAG_xxx flags */
  699.     pgoff_t pgoff;                  /* Logical page offset based on vma */
  700.     void __user *virtual_address;   /* Faulting virtual address */
  701.  
  702.     struct page *page;              /* ->fault handlers should return a
  703.                                      * page here, unless VM_FAULT_NOPAGE
  704.                                      * is set (which is also implied by
  705.                                      * VM_FAULT_ERROR).
  706.                                      */
  707. };
  708.  
  709. struct pagelist {
  710.     dma_addr_t    *page;
  711.     unsigned int   nents;
  712. };
  713.  
  714. #define page_cache_release(page)        FreePage(page_to_phys(page))
  715.  
  716. #define alloc_page(gfp_mask) (struct page*)AllocPage()
  717.  
  718. #define __free_page(page) FreePage(page_to_phys(page))
  719.  
  720. #define get_page(a)
  721. #define put_page(a)
  722.  
  723. #define pci_map_page(dev, page, offset, size, direction) \
  724.         (dma_addr_t)( (offset)+page_to_phys(page))
  725.  
  726. #define pci_unmap_page(dev, dma_address, size, direction)
  727.  
  728. #define IS_ENABLED(a)  0
  729.  
  730.  
  731. #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
  732.  
  733.  
  734.  
  735. #define cpufreq_quick_get_max(x) GetCpuFreq()
  736.  
  737. extern unsigned int tsc_khz;
  738.  
  739. #define on_each_cpu(func,info,wait)             \
  740.         ({                                      \
  741.                 func(info);                     \
  742.                 0;                              \
  743.         })
  744.  
  745.  
  746. static inline __must_check long __copy_to_user(void __user *to,
  747.         const void *from, unsigned long n)
  748. {
  749.     if (__builtin_constant_p(n)) {
  750.         switch(n) {
  751.         case 1:
  752.             *(u8 __force *)to = *(u8 *)from;
  753.             return 0;
  754.         case 2:
  755.             *(u16 __force *)to = *(u16 *)from;
  756.             return 0;
  757.         case 4:
  758.             *(u32 __force *)to = *(u32 *)from;
  759.             return 0;
  760. #ifdef CONFIG_64BIT
  761.         case 8:
  762.             *(u64 __force *)to = *(u64 *)from;
  763.             return 0;
  764. #endif
  765.         default:
  766.             break;
  767.         }
  768.     }
  769.  
  770.     __builtin_memcpy((void __force *)to, from, n);
  771.     return 0;
  772. }
  773.  
  774. struct seq_file;
  775.  
  776. void *kmap(struct page *page);
  777. void *kmap_atomic(struct page *page);
  778. void kunmap(struct page *page);
  779. void kunmap_atomic(void *vaddr);
  780.  
  781. typedef u64 async_cookie_t;
  782.  
  783. #define iowrite32(v, addr)      writel((v), (addr))
  784.  
  785.  
  786. #define __init
  787.  
  788. #define CONFIG_PAGE_OFFSET 0
  789.  
  790.  
  791. #endif
  792.