1 /* $NetBSD: libkern.h,v 1.140 2020/04/17 17:24:46 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * @(#)libkern.h 8.2 (Berkeley) 8/5/94 32 */ 33 34 #ifndef _LIB_LIBKERN_LIBKERN_H_ 35 #define _LIB_LIBKERN_LIBKERN_H_ 36 37 #ifdef _KERNEL_OPT 38 #include "opt_diagnostic.h" 39 #include "opt_kasan.h" 40 #include "opt_kcsan.h" 41 #include "opt_kmsan.h" 42 #endif 43 44 #include <sys/types.h> 45 #include <sys/inttypes.h> 46 #include <sys/null.h> 47 48 #ifndef LIBKERN_INLINE 49 #define LIBKERN_INLINE static __inline 50 #define LIBKERN_BODY 51 #endif 52 53 LIBKERN_INLINE int imax(int, int) __unused; 54 LIBKERN_INLINE int imin(int, int) __unused; 55 LIBKERN_INLINE u_int uimax(u_int, u_int) __unused; 56 LIBKERN_INLINE u_int uimin(u_int, u_int) __unused; 57 LIBKERN_INLINE long lmax(long, long) __unused; 58 LIBKERN_INLINE long lmin(long, long) __unused; 59 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused; 60 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused; 61 LIBKERN_INLINE int abs(int) __unused; 62 LIBKERN_INLINE long labs(long) __unused; 63 LIBKERN_INLINE long long llabs(long long) __unused; 64 LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused; 65 66 LIBKERN_INLINE int isspace(int) __unused; 67 LIBKERN_INLINE int isascii(int) __unused; 68 LIBKERN_INLINE int isupper(int) __unused; 69 LIBKERN_INLINE int islower(int) __unused; 70 LIBKERN_INLINE int isalpha(int) __unused; 71 LIBKERN_INLINE int isalnum(int) __unused; 72 LIBKERN_INLINE int isdigit(int) __unused; 73 LIBKERN_INLINE int isxdigit(int) __unused; 74 LIBKERN_INLINE int iscntrl(int) __unused; 75 LIBKERN_INLINE int isgraph(int) __unused; 76 LIBKERN_INLINE int isprint(int) __unused; 77 LIBKERN_INLINE int ispunct(int) __unused; 78 LIBKERN_INLINE int toupper(int) __unused; 79 LIBKERN_INLINE int tolower(int) __unused; 80 81 #ifdef LIBKERN_BODY 82 LIBKERN_INLINE int 83 imax(int a, int b) 84 { 85 return (a > b ? a : b); 86 } 87 LIBKERN_INLINE int 88 imin(int a, int b) 89 { 90 return (a < b ? a : b); 91 } 92 LIBKERN_INLINE long 93 lmax(long a, long b) 94 { 95 return (a > b ? a : b); 96 } 97 LIBKERN_INLINE long 98 lmin(long a, long b) 99 { 100 return (a < b ? a : b); 101 } 102 LIBKERN_INLINE u_int 103 uimax(u_int a, u_int b) 104 { 105 return (a > b ? a : b); 106 } 107 LIBKERN_INLINE u_int 108 uimin(u_int a, u_int b) 109 { 110 return (a < b ? a : b); 111 } 112 LIBKERN_INLINE u_long 113 ulmax(u_long a, u_long b) 114 { 115 return (a > b ? a : b); 116 } 117 LIBKERN_INLINE u_long 118 ulmin(u_long a, u_long b) 119 { 120 return (a < b ? a : b); 121 } 122 123 LIBKERN_INLINE int 124 abs(int j) 125 { 126 return(j < 0 ? -j : j); 127 } 128 129 LIBKERN_INLINE long 130 labs(long j) 131 { 132 return(j < 0 ? -j : j); 133 } 134 135 LIBKERN_INLINE long long 136 llabs(long long j) 137 { 138 return(j < 0 ? -j : j); 139 } 140 141 LIBKERN_INLINE intmax_t 142 imaxabs(intmax_t j) 143 { 144 return(j < 0 ? -j : j); 145 } 146 147 LIBKERN_INLINE int 148 isspace(int ch) 149 { 150 return (ch == ' ' || (ch >= '\t' && ch <= '\r')); 151 } 152 153 LIBKERN_INLINE int 154 isascii(int ch) 155 { 156 return ((ch & ~0x7f) == 0); 157 } 158 159 LIBKERN_INLINE int 160 isupper(int ch) 161 { 162 return (ch >= 'A' && ch <= 'Z'); 163 } 164 165 LIBKERN_INLINE int 166 islower(int ch) 167 { 168 return (ch >= 'a' && ch <= 'z'); 169 } 170 171 LIBKERN_INLINE int 172 isalpha(int ch) 173 { 174 return (isupper(ch) || islower(ch)); 175 } 176 177 LIBKERN_INLINE int 178 isalnum(int ch) 179 { 180 return (isalpha(ch) || isdigit(ch)); 181 } 182 183 LIBKERN_INLINE int 184 isdigit(int ch) 185 { 186 return (ch >= '0' && ch <= '9'); 187 } 188 189 LIBKERN_INLINE int 190 isxdigit(int ch) 191 { 192 return (isdigit(ch) || 193 (ch >= 'A' && ch <= 'F') || 194 (ch >= 'a' && ch <= 'f')); 195 } 196 197 LIBKERN_INLINE int 198 iscntrl(int ch) 199 { 200 return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F); 201 } 202 203 LIBKERN_INLINE int 204 isgraph(int ch) 205 { 206 return (ch != ' ' && isprint(ch)); 207 } 208 209 LIBKERN_INLINE int 210 isprint(int ch) 211 { 212 return (ch >= 0x20 && ch <= 0x7E); 213 } 214 215 LIBKERN_INLINE int 216 ispunct(int ch) 217 { 218 return (isprint(ch) && ch != ' ' && !isalnum(ch)); 219 } 220 221 LIBKERN_INLINE int 222 toupper(int ch) 223 { 224 if (islower(ch)) 225 return (ch - 0x20); 226 return (ch); 227 } 228 229 LIBKERN_INLINE int 230 tolower(int ch) 231 { 232 if (isupper(ch)) 233 return (ch + 0x20); 234 return (ch); 235 } 236 #endif 237 238 #define __NULL_STMT do { } while (/* CONSTCOND */ 0) 239 240 #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d " 241 242 #ifdef NDEBUG /* tradition! */ 243 #define assert(e) ((void)0) 244 #else 245 #define assert(e) (__predict_true((e)) ? (void)0 : \ 246 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__)) 247 #endif 248 249 #ifdef __COVERITY__ 250 #ifndef DIAGNOSTIC 251 #define DIAGNOSTIC 252 #endif 253 #endif 254 255 #ifndef CTASSERT 256 #define CTASSERT(x) __CTASSERT(x) 257 #endif 258 #ifndef CTASSERT_SIGNED 259 #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0) 260 #endif 261 #ifndef CTASSERT_UNSIGNED 262 #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0) 263 #endif 264 265 #ifndef DIAGNOSTIC 266 #define _DIAGASSERT(a) (void)0 267 #ifdef lint 268 #define KASSERTMSG(e, msg, ...) /* NOTHING */ 269 #define KASSERT(e) /* NOTHING */ 270 #else /* !lint */ 271 #define KASSERTMSG(e, msg, ...) ((void)0) 272 #define KASSERT(e) ((void)0) 273 #endif /* !lint */ 274 #else /* DIAGNOSTIC */ 275 #define _DIAGASSERT(a) assert(a) 276 #define KASSERTMSG(e, msg, ...) \ 277 (__predict_true((e)) ? (void)0 : \ 278 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \ 279 __FILE__, __LINE__, ## __VA_ARGS__)) 280 281 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 282 kern_assert(__KASSERTSTR, "diagnostic ", #e, \ 283 __FILE__, __LINE__)) 284 #endif 285 286 #ifndef DEBUG 287 #ifdef lint 288 #define KDASSERTMSG(e,msg, ...) /* NOTHING */ 289 #define KDASSERT(e) /* NOTHING */ 290 #else /* lint */ 291 #define KDASSERTMSG(e,msg, ...) ((void)0) 292 #define KDASSERT(e) ((void)0) 293 #endif /* lint */ 294 #else 295 #define KDASSERTMSG(e, msg, ...) \ 296 (__predict_true((e)) ? (void)0 : \ 297 kern_assert(__KASSERTSTR msg, "debugging ", #e, \ 298 __FILE__, __LINE__, ## __VA_ARGS__)) 299 300 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 301 kern_assert(__KASSERTSTR, "debugging ", #e, \ 302 __FILE__, __LINE__)) 303 #endif 304 305 /* 306 * XXX: For compatibility we use SMALL_RANDOM by default. 307 */ 308 #define SMALL_RANDOM 309 310 #ifndef offsetof 311 #if __GNUC_PREREQ__(4, 0) 312 #define offsetof(type, member) __builtin_offsetof(type, member) 313 #else 314 #define offsetof(type, member) \ 315 ((size_t)(unsigned long)(&(((type *)0)->member))) 316 #endif 317 #endif 318 319 /* 320 * Return the container of an embedded struct. Given x = &c->f, 321 * container_of(x, T, f) yields c, where T is the type of c. Example: 322 * 323 * struct foo { ... }; 324 * struct bar { 325 * int b_x; 326 * struct foo b_foo; 327 * ... 328 * }; 329 * 330 * struct bar b; 331 * struct foo *fp = b.b_foo; 332 * 333 * Now we can get at b from fp by: 334 * 335 * struct bar *bp = container_of(fp, struct bar, b_foo); 336 * 337 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of 338 * *fp does not match the type of struct bar::b_foo. 339 * We skip the validation for coverity runs to avoid warnings. 340 */ 341 #if defined(__COVERITY__) || defined(__LGTM_BOT__) 342 #define __validate_container_of(PTR, TYPE, FIELD) 0 343 #define __validate_const_container_of(PTR, TYPE, FIELD) 0 344 #else 345 #define __validate_container_of(PTR, TYPE, FIELD) \ 346 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \ 347 offsetof(TYPE, FIELD)))->FIELD)) 348 #define __validate_const_container_of(PTR, TYPE, FIELD) \ 349 (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) - \ 350 offsetof(TYPE, FIELD)))->FIELD)) 351 #endif 352 353 #define container_of(PTR, TYPE, FIELD) \ 354 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \ 355 + __validate_container_of(PTR, TYPE, FIELD)) 356 #define const_container_of(PTR, TYPE, FIELD) \ 357 ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD)) \ 358 + __validate_const_container_of(PTR, TYPE, FIELD)) 359 360 /* Prototypes for which GCC built-ins exist. */ 361 void *memcpy(void *, const void *, size_t); 362 int memcmp(const void *, const void *, size_t); 363 void *memset(void *, int, size_t); 364 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 365 #if defined(_KERNEL) && defined(KASAN) 366 void *kasan_memcpy(void *, const void *, size_t); 367 int kasan_memcmp(const void *, const void *, size_t); 368 void *kasan_memset(void *, int, size_t); 369 #define memcpy(d, s, l) kasan_memcpy(d, s, l) 370 #define memcmp(a, b, l) kasan_memcmp(a, b, l) 371 #define memset(d, v, l) kasan_memset(d, v, l) 372 #elif defined(_KERNEL) && defined(KCSAN) 373 void *kcsan_memcpy(void *, const void *, size_t); 374 int kcsan_memcmp(const void *, const void *, size_t); 375 void *kcsan_memset(void *, int, size_t); 376 #define memcpy(d, s, l) kcsan_memcpy(d, s, l) 377 #define memcmp(a, b, l) kcsan_memcmp(a, b, l) 378 #define memset(d, v, l) kcsan_memset(d, v, l) 379 #elif defined(_KERNEL) && defined(KMSAN) 380 void *kmsan_memcpy(void *, const void *, size_t); 381 int kmsan_memcmp(const void *, const void *, size_t); 382 void *kmsan_memset(void *, int, size_t); 383 #define memcpy(d, s, l) kmsan_memcpy(d, s, l) 384 #define memcmp(a, b, l) kmsan_memcmp(a, b, l) 385 #define memset(d, v, l) kmsan_memset(d, v, l) 386 #else 387 #define memcpy(d, s, l) __builtin_memcpy(d, s, l) 388 #define memcmp(a, b, l) __builtin_memcmp(a, b, l) 389 #define memset(d, v, l) __builtin_memset(d, v, l) 390 #endif 391 #endif 392 void *memmem(const void *, size_t, const void *, size_t); 393 394 char *strcpy(char *, const char *); 395 int strcmp(const char *, const char *); 396 size_t strlen(const char *); 397 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 398 #if defined(_KERNEL) && defined(KASAN) 399 char *kasan_strcpy(char *, const char *); 400 int kasan_strcmp(const char *, const char *); 401 size_t kasan_strlen(const char *); 402 #define strcpy(d, s) kasan_strcpy(d, s) 403 #define strcmp(a, b) kasan_strcmp(a, b) 404 #define strlen(a) kasan_strlen(a) 405 #elif defined(_KERNEL) && defined(KCSAN) 406 char *kcsan_strcpy(char *, const char *); 407 int kcsan_strcmp(const char *, const char *); 408 size_t kcsan_strlen(const char *); 409 #define strcpy(d, s) kcsan_strcpy(d, s) 410 #define strcmp(a, b) kcsan_strcmp(a, b) 411 #define strlen(a) kcsan_strlen(a) 412 #elif defined(_KERNEL) && defined(KMSAN) 413 char *kmsan_strcpy(char *, const char *); 414 int kmsan_strcmp(const char *, const char *); 415 size_t kmsan_strlen(const char *); 416 #define strcpy(d, s) kmsan_strcpy(d, s) 417 #define strcmp(a, b) kmsan_strcmp(a, b) 418 #define strlen(a) kmsan_strlen(a) 419 #else 420 #define strcpy(d, s) __builtin_strcpy(d, s) 421 #define strcmp(a, b) __builtin_strcmp(a, b) 422 #define strlen(a) __builtin_strlen(a) 423 #endif 424 #endif 425 size_t strnlen(const char *, size_t); 426 char *strsep(char **, const char *); 427 428 /* Functions for which we always use built-ins. */ 429 #ifdef __GNUC__ 430 #define alloca(s) __builtin_alloca(s) 431 #endif 432 433 /* These exist in GCC 3.x, but we don't bother. */ 434 char *strcat(char *, const char *); 435 char *strchr(const char *, int); 436 char *strrchr(const char *, int); 437 #if defined(_KERNEL) && defined(KASAN) 438 char *kasan_strcat(char *, const char *); 439 char *kasan_strchr(const char *, int); 440 char *kasan_strrchr(const char *, int); 441 #define strcat(d, s) kasan_strcat(d, s) 442 #define strchr(s, c) kasan_strchr(s, c) 443 #define strrchr(s, c) kasan_strrchr(s, c) 444 #elif defined(_KERNEL) && defined(KMSAN) 445 char *kmsan_strcat(char *, const char *); 446 char *kmsan_strchr(const char *, int); 447 char *kmsan_strrchr(const char *, int); 448 #define strcat(d, s) kmsan_strcat(d, s) 449 #define strchr(s, c) kmsan_strchr(s, c) 450 #define strrchr(s, c) kmsan_strrchr(s, c) 451 #endif 452 size_t strcspn(const char *, const char *); 453 char *strncpy(char *, const char *, size_t); 454 char *strncat(char *, const char *, size_t); 455 int strncmp(const char *, const char *, size_t); 456 char *strstr(const char *, const char *); 457 char *strpbrk(const char *, const char *); 458 size_t strspn(const char *, const char *); 459 460 /* 461 * ffs is an instruction on vax. 462 */ 463 int ffs(int); 464 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1)) 465 #define ffs(x) __builtin_ffs(x) 466 #endif 467 468 void kern_assert(const char *, ...) 469 __attribute__((__format__(__printf__, 1, 2))); 470 u_int32_t 471 inet_addr(const char *); 472 struct in_addr; 473 int inet_aton(const char *, struct in_addr *); 474 char *intoa(u_int32_t); 475 #define inet_ntoa(a) intoa((a).s_addr) 476 void *memchr(const void *, int, size_t); 477 478 void *memmove(void *, const void *, size_t); 479 #if defined(_KERNEL) && defined(KASAN) 480 void *kasan_memmove(void *, const void *, size_t); 481 #define memmove(d, s, l) kasan_memmove(d, s, l) 482 #elif defined(_KERNEL) && defined(KCSAN) 483 void *kcsan_memmove(void *, const void *, size_t); 484 #define memmove(d, s, l) kcsan_memmove(d, s, l) 485 #elif defined(_KERNEL) && defined(KMSAN) 486 void *kmsan_memmove(void *, const void *, size_t); 487 #define memmove(d, s, l) kmsan_memmove(d, s, l) 488 #endif 489 490 int pmatch(const char *, const char *, const char **); 491 #ifndef SMALL_RANDOM 492 void srandom(unsigned long); 493 char *initstate(unsigned long, char *, size_t); 494 char *setstate(char *); 495 #endif /* SMALL_RANDOM */ 496 long random(void); 497 void mi_vector_hash(const void * __restrict, size_t, uint32_t, 498 uint32_t[3]); 499 int scanc(u_int, const u_char *, const u_char *, int); 500 int skpc(int, size_t, u_char *); 501 int strcasecmp(const char *, const char *); 502 size_t strlcpy(char *, const char *, size_t); 503 size_t strlcat(char *, const char *, size_t); 504 int strncasecmp(const char *, const char *, size_t); 505 u_long strtoul(const char *, char **, int); 506 long long strtoll(const char *, char **, int); 507 unsigned long long strtoull(const char *, char **, int); 508 intmax_t strtoimax(const char *, char **, int); 509 uintmax_t strtoumax(const char *, char **, int); 510 intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t, 511 intmax_t, int *); 512 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t, 513 uintmax_t, int *); 514 void hexdump(void (*)(const char *, ...) __printflike(1, 2), 515 const char *, const void *, size_t); 516 517 int snprintb(char *, size_t, const char *, uint64_t); 518 int snprintb_m(char *, size_t, const char *, uint64_t, size_t); 519 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *), 520 void *); 521 uint32_t crc32(uint32_t, const uint8_t *, size_t); 522 #if __GNUC_PREREQ__(4, 5) \ 523 && (defined(__alpha_cix__) || defined(__mips_popcount)) 524 #define popcount __builtin_popcount 525 #define popcountl __builtin_popcountl 526 #define popcountll __builtin_popcountll 527 #define popcount32 __builtin_popcount 528 #define popcount64 __builtin_popcountll 529 #else 530 unsigned int popcount(unsigned int) __constfunc; 531 unsigned int popcountl(unsigned long) __constfunc; 532 unsigned int popcountll(unsigned long long) __constfunc; 533 unsigned int popcount32(uint32_t) __constfunc; 534 unsigned int popcount64(uint64_t) __constfunc; 535 #endif 536 537 void *explicit_memset(void *, int, size_t); 538 int consttime_memequal(const void *, const void *, size_t); 539 int strnvisx(char *, size_t, const char *, size_t, int); 540 #define VIS_OCTAL 0x01 541 #define VIS_SAFE 0x20 542 #define VIS_TRIM 0x40 543 544 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 545