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