1 /*-
2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6 * Copyright (c) 2014-2015 François Tigeot
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #ifndef _LINUXKPI_LINUX_KERNEL_H_
31 #define _LINUXKPI_LINUX_KERNEL_H_
32
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/param.h>
36 #include <sys/libkern.h>
37 #include <sys/stat.h>
38 #include <sys/smp.h>
39 #include <sys/stddef.h>
40 #include <sys/syslog.h>
41 #include <sys/time.h>
42
43 #include <linux/array_size.h>
44 #include <linux/bitops.h>
45 #include <linux/build_bug.h>
46 #include <linux/compiler.h>
47 #include <linux/container_of.h>
48 #include <linux/kstrtox.h>
49 #include <linux/limits.h>
50 #include <linux/math.h>
51 #include <linux/minmax.h>
52 #include <linux/stringify.h>
53 #include <linux/errno.h>
54 #include <linux/types.h>
55 #include <linux/typecheck.h>
56 #include <linux/jiffies.h>
57 #include <linux/log2.h>
58 #include <linux/kconfig.h>
59 #include <linux/instruction_pointer.h>
60 #include <linux/hex.h>
61 #include <linux/wordpart.h>
62
63 #include <asm/byteorder.h>
64 #include <asm/cpufeature.h>
65 #include <asm/processor.h>
66 #include <asm/uaccess.h>
67
68 #include <linux/stdarg.h>
69
70 #define KERN_CONT ""
71 #define KERN_EMERG "<0>"
72 #define KERN_ALERT "<1>"
73 #define KERN_CRIT "<2>"
74 #define KERN_ERR "<3>"
75 #define KERN_WARNING "<4>"
76 #define KERN_NOTICE "<5>"
77 #define KERN_INFO "<6>"
78 #define KERN_DEBUG "<7>"
79
80 #define S8_C(x) x
81 #define U8_C(x) x ## U
82 #define S16_C(x) x
83 #define U16_C(x) x ## U
84 #define S32_C(x) x
85 #define U32_C(x) x ## U
86 #define S64_C(x) x ## LL
87 #define U64_C(x) x ## ULL
88
89 #define BUG() panic("BUG at %s:%d", __FILE__, __LINE__)
90 #define BUG_ON(cond) do { \
91 if (cond) { \
92 panic("BUG ON %s failed at %s:%d", \
93 __stringify(cond), __FILE__, __LINE__); \
94 } \
95 } while (0)
96
97 extern int linuxkpi_warn_dump_stack;
98 #define WARN_ON(cond) ({ \
99 bool __ret = (cond); \
100 if (__ret) { \
101 printf("WARNING %s failed at %s:%d\n", \
102 __stringify(cond), __FILE__, __LINE__); \
103 if (linuxkpi_warn_dump_stack) \
104 linux_dump_stack(); \
105 } \
106 unlikely(__ret); \
107 })
108
109 #define WARN_ON_SMP(cond) WARN_ON(cond)
110
111 #define WARN_ON_ONCE(cond) ({ \
112 static bool __warn_on_once; \
113 bool __ret = (cond); \
114 if (__ret && !__warn_on_once) { \
115 __warn_on_once = 1; \
116 printf("WARNING %s failed at %s:%d\n", \
117 __stringify(cond), __FILE__, __LINE__); \
118 if (linuxkpi_warn_dump_stack) \
119 linux_dump_stack(); \
120 } \
121 unlikely(__ret); \
122 })
123
124 #define oops_in_progress SCHEDULER_STOPPED()
125
126 #undef ALIGN
127 #define ALIGN(x, y) roundup2((x), (y))
128 #define ALIGN_DOWN(x, y) rounddown2(x, y)
129 #undef PTR_ALIGN
130 #define PTR_ALIGN(p, a) ((__typeof(p))ALIGN((uintptr_t)(p), (a)))
131 #define IS_ALIGNED(x, a) (((x) & ((__typeof(x))(a) - 1)) == 0)
132 #define __KERNEL_DIV_ROUND_UP(x, n) howmany(x, n)
133 #define FIELD_SIZEOF(t, f) sizeof(((t *)0)->f)
134
135 #define printk(...) printf(__VA_ARGS__)
136 #define vprintk(f, a) vprintf(f, a)
137
138 #define PTR_IF(x, p) ((x) ? (p) : NULL)
139
140 #define asm __asm
141
142 extern void linux_dump_stack(void);
143 #define dump_stack() linux_dump_stack()
144
145 struct va_format {
146 const char *fmt;
147 va_list *va;
148 };
149
150 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list args)151 vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
152 {
153 ssize_t ssize = size;
154 int i;
155
156 i = vsnprintf(buf, size, fmt, args);
157
158 return ((i >= ssize) ? (ssize - 1) : i);
159 }
160
161 static inline int
scnprintf(char * buf,size_t size,const char * fmt,...)162 scnprintf(char *buf, size_t size, const char *fmt, ...)
163 {
164 va_list args;
165 int i;
166
167 va_start(args, fmt);
168 i = vscnprintf(buf, size, fmt, args);
169 va_end(args);
170
171 return (i);
172 }
173
174 /*
175 * The "pr_debug()" and "pr_devel()" macros should produce zero code
176 * unless DEBUG is defined:
177 */
178 #ifdef DEBUG
179 extern int linuxkpi_debug;
180 #define pr_debug(fmt, ...) \
181 do { \
182 if (linuxkpi_debug) \
183 log(LOG_DEBUG, fmt, ##__VA_ARGS__); \
184 } while (0)
185 #define pr_devel(fmt, ...) \
186 log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__)
187 #else
188 #define pr_debug(fmt, ...) \
189 ({ if (0) log(LOG_DEBUG, fmt, ##__VA_ARGS__); 0; })
190 #define pr_devel(fmt, ...) \
191 ({ if (0) log(LOG_DEBUG, pr_fmt(fmt), ##__VA_ARGS__); 0; })
192 #endif
193
194 #ifndef pr_fmt
195 #define pr_fmt(fmt) fmt
196 #endif
197
198 /*
199 * Print a one-time message (analogous to WARN_ONCE() et al):
200 */
201 #define printk_once(...) do { \
202 static bool __print_once; \
203 \
204 if (!__print_once) { \
205 __print_once = true; \
206 printk(__VA_ARGS__); \
207 } \
208 } while (0)
209
210 /*
211 * Log a one-time message (analogous to WARN_ONCE() et al):
212 */
213 #define log_once(level,...) do { \
214 static bool __log_once; \
215 \
216 if (unlikely(!__log_once)) { \
217 __log_once = true; \
218 log(level, __VA_ARGS__); \
219 } \
220 } while (0)
221
222 #define pr_emerg(fmt, ...) \
223 log(LOG_EMERG, pr_fmt(fmt), ##__VA_ARGS__)
224 #define pr_alert(fmt, ...) \
225 log(LOG_ALERT, pr_fmt(fmt), ##__VA_ARGS__)
226 #define pr_crit(fmt, ...) \
227 log(LOG_CRIT, pr_fmt(fmt), ##__VA_ARGS__)
228 #define pr_err(fmt, ...) \
229 log(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
230 #define pr_err_once(fmt, ...) \
231 log_once(LOG_ERR, pr_fmt(fmt), ##__VA_ARGS__)
232 #define pr_warning(fmt, ...) \
233 log(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
234 #define pr_warn(...) \
235 pr_warning(__VA_ARGS__)
236 #define pr_warn_once(fmt, ...) \
237 log_once(LOG_WARNING, pr_fmt(fmt), ##__VA_ARGS__)
238 #define pr_notice(fmt, ...) \
239 log(LOG_NOTICE, pr_fmt(fmt), ##__VA_ARGS__)
240 #define pr_info(fmt, ...) \
241 log(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
242 #define pr_info_once(fmt, ...) \
243 log_once(LOG_INFO, pr_fmt(fmt), ##__VA_ARGS__)
244 #define pr_cont(fmt, ...) \
245 printk(KERN_CONT fmt, ##__VA_ARGS__)
246 #define pr_warn_ratelimited(...) do { \
247 static linux_ratelimit_t __ratelimited; \
248 if (linux_ratelimited(&__ratelimited)) \
249 pr_warning(__VA_ARGS__); \
250 } while (0)
251
252 #ifndef WARN
253 #define WARN(condition, ...) ({ \
254 bool __ret_warn_on = (condition); \
255 if (unlikely(__ret_warn_on)) \
256 pr_warning(__VA_ARGS__); \
257 unlikely(__ret_warn_on); \
258 })
259 #endif
260
261 #ifndef WARN_ONCE
262 #define WARN_ONCE(condition, ...) ({ \
263 bool __ret_warn_on = (condition); \
264 if (unlikely(__ret_warn_on)) \
265 pr_warn_once(__VA_ARGS__); \
266 unlikely(__ret_warn_on); \
267 })
268 #endif
269
270 #define u64_to_user_ptr(val) ((void *)(uintptr_t)(val))
271
272 #define offsetofend(t, m) \
273 (offsetof(t, m) + sizeof((((t *)0)->m)))
274
275 #define smp_processor_id() PCPU_GET(cpuid)
276 #define num_possible_cpus() mp_ncpus
277 #define num_online_cpus() mp_ncpus
278
279 #if defined(__i386__) || defined(__amd64__)
280 extern bool linux_cpu_has_clflush;
281 #define cpu_has_clflush linux_cpu_has_clflush
282 #endif
283
284 typedef struct linux_ratelimit {
285 struct timeval lasttime;
286 int counter;
287 } linux_ratelimit_t;
288
289 static inline bool
linux_ratelimited(linux_ratelimit_t * rl)290 linux_ratelimited(linux_ratelimit_t *rl)
291 {
292 return (ppsratecheck(&rl->lasttime, &rl->counter, 1));
293 }
294
295 #define __is_constexpr(x) \
296 __builtin_constant_p(x)
297
298 /*
299 * The is_signed() macro below returns true if the passed data type is
300 * signed. Else false is returned.
301 */
302 #define is_signed(datatype) (((datatype)-1 / (datatype)2) == (datatype)0)
303
304 #define TAINT_WARN 0
305 #define test_taint(x) (0)
306 #define add_taint(x,y) do { \
307 } while (0)
308
309 static inline bool
mac_pton(const char * macin,uint8_t * macout)310 mac_pton(const char *macin, uint8_t *macout)
311 {
312 const char *s, *d;
313 uint8_t mac[6], hx, lx;
314 int i;
315
316 if (strlen(macin) < (3 * 6 - 1))
317 return (false);
318
319 i = 0;
320 s = macin;
321 do {
322 /* Should we also support '-'-delimiters? */
323 d = strchrnul(s, ':');
324 hx = lx = 0;
325 while (s < d) {
326 /* Fail on abc:123:xxx:... */
327 if ((d - s) > 2)
328 return (false);
329 /* We do support non-well-formed strings: 3:45:6:... */
330 if ((d - s) > 1) {
331 hx = _h2b(*s);
332 if (hx < 0)
333 return (false);
334 s++;
335 }
336 lx = _h2b(*s);
337 if (lx < 0)
338 return (false);
339 s++;
340 }
341 mac[i] = (hx << 4) | lx;
342 i++;
343 if (i >= 6)
344 return (false);
345 } while (d != NULL && *d != '\0');
346
347 memcpy(macout, mac, 6);
348 return (true);
349 }
350
351 #define DECLARE_FLEX_ARRAY(_t, _n) \
352 struct { struct { } __dummy_ ## _n; _t _n[0]; }
353
354 /*
355 * The following functions/macros are debug/diagnostics tools. They default to
356 * no-ops, except `might_sleep()` which uses `WITNESS_WARN()` on FreeBSD.
357 */
358 static inline void
__might_resched(const char * file,int line,unsigned int offsets)359 __might_resched(const char *file, int line, unsigned int offsets)
360 {
361 }
362
363 static inline void
__might_sleep(const char * file,int line)364 __might_sleep(const char *file, int line)
365 {
366 }
367
368 static inline void
might_fault(void)369 might_fault(void)
370 {
371 }
372
373 #define might_sleep() \
374 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "might_sleep()")
375
376 #define might_sleep_if(cond) do { \
377 if (cond) { might_sleep(); } \
378 } while (0)
379
380 #define might_resched() do { } while (0)
381 #define cant_sleep() do { } while (0)
382 #define cant_migrate() do { } while (0)
383 #define sched_annotate_sleep() do { } while (0)
384 #define non_block_start() do { } while (0)
385 #define non_block_end() do { } while (0)
386
387 extern enum system_states {
388 SYSTEM_BOOTING,
389 SYSTEM_SCHEDULING,
390 SYSTEM_FREEING_INITMEM,
391 SYSTEM_RUNNING,
392 SYSTEM_HALT,
393 SYSTEM_POWER_OFF,
394 SYSTEM_RESTART,
395 SYSTEM_SUSPEND,
396 } system_state;
397
398 #endif /* _LINUXKPI_LINUX_KERNEL_H_ */
399