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