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