]> git.saurik.com Git - apple/xnu.git/blobdiff - bsd/libkern/libkern.h
xnu-4903.231.4.tar.gz
[apple/xnu.git] / bsd / libkern / libkern.h
index e8fb77220095ad05aa95534c9a17a872f9c753be..73545298d305961360af39e1b3e25e6c93d5ef70 100644 (file)
@@ -1,31 +1,29 @@
 /*
 /*
- * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
+ * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
+ *
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
  * 
- * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
  * 
- * This file contains Original Code and/or Modifications of Original Code 
- * as defined in and that are subject to the Apple Public Source License 
- * Version 2.0 (the 'License'). You may not use this file except in 
- * compliance with the License.  The rights granted to you under the 
- * License may not be used to create, or enable the creation or 
- * redistribution of, unlawful or unlicensed copies of an Apple operating 
- * system, or to circumvent, violate, or enable the circumvention or 
- * violation of, any terms of an Apple operating system software license 
- * agreement.
- *
- * Please obtain a copy of the License at 
- * http://www.opensource.apple.com/apsl/ and read it before using this 
- * file.
- *
- * The Original Code and all software distributed under the License are 
- * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
- * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
- * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
- * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
- * Please see the License for the specific language governing rights and 
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
+ * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
  * limitations under the License.
  * limitations under the License.
- *
- * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
+ * 
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*-
  * Copyright (c) 1992, 1993
  */
 /*-
  * Copyright (c) 1992, 1993
  *
  *     @(#)libkern.h   8.1 (Berkeley) 6/10/93
  */
  *
  *     @(#)libkern.h   8.1 (Berkeley) 6/10/93
  */
+/*
+ * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
+ * support for mandatory and extensible security protections.  This notice
+ * is included in support of clause 2.2 (b) of the Apple Public License,
+ * Version 2.0.
+ */
 
 #ifndef _LIBKERN_LIBKERN_H_
 #define _LIBKERN_LIBKERN_H_
 
 #include <sys/appleapiopts.h>
 #include <stdint.h>
 
 #ifndef _LIBKERN_LIBKERN_H_
 #define _LIBKERN_LIBKERN_H_
 
 #include <sys/appleapiopts.h>
 #include <stdint.h>
+#include <stdarg.h>    /* for platform-specific va_list */
 #include <string.h>
 #include <string.h>
+#include <machine/limits.h>
 #include <sys/cdefs.h>
 #include <sys/types.h>
 #include <mach/vm_param.h>
 #include <sys/cdefs.h>
 #include <sys/types.h>
 #include <mach/vm_param.h>
+#include <libkern/crc.h>
+#include <libkern/copyio.h>
+
+#if defined(__arm__) || defined(__arm64__)
+#include <arm/arch.h> /* for _ARM_ARCH_* */
+#endif
 
 #ifdef __APPLE_API_OBSOLETE
 /* BCD conversions. */
 
 #ifdef __APPLE_API_OBSOLETE
 /* BCD conversions. */
@@ -118,51 +130,128 @@ min(u_int a, u_int b)
 {
        return (a < b ? a : b);
 }
 {
        return (a < b ? a : b);
 }
-static inline u_long
-ulmax(u_long a, u_long b)
+static inline u_int32_t
+ulmax(u_int32_t a, u_int32_t b)
 {
        return (a > b ? a : b);
 }
 {
        return (a > b ? a : b);
 }
-static inline u_long
-ulmin(u_long a, u_long b)
+static inline u_int32_t
+ulmin(u_int32_t a, u_int32_t b)
 {
        return (a < b ? a : b);
 }
 
 {
        return (a < b ? a : b);
 }
 
+
+
 /* Prototypes for non-quad routines. */
 extern int     ffs(int);
 /* Prototypes for non-quad routines. */
 extern int     ffs(int);
-extern int     locc(int, char *, u_int);
-extern u_long  random(void);
-extern char    *rindex(const char *, int);
+extern int     ffsll(unsigned long long);
+extern int     fls(int);
+extern int     flsll(unsigned long long);
+extern u_int32_t       random(void);
 extern int     scanc(u_int, u_char *, const u_char *, int);
 extern int     skpc(int, int, char *);
 extern long    strtol(const char*, char **, int);
 extern u_long  strtoul(const char *, char **, int);
 extern quad_t  strtoq(const char *, char **, int);
 extern u_quad_t strtouq(const char *, char **, int);
 extern int     scanc(u_int, u_char *, const u_char *, int);
 extern int     skpc(int, int, char *);
 extern long    strtol(const char*, char **, int);
 extern u_long  strtoul(const char *, char **, int);
 extern quad_t  strtoq(const char *, char **, int);
 extern u_quad_t strtouq(const char *, char **, int);
+extern char    *strsep(char **, const char *);
+extern void    *memchr(const void *, int, size_t);
+extern void    url_decode(char *str);
+
+int    snprintf(char *, size_t, const char *, ...) __printflike(3,4);
 
 
-int    snprintf(char *, size_t, const char *, ...);
-int    sprintf(char *bufp, const char *, ...);
-int    sscanf(const char *, char const *, ...);
-void printf(const char *, ...);
+/* sprintf() is being deprecated. Please use snprintf() instead. */
+int    sprintf(char *bufp, const char *, ...) __deprecated __printflike(2,3);
+int    sscanf(const char *, char const *, ...) __scanflike(2,3);
+int    printf(const char *, ...) __printflike(1,2);
 
 
+#if KERNEL_PRIVATE
+int    _consume_printf_args(int, ...);
+#endif
+
+#if CONFIG_NO_PRINTF_STRINGS
+#if KERNEL_PRIVATE
+#define printf(x, ...)  _consume_printf_args( 0, ## __VA_ARGS__ )
+#else
+#define printf(x, ...)  do {} while (0)
+#endif
+#endif
+
+uint16_t       crc16(uint16_t crc, const void *bufp, size_t len);
 uint32_t       crc32(uint32_t crc, const void *bufp, size_t len);
 
 uint32_t       crc32(uint32_t crc, const void *bufp, size_t len);
 
+#if XNU_KERNEL_PRIVATE
+#if KASAN
+uint16_t __nosan_crc16(uint16_t crc, const void *bufp, size_t len);
+#else
+static inline uint16_t
+__nosan_crc16(uint16_t crc, const void *bufp, size_t len) { return crc16(crc, bufp, len); }
+#endif
+#endif
+
 int    copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done);
 int    copyinstr(const user_addr_t uaddr, void *kaddr, size_t len, size_t *done);
 int    copyoutstr(const void *kaddr, user_addr_t udaddr, size_t len, size_t *done);
 int    copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done);
 int    copyinstr(const user_addr_t uaddr, void *kaddr, size_t len, size_t *done);
 int    copyoutstr(const void *kaddr, user_addr_t udaddr, size_t len, size_t *done);
