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