]>
Commit | Line | Data |
---|---|---|
55e303ae | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2003-2007 Apple Inc. All rights reserved. |
55e303ae | 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 | |
8f6c56a5 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@ | |
55e303ae A |
27 | */ |
28 | ||
91447636 A |
29 | #include <mach/mach_types.h> |
30 | #include <mach/task.h> | |
31 | #include <mach/thread_act.h> | |
32 | ||
33 | #include <kern/kern_types.h> | |
55e303ae A |
34 | #include <kern/processor.h> |
35 | #include <kern/thread.h> | |
55e303ae | 36 | #include <kern/ipc_tt.h> |
91447636 A |
37 | |
38 | #include <vm/vm_map.h> | |
39 | #include <vm/pmap.h> | |
40 | ||
0c530ab8 A |
41 | #include <chud/chud_xnu.h> |
42 | #include <chud/chud_xnu_private.h> | |
91447636 A |
43 | |
44 | #include <ppc/misc_protos.h> | |
55e303ae A |
45 | #include <ppc/proc_reg.h> |
46 | #include <ppc/machine_routines.h> | |
91447636 A |
47 | #include <ppc/fpu_protos.h> |
48 | ||
91447636 | 49 | #pragma mark **** thread state **** |
55e303ae A |
50 | |
51 | __private_extern__ | |
52 | kern_return_t chudxnu_copy_savearea_to_threadstate(thread_flavor_t flavor, thread_state_t tstate, mach_msg_type_number_t *count, struct savearea *sv) | |
53 | { | |
54 | struct ppc_thread_state *ts; | |
55 | struct ppc_thread_state64 *xts; | |
56 | ||
57 | switch(flavor) { | |
58 | case PPC_THREAD_STATE: | |
59 | if(*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */ | |
60 | *count = 0; | |
61 | return KERN_INVALID_ARGUMENT; | |
62 | } | |
63 | ts = (struct ppc_thread_state *) tstate; | |
64 | if(sv) { | |
65 | ts->r0 = (unsigned int)sv->save_r0; | |
66 | ts->r1 = (unsigned int)sv->save_r1; | |
67 | ts->r2 = (unsigned int)sv->save_r2; | |
68 | ts->r3 = (unsigned int)sv->save_r3; | |
69 | ts->r4 = (unsigned int)sv->save_r4; | |
70 | ts->r5 = (unsigned int)sv->save_r5; | |
71 | ts->r6 = (unsigned int)sv->save_r6; | |
72 | ts->r7 = (unsigned int)sv->save_r7; | |
73 | ts->r8 = (unsigned int)sv->save_r8; | |
74 | ts->r9 = (unsigned int)sv->save_r9; | |
75 | ts->r10 = (unsigned int)sv->save_r10; | |
76 | ts->r11 = (unsigned int)sv->save_r11; | |
77 | ts->r12 = (unsigned int)sv->save_r12; | |
78 | ts->r13 = (unsigned int)sv->save_r13; | |
79 | ts->r14 = (unsigned int)sv->save_r14; | |
80 | ts->r15 = (unsigned int)sv->save_r15; | |
81 | ts->r16 = (unsigned int)sv->save_r16; | |
82 | ts->r17 = (unsigned int)sv->save_r17; | |
83 | ts->r18 = (unsigned int)sv->save_r18; | |
84 | ts->r19 = (unsigned int)sv->save_r19; | |
85 | ts->r20 = (unsigned int)sv->save_r20; | |
86 | ts->r21 = (unsigned int)sv->save_r21; | |
87 | ts->r22 = (unsigned int)sv->save_r22; | |
88 | ts->r23 = (unsigned int)sv->save_r23; | |
89 | ts->r24 = (unsigned int)sv->save_r24; | |
90 | ts->r25 = (unsigned int)sv->save_r25; | |
91 | ts->r26 = (unsigned int)sv->save_r26; | |
92 | ts->r27 = (unsigned int)sv->save_r27; | |
93 | ts->r28 = (unsigned int)sv->save_r28; | |
94 | ts->r29 = (unsigned int)sv->save_r29; | |
95 | ts->r30 = (unsigned int)sv->save_r30; | |
96 | ts->r31 = (unsigned int)sv->save_r31; | |
97 | ts->cr = (unsigned int)sv->save_cr; | |
98 | ts->xer = (unsigned int)sv->save_xer; | |
99 | ts->lr = (unsigned int)sv->save_lr; | |
100 | ts->ctr = (unsigned int)sv->save_ctr; | |
101 | ts->srr0 = (unsigned int)sv->save_srr0; | |
102 | ts->srr1 = (unsigned int)sv->save_srr1; | |
103 | ts->mq = 0; | |
104 | ts->vrsave = (unsigned int)sv->save_vrsave; | |
105 | } else { | |
106 | bzero((void *)ts, sizeof(struct ppc_thread_state)); | |
107 | } | |
108 | *count = PPC_THREAD_STATE_COUNT; /* Pass back the amount we actually copied */ | |
109 | return KERN_SUCCESS; | |
110 | break; | |
111 | case PPC_THREAD_STATE64: | |
112 | if(*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */ | |
113 | return KERN_INVALID_ARGUMENT; | |
114 | } | |
115 | xts = (struct ppc_thread_state64 *) tstate; | |
116 | if(sv) { | |
117 | xts->r0 = sv->save_r0; | |
118 | xts->r1 = sv->save_r1; | |
119 | xts->r2 = sv->save_r2; | |
120 | xts->r3 = sv->save_r3; | |
121 | xts->r4 = sv->save_r4; | |
122 | xts->r5 = sv->save_r5; | |
123 | xts->r6 = sv->save_r6; | |
124 | xts->r7 = sv->save_r7; | |
125 | xts->r8 = sv->save_r8; | |
126 | xts->r9 = sv->save_r9; | |
127 | xts->r10 = sv->save_r10; | |
128 | xts->r11 = sv->save_r11; | |
129 | xts->r12 = sv->save_r12; | |
130 | xts->r13 = sv->save_r13; | |
131 | xts->r14 = sv->save_r14; | |
132 | xts->r15 = sv->save_r15; | |
133 | xts->r16 = sv->save_r16; | |
134 | xts->r17 = sv->save_r17; | |
135 | xts->r18 = sv->save_r18; | |
136 | xts->r19 = sv->save_r19; | |
137 | xts->r20 = sv->save_r20; | |
138 | xts->r21 = sv->save_r21; | |
139 | xts->r22 = sv->save_r22; | |
140 | xts->r23 = sv->save_r23; | |
141 | xts->r24 = sv->save_r24; | |
142 | xts->r25 = sv->save_r25; | |
143 | xts->r26 = sv->save_r26; | |
144 | xts->r27 = sv->save_r27; | |
145 | xts->r28 = sv->save_r28; | |
146 | xts->r29 = sv->save_r29; | |
147 | xts->r30 = sv->save_r30; | |
148 | xts->r31 = sv->save_r31; | |
149 | xts->cr = sv->save_cr; | |
150 | xts->xer = sv->save_xer; | |
151 | xts->lr = sv->save_lr; | |
152 | xts->ctr = sv->save_ctr; | |
153 | xts->srr0 = sv->save_srr0; | |
154 | xts->srr1 = sv->save_srr1; | |
155 | xts->vrsave = sv->save_vrsave; | |
156 | } else { | |
157 | bzero((void *)xts, sizeof(struct ppc_thread_state64)); | |
158 | } | |
159 | *count = PPC_THREAD_STATE64_COUNT; /* Pass back the amount we actually copied */ | |
160 | return KERN_SUCCESS; | |
161 | break; | |
162 | default: | |
163 | *count = 0; | |
164 | return KERN_INVALID_ARGUMENT; | |
165 | break; | |
166 | } | |
167 | } | |
168 | ||
169 | __private_extern__ | |
170 | kern_return_t chudxnu_copy_threadstate_to_savearea(struct savearea *sv, thread_flavor_t flavor, thread_state_t tstate, mach_msg_type_number_t *count) | |
171 | { | |
172 | struct ppc_thread_state *ts; | |
173 | struct ppc_thread_state64 *xts; | |
174 | ||
175 | switch(flavor) { | |
176 | case PPC_THREAD_STATE: | |
177 | if(*count < PPC_THREAD_STATE_COUNT) { /* Is the count ok? */ | |
178 | return KERN_INVALID_ARGUMENT; | |
179 | } | |
180 | ts = (struct ppc_thread_state *) tstate; | |
181 | if(sv) { | |
182 | sv->save_r0 = (uint64_t)ts->r0; | |
183 | sv->save_r1 = (uint64_t)ts->r1; | |
184 | sv->save_r2 = (uint64_t)ts->r2; | |
185 | sv->save_r3 = (uint64_t)ts->r3; | |
186 | sv->save_r4 = (uint64_t)ts->r4; | |
187 | sv->save_r5 = (uint64_t)ts->r5; | |
188 | sv->save_r6 = (uint64_t)ts->r6; | |
189 | sv->save_r7 = (uint64_t)ts->r7; | |
190 | sv->save_r8 = (uint64_t)ts->r8; | |
191 | sv->save_r9 = (uint64_t)ts->r9; | |
192 | sv->save_r10 = (uint64_t)ts->r10; | |
193 | sv->save_r11 = (uint64_t)ts->r11; | |
194 | sv->save_r12 = (uint64_t)ts->r12; | |
195 | sv->save_r13 = (uint64_t)ts->r13; | |
196 | sv->save_r14 = (uint64_t)ts->r14; | |
197 | sv->save_r15 = (uint64_t)ts->r15; | |
198 | sv->save_r16 = (uint64_t)ts->r16; | |
199 | sv->save_r17 = (uint64_t)ts->r17; | |
200 | sv->save_r18 = (uint64_t)ts->r18; | |
201 | sv->save_r19 = (uint64_t)ts->r19; | |
202 | sv->save_r20 = (uint64_t)ts->r20; | |
203 | sv->save_r21 = (uint64_t)ts->r21; | |
204 | sv->save_r22 = (uint64_t)ts->r22; | |
205 | sv->save_r23 = (uint64_t)ts->r23; | |
206 | sv->save_r24 = (uint64_t)ts->r24; | |
207 | sv->save_r25 = (uint64_t)ts->r25; | |
208 | sv->save_r26 = (uint64_t)ts->r26; | |
209 | sv->save_r27 = (uint64_t)ts->r27; | |
210 | sv->save_r28 = (uint64_t)ts->r28; | |
211 | sv->save_r29 = (uint64_t)ts->r29; | |
212 | sv->save_r30 = (uint64_t)ts->r30; | |
213 | sv->save_r31 = (uint64_t)ts->r31; | |
214 | sv->save_cr = ts->cr; | |
215 | sv->save_xer = (uint64_t)ts->xer; | |
216 | sv->save_lr = (uint64_t)ts->lr; | |
217 | sv->save_ctr = (uint64_t)ts->ctr; | |
218 | sv->save_srr0 = (uint64_t)ts->srr0; | |
219 | sv->save_srr1 = (uint64_t)ts->srr1; | |
220 | sv->save_vrsave = ts->vrsave; | |
221 | return KERN_SUCCESS; | |
55e303ae A |
222 | } |
223 | break; | |
224 | case PPC_THREAD_STATE64: | |
225 | if(*count < PPC_THREAD_STATE64_COUNT) { /* Is the count ok? */ | |
226 | return KERN_INVALID_ARGUMENT; | |
227 | } | |
228 | xts = (struct ppc_thread_state64 *) tstate; | |
229 | if(sv) { | |
230 | sv->save_r0 = xts->r0; | |
231 | sv->save_r1 = xts->r1; | |
232 | sv->save_r2 = xts->r2; | |
233 | sv->save_r3 = xts->r3; | |
234 | sv->save_r4 = xts->r4; | |
235 | sv->save_r5 = xts->r5; | |
236 | sv->save_r6 = xts->r6; | |
237 | sv->save_r7 = xts->r7; | |
238 | sv->save_r8 = xts->r8; | |
239 | sv->save_r9 = xts->r9; | |
240 | sv->save_r10 = xts->r10; | |
241 | sv->save_r11 = xts->r11; | |
242 | sv->save_r12 = xts->r12; | |
243 | sv->save_r13 = xts->r13; | |
244 | sv->save_r14 = xts->r14; | |
245 | sv->save_r15 = xts->r15; | |
246 | sv->save_r16 = xts->r16; | |
247 | sv->save_r17 = xts->r17; | |
248 | sv->save_r18 = xts->r18; | |
249 | sv->save_r19 = xts->r19; | |
250 | sv->save_r20 = xts->r20; | |
251 | sv->save_r21 = xts->r21; | |
252 | sv->save_r22 = xts->r22; | |
253 | sv->save_r23 = xts->r23; | |
254 | sv->save_r24 = xts->r24; | |
255 | sv->save_r25 = xts->r25; | |
256 | sv->save_r26 = xts->r26; | |
257 | sv->save_r27 = xts->r27; | |
258 | sv->save_r28 = xts->r28; | |
259 | sv->save_r29 = xts->r29; | |
260 | sv->save_r30 = xts->r30; | |
261 | sv->save_r31 = xts->r31; | |
262 | sv->save_cr = xts->cr; | |
263 | sv->save_xer = xts->xer; | |
264 | sv->save_lr = xts->lr; | |
265 | sv->save_ctr = xts->ctr; | |
266 | sv->save_srr0 = xts->srr0; | |
267 | sv->save_srr1 = xts->srr1; | |
268 | sv->save_vrsave = xts->vrsave; | |
269 | return KERN_SUCCESS; | |
55e303ae A |
270 | } |
271 | } | |
91447636 | 272 | return KERN_FAILURE; |
55e303ae A |
273 | } |
274 | ||
275 | __private_extern__ | |
91447636 A |
276 | kern_return_t chudxnu_thread_user_state_available(thread_t thread) |
277 | { | |
278 | if(find_user_regs(thread)) { | |
279 | return KERN_SUCCESS; | |
280 | } else { | |
281 | return KERN_FAILURE; | |
282 | } | |
283 | } | |
284 | ||
285 | __private_extern__ | |
286 | kern_return_t chudxnu_thread_get_state(thread_t thread, | |
287 | thread_flavor_t flavor, | |
55e303ae A |
288 | thread_state_t tstate, |
289 | mach_msg_type_number_t *count, | |
290 | boolean_t user_only) | |
291 | { | |
91447636 A |
292 | if(flavor==PPC_THREAD_STATE || flavor==PPC_THREAD_STATE64) { // machine_thread_get_state filters out some bits |
293 | struct savearea *sv; | |
294 | if(user_only) { | |
295 | sv = find_user_regs(thread); | |
55e303ae | 296 | } else { |
91447636 | 297 | sv = find_kern_regs(thread); |
55e303ae | 298 | } |
91447636 A |
299 | return chudxnu_copy_savearea_to_threadstate(flavor, tstate, count, sv); |
300 | } else { | |
301 | if(user_only) { | |
302 | return machine_thread_get_state(thread, flavor, tstate, count); | |
303 | } else { | |
304 | // doesn't do FP or VMX | |
305 | return machine_thread_get_kern_state(thread, flavor, tstate, count); | |
306 | } | |
307 | } | |
55e303ae A |
308 | } |
309 | ||
310 | __private_extern__ | |
91447636 A |
311 | kern_return_t chudxnu_thread_set_state(thread_t thread, |
312 | thread_flavor_t flavor, | |
313 | thread_state_t tstate, | |
314 | mach_msg_type_number_t count, | |
315 | boolean_t user_only) | |
55e303ae | 316 | { |
91447636 A |
317 | if(flavor==PPC_THREAD_STATE || flavor==PPC_THREAD_STATE64) { // machine_thread_set_state filters out some bits |
318 | struct savearea *sv; | |
319 | if(user_only) { | |
320 | sv = find_user_regs(thread); | |
55e303ae | 321 | } else { |
91447636 | 322 | sv = find_kern_regs(thread); |
55e303ae | 323 | } |
91447636 A |
324 | return chudxnu_copy_threadstate_to_savearea(sv, flavor, tstate, &count); |
325 | } else { | |
326 | return machine_thread_set_state(thread, flavor, tstate, count); // always user | |
327 | } | |
55e303ae A |
328 | } |
329 | ||
91447636 | 330 | #pragma mark **** task memory read/write **** |
55e303ae | 331 | |
91447636 A |
332 | __private_extern__ |
333 | kern_return_t chudxnu_task_read(task_t task, void *kernaddr, uint64_t usraddr, vm_size_t size) | |
334 | { | |
335 | kern_return_t ret = KERN_SUCCESS; | |
2d21ac55 A |
336 | |
337 | if(ml_at_interrupt_context()) { | |
338 | // can't do this on an interrupt stack | |
339 | return KERN_FAILURE; | |
340 | } | |
55e303ae | 341 | |
91447636 A |
342 | if(!chudxnu_is_64bit_task(task)) { // clear any cruft out of upper 32-bits for 32-bit tasks |
343 | usraddr &= 0x00000000FFFFFFFFULL; | |
344 | } | |
55e303ae | 345 | |
91447636 A |
346 | if(current_task()==task) { |
347 | thread_t cur_thr = current_thread(); | |
348 | vm_offset_t recover_handler = cur_thr->recover; | |
349 | ||
91447636 A |
350 | if(copyin(usraddr, kernaddr, size)) { |
351 | ret = KERN_FAILURE; | |
352 | } | |
2d21ac55 | 353 | |
91447636 A |
354 | cur_thr->recover = recover_handler; |
355 | } else { | |
2d21ac55 | 356 | |
91447636 A |
357 | vm_map_t map = get_task_map(task); |
358 | ret = vm_map_read_user(map, usraddr, kernaddr, size); | |
359 | } | |
360 | ||
361 | return ret; | |
362 | } | |
55e303ae | 363 | |
91447636 A |
364 | __private_extern__ |
365 | kern_return_t chudxnu_task_write(task_t task, uint64_t useraddr, void *kernaddr, vm_size_t size) | |
366 | { | |
367 | kern_return_t ret = KERN_SUCCESS; | |
2d21ac55 A |
368 | |
369 | if(ml_at_interrupt_context()) { | |
370 | // can't do this on an interrupt stack | |
371 | return KERN_FAILURE; | |
372 | } | |
373 | ||
91447636 A |
374 | if(!chudxnu_is_64bit_task(task)) { // clear any cruft out of upper 32-bits for 32-bit tasks |
375 | useraddr &= 0x00000000FFFFFFFFULL; | |
376 | } | |
377 | ||
378 | if(current_task()==task) { | |
379 | thread_t cur_thr = current_thread(); | |
380 | vm_offset_t recover_handler = cur_thr->recover; | |
381 | ||
91447636 A |
382 | if(copyout(kernaddr, useraddr, size)) { |
383 | ret = KERN_FAILURE; | |
384 | } | |
385 | cur_thr->recover = recover_handler; | |
55e303ae | 386 | } else { |
2d21ac55 | 387 | |
91447636 A |
388 | vm_map_t map = get_task_map(task); |
389 | ret = vm_map_write_user(map, kernaddr, useraddr, size); | |
390 | } | |
391 | ||
392 | return ret; | |
393 | } | |
394 | ||
395 | __private_extern__ | |
396 | kern_return_t chudxnu_kern_read(void *dstaddr, vm_offset_t srcaddr, vm_size_t size) | |
397 | { | |
2d21ac55 A |
398 | return (ml_nofault_copy(srcaddr, (vm_offset_t) dstaddr, size) == size ? |
399 | KERN_SUCCESS: KERN_FAILURE); | |
91447636 A |
400 | } |
401 | ||
402 | __private_extern__ | |
2d21ac55 | 403 | kern_return_t chudxnu_kern_write(vm_offset_t dstaddr, void *srcaddr, vm_size_t size) |
91447636 | 404 | { |
2d21ac55 A |
405 | return (ml_nofault_copy((vm_offset_t) srcaddr, dstaddr, size) == size ? |
406 | KERN_SUCCESS: KERN_FAILURE); | |
55e303ae A |
407 | } |
408 | ||
91447636 | 409 | // chudxnu_thread_get_callstack gathers a raw callstack along with any information needed to |
55e303ae A |
410 | // fix it up later (in case we stopped program as it was saving values into prev stack frame, etc.) |
411 | // after sampling has finished. | |
412 | // | |
413 | // For an N-entry callstack: | |
414 | // | |
415 | // [0] current pc | |
416 | // [1..N-3] stack frames (including current one) | |
417 | // [N-2] current LR (return value if we're in a leaf function) | |
418 | // [N-1] current r0 (in case we've saved LR in r0) | |
419 | // | |
420 | ||
421 | #define FP_LINK_OFFSET 2 | |
422 | #define STACK_ALIGNMENT_MASK 0xF // PPC stack frames are supposed to be 16-byte aligned | |
423 | #define INST_ALIGNMENT_MASK 0x3 // Instructions are always 4-bytes wide | |
424 | ||
425 | #ifndef USER_MODE | |
426 | #define USER_MODE(msr) ((msr) & MASK(MSR_PR) ? TRUE : FALSE) | |
427 | #endif | |
428 | ||
429 | #ifndef SUPERVISOR_MODE | |
430 | #define SUPERVISOR_MODE(msr) ((msr) & MASK(MSR_PR) ? FALSE : TRUE) | |
431 | #endif | |
432 | ||
0c530ab8 A |
433 | #define VALID_STACK_ADDRESS(addr) (addr>=0x1000ULL && \ |
434 | (addr&STACK_ALIGNMENT_MASK)==0x0 && \ | |
435 | (supervisor ? \ | |
436 | (addr>=kernStackMin && \ | |
437 | addr<=kernStackMax) : \ | |
438 | TRUE)) | |
91447636 | 439 | |
55e303ae A |
440 | |
441 | __private_extern__ | |
91447636 A |
442 | kern_return_t chudxnu_thread_get_callstack64( thread_t thread, |
443 | uint64_t *callStack, | |
444 | mach_msg_type_number_t *count, | |
445 | boolean_t user_only) | |
55e303ae A |
446 | { |
447 | kern_return_t kr; | |
91447636 A |
448 | task_t task = get_threadtask(thread); |
449 | uint64_t nextFramePointer = 0; | |
450 | uint64_t currPC, currLR, currR0; | |
451 | uint64_t framePointer; | |
452 | uint64_t prevPC = 0; | |
2d21ac55 A |
453 | uint64_t kernStackMin = thread->kernel_stack; |
454 | uint64_t kernStackMax = kernStackMin + KERNEL_STACK_SIZE; | |
91447636 A |
455 | uint64_t *buffer = callStack; |
456 | uint32_t tmpWord; | |
55e303ae A |
457 | int bufferIndex = 0; |
458 | int bufferMaxIndex = *count; | |
459 | boolean_t supervisor; | |
91447636 | 460 | boolean_t is64Bit; |
55e303ae A |
461 | struct savearea *sv; |
462 | ||
463 | if(user_only) { | |
91447636 | 464 | sv = find_user_regs(thread); |
55e303ae | 465 | } else { |
91447636 | 466 | sv = find_kern_regs(thread); |
55e303ae A |
467 | } |
468 | ||
469 | if(!sv) { | |
470 | *count = 0; | |
471 | return KERN_FAILURE; | |
472 | } | |
473 | ||
474 | supervisor = SUPERVISOR_MODE(sv->save_srr1); | |
91447636 | 475 | if(supervisor) { |
2d21ac55 | 476 | is64Bit = FALSE; /* XXX assuming task is always 32-bit */ |
91447636 A |
477 | } else { |
478 | is64Bit = chudxnu_is_64bit_task(task); | |
479 | } | |
55e303ae | 480 | |
91447636 A |
481 | bufferMaxIndex = bufferMaxIndex - 2; // allot space for saving the LR and R0 on the stack at the end. |
482 | if(bufferMaxIndex<2) { | |
483 | *count = 0; | |
484 | return KERN_RESOURCE_SHORTAGE; | |
485 | } | |
486 | ||
487 | currPC = sv->save_srr0; | |
488 | framePointer = sv->save_r1; /* r1 is the stack pointer (no FP on PPC) */ | |
489 | currLR = sv->save_lr; | |
490 | currR0 = sv->save_r0; | |
491 | ||
492 | bufferIndex = 0; // start with a stack of size zero | |
493 | buffer[bufferIndex++] = currPC; // save PC in position 0. | |
494 | ||
495 | // Now, fill buffer with stack backtraces. | |
496 | while(bufferIndex<bufferMaxIndex && VALID_STACK_ADDRESS(framePointer)) { | |
497 | uint64_t pc = 0; | |
498 | // Above the stack pointer, the following values are saved: | |
499 | // saved LR | |
500 | // saved CR | |
501 | // saved SP | |
502 | //-> SP | |
503 | // Here, we'll get the lr from the stack. | |
504 | uint64_t fp_link; | |
505 | ||
506 | if(is64Bit) { | |
507 | fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint64_t); | |
508 | } else { | |
509 | fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint32_t); | |
510 | } | |
511 | ||
512 | // Note that we read the pc even for the first stack frame (which, in theory, | |
513 | // is always empty because the callee fills it in just before it lowers the | |
514 | // stack. However, if we catch the program in between filling in the return | |
515 | // address and lowering the stack, we want to still have a valid backtrace. | |
516 | // FixupStack correctly disregards this value if necessary. | |
517 | ||
518 | if(supervisor) { | |
519 | if(is64Bit) { | |
520 | kr = chudxnu_kern_read(&pc, fp_link, sizeof(uint64_t)); | |
521 | } else { | |
522 | kr = chudxnu_kern_read(&tmpWord, fp_link, sizeof(uint32_t)); | |
523 | pc = tmpWord; | |
524 | } | |
525 | } else { | |
526 | if(is64Bit) { | |
527 | kr = chudxnu_task_read(task, &pc, fp_link, sizeof(uint64_t)); | |
528 | } else { | |
529 | kr = chudxnu_task_read(task, &tmpWord, fp_link, sizeof(uint32_t)); | |
530 | pc = tmpWord; | |
531 | } | |
532 | } | |
533 | if(kr!=KERN_SUCCESS) { | |
534 | pc = 0; | |
535 | break; | |
536 | } | |
537 | ||
538 | // retrieve the contents of the frame pointer and advance to the next stack frame if it's valid | |
539 | if(supervisor) { | |
540 | if(is64Bit) { | |
541 | kr = chudxnu_kern_read(&nextFramePointer, framePointer, sizeof(uint64_t)); | |
542 | } else { | |
543 | kr = chudxnu_kern_read(&tmpWord, framePointer, sizeof(uint32_t)); | |
544 | nextFramePointer = tmpWord; | |
545 | } | |
546 | } else { | |
547 | if(is64Bit) { | |
548 | kr = chudxnu_task_read(task, &nextFramePointer, framePointer, sizeof(uint64_t)); | |
549 | } else { | |
550 | kr = chudxnu_task_read(task, &tmpWord, framePointer, sizeof(uint32_t)); | |
551 | nextFramePointer = tmpWord; | |
552 | } | |
553 | } | |
554 | if(kr!=KERN_SUCCESS) { | |
555 | nextFramePointer = 0; | |
556 | } | |
557 | ||
558 | if(nextFramePointer) { | |
559 | buffer[bufferIndex++] = pc; | |
560 | prevPC = pc; | |
561 | } | |
562 | ||
563 | if(nextFramePointer<framePointer) { | |
564 | break; | |
565 | } else { | |
566 | framePointer = nextFramePointer; | |
567 | } | |
568 | } | |
569 | ||
570 | if(bufferIndex>=bufferMaxIndex) { | |
571 | *count = 0; | |
572 | return KERN_RESOURCE_SHORTAGE; | |
573 | } | |
574 | ||
575 | // Save link register and R0 at bottom of stack (used for later fixup). | |
576 | buffer[bufferIndex++] = currLR; | |
577 | buffer[bufferIndex++] = currR0; | |
578 | ||
579 | *count = bufferIndex; | |
580 | return KERN_SUCCESS; | |
581 | } | |
582 | ||
2d21ac55 A |
583 | #pragma mark **** DEPRECATED **** |
584 | ||
585 | // DEPRECATED | |
91447636 A |
586 | __private_extern__ |
587 | kern_return_t chudxnu_thread_get_callstack( thread_t thread, | |
588 | uint32_t *callStack, | |
589 | mach_msg_type_number_t *count, | |
590 | boolean_t user_only) | |
591 | { | |
592 | kern_return_t kr; | |
593 | task_t task = get_threadtask(thread); | |
594 | uint64_t nextFramePointer = 0; | |
595 | uint64_t currPC, currLR, currR0; | |
596 | uint64_t framePointer; | |
597 | uint64_t prevPC = 0; | |
2d21ac55 A |
598 | uint64_t kernStackMin = thread->kernel_stack; |
599 | uint64_t kernStackMax = kernStackMin + KERNEL_STACK_SIZE; | |
91447636 A |
600 | uint32_t *buffer = callStack; |
601 | uint32_t tmpWord; | |
602 | int bufferIndex = 0; | |
603 | int bufferMaxIndex = *count; | |
604 | boolean_t supervisor; | |
605 | boolean_t is64Bit; | |
606 | struct savearea *sv; | |
607 | ||
608 | if(user_only) { | |
609 | sv = find_user_regs(thread); | |
610 | } else { | |
611 | sv = find_kern_regs(thread); | |
612 | } | |
613 | ||
614 | if(!sv) { | |
55e303ae A |
615 | *count = 0; |
616 | return KERN_FAILURE; | |
617 | } | |
618 | ||
91447636 A |
619 | supervisor = SUPERVISOR_MODE(sv->save_srr1); |
620 | if(supervisor) { | |
2d21ac55 | 621 | is64Bit = FALSE; /* XXX assuming kernel task is always 32-bit */ |
91447636 A |
622 | } else { |
623 | is64Bit = chudxnu_is_64bit_task(task); | |
624 | } | |
625 | ||
55e303ae A |
626 | bufferMaxIndex = bufferMaxIndex - 2; // allot space for saving the LR and R0 on the stack at the end. |
627 | if(bufferMaxIndex<2) { | |
628 | *count = 0; | |
629 | return KERN_RESOURCE_SHORTAGE; | |
630 | } | |
631 | ||
632 | currPC = sv->save_srr0; | |
633 | framePointer = sv->save_r1; /* r1 is the stack pointer (no FP on PPC) */ | |
634 | currLR = sv->save_lr; | |
635 | currR0 = sv->save_r0; | |
636 | ||
637 | bufferIndex = 0; // start with a stack of size zero | |
638 | buffer[bufferIndex++] = currPC; // save PC in position 0. | |
639 | ||
640 | // Now, fill buffer with stack backtraces. | |
641 | while(bufferIndex<bufferMaxIndex && VALID_STACK_ADDRESS(framePointer)) { | |
91447636 | 642 | uint64_t pc = 0; |
55e303ae A |
643 | // Above the stack pointer, the following values are saved: |
644 | // saved LR | |
645 | // saved CR | |
646 | // saved SP | |
647 | //-> SP | |
648 | // Here, we'll get the lr from the stack. | |
91447636 A |
649 | uint64_t fp_link; |
650 | ||
651 | if(is64Bit) { | |
652 | fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint64_t); | |
653 | } else { | |
654 | fp_link = framePointer + FP_LINK_OFFSET*sizeof(uint32_t); | |
655 | } | |
55e303ae A |
656 | |
657 | // Note that we read the pc even for the first stack frame (which, in theory, | |
658 | // is always empty because the callee fills it in just before it lowers the | |
659 | // stack. However, if we catch the program in between filling in the return | |
660 | // address and lowering the stack, we want to still have a valid backtrace. | |
661 | // FixupStack correctly disregards this value if necessary. | |
662 | ||
663 | if(supervisor) { | |
91447636 A |
664 | if(is64Bit) { |
665 | kr = chudxnu_kern_read(&pc, fp_link, sizeof(uint64_t)); | |
666 | } else { | |
667 | kr = chudxnu_kern_read(&tmpWord, fp_link, sizeof(uint32_t)); | |
668 | pc = tmpWord; | |
669 | } | |
55e303ae | 670 | } else { |
91447636 A |
671 | if(is64Bit) { |
672 | kr = chudxnu_task_read(task, &pc, fp_link, sizeof(uint64_t)); | |
673 | } else { | |
674 | kr = chudxnu_task_read(task, &tmpWord, fp_link, sizeof(uint32_t)); | |
675 | pc = tmpWord; | |
676 | } | |
55e303ae A |
677 | } |
678 | if(kr!=KERN_SUCCESS) { | |
55e303ae A |
679 | pc = 0; |
680 | break; | |
681 | } | |
682 | ||
683 | // retrieve the contents of the frame pointer and advance to the next stack frame if it's valid | |
55e303ae | 684 | if(supervisor) { |
91447636 A |
685 | if(is64Bit) { |
686 | kr = chudxnu_kern_read(&nextFramePointer, framePointer, sizeof(uint64_t)); | |
687 | } else { | |
688 | kr = chudxnu_kern_read(&tmpWord, framePointer, sizeof(uint32_t)); | |
689 | nextFramePointer = tmpWord; | |
690 | } | |
55e303ae | 691 | } else { |
91447636 A |
692 | if(is64Bit) { |
693 | kr = chudxnu_task_read(task, &nextFramePointer, framePointer, sizeof(uint64_t)); | |
694 | } else { | |
695 | kr = chudxnu_task_read(task, &tmpWord, framePointer, sizeof(uint32_t)); | |
696 | nextFramePointer = tmpWord; | |
697 | } | |
55e303ae A |
698 | } |
699 | if(kr!=KERN_SUCCESS) { | |
700 | nextFramePointer = 0; | |
701 | } | |
702 | ||
703 | if(nextFramePointer) { | |
704 | buffer[bufferIndex++] = pc; | |
705 | prevPC = pc; | |
706 | } | |
707 | ||
708 | if(nextFramePointer<framePointer) { | |
709 | break; | |
710 | } else { | |
91447636 A |
711 | framePointer = nextFramePointer; |
712 | } | |
55e303ae A |
713 | } |
714 | ||
715 | if(bufferIndex>=bufferMaxIndex) { | |
716 | *count = 0; | |
717 | return KERN_RESOURCE_SHORTAGE; | |
718 | } | |
719 | ||
91447636 | 720 | // Save link register and R0 at bottom of stack (used for later fixup). |
55e303ae A |
721 | buffer[bufferIndex++] = currLR; |
722 | buffer[bufferIndex++] = currR0; | |
723 | ||
724 | *count = bufferIndex; | |
725 | return KERN_SUCCESS; | |
726 | } | |
727 |