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