Print this page
9936 atomic ops in syscall_mstate() induce significant overhead
9942 zone secflags are not initialized correctly
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/sys/sysmacros.h
+++ new/usr/src/uts/common/sys/sysmacros.h
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All Rights Reserved */
23 23
24 24
25 25 /*
26 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 *
29 29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
30 30 *
31 31 * Copyright 2018 Joyent Inc.
32 32 */
33 33
34 34 #ifndef _SYS_SYSMACROS_H
35 35 #define _SYS_SYSMACROS_H
36 36
37 37 #include <sys/param.h>
38 38 #include <sys/stddef.h>
39 39
40 40 #ifdef __cplusplus
41 41 extern "C" {
42 42 #endif
43 43
44 44 /*
45 45 * Some macros for units conversion
46 46 */
47 47 /*
48 48 * Disk blocks (sectors) and bytes.
49 49 */
50 50 #define dtob(DD) ((DD) << DEV_BSHIFT)
51 51 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
52 52 #define btodt(BB) ((BB) >> DEV_BSHIFT)
53 53 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
54 54
55 55 /* common macros */
56 56 #ifndef MIN
57 57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 58 #endif
59 59 #ifndef MAX
60 60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 61 #endif
62 62 #ifndef ABS
63 63 #define ABS(a) ((a) < 0 ? -(a) : (a))
64 64 #endif
65 65 #ifndef SIGNOF
66 66 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
67 67 #endif
68 68
69 69 #ifdef _KERNEL
70 70
71 71 /*
72 72 * Convert a single byte to/from binary-coded decimal (BCD).
73 73 */
74 74 extern unsigned char byte_to_bcd[256];
75 75 extern unsigned char bcd_to_byte[256];
76 76
77 77 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
78 78 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
79 79
80 80 #endif /* _KERNEL */
81 81
82 82 /*
83 83 * WARNING: The device number macros defined here should not be used by device
84 84 * drivers or user software. Device drivers should use the device functions
85 85 * defined in the DDI/DKI interface (see also ddi.h). Application software
86 86 * should make use of the library routines available in makedev(3). A set of
87 87 * new device macros are provided to operate on the expanded device number
88 88 * format supported in SVR4. Macro versions of the DDI device functions are
89 89 * provided for use by kernel proper routines only. Macro routines bmajor(),
90 90 * major(), minor(), emajor(), eminor(), and makedev() will be removed or
91 91 * their definitions changed at the next major release following SVR4.
92 92 */
93 93
94 94 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
95 95 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
96 96 #define O_MAXMAJ 0x7f /* SVR3 max major value */
97 97 #define O_MAXMIN 0xff /* SVR3 max minor value */
98 98
99 99
100 100 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
101 101 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
102 102 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
103 103 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
104 104 /* For 3b2 hardware devices the minor is */
105 105 /* restricted to 256 (0-255) */
106 106
107 107 #ifdef _LP64
108 108 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
109 109 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
110 110 #define L_MAXMAJ 0xfffffffful /* max major value */
111 111 #define L_MAXMIN 0xfffffffful /* max minor value */
112 112 #else
113 113 #define L_BITSMAJOR L_BITSMAJOR32
114 114 #define L_BITSMINOR L_BITSMINOR32
115 115 #define L_MAXMAJ L_MAXMAJ32
116 116 #define L_MAXMIN L_MAXMIN32
117 117 #endif
118 118
119 119 #ifdef _KERNEL
120 120
121 121 /* major part of a device internal to the kernel */
122 122
123 123 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
124 124 #define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
125 125
126 126 /* get internal major part of expanded device number */
127 127
128 128 #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
129 129
130 130 /* minor part of a device internal to the kernel */
131 131
132 132 #define minor(x) (minor_t)((x) & O_MAXMIN)
133 133
134 134 /* get internal minor part of expanded device number */
135 135
136 136 #define getminor(x) (minor_t)((x) & L_MAXMIN)
137 137
138 138 #else /* _KERNEL */
139 139
140 140 /* major part of a device external from the kernel (same as emajor below) */
141 141
142 142 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
143 143
144 144 /* minor part of a device external from the kernel (same as eminor below) */
145 145
146 146 #define minor(x) (minor_t)((x) & O_MAXMIN)
147 147
148 148 #endif /* _KERNEL */
149 149
150 150 /* create old device number */
151 151
152 152 #define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
153 153
154 154 /* make an new device number */
155 155
156 156 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
157 157
158 158
159 159 /*
160 160 * emajor() allows kernel/driver code to print external major numbers
161 161 * eminor() allows kernel/driver code to print external minor numbers
162 162 */
163 163
164 164 #define emajor(x) \
165 165 (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
166 166 NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
167 167
168 168 #define eminor(x) \
169 169 (minor_t)((x) & O_MAXMIN)
170 170
171 171 /*
172 172 * get external major and minor device
173 173 * components from expanded device number
174 174 */
175 175 #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
176 176 NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
177 177 #define geteminor(x) (minor_t)((x) & L_MAXMIN)
178 178
179 179 /*
180 180 * These are versions of the kernel routines for compressing and
181 181 * expanding long device numbers that don't return errors.
182 182 */
183 183 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
184 184
185 185 #define DEVCMPL(x) (x)
186 186 #define DEVEXPL(x) (x)
187 187
188 188 #else
189 189
190 190 #define DEVCMPL(x) \
191 191 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
192 192 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
193 193 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
194 194
195 195 #define DEVEXPL(x) \
196 196 (((x) == NODEV32) ? NODEV : \
197 197 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
198 198
199 199 #endif /* L_BITSMAJOR32 ... */
200 200
201 201 /* convert to old (SVR3.2) dev format */
202 202
203 203 #define cmpdev(x) \
204 204 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
205 205 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
206 206 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
207 207
208 208 /* convert to new (SVR4) dev format */
209 209
210 210 #define expdev(x) \
211 211 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
212 212 ((x) & O_MAXMIN))
213 213
214 214 /*
215 215 * Macro for checking power of 2 address alignment.
216 216 */
217 217 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
218 218
219 219 /*
220 220 * Macros for counting and rounding.
221 221 */
222 222 #define howmany(x, y) (((x)+((y)-1))/(y))
223 223 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
224 224
225 225 /*
226 226 * Macro to determine if value is a power of 2
227 227 */
228 228 #define ISP2(x) (((x) & ((x) - 1)) == 0)
229 229
230 230 /*
231 231 * Macros for various sorts of alignment and rounding. The "align" must
232 232 * be a power of 2. Often times it is a block, sector, or page.
233 233 */
234 234
235 235 /*
236 236 * return x rounded down to an align boundary
237 237 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
238 238 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
239 239 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
240 240 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
241 241 */
242 242 #define P2ALIGN(x, align) ((x) & -(align))
243 243
244 244 /*
245 245 * return x % (mod) align
246 246 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
247 247 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
248 248 */
249 249 #define P2PHASE(x, align) ((x) & ((align) - 1))
250 250
251 251 /*
252 252 * return how much space is left in this block (but if it's perfectly
253 253 * aligned, return 0).
254 254 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
255 255 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
256 256 */
257 257 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
258 258
259 259 /*
260 260 * return x rounded up to an align boundary
261 261 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
262 262 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
263 263 */
264 264 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
265 265
266 266 /*
267 267 * return the ending address of the block that x is in
268 268 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
269 269 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
270 270 */
271 271 #define P2END(x, align) (-(~(x) & -(align)))
272 272
273 273 /*
274 274 * return x rounded up to the next phase (offset) within align.
275 275 * phase should be < align.
276 276 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
277 277 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
278 278 */
279 279 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
280 280
281 281 /*
282 282 * return TRUE if adding len to off would cause it to cross an align
283 283 * boundary.
284 284 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
285 285 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
286 286 */
287 287 #define P2BOUNDARY(off, len, align) \
288 288 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
289 289
290 290 /*
291 291 * Return TRUE if they have the same highest bit set.
292 292 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
293 293 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
294 294 */
295 295 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
296 296
297 297 /*
298 298 * Typed version of the P2* macros. These macros should be used to ensure
299 299 * that the result is correctly calculated based on the data type of (x),
300 300 * which is passed in as the last argument, regardless of the data
301 301 * type of the alignment. For example, if (x) is of type uint64_t,
302 302 * and we want to round it up to a page boundary using "PAGESIZE" as
303 303 * the alignment, we can do either
304 304 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
305 305 * or
306 306 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
307 307 */
308 308 #define P2ALIGN_TYPED(x, align, type) \
309 309 ((type)(x) & -(type)(align))
310 310 #define P2PHASE_TYPED(x, align, type) \
311 311 ((type)(x) & ((type)(align) - 1))
312 312 #define P2NPHASE_TYPED(x, align, type) \
313 313 (-(type)(x) & ((type)(align) - 1))
314 314 #define P2ROUNDUP_TYPED(x, align, type) \
315 315 (-(-(type)(x) & -(type)(align)))
316 316 #define P2END_TYPED(x, align, type) \
317 317 (-(~(type)(x) & -(type)(align)))
318 318 #define P2PHASEUP_TYPED(x, align, phase, type) \
319 319 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
320 320 #define P2CROSS_TYPED(x, y, align, type) \
321 321 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
322 322 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
323 323 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
324 324
325 325 /*
326 326 * Macros to atomically increment/decrement a variable. mutex and var
327 327 * must be pointers.
328 328 */
329 329 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
330 330 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
331 331
332 332 /*
333 333 * Macros to declare bitfields - the order in the parameter list is
334 334 * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
335 335 * because if a field crosses a byte boundary it's not likely to be meaningful
336 336 * without reassembly in its nonnative endianness.
337 337 */
338 338 #if defined(_BIT_FIELDS_LTOH)
339 339 #define DECL_BITFIELD2(_a, _b) \
340 340 uint8_t _a, _b
341 341 #define DECL_BITFIELD3(_a, _b, _c) \
342 342 uint8_t _a, _b, _c
343 343 #define DECL_BITFIELD4(_a, _b, _c, _d) \
344 344 uint8_t _a, _b, _c, _d
345 345 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
346 346 uint8_t _a, _b, _c, _d, _e
347 347 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
348 348 uint8_t _a, _b, _c, _d, _e, _f
349 349 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
350 350 uint8_t _a, _b, _c, _d, _e, _f, _g
351 351 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
352 352 uint8_t _a, _b, _c, _d, _e, _f, _g, _h
353 353 #elif defined(_BIT_FIELDS_HTOL)
354 354 #define DECL_BITFIELD2(_a, _b) \
355 355 uint8_t _b, _a
356 356 #define DECL_BITFIELD3(_a, _b, _c) \
357 357 uint8_t _c, _b, _a
358 358 #define DECL_BITFIELD4(_a, _b, _c, _d) \
359 359 uint8_t _d, _c, _b, _a
360 360 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
361 361 uint8_t _e, _d, _c, _b, _a
362 362 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
363 363 uint8_t _f, _e, _d, _c, _b, _a
364 364 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
365 365 uint8_t _g, _f, _e, _d, _c, _b, _a
↓ open down ↓ |
365 lines elided |
↑ open up ↑ |
366 366 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
367 367 uint8_t _h, _g, _f, _e, _d, _c, _b, _a
368 368 #else
369 369 #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
370 370 #endif /* _BIT_FIELDS_LTOH */
371 371
372 372 #if !defined(ARRAY_SIZE)
373 373 #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
374 374 #endif
375 375
376 +/*
377 + * Add a value to a uint64_t that saturates at UINT64_MAX instead of wrapping
378 + * around.
379 + */
380 +#define UINT64_OVERFLOW_ADD(val, add) \
381 + ((val) > ((val) + (add)) ? (UINT64_MAX) : ((val) + (add)))
382 +
383 +/*
384 + * Convert to an int64, saturating at INT64_MAX.
385 + */
386 +#define UINT64_OVERFLOW_TO_INT64(uval) \
387 + (((uval) > INT64_MAX) ? INT64_MAX : (int64_t)(uval))
388 +
376 389 #ifdef __cplusplus
377 390 }
378 391 #endif
379 392
380 393 #endif /* _SYS_SYSMACROS_H */
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX