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