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))
42 #define mult_frac(x, numer, denom)( \
44 typeof(x) quot = (x) / (denom); \
45 typeof(x) rem = (x) % (denom); \
46 (quot * (numer)) + ((rem * (numer)) / (denom)); \
51 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
56 * container_of - cast a member of a structure out to the containing structure
57 * @ptr: the pointer to the member.
58 * @type: the type of the container struct this is embedded in.
59 * @member: the name of the member within the struct.
62 #define container_of(ptr, type, member) ({ \
63 const typeof(((type *)0)->member) * __mptr = (ptr); \
64 (type *)((char *)__mptr - offsetof(type, member)); })
67 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
68 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
69 #define round_down(x, y) ((x) & ~__round_mask(x, y))
71 #define roundup(x, y) \
73 const typeof(y) __y = y; \
74 (((x) + (__y - 1)) / __y) * __y; \
77 #define max(x, y) ({ \
78 typeof(x) _max1 = (x); \
79 typeof(y) _max2 = (y); \
80 (void) (&_max1 == &_max2); \
81 _max1 > _max2 ? _max1 : _max2; })
83 #define min(x, y) ({ \
84 typeof(x) _min1 = (x); \
85 typeof(y) _min2 = (y); \
86 (void) (&_min1 == &_min2); \
87 _min1 < _min2 ? _min1 : _min2; })
89 #define min_t(type, x, y) ({ \
92 __min1 < __min2 ? __min1: __min2; })
94 #define max_t(type, x, y) ({ \
97 __max1 > __max2 ? __max1: __max2; })
99 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
102 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
104 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
105 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
107 #define might_sleep()
111 #define cpu_relax() do {} while (0)
112 #define cpu_relax_lowlatency() do {} while (0)
115 static inline void panic(const char *fmt, ...)
126 unsigned long simple_strtoul(const char *,char **,unsigned int);
127 long simple_strtol(const char *,char **,unsigned int);
128 unsigned long long simple_strtoull(const char *,char **,unsigned int);
129 long long simple_strtoll(const char *,char **,unsigned int);
131 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
132 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
134 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
135 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
138 * kstrtoul - convert a string to an unsigned long
139 * @s: The start of the string. The string must be null-terminated, and may also
140 * include a single newline before its terminating null. The first character
141 * may also be a plus sign, but not a minus sign.
142 * @base: The number base to use. The maximum supported base is 16. If base is
143 * given as 0, then the base of the string is automatically detected with the
144 * conventional semantics - If it begins with 0x the number will be parsed as a
145 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
146 * parsed as an octal number. Otherwise it will be parsed as a decimal.
147 * @res: Where to write the result of the conversion on success.
149 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
150 * Used as a replacement for the obsolete simple_strtoull. Return code must
153 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
156 * We want to shortcut function call, but
157 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
159 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
160 __alignof__(unsigned long) == __alignof__(unsigned long long))
161 return kstrtoull(s, base, (unsigned long long *)res);
163 return _kstrtoul(s, base, res);
167 * kstrtol - convert a string to a long
168 * @s: The start of the string. The string must be null-terminated, and may also
169 * include a single newline before its terminating null. The first character
170 * may also be a plus sign or a minus sign.
171 * @base: The number base to use. The maximum supported base is 16. If base is
172 * given as 0, then the base of the string is automatically detected with the
173 * conventional semantics - If it begins with 0x the number will be parsed as a
174 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
175 * parsed as an octal number. Otherwise it will be parsed as a decimal.
176 * @res: Where to write the result of the conversion on success.
178 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
179 * Used as a replacement for the obsolete simple_strtoull. Return code must
182 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
185 * We want to shortcut function call, but
186 * __builtin_types_compatible_p(long, long long) = 0.
188 if (sizeof(long) == sizeof(long long) &&
189 __alignof__(long) == __alignof__(long long))
190 return kstrtoll(s, base, (long long *)res);
192 return _kstrtol(s, base, res);
195 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
196 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
198 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
200 return kstrtoull(s, base, res);
203 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
205 return kstrtoll(s, base, res);
208 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
210 return kstrtouint(s, base, res);
213 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
215 return kstrtoint(s, base, res);
218 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
219 #define VERIFY_OCTAL_PERMISSIONS(perms) \
220 (BUILD_BUG_ON_ZERO((perms) < 0) + \
221 BUILD_BUG_ON_ZERO((perms) > 0777) + \
222 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
223 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
224 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
225 /* USER_WRITABLE >= GROUP_WRITABLE */ \
226 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
227 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
228 BUILD_BUG_ON_ZERO((perms) & 2) + \