xref: /openbmc/linux/arch/parisc/kernel/unwind.c (revision 8e0ba125c2bf1030af3267058019ba86da96863f)
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>
171da177e4SLinus Torvalds 
187c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
191da177e4SLinus Torvalds #include <asm/assembly.h>
2005dc16d6SRandolph Chung #include <asm/asm-offsets.h>
2105dc16d6SRandolph Chung #include <asm/ptrace.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #include <asm/unwind.h>
24*8e0ba125SSven Schnelle #include <asm/switch_to.h>
25*8e0ba125SSven Schnelle #include <asm/sections.h>
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds /* #define DEBUG 1 */
281da177e4SLinus Torvalds #ifdef DEBUG
2963ba82c0SHelge Deller #define dbg(x...) pr_debug(x)
301da177e4SLinus Torvalds #else
311da177e4SLinus Torvalds #define dbg(x...)
321da177e4SLinus Torvalds #endif
331da177e4SLinus Torvalds 
3433a932d1SHelge Deller #define KERNEL_START (KERNEL_BINARY_TEXT_START)
35e036306aSRandolph Chung 
361da177e4SLinus Torvalds extern struct unwind_table_entry __start___unwind[];
371da177e4SLinus Torvalds extern struct unwind_table_entry __stop___unwind[];
381da177e4SLinus Torvalds 
3976cffeb6SHelge Deller static DEFINE_SPINLOCK(unwind_lock);
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * the kernel unwind block is not dynamically allocated so that
421da177e4SLinus Torvalds  * we can call unwind_init as early in the bootup process as
431da177e4SLinus Torvalds  * possible (before the slab allocator is initialized)
441da177e4SLinus Torvalds  */
4547293774SHelge Deller static struct unwind_table kernel_unwind_table __ro_after_init;
461da177e4SLinus Torvalds static LIST_HEAD(unwind_tables);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds static inline const struct unwind_table_entry *
491da177e4SLinus Torvalds find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
501da177e4SLinus Torvalds {
511da177e4SLinus Torvalds 	const struct unwind_table_entry *e = NULL;
521da177e4SLinus Torvalds 	unsigned long lo, hi, mid;
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds 	lo = 0;
551da177e4SLinus Torvalds 	hi = table->length - 1;
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 	while (lo <= hi) {
581da177e4SLinus Torvalds 		mid = (hi - lo) / 2 + lo;
591da177e4SLinus Torvalds 		e = &table->table[mid];
601da177e4SLinus Torvalds 		if (addr < e->region_start)
611da177e4SLinus Torvalds 			hi = mid - 1;
621da177e4SLinus Torvalds 		else if (addr > e->region_end)
631da177e4SLinus Torvalds 			lo = mid + 1;
641da177e4SLinus Torvalds 		else
651da177e4SLinus Torvalds 			return e;
661da177e4SLinus Torvalds 	}
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return NULL;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static const struct unwind_table_entry *
721da177e4SLinus Torvalds find_unwind_entry(unsigned long addr)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	struct unwind_table *table;
751da177e4SLinus Torvalds 	const struct unwind_table_entry *e = NULL;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	if (addr >= kernel_unwind_table.start &&
781da177e4SLinus Torvalds 	    addr <= kernel_unwind_table.end)
791da177e4SLinus Torvalds 		e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
80be24a897SMikulas Patocka 	else {
81be24a897SMikulas Patocka 		unsigned long flags;
82be24a897SMikulas Patocka 
83be24a897SMikulas Patocka 		spin_lock_irqsave(&unwind_lock, flags);
841da177e4SLinus Torvalds 		list_for_each_entry(table, &unwind_tables, list) {
851da177e4SLinus Torvalds 			if (addr >= table->start &&
861da177e4SLinus Torvalds 			    addr <= table->end)
871da177e4SLinus Torvalds 				e = find_unwind_entry_in_table(table, addr);
88b1b1d4a6SPhil Carmody 			if (e) {
89b1b1d4a6SPhil Carmody 				/* Move-to-front to exploit common traces */
90b1b1d4a6SPhil Carmody 				list_move(&table->list, &unwind_tables);
911da177e4SLinus Torvalds 				break;
921da177e4SLinus Torvalds 			}
93b1b1d4a6SPhil Carmody 		}
94be24a897SMikulas Patocka 		spin_unlock_irqrestore(&unwind_lock, flags);
95be24a897SMikulas Patocka 	}
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	return e;
981da177e4SLinus Torvalds }
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static void
1011da177e4SLinus Torvalds unwind_table_init(struct unwind_table *table, const char *name,
1021da177e4SLinus Torvalds 		  unsigned long base_addr, unsigned long gp,
1031da177e4SLinus Torvalds 		  void *table_start, void *table_end)
1041da177e4SLinus Torvalds {
1051da177e4SLinus Torvalds 	struct unwind_table_entry *start = table_start;
1061da177e4SLinus Torvalds 	struct unwind_table_entry *end =
1071da177e4SLinus Torvalds 		(struct unwind_table_entry *)table_end - 1;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	table->name = name;
1101da177e4SLinus Torvalds 	table->base_addr = base_addr;
1111da177e4SLinus Torvalds 	table->gp = gp;
1121da177e4SLinus Torvalds 	table->start = base_addr + start->region_start;
1131da177e4SLinus Torvalds 	table->end = base_addr + end->region_end;
1141da177e4SLinus Torvalds 	table->table = (struct unwind_table_entry *)table_start;
1151da177e4SLinus Torvalds 	table->length = end - start + 1;
1161da177e4SLinus Torvalds 	INIT_LIST_HEAD(&table->list);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	for (; start <= end; start++) {
1191da177e4SLinus Torvalds 		if (start < end &&
1201da177e4SLinus Torvalds 		    start->region_end > (start+1)->region_start) {
121c8921d72SHelge Deller 			pr_warn("Out of order unwind entry! %px and %px\n",
122c8921d72SHelge Deller 				start, start+1);
1231da177e4SLinus Torvalds 		}
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 		start->region_start += base_addr;
1261da177e4SLinus Torvalds 		start->region_end += base_addr;
1271da177e4SLinus Torvalds 	}
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
1308f78df87SHelge Deller static int cmp_unwind_table_entry(const void *a, const void *b)
1318f78df87SHelge Deller {
1328f78df87SHelge Deller 	return ((const struct unwind_table_entry *)a)->region_start
1338f78df87SHelge Deller 	     - ((const struct unwind_table_entry *)b)->region_start;
1348f78df87SHelge Deller }
1358f78df87SHelge Deller 
1361da177e4SLinus Torvalds static void
1371da177e4SLinus Torvalds unwind_table_sort(struct unwind_table_entry *start,
1381da177e4SLinus Torvalds 		  struct unwind_table_entry *finish)
1391da177e4SLinus Torvalds {
1408f78df87SHelge Deller 	sort(start, finish - start, sizeof(struct unwind_table_entry),
1418f78df87SHelge Deller 	     cmp_unwind_table_entry, NULL);
1421da177e4SLinus Torvalds }
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds struct unwind_table *
1451da177e4SLinus Torvalds unwind_table_add(const char *name, unsigned long base_addr,
1461da177e4SLinus Torvalds 		 unsigned long gp,
1471da177e4SLinus Torvalds                  void *start, void *end)
1481da177e4SLinus Torvalds {
1491da177e4SLinus Torvalds 	struct unwind_table *table;
1501da177e4SLinus Torvalds 	unsigned long flags;
1511da177e4SLinus Torvalds 	struct unwind_table_entry *s = (struct unwind_table_entry *)start;
1521da177e4SLinus Torvalds 	struct unwind_table_entry *e = (struct unwind_table_entry *)end;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	unwind_table_sort(s, e);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	table = kmalloc(sizeof(struct unwind_table), GFP_USER);
1571da177e4SLinus Torvalds 	if (table == NULL)
1581da177e4SLinus Torvalds 		return NULL;
1591da177e4SLinus Torvalds 	unwind_table_init(table, name, base_addr, gp, start, end);
1601da177e4SLinus Torvalds 	spin_lock_irqsave(&unwind_lock, flags);
1611da177e4SLinus Torvalds 	list_add_tail(&table->list, &unwind_tables);
1621da177e4SLinus Torvalds 	spin_unlock_irqrestore(&unwind_lock, flags);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	return table;
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds void unwind_table_remove(struct unwind_table *table)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	unsigned long flags;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	spin_lock_irqsave(&unwind_lock, flags);
1721da177e4SLinus Torvalds 	list_del(&table->list);
1731da177e4SLinus Torvalds 	spin_unlock_irqrestore(&unwind_lock, flags);
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	kfree(table);
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /* Called from setup_arch to import the kernel unwind info */
179c790b41bSHelge Deller int __init unwind_init(void)
1801da177e4SLinus Torvalds {
1811da177e4SLinus Torvalds 	long start, stop;
1821da177e4SLinus Torvalds 	register unsigned long gp __asm__ ("r27");
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	start = (long)&__start___unwind[0];
1851da177e4SLinus Torvalds 	stop = (long)&__stop___unwind[0];
1861da177e4SLinus Torvalds 
18763ba82c0SHelge Deller 	dbg("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
1881da177e4SLinus Torvalds 	    start, stop,
1891da177e4SLinus Torvalds 	    (stop - start) / sizeof(struct unwind_table_entry));
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
1921da177e4SLinus Torvalds 			  gp,
1931da177e4SLinus Torvalds 			  &__start___unwind[0], &__stop___unwind[0]);
1941da177e4SLinus Torvalds #if 0
1951da177e4SLinus Torvalds 	{
1961da177e4SLinus Torvalds 		int i;
1971da177e4SLinus Torvalds 		for (i = 0; i < 10; i++)
1981da177e4SLinus Torvalds 		{
1991da177e4SLinus Torvalds 			printk("region 0x%x-0x%x\n",
2001da177e4SLinus Torvalds 				__start___unwind[i].region_start,
2011da177e4SLinus Torvalds 				__start___unwind[i].region_end);
2021da177e4SLinus Torvalds 		}
2031da177e4SLinus Torvalds 	}
2041da177e4SLinus Torvalds #endif
2051da177e4SLinus Torvalds 	return 0;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208*8e0ba125SSven Schnelle static bool pc_is_kernel_fn(unsigned long pc, void *fn)
209*8e0ba125SSven Schnelle {
210*8e0ba125SSven Schnelle 	return (unsigned long)dereference_kernel_function_descriptor(fn) == pc;
211*8e0ba125SSven Schnelle }
212*8e0ba125SSven Schnelle 
21305dc16d6SRandolph Chung static int unwind_special(struct unwind_frame_info *info, unsigned long pc, int frame_size)
21405dc16d6SRandolph Chung {
215c8921d72SHelge Deller 	/*
216c8921d72SHelge Deller 	 * We have to use void * instead of a function pointer, because
217c8921d72SHelge Deller 	 * function pointers aren't a pointer to the function on 64-bit.
218c8921d72SHelge Deller 	 * Make them const so the compiler knows they live in .text
2198801ccb9SHelge Deller 	 * Note: We could use dereference_kernel_function_descriptor()
2208801ccb9SHelge Deller 	 * instead but we want to keep it simple here.
221c8921d72SHelge Deller 	 */
222c8921d72SHelge Deller 	extern void * const handle_interruption;
223c8921d72SHelge Deller 	extern void * const ret_from_kernel_thread;
224c8921d72SHelge Deller 	extern void * const syscall_exit;
225c8921d72SHelge Deller 	extern void * const intr_return;
226c8921d72SHelge Deller 	extern void * const _switch_to_ret;
227c8921d72SHelge Deller #ifdef CONFIG_IRQSTACKS
2288801ccb9SHelge Deller 	extern void * const _call_on_stack;
229c8921d72SHelge Deller #endif /* CONFIG_IRQSTACKS */
23005dc16d6SRandolph Chung 
231*8e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, handle_interruption)) {
23205dc16d6SRandolph Chung 		struct pt_regs *regs = (struct pt_regs *)(info->sp - frame_size - PT_SZ_ALGN);
23305dc16d6SRandolph Chung 		dbg("Unwinding through handle_interruption()\n");
23405dc16d6SRandolph Chung 		info->prev_sp = regs->gr[30];
23505dc16d6SRandolph Chung 		info->prev_ip = regs->iaoq[0];
23605dc16d6SRandolph Chung 		return 1;
23705dc16d6SRandolph Chung 	}
23805dc16d6SRandolph Chung 
239*8e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, ret_from_kernel_thread) ||
240*8e0ba125SSven Schnelle 	    pc_is_kernel_fn(pc, syscall_exit)) {
241c8921d72SHelge Deller 		info->prev_sp = info->prev_ip = 0;
242c8921d72SHelge Deller 		return 1;
243c8921d72SHelge Deller 	}
244c8921d72SHelge Deller 
245*8e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, intr_return)) {
246c8921d72SHelge Deller 		struct pt_regs *regs;
247c8921d72SHelge Deller 
248c8921d72SHelge Deller 		dbg("Found intr_return()\n");
249c8921d72SHelge Deller 		regs = (struct pt_regs *)(info->sp - PT_SZ_ALGN);
250c8921d72SHelge Deller 		info->prev_sp = regs->gr[30];
251c8921d72SHelge Deller 		info->prev_ip = regs->iaoq[0];
252c8921d72SHelge Deller 		info->rp = regs->gr[2];
253c8921d72SHelge Deller 		return 1;
254c8921d72SHelge Deller 	}
255c8921d72SHelge Deller 
256*8e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, _switch_to) ||
257*8e0ba125SSven Schnelle 	    pc_is_kernel_fn(pc, _switch_to_ret)) {
258c8921d72SHelge Deller 		info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
259c8921d72SHelge Deller 		info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
260c8921d72SHelge Deller 		return 1;
261c8921d72SHelge Deller 	}
262c8921d72SHelge Deller 
263c8921d72SHelge Deller #ifdef CONFIG_IRQSTACKS
264*8e0ba125SSven Schnelle 	if (pc_is_kernel_fn(pc, _call_on_stack)) {
265c8921d72SHelge Deller 		info->prev_sp = *(unsigned long *)(info->sp - FRAME_SIZE - REG_SZ);
266c8921d72SHelge Deller 		info->prev_ip = *(unsigned long *)(info->sp - FRAME_SIZE - RP_OFFSET);
267c8921d72SHelge Deller 		return 1;
268c8921d72SHelge Deller 	}
269c8921d72SHelge Deller #endif
27005dc16d6SRandolph Chung 	return 0;
27105dc16d6SRandolph Chung }
27205dc16d6SRandolph Chung 
2731da177e4SLinus Torvalds static void unwind_frame_regs(struct unwind_frame_info *info)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	const struct unwind_table_entry *e;
2761da177e4SLinus Torvalds 	unsigned long npc;
2771da177e4SLinus Torvalds 	unsigned int insn;
2781da177e4SLinus Torvalds 	long frame_size = 0;
2791da177e4SLinus Torvalds 	int looking_for_rp, rpoffset = 0;
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	e = find_unwind_entry(info->ip);
2821da177e4SLinus Torvalds 	if (e == NULL) {
2831da177e4SLinus Torvalds 		unsigned long sp;
2841da177e4SLinus Torvalds 
285c8921d72SHelge Deller 		dbg("Cannot find unwind entry for %pS; forced unwinding\n",
286c8921d72SHelge Deller 			(void *) info->ip);
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 		/* Since we are doing the unwinding blind, we don't know if
2891da177e4SLinus Torvalds 		   we are adjusting the stack correctly or extracting the rp
2901da177e4SLinus Torvalds 		   correctly. The rp is checked to see if it belongs to the
2911da177e4SLinus Torvalds 		   kernel text section, if not we assume we don't have a
2921da177e4SLinus Torvalds 		   correct stack frame and we continue to unwind the stack.
2931da177e4SLinus Torvalds 		   This is not quite correct, and will fail for loadable
2941da177e4SLinus Torvalds 		   modules. */
2951da177e4SLinus Torvalds 		sp = info->sp & ~63;
2961da177e4SLinus Torvalds 		do {
2971da177e4SLinus Torvalds 			unsigned long tmp;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 			info->prev_sp = sp - 64;
3001da177e4SLinus Torvalds 			info->prev_ip = 0;
301e77900abSHelge Deller 
302e77900abSHelge Deller 			/* The stack is at the end inside the thread_union
303e77900abSHelge Deller 			 * struct. If we reach data, we have reached the
304e77900abSHelge Deller 			 * beginning of the stack and should stop unwinding. */
305e77900abSHelge Deller 			if (info->prev_sp >= (unsigned long) task_thread_info(info->t) &&
306e77900abSHelge Deller 			    info->prev_sp < ((unsigned long) task_thread_info(info->t)
307e77900abSHelge Deller 						+ THREAD_SZ_ALGN)) {
308e77900abSHelge Deller 				info->prev_sp = 0;
309e77900abSHelge Deller 				break;
310e77900abSHelge Deller 			}
311e77900abSHelge Deller 
312cf2ec789SSven Schnelle 			if (copy_from_kernel_nofault(&tmp,
313cf2ec789SSven Schnelle 			    (void *)info->prev_sp - RP_OFFSET, sizeof(tmp)))
3141da177e4SLinus Torvalds 				break;
3151da177e4SLinus Torvalds 			info->prev_ip = tmp;
3161da177e4SLinus Torvalds 			sp = info->prev_sp;
317c790b41bSHelge Deller 		} while (!kernel_text_address(info->prev_ip));
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 		info->rp = 0;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 		dbg("analyzing func @ %lx with no unwind info, setting "
3221da177e4SLinus Torvalds 		    "prev_sp=%lx prev_ip=%lx\n", info->ip,
3231da177e4SLinus Torvalds 		    info->prev_sp, info->prev_ip);
3241da177e4SLinus Torvalds 	} else {
3251da177e4SLinus Torvalds 		dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
3261da177e4SLinus Torvalds 		    "Save_RP = %d, Millicode = %d size = %u\n",
3271da177e4SLinus Torvalds 		    e->region_start, e->region_end, e->Save_SP, e->Save_RP,
3281da177e4SLinus Torvalds 		    e->Millicode, e->Total_frame_size);
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 		looking_for_rp = e->Save_RP;
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 		for (npc = e->region_start;
3331da177e4SLinus Torvalds 		     (frame_size < (e->Total_frame_size << 3) ||
3341da177e4SLinus Torvalds 		      looking_for_rp) &&
3351da177e4SLinus Torvalds 		     npc < info->ip;
3361da177e4SLinus Torvalds 		     npc += 4) {
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 			insn = *(unsigned int *)npc;
3391da177e4SLinus Torvalds 
340be24a897SMikulas Patocka 			if ((insn & 0xffffc001) == 0x37de0000 ||
341be24a897SMikulas Patocka 			    (insn & 0xffe00001) == 0x6fc00000) {
3421da177e4SLinus Torvalds 				/* ldo X(sp), sp, or stwm X,D(sp) */
343be24a897SMikulas Patocka 				frame_size += (insn & 0x3fff) >> 1;
3441da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=%08x @ "
3451da177e4SLinus Torvalds 				    "%lx, frame_size = %ld\n", info->ip,
3461da177e4SLinus Torvalds 				    insn, npc, frame_size);
347be24a897SMikulas Patocka 			} else if ((insn & 0xffe00009) == 0x73c00008) {
3481da177e4SLinus Torvalds 				/* std,ma X,D(sp) */
349be24a897SMikulas Patocka 				frame_size += ((insn >> 4) & 0x3ff) << 3;
3501da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=%08x @ "
3511da177e4SLinus Torvalds 				    "%lx, frame_size = %ld\n", info->ip,
3521da177e4SLinus Torvalds 				    insn, npc, frame_size);
3531da177e4SLinus Torvalds 			} else if (insn == 0x6bc23fd9) {
3541da177e4SLinus Torvalds 				/* stw rp,-20(sp) */
3551da177e4SLinus Torvalds 				rpoffset = 20;
3561da177e4SLinus Torvalds 				looking_for_rp = 0;
3571da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=stw rp,"
3581da177e4SLinus Torvalds 				    "-20(sp) @ %lx\n", info->ip, npc);
3591da177e4SLinus Torvalds 			} else if (insn == 0x0fc212c1) {
3601da177e4SLinus Torvalds 				/* std rp,-16(sr0,sp) */
3611da177e4SLinus Torvalds 				rpoffset = 16;
3621da177e4SLinus Torvalds 				looking_for_rp = 0;
3631da177e4SLinus Torvalds 				dbg("analyzing func @ %lx, insn=std rp,"
3641da177e4SLinus Torvalds 				    "-16(sp) @ %lx\n", info->ip, npc);
3651da177e4SLinus Torvalds 			}
3661da177e4SLinus Torvalds 		}
3671da177e4SLinus Torvalds 
368be24a897SMikulas Patocka 		if (frame_size > e->Total_frame_size << 3)
369be24a897SMikulas Patocka 			frame_size = e->Total_frame_size << 3;
370be24a897SMikulas Patocka 
37105dc16d6SRandolph Chung 		if (!unwind_special(info, e->region_start, frame_size)) {
3721da177e4SLinus Torvalds 			info->prev_sp = info->sp - frame_size;
3731da177e4SLinus Torvalds 			if (e->Millicode)
3741da177e4SLinus Torvalds 				info->rp = info->r31;
3751da177e4SLinus Torvalds 			else if (rpoffset)
3761da177e4SLinus Torvalds 				info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
3771da177e4SLinus Torvalds 			info->prev_ip = info->rp;
3781da177e4SLinus Torvalds 			info->rp = 0;
37905dc16d6SRandolph Chung 		}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 		dbg("analyzing func @ %lx, setting prev_sp=%lx "
3821da177e4SLinus Torvalds 		    "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
3831da177e4SLinus Torvalds 		    info->prev_ip, npc);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
3881da177e4SLinus Torvalds 		       struct pt_regs *regs)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	memset(info, 0, sizeof(struct unwind_frame_info));
3911da177e4SLinus Torvalds 	info->t = t;
3921da177e4SLinus Torvalds 	info->sp = regs->gr[30];
3931da177e4SLinus Torvalds 	info->ip = regs->iaoq[0];
3941da177e4SLinus Torvalds 	info->rp = regs->gr[2];
3951da177e4SLinus Torvalds 	info->r31 = regs->gr[31];
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
3981da177e4SLinus Torvalds 	    t ? (int)t->pid : -1, info->sp, info->ip);
3991da177e4SLinus Torvalds }
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
4021da177e4SLinus Torvalds {
4031da177e4SLinus Torvalds 	struct pt_regs *r = &t->thread.regs;
4041da177e4SLinus Torvalds 	struct pt_regs *r2;
4051da177e4SLinus Torvalds 
406e0e7ed48SHelge Deller 	r2 = kmalloc(sizeof(struct pt_regs), GFP_ATOMIC);
4071da177e4SLinus Torvalds 	if (!r2)
4081da177e4SLinus Torvalds 		return;
4091da177e4SLinus Torvalds 	*r2 = *r;
4101da177e4SLinus Torvalds 	r2->gr[30] = r->ksp;
4111da177e4SLinus Torvalds 	r2->iaoq[0] = r->kpc;
4121da177e4SLinus Torvalds 	unwind_frame_init(info, t, r2);
4131da177e4SLinus Torvalds 	kfree(r2);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4169e0d5c45SHelge Deller #define get_parisc_stackpointer() ({ \
4179e0d5c45SHelge Deller 	unsigned long sp; \
4189e0d5c45SHelge Deller 	__asm__("copy %%r30, %0" : "=r"(sp)); \
4199e0d5c45SHelge Deller 	(sp); \
4209e0d5c45SHelge Deller })
4219e0d5c45SHelge Deller 
4229e0d5c45SHelge Deller void unwind_frame_init_task(struct unwind_frame_info *info,
4239e0d5c45SHelge Deller 	struct task_struct *task, struct pt_regs *regs)
4241da177e4SLinus Torvalds {
4259e0d5c45SHelge Deller 	task = task ? task : current;
4269e0d5c45SHelge Deller 
4279e0d5c45SHelge Deller 	if (task == current) {
4289e0d5c45SHelge Deller 		struct pt_regs r;
4299e0d5c45SHelge Deller 
4309e0d5c45SHelge Deller 		if (!regs) {
4319e0d5c45SHelge Deller 			memset(&r, 0, sizeof(r));
4329e0d5c45SHelge Deller 			r.iaoq[0] =  _THIS_IP_;
4339e0d5c45SHelge Deller 			r.gr[2] = _RET_IP_;
4349e0d5c45SHelge Deller 			r.gr[30] = get_parisc_stackpointer();
4359e0d5c45SHelge Deller 			regs = &r;
4369e0d5c45SHelge Deller 		}
437cf8afe5cSHelge Deller 		unwind_frame_init(info, task, regs);
4389e0d5c45SHelge Deller 	} else {
4399e0d5c45SHelge Deller 		unwind_frame_init_from_blocked_task(info, task);
4409e0d5c45SHelge Deller 	}
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds int unwind_once(struct unwind_frame_info *next_frame)
4441da177e4SLinus Torvalds {
4451da177e4SLinus Torvalds 	unwind_frame_regs(next_frame);
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (next_frame->prev_sp == 0 ||
4481da177e4SLinus Torvalds 	    next_frame->prev_ip == 0)
4491da177e4SLinus Torvalds 		return -1;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	next_frame->sp = next_frame->prev_sp;
4521da177e4SLinus Torvalds 	next_frame->ip = next_frame->prev_ip;
4531da177e4SLinus Torvalds 	next_frame->prev_sp = 0;
4541da177e4SLinus Torvalds 	next_frame->prev_ip = 0;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
4571da177e4SLinus Torvalds 	    next_frame->t ? (int)next_frame->t->pid : -1,
4581da177e4SLinus Torvalds 	    next_frame->sp, next_frame->ip);
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	return 0;
4611da177e4SLinus Torvalds }
4621da177e4SLinus Torvalds 
4631da177e4SLinus Torvalds int unwind_to_user(struct unwind_frame_info *info)
4641da177e4SLinus Torvalds {
4651da177e4SLinus Torvalds 	int ret;
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds 	do {
4681da177e4SLinus Torvalds 		ret = unwind_once(info);
4691da177e4SLinus Torvalds 	} while (!ret && !(info->ip & 3));
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds 	return ret;
4721da177e4SLinus Torvalds }
47311e17809SHelge Deller 
47411e17809SHelge Deller unsigned long return_address(unsigned int level)
47511e17809SHelge Deller {
47611e17809SHelge Deller 	struct unwind_frame_info info;
47711e17809SHelge Deller 
47811e17809SHelge Deller 	/* initialize unwind info */
4799e0d5c45SHelge Deller 	unwind_frame_init_task(&info, current, NULL);
48011e17809SHelge Deller 
48111e17809SHelge Deller 	/* unwind stack */
4829e0d5c45SHelge Deller 	level += 2;
48311e17809SHelge Deller 	do {
48411e17809SHelge Deller 		if (unwind_once(&info) < 0 || info.ip == 0)
48511e17809SHelge Deller 			return 0;
486c790b41bSHelge Deller 		if (!kernel_text_address(info.ip))
48711e17809SHelge Deller 			return 0;
48811e17809SHelge Deller 	} while (info.ip && level--);
48911e17809SHelge Deller 
49011e17809SHelge Deller 	return info.ip;
49111e17809SHelge Deller }
492