xref: /netbsd-src/sys/lib/libkern/libkern.h (revision ed75d7a867996c84cfa88e3b8906816277e957f7)
1 /*	$NetBSD: libkern.h,v 1.137 2019/12/14 17:23:47 riastradh 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 /* Prototypes for which GCC built-ins exist. */
360 void	*memcpy(void *, const void *, size_t);
361 int	 memcmp(const void *, const void *, size_t);
362 void	*memset(void *, int, size_t);
363 void	*memmem(const void *, size_t, const void *, 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 
393 char	*strcpy(char *, const char *);
394 int	 strcmp(const char *, const char *);
395 size_t	 strlen(const char *);
396 size_t	 strnlen(const char *, size_t);
397 char	*strsep(char **, 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 
427 /* Functions for which we always use built-ins. */
428 #ifdef __GNUC__
429 #define	alloca(s)		__builtin_alloca(s)
430 #endif
431 
432 /* These exist in GCC 3.x, but we don't bother. */
433 #if defined(_KERNEL) && defined(KMSAN)
434 char	*kmsan_strcat(char *, const char *);
435 char	*kmsan_strchr(const char *, int);
436 char	*kmsan_strrchr(const char *, int);
437 #define	strcat(d, s)		kmsan_strcat(d, s)
438 #define	strchr(s, c)		kmsan_strchr(s, c)
439 #define	strrchr(s, c)		kmsan_strrchr(s, c)
440 #else
441 char	*strcat(char *, const char *);
442 char	*strchr(const char *, int);
443 char	*strrchr(const char *, int);
444 #endif
445 size_t	 strcspn(const char *, const char *);
446 char	*strncpy(char *, const char *, size_t);
447 char	*strncat(char *, const char *, size_t);
448 int	 strncmp(const char *, const char *, size_t);
449 char	*strstr(const char *, const char *);
450 char	*strpbrk(const char *, const char *);
451 size_t	 strspn(const char *, const char *);
452 
453 /*
454  * ffs is an instruction on vax.
455  */
456 int	 ffs(int);
457 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
458 #define	ffs(x)		__builtin_ffs(x)
459 #endif
460 
461 void	 kern_assert(const char *, ...)
462     __attribute__((__format__(__printf__, 1, 2)));
463 u_int32_t
464 	inet_addr(const char *);
465 struct in_addr;
466 int	inet_aton(const char *, struct in_addr *);
467 char	*intoa(u_int32_t);
468 #define inet_ntoa(a) intoa((a).s_addr)
469 void	*memchr(const void *, int, size_t);
470 
471 void	*memmove(void *, const void *, size_t);
472 #if defined(_KERNEL) && defined(KASAN)
473 void	*kasan_memmove(void *, const void *, size_t);
474 #define	memmove(d, s, l)	kasan_memmove(d, s, l)
475 #elif defined(_KERNEL) && defined(KCSAN)
476 void	*kcsan_memmove(void *, const void *, size_t);
477 #define	memmove(d, s, l)	kcsan_memmove(d, s, l)
478 #elif defined(_KERNEL) && defined(KMSAN)
479 void	*kmsan_memmove(void *, const void *, size_t);
480 #define	memmove(d, s, l)	kmsan_memmove(d, s, l)
481 #endif
482 
483 int	 pmatch(const char *, const char *, const char **);
484 #ifndef SMALL_RANDOM
485 void	 srandom(unsigned long);
486 char	*initstate(unsigned long, char *, size_t);
487 char	*setstate(char *);
488 #endif /* SMALL_RANDOM */
489 long	 random(void);
490 void	 mi_vector_hash(const void * __restrict, size_t, uint32_t,
491 	    uint32_t[3]);
492 int	 scanc(u_int, const u_char *, const u_char *, int);
493 int	 skpc(int, size_t, u_char *);
494 int	 strcasecmp(const char *, const char *);
495 size_t	 strlcpy(char *, const char *, size_t);
496 size_t	 strlcat(char *, const char *, size_t);
497 int	 strncasecmp(const char *, const char *, size_t);
498 u_long	 strtoul(const char *, char **, int);
499 long long strtoll(const char *, char **, int);
500 unsigned long long strtoull(const char *, char **, int);
501 intmax_t  strtoimax(const char *, char **, int);
502 uintmax_t strtoumax(const char *, char **, int);
503 intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
504     intmax_t, int *);
505 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
506     uintmax_t, int *);
507 void	 hexdump(void (*)(const char *, ...) __printflike(1, 2),
508     const char *, const void *, size_t);
509 
510 int	 snprintb(char *, size_t, const char *, uint64_t);
511 int	 snprintb_m(char *, size_t, const char *, uint64_t, size_t);
512 int	 kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
513 		   void *);
514 uint32_t crc32(uint32_t, const uint8_t *, size_t);
515 #if __GNUC_PREREQ__(4, 5) \
516     && (defined(__alpha_cix__) || defined(__mips_popcount))
517 #define	popcount	__builtin_popcount
518 #define	popcountl	__builtin_popcountl
519 #define	popcountll	__builtin_popcountll
520 #define	popcount32	__builtin_popcount
521 #define	popcount64	__builtin_popcountll
522 #else
523 unsigned int	popcount(unsigned int) __constfunc;
524 unsigned int	popcountl(unsigned long) __constfunc;
525 unsigned int	popcountll(unsigned long long) __constfunc;
526 unsigned int	popcount32(uint32_t) __constfunc;
527 unsigned int	popcount64(uint64_t) __constfunc;
528 #endif
529 
530 void	*explicit_memset(void *, int, size_t);
531 int	consttime_memequal(const void *, const void *, size_t);
532 int	strnvisx(char *, size_t, const char *, size_t, int);
533 #define VIS_OCTAL	0x01
534 #define VIS_SAFE	0x20
535 #define VIS_TRIM	0x40
536 
537 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
538