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 #include <sys/isa_defs.h>
35 #if defined(__FreeBSD__) && defined(_KERNEL)
36 #include <sys/libkern.h>
37 #endif
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 /*
44  * Some macros for units conversion
45  */
46 /*
47  * Disk blocks (sectors) and bytes.
48  */
49 #ifndef dtob
50 #define	dtob(DD)	((DD) << DEV_BSHIFT)
51 #endif
52 #ifndef btod
53 #define	btod(BB)	(((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
54 #endif
55 #define	btodt(BB)	((BB) >> DEV_BSHIFT)
56 #define	lbtod(BB)	(((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
57 
58 /* common macros */
59 #ifndef MIN
60 #define	MIN(a, b)	((a) < (b) ? (a) : (b))
61 #endif
62 #ifndef MAX
63 #define	MAX(a, b)	((a) < (b) ? (b) : (a))
64 #endif
65 #ifndef ABS
66 #define	ABS(a)		((a) < 0 ? -(a) : (a))
67 #endif
68 #ifndef	SIGNOF
69 #define	SIGNOF(a)	((a) < 0 ? -1 : (a) > 0)
70 #endif
71 
72 #ifdef _KERNEL
73 
74 /*
75  * Convert a single byte to/from binary-coded decimal (BCD).
76  */
77 extern unsigned char byte_to_bcd[256];
78 extern unsigned char bcd_to_byte[256];
79 
80 #define	BYTE_TO_BCD(x)	byte_to_bcd[(x) & 0xff]
81 #define	BCD_TO_BYTE(x)	bcd_to_byte[(x) & 0xff]
82 
83 #endif	/* _KERNEL */
84 
85 /*
86  * WARNING: The device number macros defined here should not be used by device
87  * drivers or user software. Device drivers should use the device functions
88  * defined in the DDI/DKI interface (see also ddi.h). Application software
89  * should make use of the library routines available in makedev(3). A set of
90  * new device macros are provided to operate on the expanded device number
91  * format supported in SVR4. Macro versions of the DDI device functions are
92  * provided for use by kernel proper routines only. Macro routines bmajor(),
93  * major(), minor(), emajor(), eminor(), and makedev() will be removed or
94  * their definitions changed at the next major release following SVR4.
95  */
96 
97 #define	O_BITSMAJOR	7	/* # of SVR3 major device bits */
98 #define	O_BITSMINOR	8	/* # of SVR3 minor device bits */
99 #define	O_MAXMAJ	0x7f	/* SVR3 max major value */
100 #define	O_MAXMIN	0xff	/* SVR3 max minor value */
101 
102 
103 #define	L_BITSMAJOR32	14	/* # of SVR4 major device bits */
104 #define	L_BITSMINOR32	18	/* # of SVR4 minor device bits */
105 #define	L_MAXMAJ32	0x3fff	/* SVR4 max major value */
106 #define	L_MAXMIN32	0x3ffff	/* MAX minor for 3b2 software drivers. */
107 				/* For 3b2 hardware devices the minor is */
108 				/* restricted to 256 (0-255) */
109 
110 #ifdef _LP64
111 #define	L_BITSMAJOR	32	/* # of major device bits in 64-bit Solaris */
112 #define	L_BITSMINOR	32	/* # of minor device bits in 64-bit Solaris */
113 #define	L_MAXMAJ	0xfffffffful	/* max major value */
114 #define	L_MAXMIN	0xfffffffful	/* max minor value */
115 #else
116 #define	L_BITSMAJOR	L_BITSMAJOR32
117 #define	L_BITSMINOR	L_BITSMINOR32
118 #define	L_MAXMAJ	L_MAXMAJ32
119 #define	L_MAXMIN	L_MAXMIN32
120 #endif
121 
122 #ifdef illumos
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
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 #endif /* illumos */
183 
184 /*
185  * These are versions of the kernel routines for compressing and
186  * expanding long device numbers that don't return errors.
187  */
188 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
189 
190 #define	DEVCMPL(x)	(x)
191 #define	DEVEXPL(x)	(x)
192 
193 #else
194 
195 #define	DEVCMPL(x)	\
196 	(dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
197 	    ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
198 	    ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
199 
200 #define	DEVEXPL(x)	\
201 	(((x) == NODEV32) ? NODEV : \
202 	makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
203 
204 #endif /* L_BITSMAJOR32 ... */
205 
206 /* convert to old (SVR3.2) dev format */
207 
208 #define	cmpdev(x) \
209 	(o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
210 	    ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
211 	    ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
212 
213 /* convert to new (SVR4) dev format */
214 
215 #define	expdev(x) \
216 	(dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
217 	    ((x) & O_MAXMIN))
218 
219 /*
220  * Macro for checking power of 2 address alignment.
221  */
222 #define	IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
223 
224 /*
225  * Macros for counting and rounding.
226  */
227 #ifndef howmany
228 #define	howmany(x, y)	(((x)+((y)-1))/(y))
229 #endif
230 #ifndef roundup
231 #define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))
232 #endif
233 /*
234  * Macro to determine if value is a power of 2
235  */
236 #define	ISP2(x)		(((x) & ((x) - 1)) == 0)
237 
238 /*
239  * Macros for various sorts of alignment and rounding.  The "align" must
240  * be a power of 2.  Often times it is a block, sector, or page.
241  */
242 
243 /*
244  * return x rounded down to an align boundary
245  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
246  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
247  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
248  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
249  */
250 #define	P2ALIGN(x, align)		((x) & -(align))
251 
252 /*
253  * return x % (mod) align
254  * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
255  * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
256  */
257 #define	P2PHASE(x, align)		((x) & ((align) - 1))
258 
259 /*
260  * return how much space is left in this block (but if it's perfectly
261  * aligned, return 0).
262  * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
263  * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
264  */
265 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
266 
267 /*
268  * return x rounded up to an align boundary
269  * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
270  * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
271  */
272 #define	P2ROUNDUP(x, align)		(-(-(x) & -(align)))
273 
274 /*
275  * return the ending address of the block that x is in
276  * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
277  * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
278  */
279 #define	P2END(x, align)			(-(~(x) & -(align)))
280 
281 /*
282  * return x rounded up to the next phase (offset) within align.
283  * phase should be < align.
284  * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
285  * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
286  */
287 #define	P2PHASEUP(x, align, phase)	((phase) - (((phase) - (x)) & -(align)))
288 
289 /*
290  * return TRUE if adding len to off would cause it to cross an align
291  * boundary.
292  * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
293  * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
294  */
295 #define	P2BOUNDARY(off, len, align) \
296 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
297 
298 /*
299  * Return TRUE if they have the same highest bit set.
300  * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
301  * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
302  */
303 #define	P2SAMEHIGHBIT(x, y)		(((x) ^ (y)) < ((x) & (y)))
304 
305 /*
306  * Typed version of the P2* macros.  These macros should be used to ensure
307  * that the result is correctly calculated based on the data type of (x),
308  * which is passed in as the last argument, regardless of the data
309  * type of the alignment.  For example, if (x) is of type uint64_t,
310  * and we want to round it up to a page boundary using "PAGESIZE" as
311  * the alignment, we can do either
312  *	P2ROUNDUP(x, (uint64_t)PAGESIZE)
313  * or
314  *	P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
315  */
316 #define	P2ALIGN_TYPED(x, align, type)	\
317 	((type)(x) & -(type)(align))
318 #define	P2PHASE_TYPED(x, align, type)	\
319 	((type)(x) & ((type)(align) - 1))
320 #define	P2NPHASE_TYPED(x, align, type)	\
321 	(-(type)(x) & ((type)(align) - 1))
322 #define	P2ROUNDUP_TYPED(x, align, type)	\
323 	(-(-(type)(x) & -(type)(align)))
324 #define	P2END_TYPED(x, align, type)	\
325 	(-(~(type)(x) & -(type)(align)))
326 #define	P2PHASEUP_TYPED(x, align, phase, type)	\
327 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
328 #define	P2CROSS_TYPED(x, y, align, type)	\
329 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
330 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
331 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
332 
333 /*
334  * Macros to atomically increment/decrement a variable.  mutex and var
335  * must be pointers.
336  */
337 #define	INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
338 #define	DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
339 
340 /*
341  * Macros to declare bitfields - the order in the parameter list is
342  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
343  * because if a field crosses a byte boundary it's not likely to be meaningful
344  * without reassembly in its nonnative endianness.
345  */
346 #if defined(_BIT_FIELDS_LTOH)
347 #define	DECL_BITFIELD2(_a, _b)				\
348 	uint8_t _a, _b
349 #define	DECL_BITFIELD3(_a, _b, _c)			\
350 	uint8_t _a, _b, _c
351 #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
352 	uint8_t _a, _b, _c, _d
353 #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
354 	uint8_t _a, _b, _c, _d, _e
355 #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
356 	uint8_t _a, _b, _c, _d, _e, _f
357 #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
358 	uint8_t _a, _b, _c, _d, _e, _f, _g
359 #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
360 	uint8_t _a, _b, _c, _d, _e, _f, _g, _h
361 #elif defined(_BIT_FIELDS_HTOL)
362 #define	DECL_BITFIELD2(_a, _b)				\
363 	uint8_t _b, _a
364 #define	DECL_BITFIELD3(_a, _b, _c)			\
365 	uint8_t _c, _b, _a
366 #define	DECL_BITFIELD4(_a, _b, _c, _d)			\
367 	uint8_t _d, _c, _b, _a
368 #define	DECL_BITFIELD5(_a, _b, _c, _d, _e)		\
369 	uint8_t _e, _d, _c, _b, _a
370 #define	DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)		\
371 	uint8_t _f, _e, _d, _c, _b, _a
372 #define	DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)	\
373 	uint8_t _g, _f, _e, _d, _c, _b, _a
374 #define	DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)	\
375 	uint8_t _h, _g, _f, _e, _d, _c, _b, _a
376 #else
377 #error	One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
378 #endif  /* _BIT_FIELDS_LTOH */
379 
380 #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
381 
382 /* avoid any possibility of clashing with <stddef.h> version */
383 
384 #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
385 #endif
386 
387 /*
388  * Find highest one bit set.
389  *      Returns bit number + 1 of highest bit that is set, otherwise returns 0.
390  * High order bit is 31 (or 63 in _LP64 kernel).
391  */
392 static __inline int
393 highbit(unsigned long i)
394 {
395 #if defined(__FreeBSD__) && defined(_KERNEL) && defined(HAVE_INLINE_FLSL)
396 	return (flsl(i));
397 #else
398 	int h = 1;
399 
400 	if (i == 0)
401 		return (0);
402 #ifdef _LP64
403 	if (i & 0xffffffff00000000ul) {
404 		h += 32; i >>= 32;
405 	}
406 #endif
407 	if (i & 0xffff0000) {
408 		h += 16; i >>= 16;
409 	}
410 	if (i & 0xff00) {
411 		h += 8; i >>= 8;
412 	}
413 	if (i & 0xf0) {
414 		h += 4; i >>= 4;
415 	}
416 	if (i & 0xc) {
417 		h += 2; i >>= 2;
418 	}
419 	if (i & 0x2) {
420 		h += 1;
421 	}
422 	return (h);
423 #endif
424 }
425 
426 /*
427  * Find highest one bit set.
428  *	Returns bit number + 1 of highest bit that is set, otherwise returns 0.
429  */
430 static __inline int
431 highbit64(uint64_t i)
432 {
433 #if defined(__FreeBSD__) && defined(_KERNEL) && defined(HAVE_INLINE_FLSLL)
434 	return (flsll(i));
435 #else
436 	int h = 1;
437 
438 	if (i == 0)
439 		return (0);
440 	if (i & 0xffffffff00000000ULL) {
441 		h += 32; i >>= 32;
442 	}
443 	if (i & 0xffff0000) {
444 		h += 16; i >>= 16;
445 	}
446 	if (i & 0xff00) {
447 		h += 8; i >>= 8;
448 	}
449 	if (i & 0xf0) {
450 		h += 4; i >>= 4;
451 	}
452 	if (i & 0xc) {
453 		h += 2; i >>= 2;
454 	}
455 	if (i & 0x2) {
456 		h += 1;
457 	}
458 	return (h);
459 #endif
460 }
461 
462 #ifdef	__cplusplus
463 }
464 #endif
465 
466 #endif	/* _SYS_SYSMACROS_H */
467