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