xref: /openbmc/linux/arch/parisc/kernel/unwind.c (revision 2214c0e77259b420402e279e9ab4277ef320d371)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Kernel unwinding support
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * (c) 2002-2004 Randolph Chung <tausq@debian.org>
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * Derived partially from the IA64 implementation. The PA-RISC
81da177e4SLinus Torvalds  * Runtime Architecture Document is also a useful reference to
91da177e4SLinus Torvalds  * understand what is happening here
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds #include <linux/kernel.h>
131da177e4SLinus Torvalds #include <linux/init.h>
14e6fc0449SMatthew Wilcox #include <linux/sched.h>
151da177e4SLinus Torvalds #include <linux/slab.h>
168f78df87SHelge Deller #include <linux/sort.h>
17*2214c0e7SHelge Deller #include <linux/sched/task_stack.h>
181da177e4SLinus Torvalds 
197c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
201da177e4SLinus Torvalds #include <asm/assembly.h>
2105dc16d6SRandolph Chung #include <asm/asm-offsets.h>
2205dc16d6SRandolph Chung #include <asm/ptrace.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/unwind.h>
258e0ba125SSven Schnelle #include <asm/switch_to.h>
268e0ba125SSven Schnelle #include <asm/sections.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds /* #define DEBUG 1 */
291da177e4SLinus Torvalds #ifdef DEBUG
3063ba82c0SHelge Deller #define dbg(x...) pr_debug(x)
311da177e4SLinus Torvalds #else
321da177e4SLinus Torvalds #define dbg(x...)
331da177e4SLinus Torvalds #endif
341da177e4SLinus Torvalds 
3533a932d1SHelge Deller #define KERNEL_START (KERNEL_BINARY_TEXT_START)
36e036306aSRandolph Chung 
371da177e4SLinus Torvalds extern struct unwind_table_entry __start___unwind[];
381da177e4SLinus Torvalds extern struct unwind_table_entry __stop___unwind[];
391da177e4SLinus Torvalds 
4076cffeb6SHelge Deller static DEFINE_SPINLOCK(unwind_lock);
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * the kernel unwind block is not dynamically allocated so that
431da177e4SLinus Torvalds  * we can call unwind_init as early in the bootup process as
441da177e4SLinus Torvalds  * possible (before the slab allocator is initialized)
451da177e4SLinus Torvalds  */
4647293774SHelge Deller static struct unwind_table kernel_unwind_table __ro_after_init;
471da177e4SLinus Torvalds static LIST_HEAD(unwind_tables);
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds static inline const struct unwind_table_entry *
501da177e4SLinus Torvalds find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds 	const struct unwind_table_entry *e = NULL;
531da177e4SLinus Torvalds 	unsigned long lo, hi, mid;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	lo = 0;
561da177e4SLinus Torvalds 	hi = table->length - 1;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 	while (lo <= hi) {
591da177e4SLinus Torvalds 		mid = (hi - lo) / 2 + lo;
601da177e4SLinus Torvalds 		e = &table->table[mid];
611da177e4SLinus Torvalds 		if (addr < e->region_start)
621da177e4SLinus Torvalds 			hi = mid - 1;
631da177e4SLinus Torvalds 		else if (addr > e->region_end)
641da177e4SLinus Torvalds 			lo = mid + 1;
651da177e4SLinus Torvalds 		else
661da177e4SLinus Torvalds 			return e;
671da177e4SLinus Torvalds 	}
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	return NULL;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static const struct unwind_table_entry *
731da177e4SLinus Torvalds find_unwind_entry(unsigned long addr)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct unwind_table *table;
761da177e4SLinus Torvalds 	const struct unwind_table_entry *e = NULL;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	if (addr >= kernel_unwind_table.start &&
791da177e4SLinus Torvalds 	    addr <= kernel_unwind_table.end)
801da177e4SLinus Torvalds 		e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
81be24a897SMikulas Patocka 	else {
82be24a897SMikulas Patocka 		unsigned long flags;
83be24a897SMikulas Patocka 
84be24a897SMikulas Patocka 		spin_lock_irqsave(&unwind_lock, flags);
851da177e4SLinus Torvalds 		list_for_each_entry(table, &unwind_tables, list) {
861da177e4SLinus Torvalds 			if (addr >= table->start &&
871da177e4SLinus Torvalds 			    addr <= table->end)
881da177e4SLinus Torvalds 				e = find_unwind_entry_in_table(table, addr);
89b1b1d4a6SPhil Carmody 			if (e) {
90b1b1d4a6SPhil Carmody 				/* Move-to-front to exploit common traces */
91b1b1d4a6SPhil Carmody 				list_move(&table->list, &unwind_tables);
921da177e4SLinus Torvalds 				break;
931da177e4SLinus Torvalds 			}
94b1b1d4a6SPhil Carmody 		}
95be24a897SMikulas Patocka 		spin_unlock_irqrestore(&unwind_lock, flags);
96be24a897SMikulas Patocka 	}
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	return e;
991da177e4SLinus Torvalds }
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static void
1021da177e4SLinus Torvalds unwind_table_init(struct unwind_table *table, const char *name,
1031da177e4SLinus Torvalds 		  unsigned long base_addr, unsigned long gp,
1041da177e4SLinus Torvalds 		  void *table_start, void *table_end)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	struct unwind_table_entry *start = table_start;
1071da177e4SLinus Torvalds 	struct unwind_table_entry *end =
1081da177e4SLinus Torvalds 		(struct unwind_table_entry *)table_end - 1;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	table->name = name;
1111da177e4SLinus Torvalds 	table->base_addr = base_addr;
1121da177e4SLinus Torvalds 	table->gp = gp;
1131da177e4SLinus Torvalds 	table->start = base_addr + start->region_start;
1141da177e4SLinus Torvalds 	table->end = base_addr + end->region_end;
1151da177e4SLinus Torvalds 	table->table = (struct unwind_table_entry *)table_start;
1161da177e4SLinus Torvalds 	table->length = end - start + 1;
1171da177e4SLinus Torvalds 	INIT_LIST_HEAD(&table->list);
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	for (; start <= end; start++) {
1201da177e4SLinus Torvalds 		if (start < end &&
1211da177e4SLinus Torvalds 		    start->region_end > (start+1)->region_start) {
122c8921d72SHelge Deller 			pr_warn("Out of order unwind entry! %px and %px\n",
123c8921d72SHelge Deller 				start, start+1);
1241da177e4SLinus Torvalds 		}
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds 		start->region_start += base_addr;
1271da177e4SLinus Torvalds 		start->region_end += base_addr;
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds 
1318f78df87SHelge Deller static int cmp_unwind_table_entry(const void *a, const void *b)
1328f78df87SHelge Deller {
1338f78df87SHelge Deller 	return ((const struct unwind_table_entry *)a)->region_start
1348f78df87SHelge Deller 	     - ((const struct unwind_table_entry *)b)->region_start;
1358f78df87SHelge Deller }
1368f78df87SHelge Deller 
1371da177e4SLinus Torvalds static void
1381da177e4SLinus Torvalds unwind_table_sort(struct unwind_table_entry *start,
1391da177e4SLinus Torvalds 		  struct unwind_table_entry *finish)
1401da177e4SLinus Torvalds {
1418f78df87SHelge Deller 	sort(start, finish - start, sizeof(struct unwind_table_entry),
1428f78df87SHelge Deller 	     cmp_unwind_table_entry, NULL);
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds struct unwind_table *
1461da177e4SLinus Torvalds unwind_table_add(const char *name, unsigned long base_addr,
1471da177e4SLinus Torvalds 		 unsigned long gp,
1481da177e4SLinus Torvalds                  void *start, void *end)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	struct unwind_table *table;
1511da177e4SLinus Torvalds 	unsigned long flags;
1521da177e4SLinus Torvalds 	struct unwind_table_entry *s = (struct unwind_table_entry *)start;
1531da177e4SLinus Torvalds 	struct unwind_table_entry *e = (struct unwind_table_entry *)end;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	unwind_table_sort(s, e);
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	table = kmalloc(sizeof(struct unwind_table), GFP_USER);
1581da177e4SLinus Torvalds 	if (table == NULL)
1591da177e4SLinus Torvalds 		return NULL;
1601da177e4SLinus Torvalds 	unwind_table_init(table, name, base_addr, gp, start, end);
1611da177e4SLinus Torvalds 	spin_lock_irqsave(&unwind_lock, flags);
1621da177e4SLinus Torvalds 	list_add_tail(&table->list, &unwind_tables);
1631da177e4SLinus Torvalds 	spin_unlock_irqrestore(&unwind_lock, flags);
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	return table;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds void unwind_table_remove(struct unwind_table *table)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	unsigned long flags;
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	spin_lock_irqsave(&unwind_lock, flags);
1731da177e4SLinus Torvalds 	list_del(&table->list);
1741da177e4SLinus Torvalds 	spin_unlock_irqrestore(&unwind_lock, flags);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	kfree(table);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds /* Called from setup_arch to import the kernel unwind info */
180c790b41bSHelge Deller int __init unwind_init(void)
1811da177e4SLinus Torvalds {
1821da177e4SLinus Torvalds 	long start, stop;
1831da177e4SLinus Torvalds 	register unsigned long gp __asm__ ("r27");
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	start = (long)&__start___unwind[0];
1861da177e4SLinus Torvalds 	stop = (long)&__stop___unwind[0];
1871da177e4SLinus Torvalds 
18863ba82c0SHelge Deller 	dbg("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
1891da177e4SLinus Torvalds 	    start, stop,
1901da177e4SLinus Torvalds 	    (stop - start) / sizeof(struct unwind_table_entry));
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
1931da177e4SLinus Torvalds 			  gp,
1941da177e4SLinus Torvalds 			  &__start___unwind[0], &__stop___unwind[0]);
1951da177e4SLinus Torvalds #if 0
1961da177e4SLinus Torvalds 	{
1971da177e4SLinus Torvalds 		int i;
1981da177e4SLinus Torvalds 		for (i = 0; i < 10; i++)
1991da177e4SLinus Torvalds 		{
2001da177e4SLinus Torvalds 			printk("region 0x%x-0x%x\n",
2011da177e4SLinus Torvalds 				__start___unwind[i].region_start,
2021da177e4SLinus Torvalds 				__start___unwind[i].region_end);
2031da177e4SLinus Torvalds 		}
2041da177e4SLinus Torvalds 	}
2051da177e4SLinus Torvalds #endif
2061da177e4SLinus Torvalds 	return 0;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
2098e0ba125SSven Schnelle static bool pc_is_kernel_fn(unsigned long pc, void *fn)
2108e0ba125SSven Schnelle {
2118e0ba125SSven Schnelle 	return (unsigned long)dereference_kernel_function_descriptor(fn) == pc;
2128e0ba125SSven Schnelle }
2138e0ba125SSven Schnelle 
21405dc16d6SRandolph Chung static int unwind_special(struct unwind_frame_info *info, unsigned long pc, int frame_size)
21505dc16d6SRandolph Chung {
216c8921d72SHelge Deller 	/*
217c8921d72SHelge Deller 	 * We have to use void * instead of a function pointer, because
218c8921d72SHelge Deller 	 * function pointers aren't a pointer to the function on 64-bit.
219c8921d72SHelge Deller 	 * Make them const so the compiler knows they live in .text
2208801ccb9SHelge Deller 	 * Note: We could use dereference_kernel_function_descriptor()
2218801ccb9SHelge Deller 	 * instead but we want to keep it simple here.
222c8921d72SHelge Deller 	 */
223c8921d72SHelge Deller 	extern void * const handle_interruption;
224c8921d72SHelge Deller 	extern void * const ret_from_kernel_thread;
225c8921d72SHelge Deller 	extern void * const syscall_exit;
226c8921d72SHelge Deller 	extern void * const intr_return;
227c8921d72SHelge Deller 	extern void * const _switch_to_ret;
228c8921d72SHelge Deller #ifdef CONFIG_IRQSTACKS
2298801ccb9SHelge Deller 	extern void * const _call_on_stack;
230c8921d72SHelge Deller #endif /* CONFIG_IRQSTACKS */
23105dc16d6SRandolph Chung 
2328e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, handle_interruption)) {
23305dc16d6SRandolph Chung 		struct pt_regs *regs = (struct pt_regs *)(info->sp - frame_size - PT_SZ_ALGN);
23405dc16d6SRandolph Chung 		dbg("Unwinding through handle_interruption()\n");
23505dc16d6SRandolph Chung 		info->prev_sp = regs->gr[30];
23605dc16d6SRandolph Chung 		info->prev_ip = regs->iaoq[0];
23705dc16d6SRandolph Chung 		return 1;
23805dc16d6SRandolph Chung 	}
23905dc16d6SRandolph Chung 
2408e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, ret_from_kernel_thread) ||
2418e0ba125SSven Schnelle 	    pc_is_kernel_fn(pc, syscall_exit)) {
242c8921d72SHelge Deller 		info->prev_sp = info->prev_ip = 0;
243c8921d72SHelge Deller 		return 1;
244c8921d72SHelge Deller 	}
245c8921d72SHelge Deller 
2468e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, intr_return)) {
247c8921d72SHelge Deller 		struct pt_regs *regs;
248c8921d72SHelge Deller 
249c8921d72SHelge Deller 		dbg("Found intr_return()\n");
250c8921d72SHelge Deller 		regs = (struct pt_regs *)(info->sp - PT_SZ_ALGN);
251c8921d72SHelge Deller 		info->prev_sp = regs->gr[30];
252c8921d72SHelge Deller 		info->prev_ip = regs->iaoq[0];
253c8921d72SHelge Deller 		info->rp = regs->gr[2];
254c8921d72SHelge Deller 		return 1;
255c8921d72SHelge Deller 	}
256c8921d72SHelge Deller 
2578e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, _switch_to) ||
2588e0ba125SSven Schnelle 	    pc_is_kernel_fn(pc, _switch_to_ret)) {
259c8921d72SHelge Deller 		info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
260c8921d72SHelge Deller 		info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
261c8921d72SHelge Deller 		return 1;
262c8921d72SHelge Deller 	}
263c8921d72SHelge Deller 
264c8921d72SHelge Deller #ifdef CONFIG_IRQSTACKS
2658e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, _call_on_stack)) {
266c8921d72SHelge Deller 		info->prev_sp = *(unsigned long *)(info->sp - FRAME_SIZE - REG_SZ);
267c8921d72SHelge Deller 		info->prev_ip = *(unsigned long *)(info->sp - FRAME_SIZE - RP_OFFSET);
268c8921d72SHelge Deller 		return 1;
269c8921d72SHelge Deller 	}
270c8921d72SHelge Deller #endif
27105dc16d6SRandolph Chung 	return 0;
27205dc16d6SRandolph Chung }
27305dc16d6SRandolph Chung 
2741da177e4SLinus Torvalds static void unwind_frame_regs(struct unwind_frame_info *info)
2751da177e4SLinus Torvalds {
2761da177e4SLinus Torvalds 	const struct unwind_table_entry *e;
2771da177e4SLinus Torvalds 	unsigned long npc;
2781da177e4SLinus Torvalds 	unsigned int insn;
2791da177e4SLinus Torvalds 	long frame_size = 0;
2801da177e4SLinus Torvalds 	int looking_for_rp, rpoffset = 0;
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	e = find_unwind_entry(info->ip);
2831da177e4SLinus Torvalds 	if (e == NULL) {
2841da177e4SLinus Torvalds 		unsigned long sp;
2851da177e4SLinus Torvalds 
286c8921d72SHelge Deller 		dbg("Cannot find unwind entry for %pS; forced unwinding\n",
287c8921d72SHelge Deller 			(void *) info->ip);
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 		/* Since we are doing the unwinding blind, we don't know if
2901da177e4SLinus Torvalds 		   we are adjusting the stack correctly or extracting the rp
2911da177e4SLinus Torvalds 		   correctly. The rp is checked to see if it belongs to the
2921da177e4SLinus Torvalds 		   kernel text section, if not we assume we don't have a
2931da177e4SLinus Torvalds 		   correct stack frame and we continue to unwind the stack.
2941da177e4SLinus Torvalds 		   This is not quite correct, and will fail for loadable
2951da177e4SLinus Torvalds 		   modules. */
2961da177e4SLinus Torvalds 		sp = info->sp & ~63;
2971da177e4SLinus Torvalds 		do {
2981da177e4SLinus Torvalds 			unsigned long tmp;
2991da177e4SLinus Torvalds 
3001da177e4SLinus Torvalds 			info->prev_sp = sp - 64;
3011da177e4SLinus Torvalds 			info->prev_ip = 0;
302e77900abSHelge Deller 
303*2214c0e7SHelge Deller 			/* Check if stack is inside kernel stack area */
304*2214c0e7SHelge Deller 			if ((info->prev_sp - (unsigned long) task_stack_page(info->t))
305*2214c0e7SHelge Deller 					>= THREAD_SIZE) {
306e77900abSHelge Deller 				info->prev_sp = 0;
307e77900abSHelge Deller 				break;
308e77900abSHelge Deller 			}
309e77900abSHelge Deller 
310cf2ec789SSven Schnelle 			if (copy_from_kernel_nofault(&tmp,
311cf2ec789SSven Schnelle 			    (void *)info->prev_sp - RP_OFFSET, sizeof(tmp)))
3121da177e4SLinus Torvalds 				break;
3131da177e4SLinus Torvalds 			info->prev_ip = tmp;
3141da177e4SLinus Torvalds 			sp = info->prev_sp;
315c790b41bSHelge Deller 		} while (!kernel_text_address(info->prev_ip));
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 		info->rp = 0;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 		dbg("analyzing func @ %lx with no unwind info, setting "
3201da177e4SLinus Torvalds 		    "prev_sp=%lx prev_ip=%lx\n", info->ip,
3211da177e4SLinus Torvalds 		    info->prev_sp, info->prev_ip);
3221da177e4SLinus Torvalds 	} else {
3231da177e4SLinus Torvalds 		dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
3241da177e4SLinus Torvalds 		    "Save_RP = %d, Millicode = %d size = %u\n",
3251da177e4SLinus Torvalds 		    e->region_start, e->region_end, e->Save_SP, e->Save_RP,
3261da177e4SLinus Torvalds 		    e->Millicode, e->Total_frame_size);
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 		looking_for_rp = e->Save_RP;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		for (npc = e->region_start;
3311da177e4SLinus Torvalds 		     (frame_size < (e->Total_frame_size << 3) ||
3321da177e4SLinus Torvalds 		      looking_for_rp) &&
3331da177e4SLinus Torvalds 		     npc < info->ip;
3341da177e4SLinus Torvalds 		     npc += 4) {
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 			insn = *(unsigned int *)npc;
3371da177e4SLinus Torvalds 
338be24a897SMikulas Patocka 			if ((insn & 0xffffc001) == 0x37de0000 ||
339be24a897SMikulas Patocka 			    (insn & 0xffe00001) == 0x6fc00000) {
3401da177e4SLinus Torvalds 				/* ldo X(sp), sp, or stwm X,D(sp) */
341be24a897SMikulas Patocka 				frame_size += (insn & 0x3fff) >> 1;
3421da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=%08x @ "
3431da177e4SLinus Torvalds 				    "%lx, frame_size = %ld\n", info->ip,
3441da177e4SLinus Torvalds 				    insn, npc, frame_size);
345be24a897SMikulas Patocka 			} else if ((insn & 0xffe00009) == 0x73c00008) {
3461da177e4SLinus Torvalds 				/* std,ma X,D(sp) */
347be24a897SMikulas Patocka 				frame_size += ((insn >> 4) & 0x3ff) << 3;
3481da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=%08x @ "
3491da177e4SLinus Torvalds 				    "%lx, frame_size = %ld\n", info->ip,
3501da177e4SLinus Torvalds 				    insn, npc, frame_size);
3511da177e4SLinus Torvalds 			} else if (insn == 0x6bc23fd9) {
3521da177e4SLinus Torvalds 				/* stw rp,-20(sp) */
3531da177e4SLinus Torvalds 				rpoffset = 20;
3541da177e4SLinus Torvalds 				looking_for_rp = 0;
3551da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=stw rp,"
3561da177e4SLinus Torvalds 				    "-20(sp) @ %lx\n", info->ip, npc);
3571da177e4SLinus Torvalds 			} else if (insn == 0x0fc212c1) {
3581da177e4SLinus Torvalds 				/* std rp,-16(sr0,sp) */
3591da177e4SLinus Torvalds 				rpoffset = 16;
3601da177e4SLinus Torvalds 				looking_for_rp = 0;
3611da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=std rp,"
3621da177e4SLinus Torvalds 				    "-16(sp) @ %lx\n", info->ip, npc);
3631da177e4SLinus Torvalds 			}
3641da177e4SLinus Torvalds 		}
3651da177e4SLinus Torvalds 
366be24a897SMikulas Patocka 		if (frame_size > e->Total_frame_size << 3)
367be24a897SMikulas Patocka 			frame_size = e->Total_frame_size << 3;
368be24a897SMikulas Patocka 
36905dc16d6SRandolph Chung 		if (!unwind_special(info, e->region_start, frame_size)) {
3701da177e4SLinus Torvalds 			info->prev_sp = info->sp - frame_size;
3711da177e4SLinus Torvalds 			if (e->Millicode)
3721da177e4SLinus Torvalds 				info->rp = info->r31;
3731da177e4SLinus Torvalds 			else if (rpoffset)
3741da177e4SLinus Torvalds 				info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
3751da177e4SLinus Torvalds 			info->prev_ip = info->rp;
3761da177e4SLinus Torvalds 			info->rp = 0;
37705dc16d6SRandolph Chung 		}
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 		dbg("analyzing func @ %lx, setting prev_sp=%lx "
3801da177e4SLinus Torvalds 		    "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
3811da177e4SLinus Torvalds 		    info->prev_ip, npc);
3821da177e4SLinus Torvalds 	}
3831da177e4SLinus Torvalds }
3841da177e4SLinus Torvalds 
3851da177e4SLinus Torvalds void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
3861da177e4SLinus Torvalds 		       struct pt_regs *regs)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	memset(info, 0, sizeof(struct unwind_frame_info));
3891da177e4SLinus Torvalds 	info->t = t;
3901da177e4SLinus Torvalds 	info->sp = regs->gr[30];
3911da177e4SLinus Torvalds 	info->ip = regs->iaoq[0];
3921da177e4SLinus Torvalds 	info->rp = regs->gr[2];
3931da177e4SLinus Torvalds 	info->r31 = regs->gr[31];
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
3961da177e4SLinus Torvalds 	    t ? (int)t->pid : -1, info->sp, info->ip);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	struct pt_regs *r = &t->thread.regs;
4021da177e4SLinus Torvalds 	struct pt_regs *r2;
4031da177e4SLinus Torvalds 
404e0e7ed48SHelge Deller 	r2 = kmalloc(sizeof(struct pt_regs), GFP_ATOMIC);
4051da177e4SLinus Torvalds 	if (!r2)
4061da177e4SLinus Torvalds 		return;
4071da177e4SLinus Torvalds 	*r2 = *r;
4081da177e4SLinus Torvalds 	r2->gr[30] = r->ksp;
4091da177e4SLinus Torvalds 	r2->iaoq[0] = r->kpc;
4101da177e4SLinus Torvalds 	unwind_frame_init(info, t, r2);
4111da177e4SLinus Torvalds 	kfree(r2);
4121da177e4SLinus Torvalds }
4131da177e4SLinus Torvalds 
4149e0d5c45SHelge Deller #define get_parisc_stackpointer() ({ \
4159e0d5c45SHelge Deller 	unsigned long sp; \
4169e0d5c45SHelge Deller 	__asm__("copy %%r30, %0" : "=r"(sp)); \
4179e0d5c45SHelge Deller 	(sp); \
4189e0d5c45SHelge Deller })
4199e0d5c45SHelge Deller 
4209e0d5c45SHelge Deller void unwind_frame_init_task(struct unwind_frame_info *info,
4219e0d5c45SHelge Deller 	struct task_struct *task, struct pt_regs *regs)
4221da177e4SLinus Torvalds {
4239e0d5c45SHelge Deller 	task = task ? task : current;
4249e0d5c45SHelge Deller 
4259e0d5c45SHelge Deller 	if (task == current) {
4269e0d5c45SHelge Deller 		struct pt_regs r;
4279e0d5c45SHelge Deller 
4289e0d5c45SHelge Deller 		if (!regs) {
4299e0d5c45SHelge Deller 			memset(&r, 0, sizeof(r));
4309e0d5c45SHelge Deller 			r.iaoq[0] =  _THIS_IP_;
4319e0d5c45SHelge Deller 			r.gr[2] = _RET_IP_;
4329e0d5c45SHelge Deller 			r.gr[30] = get_parisc_stackpointer();
4339e0d5c45SHelge Deller 			regs = &r;
4349e0d5c45SHelge Deller 		}
435cf8afe5cSHelge Deller 		unwind_frame_init(info, task, regs);
4369e0d5c45SHelge Deller 	} else {
4379e0d5c45SHelge Deller 		unwind_frame_init_from_blocked_task(info, task);
4389e0d5c45SHelge Deller 	}
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds int unwind_once(struct unwind_frame_info *next_frame)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	unwind_frame_regs(next_frame);
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds 	if (next_frame->prev_sp == 0 ||
4461da177e4SLinus Torvalds 	    next_frame->prev_ip == 0)
4471da177e4SLinus Torvalds 		return -1;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	next_frame->sp = next_frame->prev_sp;
4501da177e4SLinus Torvalds 	next_frame->ip = next_frame->prev_ip;
4511da177e4SLinus Torvalds 	next_frame->prev_sp = 0;
4521da177e4SLinus Torvalds 	next_frame->prev_ip = 0;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
4551da177e4SLinus Torvalds 	    next_frame->t ? (int)next_frame->t->pid : -1,
4561da177e4SLinus Torvalds 	    next_frame->sp, next_frame->ip);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	return 0;
4591da177e4SLinus Torvalds }
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds int unwind_to_user(struct unwind_frame_info *info)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	int ret;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	do {
4661da177e4SLinus Torvalds 		ret = unwind_once(info);
4671da177e4SLinus Torvalds 	} while (!ret && !(info->ip & 3));
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	return ret;
4701da177e4SLinus Torvalds }
47111e17809SHelge Deller 
47211e17809SHelge Deller unsigned long return_address(unsigned int level)
47311e17809SHelge Deller {
47411e17809SHelge Deller 	struct unwind_frame_info info;
47511e17809SHelge Deller 
47611e17809SHelge Deller 	/* initialize unwind info */
4779e0d5c45SHelge Deller 	unwind_frame_init_task(&info, current, NULL);
47811e17809SHelge Deller 
47911e17809SHelge Deller 	/* unwind stack */
4809e0d5c45SHelge Deller 	level += 2;
48111e17809SHelge Deller 	do {
48211e17809SHelge Deller 		if (unwind_once(&info) < 0 || info.ip == 0)
48311e17809SHelge Deller 			return 0;
484c790b41bSHelge Deller 		if (!kernel_text_address(info.ip))
48511e17809SHelge Deller 			return 0;
48611e17809SHelge Deller 	} while (info.ip && level--);
48711e17809SHelge Deller 
48811e17809SHelge Deller 	return info.ip;
48911e17809SHelge Deller }
490