]>
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 A |
38 | #include <sys/kdebug.h> /* KDEBUG_ENABLE_CHUD */ |
39 | #include <libkern/OSAtomic.h> | |
40 | ||
41 | #ifdef __ppc__ | |
42 | #include <ppc/savearea.h> | |
43 | ||
44 | #define FM_ARG0 0x38ULL // offset from r1 to first argument | |
45 | #define SPILLED_WORD_COUNT 7 // number of 32-bit words spilled to the stack | |
46 | ||
47 | extern struct savearea * find_user_regs( thread_t act); | |
48 | #endif | |
6601e61a A |
49 | |
50 | #pragma mark **** kern debug **** | |
b0d623f7 A |
51 | 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); |
52 | static void chud_null_kdebug(uint32_t debugid, uintptr_t arg0, uintptr_t arg1, uintptr_t arg2, uintptr_t arg3, uintptr_t arg4); | |
53 | static chudxnu_kdebug_callback_func_t kdebug_callback_fn = chud_null_kdebug; | |
6601e61a | 54 | |
0c530ab8 A |
55 | kern_return_t chudxnu_kdebug_callback_enter(chudxnu_kdebug_callback_func_t); |
56 | kern_return_t chudxnu_kdebug_callback_cancel(void); | |
57 | ||
58 | extern void kdbg_control_chud(int val, void *fn); | |
b0d623f7 A |
59 | |
60 | static void chud_null_kdebug(uint32_t debugid __unused, uintptr_t arg0 __unused, | |
61 | uintptr_t arg1 __unused, uintptr_t arg2 __unused, uintptr_t arg3 __unused, | |
62 | uintptr_t arg4 __unused) { | |
63 | return; | |
64 | } | |
6601e61a | 65 | |
0c530ab8 A |
66 | static void |
67 | chudxnu_private_kdebug_callback( | |
b0d623f7 A |
68 | uint32_t debugid, |
69 | uintptr_t arg0, | |
70 | uintptr_t arg1, | |
71 | uintptr_t arg2, | |
72 | uintptr_t arg3, | |
73 | uintptr_t arg4) | |
6601e61a | 74 | { |
0c530ab8 A |
75 | chudxnu_kdebug_callback_func_t fn = kdebug_callback_fn; |
76 | ||
77 | if(fn) { | |
78 | (fn)(debugid, arg0, arg1, arg2, arg3, arg4); | |
6601e61a A |
79 | } |
80 | } | |
81 | ||
0c530ab8 A |
82 | __private_extern__ kern_return_t |
83 | chudxnu_kdebug_callback_enter(chudxnu_kdebug_callback_func_t func) | |
6601e61a | 84 | { |
b0d623f7 A |
85 | /* Atomically set the callback. */ |
86 | if(OSCompareAndSwapPtr(chud_null_kdebug, func, | |
87 | (void * volatile *)&kdebug_callback_fn)) { | |
88 | ||
89 | kdbg_control_chud(TRUE, (void *)chudxnu_private_kdebug_callback); | |
90 | OSBitOrAtomic((UInt32)KDEBUG_ENABLE_CHUD, (volatile UInt32 *)&kdebug_enable); | |
91 | ||
92 | return KERN_SUCCESS; | |
93 | } | |
94 | return KERN_FAILURE; | |
6601e61a A |
95 | } |
96 | ||
0c530ab8 A |
97 | __private_extern__ kern_return_t |
98 | chudxnu_kdebug_callback_cancel(void) | |
6601e61a | 99 | { |
b0d623f7 A |
100 | OSBitAndAtomic((UInt32)~(KDEBUG_ENABLE_CHUD), (volatile UInt32 *)&kdebug_enable); |
101 | kdbg_control_chud(FALSE, NULL); | |
102 | ||
103 | chudxnu_kdebug_callback_func_t old = kdebug_callback_fn; | |
104 | ||
105 | while(!OSCompareAndSwapPtr(old, chud_null_kdebug, | |
106 | (void * volatile *)&kdebug_callback_fn)) { | |
107 | old = kdebug_callback_fn; | |
108 | } | |
6601e61a A |
109 | |
110 | return KERN_SUCCESS; | |
111 | } | |
0c530ab8 A |
112 | |
113 | #pragma mark **** CHUD syscall **** | |
b0d623f7 A |
114 | 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); |
115 | ||
116 | 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); | |
117 | static chudxnu_syscall_callback_func_t syscall_callback_fn = chud_null_syscall; | |
0c530ab8 A |
118 | |
119 | kern_return_t chudxnu_syscall_callback_enter(chudxnu_syscall_callback_func_t func); | |
120 | kern_return_t chudxnu_syscall_callback_cancel(void); | |
121 | ||
b0d623f7 A |
122 | static kern_return_t chud_null_syscall(uint64_t code __unused, |
123 | uint64_t arg0 __unused, uint64_t arg1 __unused, uint64_t arg2 __unused, | |
124 | uint64_t arg3 __unused, uint64_t arg4 __unused) { | |
125 | return (kern_return_t)EINVAL; | |
126 | } | |
127 | ||
128 | /* | |
129 | * chud | |
130 | * | |
131 | * Performs performance-related tasks. A private interface registers a handler for this | |
132 | * system call. The implementation is in the CHUDProf kernel extension. | |
133 | * | |
134 | * chud() is a callback style system call used by the CHUD Tools suite of performance tools. If the CHUD | |
135 | * kexts are not loaded, this system call will always return EINVAL. The CHUD kexts contain the | |
136 | * implementation of the system call. | |
137 | * | |
138 | * The current behavior of the chud() system call is as follows: | |
139 | * | |
140 | * Parameters: p (ignored) | |
141 | * uap User argument descriptor (see below) | |
142 | * retval return value of fn (the function returned by syscall_callback_fn) | |
143 | * | |
144 | * Indirect parameters: uap->code Selects the operation to do. This is broken down into a | |
145 | * 16-bit facility and a 16-bit action. | |
146 | * | |
147 | * The rest of the indirect parameters depend on the facility and the action that is selected: | |
148 | * | |
149 | * Facility: 1 Amber instruction tracer | |
150 | * Action: 1 Indicate that a new thread has been created. No arguments are used. | |
151 | * | |
152 | * Action: 2 Indicate that a thread is about to exit. No arguments are used. | |
153 | * | |
154 | * Facility: 2 Not Supported for this system call | |
155 | * | |
156 | * Facility: 3 CHUD Trace facility | |
157 | * Action: 1 Record a backtrace of the calling process into the CHUD Trace facility sample | |
158 | * buffer. | |
159 | * | |
160 | * uap->arg1 Number of frames to skip | |
161 | * uap->arg2 Pointer to a uint64_t containing a timestamp for the | |
162 | * beginning of the sample. NULL uses the current time. | |
163 | * uap->arg3 Pointer to a uint64_t containing a timestamp for the end | |
164 | * of the sample. NULL uses the current time. | |
165 | * uap->arg4 Pointer to auxiliary data to be recorded with the sample | |
166 | * uap->arg5 Size of the auxiliary data pointed to by arg4. | |
167 | * | |
168 | * Returns: EINVAL If syscall_callback_fn returns an invalid function | |
169 | * KERN_SUCCESS Success | |
170 | * KERN_FAILURE Generic failure | |
171 | * KERN_NO_SPACE Auxiliary data is too large (only used by Facility: 3) | |
172 | * | |
173 | * Implicit returns: retval return value of fn (the function returned by syscall_callback_fn) | |
174 | */ | |
2d21ac55 | 175 | int |
b0d623f7 | 176 | chud(__unused proc_t p, struct chud_args *uap, int32_t *retval) |
0c530ab8 | 177 | { |
0c530ab8 A |
178 | chudxnu_syscall_callback_func_t fn = syscall_callback_fn; |
179 | ||
180 | if(!fn) { | |
181 | return EINVAL; | |
182 | } | |
b0d623f7 A |
183 | |
184 | #ifdef __ppc__ | |
185 | // ppc32 user land spills 2.5 64-bit args (5 x 32-bit) to the stack | |
186 | // here we have to copy them out. r1 is the stack pointer in this world. | |
187 | // the offset is calculated according to the PPC32 ABI | |
188 | // Important: this only happens for 32-bit user threads | |
189 | ||
190 | if(!IS_64BIT_PROCESS(p)) { | |
191 | struct savearea *regs = find_user_regs(current_thread()); | |
192 | if(!regs) { | |
193 | return EINVAL; | |
194 | } | |
195 | ||
196 | // %r1 is the stack pointer on ppc32 | |
197 | uint32_t stackPointer = regs->save_r1; | |
198 | ||
199 | // calculate number of bytes spilled to the stack | |
200 | uint32_t spilledSize = sizeof(struct chud_args) - (sizeof(uint32_t) * SPILLED_WORD_COUNT); | |
201 | ||
202 | // obtain offset to arguments spilled onto user-thread stack | |
203 | user_addr_t incomingAddr = (user_addr_t)stackPointer + FM_ARG0; | |
204 | ||
205 | // destination is halfway through arg3 | |
206 | uint8_t *dstAddr = (uint8_t*)(&(uap->arg3)) + sizeof(uint32_t); | |
207 | ||
208 | copyin(incomingAddr, dstAddr, spilledSize); | |
209 | } | |
210 | #endif | |
0c530ab8 A |
211 | |
212 | *retval = fn(uap->code, uap->arg1, uap->arg2, uap->arg3, uap->arg4, uap->arg5); | |
213 | ||
214 | return 0; | |
215 | } | |
216 | ||
2d21ac55 A |
217 | __private_extern__ kern_return_t |
218 | chudxnu_syscall_callback_enter(chudxnu_syscall_callback_func_t func) | |
0c530ab8 | 219 | { |
b0d623f7 A |
220 | if(OSCompareAndSwapPtr(chud_null_syscall, func, |
221 | (void * volatile *)&syscall_callback_fn)) { | |
222 | return KERN_SUCCESS; | |
223 | } | |
224 | return KERN_FAILURE; | |
0c530ab8 A |
225 | } |
226 | ||
2d21ac55 A |
227 | __private_extern__ kern_return_t |
228 | chudxnu_syscall_callback_cancel(void) | |
0c530ab8 | 229 | { |
b0d623f7 A |
230 | chudxnu_syscall_callback_func_t old = syscall_callback_fn; |
231 | ||
232 | while(!OSCompareAndSwapPtr(old, chud_null_syscall, | |
233 | (void * volatile *)&syscall_callback_fn)) { | |
234 | old = syscall_callback_fn; | |
235 | } | |
236 | ||
0c530ab8 A |
237 | return KERN_SUCCESS; |
238 | } | |
b0d623f7 A |
239 | |
240 | /* DTrace callback */ | |
241 | typedef kern_return_t (*chudxnu_dtrace_callback_t)(uint64_t selector, | |
242 | uint64_t *args, uint32_t count); | |
243 | int chudxnu_dtrace_callback(uint64_t selector, uint64_t *args, uint32_t count); | |
244 | kern_return_t chudxnu_dtrace_callback_enter(chudxnu_dtrace_callback_t fn); | |
245 | void chudxnu_dtrace_callback_cancel(void); | |
246 | ||
247 | int | |
248 | chud_null_dtrace(uint64_t selector, uint64_t *args, uint32_t count); | |
249 | ||
250 | static chudxnu_dtrace_callback_t | |
251 | dtrace_callback = (chudxnu_dtrace_callback_t) chud_null_dtrace; | |
252 | ||
253 | int | |
254 | chud_null_dtrace(uint64_t selector __unused, uint64_t *args __unused, | |
255 | uint32_t count __unused) { | |
256 | return ENXIO; | |
257 | } | |
258 | ||
259 | int | |
260 | chudxnu_dtrace_callback(uint64_t selector, uint64_t *args, uint32_t count) | |
261 | { | |
262 | /* If no callback is hooked up, let's return ENXIO */ | |
263 | int ret = ENXIO; | |
264 | ||
265 | /* Make a local stack copy of the function ptr */ | |
266 | chudxnu_dtrace_callback_t fn = dtrace_callback; | |
267 | ||
268 | if(fn) { | |
269 | ret = fn(selector, args, count); | |
270 | } | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | __private_extern__ kern_return_t | |
276 | chudxnu_dtrace_callback_enter(chudxnu_dtrace_callback_t fn) | |
277 | { | |
278 | /* Atomically enter the call back */ | |
279 | if(!OSCompareAndSwapPtr(chud_null_dtrace, fn, | |
280 | (void * volatile *) &dtrace_callback)) { | |
281 | return KERN_FAILURE; | |
282 | } | |
283 | ||
284 | return KERN_SUCCESS; | |
285 | } | |
286 | ||
287 | __private_extern__ void | |
288 | chudxnu_dtrace_callback_cancel(void) | |
289 | { | |
290 | chudxnu_dtrace_callback_t old_fn = dtrace_callback; | |
291 | ||
292 | /* Atomically clear the call back */ | |
293 | while(!OSCompareAndSwapPtr(old_fn, chud_null_dtrace, | |
294 | (void * volatile *) &dtrace_callback)) { | |
295 | old_fn = dtrace_callback; | |
296 | } | |
297 | } | |
298 |