1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23*0Sstevel@tonic-gate /* All Rights Reserved */ 24*0Sstevel@tonic-gate 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate /* 27*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28*0Sstevel@tonic-gate * Use is subject to license terms. 29*0Sstevel@tonic-gate */ 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #ifndef _SYS_SYSMACROS_H 32*0Sstevel@tonic-gate #define _SYS_SYSMACROS_H 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #include <sys/param.h> 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate #ifdef __cplusplus 39*0Sstevel@tonic-gate extern "C" { 40*0Sstevel@tonic-gate #endif 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gate /* 43*0Sstevel@tonic-gate * Some macros for units conversion 44*0Sstevel@tonic-gate */ 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * Disk blocks (sectors) and bytes. 47*0Sstevel@tonic-gate */ 48*0Sstevel@tonic-gate #define dtob(DD) ((DD) << DEV_BSHIFT) 49*0Sstevel@tonic-gate #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 50*0Sstevel@tonic-gate #define btodt(BB) ((BB) >> DEV_BSHIFT) 51*0Sstevel@tonic-gate #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate /* common macros */ 54*0Sstevel@tonic-gate #ifndef MIN 55*0Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b)) 56*0Sstevel@tonic-gate #endif 57*0Sstevel@tonic-gate #ifndef MAX 58*0Sstevel@tonic-gate #define MAX(a, b) ((a) < (b) ? (b) : (a)) 59*0Sstevel@tonic-gate #endif 60*0Sstevel@tonic-gate #ifndef ABS 61*0Sstevel@tonic-gate #define ABS(a) ((a) < 0 ? -(a) : (a)) 62*0Sstevel@tonic-gate #endif 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate #ifdef _KERNEL 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate /* 67*0Sstevel@tonic-gate * Convert a single byte to/from binary-coded decimal (BCD). 68*0Sstevel@tonic-gate */ 69*0Sstevel@tonic-gate extern unsigned char byte_to_bcd[256]; 70*0Sstevel@tonic-gate extern unsigned char bcd_to_byte[256]; 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff] 73*0Sstevel@tonic-gate #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff] 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate #endif /* _KERNEL */ 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate /* 78*0Sstevel@tonic-gate * WARNING: The device number macros defined here should not be used by device 79*0Sstevel@tonic-gate * drivers or user software. Device drivers should use the device functions 80*0Sstevel@tonic-gate * defined in the DDI/DKI interface (see also ddi.h). Application software 81*0Sstevel@tonic-gate * should make use of the library routines available in makedev(3). A set of 82*0Sstevel@tonic-gate * new device macros are provided to operate on the expanded device number 83*0Sstevel@tonic-gate * format supported in SVR4. Macro versions of the DDI device functions are 84*0Sstevel@tonic-gate * provided for use by kernel proper routines only. Macro routines bmajor(), 85*0Sstevel@tonic-gate * major(), minor(), emajor(), eminor(), and makedev() will be removed or 86*0Sstevel@tonic-gate * their definitions changed at the next major release following SVR4. 87*0Sstevel@tonic-gate */ 88*0Sstevel@tonic-gate 89*0Sstevel@tonic-gate #define O_BITSMAJOR 7 /* # of SVR3 major device bits */ 90*0Sstevel@tonic-gate #define O_BITSMINOR 8 /* # of SVR3 minor device bits */ 91*0Sstevel@tonic-gate #define O_MAXMAJ 0x7f /* SVR3 max major value */ 92*0Sstevel@tonic-gate #define O_MAXMIN 0xff /* SVR3 max minor value */ 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate 95*0Sstevel@tonic-gate #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */ 96*0Sstevel@tonic-gate #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */ 97*0Sstevel@tonic-gate #define L_MAXMAJ32 0x3fff /* SVR4 max major value */ 98*0Sstevel@tonic-gate #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */ 99*0Sstevel@tonic-gate /* For 3b2 hardware devices the minor is */ 100*0Sstevel@tonic-gate /* restricted to 256 (0-255) */ 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate #ifdef _LP64 103*0Sstevel@tonic-gate #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */ 104*0Sstevel@tonic-gate #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */ 105*0Sstevel@tonic-gate #define L_MAXMAJ 0xfffffffful /* max major value */ 106*0Sstevel@tonic-gate #define L_MAXMIN 0xfffffffful /* max minor value */ 107*0Sstevel@tonic-gate #else 108*0Sstevel@tonic-gate #define L_BITSMAJOR L_BITSMAJOR32 109*0Sstevel@tonic-gate #define L_BITSMINOR L_BITSMINOR32 110*0Sstevel@tonic-gate #define L_MAXMAJ L_MAXMAJ32 111*0Sstevel@tonic-gate #define L_MAXMIN L_MAXMIN32 112*0Sstevel@tonic-gate #endif 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate #ifdef _KERNEL 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate /* major part of a device internal to the kernel */ 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 119*0Sstevel@tonic-gate #define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 120*0Sstevel@tonic-gate 121*0Sstevel@tonic-gate /* get internal major part of expanded device number */ 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ) 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate /* minor part of a device internal to the kernel */ 126*0Sstevel@tonic-gate 127*0Sstevel@tonic-gate #define minor(x) (minor_t)((x) & O_MAXMIN) 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* get internal minor part of expanded device number */ 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate #define getminor(x) (minor_t)((x) & L_MAXMIN) 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate #else 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate /* major part of a device external from the kernel (same as emajor below) */ 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gate #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 138*0Sstevel@tonic-gate 139*0Sstevel@tonic-gate /* minor part of a device external from the kernel (same as eminor below) */ 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate #define minor(x) (minor_t)((x) & O_MAXMIN) 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gate #endif /* _KERNEL */ 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate /* create old device number */ 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate #define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN)) 148*0Sstevel@tonic-gate 149*0Sstevel@tonic-gate /* make an new device number */ 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN)) 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate /* 155*0Sstevel@tonic-gate * emajor() allows kernel/driver code to print external major numbers 156*0Sstevel@tonic-gate * eminor() allows kernel/driver code to print external minor numbers 157*0Sstevel@tonic-gate */ 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate #define emajor(x) \ 160*0Sstevel@tonic-gate (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \ 161*0Sstevel@tonic-gate NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ) 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate #define eminor(x) \ 164*0Sstevel@tonic-gate (minor_t)((x) & O_MAXMIN) 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate /* 167*0Sstevel@tonic-gate * get external major and minor device 168*0Sstevel@tonic-gate * components from expanded device number 169*0Sstevel@tonic-gate */ 170*0Sstevel@tonic-gate #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \ 171*0Sstevel@tonic-gate NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ)) 172*0Sstevel@tonic-gate #define geteminor(x) (minor_t)((x) & L_MAXMIN) 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gate /* 175*0Sstevel@tonic-gate * These are versions of the kernel routines for compressing and 176*0Sstevel@tonic-gate * expanding long device numbers that don't return errors. 177*0Sstevel@tonic-gate */ 178*0Sstevel@tonic-gate #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR) 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate #define DEVCMPL(x) (x) 181*0Sstevel@tonic-gate #define DEVEXPL(x) (x) 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate #else 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate #define DEVCMPL(x) \ 186*0Sstevel@tonic-gate (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \ 187*0Sstevel@tonic-gate ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \ 188*0Sstevel@tonic-gate ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32))) 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate #define DEVEXPL(x) \ 191*0Sstevel@tonic-gate (((x) == NODEV32) ? NODEV : \ 192*0Sstevel@tonic-gate makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32)) 193*0Sstevel@tonic-gate 194*0Sstevel@tonic-gate #endif /* L_BITSMAJOR32 ... */ 195*0Sstevel@tonic-gate 196*0Sstevel@tonic-gate /* convert to old (SVR3.2) dev format */ 197*0Sstevel@tonic-gate 198*0Sstevel@tonic-gate #define cmpdev(x) \ 199*0Sstevel@tonic-gate (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \ 200*0Sstevel@tonic-gate ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \ 201*0Sstevel@tonic-gate ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN))) 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate /* convert to new (SVR4) dev format */ 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gate #define expdev(x) \ 206*0Sstevel@tonic-gate (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \ 207*0Sstevel@tonic-gate ((x) & O_MAXMIN)) 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate /* 210*0Sstevel@tonic-gate * Macro for checking power of 2 address alignment. 211*0Sstevel@tonic-gate */ 212*0Sstevel@tonic-gate #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate /* 215*0Sstevel@tonic-gate * Macros for counting and rounding. 216*0Sstevel@tonic-gate */ 217*0Sstevel@tonic-gate #define howmany(x, y) (((x)+((y)-1))/(y)) 218*0Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 219*0Sstevel@tonic-gate 220*0Sstevel@tonic-gate /* 221*0Sstevel@tonic-gate * Macro to determine if value is a power of 2 222*0Sstevel@tonic-gate */ 223*0Sstevel@tonic-gate #define ISP2(x) (((x) & ((x) - 1)) == 0) 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gate /* 226*0Sstevel@tonic-gate * Macros for various sorts of alignment and rounding when the alignment 227*0Sstevel@tonic-gate * is known to be a power of 2. 228*0Sstevel@tonic-gate */ 229*0Sstevel@tonic-gate #define P2ALIGN(x, align) ((x) & -(align)) 230*0Sstevel@tonic-gate #define P2PHASE(x, align) ((x) & ((align) - 1)) 231*0Sstevel@tonic-gate #define P2NPHASE(x, align) (-(x) & ((align) - 1)) 232*0Sstevel@tonic-gate #define P2ROUNDUP(x, align) (-(-(x) & -(align))) 233*0Sstevel@tonic-gate #define P2END(x, align) (-(~(x) & -(align))) 234*0Sstevel@tonic-gate #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align))) 235*0Sstevel@tonic-gate #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1) 236*0Sstevel@tonic-gate /* 237*0Sstevel@tonic-gate * Determine whether two numbers have the same high-order bit. 238*0Sstevel@tonic-gate */ 239*0Sstevel@tonic-gate #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y))) 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* 242*0Sstevel@tonic-gate * Typed version of the P2* macros. These macros should be used to ensure 243*0Sstevel@tonic-gate * that the result is correctly calculated based on the data type of (x), 244*0Sstevel@tonic-gate * which is passed in as the last argument, regardless of the data 245*0Sstevel@tonic-gate * type of the alignment. For example, if (x) is of type uint64_t, 246*0Sstevel@tonic-gate * and we want to round it up to a page boundary using "PAGESIZE" as 247*0Sstevel@tonic-gate * the alignment, we can do either 248*0Sstevel@tonic-gate * P2ROUNDUP(x, (uint64_t)PAGESIZE) 249*0Sstevel@tonic-gate * or 250*0Sstevel@tonic-gate * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t) 251*0Sstevel@tonic-gate */ 252*0Sstevel@tonic-gate #define P2ALIGN_TYPED(x, align, type) \ 253*0Sstevel@tonic-gate ((type)(x) & -(type)(align)) 254*0Sstevel@tonic-gate #define P2PHASE_TYPED(x, align, type) \ 255*0Sstevel@tonic-gate ((type)(x) & ((type)(align) - 1)) 256*0Sstevel@tonic-gate #define P2NPHASE_TYPED(x, align, type) \ 257*0Sstevel@tonic-gate (-(type)(x) & ((type)(align) - 1)) 258*0Sstevel@tonic-gate #define P2ROUNDUP_TYPED(x, align, type) \ 259*0Sstevel@tonic-gate (-(-(type)(x) & -(type)(align))) 260*0Sstevel@tonic-gate #define P2END_TYPED(x, align, type) \ 261*0Sstevel@tonic-gate (-(~(type)(x) & -(type)(align))) 262*0Sstevel@tonic-gate #define P2PHASEUP_TYPED(x, align, phase, type) \ 263*0Sstevel@tonic-gate ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align))) 264*0Sstevel@tonic-gate #define P2CROSS_TYPED(x, y, align, type) \ 265*0Sstevel@tonic-gate (((type)(x) ^ (type)(y)) > (type)(align) - 1) 266*0Sstevel@tonic-gate #define P2SAMEHIGHBIT_TYPED(x, y, type) \ 267*0Sstevel@tonic-gate (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y))) 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate /* 270*0Sstevel@tonic-gate * Macros to atomically increment/decrement a variable. mutex and var 271*0Sstevel@tonic-gate * must be pointers. 272*0Sstevel@tonic-gate */ 273*0Sstevel@tonic-gate #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex) 274*0Sstevel@tonic-gate #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex) 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof) 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate /* avoid any possibility of clashing with <stddef.h> version */ 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gate #define offsetof(s, m) ((size_t)(&(((s *)0)->m))) 281*0Sstevel@tonic-gate #endif 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gate #ifdef __cplusplus 284*0Sstevel@tonic-gate } 285*0Sstevel@tonic-gate #endif 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate #endif /* _SYS_SYSMACROS_H */ 288