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