xref: /titanic_52/usr/src/uts/common/sys/sysmacros.h (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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 http://www.opensolaris.org/os/licensing.
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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <sys/param.h>
36 
37 #ifdef	__cplusplus
38 extern "C" {
39 #endif
40 
41 /*
42  * Some macros for units conversion
43  */
44 /*
45  * Disk blocks (sectors) and bytes.
46  */
47 #define	dtob(DD)	((DD) << DEV_BSHIFT)
48 #define	btod(BB)	(((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
49 #define	btodt(BB)	((BB) >> DEV_BSHIFT)
50 #define	lbtod(BB)	(((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
51 
52 /* common macros */
53 #ifndef MIN
54 #define	MIN(a, b)	((a) < (b) ? (a) : (b))
55 #endif
56 #ifndef MAX
57 #define	MAX(a, b)	((a) < (b) ? (b) : (a))
58 #endif
59 #ifndef ABS
60 #define	ABS(a)		((a) < 0 ? -(a) : (a))
61 #endif
62 
63 #ifdef _KERNEL
64 
65 /*
66  * Convert a single byte to/from binary-coded decimal (BCD).
67  */
68 extern unsigned char byte_to_bcd[256];
69 extern unsigned char bcd_to_byte[256];
70 
71 #define	BYTE_TO_BCD(x)	byte_to_bcd[(x) & 0xff]
72 #define	BCD_TO_BYTE(x)	bcd_to_byte[(x) & 0xff]
73 
74 #endif	/* _KERNEL */
75 
76 /*
77  * WARNING: The device number macros defined here should not be used by device
78  * drivers or user software. Device drivers should use the device functions
79  * defined in the DDI/DKI interface (see also ddi.h). Application software
80  * should make use of the library routines available in makedev(3). A set of
81  * new device macros are provided to operate on the expanded device number
82  * format supported in SVR4. Macro versions of the DDI device functions are
83  * provided for use by kernel proper routines only. Macro routines bmajor(),
84  * major(), minor(), emajor(), eminor(), and makedev() will be removed or
85  * their definitions changed at the next major release following SVR4.
86  */
87 
88 #define	O_BITSMAJOR	7	/* # of SVR3 major device bits */
89 #define	O_BITSMINOR	8	/* # of SVR3 minor device bits */
90 #define	O_MAXMAJ	0x7f	/* SVR3 max major value */
91 #define	O_MAXMIN	0xff	/* SVR3 max minor value */
92 
93 
94 #define	L_BITSMAJOR32	14	/* # of SVR4 major device bits */
95 #define	L_BITSMINOR32	18	/* # of SVR4 minor device bits */
96 #define	L_MAXMAJ32	0x3fff	/* SVR4 max major value */
97 #define	L_MAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
98 				/* For 3b2 hardware devices the minor is */
99 				/* restricted to 256 (0-255) */
100 
101 #ifdef _LP64
102 #define	L_BITSMAJOR	32	/* # of major device bits in 64-bit Solaris */
103 #define	L_BITSMINOR	32	/* # of minor device bits in 64-bit Solaris */
104 #define	L_MAXMAJ	0xfffffffful	/* max major value */
105 #define	L_MAXMIN	0xfffffffful	/* max minor value */
106 #else
107 #define	L_BITSMAJOR	L_BITSMAJOR32
108 #define	L_BITSMINOR	L_BITSMINOR32
109 #define	L_MAXMAJ	L_MAXMAJ32
110 #define	L_MAXMIN	L_MAXMIN32
111 #endif
112 
113 #ifdef _KERNEL
114 
115 /* major part of a device internal to the kernel */
116 
117 #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
118 #define	bmajor(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
119 
120 /* get internal major part of expanded device number */
121 
122 #define	getmajor(x)	(major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
123 
124 /* minor part of a device internal to the kernel */
125 
126 #define	minor(x)	(minor_t)((x) & O_MAXMIN)
127 
128 /* get internal minor part of expanded device number */
129 
130 #define	getminor(x)	(minor_t)((x) & L_MAXMIN)
131 
132 #else
133 
134 /* major part of a device external from the kernel (same as emajor below) */
135 
136 #define	major(x)	(major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
137 
138 /* minor part of a device external from the kernel  (same as eminor below) */
139 
140 #define	minor(x)	(minor_t)((x) & O_MAXMIN)
141 
142 #endif	/* _KERNEL */
143 
144 /* create old device number */
145 
146 #define	makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
147 
148 /* make an new device number */
149 
150 #define	makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
151 
152 
153 /*
154  * emajor() allows kernel/driver code to print external major numbers
155  * eminor() allows kernel/driver code to print external minor numbers
156  */
157 
158 #define	emajor(x) \
159 	(major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
160 	    NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
161 
162 #define	eminor(x) \
163 	(minor_t)((x) & O_MAXMIN)
164 
165 /*
166  * get external major and minor device
167  * components from expanded device number
168  */
169 #define	getemajor(x)	(major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
170 			    NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
171 #define	geteminor(x)	(minor_t)((x) & L_MAXMIN)
172 
173 /*
174  * These are versions of the kernel routines for compressing and
175  * expanding long device numbers that don't return errors.
176  */
177 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
178 
179 #define	DEVCMPL(x)	(x)
180 #define	DEVEXPL(x)	(x)
181 
182 #else
183 
184 #define	DEVCMPL(x)	\
185 	(dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
186 	    ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
187 	    ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
188 
189 #define	DEVEXPL(x)	\
190 	(((x) == NODEV32) ? NODEV : \
191 	makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
192 
193 #endif /* L_BITSMAJOR32 ... */
194 
195 /* convert to old (SVR3.2) dev format */
196 
197 #define	cmpdev(x) \
198 	(o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
199 	    ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
200 	    ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
201 
202 /* convert to new (SVR4) dev format */
203 
204 #define	expdev(x) \
205 	(dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
206 	    ((x) & O_MAXMIN))
207 
208 /*
209  * Macro for checking power of 2 address alignment.
210  */
211 #define	IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
212 
213 /*
214  * Macros for counting and rounding.
215  */
216 #define	howmany(x, y)	(((x)+((y)-1))/(y))
217 #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
218 
219 /*
220  * Macro to determine if value is a power of 2
221  */
222 #define	ISP2(x)		(((x) & ((x) - 1)) == 0)
223 
224 /*
225  * Macros for various sorts of alignment and rounding when the alignment
226  * is known to be a power of 2.
227  */
228 #define	P2ALIGN(x, align)		((x) & -(align))
229 #define	P2PHASE(x, align)		((x) & ((align) - 1))
230 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
231 #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
232 #define	P2END(x, align)			(-(~(x) & -(align)))
233 #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
234 #define	P2CROSS(x, y, align)		(((x) ^ (y)) > (align) - 1)
235 /*
236  * Determine whether two numbers have the same high-order bit.
237  */
238 #define	P2SAMEHIGHBIT(x, y)		(((x) ^ (y)) < ((x) & (y)))
239 
240 /*
241  * Typed version of the P2* macros.  These macros should be used to ensure
242  * that the result is correctly calculated based on the data type of (x),
243  * which is passed in as the last argument, regardless of the data
244  * type of the alignment.  For example, if (x) is of type uint64_t,
245  * and we want to round it up to a page boundary using "PAGESIZE" as
246  * the alignment, we can do either
247  *	P2ROUNDUP(x, (uint64_t)PAGESIZE)
248  * or
249  *	P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
250  */
251 #define	P2ALIGN_TYPED(x, align, type)	\
252 	((type)(x) & -(type)(align))
253 #define	P2PHASE_TYPED(x, align, type)	\
254 	((type)(x) & ((type)(align) - 1))
255 #define	P2NPHASE_TYPED(x, align, type)	\
256 	(-(type)(x) & ((type)(align) - 1))
257 #define	P2ROUNDUP_TYPED(x, align, type)	\
258 	(-(-(type)(x) & -(type)(align)))
259 #define	P2END_TYPED(x, align, type)	\
260 	(-(~(type)(x) & -(type)(align)))
261 #define	P2PHASEUP_TYPED(x, align, phase, type)	\
262 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
263 #define	P2CROSS_TYPED(x, y, align, type)	\
264 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
265 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
266 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
267 
268 /*
269  * Macros to atomically increment/decrement a variable.  mutex and var
270  * must be pointers.
271  */
272 #define	INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
273 #define	DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
274 
275 /*
276  * Macros to declare bitfields - the order in the parameter list is
277  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
278  * because if a field crosses a byte boundary it's not likely to be meaningful
279  * without reassembly in its nonnative endianness.
280  */
281 #if defined(_BIT_FIELDS_LTOH)
282 #define	DECL_BITFIELD2(_a, _b)				\
283 	uint8_t _a, _b
284 #define	DECL_BITFIELD3(_a, _b, _c)			\
285 	uint8_t _a, _b, _c
286 #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
287 	uint8_t _a, _b, _c, _d
288 #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
289 	uint8_t _a, _b, _c, _d, _e
290 #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
291 	uint8_t _a, _b, _c, _d, _e, _f
292 #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
293 	uint8_t _a, _b, _c, _d, _e, _f, _g
294 #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
295 	uint8_t _a, _b, _c, _d, _e, _f, _g, _h
296 #elif defined(_BIT_FIELDS_HTOL)
297 #define	DECL_BITFIELD2(_a, _b)				\
298 	uint8_t _b, _a
299 #define	DECL_BITFIELD3(_a, _b, _c)			\
300 	uint8_t _c, _b, _a
301 #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
302 	uint8_t _d, _c, _b, _a
303 #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
304 	uint8_t _e, _d, _c, _b, _a
305 #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
306 	uint8_t _f, _e, _d, _c, _b, _a
307 #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
308 	uint8_t _g, _f, _e, _d, _c, _b, _a
309 #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
310 	uint8_t _h, _g, _f, _e, _d, _c, _b, _a
311 #else
312 #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
313 #endif  /* _BIT_FIELDS_LTOH */
314 
315 #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
316 
317 /* avoid any possibility of clashing with <stddef.h> version */
318 
319 #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
320 #endif
321 
322 #ifdef	__cplusplus
323 }
324 #endif
325 
326 #endif	/* _SYS_SYSMACROS_H */
327