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