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