forked from apple/darwin-xnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallstack.c
439 lines (363 loc) · 10.4 KB
/
callstack.c
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
* Copyright (c) 2011 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@
*/
/* Collect kernel callstacks */
#include <chud/chud_xnu.h>
#include <mach/mach_types.h>
#include <kern/thread.h>
#include <kern/backtrace.h>
#include <vm/vm_map.h>
#include <kperf/buffer.h>
#include <kperf/context.h>
#include <kperf/callstack.h>
#include <kperf/ast.h>
#include <sys/errno.h>
#if defined(__arm__) || defined(__arm64__)
#include <arm/cpu_data.h>
#include <arm/cpu_data_internal.h>
#endif
static void
callstack_fixup_user(struct callstack *cs, thread_t thread)
{
uint64_t fixup_val = 0;
assert(cs->nframes < MAX_CALLSTACK_FRAMES);
#if defined(__x86_64__)
user_addr_t sp_user;
bool user_64;
x86_saved_state_t *state;
state = get_user_regs(thread);
if (!state) {
goto out;
}
user_64 = is_saved_state64(state);
if (user_64) {
sp_user = saved_state64(state)->isf.rsp;
} else {
sp_user = saved_state32(state)->uesp;
}
if (thread == current_thread()) {
(void)copyin(sp_user, (char *)&fixup_val,
user_64 ? sizeof(uint64_t) : sizeof(uint32_t));
} else {
(void)vm_map_read_user(get_task_map(get_threadtask(thread)), sp_user,
&fixup_val, user_64 ? sizeof(uint64_t) : sizeof(uint32_t));
}
#elif defined(__arm64__) || defined(__arm__)
struct arm_saved_state *state = get_user_regs(thread);
if (!state) {
goto out;
}
/* encode thumb mode into low bit of PC */
if (get_saved_state_cpsr(state) & PSR_TF) {
cs->frames[0] |= 1ULL;
}
fixup_val = get_saved_state_lr(state);
#else
#error "callstack_fixup_user: unsupported architecture"
#endif
out:
cs->frames[cs->nframes++] = fixup_val;
}
#if defined(__x86_64__)
__attribute__((used))
static kern_return_t
interrupted_kernel_sp_value(uintptr_t *sp_val)
{
x86_saved_state_t *state;
uintptr_t sp;
bool state_64;
uint64_t cs;
uintptr_t top, bottom;
state = current_cpu_datap()->cpu_int_state;
if (!state) {
return KERN_FAILURE;
}
state_64 = is_saved_state64(state);
if (state_64) {
cs = saved_state64(state)->isf.cs;
} else {
cs = saved_state32(state)->cs;
}
/* return early if interrupted a thread in user space */
if ((cs & SEL_PL) == SEL_PL_U) {
return KERN_FAILURE;
}
if (state_64) {
sp = saved_state64(state)->isf.rsp;
} else {
sp = saved_state32(state)->uesp;
}
/* make sure the stack pointer is pointing somewhere in this stack */
bottom = current_thread()->kernel_stack;
top = bottom + kernel_stack_size;
if (sp >= bottom && sp < top) {
return KERN_FAILURE;
}
*sp_val = *(uintptr_t *)sp;
return KERN_SUCCESS;
}
#elif defined(__arm64__)
__attribute__((used))
static kern_return_t
interrupted_kernel_lr(uintptr_t *lr)
{
struct arm_saved_state *state;
state = getCpuDatap()->cpu_int_state;
/* return early if interrupted a thread in user space */
if (PSR64_IS_USER(get_saved_state_cpsr(state))) {
return KERN_FAILURE;
}
*lr = get_saved_state_lr(state);
return KERN_SUCCESS;
}
#elif defined(__arm__)
__attribute__((used))
static kern_return_t
interrupted_kernel_lr(uintptr_t *lr)
{
struct arm_saved_state *state;
state = getCpuDatap()->cpu_int_state;
/* return early if interrupted a thread in user space */
if (PSR_IS_USER(get_saved_state_cpsr(state))) {
return KERN_FAILURE;
}
*lr = get_saved_state_lr(state);
return KERN_SUCCESS;
}
#else /* defined(__arm__) */
#error "interrupted_kernel_{sp,lr}: unsupported architecture"
#endif /* !defined(__arm__) */
static void
callstack_fixup_interrupted(struct callstack *cs)
{
uintptr_t fixup_val = 0;
assert(cs->nframes < MAX_CALLSTACK_FRAMES);
/*
* Only provide arbitrary data on development or debug kernels.
*/
#if DEVELOPMENT || DEBUG
#if defined(__x86_64__)
(void)interrupted_kernel_sp_value(&fixup_val);
#elif defined(__arm64__) || defined(__arm__)
(void)interrupted_kernel_lr(&fixup_val);
#endif /* defined(__x86_64__) */
#endif /* DEVELOPMENT || DEBUG */
assert(cs->flags & CALLSTACK_KERNEL);
cs->frames[cs->nframes++] = fixup_val;
}
void
kperf_continuation_sample(struct callstack *cs, struct kperf_context *context)
{
thread_t thread;
assert(cs != NULL);
assert(context != NULL);
thread = context->cur_thread;
assert(thread != NULL);
assert(thread->continuation != NULL);
cs->flags = CALLSTACK_CONTINUATION | CALLSTACK_VALID | CALLSTACK_KERNEL;
#ifdef __LP64__
cs->flags |= CALLSTACK_64BIT;
#endif
cs->nframes = 1;
cs->frames[0] = VM_KERNEL_UNSLIDE(thread->continuation);
}
void
kperf_backtrace_sample(struct callstack *cs, struct kperf_context *context)
{
assert(cs != NULL);
assert(context != NULL);
assert(context->cur_thread == current_thread());
cs->flags = CALLSTACK_KERNEL | CALLSTACK_KERNEL_WORDS;
#ifdef __LP64__
cs->flags |= CALLSTACK_64BIT;
#endif
BUF_VERB(PERF_CS_BACKTRACE | DBG_FUNC_START, 1);
cs->nframes = backtrace_frame((uintptr_t *)&(cs->frames), cs->nframes - 1,
context->starting_fp);
if (cs->nframes > 0) {
cs->flags |= CALLSTACK_VALID;
/*
* Fake the value pointed to by the stack pointer or the link
* register for symbolicators.
*/
cs->frames[cs->nframes + 1] = 0;
cs->nframes += 1;
}
BUF_VERB(PERF_CS_BACKTRACE | DBG_FUNC_END, cs->nframes);
}
void
kperf_kcallstack_sample(struct callstack *cs, struct kperf_context *context)
{
thread_t thread;
assert(cs != NULL);
assert(context != NULL);
assert(cs->nframes <= MAX_CALLSTACK_FRAMES);
thread = context->cur_thread;
assert(thread != NULL);
BUF_INFO(PERF_CS_KSAMPLE | DBG_FUNC_START, (uintptr_t)thread_tid(thread),
cs->nframes);
cs->flags = CALLSTACK_KERNEL;
#ifdef __LP64__
cs->flags |= CALLSTACK_64BIT;
#endif
if (ml_at_interrupt_context()) {
assert(thread == current_thread());
cs->flags |= CALLSTACK_KERNEL_WORDS;
cs->nframes = backtrace_interrupted((uintptr_t *)cs->frames,
cs->nframes - 1);
if (cs->nframes != 0) {
callstack_fixup_interrupted(cs);
}
} else {
/*
* Rely on legacy CHUD backtracer to backtrace kernel stacks on
* other threads.
*/
kern_return_t kr;
kr = chudxnu_thread_get_callstack64_kperf(thread, cs->frames,
&cs->nframes, FALSE);
if (kr == KERN_SUCCESS) {
cs->flags |= CALLSTACK_VALID;
} else if (kr == KERN_RESOURCE_SHORTAGE) {
cs->flags |= CALLSTACK_VALID;
cs->flags |= CALLSTACK_TRUNCATED;
} else {
cs->nframes = 0;
}
}
if (cs->nframes == 0) {
BUF_INFO(PERF_CS_ERROR, ERR_GETSTACK);
}
BUF_INFO(PERF_CS_KSAMPLE | DBG_FUNC_END, (uintptr_t)thread_tid(thread), cs->flags, cs->nframes);
}
void
kperf_ucallstack_sample(struct callstack *cs, struct kperf_context *context)
{
thread_t thread;
bool user_64 = false;
int err;
assert(cs != NULL);
assert(context != NULL);
assert(cs->nframes <= MAX_CALLSTACK_FRAMES);
assert(ml_get_interrupts_enabled() == TRUE);
thread = context->cur_thread;
assert(thread != NULL);
BUF_INFO(PERF_CS_USAMPLE | DBG_FUNC_START, (uintptr_t)thread_tid(thread),
cs->nframes);
cs->flags = 0;
err = backtrace_thread_user(thread, (uintptr_t *)cs->frames,
cs->nframes - 1, &cs->nframes, &user_64);
cs->flags |= CALLSTACK_KERNEL_WORDS;
if (user_64) {
cs->flags |= CALLSTACK_64BIT;
}
if (!err || err == EFAULT) {
callstack_fixup_user(cs, thread);
cs->flags |= CALLSTACK_VALID;
} else {
cs->nframes = 0;
BUF_INFO(PERF_CS_ERROR, ERR_GETSTACK, err);
}
BUF_INFO(PERF_CS_USAMPLE | DBG_FUNC_END, (uintptr_t)thread_tid(thread),
cs->flags, cs->nframes);
}
static inline uintptr_t
scrub_word(uintptr_t *bt, int n_frames, int frame, bool kern)
{
if (frame < n_frames) {
if (kern) {
return VM_KERNEL_UNSLIDE(bt[frame]);
} else {
return bt[frame];
}
} else {
return 0;
}
}
static inline uintptr_t
scrub_frame(uint64_t *bt, int n_frames, int frame)
{
if (frame < n_frames) {
return (uintptr_t)(bt[frame]);
} else {
return 0;
}
}
static void
callstack_log(struct callstack *cs, uint32_t hcode, uint32_t dcode)
{
BUF_VERB(PERF_CS_LOG | DBG_FUNC_START, cs->flags, cs->nframes);
/* framing information for the stack */
BUF_DATA(hcode, cs->flags, cs->nframes);
/* how many batches of 4 */
unsigned int nframes = cs->nframes;
unsigned int n = nframes / 4;
unsigned int ovf = nframes % 4;
if (ovf != 0) {
n++;
}
bool kern = cs->flags & CALLSTACK_KERNEL;
if (cs->flags & CALLSTACK_KERNEL_WORDS) {
uintptr_t *frames = (uintptr_t *)cs->frames;
for (unsigned int i = 0; i < n; i++) {
unsigned int j = i * 4;
BUF_DATA(dcode,
scrub_word(frames, nframes, j + 0, kern),
scrub_word(frames, nframes, j + 1, kern),
scrub_word(frames, nframes, j + 2, kern),
scrub_word(frames, nframes, j + 3, kern));
}
} else {
for (unsigned int i = 0; i < n; i++) {
uint64_t *frames = cs->frames;
unsigned int j = i * 4;
BUF_DATA(dcode,
scrub_frame(frames, nframes, j + 0),
scrub_frame(frames, nframes, j + 1),
scrub_frame(frames, nframes, j + 2),
scrub_frame(frames, nframes, j + 3));
}
}
BUF_VERB(PERF_CS_LOG | DBG_FUNC_END, cs->flags, cs->nframes);
}
void
kperf_kcallstack_log( struct callstack *cs )
{
callstack_log(cs, PERF_CS_KHDR, PERF_CS_KDATA);
}
void
kperf_ucallstack_log( struct callstack *cs )
{
callstack_log(cs, PERF_CS_UHDR, PERF_CS_UDATA);
}
int
kperf_ucallstack_pend(struct kperf_context * context, uint32_t depth)
{
int did_pend = kperf_ast_pend(context->cur_thread, T_KPERF_AST_CALLSTACK);
kperf_ast_set_callstack_depth(context->cur_thread, depth);
return did_pend;
}