xref: /xnu-8792.41.9/bsd/libkern/libkern.h (revision 5c2921b07a2480ab43ec66f5b9e41cb872bc554f)
1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions  * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions  *
4*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions  *
6*5c2921b0SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions  *
15*5c2921b0SApple OSS Distributions  * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions  *
18*5c2921b0SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions  * limitations under the License.
25*5c2921b0SApple OSS Distributions  *
26*5c2921b0SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions  */
28*5c2921b0SApple OSS Distributions /*-
29*5c2921b0SApple OSS Distributions  * Copyright (c) 1992, 1993
30*5c2921b0SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
31*5c2921b0SApple OSS Distributions  *
32*5c2921b0SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*5c2921b0SApple OSS Distributions  * modification, are permitted provided that the following conditions
34*5c2921b0SApple OSS Distributions  * are met:
35*5c2921b0SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
36*5c2921b0SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
37*5c2921b0SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
38*5c2921b0SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
39*5c2921b0SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
40*5c2921b0SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
41*5c2921b0SApple OSS Distributions  *    must display the following acknowledgement:
42*5c2921b0SApple OSS Distributions  *	This product includes software developed by the University of
43*5c2921b0SApple OSS Distributions  *	California, Berkeley and its contributors.
44*5c2921b0SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
45*5c2921b0SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
46*5c2921b0SApple OSS Distributions  *    without specific prior written permission.
47*5c2921b0SApple OSS Distributions  *
48*5c2921b0SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*5c2921b0SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*5c2921b0SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*5c2921b0SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*5c2921b0SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*5c2921b0SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*5c2921b0SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*5c2921b0SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*5c2921b0SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*5c2921b0SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*5c2921b0SApple OSS Distributions  * SUCH DAMAGE.
59*5c2921b0SApple OSS Distributions  *
60*5c2921b0SApple OSS Distributions  *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
61*5c2921b0SApple OSS Distributions  */
62*5c2921b0SApple OSS Distributions /*
63*5c2921b0SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64*5c2921b0SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
65*5c2921b0SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
66*5c2921b0SApple OSS Distributions  * Version 2.0.
67*5c2921b0SApple OSS Distributions  */
68*5c2921b0SApple OSS Distributions 
69*5c2921b0SApple OSS Distributions #ifndef _LIBKERN_LIBKERN_H_
70*5c2921b0SApple OSS Distributions #define _LIBKERN_LIBKERN_H_
71*5c2921b0SApple OSS Distributions 
72*5c2921b0SApple OSS Distributions #include <sys/appleapiopts.h>
73*5c2921b0SApple OSS Distributions #include <stdint.h>
74*5c2921b0SApple OSS Distributions #include <stdarg.h>     /* for platform-specific va_list */
75*5c2921b0SApple OSS Distributions #include <string.h>
76*5c2921b0SApple OSS Distributions #include <machine/limits.h>
77*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
78*5c2921b0SApple OSS Distributions #include <sys/types.h>
79*5c2921b0SApple OSS Distributions #include <mach/vm_param.h>
80*5c2921b0SApple OSS Distributions #include <libkern/crc.h>
81*5c2921b0SApple OSS Distributions #include <libkern/copyio.h>
82*5c2921b0SApple OSS Distributions 
83*5c2921b0SApple OSS Distributions #if defined(__arm__) || defined(__arm64__)
84*5c2921b0SApple OSS Distributions #include <arm/arch.h> /* for _ARM_ARCH_* */
85*5c2921b0SApple OSS Distributions #endif
86*5c2921b0SApple OSS Distributions 
87*5c2921b0SApple OSS Distributions #ifdef __APPLE_API_OBSOLETE
88*5c2921b0SApple OSS Distributions /* BCD conversions. */
89*5c2921b0SApple OSS Distributions extern u_char const     bcd2bin_data[];
90*5c2921b0SApple OSS Distributions extern u_char const     bin2bcd_data[];
91*5c2921b0SApple OSS Distributions 
92*5c2921b0SApple OSS Distributions #define bcd2bin(bcd)    (bcd2bin_data[bcd])
93*5c2921b0SApple OSS Distributions #define bin2bcd(bin)    (bin2bcd_data[bin])
94*5c2921b0SApple OSS Distributions #endif /* __APPLE_API_OBSOLETE */
95*5c2921b0SApple OSS Distributions 
96*5c2921b0SApple OSS Distributions #ifdef __APPLE_API_PRIVATE
97*5c2921b0SApple OSS Distributions extern char const       hex2ascii_data[];
98*5c2921b0SApple OSS Distributions 
99*5c2921b0SApple OSS Distributions #define hex2ascii(hex)  (hex2ascii_data[hex])
100*5c2921b0SApple OSS Distributions #endif /* __APPLE_API_PRIVATE */
101*5c2921b0SApple OSS Distributions 
102*5c2921b0SApple OSS Distributions __BEGIN_DECLS
103*5c2921b0SApple OSS Distributions static inline int
imax(int a,int b)104*5c2921b0SApple OSS Distributions imax(int a, int b)
105*5c2921b0SApple OSS Distributions {
106*5c2921b0SApple OSS Distributions 	return a > b ? a : b;
107*5c2921b0SApple OSS Distributions }
108*5c2921b0SApple OSS Distributions static inline int
imin(int a,int b)109*5c2921b0SApple OSS Distributions imin(int a, int b)
110*5c2921b0SApple OSS Distributions {
111*5c2921b0SApple OSS Distributions 	return a < b ? a : b;
112*5c2921b0SApple OSS Distributions }
113*5c2921b0SApple OSS Distributions static inline long
lmax(long a,long b)114*5c2921b0SApple OSS Distributions lmax(long a, long b)
115*5c2921b0SApple OSS Distributions {
116*5c2921b0SApple OSS Distributions 	return a > b ? a : b;
117*5c2921b0SApple OSS Distributions }
118*5c2921b0SApple OSS Distributions static inline long
lmin(long a,long b)119*5c2921b0SApple OSS Distributions lmin(long a, long b)
120*5c2921b0SApple OSS Distributions {
121*5c2921b0SApple OSS Distributions 	return a < b ? a : b;
122*5c2921b0SApple OSS Distributions }
123*5c2921b0SApple OSS Distributions static inline u_int
max(u_int a,u_int b)124*5c2921b0SApple OSS Distributions max(u_int a, u_int b)
125*5c2921b0SApple OSS Distributions {
126*5c2921b0SApple OSS Distributions 	return a > b ? a : b;
127*5c2921b0SApple OSS Distributions }
128*5c2921b0SApple OSS Distributions static inline u_int
min(u_int a,u_int b)129*5c2921b0SApple OSS Distributions min(u_int a, u_int b)
130*5c2921b0SApple OSS Distributions {
131*5c2921b0SApple OSS Distributions 	return a < b ? a : b;
132*5c2921b0SApple OSS Distributions }
133*5c2921b0SApple OSS Distributions static inline u_int32_t
ulmax(u_int32_t a,u_int32_t b)134*5c2921b0SApple OSS Distributions ulmax(u_int32_t a, u_int32_t b)
135*5c2921b0SApple OSS Distributions {
136*5c2921b0SApple OSS Distributions 	return a > b ? a : b;
137*5c2921b0SApple OSS Distributions }
138*5c2921b0SApple OSS Distributions static inline u_int32_t
ulmin(u_int32_t a,u_int32_t b)139*5c2921b0SApple OSS Distributions ulmin(u_int32_t a, u_int32_t b)
140*5c2921b0SApple OSS Distributions {
141*5c2921b0SApple OSS Distributions 	return a < b ? a : b;
142*5c2921b0SApple OSS Distributions }
143*5c2921b0SApple OSS Distributions 
144*5c2921b0SApple OSS Distributions 
145*5c2921b0SApple OSS Distributions 
146*5c2921b0SApple OSS Distributions /* Prototypes for non-quad routines. */
147*5c2921b0SApple OSS Distributions extern int      ffs(unsigned int);
148*5c2921b0SApple OSS Distributions extern int      ffsll(unsigned long long);
149*5c2921b0SApple OSS Distributions extern int      fls(unsigned int);
150*5c2921b0SApple OSS Distributions extern int      flsll(unsigned long long);
151*5c2921b0SApple OSS Distributions extern u_int32_t        random(void);
152*5c2921b0SApple OSS Distributions extern size_t   scanc(size_t, u_char *, const u_char *, u_char);
153*5c2921b0SApple OSS Distributions extern long     strtol(const char*, char **, int);
154*5c2921b0SApple OSS Distributions extern u_long   strtoul(const char *, char **, int);
155*5c2921b0SApple OSS Distributions extern quad_t   strtoq(const char *, char **, int);
156*5c2921b0SApple OSS Distributions extern u_quad_t strtouq(const char *, char **, int);
157*5c2921b0SApple OSS Distributions extern char     *strsep(char **, const char *);
158*5c2921b0SApple OSS Distributions extern void     *memchr(const void *, int, size_t);
159*5c2921b0SApple OSS Distributions extern void     url_decode(char *str);
160*5c2921b0SApple OSS Distributions 
161*5c2921b0SApple OSS Distributions /*
162*5c2921b0SApple OSS Distributions  * NOTE: snprintf() returns the full length of the formatted string even if it
163*5c2921b0SApple OSS Distributions  * couldn't fit in the supplied buffer.
164*5c2921b0SApple OSS Distributions  * Use scnprintf() if you need the actual number of bytes (minus the \0)
165*5c2921b0SApple OSS Distributions  */
166*5c2921b0SApple OSS Distributions int     snprintf(char *, size_t, const char *, ...) __printflike(3, 4);
167*5c2921b0SApple OSS Distributions int     scnprintf(char *, size_t, const char *, ...) __printflike(3, 4);
168*5c2921b0SApple OSS Distributions 
169*5c2921b0SApple OSS Distributions /* sprintf() is being deprecated. Please use snprintf() instead. */
170*5c2921b0SApple OSS Distributions int     sprintf(char *bufp, const char *, ...) __deprecated __printflike(2, 3);
171*5c2921b0SApple OSS Distributions int     sscanf(const char *, char const *, ...) __scanflike(2, 3);
172*5c2921b0SApple OSS Distributions int     printf(const char *, ...) __printflike(1, 2);
173*5c2921b0SApple OSS Distributions 
174*5c2921b0SApple OSS Distributions #if KERNEL_PRIVATE
175*5c2921b0SApple OSS Distributions int     _consume_printf_args(int, ...);
176*5c2921b0SApple OSS Distributions #endif
177*5c2921b0SApple OSS Distributions 
178*5c2921b0SApple OSS Distributions #if CONFIG_NO_PRINTF_STRINGS
179*5c2921b0SApple OSS Distributions #if KERNEL_PRIVATE
180*5c2921b0SApple OSS Distributions #define printf(x, ...)  _consume_printf_args( 0, ## __VA_ARGS__ )
181*5c2921b0SApple OSS Distributions #else
182*5c2921b0SApple OSS Distributions #define printf(x, ...)  do {} while (0)
183*5c2921b0SApple OSS Distributions #endif
184*5c2921b0SApple OSS Distributions #endif
185*5c2921b0SApple OSS Distributions 
186*5c2921b0SApple OSS Distributions uint16_t        crc16(uint16_t crc, const void *bufp, size_t len);
187*5c2921b0SApple OSS Distributions uint32_t        crc32(uint32_t crc, const void *bufp, size_t len);
188*5c2921b0SApple OSS Distributions 
189*5c2921b0SApple OSS Distributions #if XNU_KERNEL_PRIVATE
190*5c2921b0SApple OSS Distributions #if KASAN
191*5c2921b0SApple OSS Distributions uint16_t __nosan_crc16(uint16_t crc, const void *bufp, size_t len);
192*5c2921b0SApple OSS Distributions #else
193*5c2921b0SApple OSS Distributions static inline uint16_t
__nosan_crc16(uint16_t crc,const void * bufp,size_t len)194*5c2921b0SApple OSS Distributions __nosan_crc16(uint16_t crc, const void *bufp, size_t len)
195*5c2921b0SApple OSS Distributions {
196*5c2921b0SApple OSS Distributions 	return crc16(crc, bufp, len);
197*5c2921b0SApple OSS Distributions }
198*5c2921b0SApple OSS Distributions #endif
199*5c2921b0SApple OSS Distributions #endif
200*5c2921b0SApple OSS Distributions 
201*5c2921b0SApple OSS Distributions int     copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done);
202*5c2921b0SApple OSS Distributions int     copyinstr(const user_addr_t uaddr, void *kaddr, size_t len, size_t *done) OS_WARN_RESULT;
203*5c2921b0SApple OSS Distributions int     copyoutstr(const void *kaddr, user_addr_t udaddr, size_t len, size_t *done);
204*5c2921b0SApple OSS Distributions #if XNU_KERNEL_PRIVATE
205*5c2921b0SApple OSS Distributions int     copyin_atomic32(const user_addr_t user_addr, uint32_t *u32);
206*5c2921b0SApple OSS Distributions int     copyin_atomic32_wait_if_equals(const user_addr_t user_addr, uint32_t u32);
207*5c2921b0SApple OSS Distributions int     copyin_atomic64(const user_addr_t user_addr, uint64_t *u64);
208*5c2921b0SApple OSS Distributions int     copyout_atomic32(uint32_t u32, user_addr_t user_addr);
209*5c2921b0SApple OSS Distributions int     copyout_atomic64(uint64_t u64, user_addr_t user_addr);
210*5c2921b0SApple OSS Distributions int     copyoutstr_prevalidate(const void *kaddr, user_addr_t uaddr, size_t len);
211*5c2921b0SApple OSS Distributions #endif
212*5c2921b0SApple OSS Distributions 
213*5c2921b0SApple OSS Distributions int vsscanf(const char *, char const *, va_list);
214*5c2921b0SApple OSS Distributions 
215*5c2921b0SApple OSS Distributions extern int      vprintf(const char *, va_list) __printflike(1, 0);
216*5c2921b0SApple OSS Distributions extern int      vsnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
217*5c2921b0SApple OSS Distributions extern int      vscnprintf(char *, size_t, const char *, va_list) __printflike(3, 0);
218*5c2921b0SApple OSS Distributions 
219*5c2921b0SApple OSS Distributions #if XNU_KERNEL_PRIVATE
220*5c2921b0SApple OSS Distributions extern bool     printf_log_locked(bool addcr, const char*, ...) __printflike(2, 3);
221*5c2921b0SApple OSS Distributions extern bool     vprintf_log_locked(const char *, va_list, bool driverkit) __printflike(1, 0);
222*5c2921b0SApple OSS Distributions extern void     osobject_retain(void * object);
223*5c2921b0SApple OSS Distributions extern void     osobject_release(void * object);
224*5c2921b0SApple OSS Distributions #endif
225*5c2921b0SApple OSS Distributions 
226*5c2921b0SApple OSS Distributions /* vsprintf() is being deprecated. Please use vsnprintf() instead. */
227*5c2921b0SApple OSS Distributions extern int      vsprintf(char *bufp, const char *, va_list) __deprecated __printflike(2, 0);
228*5c2921b0SApple OSS Distributions 
229*5c2921b0SApple OSS Distributions #ifdef KERNEL_PRIVATE
230*5c2921b0SApple OSS Distributions #ifdef __arm__
231*5c2921b0SApple OSS Distributions void flush_inner_dcaches(void);
232*5c2921b0SApple OSS Distributions void clean_inner_dcaches(void);
233*5c2921b0SApple OSS Distributions #endif
234*5c2921b0SApple OSS Distributions extern void invalidate_icache(vm_offset_t, unsigned, int);
235*5c2921b0SApple OSS Distributions extern void flush_dcache(vm_offset_t, unsigned, int);
236*5c2921b0SApple OSS Distributions #else
237*5c2921b0SApple OSS Distributions extern void invalidate_icache(vm_offset_t, unsigned, int);
238*5c2921b0SApple OSS Distributions extern void flush_dcache(vm_offset_t, unsigned, int);
239*5c2921b0SApple OSS Distributions #endif
240*5c2921b0SApple OSS Distributions extern void invalidate_icache64(addr64_t, unsigned, int);
241*5c2921b0SApple OSS Distributions extern void flush_dcache64(addr64_t, unsigned, int);
242*5c2921b0SApple OSS Distributions 
243*5c2921b0SApple OSS Distributions 
244*5c2921b0SApple OSS Distributions static inline int
clz(unsigned int num)245*5c2921b0SApple OSS Distributions clz(unsigned int num)
246*5c2921b0SApple OSS Distributions {
247*5c2921b0SApple OSS Distributions #if (__arm__ || __arm64__)
248*5c2921b0SApple OSS Distributions 	// On ARM, clz(0) is defined to return number of bits in the input type
249*5c2921b0SApple OSS Distributions 	return __builtin_clz(num);
250*5c2921b0SApple OSS Distributions #else
251*5c2921b0SApple OSS Distributions 	// On Intel, clz(0) is undefined
252*5c2921b0SApple OSS Distributions 	return num ? __builtin_clz(num) : sizeof(num) * CHAR_BIT;
253*5c2921b0SApple OSS Distributions #endif
254*5c2921b0SApple OSS Distributions }
255*5c2921b0SApple OSS Distributions 
256*5c2921b0SApple OSS Distributions #if XNU_KERNEL_PRIVATE
257*5c2921b0SApple OSS Distributions 
258*5c2921b0SApple OSS Distributions /*
259*5c2921b0SApple OSS Distributions  * Define a function that for whatever reason needs to exist, but must never be
260*5c2921b0SApple OSS Distributions  * called.
261*5c2921b0SApple OSS Distributions  */
262*5c2921b0SApple OSS Distributions #define UNSUPPORTED_API(funcname, ...) \
263*5c2921b0SApple OSS Distributions 	_Pragma("clang diagnostic push") \
264*5c2921b0SApple OSS Distributions 	_Pragma("clang diagnostic ignored \"-Wunused-parameter\"") \
265*5c2921b0SApple OSS Distributions 	funcname(__VA_ARGS__) { panic("%s: unsupported API", __func__); } \
266*5c2921b0SApple OSS Distributions 	_Pragma("clang diagnostic pop")
267*5c2921b0SApple OSS Distributions 
268*5c2921b0SApple OSS Distributions #endif
269*5c2921b0SApple OSS Distributions 
270*5c2921b0SApple OSS Distributions __END_DECLS
271*5c2921b0SApple OSS Distributions 
272*5c2921b0SApple OSS Distributions #endif /* _LIBKERN_LIBKERN_H_ */
273