1 #ifndef __TOOLS_LINUX_KERNEL_H
2 #define __TOOLS_LINUX_KERNEL_H
10 #include <linux/bug.h>
11 #include <linux/byteorder.h>
12 #include <linux/compiler.h>
14 #define IS_ENABLED(opt) 0
15 #define EXPORT_SYMBOL(sym)
17 #define U8_MAX ((u8)~0U)
18 #define S8_MAX ((s8)(U8_MAX>>1))
19 #define S8_MIN ((s8)(-S8_MAX - 1))
20 #define U16_MAX ((u16)~0U)
21 #define S16_MAX ((s16)(U16_MAX>>1))
22 #define S16_MIN ((s16)(-S16_MAX - 1))
23 #define U32_MAX ((u32)~0U)
24 #define S32_MAX ((s32)(U32_MAX>>1))
25 #define S32_MIN ((s32)(-S32_MAX - 1))
26 #define U64_MAX ((u64)~0ULL)
27 #define S64_MAX ((s64)(U64_MAX>>1))
28 #define S64_MIN ((s64)(-S64_MAX - 1))
30 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
31 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
33 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
34 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
36 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
37 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
39 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
41 #define mult_frac(x, numer, denom)( \
43 typeof(x) quot = (x) / (denom); \
44 typeof(x) rem = (x) % (denom); \
45 (quot * (numer)) + ((rem * (numer)) / (denom)); \
50 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
55 * container_of - cast a member of a structure out to the containing structure
56 * @ptr: the pointer to the member.
57 * @type: the type of the container struct this is embedded in.
58 * @member: the name of the member within the struct.
61 #define container_of(ptr, type, member) ({ \
62 const typeof(((type *)0)->member) * __mptr = (ptr); \
63 (type *)((char *)__mptr - offsetof(type, member)); })
66 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
67 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
68 #define round_down(x, y) ((x) & ~__round_mask(x, y))
70 #define roundup(x, y) \
72 const typeof(y) __y = y; \
73 (((x) + (__y - 1)) / __y) * __y; \
76 #define max(x, y) ({ \
77 typeof(x) _max1 = (x); \
78 typeof(y) _max2 = (y); \
79 (void) (&_max1 == &_max2); \
80 _max1 > _max2 ? _max1 : _max2; })
82 #define min(x, y) ({ \
83 typeof(x) _min1 = (x); \
84 typeof(y) _min2 = (y); \
85 (void) (&_min1 == &_min2); \
86 _min1 < _min2 ? _min1 : _min2; })
88 #define min_t(type, x, y) ({ \
91 __min1 < __min2 ? __min1: __min2; })
93 #define max_t(type, x, y) ({ \
96 __max1 > __max2 ? __max1: __max2; })
98 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
101 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
103 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
104 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
106 #define might_sleep()
110 #define cpu_relax() do {} while (0)
111 #define cpu_relax_lowlatency() do {} while (0)
114 static inline void panic(const char *fmt, ...)
125 unsigned long simple_strtoul(const char *,char **,unsigned int);
126 long simple_strtol(const char *,char **,unsigned int);
127 unsigned long long simple_strtoull(const char *,char **,unsigned int);
128 long long simple_strtoll(const char *,char **,unsigned int);
130 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
131 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
133 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
134 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
137 * kstrtoul - convert a string to an unsigned long
138 * @s: The start of the string. The string must be null-terminated, and may also
139 * include a single newline before its terminating null. The first character
140 * may also be a plus sign, but not a minus sign.
141 * @base: The number base to use. The maximum supported base is 16. If base is
142 * given as 0, then the base of the string is automatically detected with the
143 * conventional semantics - If it begins with 0x the number will be parsed as a
144 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
145 * parsed as an octal number. Otherwise it will be parsed as a decimal.
146 * @res: Where to write the result of the conversion on success.
148 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
149 * Used as a replacement for the obsolete simple_strtoull. Return code must
152 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
155 * We want to shortcut function call, but
156 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
158 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
159 __alignof__(unsigned long) == __alignof__(unsigned long long))
160 return kstrtoull(s, base, (unsigned long long *)res);
162 return _kstrtoul(s, base, res);
166 * kstrtol - convert a string to a long
167 * @s: The start of the string. The string must be null-terminated, and may also
168 * include a single newline before its terminating null. The first character
169 * may also be a plus sign or a minus sign.
170 * @base: The number base to use. The maximum supported base is 16. If base is
171 * given as 0, then the base of the string is automatically detected with the
172 * conventional semantics - If it begins with 0x the number will be parsed as a
173 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
174 * parsed as an octal number. Otherwise it will be parsed as a decimal.
175 * @res: Where to write the result of the conversion on success.
177 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
178 * Used as a replacement for the obsolete simple_strtoull. Return code must
181 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
184 * We want to shortcut function call, but
185 * __builtin_types_compatible_p(long, long long) = 0.
187 if (sizeof(long) == sizeof(long long) &&
188 __alignof__(long) == __alignof__(long long))
189 return kstrtoll(s, base, (long long *)res);
191 return _kstrtol(s, base, res);
194 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
195 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
197 static inline int __must_check kstrtou64(const char *s, unsigned int base, u64 *res)
199 return kstrtoull(s, base, res);
202 static inline int __must_check kstrtos64(const char *s, unsigned int base, s64 *res)
204 return kstrtoll(s, base, res);
207 static inline int __must_check kstrtou32(const char *s, unsigned int base, u32 *res)
209 return kstrtouint(s, base, res);
212 static inline int __must_check kstrtos32(const char *s, unsigned int base, s32 *res)
214 return kstrtoint(s, base, res);
217 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
218 #define VERIFY_OCTAL_PERMISSIONS(perms) \
219 (BUILD_BUG_ON_ZERO((perms) < 0) + \
220 BUILD_BUG_ON_ZERO((perms) > 0777) + \
221 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
222 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
223 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
224 /* USER_WRITABLE >= GROUP_WRITABLE */ \
225 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
226 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
227 BUILD_BUG_ON_ZERO((perms) & 2) + \