| 1 | /* |
| 2 | * Copyright (c) 2000-2016 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
| 5 | * |
| 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. |
| 14 | * |
| 15 | * Please obtain a copy of the License at |
| 16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. |
| 17 | * |
| 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 |
| 20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
| 21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, |
| 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. |
| 25 | * |
| 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
| 27 | */ |
| 28 | /* |
| 29 | * @OSF_COPYRIGHT@ |
| 30 | */ |
| 31 | /* |
| 32 | * NOTICE: This file was modified by McAfee Research in 2004 to introduce |
| 33 | * support for mandatory and extensible security protections. This notice |
| 34 | * is included in support of clause 2.2 (b) of the Apple Public License, |
| 35 | * Version 2.0. |
| 36 | */ |
| 37 | |
| 38 | #ifndef _MISC_PROTOS_H_ |
| 39 | #define _MISC_PROTOS_H_ |
| 40 | |
| 41 | #include <stdarg.h> |
| 42 | #include <string.h> |
| 43 | #include <machine/setjmp.h> |
| 44 | #include <mach/boolean.h> |
| 45 | #include <mach/message.h> |
| 46 | #include <mach/machine/vm_types.h> |
| 47 | #include <ipc/ipc_types.h> |
| 48 | #include <kern/debug.h> |
| 49 | #include <libkern/copyio.h> |
| 50 | |
| 51 | #ifndef MIN |
| 52 | #define MIN(a, b) (((a)<(b))?(a):(b)) |
| 53 | #endif /* MIN */ |
| 54 | #ifndef MAX |
| 55 | #define MAX(a, b) (((a)>(b))?(a):(b)) |
| 56 | #endif /* MAX */ |
| 57 | |
| 58 | /* Set a bit in a bit array */ |
| 59 | extern void setbit( |
| 60 | int which, |
| 61 | int *bitmap); |
| 62 | |
| 63 | /* Clear a bit in a bit array */ |
| 64 | extern void clrbit( |
| 65 | int which, |
| 66 | int *bitmap); |
| 67 | |
| 68 | /* Find the first set bit in a bit array */ |
| 69 | extern int ffsbit( |
| 70 | int *bitmap); |
| 71 | extern int ffs( |
| 72 | unsigned int mask); |
| 73 | extern int ffsll( |
| 74 | unsigned long long mask); |
| 75 | |
| 76 | /* Find the last set bit in a bit array */ |
| 77 | extern int fls( |
| 78 | unsigned int mask); |
| 79 | extern int flsll( |
| 80 | unsigned long long mask); |
| 81 | |
| 82 | /* |
| 83 | * Test if indicated bit is set in bit string. |
| 84 | */ |
| 85 | extern int testbit( |
| 86 | int which, |
| 87 | int *bitmap); |
| 88 | |
| 89 | /* |
| 90 | * Move an aligned 32 or 64-bit word from user space to kernel space |
| 91 | * using a single read instruction |
| 92 | */ |
| 93 | extern int copyin_atomic32( |
| 94 | const user_addr_t user_addr, |
| 95 | uint32_t *kernel_addr); |
| 96 | |
| 97 | extern int copyin_atomic64( |
| 98 | const user_addr_t user_addr, |
| 99 | uint64_t *kernel_addr); |
| 100 | |
| 101 | #if CONFIG_DTRACE |
| 102 | extern int dtrace_nofault_copy8( |
| 103 | const uintptr_t kernel_addr, |
| 104 | uint8_t *value); |
| 105 | |
| 106 | extern int dtrace_nofault_copy16( |
| 107 | const uintptr_t kernel_addr, |
| 108 | uint16_t *value); |
| 109 | |
| 110 | extern int dtrace_nofault_copy32( |
| 111 | const uintptr_t kernel_addr, |
| 112 | uint32_t *value); |
| 113 | |
| 114 | extern int dtrace_nofault_copy64( |
| 115 | const uintptr_t kernel_addr, |
| 116 | uint64_t *value); |
| 117 | #endif /* CONFIG_DTRACE */ |
| 118 | |
| 119 | /* |
| 120 | * Does an atomic copyin at the specified user_address and compares |
| 121 | * it to the passed in value, and if it matches, waits. |
| 122 | * |
| 123 | * This is used to implement adaptive spinning for userspace synchronization |
| 124 | * |
| 125 | * Returns: |
| 126 | * 0: the value mached, and it paused efficiently for the platform |
| 127 | * ESTALE: the value didn't match, and it returned immediately |
| 128 | * other: the copyin failed (EFAULT, EINVAL, ...) |
| 129 | */ |
| 130 | extern int copyin_atomic32_wait_if_equals( |
| 131 | const user_addr_t user_addr, |
| 132 | uint32_t value); |
| 133 | |
| 134 | /* |
| 135 | * Move a 32 or 64-bit word from kernel space to user space |
| 136 | * using a single write instruction |
| 137 | */ |
| 138 | extern int copyout_atomic32( |
| 139 | uint32_t u32, |
| 140 | user_addr_t user_addr); |
| 141 | |
| 142 | extern int copyout_atomic64( |
| 143 | uint64_t u64, |
| 144 | user_addr_t user_addr); |
| 145 | |
| 146 | /* Move a NUL-terminated string from a user space to kernel space */ |
| 147 | extern int copyinstr( |
| 148 | const user_addr_t user_addr, |
| 149 | char *kernel_addr, |
| 150 | vm_size_t max, |
| 151 | vm_size_t *actual); |
| 152 | |
| 153 | /* Move arbitrarily-aligned data from a user space to kernel space */ |
| 154 | extern int copyinmsg( |
| 155 | const user_addr_t user_addr, |
| 156 | char *kernel_addr, |
| 157 | mach_msg_size_t nbytes); |
| 158 | |
| 159 | /* Move arbitrarily-aligned data from a kernel space to user space */ |
| 160 | extern int copyoutmsg( |
| 161 | const char *kernel_addr, |
| 162 | user_addr_t user_addr, |
| 163 | mach_msg_size_t nbytes); |
| 164 | |
| 165 | #if (DEBUG || DEVELOPMENT) |
| 166 | extern int verify_write(const void *source, void *dst, size_t size); |
| 167 | #endif |
| 168 | extern int sscanf(const char *input, const char *fmt, ...) __scanflike(2, 3); |
| 169 | |
| 170 | /* sprintf() is being deprecated. Please use snprintf() instead. */ |
| 171 | extern integer_t sprintf(char *buf, const char *fmt, ...) __printflike(2, 3) __deprecated; |
| 172 | |
| 173 | extern int printf(const char *format, ...) __printflike(1, 2); |
| 174 | extern int vprintf(const char *format, va_list ap) __printflike(1, 0); |
| 175 | |
| 176 | #if KERNEL_PRIVATE |
| 177 | int _consume_printf_args(int, ...); |
| 178 | #endif |
| 179 | |
| 180 | #if CONFIG_NO_PRINTF_STRINGS |
| 181 | #if KERNEL_PRIVATE |
| 182 | #define printf(x, ...) _consume_printf_args( 0, ## __VA_ARGS__ ) |
| 183 | #else |
| 184 | #define printf(x, ...) do {} while (0) |
| 185 | #endif |
| 186 | #endif |
| 187 | |
| 188 | extern int paniclog_append_noflush(const char *format, ...) __printflike(1, 2); |
| 189 | |
| 190 | extern int kdb_printf(const char *format, ...) __printflike(1, 2); |
| 191 | |
| 192 | extern int kdb_log(const char *format, ...) __printflike(1, 2); |
| 193 | |
| 194 | extern int kdb_printf_unbuffered(const char *format, ...) __printflike(1, 2); |
| 195 | |
| 196 | extern int snprintf(char *, size_t, const char *, ...) __printflike(3, 4); |
| 197 | extern int scnprintf(char *, size_t, const char *, ...) __printflike(3, 4); |
| 198 | |
| 199 | extern void log(int level, char *fmt, ...) __printflike(2, 3); |
| 200 | |
| 201 | void |
| 202 | _doprnt( |
| 203 | const char *fmt, |
| 204 | va_list *argp, |
| 205 | void (*putc)(char), |
| 206 | int radix) __printflike(1, 0); |
| 207 | |
| 208 | void |
| 209 | _doprnt_log( |
| 210 | const char *fmt, |
| 211 | va_list *argp, |
| 212 | void (*putc)(char), |
| 213 | int radix) __printflike(1, 0); |
| 214 | |
| 215 | int |
| 216 | __doprnt( |
| 217 | const char *fmt, |
| 218 | va_list argp, |
| 219 | void (*putc)(int, void *), |
| 220 | void *arg, |
| 221 | int radix, |
| 222 | int is_log) __printflike(1, 0); |
| 223 | |
| 224 | extern void console_write_char(char); |
| 225 | |
| 226 | extern void conslog_putc(char); |
| 227 | |
| 228 | extern void cons_putc_locked(char); |
| 229 | |
| 230 | extern void consdebug_putc(char); |
| 231 | |
| 232 | extern void consdebug_log(char); |
| 233 | |
| 234 | extern void consdebug_putc_unbuffered(char); |
| 235 | |
| 236 | extern void console_write_unbuffered(char); |
| 237 | |
| 238 | extern void console_write(char *, int); |
| 239 | |
| 240 | extern void console_suspend(void); |
| 241 | |
| 242 | extern void console_resume(void); |
| 243 | |
| 244 | extern int console_read_char(void); |
| 245 | |
| 246 | extern int console_try_read_char(void); |
| 247 | |
| 248 | extern int _setjmp( |
| 249 | jmp_buf_t *jmp_buf); |
| 250 | |
| 251 | extern int _longjmp( |
| 252 | jmp_buf_t *jmp_buf, |
| 253 | int value); |
| 254 | |
| 255 | extern void bootstrap_create(void); |
| 256 | |
| 257 | extern kern_return_t kernel_set_special_port( |
| 258 | host_priv_t host_priv, |
| 259 | int which, |
| 260 | ipc_port_t port); |
| 261 | |
| 262 | extern kern_return_t kernel_get_special_port( |
| 263 | host_priv_t host_priv, |
| 264 | int which, |
| 265 | ipc_port_t *portp); |
| 266 | |
| 267 | user_addr_t get_useraddr(void); |
| 268 | |
| 269 | /* symbol lookup */ |
| 270 | #ifndef __cplusplus |
| 271 | struct kmod_info_t; |
| 272 | #endif |
| 273 | |
| 274 | extern uint64_t early_random(void); |
| 275 | |
| 276 | #endif /* _MISC_PROTOS_H_ */ |
| 277 | |