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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
31 * Copyright 2018 Joyent Inc.
34 #ifndef _SYS_SYSMACROS_H
35 #define _SYS_SYSMACROS_H
37 #include <sys/param.h>
38 #include <sys/stddef.h>
45 * Some macros for units conversion
48 * Disk blocks (sectors) and bytes.
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)
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
63 #define ABS(a) ((a) < 0 ? -(a) : (a))
66 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
72 * Convert a single byte to/from binary-coded decimal (BCD).
74 extern unsigned char byte_to_bcd
[256];
75 extern unsigned char bcd_to_byte
[256];
77 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
78 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
83 * WARNING: The device number macros defined here should not be used by device
84 * drivers or user software. Device drivers should use the device functions
85 * defined in the DDI/DKI interface (see also ddi.h). Application software
86 * should make use of the library routines available in makedev(3C). A set of
87 * new device macros are provided to operate on the expanded device number
88 * format supported in SVR4. Macro versions of the DDI device functions are
89 * provided for use by kernel proper routines only.
92 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
93 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
94 #define O_MAXMAJ 0x7f /* SVR3 max major value */
95 #define O_MAXMIN 0xff /* SVR3 max minor value */
98 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
99 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
100 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
101 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
102 /* For 3b2 hardware devices the minor is */
103 /* restricted to 256 (0-255) */
106 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
107 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
108 #define L_MAXMAJ 0xfffffffful /* max major value */
109 #define L_MAXMIN 0xfffffffful /* max minor value */
111 #define L_BITSMAJOR L_BITSMAJOR32
112 #define L_BITSMINOR L_BITSMINOR32
113 #define L_MAXMAJ L_MAXMAJ32
114 #define L_MAXMIN L_MAXMIN32
119 /* get internal major part of expanded device number */
121 #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
123 /* get internal minor part of expanded device number */
125 #define getminor(x) (minor_t)((x) & L_MAXMIN)
129 /* make an new device number */
131 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
134 * get external major and minor device
135 * components from expanded device number
137 #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
138 NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
139 #define geteminor(x) (minor_t)((x) & L_MAXMIN)
142 * These are versions of the kernel routines for compressing and
143 * expanding long device numbers that don't return errors.
145 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
147 #define DEVCMPL(x) (x)
148 #define DEVEXPL(x) (x)
153 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
154 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
155 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
158 (((x) == NODEV32) ? NODEV : \
159 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
161 #endif /* L_BITSMAJOR32 ... */
163 /* convert to old (SVR3.2) dev format */
166 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
167 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
168 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
170 /* convert to new (SVR4) dev format */
173 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
177 * Macro for checking power of 2 address alignment.
179 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
182 * Macros for counting and rounding.
184 #define howmany(x, y) (((x)+((y)-1))/(y))
185 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
188 * Macro to determine if value is a power of 2
190 #define ISP2(x) (((x) & ((x) - 1)) == 0)
193 * Macros for various sorts of alignment and rounding. The "align" must
194 * be a power of 2. Often times it is a block, sector, or page.
198 * return x rounded down to an align boundary
199 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
200 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
201 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
202 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
204 #define P2ALIGN(x, align) ((x) & -(align))
207 * return x % (mod) align
208 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
209 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
211 #define P2PHASE(x, align) ((x) & ((align) - 1))
214 * return how much space is left in this block (but if it's perfectly
215 * aligned, return 0).
216 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
217 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
219 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
222 * return x rounded up to an align boundary
223 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
224 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
226 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
229 * return the ending address of the block that x is in
230 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
231 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
233 #define P2END(x, align) (-(~(x) & -(align)))
236 * return x rounded up to the next phase (offset) within align.
237 * phase should be < align.
238 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
239 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
241 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
244 * return TRUE if adding len to off would cause it to cross an align
246 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
247 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
249 #define P2BOUNDARY(off, len, align) \
250 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
253 * Return TRUE if they have the same highest bit set.
254 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
255 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
257 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
260 * Typed version of the P2* macros. These macros should be used to ensure
261 * that the result is correctly calculated based on the data type of (x),
262 * which is passed in as the last argument, regardless of the data
263 * type of the alignment. For example, if (x) is of type uint64_t,
264 * and we want to round it up to a page boundary using "PAGESIZE" as
265 * the alignment, we can do either
266 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
268 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
270 #define P2ALIGN_TYPED(x, align, type) \
271 ((type)(x) & -(type)(align))
272 #define P2PHASE_TYPED(x, align, type) \
273 ((type)(x) & ((type)(align) - 1))
274 #define P2NPHASE_TYPED(x, align, type) \
275 (-(type)(x) & ((type)(align) - 1))
276 #define P2ROUNDUP_TYPED(x, align, type) \
277 (-(-(type)(x) & -(type)(align)))
278 #define P2END_TYPED(x, align, type) \
279 (-(~(type)(x) & -(type)(align)))
280 #define P2PHASEUP_TYPED(x, align, phase, type) \
281 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
282 #define P2CROSS_TYPED(x, y, align, type) \
283 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
284 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
285 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
288 * Macros to atomically increment/decrement a variable. mutex and var
291 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
292 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
295 * Macros to declare bitfields - the order in the parameter list is
296 * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
297 * because if a field crosses a byte boundary it's not likely to be meaningful
298 * without reassembly in its nonnative endianness.
300 #if defined(_BIT_FIELDS_LTOH)
301 #define DECL_BITFIELD2(_a, _b) \
303 #define DECL_BITFIELD3(_a, _b, _c) \
305 #define DECL_BITFIELD4(_a, _b, _c, _d) \
306 uint8_t _a, _b, _c, _d
307 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
308 uint8_t _a, _b, _c, _d, _e
309 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
310 uint8_t _a, _b, _c, _d, _e, _f
311 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
312 uint8_t _a, _b, _c, _d, _e, _f, _g
313 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
314 uint8_t _a, _b, _c, _d, _e, _f, _g, _h
315 #elif defined(_BIT_FIELDS_HTOL)
316 #define DECL_BITFIELD2(_a, _b) \
318 #define DECL_BITFIELD3(_a, _b, _c) \
320 #define DECL_BITFIELD4(_a, _b, _c, _d) \
321 uint8_t _d, _c, _b, _a
322 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
323 uint8_t _e, _d, _c, _b, _a
324 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
325 uint8_t _f, _e, _d, _c, _b, _a
326 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
327 uint8_t _g, _f, _e, _d, _c, _b, _a
328 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
329 uint8_t _h, _g, _f, _e, _d, _c, _b, _a
331 #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
332 #endif /* _BIT_FIELDS_LTOH */
334 #if !defined(ARRAY_SIZE)
335 #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
339 * Add a value to a uint64_t that saturates at UINT64_MAX instead of wrapping
342 #define UINT64_OVERFLOW_ADD(val, add) \
343 ((val) > ((val) + (add)) ? (UINT64_MAX) : ((val) + (add)))
346 * Convert to an int64, saturating at INT64_MAX.
348 #define UINT64_OVERFLOW_TO_INT64(uval) \
349 (((uval) > INT64_MAX) ? INT64_MAX : (int64_t)(uval))
355 #endif /* _SYS_SYSMACROS_H */