Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 1 | /* |
| 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> |
| 19 | #include <asm/ptrace.h> |
| 20 | #include <asm/uaccess.h> |
| 21 | |
| 22 | |
| 23 | /* |
| 24 | * The registers we're interested in are at the end of the variable |
| 25 | * length saved register structure. The fp points at the end of this |
| 26 | * structure so the address of this struct is: |
| 27 | * (struct frame_tail *)(xxx->fp)-1 |
| 28 | */ |
| 29 | struct frame_tail { |
| 30 | struct frame_tail *fp; |
| 31 | unsigned long sp; |
| 32 | unsigned long lr; |
| 33 | } __attribute__((packed)); |
| 34 | |
| 35 | |
| 36 | #ifdef CONFIG_FRAME_POINTER |
| 37 | static struct frame_tail* kernel_backtrace(struct frame_tail *tail) |
| 38 | { |
| 39 | oprofile_add_trace(tail->lr); |
| 40 | |
| 41 | /* frame pointers should strictly progress back up the stack |
| 42 | * (towards higher addresses) */ |
| 43 | if (tail >= tail->fp) |
| 44 | return NULL; |
| 45 | |
| 46 | return tail->fp-1; |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | static struct frame_tail* user_backtrace(struct frame_tail *tail) |
| 51 | { |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 52 | struct frame_tail buftail[2]; |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 53 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 54 | /* Also check accessibility of one struct frame_tail beyond */ |
| 55 | if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) |
| 56 | return NULL; |
| 57 | if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 58 | return NULL; |
| 59 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 60 | oprofile_add_trace(buftail[0].lr); |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 61 | |
| 62 | /* frame pointers should strictly progress back up the stack |
| 63 | * (towards higher addresses) */ |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 64 | if (tail >= buftail[0].fp) |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 65 | return NULL; |
| 66 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 67 | return buftail[0].fp-1; |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* |
| 71 | * | | /\ Higher addresses |
| 72 | * | | |
| 73 | * --------------- stack base (address of current_thread_info) |
| 74 | * | thread info | |
| 75 | * . . |
| 76 | * | stack | |
| 77 | * --------------- saved regs->ARM_fp value if valid (frame_tail address) |
| 78 | * . . |
| 79 | * --------------- struct pt_regs stored on stack (struct pt_regs *) |
| 80 | * | | |
| 81 | * . . |
| 82 | * | | |
| 83 | * --------------- %esp |
| 84 | * | | |
| 85 | * | | \/ Lower addresses |
| 86 | * |
| 87 | * Thus, &pt_regs <-> stack base restricts the valid(ish) fp values |
| 88 | */ |
| 89 | static int valid_kernel_stack(struct frame_tail *tail, struct pt_regs *regs) |
| 90 | { |
| 91 | unsigned long tailaddr = (unsigned long)tail; |
| 92 | unsigned long stack = (unsigned long)regs; |
| 93 | unsigned long stack_base = (stack & ~(THREAD_SIZE - 1)) + THREAD_SIZE; |
| 94 | |
| 95 | return (tailaddr > stack) && (tailaddr < stack_base); |
| 96 | } |
| 97 | |
Richard Purdie | c013622 | 2005-08-04 15:06:59 +0100 | [diff] [blame] | 98 | void arm_backtrace(struct pt_regs * const regs, unsigned int depth) |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 99 | { |
| 100 | struct frame_tail *tail; |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 101 | |
| 102 | tail = ((struct frame_tail *) regs->ARM_fp) - 1; |
| 103 | |
| 104 | if (!user_mode(regs)) { |
| 105 | |
| 106 | #ifdef CONFIG_FRAME_POINTER |
| 107 | while (depth-- && tail && valid_kernel_stack(tail, regs)) { |
| 108 | tail = kernel_backtrace(tail); |
| 109 | } |
| 110 | #endif |
| 111 | return; |
| 112 | } |
| 113 | |
Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 114 | while (depth-- && tail && !((unsigned long) tail & 3)) |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 115 | tail = user_backtrace(tail); |
Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 116 | } |