| 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> | 
| Russell King | 33fa9b1 | 2008-09-06 11:35:55 +0100 | [diff] [blame] | 19 | #include <linux/uaccess.h> | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 20 | #include <asm/ptrace.h> | 
| Catalin Marinas | 2d7c11b | 2009-02-11 13:07:53 +0100 | [diff] [blame] | 21 | #include <asm/stacktrace.h> | 
| Russell King | f16fb1e | 2007-04-28 09:59:37 +0100 | [diff] [blame] | 22 |  | 
 | 23 | static int report_trace(struct stackframe *frame, void *d) | 
 | 24 | { | 
 | 25 | 	unsigned int *depth = d; | 
 | 26 |  | 
 | 27 | 	if (*depth) { | 
| Catalin Marinas | 2d7c11b | 2009-02-11 13:07:53 +0100 | [diff] [blame] | 28 | 		oprofile_add_trace(frame->pc); | 
| Russell King | f16fb1e | 2007-04-28 09:59:37 +0100 | [diff] [blame] | 29 | 		(*depth)--; | 
 | 30 | 	} | 
 | 31 |  | 
 | 32 | 	return *depth == 0; | 
 | 33 | } | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 34 |  | 
 | 35 | /* | 
 | 36 |  * The registers we're interested in are at the end of the variable | 
 | 37 |  * length saved register structure. The fp points at the end of this | 
 | 38 |  * structure so the address of this struct is: | 
 | 39 |  * (struct frame_tail *)(xxx->fp)-1 | 
 | 40 |  */ | 
 | 41 | struct frame_tail { | 
 | 42 | 	struct frame_tail *fp; | 
 | 43 | 	unsigned long sp; | 
 | 44 | 	unsigned long lr; | 
 | 45 | } __attribute__((packed)); | 
 | 46 |  | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 47 | static struct frame_tail* user_backtrace(struct frame_tail *tail) | 
 | 48 | { | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 49 | 	struct frame_tail buftail[2]; | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 50 |  | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 51 | 	/* Also check accessibility of one struct frame_tail beyond */ | 
 | 52 | 	if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) | 
 | 53 | 		return NULL; | 
 | 54 | 	if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 55 | 		return NULL; | 
 | 56 |  | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 57 | 	oprofile_add_trace(buftail[0].lr); | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 58 |  | 
 | 59 | 	/* frame pointers should strictly progress back up the stack | 
 | 60 | 	 * (towards higher addresses) */ | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 61 | 	if (tail >= buftail[0].fp) | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 62 | 		return NULL; | 
 | 63 |  | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 64 | 	return buftail[0].fp-1; | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 65 | } | 
 | 66 |  | 
| Richard Purdie | c013622 | 2005-08-04 15:06:59 +0100 | [diff] [blame] | 67 | void arm_backtrace(struct pt_regs * const regs, unsigned int depth) | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 68 | { | 
| Russell King | f16fb1e | 2007-04-28 09:59:37 +0100 | [diff] [blame] | 69 | 	struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1; | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 70 |  | 
 | 71 | 	if (!user_mode(regs)) { | 
| Catalin Marinas | 2d7c11b | 2009-02-11 13:07:53 +0100 | [diff] [blame] | 72 | 		struct stackframe frame; | 
 | 73 | 		frame.fp = regs->ARM_fp; | 
 | 74 | 		frame.sp = regs->ARM_sp; | 
 | 75 | 		frame.lr = regs->ARM_lr; | 
 | 76 | 		frame.pc = regs->ARM_pc; | 
 | 77 | 		walk_stackframe(&frame, report_trace, &depth); | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 78 | 		return; | 
 | 79 | 	} | 
 | 80 |  | 
| Hugh Dickins | c34d1b4 | 2005-10-29 18:16:32 -0700 | [diff] [blame] | 81 | 	while (depth-- && tail && !((unsigned long) tail & 3)) | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 82 | 		tail = user_backtrace(tail); | 
| Richard Purdie | fa0ebff | 2005-06-28 21:01:03 +0100 | [diff] [blame] | 83 | } |