1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
23
24
25 /*
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
28 */
29
30 #ifndef _SYS_SYSMACROS_H
31 #define _SYS_SYSMACROS_H
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/isa_defs.h>
36 #include <sys/libkern.h>
37 #include <sys/zone.h>
38 #include <sys/condvar.h>
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 /*
45 * Some macros for units conversion
46 */
47 /*
48 * Disk blocks (sectors) and bytes.
49 */
50 #define dtob(DD) ((DD) << DEV_BSHIFT)
51 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
52 #define btodt(BB) ((BB) >> DEV_BSHIFT)
53 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
54
55 /* common macros */
56 #ifndef MIN
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 #endif
59 #ifndef MAX
60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 #endif
62 #ifndef ABS
63 #define ABS(a) ((a) < 0 ? -(a) : (a))
64 #endif
65 #ifndef SIGNOF
66 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
67 #endif
68 #ifndef ARRAY_SIZE
69 #define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
70 #endif
71 #ifndef DIV_ROUND_UP
72 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
73 #endif
74
75 #ifdef _STANDALONE
76 #define boot_ncpus 1
77 #else /* _STANDALONE */
78 #define boot_ncpus mp_ncpus
79 #endif /* _STANDALONE */
80 #define kpreempt_disable() critical_enter()
81 #define kpreempt_enable() critical_exit()
82 #define CPU_SEQID curcpu
83 #define CPU_SEQID_UNSTABLE curcpu
84 #define is_system_labeled() 0
85 /*
86 * Convert a single byte to/from binary-coded decimal (BCD).
87 */
88 extern unsigned char byte_to_bcd[256];
89 extern unsigned char bcd_to_byte[256];
90
91 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
92 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
93
94 /*
95 * WARNING: The device number macros defined here should not be used by device
96 * drivers or user software. Device drivers should use the device functions
97 * defined in the DDI/DKI interface (see also ddi.h). Application software
98 * should make use of the library routines available in makedev(3). A set of
99 * new device macros are provided to operate on the expanded device number
100 * format supported in SVR4. Macro versions of the DDI device functions are
101 * provided for use by kernel proper routines only. Macro routines bmajor(),
102 * major(), minor(), emajor(), eminor(), and makedev() will be removed or
103 * their definitions changed at the next major release following SVR4.
104 */
105
106 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
107 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
108 #define O_MAXMAJ 0x7f /* SVR3 max major value */
109 #define O_MAXMIN 0xff /* SVR3 max minor value */
110
111
112 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
113 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
114 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
115 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
116 /* For 3b2 hardware devices the minor is */
117 /* restricted to 256 (0-255) */
118
119 #ifdef _LP64
120 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
121 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
122 #define L_MAXMAJ 0xfffffffful /* max major value */
123 #define L_MAXMIN 0xfffffffful /* max minor value */
124 #else
125 #define L_BITSMAJOR L_BITSMAJOR32
126 #define L_BITSMINOR L_BITSMINOR32
127 #define L_MAXMAJ L_MAXMAJ32
128 #define L_MAXMIN L_MAXMIN32
129 #endif
130
131 /*
132 * These are versions of the kernel routines for compressing and
133 * expanding long device numbers that don't return errors.
134 */
135 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
136
137 #define DEVCMPL(x) (x)
138 #define DEVEXPL(x) (x)
139
140 #else
141
142 #define DEVCMPL(x) \
143 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
144 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
145 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
146
147 #define DEVEXPL(x) \
148 (((x) == NODEV32) ? NODEV : \
149 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
150
151 #endif /* L_BITSMAJOR32 ... */
152
153 /* convert to old (SVR3.2) dev format */
154
155 #define cmpdev(x) \
156 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
157 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
158 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
159
160 /* convert to new (SVR4) dev format */
161
162 #define expdev(x) \
163 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
164 ((x) & O_MAXMIN))
165
166 /*
167 * Macro for checking power of 2 address alignment.
168 */
169 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
170
171 /*
172 * Macros for counting and rounding.
173 */
174 #define howmany(x, y) (((x)+((y)-1))/(y))
175 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
176
177 /*
178 * Macro to determine if value is a power of 2
179 */
180 #define ISP2(x) (((x) & ((x) - 1)) == 0)
181
182 /*
183 * Macros for various sorts of alignment and rounding. The "align" must
184 * be a power of 2. Often times it is a block, sector, or page.
185 */
186
187 /*
188 * return x rounded down to an align boundary
189 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
190 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
191 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
192 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
193 */
194 #define P2ALIGN(x, align) ((x) & -(align))
195
196 /*
197 * return x % (mod) align
198 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
199 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
200 */
201 #define P2PHASE(x, align) ((x) & ((align) - 1))
202
203 /*
204 * return how much space is left in this block (but if it's perfectly
205 * aligned, return 0).
206 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
207 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
208 */
209 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
210
211 /*
212 * return x rounded up to an align boundary
213 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
214 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
215 */
216 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
217
218 /*
219 * return the ending address of the block that x is in
220 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
221 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
222 */
223 #define P2END(x, align) (-(~(x) & -(align)))
224
225 /*
226 * return x rounded up to the next phase (offset) within align.
227 * phase should be < align.
228 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
229 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
230 */
231 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
232
233 /*
234 * return TRUE if adding len to off would cause it to cross an align
235 * boundary.
236 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
237 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
238 */
239 #define P2BOUNDARY(off, len, align) \
240 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
241
242 /*
243 * Return TRUE if they have the same highest bit set.
244 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
245 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
246 */
247 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
248
249 /*
250 * Typed version of the P2* macros. These macros should be used to ensure
251 * that the result is correctly calculated based on the data type of (x),
252 * which is passed in as the last argument, regardless of the data
253 * type of the alignment. For example, if (x) is of type uint64_t,
254 * and we want to round it up to a page boundary using "PAGESIZE" as
255 * the alignment, we can do either
256 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
257 * or
258 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
259 */
260 #define P2ALIGN_TYPED(x, align, type) \
261 ((type)(x) & -(type)(align))
262 #define P2PHASE_TYPED(x, align, type) \
263 ((type)(x) & ((type)(align) - 1))
264 #define P2NPHASE_TYPED(x, align, type) \
265 (-(type)(x) & ((type)(align) - 1))
266 #define P2ROUNDUP_TYPED(x, align, type) \
267 (-(-(type)(x) & -(type)(align)))
268 #define P2END_TYPED(x, align, type) \
269 (-(~(type)(x) & -(type)(align)))
270 #define P2PHASEUP_TYPED(x, align, phase, type) \
271 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
272 #define P2CROSS_TYPED(x, y, align, type) \
273 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
274 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
275 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
276
277 /*
278 * Macros to atomically increment/decrement a variable. mutex and var
279 * must be pointers.
280 */
281 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
282 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
283
284 #if !defined(_KMEMUSER) && !defined(offsetof)
285
286 /* avoid any possibility of clashing with <stddef.h> version */
287
288 #define offsetof(type, field) __offsetof(type, field)
289 #endif
290
291 /*
292 * Find highest one bit set.
293 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
294 * High order bit is 31 (or 63 in _LP64 kernel).
295 */
296 static __inline int
297 highbit(ulong_t i)
298 {
299 #if defined(HAVE_INLINE_FLSL)
300 return (flsl(i));
301 #else
302 int h = 1;
303
304 if (i == 0)
305 return (0);
306 #ifdef _LP64
307 if (i & 0xffffffff00000000ul) {
308 h += 32; i >>= 32;
309 }
310 #endif
311 if (i & 0xffff0000) {
312 h += 16; i >>= 16;
313 }
314 if (i & 0xff00) {
315 h += 8; i >>= 8;
316 }
317 if (i & 0xf0) {
318 h += 4; i >>= 4;
319 }
320 if (i & 0xc) {
321 h += 2; i >>= 2;
322 }
323 if (i & 0x2) {
324 h += 1;
325 }
326 return (h);
327 #endif
328 }
329
330 /*
331 * Find highest one bit set.
332 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
333 */
334 static __inline int
335 highbit64(uint64_t i)
336 {
337 #if defined(HAVE_INLINE_FLSLL)
338 return (flsll(i));
339 #else
340 int h = 1;
341
342 if (i == 0)
343 return (0);
344 if (i & 0xffffffff00000000ULL) {
345 h += 32; i >>= 32;
346 }
347 if (i & 0xffff0000) {
348 h += 16; i >>= 16;
349 }
350 if (i & 0xff00) {
351 h += 8; i >>= 8;
352 }
353 if (i & 0xf0) {
354 h += 4; i >>= 4;
355 }
356 if (i & 0xc) {
357 h += 2; i >>= 2;
358 }
359 if (i & 0x2) {
360 h += 1;
361 }
362 return (h);
363 #endif
364 }
365
366 #ifdef __cplusplus
367 }
368 #endif
369
370 #endif /* _SYS_SYSMACROS_H */
Cache object: ef6fd89c2167879a01cea230bf2eb482
|