blob: cefc21c2eee4967827f1f2e1becf80193a054c07 [file] [log] [blame]
Richard Purdiefa0ebff2005-06-28 21:01:03 +01001/*
2 * Arm specific backtracing code for oprofile
3 *
4 * Copyright 2005 Openedhand Ltd.
5 *
6 * Author: Richard Purdie <rpurdie@openedhand.com>
7 *
8 * Based on i386 oprofile backtrace code by John Levon, David Smith
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/oprofile.h>
17#include <linux/sched.h>
18#include <linux/mm.h>
Russell King33fa9b12008-09-06 11:35:55 +010019#include <linux/uaccess.h>
Richard Purdiefa0ebff2005-06-28 21:01:03 +010020#include <asm/ptrace.h>
Richard Purdiefa0ebff2005-06-28 21:01:03 +010021
Russell Kingf16fb1e2007-04-28 09:59:37 +010022#include "../kernel/stacktrace.h"
23
24static int report_trace(struct stackframe *frame, void *d)
25{
26 unsigned int *depth = d;
27
28 if (*depth) {
29 oprofile_add_trace(frame->lr);
30 (*depth)--;
31 }
32
33 return *depth == 0;
34}
Richard Purdiefa0ebff2005-06-28 21:01:03 +010035
36/*
37 * The registers we're interested in are at the end of the variable
38 * length saved register structure. The fp points at the end of this
39 * structure so the address of this struct is:
40 * (struct frame_tail *)(xxx->fp)-1
41 */
42struct frame_tail {
43 struct frame_tail *fp;
44 unsigned long sp;
45 unsigned long lr;
46} __attribute__((packed));
47
Richard Purdiefa0ebff2005-06-28 21:01:03 +010048static struct frame_tail* user_backtrace(struct frame_tail *tail)
49{
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070050 struct frame_tail buftail[2];
Richard Purdiefa0ebff2005-06-28 21:01:03 +010051
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070052 /* Also check accessibility of one struct frame_tail beyond */
53 if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
54 return NULL;
55 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
Richard Purdiefa0ebff2005-06-28 21:01:03 +010056 return NULL;
57
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070058 oprofile_add_trace(buftail[0].lr);
Richard Purdiefa0ebff2005-06-28 21:01:03 +010059
60 /* frame pointers should strictly progress back up the stack
61 * (towards higher addresses) */
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070062 if (tail >= buftail[0].fp)
Richard Purdiefa0ebff2005-06-28 21:01:03 +010063 return NULL;
64
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070065 return buftail[0].fp-1;
Richard Purdiefa0ebff2005-06-28 21:01:03 +010066}
67
Richard Purdiec0136222005-08-04 15:06:59 +010068void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
Richard Purdiefa0ebff2005-06-28 21:01:03 +010069{
Russell Kingf16fb1e2007-04-28 09:59:37 +010070 struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
Richard Purdiefa0ebff2005-06-28 21:01:03 +010071
72 if (!user_mode(regs)) {
Russell Kingf16fb1e2007-04-28 09:59:37 +010073 unsigned long base = ((unsigned long)regs) & ~(THREAD_SIZE - 1);
74 walk_stackframe(regs->ARM_fp, base, base + THREAD_SIZE,
75 report_trace, &depth);
Richard Purdiefa0ebff2005-06-28 21:01:03 +010076 return;
77 }
78
Hugh Dickinsc34d1b42005-10-29 18:16:32 -070079 while (depth-- && tail && !((unsigned long) tail & 3))
Richard Purdiefa0ebff2005-06-28 21:01:03 +010080 tail = user_backtrace(tail);
Richard Purdiefa0ebff2005-06-28 21:01:03 +010081}