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