sysmacros.h revision 168404
1168404Spjd/* 2168404Spjd * CDDL HEADER START 3168404Spjd * 4168404Spjd * The contents of this file are subject to the terms of the 5168404Spjd * Common Development and Distribution License, Version 1.0 only 6168404Spjd * (the "License"). You may not use this file except in compliance 7168404Spjd * with the License. 8168404Spjd * 9168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10168404Spjd * or http://www.opensolaris.org/os/licensing. 11168404Spjd * See the License for the specific language governing permissions 12168404Spjd * and limitations under the License. 13168404Spjd * 14168404Spjd * When distributing Covered Code, include this CDDL HEADER in each 15168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16168404Spjd * If applicable, add the following below this CDDL HEADER, with the 17168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying 18168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner] 19168404Spjd * 20168404Spjd * CDDL HEADER END 21168404Spjd */ 22168404Spjd/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 23168404Spjd/* All Rights Reserved */ 24168404Spjd 25168404Spjd 26168404Spjd/* 27168404Spjd * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 28168404Spjd * Use is subject to license terms. 29168404Spjd */ 30168404Spjd 31168404Spjd#ifndef _SYS_SYSMACROS_H 32168404Spjd#define _SYS_SYSMACROS_H 33168404Spjd 34168404Spjd#pragma ident "%Z%%M% %I% %E% SMI" 35168404Spjd 36168404Spjd#include <sys/param.h> 37168404Spjd 38168404Spjd#ifdef __cplusplus 39168404Spjdextern "C" { 40168404Spjd#endif 41168404Spjd 42168404Spjd/* 43168404Spjd * Some macros for units conversion 44168404Spjd */ 45168404Spjd/* 46168404Spjd * Disk blocks (sectors) and bytes. 47168404Spjd */ 48168404Spjd#define dtob(DD) ((DD) << DEV_BSHIFT) 49168404Spjd#define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 50168404Spjd#define btodt(BB) ((BB) >> DEV_BSHIFT) 51168404Spjd#define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 52168404Spjd 53168404Spjd/* common macros */ 54168404Spjd#ifndef MIN 55168404Spjd#define MIN(a, b) ((a) < (b) ? (a) : (b)) 56168404Spjd#endif 57168404Spjd#ifndef MAX 58168404Spjd#define MAX(a, b) ((a) < (b) ? (b) : (a)) 59168404Spjd#endif 60168404Spjd#ifndef ABS 61168404Spjd#define ABS(a) ((a) < 0 ? -(a) : (a)) 62168404Spjd#endif 63168404Spjd 64168404Spjd#ifdef _KERNEL 65168404Spjd 66168404Spjd/* 67168404Spjd * Convert a single byte to/from binary-coded decimal (BCD). 68168404Spjd */ 69168404Spjdextern unsigned char byte_to_bcd[256]; 70168404Spjdextern unsigned char bcd_to_byte[256]; 71168404Spjd 72168404Spjd#define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff] 73168404Spjd#define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff] 74168404Spjd 75168404Spjd#endif /* _KERNEL */ 76168404Spjd 77168404Spjd/* 78168404Spjd * WARNING: The device number macros defined here should not be used by device 79168404Spjd * drivers or user software. Device drivers should use the device functions 80168404Spjd * defined in the DDI/DKI interface (see also ddi.h). Application software 81168404Spjd * should make use of the library routines available in makedev(3). A set of 82168404Spjd * new device macros are provided to operate on the expanded device number 83168404Spjd * format supported in SVR4. Macro versions of the DDI device functions are 84168404Spjd * provided for use by kernel proper routines only. Macro routines bmajor(), 85168404Spjd * major(), minor(), emajor(), eminor(), and makedev() will be removed or 86168404Spjd * their definitions changed at the next major release following SVR4. 87168404Spjd */ 88168404Spjd 89168404Spjd#define O_BITSMAJOR 7 /* # of SVR3 major device bits */ 90168404Spjd#define O_BITSMINOR 8 /* # of SVR3 minor device bits */ 91168404Spjd#define O_MAXMAJ 0x7f /* SVR3 max major value */ 92168404Spjd#define O_MAXMIN 0xff /* SVR3 max minor value */ 93168404Spjd 94168404Spjd 95168404Spjd#define L_BITSMAJOR32 14 /* # of SVR4 major device bits */ 96168404Spjd#define L_BITSMINOR32 18 /* # of SVR4 minor device bits */ 97168404Spjd#define L_MAXMAJ32 0x3fff /* SVR4 max major value */ 98168404Spjd#define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */ 99168404Spjd /* For 3b2 hardware devices the minor is */ 100168404Spjd /* restricted to 256 (0-255) */ 101168404Spjd 102168404Spjd#ifdef _LP64 103168404Spjd#define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */ 104168404Spjd#define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */ 105168404Spjd#define L_MAXMAJ 0xfffffffful /* max major value */ 106168404Spjd#define L_MAXMIN 0xfffffffful /* max minor value */ 107168404Spjd#else 108168404Spjd#define L_BITSMAJOR L_BITSMAJOR32 109168404Spjd#define L_BITSMINOR L_BITSMINOR32 110168404Spjd#define L_MAXMAJ L_MAXMAJ32 111168404Spjd#define L_MAXMIN L_MAXMIN32 112168404Spjd#endif 113168404Spjd 114168404Spjd#ifdef _KERNEL 115168404Spjd 116168404Spjd/* major part of a device internal to the kernel */ 117168404Spjd 118168404Spjd#define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 119168404Spjd#define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 120168404Spjd 121168404Spjd/* get internal major part of expanded device number */ 122168404Spjd 123168404Spjd#define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ) 124168404Spjd 125168404Spjd/* minor part of a device internal to the kernel */ 126168404Spjd 127168404Spjd#define minor(x) (minor_t)((x) & O_MAXMIN) 128168404Spjd 129168404Spjd/* get internal minor part of expanded device number */ 130168404Spjd 131168404Spjd#define getminor(x) (minor_t)((x) & L_MAXMIN) 132168404Spjd 133168404Spjd#else 134168404Spjd 135168404Spjd/* major part of a device external from the kernel (same as emajor below) */ 136168404Spjd 137168404Spjd#define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 138168404Spjd 139168404Spjd/* minor part of a device external from the kernel (same as eminor below) */ 140168404Spjd 141168404Spjd#define minor(x) (minor_t)((x) & O_MAXMIN) 142168404Spjd 143168404Spjd#endif /* _KERNEL */ 144168404Spjd 145168404Spjd/* create old device number */ 146168404Spjd 147168404Spjd#define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN)) 148168404Spjd 149168404Spjd/* make an new device number */ 150168404Spjd 151168404Spjd#define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN)) 152168404Spjd 153168404Spjd 154168404Spjd/* 155168404Spjd * emajor() allows kernel/driver code to print external major numbers 156168404Spjd * eminor() allows kernel/driver code to print external minor numbers 157168404Spjd */ 158168404Spjd 159168404Spjd#define emajor(x) \ 160168404Spjd (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \ 161168404Spjd NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ) 162168404Spjd 163168404Spjd#define eminor(x) \ 164168404Spjd (minor_t)((x) & O_MAXMIN) 165168404Spjd 166168404Spjd/* 167168404Spjd * get external major and minor device 168168404Spjd * components from expanded device number 169168404Spjd */ 170168404Spjd#define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \ 171168404Spjd NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ)) 172168404Spjd#define geteminor(x) (minor_t)((x) & L_MAXMIN) 173168404Spjd 174168404Spjd/* 175168404Spjd * These are versions of the kernel routines for compressing and 176168404Spjd * expanding long device numbers that don't return errors. 177168404Spjd */ 178168404Spjd#if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR) 179168404Spjd 180168404Spjd#define DEVCMPL(x) (x) 181168404Spjd#define DEVEXPL(x) (x) 182168404Spjd 183168404Spjd#else 184168404Spjd 185168404Spjd#define DEVCMPL(x) \ 186168404Spjd (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \ 187168404Spjd ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \ 188168404Spjd ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32))) 189168404Spjd 190168404Spjd#define DEVEXPL(x) \ 191168404Spjd (((x) == NODEV32) ? NODEV : \ 192168404Spjd makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32)) 193168404Spjd 194168404Spjd#endif /* L_BITSMAJOR32 ... */ 195168404Spjd 196168404Spjd/* convert to old (SVR3.2) dev format */ 197168404Spjd 198168404Spjd#define cmpdev(x) \ 199168404Spjd (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \ 200168404Spjd ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \ 201168404Spjd ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN))) 202168404Spjd 203168404Spjd/* convert to new (SVR4) dev format */ 204168404Spjd 205168404Spjd#define expdev(x) \ 206168404Spjd (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \ 207168404Spjd ((x) & O_MAXMIN)) 208168404Spjd 209168404Spjd/* 210168404Spjd * Macro for checking power of 2 address alignment. 211168404Spjd */ 212168404Spjd#define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) 213168404Spjd 214168404Spjd/* 215168404Spjd * Macros for counting and rounding. 216168404Spjd */ 217168404Spjd#define howmany(x, y) (((x)+((y)-1))/(y)) 218168404Spjd#define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 219168404Spjd 220168404Spjd/* 221168404Spjd * Macro to determine if value is a power of 2 222168404Spjd */ 223168404Spjd#define ISP2(x) (((x) & ((x) - 1)) == 0) 224168404Spjd 225168404Spjd/* 226168404Spjd * Macros for various sorts of alignment and rounding when the alignment 227168404Spjd * is known to be a power of 2. 228168404Spjd */ 229168404Spjd#define P2ALIGN(x, align) ((x) & -(align)) 230168404Spjd#define P2PHASE(x, align) ((x) & ((align) - 1)) 231168404Spjd#define P2NPHASE(x, align) (-(x) & ((align) - 1)) 232168404Spjd#define P2ROUNDUP(x, align) (-(-(x) & -(align))) 233168404Spjd#define P2END(x, align) (-(~(x) & -(align))) 234168404Spjd#define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align))) 235168404Spjd#define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1) 236168404Spjd/* 237168404Spjd * Determine whether two numbers have the same high-order bit. 238168404Spjd */ 239168404Spjd#define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y))) 240168404Spjd 241168404Spjd/* 242168404Spjd * Typed version of the P2* macros. These macros should be used to ensure 243168404Spjd * that the result is correctly calculated based on the data type of (x), 244168404Spjd * which is passed in as the last argument, regardless of the data 245168404Spjd * type of the alignment. For example, if (x) is of type uint64_t, 246168404Spjd * and we want to round it up to a page boundary using "PAGESIZE" as 247168404Spjd * the alignment, we can do either 248168404Spjd * P2ROUNDUP(x, (uint64_t)PAGESIZE) 249168404Spjd * or 250168404Spjd * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t) 251168404Spjd */ 252168404Spjd#define P2ALIGN_TYPED(x, align, type) \ 253168404Spjd ((type)(x) & -(type)(align)) 254168404Spjd#define P2PHASE_TYPED(x, align, type) \ 255168404Spjd ((type)(x) & ((type)(align) - 1)) 256168404Spjd#define P2NPHASE_TYPED(x, align, type) \ 257168404Spjd (-(type)(x) & ((type)(align) - 1)) 258168404Spjd#define P2ROUNDUP_TYPED(x, align, type) \ 259168404Spjd (-(-(type)(x) & -(type)(align))) 260168404Spjd#define P2END_TYPED(x, align, type) \ 261168404Spjd (-(~(type)(x) & -(type)(align))) 262168404Spjd#define P2PHASEUP_TYPED(x, align, phase, type) \ 263168404Spjd ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align))) 264168404Spjd#define P2CROSS_TYPED(x, y, align, type) \ 265168404Spjd (((type)(x) ^ (type)(y)) > (type)(align) - 1) 266168404Spjd#define P2SAMEHIGHBIT_TYPED(x, y, type) \ 267168404Spjd (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y))) 268168404Spjd 269168404Spjd/* 270168404Spjd * Macros to atomically increment/decrement a variable. mutex and var 271168404Spjd * must be pointers. 272168404Spjd */ 273168404Spjd#define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex) 274168404Spjd#define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex) 275168404Spjd 276168404Spjd#if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof) 277168404Spjd 278168404Spjd/* avoid any possibility of clashing with <stddef.h> version */ 279168404Spjd 280168404Spjd#define offsetof(s, m) ((size_t)(&(((s *)0)->m))) 281168404Spjd#endif 282168404Spjd 283168404Spjd#ifdef __cplusplus 284168404Spjd} 285168404Spjd#endif 286168404Spjd 287168404Spjd#endif /* _SYS_SYSMACROS_H */ 288