Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | /* * Copyright (c) 2003-2006 Apple Computer, Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_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. * * 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. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <stdint.h> #include <mach/boolean.h> #include <mach/mach_types.h> #include <sys/syscall.h> #include <sys/types.h> /* u_int */ #include <sys/proc.h> /* proc_t */ #include <sys/systm.h> /* struct sysent */ #include <sys/sysproto.h> #include <sys/kdebug.h> /* KDEBUG_ENABLE_CHUD */ #include <sys/kauth.h> /* kauth_cred_get */ #include <libkern/OSAtomic.h> #if CONFIG_MACF #include <security/mac_framework.h> /* mac_system_check_chud */ #endif #pragma mark **** kern debug **** typedef void (*chudxnu_kdebug_callback_func_t)(uint32_t debugid, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4); static void chud_null_kdebug(uint32_t debugid, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4); static chudxnu_kdebug_callback_func_t kdebug_callback_fn = chud_null_kdebug; kern_return_t chudxnu_kdebug_callback_enter(chudxnu_kdebug_callback_func_t); kern_return_t chudxnu_kdebug_callback_cancel(void); extern void kdbg_control_chud(int val, void *fn); extern void kperf_kdebug_callback(uint32_t debugid); static void chud_null_kdebug(uint32_t debugid __unused, uintptr_t arg0 __unused, uintptr_t arg1 __unused, uintptr_t arg2 __unused, uintptr_t arg3 __unused, uintptr_t arg4 __unused) { return; } static void chudxnu_private_kdebug_callback( uint32_t debugid, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4) { chudxnu_kdebug_callback_func_t fn = kdebug_callback_fn; #if KPERF /* call out to kperf first */ kperf_kdebug_callback(debugid); #endif if(fn) { (fn)(debugid, arg0, arg1, arg2, arg3, arg4); } } __private_extern__ kern_return_t chudxnu_kdebug_callback_enter(chudxnu_kdebug_callback_func_t func) { /* Atomically set the callback. */ if(OSCompareAndSwapPtr(chud_null_kdebug, func, (void * volatile *)&kdebug_callback_fn)) { kdbg_control_chud(TRUE, (void *)chudxnu_private_kdebug_callback); return KERN_SUCCESS; } return KERN_FAILURE; } __private_extern__ kern_return_t chudxnu_kdebug_callback_cancel(void) { kdbg_control_chud(FALSE, NULL); chudxnu_kdebug_callback_func_t old = kdebug_callback_fn; while(!OSCompareAndSwapPtr(old, chud_null_kdebug, (void * volatile *)&kdebug_callback_fn)) { old = kdebug_callback_fn; } return KERN_SUCCESS; } #pragma mark **** CHUD syscall **** typedef kern_return_t (*chudxnu_syscall_callback_func_t)(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); static kern_return_t chud_null_syscall(uint64_t code, uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3, uint64_t arg4); static chudxnu_syscall_callback_func_t syscall_callback_fn = chud_null_syscall; kern_return_t chudxnu_syscall_callback_enter(chudxnu_syscall_callback_func_t func); kern_return_t chudxnu_syscall_callback_cancel(void); static kern_return_t chud_null_syscall(uint64_t code __unused, uint64_t arg0 __unused, uint64_t arg1 __unused, uint64_t arg2 __unused, uint64_t arg3 __unused, uint64_t arg4 __unused) { return (kern_return_t)EINVAL; } /* * chud * * Performs performance-related tasks. A private interface registers a handler for this * system call. The implementation is in the CHUDProf kernel extension. * * chud() is a callback style system call used by the CHUD Tools suite of performance tools. If the CHUD * kexts are not loaded, this system call will always return EINVAL. The CHUD kexts contain the * implementation of the system call. * * The current behavior of the chud() system call is as follows: * * Parameters: p (ignored) * uap User argument descriptor (see below) * retval return value of fn (the function returned by syscall_callback_fn) * * Indirect parameters: uap->code Selects the operation to do. This is broken down into a * 16-bit facility and a 16-bit action. * * The rest of the indirect parameters depend on the facility and the action that is selected: * * Facility: 1 Amber instruction tracer * Action: 1 Indicate that a new thread has been created. No arguments are used. * * Action: 2 Indicate that a thread is about to exit. No arguments are used. * * Facility: 2 Not Supported for this system call * * Facility: 3 CHUD Trace facility * Action: 1 Record a backtrace of the calling process into the CHUD Trace facility sample * buffer. * * uap->arg1 Number of frames to skip * uap->arg2 Pointer to a uint64_t containing a timestamp for the * beginning of the sample. NULL uses the current time. * uap->arg3 Pointer to a uint64_t containing a timestamp for the end * of the sample. NULL uses the current time. * uap->arg4 Pointer to auxiliary data to be recorded with the sample * uap->arg5 Size of the auxiliary data pointed to by arg4. * * Returns: EINVAL If syscall_callback_fn returns an invalid function * KERN_SUCCESS Success * KERN_FAILURE Generic failure * KERN_NO_SPACE Auxiliary data is too large (only used by Facility: 3) * * Implicit returns: retval return value of fn (the function returned by syscall_callback_fn) */ int chud(__unused proc_t p, struct chud_args *uap, int32_t *retval) { #if CONFIG_MACF int error = mac_system_check_chud(kauth_cred_get()); if (error) return error; #endif chudxnu_syscall_callback_func_t fn = syscall_callback_fn; if(!fn) { return EINVAL; } *retval = fn(uap->code, uap->arg1, uap->arg2, uap->arg3, uap->arg4, uap->arg5); return 0; } __private_extern__ kern_return_t chudxnu_syscall_callback_enter(chudxnu_syscall_callback_func_t func) { if(OSCompareAndSwapPtr(chud_null_syscall, func, (void * volatile *)&syscall_callback_fn)) { return KERN_SUCCESS; } return KERN_FAILURE; } __private_extern__ kern_return_t chudxnu_syscall_callback_cancel(void) { chudxnu_syscall_callback_func_t old = syscall_callback_fn; while(!OSCompareAndSwapPtr(old, chud_null_syscall, (void * volatile *)&syscall_callback_fn)) { old = syscall_callback_fn; } return KERN_SUCCESS; } /* DTrace callback */ typedef kern_return_t (*chudxnu_dtrace_callback_t)(uint64_t selector, uint64_t *args, uint32_t count); int chudxnu_dtrace_callback(uint64_t selector, uint64_t *args, uint32_t count); kern_return_t chudxnu_dtrace_callback_enter(chudxnu_dtrace_callback_t fn); void chudxnu_dtrace_callback_cancel(void); int chud_null_dtrace(uint64_t selector, uint64_t *args, uint32_t count); static chudxnu_dtrace_callback_t dtrace_callback = (chudxnu_dtrace_callback_t) chud_null_dtrace; int chud_null_dtrace(uint64_t selector __unused, uint64_t *args __unused, uint32_t count __unused) { return ENXIO; } int chudxnu_dtrace_callback(uint64_t selector, uint64_t *args, uint32_t count) { /* If no callback is hooked up, let's return ENXIO */ int ret = ENXIO; /* Make a local stack copy of the function ptr */ chudxnu_dtrace_callback_t fn = dtrace_callback; if(fn) { ret = fn(selector, args, count); } return ret; } __private_extern__ kern_return_t chudxnu_dtrace_callback_enter(chudxnu_dtrace_callback_t fn) { /* Atomically enter the call back */ if(!OSCompareAndSwapPtr(chud_null_dtrace, fn, (void * volatile *) &dtrace_callback)) { return KERN_FAILURE; } return KERN_SUCCESS; } __private_extern__ void chudxnu_dtrace_callback_cancel(void) { chudxnu_dtrace_callback_t old_fn = dtrace_callback; /* Atomically clear the call back */ while(!OSCompareAndSwapPtr(old_fn, chud_null_dtrace, (void * volatile *) &dtrace_callback)) { old_fn = dtrace_callback; } } |