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