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