| Dave Peverley | 40a8b42 | 2008-12-16 09:35:40 +0900 | [diff] [blame] | 1 | /* | 
 | 2 |  * SH specific backtracing code for oprofile | 
 | 3 |  * | 
 | 4 |  * Copyright 2007 STMicroelectronics Ltd. | 
 | 5 |  * | 
 | 6 |  * Author: Dave Peverley <dpeverley@mpc-data.co.uk> | 
 | 7 |  * | 
 | 8 |  * Based on ARM oprofile backtrace code by Richard Purdie and in turn, i386 | 
 | 9 |  * oprofile backtrace code by John Levon, David Smith | 
 | 10 |  * | 
 | 11 |  * This program is free software; you can redistribute it and/or modify | 
 | 12 |  * it under the terms of the GNU General Public License version 2 as | 
 | 13 |  * published by the Free Software Foundation. | 
 | 14 |  * | 
 | 15 |  */ | 
 | 16 | #include <linux/oprofile.h> | 
 | 17 | #include <linux/sched.h> | 
 | 18 | #include <linux/kallsyms.h> | 
 | 19 | #include <linux/mm.h> | 
| Matt Fleming | 0eff9f6 | 2009-08-11 22:43:20 +0100 | [diff] [blame] | 20 | #include <asm/unwinder.h> | 
| Dave Peverley | 40a8b42 | 2008-12-16 09:35:40 +0900 | [diff] [blame] | 21 | #include <asm/ptrace.h> | 
 | 22 | #include <asm/uaccess.h> | 
 | 23 | #include <asm/sections.h> | 
| Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 24 | #include <asm/stacktrace.h> | 
 | 25 |  | 
 | 26 | static void backtrace_warning_symbol(void *data, char *msg, | 
 | 27 | 				     unsigned long symbol) | 
 | 28 | { | 
 | 29 | 	/* Ignore warnings */ | 
 | 30 | } | 
 | 31 |  | 
 | 32 | static void backtrace_warning(void *data, char *msg) | 
 | 33 | { | 
 | 34 | 	/* Ignore warnings */ | 
 | 35 | } | 
 | 36 |  | 
 | 37 | static int backtrace_stack(void *data, char *name) | 
 | 38 | { | 
 | 39 | 	/* Yes, we want all stacks */ | 
 | 40 | 	return 0; | 
 | 41 | } | 
 | 42 |  | 
 | 43 | static void backtrace_address(void *data, unsigned long addr, int reliable) | 
 | 44 | { | 
 | 45 | 	unsigned int *depth = data; | 
 | 46 |  | 
 | 47 | 	if ((*depth)--) | 
 | 48 | 		oprofile_add_trace(addr); | 
 | 49 | } | 
 | 50 |  | 
 | 51 | static struct stacktrace_ops backtrace_ops = { | 
 | 52 | 	.warning = backtrace_warning, | 
 | 53 | 	.warning_symbol = backtrace_warning_symbol, | 
 | 54 | 	.stack = backtrace_stack, | 
 | 55 | 	.address = backtrace_address, | 
 | 56 | }; | 
| Dave Peverley | 40a8b42 | 2008-12-16 09:35:40 +0900 | [diff] [blame] | 57 |  | 
 | 58 | /* Limit to stop backtracing too far. */ | 
 | 59 | static int backtrace_limit = 20; | 
 | 60 |  | 
 | 61 | static unsigned long * | 
 | 62 | user_backtrace(unsigned long *stackaddr, struct pt_regs *regs) | 
 | 63 | { | 
 | 64 | 	unsigned long buf_stack; | 
 | 65 |  | 
 | 66 | 	/* Also check accessibility of address */ | 
 | 67 | 	if (!access_ok(VERIFY_READ, stackaddr, sizeof(unsigned long))) | 
 | 68 | 		return NULL; | 
 | 69 |  | 
 | 70 | 	if (__copy_from_user_inatomic(&buf_stack, stackaddr, sizeof(unsigned long))) | 
 | 71 | 		return NULL; | 
 | 72 |  | 
 | 73 | 	/* Quick paranoia check */ | 
 | 74 | 	if (buf_stack & 3) | 
 | 75 | 		return NULL; | 
 | 76 |  | 
 | 77 | 	oprofile_add_trace(buf_stack); | 
 | 78 |  | 
 | 79 | 	stackaddr++; | 
 | 80 |  | 
 | 81 | 	return stackaddr; | 
 | 82 | } | 
 | 83 |  | 
| Dave Peverley | 40a8b42 | 2008-12-16 09:35:40 +0900 | [diff] [blame] | 84 | void sh_backtrace(struct pt_regs * const regs, unsigned int depth) | 
 | 85 | { | 
 | 86 | 	unsigned long *stackaddr; | 
 | 87 |  | 
 | 88 | 	/* | 
 | 89 | 	 * Paranoia - clip max depth as we could get lost in the weeds. | 
 | 90 | 	 */ | 
 | 91 | 	if (depth > backtrace_limit) | 
 | 92 | 		depth = backtrace_limit; | 
 | 93 |  | 
 | 94 | 	stackaddr = (unsigned long *)regs->regs[15]; | 
 | 95 | 	if (!user_mode(regs)) { | 
| Matt Fleming | 4e14dfc | 2009-08-07 16:11:19 +0100 | [diff] [blame] | 96 | 		if (depth) | 
| Matt Fleming | 0eff9f6 | 2009-08-11 22:43:20 +0100 | [diff] [blame] | 97 | 			unwind_stack(NULL, regs, stackaddr, | 
 | 98 | 				     &backtrace_ops, &depth); | 
| Dave Peverley | 40a8b42 | 2008-12-16 09:35:40 +0900 | [diff] [blame] | 99 | 		return; | 
 | 100 | 	} | 
 | 101 |  | 
 | 102 | 	while (depth-- && (stackaddr != NULL)) | 
 | 103 | 		stackaddr = user_backtrace(stackaddr, regs); | 
 | 104 | } |