kernel.h revision 1.13 1 1.10 riastrad /* $NetBSD: kernel.h,v 1.13 2018/08/27 06:15:32 riastradh Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Taylor R. Campbell.
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #ifndef _LINUX_KERNEL_H_
33 1.1 skrll #define _LINUX_KERNEL_H_
34 1.1 skrll
35 1.1 skrll #include <sys/cdefs.h>
36 1.1 skrll #include <sys/types.h>
37 1.1 skrll #include <sys/param.h>
38 1.3 riastrad #include <sys/systm.h>
39 1.3 riastrad
40 1.7 riastrad #include <lib/libkern/libkern.h>
41 1.13 riastrad #include <linux/bitops.h>
42 1.10 riastrad #include <linux/printk.h>
43 1.7 riastrad
44 1.12 riastrad #define U16_MAX UINT16_MAX
45 1.12 riastrad #define U32_MAX UINT32_MAX
46 1.12 riastrad #define U64_MAX UINT64_MAX
47 1.12 riastrad
48 1.3 riastrad #define oops_in_progress (panicstr != NULL)
49 1.3 riastrad
50 1.8 skrll #define IS_ENABLED(option) (option)
51 1.11 riastrad #define IS_BUILTIN(option) (1) /* Probably... */
52 1.1 skrll
53 1.1 skrll #define __printf __printflike
54 1.1 skrll #define __user
55 1.1 skrll #define __must_check /* __attribute__((warn_unused_result)), if GCC */
56 1.1 skrll #define __always_unused __unused
57 1.1 skrll
58 1.1 skrll #define barrier() __insn_barrier()
59 1.3 riastrad #define likely(X) __predict_true(X)
60 1.1 skrll #define unlikely(X) __predict_false(X)
61 1.1 skrll
62 1.2 riastrad /*
63 1.2 riastrad * XXX Linux kludge to work around GCC uninitialized variable warning.
64 1.2 riastrad * Linux does `x = x', which is bollocks.
65 1.2 riastrad */
66 1.2 riastrad #define uninitialized_var(x) x = 0
67 1.1 skrll
68 1.1 skrll /* XXX These will multiply evaluate their arguments. */
69 1.9 riastrad #define min(X, Y) MIN(X, Y)
70 1.9 riastrad #define max(X, Y) MAX(X, Y)
71 1.9 riastrad
72 1.1 skrll #define max_t(T, X, Y) MAX(X, Y)
73 1.1 skrll #define min_t(T, X, Y) MIN(X, Y)
74 1.1 skrll
75 1.3 riastrad #define clamp_t(T, X, MIN, MAX) min_t(T, max_t(T, X, MIN), MAX)
76 1.6 riastrad #define clamp(X, MN, MX) MIN(MAX(X, MN), MX)
77 1.3 riastrad
78 1.1 skrll /*
79 1.2 riastrad * Rounding to nearest.
80 1.2 riastrad */
81 1.2 riastrad #define DIV_ROUND_CLOSEST(N, D) \
82 1.2 riastrad ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \
83 1.2 riastrad : (((N) - ((D) / 2)) / (D)))
84 1.2 riastrad
85 1.2 riastrad /*
86 1.1 skrll * Rounding to what may or may not be powers of two.
87 1.1 skrll */
88 1.1 skrll #define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N))
89 1.3 riastrad #define DIV_ROUND_UP_ULL(X, N) DIV_ROUND_UP((unsigned long long)(X), (N))
90 1.1 skrll
91 1.1 skrll /*
92 1.1 skrll * Rounding to powers of two -- carefully avoiding multiple evaluation
93 1.1 skrll * of arguments and pitfalls with C integer arithmetic rules.
94 1.1 skrll */
95 1.1 skrll #define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
96 1.1 skrll #define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
97 1.1 skrll
98 1.4 riastrad #define IS_ALIGNED(X, N) (((X) & ((N) - 1)) == 0)
99 1.4 riastrad
100 1.1 skrll /*
101 1.1 skrll * These select 32-bit halves of what may be 32- or 64-bit quantities,
102 1.1 skrll * for which straight 32-bit shifts may be undefined behaviour (and do
103 1.1 skrll * the wrong thing on most machines: return the input unshifted by
104 1.1 skrll * ignoring the upper bits of the shift count).
105 1.1 skrll */
106 1.1 skrll #define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
107 1.1 skrll #define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
108 1.1 skrll
109 1.1 skrll #define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
110 1.1 skrll
111 1.1 skrll #define swap(X, Y) do \
112 1.1 skrll { \
113 1.1 skrll /* XXX Kludge for type-safety. */ \
114 1.1 skrll if (&(X) != &(Y)) { \
115 1.1 skrll CTASSERT(sizeof(X) == sizeof(Y)); \
116 1.1 skrll /* XXX Can't do this much better without typeof. */ \
117 1.1 skrll char __swap_tmp[sizeof(X)]; \
118 1.1 skrll (void)memcpy(__swap_tmp, &(X), sizeof(X)); \
119 1.1 skrll (void)memcpy(&(X), &(Y), sizeof(X)); \
120 1.1 skrll (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \
121 1.1 skrll } \
122 1.1 skrll } while (0)
123 1.1 skrll
124 1.1 skrll static inline int64_t
125 1.1 skrll abs64(int64_t x)
126 1.1 skrll {
127 1.1 skrll return (x < 0? (-x) : x);
128 1.1 skrll }
129 1.1 skrll
130 1.3 riastrad static inline uintmax_t
131 1.3 riastrad mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
132 1.3 riastrad {
133 1.3 riastrad uintmax_t q = (x / divisor);
134 1.3 riastrad uintmax_t r = (x % divisor);
135 1.3 riastrad
136 1.3 riastrad return ((q * multiplier) + ((r * multiplier) / divisor));
137 1.3 riastrad }
138 1.3 riastrad
139 1.2 riastrad static int panic_timeout __unused = 0;
140 1.2 riastrad
141 1.3 riastrad static inline int
142 1.3 riastrad vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
143 1.3 riastrad {
144 1.3 riastrad int ret;
145 1.3 riastrad
146 1.3 riastrad ret = vsnprintf(buf, size, fmt, va);
147 1.3 riastrad if (__predict_false(ret < 0))
148 1.3 riastrad return ret;
149 1.3 riastrad if (__predict_false(size == 0))
150 1.3 riastrad return 0;
151 1.3 riastrad if (__predict_false(size <= ret))
152 1.3 riastrad return (size - 1);
153 1.3 riastrad
154 1.3 riastrad return ret;
155 1.3 riastrad }
156 1.3 riastrad
157 1.3 riastrad static inline int
158 1.3 riastrad scnprintf(char *buf, size_t size, const char *fmt, ...)
159 1.3 riastrad {
160 1.3 riastrad va_list va;
161 1.3 riastrad int ret;
162 1.3 riastrad
163 1.3 riastrad va_start(va, fmt);
164 1.3 riastrad ret = vscnprintf(buf, size, fmt, va);
165 1.3 riastrad va_end(va);
166 1.3 riastrad
167 1.3 riastrad return ret;
168 1.3 riastrad }
169 1.3 riastrad
170 1.5 riastrad static inline int
171 1.5 riastrad kstrtol(const char *s, unsigned base, long *vp)
172 1.5 riastrad {
173 1.5 riastrad long long v;
174 1.5 riastrad
175 1.5 riastrad v = strtoll(s, NULL, base);
176 1.5 riastrad if (v < LONG_MIN || LONG_MAX < v)
177 1.5 riastrad return -ERANGE;
178 1.5 riastrad *vp = v;
179 1.5 riastrad return 0;
180 1.5 riastrad }
181 1.5 riastrad
182 1.1 skrll #endif /* _LINUX_KERNEL_H_ */
183