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