xref: /OK3568_Linux_fs/kernel/arch/ia64/oprofile/backtrace.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /**
2*4882a593Smuzhiyun  * @file backtrace.c
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * @remark Copyright 2004 Silicon Graphics Inc.  All Rights Reserved.
5*4882a593Smuzhiyun  * @remark Read the file COPYING
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * @author Greg Banks <gnb@melbourne.sgi.com>
8*4882a593Smuzhiyun  * @author Keith Owens <kaos@melbourne.sgi.com>
9*4882a593Smuzhiyun  * Based on work done for the ia64 port of the SGI kernprof patch, which is
10*4882a593Smuzhiyun  *    Copyright (c) 2003-2004 Silicon Graphics Inc.  All Rights Reserved.
11*4882a593Smuzhiyun  */
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include <linux/oprofile.h>
14*4882a593Smuzhiyun #include <linux/sched.h>
15*4882a593Smuzhiyun #include <linux/mm.h>
16*4882a593Smuzhiyun #include <asm/ptrace.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * For IA64 we need to perform a complex little dance to get both
20*4882a593Smuzhiyun  * the struct pt_regs and a synthetic struct switch_stack in place
21*4882a593Smuzhiyun  * to allow the unwind code to work.  This dance requires our unwind
22*4882a593Smuzhiyun  * using code to be called from a function called from unw_init_running().
23*4882a593Smuzhiyun  * There we only get a single void* data pointer, so use this struct
24*4882a593Smuzhiyun  * to hold all the data we need during the unwind.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun typedef struct
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	unsigned int depth;
29*4882a593Smuzhiyun 	struct pt_regs *regs;
30*4882a593Smuzhiyun 	struct unw_frame_info frame;
31*4882a593Smuzhiyun 	unsigned long *prev_pfs_loc;	/* state for WAR for old spinlock ool code */
32*4882a593Smuzhiyun } ia64_backtrace_t;
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun /* Returns non-zero if the PC is in the Interrupt Vector Table */
in_ivt_code(unsigned long pc)35*4882a593Smuzhiyun static __inline__ int in_ivt_code(unsigned long pc)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	extern char ia64_ivt[];
38*4882a593Smuzhiyun 	return (pc >= (u_long)ia64_ivt && pc < (u_long)ia64_ivt+32768);
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun /*
42*4882a593Smuzhiyun  * Unwind to next stack frame.
43*4882a593Smuzhiyun  */
next_frame(ia64_backtrace_t * bt)44*4882a593Smuzhiyun static __inline__ int next_frame(ia64_backtrace_t *bt)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	/*
47*4882a593Smuzhiyun 	 * Avoid unsightly console message from unw_unwind() when attempting
48*4882a593Smuzhiyun 	 * to unwind through the Interrupt Vector Table which has no unwind
49*4882a593Smuzhiyun 	 * information.
50*4882a593Smuzhiyun 	 */
51*4882a593Smuzhiyun 	if (in_ivt_code(bt->frame.ip))
52*4882a593Smuzhiyun 		return 0;
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	/*
55*4882a593Smuzhiyun 	 * WAR for spinlock contention from leaf functions.  ia64_spinlock_contention_pre3_4
56*4882a593Smuzhiyun 	 * has ar.pfs == r0.  Leaf functions do not modify ar.pfs so ar.pfs remains
57*4882a593Smuzhiyun 	 * as 0, stopping the backtrace.  Record the previous ar.pfs when the current
58*4882a593Smuzhiyun 	 * IP is in ia64_spinlock_contention_pre3_4 then unwind, if pfs_loc has not changed
59*4882a593Smuzhiyun 	 * after unwind then use pt_regs.ar_pfs which is where the real ar.pfs is for
60*4882a593Smuzhiyun 	 * leaf functions.
61*4882a593Smuzhiyun 	 */
62*4882a593Smuzhiyun 	if (bt->prev_pfs_loc && bt->regs && bt->frame.pfs_loc == bt->prev_pfs_loc)
63*4882a593Smuzhiyun 		bt->frame.pfs_loc = &bt->regs->ar_pfs;
64*4882a593Smuzhiyun 	bt->prev_pfs_loc = NULL;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return unw_unwind(&bt->frame) == 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 
do_ia64_backtrace(struct unw_frame_info * info,void * vdata)70*4882a593Smuzhiyun static void do_ia64_backtrace(struct unw_frame_info *info, void *vdata)
71*4882a593Smuzhiyun {
72*4882a593Smuzhiyun 	ia64_backtrace_t *bt = vdata;
73*4882a593Smuzhiyun 	struct switch_stack *sw;
74*4882a593Smuzhiyun 	int count = 0;
75*4882a593Smuzhiyun 	u_long pc, sp;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	sw = (struct switch_stack *)(info+1);
78*4882a593Smuzhiyun 	/* padding from unw_init_running */
79*4882a593Smuzhiyun 	sw = (struct switch_stack *)(((unsigned long)sw + 15) & ~15);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	unw_init_frame_info(&bt->frame, current, sw);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	/* skip over interrupt frame and oprofile calls */
84*4882a593Smuzhiyun 	do {
85*4882a593Smuzhiyun 		unw_get_sp(&bt->frame, &sp);
86*4882a593Smuzhiyun 		if (sp >= (u_long)bt->regs)
87*4882a593Smuzhiyun 			break;
88*4882a593Smuzhiyun 		if (!next_frame(bt))
89*4882a593Smuzhiyun 			return;
90*4882a593Smuzhiyun 	} while (count++ < 200);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	/* finally, grab the actual sample */
93*4882a593Smuzhiyun 	while (bt->depth-- && next_frame(bt)) {
94*4882a593Smuzhiyun 		unw_get_ip(&bt->frame, &pc);
95*4882a593Smuzhiyun 		oprofile_add_trace(pc);
96*4882a593Smuzhiyun 		if (unw_is_intr_frame(&bt->frame)) {
97*4882a593Smuzhiyun 			/*
98*4882a593Smuzhiyun 			 * Interrupt received on kernel stack; this can
99*4882a593Smuzhiyun 			 * happen when timer interrupt fires while processing
100*4882a593Smuzhiyun 			 * a softirq from the tail end of a hardware interrupt
101*4882a593Smuzhiyun 			 * which interrupted a system call.  Don't laugh, it
102*4882a593Smuzhiyun 			 * happens!  Splice the backtrace into two parts to
103*4882a593Smuzhiyun 			 * avoid spurious cycles in the gprof output.
104*4882a593Smuzhiyun 			 */
105*4882a593Smuzhiyun 			/* TODO: split rather than drop the 2nd half */
106*4882a593Smuzhiyun 			break;
107*4882a593Smuzhiyun 		}
108*4882a593Smuzhiyun 	}
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun void
ia64_backtrace(struct pt_regs * const regs,unsigned int depth)112*4882a593Smuzhiyun ia64_backtrace(struct pt_regs * const regs, unsigned int depth)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun 	ia64_backtrace_t bt;
115*4882a593Smuzhiyun 	unsigned long flags;
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 	/*
118*4882a593Smuzhiyun 	 * On IA64 there is little hope of getting backtraces from
119*4882a593Smuzhiyun 	 * user space programs -- the problems of getting the unwind
120*4882a593Smuzhiyun 	 * information from arbitrary user programs are extreme.
121*4882a593Smuzhiyun 	 */
122*4882a593Smuzhiyun 	if (user_mode(regs))
123*4882a593Smuzhiyun 		return;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	bt.depth = depth;
126*4882a593Smuzhiyun 	bt.regs = regs;
127*4882a593Smuzhiyun 	bt.prev_pfs_loc = NULL;
128*4882a593Smuzhiyun 	local_irq_save(flags);
129*4882a593Smuzhiyun 	unw_init_running(do_ia64_backtrace, &bt);
130*4882a593Smuzhiyun 	local_irq_restore(flags);
131*4882a593Smuzhiyun }
132