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