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