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.
30 #ifndef _SYS_SYSMACROS_H
31 #define _SYS_SYSMACROS_H
33 #include <sys/param.h>
34 #include <sys/isa_defs.h>
35 #if defined(__FreeBSD__) && defined(_KERNEL)
36 #include <sys/libkern.h>
44 * Some macros for units conversion
47 * Disk blocks (sectors) and bytes.
49 #define dtob(DD) ((DD) << DEV_BSHIFT)
50 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
51 #define btodt(BB) ((BB) >> DEV_BSHIFT)
52 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
56 #define MIN(a, b) ((a) < (b) ? (a) : (b))
59 #define MAX(a, b) ((a) < (b) ? (b) : (a))
62 #define ABS(a) ((a) < 0 ? -(a) : (a))
65 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
71 * Convert a single byte to/from binary-coded decimal (BCD).
73 extern unsigned char byte_to_bcd[256];
74 extern unsigned char bcd_to_byte[256];
76 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
77 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
82 * WARNING: The device number macros defined here should not be used by device
83 * drivers or user software. Device drivers should use the device functions
84 * defined in the DDI/DKI interface (see also ddi.h). Application software
85 * should make use of the library routines available in makedev(3). A set of
86 * new device macros are provided to operate on the expanded device number
87 * format supported in SVR4. Macro versions of the DDI device functions are
88 * provided for use by kernel proper routines only. Macro routines bmajor(),
89 * major(), minor(), emajor(), eminor(), and makedev() will be removed or
90 * their definitions changed at the next major release following SVR4.
93 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
94 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
95 #define O_MAXMAJ 0x7f /* SVR3 max major value */
96 #define O_MAXMIN 0xff /* SVR3 max minor value */
99 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
100 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
101 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
102 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
103 /* For 3b2 hardware devices the minor is */
104 /* restricted to 256 (0-255) */
107 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
108 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
109 #define L_MAXMAJ 0xfffffffful /* max major value */
110 #define L_MAXMIN 0xfffffffful /* max minor value */
112 #define L_BITSMAJOR L_BITSMAJOR32
113 #define L_BITSMINOR L_BITSMINOR32
114 #define L_MAXMAJ L_MAXMAJ32
115 #define L_MAXMIN L_MAXMIN32
121 /* major part of a device internal to the kernel */
123 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
124 #define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
126 /* get internal major part of expanded device number */
128 #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
130 /* minor part of a device internal to the kernel */
132 #define minor(x) (minor_t)((x) & O_MAXMIN)
134 /* get internal minor part of expanded device number */
136 #define getminor(x) (minor_t)((x) & L_MAXMIN)
140 /* major part of a device external from the kernel (same as emajor below) */
142 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
144 /* minor part of a device external from the kernel (same as eminor below) */
146 #define minor(x) (minor_t)((x) & O_MAXMIN)
150 /* create old device number */
152 #define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
154 /* make an new device number */
156 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
160 * emajor() allows kernel/driver code to print external major numbers
161 * eminor() allows kernel/driver code to print external minor numbers
165 (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
166 NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
169 (minor_t)((x) & O_MAXMIN)
172 * get external major and minor device
173 * components from expanded device number
175 #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
176 NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
177 #define geteminor(x) (minor_t)((x) & L_MAXMIN)
181 * These are versions of the kernel routines for compressing and
182 * expanding long device numbers that don't return errors.
184 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
186 #define DEVCMPL(x) (x)
187 #define DEVEXPL(x) (x)
192 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
193 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
194 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
197 (((x) == NODEV32) ? NODEV : \
198 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
200 #endif /* L_BITSMAJOR32 ... */
202 /* convert to old (SVR3.2) dev format */
205 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
206 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
207 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
209 /* convert to new (SVR4) dev format */
212 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
216 * Macro for checking power of 2 address alignment.
218 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
221 * Macros for counting and rounding.
223 #define howmany(x, y) (((x)+((y)-1))/(y))
224 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
227 * Macro to determine if value is a power of 2
229 #define ISP2(x) (((x) & ((x) - 1)) == 0)
232 * Macros for various sorts of alignment and rounding. The "align" must
233 * be a power of 2. Often times it is a block, sector, or page.
237 * return x rounded down to an align boundary
238 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
239 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
240 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
241 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
243 #define P2ALIGN(x, align) ((x) & -(align))
246 * return x % (mod) align
247 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
248 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
250 #define P2PHASE(x, align) ((x) & ((align) - 1))
253 * return how much space is left in this block (but if it's perfectly
254 * aligned, return 0).
255 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
256 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
258 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
261 * return x rounded up to an align boundary
262 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
263 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
265 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
268 * return the ending address of the block that x is in
269 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
270 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
272 #define P2END(x, align) (-(~(x) & -(align)))
275 * return x rounded up to the next phase (offset) within align.
276 * phase should be < align.
277 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
278 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
280 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
283 * return TRUE if adding len to off would cause it to cross an align
285 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
286 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
288 #define P2BOUNDARY(off, len, align) \
289 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
292 * Return TRUE if they have the same highest bit set.
293 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
294 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
296 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
299 * Typed version of the P2* macros. These macros should be used to ensure
300 * that the result is correctly calculated based on the data type of (x),
301 * which is passed in as the last argument, regardless of the data
302 * type of the alignment. For example, if (x) is of type uint64_t,
303 * and we want to round it up to a page boundary using "PAGESIZE" as
304 * the alignment, we can do either
305 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
307 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
309 #define P2ALIGN_TYPED(x, align, type) \
310 ((type)(x) & -(type)(align))
311 #define P2PHASE_TYPED(x, align, type) \
312 ((type)(x) & ((type)(align) - 1))
313 #define P2NPHASE_TYPED(x, align, type) \
314 (-(type)(x) & ((type)(align) - 1))
315 #define P2ROUNDUP_TYPED(x, align, type) \
316 (-(-(type)(x) & -(type)(align)))
317 #define P2END_TYPED(x, align, type) \
318 (-(~(type)(x) & -(type)(align)))
319 #define P2PHASEUP_TYPED(x, align, phase, type) \
320 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
321 #define P2CROSS_TYPED(x, y, align, type) \
322 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
323 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
324 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
327 * Macros to atomically increment/decrement a variable. mutex and var
330 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
331 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
334 * Macros to declare bitfields - the order in the parameter list is
335 * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
336 * because if a field crosses a byte boundary it's not likely to be meaningful
337 * without reassembly in its nonnative endianness.
339 #if defined(_BIT_FIELDS_LTOH)
340 #define DECL_BITFIELD2(_a, _b) \
342 #define DECL_BITFIELD3(_a, _b, _c) \
344 #define DECL_BITFIELD4(_a, _b, _c, _d) \
345 uint8_t _a, _b, _c, _d
346 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
347 uint8_t _a, _b, _c, _d, _e
348 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
349 uint8_t _a, _b, _c, _d, _e, _f
350 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
351 uint8_t _a, _b, _c, _d, _e, _f, _g
352 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
353 uint8_t _a, _b, _c, _d, _e, _f, _g, _h
354 #elif defined(_BIT_FIELDS_HTOL)
355 #define DECL_BITFIELD2(_a, _b) \
357 #define DECL_BITFIELD3(_a, _b, _c) \
359 #define DECL_BITFIELD4(_a, _b, _c, _d) \
360 uint8_t _d, _c, _b, _a
361 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
362 uint8_t _e, _d, _c, _b, _a
363 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
364 uint8_t _f, _e, _d, _c, _b, _a
365 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
366 uint8_t _g, _f, _e, _d, _c, _b, _a
367 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
368 uint8_t _h, _g, _f, _e, _d, _c, _b, _a
370 #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
371 #endif /* _BIT_FIELDS_LTOH */
373 #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
375 /* avoid any possibility of clashing with <stddef.h> version */
377 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
381 * Find highest one bit set.
382 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
383 * High order bit is 31 (or 63 in _LP64 kernel).
388 #if defined(__FreeBSD__) && defined(_KERNEL) && defined(HAVE_INLINE_FLSL)
396 if (i & 0xffffffff00000000ul) {
400 if (i & 0xffff0000) {
420 * Find highest one bit set.
421 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
424 highbit64(uint64_t i)
426 #if defined(__FreeBSD__) && defined(_KERNEL) && defined(HAVE_INLINE_FLSLL)
433 if (i & 0xffffffff00000000ULL) {
436 if (i & 0xffff0000) {
459 #endif /* _SYS_SYSMACROS_H */