1 #ifndef __TOOLS_LINUX_KERNEL_H
2 #define __TOOLS_LINUX_KERNEL_H
10 #include <linux/bug.h>
11 #include <linux/compiler.h>
13 #define IS_ENABLED(opt) 0
14 #define EXPORT_SYMBOL(sym)
16 #define U8_MAX ((u8)~0U)
17 #define S8_MAX ((s8)(U8_MAX>>1))
18 #define S8_MIN ((s8)(-S8_MAX - 1))
19 #define U16_MAX ((u16)~0U)
20 #define S16_MAX ((s16)(U16_MAX>>1))
21 #define S16_MIN ((s16)(-S16_MAX - 1))
22 #define U32_MAX ((u32)~0U)
23 #define S32_MAX ((s32)(U32_MAX>>1))
24 #define S32_MIN ((s32)(-S32_MAX - 1))
25 #define U64_MAX ((u64)~0ULL)
26 #define S64_MAX ((s64)(U64_MAX>>1))
27 #define S64_MIN ((s64)(-S64_MAX - 1))
29 #define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
30 #define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
32 #define PTR_ALIGN(p, a) ((typeof(p))ALIGN((unsigned long)(p), (a)))
33 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
35 #define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
36 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
38 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
40 #define mult_frac(x, numer, denom)( \
42 typeof(x) quot = (x) / (denom); \
43 typeof(x) rem = (x) % (denom); \
44 (quot * (numer)) + ((rem * (numer)) / (denom)); \
49 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
54 * container_of - cast a member of a structure out to the containing structure
55 * @ptr: the pointer to the member.
56 * @type: the type of the container struct this is embedded in.
57 * @member: the name of the member within the struct.
60 #define container_of(ptr, type, member) ({ \
61 const typeof(((type *)0)->member) * __mptr = (ptr); \
62 (type *)((char *)__mptr - offsetof(type, member)); })
65 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
66 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
67 #define round_down(x, y) ((x) & ~__round_mask(x, y))
69 #define roundup(x, y) \
71 const typeof(y) __y = y; \
72 (((x) + (__y - 1)) / __y) * __y; \
75 #define max(x, y) ({ \
76 typeof(x) _max1 = (x); \
77 typeof(y) _max2 = (y); \
78 (void) (&_max1 == &_max2); \
79 _max1 > _max2 ? _max1 : _max2; })
81 #define min(x, y) ({ \
82 typeof(x) _min1 = (x); \
83 typeof(y) _min2 = (y); \
84 (void) (&_min1 == &_min2); \
85 _min1 < _min2 ? _min1 : _min2; })
87 #define min_t(type, x, y) ({ \
90 __min1 < __min2 ? __min1: __min2; })
92 #define max_t(type, x, y) ({ \
95 __max1 > __max2 ? __max1: __max2; })
97 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
100 do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
102 #define _RET_IP_ (unsigned long)__builtin_return_address(0)
103 #define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; })
105 #define might_sleep()
109 #define cpu_relax() do {} while (0)
110 #define cpu_relax_lowlatency() do {} while (0)
113 static inline void panic(const char *fmt, ...)
124 unsigned long simple_strtoul(const char *,char **,unsigned int);
125 long simple_strtol(const char *,char **,unsigned int);
126 unsigned long long simple_strtoull(const char *,char **,unsigned int);
127 long long simple_strtoll(const char *,char **,unsigned int);
129 int __must_check _kstrtoul(const char *s, unsigned int base, unsigned long *res);
130 int __must_check _kstrtol(const char *s, unsigned int base, long *res);
132 int __must_check kstrtoull(const char *s, unsigned int base, unsigned long long *res);
133 int __must_check kstrtoll(const char *s, unsigned int base, long long *res);
136 * kstrtoul - convert a string to an unsigned long
137 * @s: The start of the string. The string must be null-terminated, and may also
138 * include a single newline before its terminating null. The first character
139 * may also be a plus sign, but not a minus sign.
140 * @base: The number base to use. The maximum supported base is 16. If base is
141 * given as 0, then the base of the string is automatically detected with the
142 * conventional semantics - If it begins with 0x the number will be parsed as a
143 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
144 * parsed as an octal number. Otherwise it will be parsed as a decimal.
145 * @res: Where to write the result of the conversion on success.
147 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
148 * Used as a replacement for the obsolete simple_strtoull. Return code must
151 static inline int __must_check kstrtoul(const char *s, unsigned int base, unsigned long *res)
154 * We want to shortcut function call, but
155 * __builtin_types_compatible_p(unsigned long, unsigned long long) = 0.
157 if (sizeof(unsigned long) == sizeof(unsigned long long) &&
158 __alignof__(unsigned long) == __alignof__(unsigned long long))
159 return kstrtoull(s, base, (unsigned long long *)res);
161 return _kstrtoul(s, base, res);
165 * kstrtol - convert a string to a long
166 * @s: The start of the string. The string must be null-terminated, and may also
167 * include a single newline before its terminating null. The first character
168 * may also be a plus sign or a minus sign.
169 * @base: The number base to use. The maximum supported base is 16. If base is
170 * given as 0, then the base of the string is automatically detected with the
171 * conventional semantics - If it begins with 0x the number will be parsed as a
172 * hexadecimal (case insensitive), if it otherwise begins with 0, it will be
173 * parsed as an octal number. Otherwise it will be parsed as a decimal.
174 * @res: Where to write the result of the conversion on success.
176 * Returns 0 on success, -ERANGE on overflow and -EINVAL on parsing error.
177 * Used as a replacement for the obsolete simple_strtoull. Return code must
180 static inline int __must_check kstrtol(const char *s, unsigned int base, long *res)
183 * We want to shortcut function call, but
184 * __builtin_types_compatible_p(long, long long) = 0.
186 if (sizeof(long) == sizeof(long long) &&
187 __alignof__(long) == __alignof__(long long))
188 return kstrtoll(s, base, (long long *)res);
190 return _kstrtol(s, base, res);
193 int __must_check kstrtouint(const char *s, unsigned int base, unsigned int *res);
194 int __must_check kstrtoint(const char *s, unsigned int base, int *res);
196 /* Permissions on a sysfs file: you didn't miss the 0 prefix did you? */
197 #define VERIFY_OCTAL_PERMISSIONS(perms) \
198 (BUILD_BUG_ON_ZERO((perms) < 0) + \
199 BUILD_BUG_ON_ZERO((perms) > 0777) + \
200 /* USER_READABLE >= GROUP_READABLE >= OTHER_READABLE */ \
201 BUILD_BUG_ON_ZERO((((perms) >> 6) & 4) < (((perms) >> 3) & 4)) + \
202 BUILD_BUG_ON_ZERO((((perms) >> 3) & 4) < ((perms) & 4)) + \
203 /* USER_WRITABLE >= GROUP_WRITABLE */ \
204 BUILD_BUG_ON_ZERO((((perms) >> 6) & 2) < (((perms) >> 3) & 2)) + \
205 /* OTHER_WRITABLE? Generally considered a bad idea. */ \
206 BUILD_BUG_ON_ZERO((perms) & 2) + \
209 #define offset_in_page(p) ((unsigned long)(p) & ~PAGE_MASK)