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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
|
/*
* drivers/misc/tegra-profiler/backtrace.c
*
* Copyright (c) 2015, NVIDIA CORPORATION. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/uaccess.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/tegra_profiler.h>
#include "quadd.h"
#include "backtrace.h"
#include "eh_unwind.h"
#include "dwarf_unwind.h"
#include "hrt.h"
static inline int
is_thumb_mode(struct pt_regs *regs)
{
#ifdef CONFIG_ARM64
return compat_thumb_mode(regs);
#else
return thumb_mode(regs);
#endif
}
unsigned long
quadd_user_stack_pointer(struct pt_regs *regs)
{
#ifdef CONFIG_ARM64
if (compat_user_mode(regs))
return regs->compat_sp;
#endif
return user_stack_pointer(regs);
}
unsigned long
quadd_get_user_frame_pointer(struct pt_regs *regs)
{
unsigned long fp;
#ifdef CONFIG_ARM64
if (compat_user_mode(regs))
fp = is_thumb_mode(regs) ?
regs->compat_usr(7) : regs->compat_usr(11);
else
fp = regs->regs[29];
#else
fp = is_thumb_mode(regs) ? regs->ARM_r7 : regs->ARM_fp;
#endif
return fp;
}
unsigned long
quadd_user_link_register(struct pt_regs *regs)
{
#ifdef CONFIG_ARM64
return compat_user_mode(regs) ?
regs->compat_lr : regs->regs[30];
#else
return regs->ARM_lr;
#endif
}
static inline void
put_unw_type(u32 *p, int bt_idx, unsigned int type)
{
int word_idx, shift;
word_idx = bt_idx / 8;
shift = (bt_idx % 8) * 4;
*(p + word_idx) &= ~(0x0f << shift);
*(p + word_idx) |= (type & 0x0f) << shift;
}
int
quadd_callchain_store(struct quadd_callchain *cc,
unsigned long ip, unsigned int type)
{
unsigned long low_addr = cc->hrt->low_addr;
if (ip < low_addr || !validate_pc_addr(ip, sizeof(unsigned long))) {
cc->unw_rc = QUADD_URC_PC_INCORRECT;
return 0;
}
if (cc->nr >= QUADD_MAX_STACK_DEPTH) {
cc->unw_rc = QUADD_URC_LEVEL_TOO_DEEP;
return 0;
}
put_unw_type(cc->types, cc->nr, type);
if (cc->cs_64)
cc->ip_64[cc->nr++] = ip;
else
cc->ip_32[cc->nr++] = ip;
return 1;
}
static int
is_ex_entry_exist(struct pt_regs *regs,
unsigned long addr,
struct task_struct *task)
{
#ifdef CONFIG_ARM64
if (!compat_user_mode(regs))
return quadd_aarch64_is_ex_entry_exist(regs, addr, task);
#endif
return quadd_aarch32_is_ex_entry_exist(regs, addr, task);
}
static unsigned long __user *
user_backtrace(struct pt_regs *regs,
unsigned long __user *tail,
struct quadd_callchain *cc,
struct vm_area_struct *stack_vma,
struct task_struct *task)
{
int nr_added;
unsigned long value, value_lr = 0, value_fp = 0;
unsigned long __user *fp_prev = NULL;
if (!is_vma_addr((unsigned long)tail, stack_vma, sizeof(*tail)))
return NULL;
if (__copy_from_user_inatomic(&value, tail, sizeof(unsigned long))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
if (is_vma_addr(value, stack_vma, sizeof(value))) {
/* gcc thumb/clang frame */
value_fp = value;
if (!is_vma_addr((unsigned long)(tail + 1), stack_vma,
sizeof(*tail)))
return NULL;
if (__copy_from_user_inatomic(&value_lr, tail + 1,
sizeof(value_lr))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
cc->curr_fp = value_fp;
cc->curr_sp = (unsigned long)tail + sizeof(value_fp) * 2;
cc->curr_pc = value_lr;
} else {
/* gcc arm frame */
if (__copy_from_user_inatomic(&value_fp, tail - 1,
sizeof(value_fp))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
cc->curr_fp = value_fp;
cc->curr_sp = (unsigned long)tail + sizeof(value_fp);
cc->curr_pc = value_lr = value;
if (!is_vma_addr(value_fp, stack_vma, sizeof(value_fp)))
return NULL;
}
fp_prev = (unsigned long __user *)value_fp;
nr_added = quadd_callchain_store(cc, value_lr, QUADD_UNW_TYPE_FP);
if (nr_added == 0)
return NULL;
if (cc->unw_method == QUADD_UNW_METHOD_MIXED &&
is_ex_entry_exist(regs, value_lr, task))
return NULL;
if (fp_prev <= tail)
return NULL;
return fp_prev;
}
static unsigned int
get_user_callchain_fp(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
unsigned long fp, sp, pc, reg;
struct vm_area_struct *vma, *vma_pc = NULL;
unsigned long __user *tail = NULL;
struct mm_struct *mm = task->mm;
cc->nr = 0;
cc->unw_rc = QUADD_URC_FP_INCORRECT;
if (!regs || !mm) {
cc->unw_rc = QUADD_URC_FAILURE;
return 0;
}
sp = quadd_user_stack_pointer(regs);
pc = instruction_pointer(regs);
fp = quadd_get_user_frame_pointer(regs);
if (fp == 0 || fp < sp || fp & 0x3)
return 0;
vma = find_vma(mm, sp);
if (!vma) {
cc->unw_rc = QUADD_URC_SP_INCORRECT;
return 0;
}
if (!is_vma_addr(fp, vma, sizeof(fp)))
return 0;
if (probe_kernel_address(fp, reg)) {
pr_warn_once("%s: failed for address: %#lx\n", __func__, fp);
cc->unw_rc = QUADD_URC_EACCESS;
return 0;
}
pr_debug("sp/fp: %#lx/%#lx, pc/lr: %#lx/%#lx, *fp: %#lx, stack: %#lx-%#lx\n",
sp, fp, pc, quadd_user_link_register(regs), reg,
vma->vm_start, vma->vm_end);
if (is_thumb_mode(regs)) {
if (reg <= fp || !is_vma_addr(reg, vma, sizeof(reg)))
return 0;
} else if (reg > fp && is_vma_addr(reg, vma, sizeof(reg))) {
/* fp --> fp prev */
unsigned long value;
int read_lr = 0;
if (is_vma_addr(fp + sizeof(unsigned long), vma, sizeof(fp))) {
if (__copy_from_user_inatomic(
&value,
(unsigned long __user *)fp + 1,
sizeof(unsigned long))) {
cc->unw_rc = QUADD_URC_EACCESS;
return 0;
}
vma_pc = find_vma(mm, pc);
read_lr = 1;
}
if (!read_lr || !is_vma_addr(value, vma_pc, sizeof(value))) {
/* gcc: fp --> short frame tail (fp) */
int nr_added;
unsigned long lr = quadd_user_link_register(regs);
nr_added = quadd_callchain_store(cc, lr,
QUADD_UNW_TYPE_LR_FP);
if (nr_added == 0)
return cc->nr;
tail = (unsigned long __user *)reg;
}
}
if (!tail)
tail = (unsigned long __user *)fp;
while (tail && !((unsigned long)tail & 0x3))
tail = user_backtrace(regs, tail, cc, vma, task);
return cc->nr;
}
static unsigned int
__user_backtrace(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
struct mm_struct *mm = task->mm;
struct vm_area_struct *vma;
unsigned long __user *tail;
cc->unw_rc = QUADD_URC_FP_INCORRECT;
if (!mm) {
cc->unw_rc = QUADD_URC_FAILURE;
return cc->nr;
}
vma = find_vma(mm, cc->curr_sp);
if (!vma) {
cc->unw_rc = QUADD_URC_SP_INCORRECT;
return cc->nr;
}
tail = (unsigned long __user *)cc->curr_fp;
while (tail && !((unsigned long)tail & 0x3))
tail = user_backtrace(regs, tail, cc, vma, task);
return cc->nr;
}
#ifdef CONFIG_ARM64
static u32 __user *
user_backtrace_compat(struct pt_regs *regs,
u32 __user *tail,
struct quadd_callchain *cc,
struct vm_area_struct *stack_vma,
struct task_struct *task)
{
int nr_added;
u32 value, value_lr = 0, value_fp = 0;
u32 __user *fp_prev = NULL;
if (!is_vma_addr((unsigned long)tail, stack_vma, sizeof(*tail)))
return NULL;
if (__copy_from_user_inatomic(&value, tail, sizeof(value))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
if (is_vma_addr(value, stack_vma, sizeof(value))) {
/* gcc thumb/clang frame */
value_fp = value;
if (!is_vma_addr((unsigned long)(tail + 1), stack_vma,
sizeof(*tail)))
return NULL;
if (__copy_from_user_inatomic(&value_lr, tail + 1,
sizeof(value_lr))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
cc->curr_fp = value_fp;
cc->curr_sp = (unsigned long)tail + sizeof(value_fp) * 2;
cc->curr_pc = value_lr;
} else {
/* gcc arm frame */
if (__copy_from_user_inatomic(&value_fp, tail - 1,
sizeof(value_fp))) {
cc->unw_rc = QUADD_URC_EACCESS;
return NULL;
}
cc->curr_fp = value_fp;
cc->curr_sp = (unsigned long)tail + sizeof(value_fp);
cc->curr_pc = value_lr = value;
if (!is_vma_addr(value_fp, stack_vma, sizeof(value_fp)))
return NULL;
}
fp_prev = (u32 __user *)(unsigned long)value_fp;
nr_added = quadd_callchain_store(cc, value_lr, QUADD_UNW_TYPE_FP);
if (nr_added == 0)
return NULL;
if (cc->unw_method == QUADD_UNW_METHOD_MIXED &&
is_ex_entry_exist(regs, value_lr, task))
return NULL;
if (fp_prev <= tail)
return NULL;
return fp_prev;
}
static unsigned int
get_user_callchain_fp_compat(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
u32 fp, sp, pc, reg;
struct vm_area_struct *vma, *vma_pc = NULL;
u32 __user *tail = NULL;
struct mm_struct *mm = task->mm;
cc->nr = 0;
cc->unw_rc = QUADD_URC_FP_INCORRECT;
if (!regs || !mm) {
cc->unw_rc = QUADD_URC_FAILURE;
return 0;
}
sp = quadd_user_stack_pointer(regs);
pc = instruction_pointer(regs);
fp = quadd_get_user_frame_pointer(regs);
if (fp == 0 || fp < sp || fp & 0x3)
return 0;
vma = find_vma(mm, sp);
if (!vma) {
cc->unw_rc = QUADD_URC_SP_INCORRECT;
return 0;
}
if (!is_vma_addr(fp, vma, sizeof(fp)))
return 0;
if (probe_kernel_address((unsigned long)fp, reg)) {
pr_warn_once("%s: failed for address: %#x\n", __func__, fp);
cc->unw_rc = QUADD_URC_EACCESS;
return 0;
}
pr_debug("sp/fp: %#x/%#x, pc/lr: %#x/%#x, *fp: %#x, stack: %#lx-%#lx\n",
sp, fp, pc, (u32)quadd_user_link_register(regs), reg,
vma->vm_start, vma->vm_end);
if (is_thumb_mode(regs)) {
if (reg <= fp || !is_vma_addr(reg, vma, sizeof(reg)))
return 0;
} else if (reg > fp && is_vma_addr(reg, vma, sizeof(reg))) {
/* fp --> fp prev */
u32 value;
int read_lr = 0;
if (is_vma_addr(fp + sizeof(u32), vma, sizeof(fp))) {
if (__copy_from_user_inatomic(
&value,
(u32 __user *)(fp + sizeof(u32)),
sizeof(value))) {
cc->unw_rc = QUADD_URC_EACCESS;
return 0;
}
vma_pc = find_vma(mm, pc);
read_lr = 1;
}
if (!read_lr || !is_vma_addr(value, vma_pc, sizeof(value))) {
/* gcc: fp --> short frame tail (fp) */
int nr_added;
u32 lr = quadd_user_link_register(regs);
nr_added = quadd_callchain_store(cc, lr,
QUADD_UNW_TYPE_LR_FP);
if (nr_added == 0)
return cc->nr;
tail = (u32 __user *)(unsigned long)reg;
}
}
if (!tail)
tail = (u32 __user *)(unsigned long)fp;
while (tail && !((unsigned long)tail & 0x3))
tail = user_backtrace_compat(regs, tail, cc, vma, task);
return cc->nr;
}
static unsigned int
__user_backtrace_compat(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
struct mm_struct *mm = task->mm;
struct vm_area_struct *vma;
u32 __user *tail;
cc->unw_rc = QUADD_URC_FP_INCORRECT;
if (!mm) {
cc->unw_rc = QUADD_URC_FAILURE;
return cc->nr;
}
vma = find_vma(mm, cc->curr_sp);
if (!vma) {
cc->unw_rc = QUADD_URC_SP_INCORRECT;
return cc->nr;
}
tail = (u32 __user *)cc->curr_fp;
while (tail && !((unsigned long)tail & 0x3))
tail = user_backtrace_compat(regs, tail, cc, vma, task);
return cc->nr;
}
#endif /* CONFIG_ARM64 */
static unsigned int
__get_user_callchain_fp(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
if (cc->nr > 0) {
if (cc->unw_rc == QUADD_URC_LEVEL_TOO_DEEP)
return cc->nr;
#ifdef CONFIG_ARM64
if (compat_user_mode(regs))
__user_backtrace_compat(regs, cc, task);
else
__user_backtrace(regs, cc, task);
#else
__user_backtrace(regs, cc, task);
#endif
return cc->nr;
}
#ifdef CONFIG_ARM64
if (compat_user_mode(regs))
return get_user_callchain_fp_compat(regs, cc, task);
#endif
return get_user_callchain_fp(regs, cc, task);
}
static unsigned int
get_user_callchain_ut(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
#ifdef CONFIG_ARM64
if (!compat_user_mode(regs))
return quadd_aarch64_get_user_callchain_ut(regs, cc, task);
#endif
return quadd_aarch32_get_user_callchain_ut(regs, cc, task);
}
static unsigned int
get_user_callchain_mixed(struct pt_regs *regs,
struct quadd_callchain *cc,
struct task_struct *task)
{
int nr_prev;
do {
nr_prev = cc->nr;
get_user_callchain_ut(regs, cc, task);
if (nr_prev > 0 && cc->nr == nr_prev)
break;
nr_prev = cc->nr;
__get_user_callchain_fp(regs, cc, task);
} while (nr_prev != cc->nr);
return cc->nr;
}
unsigned int
quadd_get_user_callchain(struct pt_regs *regs,
struct quadd_callchain *cc,
struct quadd_ctx *ctx,
struct task_struct *task)
{
unsigned int method = cc->unw_method;
cc->nr = 0;
if (!regs) {
cc->unw_rc = QUADD_URC_FAILURE;
return 0;
}
cc->curr_sp = 0;
cc->curr_fp = 0;
cc->curr_pc = 0;
#ifdef CONFIG_ARM64
cc->cs_64 = compat_user_mode(regs) ? 0 : 1;
#else
cc->cs_64 = 0;
#endif
cc->unw_rc = 0;
switch (method) {
case QUADD_UNW_METHOD_FP:
__get_user_callchain_fp(regs, cc, task);
break;
case QUADD_UNW_METHOD_EHT:
get_user_callchain_ut(regs, cc, task);
break;
case QUADD_UNW_METHOD_MIXED:
get_user_callchain_mixed(regs, cc, task);
break;
case QUADD_UNW_METHOD_NONE:
default:
break;
}
return cc->nr;
}
|