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 
  32 #ifndef _SYS_SYSMACROS_H
  33 #define _SYS_SYSMACROS_H
  34 
  35 #include <sys/param.h>
  36 
  37 #ifdef  __cplusplus
  38 extern "C" {
  39 #endif
  40 
  41 /*
  42  * Some macros for units conversion
  43  */
  44 /*
  45  * Disk blocks (sectors) and bytes.
  46  */
  47 #define dtob(DD)        ((DD) << DEV_BSHIFT)
  48 #define btod(BB)        (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
  49 #define btodt(BB)       ((BB) >> DEV_BSHIFT)
  50 #define lbtod(BB)       (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
  51 
  52 /* common macros */
  53 #ifndef MIN
  54 #define MIN(a, b)       ((a) < (b) ? (a) : (b))
  55 #endif
  56 #ifndef MAX
  57 #define MAX(a, b)       ((a) < (b) ? (b) : (a))
  58 #endif
  59 #ifndef ABS
  60 #define ABS(a)          ((a) < 0 ? -(a) : (a))
  61 #endif
  62 #ifndef SIGNOF
  63 #define SIGNOF(a)       ((a) < 0 ? -1 : (a) > 0)
  64 #endif
  65 
  66 #ifdef _KERNEL
  67 
  68 /*
  69  * Convert a single byte to/from binary-coded decimal (BCD).
  70  */
  71 extern unsigned char byte_to_bcd[256];
  72 extern unsigned char bcd_to_byte[256];
  73 
  74 #define BYTE_TO_BCD(x)  byte_to_bcd[(x) & 0xff]
  75 #define BCD_TO_BYTE(x)  bcd_to_byte[(x) & 0xff]
  76 
  77 #endif  /* _KERNEL */
  78 
  79 /*
  80  * WARNING: The device number macros defined here should not be used by device
  81  * drivers or user software. Device drivers should use the device functions
  82  * defined in the DDI/DKI interface (see also ddi.h). Application software
  83  * should make use of the library routines available in makedev(3). A set of
  84  * new device macros are provided to operate on the expanded device number
  85  * format supported in SVR4. Macro versions of the DDI device functions are
  86  * provided for use by kernel proper routines only.
  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 /* get internal major part of expanded device number */
 117 
 118 #define getmajor(x)     (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
 119 
 120 /* get internal minor part of expanded device number */
 121 
 122 #define getminor(x)     (minor_t)((x) & L_MAXMIN)
 123 
 124 #endif  /* _KERNEL */
 125 
 126 /* make an new device number */
 127 
 128 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
 129 
 130 /*
 131  * get external major and minor device
 132  * components from expanded device number
 133  */
 134 #define getemajor(x)    (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
 135                             NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
 136 #define geteminor(x)    (minor_t)((x) & L_MAXMIN)
 137 
 138 /*
 139  * These are versions of the kernel routines for compressing and
 140  * expanding long device numbers that don't return errors.
 141  */
 142 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
 143 
 144 #define DEVCMPL(x)      (x)
 145 #define DEVEXPL(x)      (x)
 146 
 147 #else
 148 
 149 #define DEVCMPL(x)      \
 150         (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
 151             ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
 152             ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
 153 
 154 #define DEVEXPL(x)      \
 155         (((x) == NODEV32) ? NODEV : \
 156         makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
 157 
 158 #endif /* L_BITSMAJOR32 ... */
 159 
 160 /* convert to old (SVR3.2) dev format */
 161 
 162 #define cmpdev(x) \
 163         (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
 164             ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
 165             ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
 166 
 167 /* convert to new (SVR4) dev format */
 168 
 169 #define expdev(x) \
 170         (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
 171             ((x) & O_MAXMIN))
 172 
 173 /*
 174  * Macro for checking power of 2 address alignment.
 175  */
 176 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
 177 
 178 /*
 179  * Macros for counting and rounding.
 180  */
 181 #define howmany(x, y)   (((x)+((y)-1))/(y))
 182 #define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
 183 
 184 /*
 185  * Macro to determine if value is a power of 2
 186  */
 187 #define ISP2(x)         (((x) & ((x) - 1)) == 0)
 188 
 189 /*
 190  * Macros for various sorts of alignment and rounding.  The "align" must
 191  * be a power of 2.  Often times it is a block, sector, or page.
 192  */
 193 
 194 /*
 195  * return x rounded down to an align boundary
 196  * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
 197  * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
 198  * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
 199  * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
 200  */
 201 #define P2ALIGN(x, align)               ((x) & -(align))
 202 
 203 /*
 204  * return x % (mod) align
 205  * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
 206  * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
 207  */
 208 #define P2PHASE(x, align)               ((x) & ((align) - 1))
 209 
 210 /*
 211  * return how much space is left in this block (but if it's perfectly
 212  * aligned, return 0).
 213  * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
 214  * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
 215  */
 216 #define P2NPHASE(x, align)              (-(x) & ((align) - 1))
 217 
 218 /*
 219  * return x rounded up to an align boundary
 220  * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
 221  * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
 222  */
 223 #define P2ROUNDUP(x, align)             (-(-(x) & -(align)))
 224 
 225 /*
 226  * return the ending address of the block that x is in
 227  * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
 228  * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
 229  */
 230 #define P2END(x, align)                 (-(~(x) & -(align)))
 231 
 232 /*
 233  * return x rounded up to the next phase (offset) within align.
 234  * phase should be < align.
 235  * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
 236  * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
 237  */
 238 #define P2PHASEUP(x, align, phase)      ((phase) - (((phase) - (x)) & -(align)))
 239 
 240 /*
 241  * return TRUE if adding len to off would cause it to cross an align
 242  * boundary.
 243  * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
 244  * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
 245  */
 246 #define P2BOUNDARY(off, len, align) \
 247         (((off) ^ ((off) + (len) - 1)) > (align) - 1)
 248 
 249 /*
 250  * Return TRUE if they have the same highest bit set.
 251  * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
 252  * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
 253  */
 254 #define P2SAMEHIGHBIT(x, y)             (((x) ^ (y)) < ((x) & (y)))
 255 
 256 /*
 257  * Typed version of the P2* macros.  These macros should be used to ensure
 258  * that the result is correctly calculated based on the data type of (x),
 259  * which is passed in as the last argument, regardless of the data
 260  * type of the alignment.  For example, if (x) is of type uint64_t,
 261  * and we want to round it up to a page boundary using "PAGESIZE" as
 262  * the alignment, we can do either
 263  *      P2ROUNDUP(x, (uint64_t)PAGESIZE)
 264  * or
 265  *      P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
 266  */
 267 #define P2ALIGN_TYPED(x, align, type)   \
 268         ((type)(x) & -(type)(align))
 269 #define P2PHASE_TYPED(x, align, type)   \
 270         ((type)(x) & ((type)(align) - 1))
 271 #define P2NPHASE_TYPED(x, align, type)  \
 272         (-(type)(x) & ((type)(align) - 1))
 273 #define P2ROUNDUP_TYPED(x, align, type) \
 274         (-(-(type)(x) & -(type)(align)))
 275 #define P2END_TYPED(x, align, type)     \
 276         (-(~(type)(x) & -(type)(align)))
 277 #define P2PHASEUP_TYPED(x, align, phase, type)  \
 278         ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
 279 #define P2CROSS_TYPED(x, y, align, type)        \
 280         (((type)(x) ^ (type)(y)) > (type)(align) - 1)
 281 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
 282         (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
 283 
 284 /*
 285  * Macros to atomically increment/decrement a variable.  mutex and var
 286  * must be pointers.
 287  */
 288 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
 289 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
 290 
 291 /*
 292  * Macros to declare bitfields - the order in the parameter list is
 293  * Low to High - that is, declare bit 0 first.  We only support 8-bit bitfields
 294  * because if a field crosses a byte boundary it's not likely to be meaningful
 295  * without reassembly in its nonnative endianness.
 296  */
 297 #if defined(_BIT_FIELDS_LTOH)
 298 #define DECL_BITFIELD2(_a, _b)                          \
 299         uint8_t _a, _b
 300 #define DECL_BITFIELD3(_a, _b, _c)                      \
 301         uint8_t _a, _b, _c
 302 #define DECL_BITFIELD4(_a, _b, _c, _d)                  \
 303         uint8_t _a, _b, _c, _d
 304 #define DECL_BITFIELD5(_a, _b, _c, _d, _e)              \
 305         uint8_t _a, _b, _c, _d, _e
 306 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)          \
 307         uint8_t _a, _b, _c, _d, _e, _f
 308 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)      \
 309         uint8_t _a, _b, _c, _d, _e, _f, _g
 310 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)  \
 311         uint8_t _a, _b, _c, _d, _e, _f, _g, _h
 312 #elif defined(_BIT_FIELDS_HTOL)
 313 #define DECL_BITFIELD2(_a, _b)                          \
 314         uint8_t _b, _a
 315 #define DECL_BITFIELD3(_a, _b, _c)                      \
 316         uint8_t _c, _b, _a
 317 #define DECL_BITFIELD4(_a, _b, _c, _d)                  \
 318         uint8_t _d, _c, _b, _a
 319 #define DECL_BITFIELD5(_a, _b, _c, _d, _e)              \
 320         uint8_t _e, _d, _c, _b, _a
 321 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f)          \
 322         uint8_t _f, _e, _d, _c, _b, _a
 323 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g)      \
 324         uint8_t _g, _f, _e, _d, _c, _b, _a
 325 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h)  \
 326         uint8_t _h, _g, _f, _e, _d, _c, _b, _a
 327 #else
 328 #error  One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
 329 #endif  /* _BIT_FIELDS_LTOH */
 330 
 331 /* avoid any possibility of clashing with <stddef.h> version */
 332 #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_KMEMUSER)
 333 
 334 #if !defined(offsetof)
 335 #define offsetof(s, m)  ((size_t)(&(((s *)0)->m)))
 336 #endif /* !offsetof */
 337 
 338 #define container_of(m, s, name)                        \
 339         (void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name))
 340 
 341 #define ARRAY_SIZE(x)   (sizeof (x) / sizeof (x[0]))
 342 #endif /* _KERNEL, !_KMEMUSER */
 343 
 344 #ifdef  __cplusplus
 345 }
 346 #endif
 347 
 348 #endif  /* _SYS_SYSMACROS_H */