-int    copyin(const user_addr_t uaddr, void *kaddr, size_t len);
-int    copyout(const void *kaddr, user_addr_t udaddr, size_t len);
+#if XNU_KERNEL_PRIVATE
+extern int copyin_word(const user_addr_t user_addr, uint64_t *kernel_addr, vm_size_t nbytes);
+#endif
 
 
-int vsscanf(const char *, char const *, __darwin_va_list);
-extern int     vsnprintf(char *, size_t, const char *, __darwin_va_list);
-extern int     vsprintf(char *bufp, const char *, __darwin_va_list); 
+int vsscanf(const char *, char const *, va_list);
 
 
+extern int     vprintf(const char *, va_list) __printflike(1,0);
+extern int     vsnprintf(char *, size_t, const char *, va_list) __printflike(3,0);
+
+#if XNU_KERNEL_PRIVATE
+extern int     vprintf_log_locked(const char *, va_list) __printflike(1,0);
+extern void    osobject_retain(void * object);
+extern void    osobject_release(void * object);
+#endif
+
+/* vsprintf() is being deprecated. Please use vsnprintf() instead. */
+extern int     vsprintf(char *bufp, const char *, va_list) __deprecated __printflike(2,0);
+
+#ifdef KERNEL_PRIVATE
+#ifdef __arm__
+void flush_inner_dcaches(void);
+void clean_inner_dcaches(void);
+#endif
+extern void invalidate_icache(vm_offset_t, unsigned, int);
+extern void flush_dcache(vm_offset_t, unsigned, int);
+#else
 extern void invalidate_icache(vm_offset_t, unsigned, int);
 extern void flush_dcache(vm_offset_t, unsigned, int);
 extern void invalidate_icache(vm_offset_t, unsigned, int);
 extern void flush_dcache(vm_offset_t, unsigned, int);
+#endif
 extern void invalidate_icache64(addr64_t, unsigned, int);
 extern void flush_dcache64(addr64_t, unsigned, int);
 
 extern void invalidate_icache64(addr64_t, unsigned, int);
 extern void flush_dcache64(addr64_t, unsigned, int);
 
+
+static inline int
+clz(unsigned int num)
+{
+#if (__arm__ || __arm64__)
+       // On ARM, clz(0) is defined to return number of bits in the input type
+       return __builtin_clz(num);
+#else
+       // On Intel, clz(0) is undefined
+       return num ? __builtin_clz(num) : sizeof(num) * CHAR_BIT;
+#endif
+}
+
+#if XNU_KERNEL_PRIVATE
+
+/*
+ * Define a function that for whatever reason needs to exist, but must never be
+ * called.
+ */
+#define UNSUPPORTED_API(funcname, ...) \
+       _Pragma("clang diagnostic push") \
+       _Pragma("clang diagnostic ignored \"-Wunused-parameter\"") \
+       funcname(__VA_ARGS__) { panic(__func__ ": unsupported API\n"); } \
+       _Pragma("clang diagnostic pop")
+
+#endif
+
 __END_DECLS
 
 #endif /* _LIBKERN_LIBKERN_H_ */
 __END_DECLS
 
 #endif /* _LIBKERN_LIBKERN_H_ */