1 #ifndef __TOOLS_LINUX_KERNEL_H
2 #define __TOOLS_LINUX_KERNEL_H
11 #include <linux/bug.h>
12 #include <linux/byteorder.h>
13 #include <linux/compiler.h>
15 #define IS_ENABLED(opt) 0
16 #define EXPORT_SYMBOL(sym)
18 #define U8_MAX ((u8)~0U)
19 #define S8_MAX ((s8)(U8_MAX>>1))
20 #define S8_MIN ((s8)(-S8_MAX - 1))
21 #define U16_MAX ((u16)~0U)
22 #define S16_MAX ((s16)(U16_MAX>>1))
23 #define S16_MIN ((s16)(-S16_MAX - 1))
24 #define U32_MAX ((u32)~0U)
25 #define S32_MAX ((s32)(U32_MAX>>1))
26 #define S32_MIN ((s32)(-S32_MAX - 1))
27 #define U64_MAX ((u64)~0ULL)
28 #define S64_MAX ((s64)(U64_MAX>>1))
29 #define S64_MIN ((s64)(-S64_MAX - 1))
31 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
32 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
34 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
35 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
37 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
38 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
40 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
43 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
48 * container_of - cast a member of a structure out to the containing structure
49 * @ptr: the pointer to the member.
50 * @type: the type of the container struct this is embedded in.
51 * @member: the name of the member within the struct.
54 #define container_of(ptr, type, member) ({ \
55 const typeof(((type *)0)->member) * __mptr = (ptr); \
56 (type *)((char *)__mptr - offsetof(type, member)); })
59 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
60 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
61 #define round_down(x, y) ((x) & ~__round_mask(x, y))
63 #define roundup(x, y) \
65 const typeof(y) __y = y; \
66 (((x) + (__y - 1)) / __y) * __y; \
69 #define max(x, y) ({ \
70 typeof(x) _max1 = (x); \
71 typeof(y) _max2 = (y); \
72 (void) (&_max1 == &_max2); \
73 _max1 > _max2 ? _max1 : _max2; })
75 #define min(x, y) ({ \
76 typeof(x) _min1 = (x); \
77 typeof(y) _min2 = (y); \
78 (void) (&_min1 == &_min2); \
79 _min1 < _min2 ? _min1 : _min2; })
81 #define min_t(type, x, y) ({ \
84 __min1 < __min2 ? __min1: __min2; })
86 #define max_t(type, x, y) ({ \
89 __max1 > __max2 ? __max1: __max2; })
91 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
94 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
96 /* This counts to 12. Any more, it will return 13th argument. */
97 #define __COUNT_ARGS(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _n, X...) _n
98 #define COUNT_ARGS(X...) __COUNT_ARGS(, ##X, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
100 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
101 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
103 #define might_sleep()
105 #define cpu_relax() do {} while (0)
106 #define cpu_relax_lowlatency() do {} while (0)
108 #define panic(fmt, ...) \
110 printf(fmt, ##__VA_ARGS__); \
114 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
115 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
117 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
118 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
121 * kstrtoul - convert a string to an unsigned long
122 * @s: The start of the string. The string must be null-terminated, and may also
123 * include a single newline before its terminating null. The first character
124 * may also be a plus sign, but not a minus sign.
125 * @base: The number base to use. The maximum supported base is 16. If base is
126 * given as 0, then the base of the string is automatically detected with the
127 * conventional semantics - If it begins with 0x the number will be parsed as a
128 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
129 * parsed as an octal number. Otherwise it will be parsed as a decimal.
130 * @res: Where to write the result of the conversion on success.
132 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
133 * Used as a replacement for the obsolete simple_strtoull. Return code must
136 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
139 * We want to shortcut function call, but
140 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
142 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
143 __alignof__(unsigned long) == __alignof__(unsigned long long))
144 return kstrtoull(s, base, (unsigned long long *)res);
146 return _kstrtoul(s, base, res);
150 * kstrtol - convert a string to a long
151 * @s: The start of the string. The string must be null-terminated, and may also
152 * include a single newline before its terminating null. The first character
153 * may also be a plus sign or a minus sign.
154 * @base: The number base to use. The maximum supported base is 16. If base is
155 * given as 0, then the base of the string is automatically detected with the
156 * conventional semantics - If it begins with 0x the number will be parsed as a
157 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
158 * parsed as an octal number. Otherwise it will be parsed as a decimal.
159 * @res: Where to write the result of the conversion on success.
161 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
162 * Used as a replacement for the obsolete simple_strtoull. Return code must
165 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
168 * We want to shortcut function call, but
169 * __builtin_types_compatible_p(long, long long) = 0.
171 if (sizeof(long) == sizeof(long long) &&
172 __alignof__(long) == __alignof__(long long))
173 return kstrtoll(s, base, (long long *)res);
175 return _kstrtol(s, base, res);
178 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
179 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
181 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
183 return kstrtoull(s, base, res);
186 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
188 return kstrtoll(s, base, res);
191 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
193 return kstrtouint(s, base, res);
196 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
198 return kstrtoint(s, base, res);
201 /* The hash is always the low bits of hash_len */
202 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
203 #define HASH_LEN_DECLARE u32 hash; u32 len
205 #define HASH_LEN_DECLARE u32 len; u32 hash
215 const unsigned char *name;
218 #define QSTR_INIT(n,l) { { { .len = l } }, .name = n }
220 #define POISON_FREE 0x6b