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