18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 1999-2004 Hewlett-Packard Co
48c2ecf20Sopenharmony_ci *	David Mosberger-Tang <davidm@hpl.hp.com>
58c2ecf20Sopenharmony_ci * Copyright (C) 2003 Fenghua Yu <fenghua.yu@intel.com>
68c2ecf20Sopenharmony_ci * 	- Change pt_regs_off() to make it less dependent on pt_regs structure.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * This file implements call frame unwind support for the Linux
108c2ecf20Sopenharmony_ci * kernel.  Parsing and processing the unwind information is
118c2ecf20Sopenharmony_ci * time-consuming, so this implementation translates the unwind
128c2ecf20Sopenharmony_ci * descriptors into unwind scripts.  These scripts are very simple
138c2ecf20Sopenharmony_ci * (basically a sequence of assignments) and efficient to execute.
148c2ecf20Sopenharmony_ci * They are cached for later re-use.  Each script is specific for a
158c2ecf20Sopenharmony_ci * given instruction pointer address and the set of predicate values
168c2ecf20Sopenharmony_ci * that the script depends on (most unwind descriptors are
178c2ecf20Sopenharmony_ci * unconditional and scripts often do not depend on predicates at
188c2ecf20Sopenharmony_ci * all).  This code is based on the unwind conventions described in
198c2ecf20Sopenharmony_ci * the "IA-64 Software Conventions and Runtime Architecture" manual.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * SMP conventions:
228c2ecf20Sopenharmony_ci *	o updates to the global unwind data (in structure "unw") are serialized
238c2ecf20Sopenharmony_ci *	  by the unw.lock spinlock
248c2ecf20Sopenharmony_ci *	o each unwind script has its own read-write lock; a thread must acquire
258c2ecf20Sopenharmony_ci *	  a read lock before executing a script and must acquire a write lock
268c2ecf20Sopenharmony_ci *	  before modifying a script
278c2ecf20Sopenharmony_ci *	o if both the unw.lock spinlock and a script's read-write lock must be
288c2ecf20Sopenharmony_ci *	  acquired, then the read-write lock must be acquired first.
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci#include <linux/module.h>
318c2ecf20Sopenharmony_ci#include <linux/memblock.h>
328c2ecf20Sopenharmony_ci#include <linux/elf.h>
338c2ecf20Sopenharmony_ci#include <linux/kernel.h>
348c2ecf20Sopenharmony_ci#include <linux/sched.h>
358c2ecf20Sopenharmony_ci#include <linux/slab.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#include <asm/unwind.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include <asm/delay.h>
408c2ecf20Sopenharmony_ci#include <asm/page.h>
418c2ecf20Sopenharmony_ci#include <asm/ptrace.h>
428c2ecf20Sopenharmony_ci#include <asm/ptrace_offsets.h>
438c2ecf20Sopenharmony_ci#include <asm/rse.h>
448c2ecf20Sopenharmony_ci#include <asm/sections.h>
458c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#include "entry.h"
488c2ecf20Sopenharmony_ci#include "unwind_i.h"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define UNW_LOG_CACHE_SIZE	7	/* each unw_script is ~256 bytes in size */
518c2ecf20Sopenharmony_ci#define UNW_CACHE_SIZE		(1 << UNW_LOG_CACHE_SIZE)
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci#define UNW_LOG_HASH_SIZE	(UNW_LOG_CACHE_SIZE + 1)
548c2ecf20Sopenharmony_ci#define UNW_HASH_SIZE		(1 << UNW_LOG_HASH_SIZE)
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci#define UNW_STATS	0	/* WARNING: this disabled interrupts for long time-spans!! */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#ifdef UNW_DEBUG
598c2ecf20Sopenharmony_ci  static unsigned int unw_debug_level = UNW_DEBUG;
608c2ecf20Sopenharmony_ci#  define UNW_DEBUG_ON(n)	unw_debug_level >= n
618c2ecf20Sopenharmony_ci   /* Do not code a printk level, not all debug lines end in newline */
628c2ecf20Sopenharmony_ci#  define UNW_DPRINT(n, ...)  if (UNW_DEBUG_ON(n)) printk(__VA_ARGS__)
638c2ecf20Sopenharmony_ci#  undef inline
648c2ecf20Sopenharmony_ci#  define inline
658c2ecf20Sopenharmony_ci#else /* !UNW_DEBUG */
668c2ecf20Sopenharmony_ci#  define UNW_DEBUG_ON(n)  0
678c2ecf20Sopenharmony_ci#  define UNW_DPRINT(n, ...)
688c2ecf20Sopenharmony_ci#endif /* UNW_DEBUG */
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#if UNW_STATS
718c2ecf20Sopenharmony_ci# define STAT(x...)	x
728c2ecf20Sopenharmony_ci#else
738c2ecf20Sopenharmony_ci# define STAT(x...)
748c2ecf20Sopenharmony_ci#endif
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define alloc_reg_state()	kmalloc(sizeof(struct unw_reg_state), GFP_ATOMIC)
778c2ecf20Sopenharmony_ci#define free_reg_state(usr)	kfree(usr)
788c2ecf20Sopenharmony_ci#define alloc_labeled_state()	kmalloc(sizeof(struct unw_labeled_state), GFP_ATOMIC)
798c2ecf20Sopenharmony_ci#define free_labeled_state(usr)	kfree(usr)
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_citypedef unsigned long unw_word;
828c2ecf20Sopenharmony_citypedef unsigned char unw_hash_index_t;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic struct {
858c2ecf20Sopenharmony_ci	spinlock_t lock;			/* spinlock for unwind data */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/* list of unwind tables (one per load-module) */
888c2ecf20Sopenharmony_ci	struct unw_table *tables;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	unsigned long r0;			/* constant 0 for r0 */
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	/* table of registers that prologues can save (and order in which they're saved): */
938c2ecf20Sopenharmony_ci	const unsigned char save_order[8];
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/* maps a preserved register index (preg_index) to corresponding switch_stack offset: */
968c2ecf20Sopenharmony_ci	unsigned short sw_off[sizeof(struct unw_frame_info) / 8];
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	unsigned short lru_head;		/* index of lead-recently used script */
998c2ecf20Sopenharmony_ci	unsigned short lru_tail;		/* index of most-recently used script */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	/* index into unw_frame_info for preserved register i */
1028c2ecf20Sopenharmony_ci	unsigned short preg_index[UNW_NUM_REGS];
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	short pt_regs_offsets[32];
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	/* unwind table for the kernel: */
1078c2ecf20Sopenharmony_ci	struct unw_table kernel_table;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* unwind table describing the gate page (kernel code that is mapped into user space): */
1108c2ecf20Sopenharmony_ci	size_t gate_table_size;
1118c2ecf20Sopenharmony_ci	unsigned long *gate_table;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	/* hash table that maps instruction pointer to script index: */
1148c2ecf20Sopenharmony_ci	unsigned short hash[UNW_HASH_SIZE];
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	/* script cache: */
1178c2ecf20Sopenharmony_ci	struct unw_script cache[UNW_CACHE_SIZE];
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci# ifdef UNW_DEBUG
1208c2ecf20Sopenharmony_ci	const char *preg_name[UNW_NUM_REGS];
1218c2ecf20Sopenharmony_ci# endif
1228c2ecf20Sopenharmony_ci# if UNW_STATS
1238c2ecf20Sopenharmony_ci	struct {
1248c2ecf20Sopenharmony_ci		struct {
1258c2ecf20Sopenharmony_ci			int lookups;
1268c2ecf20Sopenharmony_ci			int hinted_hits;
1278c2ecf20Sopenharmony_ci			int normal_hits;
1288c2ecf20Sopenharmony_ci			int collision_chain_traversals;
1298c2ecf20Sopenharmony_ci		} cache;
1308c2ecf20Sopenharmony_ci		struct {
1318c2ecf20Sopenharmony_ci			unsigned long build_time;
1328c2ecf20Sopenharmony_ci			unsigned long run_time;
1338c2ecf20Sopenharmony_ci			unsigned long parse_time;
1348c2ecf20Sopenharmony_ci			int builds;
1358c2ecf20Sopenharmony_ci			int news;
1368c2ecf20Sopenharmony_ci			int collisions;
1378c2ecf20Sopenharmony_ci			int runs;
1388c2ecf20Sopenharmony_ci		} script;
1398c2ecf20Sopenharmony_ci		struct {
1408c2ecf20Sopenharmony_ci			unsigned long init_time;
1418c2ecf20Sopenharmony_ci			unsigned long unwind_time;
1428c2ecf20Sopenharmony_ci			int inits;
1438c2ecf20Sopenharmony_ci			int unwinds;
1448c2ecf20Sopenharmony_ci		} api;
1458c2ecf20Sopenharmony_ci	} stat;
1468c2ecf20Sopenharmony_ci# endif
1478c2ecf20Sopenharmony_ci} unw = {
1488c2ecf20Sopenharmony_ci	.tables = &unw.kernel_table,
1498c2ecf20Sopenharmony_ci	.lock = __SPIN_LOCK_UNLOCKED(unw.lock),
1508c2ecf20Sopenharmony_ci	.save_order = {
1518c2ecf20Sopenharmony_ci		UNW_REG_RP, UNW_REG_PFS, UNW_REG_PSP, UNW_REG_PR,
1528c2ecf20Sopenharmony_ci		UNW_REG_UNAT, UNW_REG_LC, UNW_REG_FPSR, UNW_REG_PRI_UNAT_GR
1538c2ecf20Sopenharmony_ci	},
1548c2ecf20Sopenharmony_ci	.preg_index = {
1558c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, pri_unat_loc)/8,	/* PRI_UNAT_GR */
1568c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, pri_unat_loc)/8,	/* PRI_UNAT_MEM */
1578c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, bsp_loc)/8,
1588c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, bspstore_loc)/8,
1598c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, pfs_loc)/8,
1608c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, rnat_loc)/8,
1618c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, psp)/8,
1628c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, rp_loc)/8,
1638c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, r4)/8,
1648c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, r5)/8,
1658c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, r6)/8,
1668c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, r7)/8,
1678c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, unat_loc)/8,
1688c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, pr_loc)/8,
1698c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, lc_loc)/8,
1708c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fpsr_loc)/8,
1718c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, b1_loc)/8,
1728c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, b2_loc)/8,
1738c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, b3_loc)/8,
1748c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, b4_loc)/8,
1758c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, b5_loc)/8,
1768c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, f2_loc)/8,
1778c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, f3_loc)/8,
1788c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, f4_loc)/8,
1798c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, f5_loc)/8,
1808c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[16 - 16])/8,
1818c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[17 - 16])/8,
1828c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[18 - 16])/8,
1838c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[19 - 16])/8,
1848c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[20 - 16])/8,
1858c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[21 - 16])/8,
1868c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[22 - 16])/8,
1878c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[23 - 16])/8,
1888c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[24 - 16])/8,
1898c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[25 - 16])/8,
1908c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[26 - 16])/8,
1918c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[27 - 16])/8,
1928c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[28 - 16])/8,
1938c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[29 - 16])/8,
1948c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[30 - 16])/8,
1958c2ecf20Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[31 - 16])/8,
1968c2ecf20Sopenharmony_ci	},
1978c2ecf20Sopenharmony_ci	.pt_regs_offsets = {
1988c2ecf20Sopenharmony_ci		[0] = -1,
1998c2ecf20Sopenharmony_ci		offsetof(struct pt_regs,  r1),
2008c2ecf20Sopenharmony_ci		offsetof(struct pt_regs,  r2),
2018c2ecf20Sopenharmony_ci		offsetof(struct pt_regs,  r3),
2028c2ecf20Sopenharmony_ci		[4] = -1, [5] = -1, [6] = -1, [7] = -1,
2038c2ecf20Sopenharmony_ci		offsetof(struct pt_regs,  r8),
2048c2ecf20Sopenharmony_ci		offsetof(struct pt_regs,  r9),
2058c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r10),
2068c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r11),
2078c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r12),
2088c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r13),
2098c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r14),
2108c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r15),
2118c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r16),
2128c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r17),
2138c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r18),
2148c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r19),
2158c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r20),
2168c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r21),
2178c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r22),
2188c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r23),
2198c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r24),
2208c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r25),
2218c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r26),
2228c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r27),
2238c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r28),
2248c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r29),
2258c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r30),
2268c2ecf20Sopenharmony_ci		offsetof(struct pt_regs, r31),
2278c2ecf20Sopenharmony_ci	},
2288c2ecf20Sopenharmony_ci	.hash = { [0 ... UNW_HASH_SIZE - 1] = -1 },
2298c2ecf20Sopenharmony_ci#ifdef UNW_DEBUG
2308c2ecf20Sopenharmony_ci	.preg_name = {
2318c2ecf20Sopenharmony_ci		"pri_unat_gr", "pri_unat_mem", "bsp", "bspstore", "ar.pfs", "ar.rnat", "psp", "rp",
2328c2ecf20Sopenharmony_ci		"r4", "r5", "r6", "r7",
2338c2ecf20Sopenharmony_ci		"ar.unat", "pr", "ar.lc", "ar.fpsr",
2348c2ecf20Sopenharmony_ci		"b1", "b2", "b3", "b4", "b5",
2358c2ecf20Sopenharmony_ci		"f2", "f3", "f4", "f5",
2368c2ecf20Sopenharmony_ci		"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
2378c2ecf20Sopenharmony_ci		"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci#endif
2408c2ecf20Sopenharmony_ci};
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic inline int
2438c2ecf20Sopenharmony_ciread_only (void *addr)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	return (unsigned long) ((char *) addr - (char *) &unw.r0) < sizeof(unw.r0);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/*
2498c2ecf20Sopenharmony_ci * Returns offset of rREG in struct pt_regs.
2508c2ecf20Sopenharmony_ci */
2518c2ecf20Sopenharmony_cistatic inline unsigned long
2528c2ecf20Sopenharmony_cipt_regs_off (unsigned long reg)
2538c2ecf20Sopenharmony_ci{
2548c2ecf20Sopenharmony_ci	short off = -1;
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (reg < ARRAY_SIZE(unw.pt_regs_offsets))
2578c2ecf20Sopenharmony_ci		off = unw.pt_regs_offsets[reg];
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	if (off < 0) {
2608c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bad scratch reg r%lu\n", __func__, reg);
2618c2ecf20Sopenharmony_ci		off = 0;
2628c2ecf20Sopenharmony_ci	}
2638c2ecf20Sopenharmony_ci	return (unsigned long) off;
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic inline struct pt_regs *
2678c2ecf20Sopenharmony_ciget_scratch_regs (struct unw_frame_info *info)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	if (!info->pt) {
2708c2ecf20Sopenharmony_ci		/* This should not happen with valid unwind info.  */
2718c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bad unwind info: resetting info->pt\n", __func__);
2728c2ecf20Sopenharmony_ci		if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
2738c2ecf20Sopenharmony_ci			info->pt = (unsigned long) ((struct pt_regs *) info->psp - 1);
2748c2ecf20Sopenharmony_ci		else
2758c2ecf20Sopenharmony_ci			info->pt = info->sp - 16;
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s: sp 0x%lx pt 0x%lx\n", __func__, info->sp, info->pt);
2788c2ecf20Sopenharmony_ci	return (struct pt_regs *) info->pt;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci/* Unwind accessors.  */
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ciint
2848c2ecf20Sopenharmony_ciunw_access_gr (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	unsigned long *addr, *nat_addr, nat_mask = 0, dummy_nat;
2878c2ecf20Sopenharmony_ci	struct unw_ireg *ireg;
2888c2ecf20Sopenharmony_ci	struct pt_regs *pt;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if ((unsigned) regnum - 1 >= 127) {
2918c2ecf20Sopenharmony_ci		if (regnum == 0 && !write) {
2928c2ecf20Sopenharmony_ci			*val = 0;	/* read r0 always returns 0 */
2938c2ecf20Sopenharmony_ci			*nat = 0;
2948c2ecf20Sopenharmony_ci			return 0;
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent r%u\n",
2978c2ecf20Sopenharmony_ci			   __func__, regnum);
2988c2ecf20Sopenharmony_ci		return -1;
2998c2ecf20Sopenharmony_ci	}
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	if (regnum < 32) {
3028c2ecf20Sopenharmony_ci		if (regnum >= 4 && regnum <= 7) {
3038c2ecf20Sopenharmony_ci			/* access a preserved register */
3048c2ecf20Sopenharmony_ci			ireg = &info->r4 + (regnum - 4);
3058c2ecf20Sopenharmony_ci			addr = ireg->loc;
3068c2ecf20Sopenharmony_ci			if (addr) {
3078c2ecf20Sopenharmony_ci				nat_addr = addr + ireg->nat.off;
3088c2ecf20Sopenharmony_ci				switch (ireg->nat.type) {
3098c2ecf20Sopenharmony_ci				      case UNW_NAT_VAL:
3108c2ecf20Sopenharmony_ci					/* simulate getf.sig/setf.sig */
3118c2ecf20Sopenharmony_ci					if (write) {
3128c2ecf20Sopenharmony_ci						if (*nat) {
3138c2ecf20Sopenharmony_ci							/* write NaTVal and be done with it */
3148c2ecf20Sopenharmony_ci							addr[0] = 0;
3158c2ecf20Sopenharmony_ci							addr[1] = 0x1fffe;
3168c2ecf20Sopenharmony_ci							return 0;
3178c2ecf20Sopenharmony_ci						}
3188c2ecf20Sopenharmony_ci						addr[1] = 0x1003e;
3198c2ecf20Sopenharmony_ci					} else {
3208c2ecf20Sopenharmony_ci						if (addr[0] == 0 && addr[1] == 0x1ffe) {
3218c2ecf20Sopenharmony_ci							/* return NaT and be done with it */
3228c2ecf20Sopenharmony_ci							*val = 0;
3238c2ecf20Sopenharmony_ci							*nat = 1;
3248c2ecf20Sopenharmony_ci							return 0;
3258c2ecf20Sopenharmony_ci						}
3268c2ecf20Sopenharmony_ci					}
3278c2ecf20Sopenharmony_ci					fallthrough;
3288c2ecf20Sopenharmony_ci				      case UNW_NAT_NONE:
3298c2ecf20Sopenharmony_ci					dummy_nat = 0;
3308c2ecf20Sopenharmony_ci					nat_addr = &dummy_nat;
3318c2ecf20Sopenharmony_ci					break;
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci				      case UNW_NAT_MEMSTK:
3348c2ecf20Sopenharmony_ci					nat_mask = (1UL << ((long) addr & 0x1f8)/8);
3358c2ecf20Sopenharmony_ci					break;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci				      case UNW_NAT_REGSTK:
3388c2ecf20Sopenharmony_ci					nat_addr = ia64_rse_rnat_addr(addr);
3398c2ecf20Sopenharmony_ci					if ((unsigned long) addr < info->regstk.limit
3408c2ecf20Sopenharmony_ci					    || (unsigned long) addr >= info->regstk.top)
3418c2ecf20Sopenharmony_ci					{
3428c2ecf20Sopenharmony_ci						UNW_DPRINT(0, "unwind.%s: %p outside of regstk "
3438c2ecf20Sopenharmony_ci							"[0x%lx-0x%lx)\n",
3448c2ecf20Sopenharmony_ci							__func__, (void *) addr,
3458c2ecf20Sopenharmony_ci							info->regstk.limit,
3468c2ecf20Sopenharmony_ci							info->regstk.top);
3478c2ecf20Sopenharmony_ci						return -1;
3488c2ecf20Sopenharmony_ci					}
3498c2ecf20Sopenharmony_ci					if ((unsigned long) nat_addr >= info->regstk.top)
3508c2ecf20Sopenharmony_ci						nat_addr = &info->sw->ar_rnat;
3518c2ecf20Sopenharmony_ci					nat_mask = (1UL << ia64_rse_slot_num(addr));
3528c2ecf20Sopenharmony_ci					break;
3538c2ecf20Sopenharmony_ci				}
3548c2ecf20Sopenharmony_ci			} else {
3558c2ecf20Sopenharmony_ci				addr = &info->sw->r4 + (regnum - 4);
3568c2ecf20Sopenharmony_ci				nat_addr = &info->sw->ar_unat;
3578c2ecf20Sopenharmony_ci				nat_mask = (1UL << ((long) addr & 0x1f8)/8);
3588c2ecf20Sopenharmony_ci			}
3598c2ecf20Sopenharmony_ci		} else {
3608c2ecf20Sopenharmony_ci			/* access a scratch register */
3618c2ecf20Sopenharmony_ci			pt = get_scratch_regs(info);
3628c2ecf20Sopenharmony_ci			addr = (unsigned long *) ((unsigned long)pt + pt_regs_off(regnum));
3638c2ecf20Sopenharmony_ci			if (info->pri_unat_loc)
3648c2ecf20Sopenharmony_ci				nat_addr = info->pri_unat_loc;
3658c2ecf20Sopenharmony_ci			else
3668c2ecf20Sopenharmony_ci				nat_addr = &info->sw->caller_unat;
3678c2ecf20Sopenharmony_ci			nat_mask = (1UL << ((long) addr & 0x1f8)/8);
3688c2ecf20Sopenharmony_ci		}
3698c2ecf20Sopenharmony_ci	} else {
3708c2ecf20Sopenharmony_ci		/* access a stacked register */
3718c2ecf20Sopenharmony_ci		addr = ia64_rse_skip_regs((unsigned long *) info->bsp, regnum - 32);
3728c2ecf20Sopenharmony_ci		nat_addr = ia64_rse_rnat_addr(addr);
3738c2ecf20Sopenharmony_ci		if ((unsigned long) addr < info->regstk.limit
3748c2ecf20Sopenharmony_ci		    || (unsigned long) addr >= info->regstk.top)
3758c2ecf20Sopenharmony_ci		{
3768c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to access register outside "
3778c2ecf20Sopenharmony_ci				   "of rbs\n",  __func__);
3788c2ecf20Sopenharmony_ci			return -1;
3798c2ecf20Sopenharmony_ci		}
3808c2ecf20Sopenharmony_ci		if ((unsigned long) nat_addr >= info->regstk.top)
3818c2ecf20Sopenharmony_ci			nat_addr = &info->sw->ar_rnat;
3828c2ecf20Sopenharmony_ci		nat_mask = (1UL << ia64_rse_slot_num(addr));
3838c2ecf20Sopenharmony_ci	}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	if (write) {
3868c2ecf20Sopenharmony_ci		if (read_only(addr)) {
3878c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
3888c2ecf20Sopenharmony_ci				__func__);
3898c2ecf20Sopenharmony_ci		} else {
3908c2ecf20Sopenharmony_ci			*addr = *val;
3918c2ecf20Sopenharmony_ci			if (*nat)
3928c2ecf20Sopenharmony_ci				*nat_addr |= nat_mask;
3938c2ecf20Sopenharmony_ci			else
3948c2ecf20Sopenharmony_ci				*nat_addr &= ~nat_mask;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci	} else {
3978c2ecf20Sopenharmony_ci		if ((*nat_addr & nat_mask) == 0) {
3988c2ecf20Sopenharmony_ci			*val = *addr;
3998c2ecf20Sopenharmony_ci			*nat = 0;
4008c2ecf20Sopenharmony_ci		} else {
4018c2ecf20Sopenharmony_ci			*val = 0;	/* if register is a NaT, *addr may contain kernel data! */
4028c2ecf20Sopenharmony_ci			*nat = 1;
4038c2ecf20Sopenharmony_ci		}
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	return 0;
4068c2ecf20Sopenharmony_ci}
4078c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_access_gr);
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ciint
4108c2ecf20Sopenharmony_ciunw_access_br (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	unsigned long *addr;
4138c2ecf20Sopenharmony_ci	struct pt_regs *pt;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	switch (regnum) {
4168c2ecf20Sopenharmony_ci		/* scratch: */
4178c2ecf20Sopenharmony_ci	      case 0: pt = get_scratch_regs(info); addr = &pt->b0; break;
4188c2ecf20Sopenharmony_ci	      case 6: pt = get_scratch_regs(info); addr = &pt->b6; break;
4198c2ecf20Sopenharmony_ci	      case 7: pt = get_scratch_regs(info); addr = &pt->b7; break;
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci		/* preserved: */
4228c2ecf20Sopenharmony_ci	      case 1: case 2: case 3: case 4: case 5:
4238c2ecf20Sopenharmony_ci		addr = *(&info->b1_loc + (regnum - 1));
4248c2ecf20Sopenharmony_ci		if (!addr)
4258c2ecf20Sopenharmony_ci			addr = &info->sw->b1 + (regnum - 1);
4268c2ecf20Sopenharmony_ci		break;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	      default:
4298c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent b%u\n",
4308c2ecf20Sopenharmony_ci			   __func__, regnum);
4318c2ecf20Sopenharmony_ci		return -1;
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci	if (write)
4348c2ecf20Sopenharmony_ci		if (read_only(addr)) {
4358c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
4368c2ecf20Sopenharmony_ci				__func__);
4378c2ecf20Sopenharmony_ci		} else
4388c2ecf20Sopenharmony_ci			*addr = *val;
4398c2ecf20Sopenharmony_ci	else
4408c2ecf20Sopenharmony_ci		*val = *addr;
4418c2ecf20Sopenharmony_ci	return 0;
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_access_br);
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ciint
4468c2ecf20Sopenharmony_ciunw_access_fr (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
4478c2ecf20Sopenharmony_ci{
4488c2ecf20Sopenharmony_ci	struct ia64_fpreg *addr = NULL;
4498c2ecf20Sopenharmony_ci	struct pt_regs *pt;
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci	if ((unsigned) (regnum - 2) >= 126) {
4528c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent f%u\n",
4538c2ecf20Sopenharmony_ci			   __func__, regnum);
4548c2ecf20Sopenharmony_ci		return -1;
4558c2ecf20Sopenharmony_ci	}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (regnum <= 5) {
4588c2ecf20Sopenharmony_ci		addr = *(&info->f2_loc + (regnum - 2));
4598c2ecf20Sopenharmony_ci		if (!addr)
4608c2ecf20Sopenharmony_ci			addr = &info->sw->f2 + (regnum - 2);
4618c2ecf20Sopenharmony_ci	} else if (regnum <= 15) {
4628c2ecf20Sopenharmony_ci		if (regnum <= 11) {
4638c2ecf20Sopenharmony_ci			pt = get_scratch_regs(info);
4648c2ecf20Sopenharmony_ci			addr = &pt->f6  + (regnum - 6);
4658c2ecf20Sopenharmony_ci		}
4668c2ecf20Sopenharmony_ci		else
4678c2ecf20Sopenharmony_ci			addr = &info->sw->f12 + (regnum - 12);
4688c2ecf20Sopenharmony_ci	} else if (regnum <= 31) {
4698c2ecf20Sopenharmony_ci		addr = info->fr_loc[regnum - 16];
4708c2ecf20Sopenharmony_ci		if (!addr)
4718c2ecf20Sopenharmony_ci			addr = &info->sw->f16 + (regnum - 16);
4728c2ecf20Sopenharmony_ci	} else {
4738c2ecf20Sopenharmony_ci		struct task_struct *t = info->task;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		if (write)
4768c2ecf20Sopenharmony_ci			ia64_sync_fph(t);
4778c2ecf20Sopenharmony_ci		else
4788c2ecf20Sopenharmony_ci			ia64_flush_fph(t);
4798c2ecf20Sopenharmony_ci		addr = t->thread.fph + (regnum - 32);
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (write)
4838c2ecf20Sopenharmony_ci		if (read_only(addr)) {
4848c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
4858c2ecf20Sopenharmony_ci				__func__);
4868c2ecf20Sopenharmony_ci		} else
4878c2ecf20Sopenharmony_ci			*addr = *val;
4888c2ecf20Sopenharmony_ci	else
4898c2ecf20Sopenharmony_ci		*val = *addr;
4908c2ecf20Sopenharmony_ci	return 0;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_access_fr);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ciint
4958c2ecf20Sopenharmony_ciunw_access_ar (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	unsigned long *addr;
4988c2ecf20Sopenharmony_ci	struct pt_regs *pt;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	switch (regnum) {
5018c2ecf20Sopenharmony_ci	      case UNW_AR_BSP:
5028c2ecf20Sopenharmony_ci		addr = info->bsp_loc;
5038c2ecf20Sopenharmony_ci		if (!addr)
5048c2ecf20Sopenharmony_ci			addr = &info->sw->ar_bspstore;
5058c2ecf20Sopenharmony_ci		break;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	      case UNW_AR_BSPSTORE:
5088c2ecf20Sopenharmony_ci		addr = info->bspstore_loc;
5098c2ecf20Sopenharmony_ci		if (!addr)
5108c2ecf20Sopenharmony_ci			addr = &info->sw->ar_bspstore;
5118c2ecf20Sopenharmony_ci		break;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	      case UNW_AR_PFS:
5148c2ecf20Sopenharmony_ci		addr = info->pfs_loc;
5158c2ecf20Sopenharmony_ci		if (!addr)
5168c2ecf20Sopenharmony_ci			addr = &info->sw->ar_pfs;
5178c2ecf20Sopenharmony_ci		break;
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_ci	      case UNW_AR_RNAT:
5208c2ecf20Sopenharmony_ci		addr = info->rnat_loc;
5218c2ecf20Sopenharmony_ci		if (!addr)
5228c2ecf20Sopenharmony_ci			addr = &info->sw->ar_rnat;
5238c2ecf20Sopenharmony_ci		break;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	      case UNW_AR_UNAT:
5268c2ecf20Sopenharmony_ci		addr = info->unat_loc;
5278c2ecf20Sopenharmony_ci		if (!addr)
5288c2ecf20Sopenharmony_ci			addr = &info->sw->caller_unat;
5298c2ecf20Sopenharmony_ci		break;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	      case UNW_AR_LC:
5328c2ecf20Sopenharmony_ci		addr = info->lc_loc;
5338c2ecf20Sopenharmony_ci		if (!addr)
5348c2ecf20Sopenharmony_ci			addr = &info->sw->ar_lc;
5358c2ecf20Sopenharmony_ci		break;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	      case UNW_AR_EC:
5388c2ecf20Sopenharmony_ci		if (!info->cfm_loc)
5398c2ecf20Sopenharmony_ci			return -1;
5408c2ecf20Sopenharmony_ci		if (write)
5418c2ecf20Sopenharmony_ci			*info->cfm_loc =
5428c2ecf20Sopenharmony_ci				(*info->cfm_loc & ~(0x3fUL << 52)) | ((*val & 0x3f) << 52);
5438c2ecf20Sopenharmony_ci		else
5448c2ecf20Sopenharmony_ci			*val = (*info->cfm_loc >> 52) & 0x3f;
5458c2ecf20Sopenharmony_ci		return 0;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	      case UNW_AR_FPSR:
5488c2ecf20Sopenharmony_ci		addr = info->fpsr_loc;
5498c2ecf20Sopenharmony_ci		if (!addr)
5508c2ecf20Sopenharmony_ci			addr = &info->sw->ar_fpsr;
5518c2ecf20Sopenharmony_ci		break;
5528c2ecf20Sopenharmony_ci
5538c2ecf20Sopenharmony_ci	      case UNW_AR_RSC:
5548c2ecf20Sopenharmony_ci		pt = get_scratch_regs(info);
5558c2ecf20Sopenharmony_ci		addr = &pt->ar_rsc;
5568c2ecf20Sopenharmony_ci		break;
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	      case UNW_AR_CCV:
5598c2ecf20Sopenharmony_ci		pt = get_scratch_regs(info);
5608c2ecf20Sopenharmony_ci		addr = &pt->ar_ccv;
5618c2ecf20Sopenharmony_ci		break;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	      case UNW_AR_CSD:
5648c2ecf20Sopenharmony_ci		pt = get_scratch_regs(info);
5658c2ecf20Sopenharmony_ci		addr = &pt->ar_csd;
5668c2ecf20Sopenharmony_ci		break;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	      case UNW_AR_SSD:
5698c2ecf20Sopenharmony_ci		pt = get_scratch_regs(info);
5708c2ecf20Sopenharmony_ci		addr = &pt->ar_ssd;
5718c2ecf20Sopenharmony_ci		break;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci	      default:
5748c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent ar%u\n",
5758c2ecf20Sopenharmony_ci			   __func__, regnum);
5768c2ecf20Sopenharmony_ci		return -1;
5778c2ecf20Sopenharmony_ci	}
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (write) {
5808c2ecf20Sopenharmony_ci		if (read_only(addr)) {
5818c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
5828c2ecf20Sopenharmony_ci				__func__);
5838c2ecf20Sopenharmony_ci		} else
5848c2ecf20Sopenharmony_ci			*addr = *val;
5858c2ecf20Sopenharmony_ci	} else
5868c2ecf20Sopenharmony_ci		*val = *addr;
5878c2ecf20Sopenharmony_ci	return 0;
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_access_ar);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ciint
5928c2ecf20Sopenharmony_ciunw_access_pr (struct unw_frame_info *info, unsigned long *val, int write)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	unsigned long *addr;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	addr = info->pr_loc;
5978c2ecf20Sopenharmony_ci	if (!addr)
5988c2ecf20Sopenharmony_ci		addr = &info->sw->pr;
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (write) {
6018c2ecf20Sopenharmony_ci		if (read_only(addr)) {
6028c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
6038c2ecf20Sopenharmony_ci				__func__);
6048c2ecf20Sopenharmony_ci		} else
6058c2ecf20Sopenharmony_ci			*addr = *val;
6068c2ecf20Sopenharmony_ci	} else
6078c2ecf20Sopenharmony_ci		*val = *addr;
6088c2ecf20Sopenharmony_ci	return 0;
6098c2ecf20Sopenharmony_ci}
6108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_access_pr);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci/* Routines to manipulate the state stack.  */
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_cistatic inline void
6168c2ecf20Sopenharmony_cipush (struct unw_state_record *sr)
6178c2ecf20Sopenharmony_ci{
6188c2ecf20Sopenharmony_ci	struct unw_reg_state *rs;
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_ci	rs = alloc_reg_state();
6218c2ecf20Sopenharmony_ci	if (!rs) {
6228c2ecf20Sopenharmony_ci		printk(KERN_ERR "unwind: cannot stack reg state!\n");
6238c2ecf20Sopenharmony_ci		return;
6248c2ecf20Sopenharmony_ci	}
6258c2ecf20Sopenharmony_ci	memcpy(rs, &sr->curr, sizeof(*rs));
6268c2ecf20Sopenharmony_ci	sr->curr.next = rs;
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cistatic void
6308c2ecf20Sopenharmony_cipop (struct unw_state_record *sr)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct unw_reg_state *rs = sr->curr.next;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (!rs) {
6358c2ecf20Sopenharmony_ci		printk(KERN_ERR "unwind: stack underflow!\n");
6368c2ecf20Sopenharmony_ci		return;
6378c2ecf20Sopenharmony_ci	}
6388c2ecf20Sopenharmony_ci	memcpy(&sr->curr, rs, sizeof(*rs));
6398c2ecf20Sopenharmony_ci	free_reg_state(rs);
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci/* Make a copy of the state stack.  Non-recursive to avoid stack overflows.  */
6438c2ecf20Sopenharmony_cistatic struct unw_reg_state *
6448c2ecf20Sopenharmony_cidup_state_stack (struct unw_reg_state *rs)
6458c2ecf20Sopenharmony_ci{
6468c2ecf20Sopenharmony_ci	struct unw_reg_state *copy, *prev = NULL, *first = NULL;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	while (rs) {
6498c2ecf20Sopenharmony_ci		copy = alloc_reg_state();
6508c2ecf20Sopenharmony_ci		if (!copy) {
6518c2ecf20Sopenharmony_ci			printk(KERN_ERR "unwind.dup_state_stack: out of memory\n");
6528c2ecf20Sopenharmony_ci			return NULL;
6538c2ecf20Sopenharmony_ci		}
6548c2ecf20Sopenharmony_ci		memcpy(copy, rs, sizeof(*copy));
6558c2ecf20Sopenharmony_ci		if (first)
6568c2ecf20Sopenharmony_ci			prev->next = copy;
6578c2ecf20Sopenharmony_ci		else
6588c2ecf20Sopenharmony_ci			first = copy;
6598c2ecf20Sopenharmony_ci		rs = rs->next;
6608c2ecf20Sopenharmony_ci		prev = copy;
6618c2ecf20Sopenharmony_ci	}
6628c2ecf20Sopenharmony_ci	return first;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/* Free all stacked register states (but not RS itself).  */
6668c2ecf20Sopenharmony_cistatic void
6678c2ecf20Sopenharmony_cifree_state_stack (struct unw_reg_state *rs)
6688c2ecf20Sopenharmony_ci{
6698c2ecf20Sopenharmony_ci	struct unw_reg_state *p, *next;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	for (p = rs->next; p != NULL; p = next) {
6728c2ecf20Sopenharmony_ci		next = p->next;
6738c2ecf20Sopenharmony_ci		free_reg_state(p);
6748c2ecf20Sopenharmony_ci	}
6758c2ecf20Sopenharmony_ci	rs->next = NULL;
6768c2ecf20Sopenharmony_ci}
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci/* Unwind decoder routines */
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_cistatic enum unw_register_index __attribute_const__
6818c2ecf20Sopenharmony_cidecode_abreg (unsigned char abreg, int memory)
6828c2ecf20Sopenharmony_ci{
6838c2ecf20Sopenharmony_ci	switch (abreg) {
6848c2ecf20Sopenharmony_ci	      case 0x04 ... 0x07: return UNW_REG_R4 + (abreg - 0x04);
6858c2ecf20Sopenharmony_ci	      case 0x22 ... 0x25: return UNW_REG_F2 + (abreg - 0x22);
6868c2ecf20Sopenharmony_ci	      case 0x30 ... 0x3f: return UNW_REG_F16 + (abreg - 0x30);
6878c2ecf20Sopenharmony_ci	      case 0x41 ... 0x45: return UNW_REG_B1 + (abreg - 0x41);
6888c2ecf20Sopenharmony_ci	      case 0x60: return UNW_REG_PR;
6898c2ecf20Sopenharmony_ci	      case 0x61: return UNW_REG_PSP;
6908c2ecf20Sopenharmony_ci	      case 0x62: return memory ? UNW_REG_PRI_UNAT_MEM : UNW_REG_PRI_UNAT_GR;
6918c2ecf20Sopenharmony_ci	      case 0x63: return UNW_REG_RP;
6928c2ecf20Sopenharmony_ci	      case 0x64: return UNW_REG_BSP;
6938c2ecf20Sopenharmony_ci	      case 0x65: return UNW_REG_BSPSTORE;
6948c2ecf20Sopenharmony_ci	      case 0x66: return UNW_REG_RNAT;
6958c2ecf20Sopenharmony_ci	      case 0x67: return UNW_REG_UNAT;
6968c2ecf20Sopenharmony_ci	      case 0x68: return UNW_REG_FPSR;
6978c2ecf20Sopenharmony_ci	      case 0x69: return UNW_REG_PFS;
6988c2ecf20Sopenharmony_ci	      case 0x6a: return UNW_REG_LC;
6998c2ecf20Sopenharmony_ci	      default:
7008c2ecf20Sopenharmony_ci		break;
7018c2ecf20Sopenharmony_ci	}
7028c2ecf20Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: bad abreg=0x%x\n", __func__, abreg);
7038c2ecf20Sopenharmony_ci	return UNW_REG_LC;
7048c2ecf20Sopenharmony_ci}
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_cistatic void
7078c2ecf20Sopenharmony_ciset_reg (struct unw_reg_info *reg, enum unw_where where, int when, unsigned long val)
7088c2ecf20Sopenharmony_ci{
7098c2ecf20Sopenharmony_ci	reg->val = val;
7108c2ecf20Sopenharmony_ci	reg->where = where;
7118c2ecf20Sopenharmony_ci	if (reg->when == UNW_WHEN_NEVER)
7128c2ecf20Sopenharmony_ci		reg->when = when;
7138c2ecf20Sopenharmony_ci}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_cistatic void
7168c2ecf20Sopenharmony_cialloc_spill_area (unsigned long *offp, unsigned long regsize,
7178c2ecf20Sopenharmony_ci		  struct unw_reg_info *lo, struct unw_reg_info *hi)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct unw_reg_info *reg;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	for (reg = hi; reg >= lo; --reg) {
7228c2ecf20Sopenharmony_ci		if (reg->where == UNW_WHERE_SPILL_HOME) {
7238c2ecf20Sopenharmony_ci			reg->where = UNW_WHERE_PSPREL;
7248c2ecf20Sopenharmony_ci			*offp -= regsize;
7258c2ecf20Sopenharmony_ci			reg->val = *offp;
7268c2ecf20Sopenharmony_ci		}
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci}
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic inline void
7318c2ecf20Sopenharmony_cispill_next_when (struct unw_reg_info **regp, struct unw_reg_info *lim, unw_word t)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	struct unw_reg_info *reg;
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_ci	for (reg = *regp; reg <= lim; ++reg) {
7368c2ecf20Sopenharmony_ci		if (reg->where == UNW_WHERE_SPILL_HOME) {
7378c2ecf20Sopenharmony_ci			reg->when = t;
7388c2ecf20Sopenharmony_ci			*regp = reg + 1;
7398c2ecf20Sopenharmony_ci			return;
7408c2ecf20Sopenharmony_ci		}
7418c2ecf20Sopenharmony_ci	}
7428c2ecf20Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: excess spill!\n",  __func__);
7438c2ecf20Sopenharmony_ci}
7448c2ecf20Sopenharmony_ci
7458c2ecf20Sopenharmony_cistatic inline void
7468c2ecf20Sopenharmony_cifinish_prologue (struct unw_state_record *sr)
7478c2ecf20Sopenharmony_ci{
7488c2ecf20Sopenharmony_ci	struct unw_reg_info *reg;
7498c2ecf20Sopenharmony_ci	unsigned long off;
7508c2ecf20Sopenharmony_ci	int i;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	/*
7538c2ecf20Sopenharmony_ci	 * First, resolve implicit register save locations (see Section "11.4.2.3 Rules
7548c2ecf20Sopenharmony_ci	 * for Using Unwind Descriptors", rule 3):
7558c2ecf20Sopenharmony_ci	 */
7568c2ecf20Sopenharmony_ci	for (i = 0; i < (int) ARRAY_SIZE(unw.save_order); ++i) {
7578c2ecf20Sopenharmony_ci		reg = sr->curr.reg + unw.save_order[i];
7588c2ecf20Sopenharmony_ci		if (reg->where == UNW_WHERE_GR_SAVE) {
7598c2ecf20Sopenharmony_ci			reg->where = UNW_WHERE_GR;
7608c2ecf20Sopenharmony_ci			reg->val = sr->gr_save_loc++;
7618c2ecf20Sopenharmony_ci		}
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/*
7658c2ecf20Sopenharmony_ci	 * Next, compute when the fp, general, and branch registers get
7668c2ecf20Sopenharmony_ci	 * saved.  This must come before alloc_spill_area() because
7678c2ecf20Sopenharmony_ci	 * we need to know which registers are spilled to their home
7688c2ecf20Sopenharmony_ci	 * locations.
7698c2ecf20Sopenharmony_ci	 */
7708c2ecf20Sopenharmony_ci	if (sr->imask) {
7718c2ecf20Sopenharmony_ci		unsigned char kind, mask = 0, *cp = sr->imask;
7728c2ecf20Sopenharmony_ci		int t;
7738c2ecf20Sopenharmony_ci		static const unsigned char limit[3] = {
7748c2ecf20Sopenharmony_ci			UNW_REG_F31, UNW_REG_R7, UNW_REG_B5
7758c2ecf20Sopenharmony_ci		};
7768c2ecf20Sopenharmony_ci		struct unw_reg_info *(regs[3]);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci		regs[0] = sr->curr.reg + UNW_REG_F2;
7798c2ecf20Sopenharmony_ci		regs[1] = sr->curr.reg + UNW_REG_R4;
7808c2ecf20Sopenharmony_ci		regs[2] = sr->curr.reg + UNW_REG_B1;
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		for (t = 0; t < sr->region_len; ++t) {
7838c2ecf20Sopenharmony_ci			if ((t & 3) == 0)
7848c2ecf20Sopenharmony_ci				mask = *cp++;
7858c2ecf20Sopenharmony_ci			kind = (mask >> 2*(3-(t & 3))) & 3;
7868c2ecf20Sopenharmony_ci			if (kind > 0)
7878c2ecf20Sopenharmony_ci				spill_next_when(&regs[kind - 1], sr->curr.reg + limit[kind - 1],
7888c2ecf20Sopenharmony_ci						sr->region_start + t);
7898c2ecf20Sopenharmony_ci		}
7908c2ecf20Sopenharmony_ci	}
7918c2ecf20Sopenharmony_ci	/*
7928c2ecf20Sopenharmony_ci	 * Next, lay out the memory stack spill area:
7938c2ecf20Sopenharmony_ci	 */
7948c2ecf20Sopenharmony_ci	if (sr->any_spills) {
7958c2ecf20Sopenharmony_ci		off = sr->spill_offset;
7968c2ecf20Sopenharmony_ci		alloc_spill_area(&off, 16, sr->curr.reg + UNW_REG_F2, sr->curr.reg + UNW_REG_F31);
7978c2ecf20Sopenharmony_ci		alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_B1, sr->curr.reg + UNW_REG_B5);
7988c2ecf20Sopenharmony_ci		alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_R4, sr->curr.reg + UNW_REG_R7);
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci/*
8038c2ecf20Sopenharmony_ci * Region header descriptors.
8048c2ecf20Sopenharmony_ci */
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_cistatic void
8078c2ecf20Sopenharmony_cidesc_prologue (int body, unw_word rlen, unsigned char mask, unsigned char grsave,
8088c2ecf20Sopenharmony_ci	       struct unw_state_record *sr)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	int i, region_start;
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	if (!(sr->in_body || sr->first_region))
8138c2ecf20Sopenharmony_ci		finish_prologue(sr);
8148c2ecf20Sopenharmony_ci	sr->first_region = 0;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	/* check if we're done: */
8178c2ecf20Sopenharmony_ci	if (sr->when_target < sr->region_start + sr->region_len) {
8188c2ecf20Sopenharmony_ci		sr->done = 1;
8198c2ecf20Sopenharmony_ci		return;
8208c2ecf20Sopenharmony_ci	}
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	region_start = sr->region_start + sr->region_len;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	for (i = 0; i < sr->epilogue_count; ++i)
8258c2ecf20Sopenharmony_ci		pop(sr);
8268c2ecf20Sopenharmony_ci	sr->epilogue_count = 0;
8278c2ecf20Sopenharmony_ci	sr->epilogue_start = UNW_WHEN_NEVER;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci	sr->region_start = region_start;
8308c2ecf20Sopenharmony_ci	sr->region_len = rlen;
8318c2ecf20Sopenharmony_ci	sr->in_body = body;
8328c2ecf20Sopenharmony_ci
8338c2ecf20Sopenharmony_ci	if (!body) {
8348c2ecf20Sopenharmony_ci		push(sr);
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci		for (i = 0; i < 4; ++i) {
8378c2ecf20Sopenharmony_ci			if (mask & 0x8)
8388c2ecf20Sopenharmony_ci				set_reg(sr->curr.reg + unw.save_order[i], UNW_WHERE_GR,
8398c2ecf20Sopenharmony_ci					sr->region_start + sr->region_len - 1, grsave++);
8408c2ecf20Sopenharmony_ci			mask <<= 1;
8418c2ecf20Sopenharmony_ci		}
8428c2ecf20Sopenharmony_ci		sr->gr_save_loc = grsave;
8438c2ecf20Sopenharmony_ci		sr->any_spills = 0;
8448c2ecf20Sopenharmony_ci		sr->imask = NULL;
8458c2ecf20Sopenharmony_ci		sr->spill_offset = 0x10;	/* default to psp+16 */
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci/*
8508c2ecf20Sopenharmony_ci * Prologue descriptors.
8518c2ecf20Sopenharmony_ci */
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_cistatic inline void
8548c2ecf20Sopenharmony_cidesc_abi (unsigned char abi, unsigned char context, struct unw_state_record *sr)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	if (abi == 3 && context == 'i') {
8578c2ecf20Sopenharmony_ci		sr->flags |= UNW_FLAG_INTERRUPT_FRAME;
8588c2ecf20Sopenharmony_ci		UNW_DPRINT(3, "unwind.%s: interrupt frame\n",  __func__);
8598c2ecf20Sopenharmony_ci	}
8608c2ecf20Sopenharmony_ci	else
8618c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind%s: ignoring unwabi(abi=0x%x,context=0x%x)\n",
8628c2ecf20Sopenharmony_ci				__func__, abi, context);
8638c2ecf20Sopenharmony_ci}
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_cistatic inline void
8668c2ecf20Sopenharmony_cidesc_br_gr (unsigned char brmask, unsigned char gr, struct unw_state_record *sr)
8678c2ecf20Sopenharmony_ci{
8688c2ecf20Sopenharmony_ci	int i;
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	for (i = 0; i < 5; ++i) {
8718c2ecf20Sopenharmony_ci		if (brmask & 1)
8728c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_GR,
8738c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, gr++);
8748c2ecf20Sopenharmony_ci		brmask >>= 1;
8758c2ecf20Sopenharmony_ci	}
8768c2ecf20Sopenharmony_ci}
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_cistatic inline void
8798c2ecf20Sopenharmony_cidesc_br_mem (unsigned char brmask, struct unw_state_record *sr)
8808c2ecf20Sopenharmony_ci{
8818c2ecf20Sopenharmony_ci	int i;
8828c2ecf20Sopenharmony_ci
8838c2ecf20Sopenharmony_ci	for (i = 0; i < 5; ++i) {
8848c2ecf20Sopenharmony_ci		if (brmask & 1) {
8858c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_SPILL_HOME,
8868c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
8878c2ecf20Sopenharmony_ci			sr->any_spills = 1;
8888c2ecf20Sopenharmony_ci		}
8898c2ecf20Sopenharmony_ci		brmask >>= 1;
8908c2ecf20Sopenharmony_ci	}
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistatic inline void
8948c2ecf20Sopenharmony_cidesc_frgr_mem (unsigned char grmask, unw_word frmask, struct unw_state_record *sr)
8958c2ecf20Sopenharmony_ci{
8968c2ecf20Sopenharmony_ci	int i;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
8998c2ecf20Sopenharmony_ci		if ((grmask & 1) != 0) {
9008c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
9018c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
9028c2ecf20Sopenharmony_ci			sr->any_spills = 1;
9038c2ecf20Sopenharmony_ci		}
9048c2ecf20Sopenharmony_ci		grmask >>= 1;
9058c2ecf20Sopenharmony_ci	}
9068c2ecf20Sopenharmony_ci	for (i = 0; i < 20; ++i) {
9078c2ecf20Sopenharmony_ci		if ((frmask & 1) != 0) {
9088c2ecf20Sopenharmony_ci			int base = (i < 4) ? UNW_REG_F2 : UNW_REG_F16 - 4;
9098c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + base + i, UNW_WHERE_SPILL_HOME,
9108c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
9118c2ecf20Sopenharmony_ci			sr->any_spills = 1;
9128c2ecf20Sopenharmony_ci		}
9138c2ecf20Sopenharmony_ci		frmask >>= 1;
9148c2ecf20Sopenharmony_ci	}
9158c2ecf20Sopenharmony_ci}
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_cistatic inline void
9188c2ecf20Sopenharmony_cidesc_fr_mem (unsigned char frmask, struct unw_state_record *sr)
9198c2ecf20Sopenharmony_ci{
9208c2ecf20Sopenharmony_ci	int i;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
9238c2ecf20Sopenharmony_ci		if ((frmask & 1) != 0) {
9248c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_F2 + i, UNW_WHERE_SPILL_HOME,
9258c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
9268c2ecf20Sopenharmony_ci			sr->any_spills = 1;
9278c2ecf20Sopenharmony_ci		}
9288c2ecf20Sopenharmony_ci		frmask >>= 1;
9298c2ecf20Sopenharmony_ci	}
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic inline void
9338c2ecf20Sopenharmony_cidesc_gr_gr (unsigned char grmask, unsigned char gr, struct unw_state_record *sr)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	int i;
9368c2ecf20Sopenharmony_ci
9378c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
9388c2ecf20Sopenharmony_ci		if ((grmask & 1) != 0)
9398c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_GR,
9408c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, gr++);
9418c2ecf20Sopenharmony_ci		grmask >>= 1;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic inline void
9468c2ecf20Sopenharmony_cidesc_gr_mem (unsigned char grmask, struct unw_state_record *sr)
9478c2ecf20Sopenharmony_ci{
9488c2ecf20Sopenharmony_ci	int i;
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	for (i = 0; i < 4; ++i) {
9518c2ecf20Sopenharmony_ci		if ((grmask & 1) != 0) {
9528c2ecf20Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
9538c2ecf20Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
9548c2ecf20Sopenharmony_ci			sr->any_spills = 1;
9558c2ecf20Sopenharmony_ci		}
9568c2ecf20Sopenharmony_ci		grmask >>= 1;
9578c2ecf20Sopenharmony_ci	}
9588c2ecf20Sopenharmony_ci}
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_cistatic inline void
9618c2ecf20Sopenharmony_cidesc_mem_stack_f (unw_word t, unw_word size, struct unw_state_record *sr)
9628c2ecf20Sopenharmony_ci{
9638c2ecf20Sopenharmony_ci	set_reg(sr->curr.reg + UNW_REG_PSP, UNW_WHERE_NONE,
9648c2ecf20Sopenharmony_ci		sr->region_start + min_t(int, t, sr->region_len - 1), 16*size);
9658c2ecf20Sopenharmony_ci}
9668c2ecf20Sopenharmony_ci
9678c2ecf20Sopenharmony_cistatic inline void
9688c2ecf20Sopenharmony_cidesc_mem_stack_v (unw_word t, struct unw_state_record *sr)
9698c2ecf20Sopenharmony_ci{
9708c2ecf20Sopenharmony_ci	sr->curr.reg[UNW_REG_PSP].when = sr->region_start + min_t(int, t, sr->region_len - 1);
9718c2ecf20Sopenharmony_ci}
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_cistatic inline void
9748c2ecf20Sopenharmony_cidesc_reg_gr (unsigned char reg, unsigned char dst, struct unw_state_record *sr)
9758c2ecf20Sopenharmony_ci{
9768c2ecf20Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_GR, sr->region_start + sr->region_len - 1, dst);
9778c2ecf20Sopenharmony_ci}
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_cistatic inline void
9808c2ecf20Sopenharmony_cidesc_reg_psprel (unsigned char reg, unw_word pspoff, struct unw_state_record *sr)
9818c2ecf20Sopenharmony_ci{
9828c2ecf20Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_PSPREL, sr->region_start + sr->region_len - 1,
9838c2ecf20Sopenharmony_ci		0x10 - 4*pspoff);
9848c2ecf20Sopenharmony_ci}
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_cistatic inline void
9878c2ecf20Sopenharmony_cidesc_reg_sprel (unsigned char reg, unw_word spoff, struct unw_state_record *sr)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_SPREL, sr->region_start + sr->region_len - 1,
9908c2ecf20Sopenharmony_ci		4*spoff);
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_cistatic inline void
9948c2ecf20Sopenharmony_cidesc_rp_br (unsigned char dst, struct unw_state_record *sr)
9958c2ecf20Sopenharmony_ci{
9968c2ecf20Sopenharmony_ci	sr->return_link_reg = dst;
9978c2ecf20Sopenharmony_ci}
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_cistatic inline void
10008c2ecf20Sopenharmony_cidesc_reg_when (unsigned char regnum, unw_word t, struct unw_state_record *sr)
10018c2ecf20Sopenharmony_ci{
10028c2ecf20Sopenharmony_ci	struct unw_reg_info *reg = sr->curr.reg + regnum;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	if (reg->where == UNW_WHERE_NONE)
10058c2ecf20Sopenharmony_ci		reg->where = UNW_WHERE_GR_SAVE;
10068c2ecf20Sopenharmony_ci	reg->when = sr->region_start + min_t(int, t, sr->region_len - 1);
10078c2ecf20Sopenharmony_ci}
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_cistatic inline void
10108c2ecf20Sopenharmony_cidesc_spill_base (unw_word pspoff, struct unw_state_record *sr)
10118c2ecf20Sopenharmony_ci{
10128c2ecf20Sopenharmony_ci	sr->spill_offset = 0x10 - 4*pspoff;
10138c2ecf20Sopenharmony_ci}
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_cistatic inline unsigned char *
10168c2ecf20Sopenharmony_cidesc_spill_mask (unsigned char *imaskp, struct unw_state_record *sr)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	sr->imask = imaskp;
10198c2ecf20Sopenharmony_ci	return imaskp + (2*sr->region_len + 7)/8;
10208c2ecf20Sopenharmony_ci}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci/*
10238c2ecf20Sopenharmony_ci * Body descriptors.
10248c2ecf20Sopenharmony_ci */
10258c2ecf20Sopenharmony_cistatic inline void
10268c2ecf20Sopenharmony_cidesc_epilogue (unw_word t, unw_word ecount, struct unw_state_record *sr)
10278c2ecf20Sopenharmony_ci{
10288c2ecf20Sopenharmony_ci	sr->epilogue_start = sr->region_start + sr->region_len - 1 - t;
10298c2ecf20Sopenharmony_ci	sr->epilogue_count = ecount + 1;
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_cistatic inline void
10338c2ecf20Sopenharmony_cidesc_copy_state (unw_word label, struct unw_state_record *sr)
10348c2ecf20Sopenharmony_ci{
10358c2ecf20Sopenharmony_ci	struct unw_labeled_state *ls;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	for (ls = sr->labeled_states; ls; ls = ls->next) {
10388c2ecf20Sopenharmony_ci		if (ls->label == label) {
10398c2ecf20Sopenharmony_ci			free_state_stack(&sr->curr);
10408c2ecf20Sopenharmony_ci			memcpy(&sr->curr, &ls->saved_state, sizeof(sr->curr));
10418c2ecf20Sopenharmony_ci			sr->curr.next = dup_state_stack(ls->saved_state.next);
10428c2ecf20Sopenharmony_ci			return;
10438c2ecf20Sopenharmony_ci		}
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci	printk(KERN_ERR "unwind: failed to find state labeled 0x%lx\n", label);
10468c2ecf20Sopenharmony_ci}
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_cistatic inline void
10498c2ecf20Sopenharmony_cidesc_label_state (unw_word label, struct unw_state_record *sr)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	struct unw_labeled_state *ls;
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	ls = alloc_labeled_state();
10548c2ecf20Sopenharmony_ci	if (!ls) {
10558c2ecf20Sopenharmony_ci		printk(KERN_ERR "unwind.desc_label_state(): out of memory\n");
10568c2ecf20Sopenharmony_ci		return;
10578c2ecf20Sopenharmony_ci	}
10588c2ecf20Sopenharmony_ci	ls->label = label;
10598c2ecf20Sopenharmony_ci	memcpy(&ls->saved_state, &sr->curr, sizeof(ls->saved_state));
10608c2ecf20Sopenharmony_ci	ls->saved_state.next = dup_state_stack(sr->curr.next);
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	/* insert into list of labeled states: */
10638c2ecf20Sopenharmony_ci	ls->next = sr->labeled_states;
10648c2ecf20Sopenharmony_ci	sr->labeled_states = ls;
10658c2ecf20Sopenharmony_ci}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci/*
10688c2ecf20Sopenharmony_ci * General descriptors.
10698c2ecf20Sopenharmony_ci */
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_cistatic inline int
10728c2ecf20Sopenharmony_cidesc_is_active (unsigned char qp, unw_word t, struct unw_state_record *sr)
10738c2ecf20Sopenharmony_ci{
10748c2ecf20Sopenharmony_ci	if (sr->when_target <= sr->region_start + min_t(int, t, sr->region_len - 1))
10758c2ecf20Sopenharmony_ci		return 0;
10768c2ecf20Sopenharmony_ci	if (qp > 0) {
10778c2ecf20Sopenharmony_ci		if ((sr->pr_val & (1UL << qp)) == 0)
10788c2ecf20Sopenharmony_ci			return 0;
10798c2ecf20Sopenharmony_ci		sr->pr_mask |= (1UL << qp);
10808c2ecf20Sopenharmony_ci	}
10818c2ecf20Sopenharmony_ci	return 1;
10828c2ecf20Sopenharmony_ci}
10838c2ecf20Sopenharmony_ci
10848c2ecf20Sopenharmony_cistatic inline void
10858c2ecf20Sopenharmony_cidesc_restore_p (unsigned char qp, unw_word t, unsigned char abreg, struct unw_state_record *sr)
10868c2ecf20Sopenharmony_ci{
10878c2ecf20Sopenharmony_ci	struct unw_reg_info *r;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
10908c2ecf20Sopenharmony_ci		return;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 0);
10938c2ecf20Sopenharmony_ci	r->where = UNW_WHERE_NONE;
10948c2ecf20Sopenharmony_ci	r->when = UNW_WHEN_NEVER;
10958c2ecf20Sopenharmony_ci	r->val = 0;
10968c2ecf20Sopenharmony_ci}
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_cistatic inline void
10998c2ecf20Sopenharmony_cidesc_spill_reg_p (unsigned char qp, unw_word t, unsigned char abreg, unsigned char x,
11008c2ecf20Sopenharmony_ci		     unsigned char ytreg, struct unw_state_record *sr)
11018c2ecf20Sopenharmony_ci{
11028c2ecf20Sopenharmony_ci	enum unw_where where = UNW_WHERE_GR;
11038c2ecf20Sopenharmony_ci	struct unw_reg_info *r;
11048c2ecf20Sopenharmony_ci
11058c2ecf20Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
11068c2ecf20Sopenharmony_ci		return;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	if (x)
11098c2ecf20Sopenharmony_ci		where = UNW_WHERE_BR;
11108c2ecf20Sopenharmony_ci	else if (ytreg & 0x80)
11118c2ecf20Sopenharmony_ci		where = UNW_WHERE_FR;
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 0);
11148c2ecf20Sopenharmony_ci	r->where = where;
11158c2ecf20Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
11168c2ecf20Sopenharmony_ci	r->val = (ytreg & 0x7f);
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic inline void
11208c2ecf20Sopenharmony_cidesc_spill_psprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word pspoff,
11218c2ecf20Sopenharmony_ci		     struct unw_state_record *sr)
11228c2ecf20Sopenharmony_ci{
11238c2ecf20Sopenharmony_ci	struct unw_reg_info *r;
11248c2ecf20Sopenharmony_ci
11258c2ecf20Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
11268c2ecf20Sopenharmony_ci		return;
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 1);
11298c2ecf20Sopenharmony_ci	r->where = UNW_WHERE_PSPREL;
11308c2ecf20Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
11318c2ecf20Sopenharmony_ci	r->val = 0x10 - 4*pspoff;
11328c2ecf20Sopenharmony_ci}
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_cistatic inline void
11358c2ecf20Sopenharmony_cidesc_spill_sprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word spoff,
11368c2ecf20Sopenharmony_ci		       struct unw_state_record *sr)
11378c2ecf20Sopenharmony_ci{
11388c2ecf20Sopenharmony_ci	struct unw_reg_info *r;
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
11418c2ecf20Sopenharmony_ci		return;
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 1);
11448c2ecf20Sopenharmony_ci	r->where = UNW_WHERE_SPREL;
11458c2ecf20Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
11468c2ecf20Sopenharmony_ci	r->val = 4*spoff;
11478c2ecf20Sopenharmony_ci}
11488c2ecf20Sopenharmony_ci
11498c2ecf20Sopenharmony_ci#define UNW_DEC_BAD_CODE(code)			printk(KERN_ERR "unwind: unknown code 0x%02x\n", \
11508c2ecf20Sopenharmony_ci						       code);
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci/*
11538c2ecf20Sopenharmony_ci * region headers:
11548c2ecf20Sopenharmony_ci */
11558c2ecf20Sopenharmony_ci#define UNW_DEC_PROLOGUE_GR(fmt,r,m,gr,arg)	desc_prologue(0,r,m,gr,arg)
11568c2ecf20Sopenharmony_ci#define UNW_DEC_PROLOGUE(fmt,b,r,arg)		desc_prologue(b,r,0,32,arg)
11578c2ecf20Sopenharmony_ci/*
11588c2ecf20Sopenharmony_ci * prologue descriptors:
11598c2ecf20Sopenharmony_ci */
11608c2ecf20Sopenharmony_ci#define UNW_DEC_ABI(fmt,a,c,arg)		desc_abi(a,c,arg)
11618c2ecf20Sopenharmony_ci#define UNW_DEC_BR_GR(fmt,b,g,arg)		desc_br_gr(b,g,arg)
11628c2ecf20Sopenharmony_ci#define UNW_DEC_BR_MEM(fmt,b,arg)		desc_br_mem(b,arg)
11638c2ecf20Sopenharmony_ci#define UNW_DEC_FRGR_MEM(fmt,g,f,arg)		desc_frgr_mem(g,f,arg)
11648c2ecf20Sopenharmony_ci#define UNW_DEC_FR_MEM(fmt,f,arg)		desc_fr_mem(f,arg)
11658c2ecf20Sopenharmony_ci#define UNW_DEC_GR_GR(fmt,m,g,arg)		desc_gr_gr(m,g,arg)
11668c2ecf20Sopenharmony_ci#define UNW_DEC_GR_MEM(fmt,m,arg)		desc_gr_mem(m,arg)
11678c2ecf20Sopenharmony_ci#define UNW_DEC_MEM_STACK_F(fmt,t,s,arg)	desc_mem_stack_f(t,s,arg)
11688c2ecf20Sopenharmony_ci#define UNW_DEC_MEM_STACK_V(fmt,t,arg)		desc_mem_stack_v(t,arg)
11698c2ecf20Sopenharmony_ci#define UNW_DEC_REG_GR(fmt,r,d,arg)		desc_reg_gr(r,d,arg)
11708c2ecf20Sopenharmony_ci#define UNW_DEC_REG_PSPREL(fmt,r,o,arg)		desc_reg_psprel(r,o,arg)
11718c2ecf20Sopenharmony_ci#define UNW_DEC_REG_SPREL(fmt,r,o,arg)		desc_reg_sprel(r,o,arg)
11728c2ecf20Sopenharmony_ci#define UNW_DEC_REG_WHEN(fmt,r,t,arg)		desc_reg_when(r,t,arg)
11738c2ecf20Sopenharmony_ci#define UNW_DEC_PRIUNAT_WHEN_GR(fmt,t,arg)	desc_reg_when(UNW_REG_PRI_UNAT_GR,t,arg)
11748c2ecf20Sopenharmony_ci#define UNW_DEC_PRIUNAT_WHEN_MEM(fmt,t,arg)	desc_reg_when(UNW_REG_PRI_UNAT_MEM,t,arg)
11758c2ecf20Sopenharmony_ci#define UNW_DEC_PRIUNAT_GR(fmt,r,arg)		desc_reg_gr(UNW_REG_PRI_UNAT_GR,r,arg)
11768c2ecf20Sopenharmony_ci#define UNW_DEC_PRIUNAT_PSPREL(fmt,o,arg)	desc_reg_psprel(UNW_REG_PRI_UNAT_MEM,o,arg)
11778c2ecf20Sopenharmony_ci#define UNW_DEC_PRIUNAT_SPREL(fmt,o,arg)	desc_reg_sprel(UNW_REG_PRI_UNAT_MEM,o,arg)
11788c2ecf20Sopenharmony_ci#define UNW_DEC_RP_BR(fmt,d,arg)		desc_rp_br(d,arg)
11798c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_BASE(fmt,o,arg)		desc_spill_base(o,arg)
11808c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_MASK(fmt,m,arg)		(m = desc_spill_mask(m,arg))
11818c2ecf20Sopenharmony_ci/*
11828c2ecf20Sopenharmony_ci * body descriptors:
11838c2ecf20Sopenharmony_ci */
11848c2ecf20Sopenharmony_ci#define UNW_DEC_EPILOGUE(fmt,t,c,arg)		desc_epilogue(t,c,arg)
11858c2ecf20Sopenharmony_ci#define UNW_DEC_COPY_STATE(fmt,l,arg)		desc_copy_state(l,arg)
11868c2ecf20Sopenharmony_ci#define UNW_DEC_LABEL_STATE(fmt,l,arg)		desc_label_state(l,arg)
11878c2ecf20Sopenharmony_ci/*
11888c2ecf20Sopenharmony_ci * general unwind descriptors:
11898c2ecf20Sopenharmony_ci */
11908c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_REG_P(f,p,t,a,x,y,arg)	desc_spill_reg_p(p,t,a,x,y,arg)
11918c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_REG(f,t,a,x,y,arg)	desc_spill_reg_p(0,t,a,x,y,arg)
11928c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_PSPREL_P(f,p,t,a,o,arg)	desc_spill_psprel_p(p,t,a,o,arg)
11938c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_PSPREL(f,t,a,o,arg)	desc_spill_psprel_p(0,t,a,o,arg)
11948c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_SPREL_P(f,p,t,a,o,arg)	desc_spill_sprel_p(p,t,a,o,arg)
11958c2ecf20Sopenharmony_ci#define UNW_DEC_SPILL_SPREL(f,t,a,o,arg)	desc_spill_sprel_p(0,t,a,o,arg)
11968c2ecf20Sopenharmony_ci#define UNW_DEC_RESTORE_P(f,p,t,a,arg)		desc_restore_p(p,t,a,arg)
11978c2ecf20Sopenharmony_ci#define UNW_DEC_RESTORE(f,t,a,arg)		desc_restore_p(0,t,a,arg)
11988c2ecf20Sopenharmony_ci
11998c2ecf20Sopenharmony_ci#include "unwind_decoder.c"
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci
12028c2ecf20Sopenharmony_ci/* Unwind scripts. */
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic inline unw_hash_index_t
12058c2ecf20Sopenharmony_cihash (unsigned long ip)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	/* magic number = ((sqrt(5)-1)/2)*2^64 */
12088c2ecf20Sopenharmony_ci	static const unsigned long hashmagic = 0x9e3779b97f4a7c16UL;
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	return (ip >> 4) * hashmagic >> (64 - UNW_LOG_HASH_SIZE);
12118c2ecf20Sopenharmony_ci}
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_cistatic inline long
12148c2ecf20Sopenharmony_cicache_match (struct unw_script *script, unsigned long ip, unsigned long pr)
12158c2ecf20Sopenharmony_ci{
12168c2ecf20Sopenharmony_ci	read_lock(&script->lock);
12178c2ecf20Sopenharmony_ci	if (ip == script->ip && ((pr ^ script->pr_val) & script->pr_mask) == 0)
12188c2ecf20Sopenharmony_ci		/* keep the read lock... */
12198c2ecf20Sopenharmony_ci		return 1;
12208c2ecf20Sopenharmony_ci	read_unlock(&script->lock);
12218c2ecf20Sopenharmony_ci	return 0;
12228c2ecf20Sopenharmony_ci}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_cistatic inline struct unw_script *
12258c2ecf20Sopenharmony_ciscript_lookup (struct unw_frame_info *info)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	struct unw_script *script = unw.cache + info->hint;
12288c2ecf20Sopenharmony_ci	unsigned short index;
12298c2ecf20Sopenharmony_ci	unsigned long ip, pr;
12308c2ecf20Sopenharmony_ci
12318c2ecf20Sopenharmony_ci	if (UNW_DEBUG_ON(0))
12328c2ecf20Sopenharmony_ci		return NULL;	/* Always regenerate scripts in debug mode */
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	STAT(++unw.stat.cache.lookups);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	ip = info->ip;
12378c2ecf20Sopenharmony_ci	pr = info->pr;
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_ci	if (cache_match(script, ip, pr)) {
12408c2ecf20Sopenharmony_ci		STAT(++unw.stat.cache.hinted_hits);
12418c2ecf20Sopenharmony_ci		return script;
12428c2ecf20Sopenharmony_ci	}
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	index = unw.hash[hash(ip)];
12458c2ecf20Sopenharmony_ci	if (index >= UNW_CACHE_SIZE)
12468c2ecf20Sopenharmony_ci		return NULL;
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	script = unw.cache + index;
12498c2ecf20Sopenharmony_ci	while (1) {
12508c2ecf20Sopenharmony_ci		if (cache_match(script, ip, pr)) {
12518c2ecf20Sopenharmony_ci			/* update hint; no locking required as single-word writes are atomic */
12528c2ecf20Sopenharmony_ci			STAT(++unw.stat.cache.normal_hits);
12538c2ecf20Sopenharmony_ci			unw.cache[info->prev_script].hint = script - unw.cache;
12548c2ecf20Sopenharmony_ci			return script;
12558c2ecf20Sopenharmony_ci		}
12568c2ecf20Sopenharmony_ci		if (script->coll_chain >= UNW_HASH_SIZE)
12578c2ecf20Sopenharmony_ci			return NULL;
12588c2ecf20Sopenharmony_ci		script = unw.cache + script->coll_chain;
12598c2ecf20Sopenharmony_ci		STAT(++unw.stat.cache.collision_chain_traversals);
12608c2ecf20Sopenharmony_ci	}
12618c2ecf20Sopenharmony_ci}
12628c2ecf20Sopenharmony_ci
12638c2ecf20Sopenharmony_ci/*
12648c2ecf20Sopenharmony_ci * On returning, a write lock for the SCRIPT is still being held.
12658c2ecf20Sopenharmony_ci */
12668c2ecf20Sopenharmony_cistatic inline struct unw_script *
12678c2ecf20Sopenharmony_ciscript_new (unsigned long ip)
12688c2ecf20Sopenharmony_ci{
12698c2ecf20Sopenharmony_ci	struct unw_script *script, *prev, *tmp;
12708c2ecf20Sopenharmony_ci	unw_hash_index_t index;
12718c2ecf20Sopenharmony_ci	unsigned short head;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	STAT(++unw.stat.script.news);
12748c2ecf20Sopenharmony_ci
12758c2ecf20Sopenharmony_ci	/*
12768c2ecf20Sopenharmony_ci	 * Can't (easily) use cmpxchg() here because of ABA problem
12778c2ecf20Sopenharmony_ci	 * that is intrinsic in cmpxchg()...
12788c2ecf20Sopenharmony_ci	 */
12798c2ecf20Sopenharmony_ci	head = unw.lru_head;
12808c2ecf20Sopenharmony_ci	script = unw.cache + head;
12818c2ecf20Sopenharmony_ci	unw.lru_head = script->lru_chain;
12828c2ecf20Sopenharmony_ci
12838c2ecf20Sopenharmony_ci	/*
12848c2ecf20Sopenharmony_ci	 * We'd deadlock here if we interrupted a thread that is holding a read lock on
12858c2ecf20Sopenharmony_ci	 * script->lock.  Thus, if the write_trylock() fails, we simply bail out.  The
12868c2ecf20Sopenharmony_ci	 * alternative would be to disable interrupts whenever we hold a read-lock, but
12878c2ecf20Sopenharmony_ci	 * that seems silly.
12888c2ecf20Sopenharmony_ci	 */
12898c2ecf20Sopenharmony_ci	if (!write_trylock(&script->lock))
12908c2ecf20Sopenharmony_ci		return NULL;
12918c2ecf20Sopenharmony_ci
12928c2ecf20Sopenharmony_ci	/* re-insert script at the tail of the LRU chain: */
12938c2ecf20Sopenharmony_ci	unw.cache[unw.lru_tail].lru_chain = head;
12948c2ecf20Sopenharmony_ci	unw.lru_tail = head;
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	/* remove the old script from the hash table (if it's there): */
12978c2ecf20Sopenharmony_ci	if (script->ip) {
12988c2ecf20Sopenharmony_ci		index = hash(script->ip);
12998c2ecf20Sopenharmony_ci		tmp = unw.cache + unw.hash[index];
13008c2ecf20Sopenharmony_ci		prev = NULL;
13018c2ecf20Sopenharmony_ci		while (1) {
13028c2ecf20Sopenharmony_ci			if (tmp == script) {
13038c2ecf20Sopenharmony_ci				if (prev)
13048c2ecf20Sopenharmony_ci					prev->coll_chain = tmp->coll_chain;
13058c2ecf20Sopenharmony_ci				else
13068c2ecf20Sopenharmony_ci					unw.hash[index] = tmp->coll_chain;
13078c2ecf20Sopenharmony_ci				break;
13088c2ecf20Sopenharmony_ci			} else
13098c2ecf20Sopenharmony_ci				prev = tmp;
13108c2ecf20Sopenharmony_ci			if (tmp->coll_chain >= UNW_CACHE_SIZE)
13118c2ecf20Sopenharmony_ci			/* old script wasn't in the hash-table */
13128c2ecf20Sopenharmony_ci				break;
13138c2ecf20Sopenharmony_ci			tmp = unw.cache + tmp->coll_chain;
13148c2ecf20Sopenharmony_ci		}
13158c2ecf20Sopenharmony_ci	}
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	/* enter new script in the hash table */
13188c2ecf20Sopenharmony_ci	index = hash(ip);
13198c2ecf20Sopenharmony_ci	script->coll_chain = unw.hash[index];
13208c2ecf20Sopenharmony_ci	unw.hash[index] = script - unw.cache;
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	script->ip = ip;	/* set new IP while we're holding the locks */
13238c2ecf20Sopenharmony_ci
13248c2ecf20Sopenharmony_ci	STAT(if (script->coll_chain < UNW_CACHE_SIZE) ++unw.stat.script.collisions);
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	script->flags = 0;
13278c2ecf20Sopenharmony_ci	script->hint = 0;
13288c2ecf20Sopenharmony_ci	script->count = 0;
13298c2ecf20Sopenharmony_ci	return script;
13308c2ecf20Sopenharmony_ci}
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cistatic void
13338c2ecf20Sopenharmony_ciscript_finalize (struct unw_script *script, struct unw_state_record *sr)
13348c2ecf20Sopenharmony_ci{
13358c2ecf20Sopenharmony_ci	script->pr_mask = sr->pr_mask;
13368c2ecf20Sopenharmony_ci	script->pr_val = sr->pr_val;
13378c2ecf20Sopenharmony_ci	/*
13388c2ecf20Sopenharmony_ci	 * We could down-grade our write-lock on script->lock here but
13398c2ecf20Sopenharmony_ci	 * the rwlock API doesn't offer atomic lock downgrading, so
13408c2ecf20Sopenharmony_ci	 * we'll just keep the write-lock and release it later when
13418c2ecf20Sopenharmony_ci	 * we're done using the script.
13428c2ecf20Sopenharmony_ci	 */
13438c2ecf20Sopenharmony_ci}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_cistatic inline void
13468c2ecf20Sopenharmony_ciscript_emit (struct unw_script *script, struct unw_insn insn)
13478c2ecf20Sopenharmony_ci{
13488c2ecf20Sopenharmony_ci	if (script->count >= UNW_MAX_SCRIPT_LEN) {
13498c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: script exceeds maximum size of %u instructions!\n",
13508c2ecf20Sopenharmony_ci			__func__, UNW_MAX_SCRIPT_LEN);
13518c2ecf20Sopenharmony_ci		return;
13528c2ecf20Sopenharmony_ci	}
13538c2ecf20Sopenharmony_ci	script->insn[script->count++] = insn;
13548c2ecf20Sopenharmony_ci}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_cistatic inline void
13578c2ecf20Sopenharmony_ciemit_nat_info (struct unw_state_record *sr, int i, struct unw_script *script)
13588c2ecf20Sopenharmony_ci{
13598c2ecf20Sopenharmony_ci	struct unw_reg_info *r = sr->curr.reg + i;
13608c2ecf20Sopenharmony_ci	enum unw_insn_opcode opc;
13618c2ecf20Sopenharmony_ci	struct unw_insn insn;
13628c2ecf20Sopenharmony_ci	unsigned long val = 0;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci	switch (r->where) {
13658c2ecf20Sopenharmony_ci	      case UNW_WHERE_GR:
13668c2ecf20Sopenharmony_ci		if (r->val >= 32) {
13678c2ecf20Sopenharmony_ci			/* register got spilled to a stacked register */
13688c2ecf20Sopenharmony_ci			opc = UNW_INSN_SETNAT_TYPE;
13698c2ecf20Sopenharmony_ci			val = UNW_NAT_REGSTK;
13708c2ecf20Sopenharmony_ci		} else
13718c2ecf20Sopenharmony_ci			/* register got spilled to a scratch register */
13728c2ecf20Sopenharmony_ci			opc = UNW_INSN_SETNAT_MEMSTK;
13738c2ecf20Sopenharmony_ci		break;
13748c2ecf20Sopenharmony_ci
13758c2ecf20Sopenharmony_ci	      case UNW_WHERE_FR:
13768c2ecf20Sopenharmony_ci		opc = UNW_INSN_SETNAT_TYPE;
13778c2ecf20Sopenharmony_ci		val = UNW_NAT_VAL;
13788c2ecf20Sopenharmony_ci		break;
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci	      case UNW_WHERE_BR:
13818c2ecf20Sopenharmony_ci		opc = UNW_INSN_SETNAT_TYPE;
13828c2ecf20Sopenharmony_ci		val = UNW_NAT_NONE;
13838c2ecf20Sopenharmony_ci		break;
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_ci	      case UNW_WHERE_PSPREL:
13868c2ecf20Sopenharmony_ci	      case UNW_WHERE_SPREL:
13878c2ecf20Sopenharmony_ci		opc = UNW_INSN_SETNAT_MEMSTK;
13888c2ecf20Sopenharmony_ci		break;
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci	      default:
13918c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: don't know how to emit nat info for where = %u\n",
13928c2ecf20Sopenharmony_ci			   __func__, r->where);
13938c2ecf20Sopenharmony_ci		return;
13948c2ecf20Sopenharmony_ci	}
13958c2ecf20Sopenharmony_ci	insn.opc = opc;
13968c2ecf20Sopenharmony_ci	insn.dst = unw.preg_index[i];
13978c2ecf20Sopenharmony_ci	insn.val = val;
13988c2ecf20Sopenharmony_ci	script_emit(script, insn);
13998c2ecf20Sopenharmony_ci}
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_cistatic void
14028c2ecf20Sopenharmony_cicompile_reg (struct unw_state_record *sr, int i, struct unw_script *script)
14038c2ecf20Sopenharmony_ci{
14048c2ecf20Sopenharmony_ci	struct unw_reg_info *r = sr->curr.reg + i;
14058c2ecf20Sopenharmony_ci	enum unw_insn_opcode opc;
14068c2ecf20Sopenharmony_ci	unsigned long val, rval;
14078c2ecf20Sopenharmony_ci	struct unw_insn insn;
14088c2ecf20Sopenharmony_ci	long need_nat_info;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	if (r->where == UNW_WHERE_NONE || r->when >= sr->when_target)
14118c2ecf20Sopenharmony_ci		return;
14128c2ecf20Sopenharmony_ci
14138c2ecf20Sopenharmony_ci	opc = UNW_INSN_MOVE;
14148c2ecf20Sopenharmony_ci	val = rval = r->val;
14158c2ecf20Sopenharmony_ci	need_nat_info = (i >= UNW_REG_R4 && i <= UNW_REG_R7);
14168c2ecf20Sopenharmony_ci
14178c2ecf20Sopenharmony_ci	switch (r->where) {
14188c2ecf20Sopenharmony_ci	      case UNW_WHERE_GR:
14198c2ecf20Sopenharmony_ci		if (rval >= 32) {
14208c2ecf20Sopenharmony_ci			opc = UNW_INSN_MOVE_STACKED;
14218c2ecf20Sopenharmony_ci			val = rval - 32;
14228c2ecf20Sopenharmony_ci		} else if (rval >= 4 && rval <= 7) {
14238c2ecf20Sopenharmony_ci			if (need_nat_info) {
14248c2ecf20Sopenharmony_ci				opc = UNW_INSN_MOVE2;
14258c2ecf20Sopenharmony_ci				need_nat_info = 0;
14268c2ecf20Sopenharmony_ci			}
14278c2ecf20Sopenharmony_ci			val = unw.preg_index[UNW_REG_R4 + (rval - 4)];
14288c2ecf20Sopenharmony_ci		} else if (rval == 0) {
14298c2ecf20Sopenharmony_ci			opc = UNW_INSN_MOVE_CONST;
14308c2ecf20Sopenharmony_ci			val = 0;
14318c2ecf20Sopenharmony_ci		} else {
14328c2ecf20Sopenharmony_ci			/* register got spilled to a scratch register */
14338c2ecf20Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
14348c2ecf20Sopenharmony_ci			val = pt_regs_off(rval);
14358c2ecf20Sopenharmony_ci		}
14368c2ecf20Sopenharmony_ci		break;
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	      case UNW_WHERE_FR:
14398c2ecf20Sopenharmony_ci		if (rval <= 5)
14408c2ecf20Sopenharmony_ci			val = unw.preg_index[UNW_REG_F2  + (rval -  2)];
14418c2ecf20Sopenharmony_ci		else if (rval >= 16 && rval <= 31)
14428c2ecf20Sopenharmony_ci			val = unw.preg_index[UNW_REG_F16 + (rval - 16)];
14438c2ecf20Sopenharmony_ci		else {
14448c2ecf20Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
14458c2ecf20Sopenharmony_ci			if (rval <= 11)
14468c2ecf20Sopenharmony_ci				val = offsetof(struct pt_regs, f6) + 16*(rval - 6);
14478c2ecf20Sopenharmony_ci			else
14488c2ecf20Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: kernel may not touch f%lu\n",
14498c2ecf20Sopenharmony_ci					   __func__, rval);
14508c2ecf20Sopenharmony_ci		}
14518c2ecf20Sopenharmony_ci		break;
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	      case UNW_WHERE_BR:
14548c2ecf20Sopenharmony_ci		if (rval >= 1 && rval <= 5)
14558c2ecf20Sopenharmony_ci			val = unw.preg_index[UNW_REG_B1 + (rval - 1)];
14568c2ecf20Sopenharmony_ci		else {
14578c2ecf20Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
14588c2ecf20Sopenharmony_ci			if (rval == 0)
14598c2ecf20Sopenharmony_ci				val = offsetof(struct pt_regs, b0);
14608c2ecf20Sopenharmony_ci			else if (rval == 6)
14618c2ecf20Sopenharmony_ci				val = offsetof(struct pt_regs, b6);
14628c2ecf20Sopenharmony_ci			else
14638c2ecf20Sopenharmony_ci				val = offsetof(struct pt_regs, b7);
14648c2ecf20Sopenharmony_ci		}
14658c2ecf20Sopenharmony_ci		break;
14668c2ecf20Sopenharmony_ci
14678c2ecf20Sopenharmony_ci	      case UNW_WHERE_SPREL:
14688c2ecf20Sopenharmony_ci		opc = UNW_INSN_ADD_SP;
14698c2ecf20Sopenharmony_ci		break;
14708c2ecf20Sopenharmony_ci
14718c2ecf20Sopenharmony_ci	      case UNW_WHERE_PSPREL:
14728c2ecf20Sopenharmony_ci		opc = UNW_INSN_ADD_PSP;
14738c2ecf20Sopenharmony_ci		break;
14748c2ecf20Sopenharmony_ci
14758c2ecf20Sopenharmony_ci	      default:
14768c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind%s: register %u has unexpected `where' value of %u\n",
14778c2ecf20Sopenharmony_ci			   __func__, i, r->where);
14788c2ecf20Sopenharmony_ci		break;
14798c2ecf20Sopenharmony_ci	}
14808c2ecf20Sopenharmony_ci	insn.opc = opc;
14818c2ecf20Sopenharmony_ci	insn.dst = unw.preg_index[i];
14828c2ecf20Sopenharmony_ci	insn.val = val;
14838c2ecf20Sopenharmony_ci	script_emit(script, insn);
14848c2ecf20Sopenharmony_ci	if (need_nat_info)
14858c2ecf20Sopenharmony_ci		emit_nat_info(sr, i, script);
14868c2ecf20Sopenharmony_ci
14878c2ecf20Sopenharmony_ci	if (i == UNW_REG_PSP) {
14888c2ecf20Sopenharmony_ci		/*
14898c2ecf20Sopenharmony_ci		 * info->psp must contain the _value_ of the previous
14908c2ecf20Sopenharmony_ci		 * sp, not it's save location.  We get this by
14918c2ecf20Sopenharmony_ci		 * dereferencing the value we just stored in
14928c2ecf20Sopenharmony_ci		 * info->psp:
14938c2ecf20Sopenharmony_ci		 */
14948c2ecf20Sopenharmony_ci		insn.opc = UNW_INSN_LOAD;
14958c2ecf20Sopenharmony_ci		insn.dst = insn.val = unw.preg_index[UNW_REG_PSP];
14968c2ecf20Sopenharmony_ci		script_emit(script, insn);
14978c2ecf20Sopenharmony_ci	}
14988c2ecf20Sopenharmony_ci}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_cistatic inline const struct unw_table_entry *
15018c2ecf20Sopenharmony_cilookup (struct unw_table *table, unsigned long rel_ip)
15028c2ecf20Sopenharmony_ci{
15038c2ecf20Sopenharmony_ci	const struct unw_table_entry *e = NULL;
15048c2ecf20Sopenharmony_ci	unsigned long lo, hi, mid;
15058c2ecf20Sopenharmony_ci
15068c2ecf20Sopenharmony_ci	/* do a binary search for right entry: */
15078c2ecf20Sopenharmony_ci	for (lo = 0, hi = table->length; lo < hi; ) {
15088c2ecf20Sopenharmony_ci		mid = (lo + hi) / 2;
15098c2ecf20Sopenharmony_ci		e = &table->array[mid];
15108c2ecf20Sopenharmony_ci		if (rel_ip < e->start_offset)
15118c2ecf20Sopenharmony_ci			hi = mid;
15128c2ecf20Sopenharmony_ci		else if (rel_ip >= e->end_offset)
15138c2ecf20Sopenharmony_ci			lo = mid + 1;
15148c2ecf20Sopenharmony_ci		else
15158c2ecf20Sopenharmony_ci			break;
15168c2ecf20Sopenharmony_ci	}
15178c2ecf20Sopenharmony_ci	if (rel_ip < e->start_offset || rel_ip >= e->end_offset)
15188c2ecf20Sopenharmony_ci		return NULL;
15198c2ecf20Sopenharmony_ci	return e;
15208c2ecf20Sopenharmony_ci}
15218c2ecf20Sopenharmony_ci
15228c2ecf20Sopenharmony_ci/*
15238c2ecf20Sopenharmony_ci * Build an unwind script that unwinds from state OLD_STATE to the
15248c2ecf20Sopenharmony_ci * entrypoint of the function that called OLD_STATE.
15258c2ecf20Sopenharmony_ci */
15268c2ecf20Sopenharmony_cistatic inline struct unw_script *
15278c2ecf20Sopenharmony_cibuild_script (struct unw_frame_info *info)
15288c2ecf20Sopenharmony_ci{
15298c2ecf20Sopenharmony_ci	const struct unw_table_entry *e = NULL;
15308c2ecf20Sopenharmony_ci	struct unw_script *script = NULL;
15318c2ecf20Sopenharmony_ci	struct unw_labeled_state *ls, *next;
15328c2ecf20Sopenharmony_ci	unsigned long ip = info->ip;
15338c2ecf20Sopenharmony_ci	struct unw_state_record sr;
15348c2ecf20Sopenharmony_ci	struct unw_table *table, *prev;
15358c2ecf20Sopenharmony_ci	struct unw_reg_info *r;
15368c2ecf20Sopenharmony_ci	struct unw_insn insn;
15378c2ecf20Sopenharmony_ci	u8 *dp, *desc_end;
15388c2ecf20Sopenharmony_ci	u64 hdr;
15398c2ecf20Sopenharmony_ci	int i;
15408c2ecf20Sopenharmony_ci	STAT(unsigned long start, parse_start;)
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	STAT(++unw.stat.script.builds; start = ia64_get_itc());
15438c2ecf20Sopenharmony_ci
15448c2ecf20Sopenharmony_ci	/* build state record */
15458c2ecf20Sopenharmony_ci	memset(&sr, 0, sizeof(sr));
15468c2ecf20Sopenharmony_ci	for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
15478c2ecf20Sopenharmony_ci		r->when = UNW_WHEN_NEVER;
15488c2ecf20Sopenharmony_ci	sr.pr_val = info->pr;
15498c2ecf20Sopenharmony_ci
15508c2ecf20Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s: ip 0x%lx\n", __func__, ip);
15518c2ecf20Sopenharmony_ci	script = script_new(ip);
15528c2ecf20Sopenharmony_ci	if (!script) {
15538c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: failed to create unwind script\n",  __func__);
15548c2ecf20Sopenharmony_ci		STAT(unw.stat.script.build_time += ia64_get_itc() - start);
15558c2ecf20Sopenharmony_ci		return NULL;
15568c2ecf20Sopenharmony_ci	}
15578c2ecf20Sopenharmony_ci	unw.cache[info->prev_script].hint = script - unw.cache;
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	/* search the kernels and the modules' unwind tables for IP: */
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci	STAT(parse_start = ia64_get_itc());
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	prev = NULL;
15648c2ecf20Sopenharmony_ci	for (table = unw.tables; table; table = table->next) {
15658c2ecf20Sopenharmony_ci		if (ip >= table->start && ip < table->end) {
15668c2ecf20Sopenharmony_ci			/*
15678c2ecf20Sopenharmony_ci			 * Leave the kernel unwind table at the very front,
15688c2ecf20Sopenharmony_ci			 * lest moving it breaks some assumption elsewhere.
15698c2ecf20Sopenharmony_ci			 * Otherwise, move the matching table to the second
15708c2ecf20Sopenharmony_ci			 * position in the list so that traversals can benefit
15718c2ecf20Sopenharmony_ci			 * from commonality in backtrace paths.
15728c2ecf20Sopenharmony_ci			 */
15738c2ecf20Sopenharmony_ci			if (prev && prev != unw.tables) {
15748c2ecf20Sopenharmony_ci				/* unw is safe - we're already spinlocked */
15758c2ecf20Sopenharmony_ci				prev->next = table->next;
15768c2ecf20Sopenharmony_ci				table->next = unw.tables->next;
15778c2ecf20Sopenharmony_ci				unw.tables->next = table;
15788c2ecf20Sopenharmony_ci			}
15798c2ecf20Sopenharmony_ci			e = lookup(table, ip - table->segment_base);
15808c2ecf20Sopenharmony_ci			break;
15818c2ecf20Sopenharmony_ci		}
15828c2ecf20Sopenharmony_ci		prev = table;
15838c2ecf20Sopenharmony_ci	}
15848c2ecf20Sopenharmony_ci	if (!e) {
15858c2ecf20Sopenharmony_ci		/* no info, return default unwinder (leaf proc, no mem stack, no saved regs)  */
15868c2ecf20Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: no unwind info for ip=0x%lx (prev ip=0x%lx)\n",
15878c2ecf20Sopenharmony_ci			__func__, ip, unw.cache[info->prev_script].ip);
15888c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
15898c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].when = -1;
15908c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].val = 0;
15918c2ecf20Sopenharmony_ci		compile_reg(&sr, UNW_REG_RP, script);
15928c2ecf20Sopenharmony_ci		script_finalize(script, &sr);
15938c2ecf20Sopenharmony_ci		STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
15948c2ecf20Sopenharmony_ci		STAT(unw.stat.script.build_time += ia64_get_itc() - start);
15958c2ecf20Sopenharmony_ci		return script;
15968c2ecf20Sopenharmony_ci	}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_ci	sr.when_target = (3*((ip & ~0xfUL) - (table->segment_base + e->start_offset))/16
15998c2ecf20Sopenharmony_ci			  + (ip & 0xfUL));
16008c2ecf20Sopenharmony_ci	hdr = *(u64 *) (table->segment_base + e->info_offset);
16018c2ecf20Sopenharmony_ci	dp =   (u8 *)  (table->segment_base + e->info_offset + 8);
16028c2ecf20Sopenharmony_ci	desc_end = dp + 8*UNW_LENGTH(hdr);
16038c2ecf20Sopenharmony_ci
16048c2ecf20Sopenharmony_ci	while (!sr.done && dp < desc_end)
16058c2ecf20Sopenharmony_ci		dp = unw_decode(dp, sr.in_body, &sr);
16068c2ecf20Sopenharmony_ci
16078c2ecf20Sopenharmony_ci	if (sr.when_target > sr.epilogue_start) {
16088c2ecf20Sopenharmony_ci		/*
16098c2ecf20Sopenharmony_ci		 * sp has been restored and all values on the memory stack below
16108c2ecf20Sopenharmony_ci		 * psp also have been restored.
16118c2ecf20Sopenharmony_ci		 */
16128c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].val = 0;
16138c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].where = UNW_WHERE_NONE;
16148c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].when = UNW_WHEN_NEVER;
16158c2ecf20Sopenharmony_ci		for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
16168c2ecf20Sopenharmony_ci			if ((r->where == UNW_WHERE_PSPREL && r->val <= 0x10)
16178c2ecf20Sopenharmony_ci			    || r->where == UNW_WHERE_SPREL)
16188c2ecf20Sopenharmony_ci			{
16198c2ecf20Sopenharmony_ci				r->val = 0;
16208c2ecf20Sopenharmony_ci				r->where = UNW_WHERE_NONE;
16218c2ecf20Sopenharmony_ci				r->when = UNW_WHEN_NEVER;
16228c2ecf20Sopenharmony_ci			}
16238c2ecf20Sopenharmony_ci	}
16248c2ecf20Sopenharmony_ci
16258c2ecf20Sopenharmony_ci	script->flags = sr.flags;
16268c2ecf20Sopenharmony_ci
16278c2ecf20Sopenharmony_ci	/*
16288c2ecf20Sopenharmony_ci	 * If RP did't get saved, generate entry for the return link
16298c2ecf20Sopenharmony_ci	 * register.
16308c2ecf20Sopenharmony_ci	 */
16318c2ecf20Sopenharmony_ci	if (sr.curr.reg[UNW_REG_RP].when >= sr.when_target) {
16328c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
16338c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].when = -1;
16348c2ecf20Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].val = sr.return_link_reg;
16358c2ecf20Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: using default for rp at ip=0x%lx where=%d val=0x%lx\n",
16368c2ecf20Sopenharmony_ci			   __func__, ip, sr.curr.reg[UNW_REG_RP].where,
16378c2ecf20Sopenharmony_ci			   sr.curr.reg[UNW_REG_RP].val);
16388c2ecf20Sopenharmony_ci	}
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci#ifdef UNW_DEBUG
16418c2ecf20Sopenharmony_ci	UNW_DPRINT(1, "unwind.%s: state record for func 0x%lx, t=%u:\n",
16428c2ecf20Sopenharmony_ci		__func__, table->segment_base + e->start_offset, sr.when_target);
16438c2ecf20Sopenharmony_ci	for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r) {
16448c2ecf20Sopenharmony_ci		if (r->where != UNW_WHERE_NONE || r->when != UNW_WHEN_NEVER) {
16458c2ecf20Sopenharmony_ci			UNW_DPRINT(1, "  %s <- ", unw.preg_name[r - sr.curr.reg]);
16468c2ecf20Sopenharmony_ci			switch (r->where) {
16478c2ecf20Sopenharmony_ci			      case UNW_WHERE_GR:     UNW_DPRINT(1, "r%lu", r->val); break;
16488c2ecf20Sopenharmony_ci			      case UNW_WHERE_FR:     UNW_DPRINT(1, "f%lu", r->val); break;
16498c2ecf20Sopenharmony_ci			      case UNW_WHERE_BR:     UNW_DPRINT(1, "b%lu", r->val); break;
16508c2ecf20Sopenharmony_ci			      case UNW_WHERE_SPREL:  UNW_DPRINT(1, "[sp+0x%lx]", r->val); break;
16518c2ecf20Sopenharmony_ci			      case UNW_WHERE_PSPREL: UNW_DPRINT(1, "[psp+0x%lx]", r->val); break;
16528c2ecf20Sopenharmony_ci			      case UNW_WHERE_NONE:
16538c2ecf20Sopenharmony_ci				UNW_DPRINT(1, "%s+0x%lx", unw.preg_name[r - sr.curr.reg], r->val);
16548c2ecf20Sopenharmony_ci				break;
16558c2ecf20Sopenharmony_ci
16568c2ecf20Sopenharmony_ci			      default:
16578c2ecf20Sopenharmony_ci				UNW_DPRINT(1, "BADWHERE(%d)", r->where);
16588c2ecf20Sopenharmony_ci				break;
16598c2ecf20Sopenharmony_ci			}
16608c2ecf20Sopenharmony_ci			UNW_DPRINT(1, "\t\t%d\n", r->when);
16618c2ecf20Sopenharmony_ci		}
16628c2ecf20Sopenharmony_ci	}
16638c2ecf20Sopenharmony_ci#endif
16648c2ecf20Sopenharmony_ci
16658c2ecf20Sopenharmony_ci	STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	/* translate state record into unwinder instructions: */
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_ci	/*
16708c2ecf20Sopenharmony_ci	 * First, set psp if we're dealing with a fixed-size frame;
16718c2ecf20Sopenharmony_ci	 * subsequent instructions may depend on this value.
16728c2ecf20Sopenharmony_ci	 */
16738c2ecf20Sopenharmony_ci	if (sr.when_target > sr.curr.reg[UNW_REG_PSP].when
16748c2ecf20Sopenharmony_ci	    && (sr.curr.reg[UNW_REG_PSP].where == UNW_WHERE_NONE)
16758c2ecf20Sopenharmony_ci	    && sr.curr.reg[UNW_REG_PSP].val != 0) {
16768c2ecf20Sopenharmony_ci		/* new psp is sp plus frame size */
16778c2ecf20Sopenharmony_ci		insn.opc = UNW_INSN_ADD;
16788c2ecf20Sopenharmony_ci		insn.dst = offsetof(struct unw_frame_info, psp)/8;
16798c2ecf20Sopenharmony_ci		insn.val = sr.curr.reg[UNW_REG_PSP].val;	/* frame size */
16808c2ecf20Sopenharmony_ci		script_emit(script, insn);
16818c2ecf20Sopenharmony_ci	}
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	/* determine where the primary UNaT is: */
16848c2ecf20Sopenharmony_ci	if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
16858c2ecf20Sopenharmony_ci		i = UNW_REG_PRI_UNAT_MEM;
16868c2ecf20Sopenharmony_ci	else if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when)
16878c2ecf20Sopenharmony_ci		i = UNW_REG_PRI_UNAT_GR;
16888c2ecf20Sopenharmony_ci	else if (sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when > sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
16898c2ecf20Sopenharmony_ci		i = UNW_REG_PRI_UNAT_MEM;
16908c2ecf20Sopenharmony_ci	else
16918c2ecf20Sopenharmony_ci		i = UNW_REG_PRI_UNAT_GR;
16928c2ecf20Sopenharmony_ci
16938c2ecf20Sopenharmony_ci	compile_reg(&sr, i, script);
16948c2ecf20Sopenharmony_ci
16958c2ecf20Sopenharmony_ci	for (i = UNW_REG_BSP; i < UNW_NUM_REGS; ++i)
16968c2ecf20Sopenharmony_ci		compile_reg(&sr, i, script);
16978c2ecf20Sopenharmony_ci
16988c2ecf20Sopenharmony_ci	/* free labeled register states & stack: */
16998c2ecf20Sopenharmony_ci
17008c2ecf20Sopenharmony_ci	STAT(parse_start = ia64_get_itc());
17018c2ecf20Sopenharmony_ci	for (ls = sr.labeled_states; ls; ls = next) {
17028c2ecf20Sopenharmony_ci		next = ls->next;
17038c2ecf20Sopenharmony_ci		free_state_stack(&ls->saved_state);
17048c2ecf20Sopenharmony_ci		free_labeled_state(ls);
17058c2ecf20Sopenharmony_ci	}
17068c2ecf20Sopenharmony_ci	free_state_stack(&sr.curr);
17078c2ecf20Sopenharmony_ci	STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci	script_finalize(script, &sr);
17108c2ecf20Sopenharmony_ci	STAT(unw.stat.script.build_time += ia64_get_itc() - start);
17118c2ecf20Sopenharmony_ci	return script;
17128c2ecf20Sopenharmony_ci}
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci/*
17158c2ecf20Sopenharmony_ci * Apply the unwinding actions represented by OPS and update SR to
17168c2ecf20Sopenharmony_ci * reflect the state that existed upon entry to the function that this
17178c2ecf20Sopenharmony_ci * unwinder represents.
17188c2ecf20Sopenharmony_ci */
17198c2ecf20Sopenharmony_cistatic inline void
17208c2ecf20Sopenharmony_cirun_script (struct unw_script *script, struct unw_frame_info *state)
17218c2ecf20Sopenharmony_ci{
17228c2ecf20Sopenharmony_ci	struct unw_insn *ip, *limit, next_insn;
17238c2ecf20Sopenharmony_ci	unsigned long opc, dst, val, off;
17248c2ecf20Sopenharmony_ci	unsigned long *s = (unsigned long *) state;
17258c2ecf20Sopenharmony_ci	STAT(unsigned long start;)
17268c2ecf20Sopenharmony_ci
17278c2ecf20Sopenharmony_ci	STAT(++unw.stat.script.runs; start = ia64_get_itc());
17288c2ecf20Sopenharmony_ci	state->flags = script->flags;
17298c2ecf20Sopenharmony_ci	ip = script->insn;
17308c2ecf20Sopenharmony_ci	limit = script->insn + script->count;
17318c2ecf20Sopenharmony_ci	next_insn = *ip;
17328c2ecf20Sopenharmony_ci
17338c2ecf20Sopenharmony_ci	while (ip++ < limit) {
17348c2ecf20Sopenharmony_ci		opc = next_insn.opc;
17358c2ecf20Sopenharmony_ci		dst = next_insn.dst;
17368c2ecf20Sopenharmony_ci		val = next_insn.val;
17378c2ecf20Sopenharmony_ci		next_insn = *ip;
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	  redo:
17408c2ecf20Sopenharmony_ci		switch (opc) {
17418c2ecf20Sopenharmony_ci		      case UNW_INSN_ADD:
17428c2ecf20Sopenharmony_ci			s[dst] += val;
17438c2ecf20Sopenharmony_ci			break;
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci		      case UNW_INSN_MOVE2:
17468c2ecf20Sopenharmony_ci			if (!s[val])
17478c2ecf20Sopenharmony_ci				goto lazy_init;
17488c2ecf20Sopenharmony_ci			s[dst+1] = s[val+1];
17498c2ecf20Sopenharmony_ci			s[dst] = s[val];
17508c2ecf20Sopenharmony_ci			break;
17518c2ecf20Sopenharmony_ci
17528c2ecf20Sopenharmony_ci		      case UNW_INSN_MOVE:
17538c2ecf20Sopenharmony_ci			if (!s[val])
17548c2ecf20Sopenharmony_ci				goto lazy_init;
17558c2ecf20Sopenharmony_ci			s[dst] = s[val];
17568c2ecf20Sopenharmony_ci			break;
17578c2ecf20Sopenharmony_ci
17588c2ecf20Sopenharmony_ci		      case UNW_INSN_MOVE_SCRATCH:
17598c2ecf20Sopenharmony_ci			if (state->pt) {
17608c2ecf20Sopenharmony_ci				s[dst] = (unsigned long) get_scratch_regs(state) + val;
17618c2ecf20Sopenharmony_ci			} else {
17628c2ecf20Sopenharmony_ci				s[dst] = 0;
17638c2ecf20Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: no state->pt, dst=%ld, val=%ld\n",
17648c2ecf20Sopenharmony_ci					   __func__, dst, val);
17658c2ecf20Sopenharmony_ci			}
17668c2ecf20Sopenharmony_ci			break;
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci		      case UNW_INSN_MOVE_CONST:
17698c2ecf20Sopenharmony_ci			if (val == 0)
17708c2ecf20Sopenharmony_ci				s[dst] = (unsigned long) &unw.r0;
17718c2ecf20Sopenharmony_ci			else {
17728c2ecf20Sopenharmony_ci				s[dst] = 0;
17738c2ecf20Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: UNW_INSN_MOVE_CONST bad val=%ld\n",
17748c2ecf20Sopenharmony_ci					   __func__, val);
17758c2ecf20Sopenharmony_ci			}
17768c2ecf20Sopenharmony_ci			break;
17778c2ecf20Sopenharmony_ci
17788c2ecf20Sopenharmony_ci
17798c2ecf20Sopenharmony_ci		      case UNW_INSN_MOVE_STACKED:
17808c2ecf20Sopenharmony_ci			s[dst] = (unsigned long) ia64_rse_skip_regs((unsigned long *)state->bsp,
17818c2ecf20Sopenharmony_ci								    val);
17828c2ecf20Sopenharmony_ci			break;
17838c2ecf20Sopenharmony_ci
17848c2ecf20Sopenharmony_ci		      case UNW_INSN_ADD_PSP:
17858c2ecf20Sopenharmony_ci			s[dst] = state->psp + val;
17868c2ecf20Sopenharmony_ci			break;
17878c2ecf20Sopenharmony_ci
17888c2ecf20Sopenharmony_ci		      case UNW_INSN_ADD_SP:
17898c2ecf20Sopenharmony_ci			s[dst] = state->sp + val;
17908c2ecf20Sopenharmony_ci			break;
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_ci		      case UNW_INSN_SETNAT_MEMSTK:
17938c2ecf20Sopenharmony_ci			if (!state->pri_unat_loc)
17948c2ecf20Sopenharmony_ci				state->pri_unat_loc = &state->sw->caller_unat;
17958c2ecf20Sopenharmony_ci			/* register off. is a multiple of 8, so the least 3 bits (type) are 0 */
17968c2ecf20Sopenharmony_ci			s[dst+1] = ((unsigned long) state->pri_unat_loc - s[dst]) | UNW_NAT_MEMSTK;
17978c2ecf20Sopenharmony_ci			break;
17988c2ecf20Sopenharmony_ci
17998c2ecf20Sopenharmony_ci		      case UNW_INSN_SETNAT_TYPE:
18008c2ecf20Sopenharmony_ci			s[dst+1] = val;
18018c2ecf20Sopenharmony_ci			break;
18028c2ecf20Sopenharmony_ci
18038c2ecf20Sopenharmony_ci		      case UNW_INSN_LOAD:
18048c2ecf20Sopenharmony_ci#ifdef UNW_DEBUG
18058c2ecf20Sopenharmony_ci			if ((s[val] & (local_cpu_data->unimpl_va_mask | 0x7)) != 0
18068c2ecf20Sopenharmony_ci			    || s[val] < TASK_SIZE)
18078c2ecf20Sopenharmony_ci			{
18088c2ecf20Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: rejecting bad psp=0x%lx\n",
18098c2ecf20Sopenharmony_ci					   __func__, s[val]);
18108c2ecf20Sopenharmony_ci				break;
18118c2ecf20Sopenharmony_ci			}
18128c2ecf20Sopenharmony_ci#endif
18138c2ecf20Sopenharmony_ci			s[dst] = *(unsigned long *) s[val];
18148c2ecf20Sopenharmony_ci			break;
18158c2ecf20Sopenharmony_ci		}
18168c2ecf20Sopenharmony_ci	}
18178c2ecf20Sopenharmony_ci	STAT(unw.stat.script.run_time += ia64_get_itc() - start);
18188c2ecf20Sopenharmony_ci	return;
18198c2ecf20Sopenharmony_ci
18208c2ecf20Sopenharmony_ci  lazy_init:
18218c2ecf20Sopenharmony_ci	off = unw.sw_off[val];
18228c2ecf20Sopenharmony_ci	s[val] = (unsigned long) state->sw + off;
18238c2ecf20Sopenharmony_ci	if (off >= offsetof(struct switch_stack, r4) && off <= offsetof(struct switch_stack, r7))
18248c2ecf20Sopenharmony_ci		/*
18258c2ecf20Sopenharmony_ci		 * We're initializing a general register: init NaT info, too.  Note that
18268c2ecf20Sopenharmony_ci		 * the offset is a multiple of 8 which gives us the 3 bits needed for
18278c2ecf20Sopenharmony_ci		 * the type field.
18288c2ecf20Sopenharmony_ci		 */
18298c2ecf20Sopenharmony_ci		s[val+1] = (offsetof(struct switch_stack, ar_unat) - off) | UNW_NAT_MEMSTK;
18308c2ecf20Sopenharmony_ci	goto redo;
18318c2ecf20Sopenharmony_ci}
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_cistatic int
18348c2ecf20Sopenharmony_cifind_save_locs (struct unw_frame_info *info)
18358c2ecf20Sopenharmony_ci{
18368c2ecf20Sopenharmony_ci	int have_write_lock = 0;
18378c2ecf20Sopenharmony_ci	struct unw_script *scr;
18388c2ecf20Sopenharmony_ci	unsigned long flags = 0;
18398c2ecf20Sopenharmony_ci
18408c2ecf20Sopenharmony_ci	if ((info->ip & (local_cpu_data->unimpl_va_mask | 0xf)) || info->ip < TASK_SIZE) {
18418c2ecf20Sopenharmony_ci		/* don't let obviously bad addresses pollute the cache */
18428c2ecf20Sopenharmony_ci		/* FIXME: should really be level 0 but it occurs too often. KAO */
18438c2ecf20Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: rejecting bad ip=0x%lx\n", __func__, info->ip);
18448c2ecf20Sopenharmony_ci		info->rp_loc = NULL;
18458c2ecf20Sopenharmony_ci		return -1;
18468c2ecf20Sopenharmony_ci	}
18478c2ecf20Sopenharmony_ci
18488c2ecf20Sopenharmony_ci	scr = script_lookup(info);
18498c2ecf20Sopenharmony_ci	if (!scr) {
18508c2ecf20Sopenharmony_ci		spin_lock_irqsave(&unw.lock, flags);
18518c2ecf20Sopenharmony_ci		scr = build_script(info);
18528c2ecf20Sopenharmony_ci		if (!scr) {
18538c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&unw.lock, flags);
18548c2ecf20Sopenharmony_ci			UNW_DPRINT(0,
18558c2ecf20Sopenharmony_ci				   "unwind.%s: failed to locate/build unwind script for ip %lx\n",
18568c2ecf20Sopenharmony_ci				   __func__, info->ip);
18578c2ecf20Sopenharmony_ci			return -1;
18588c2ecf20Sopenharmony_ci		}
18598c2ecf20Sopenharmony_ci		have_write_lock = 1;
18608c2ecf20Sopenharmony_ci	}
18618c2ecf20Sopenharmony_ci	info->hint = scr->hint;
18628c2ecf20Sopenharmony_ci	info->prev_script = scr - unw.cache;
18638c2ecf20Sopenharmony_ci
18648c2ecf20Sopenharmony_ci	run_script(scr, info);
18658c2ecf20Sopenharmony_ci
18668c2ecf20Sopenharmony_ci	if (have_write_lock) {
18678c2ecf20Sopenharmony_ci		write_unlock(&scr->lock);
18688c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&unw.lock, flags);
18698c2ecf20Sopenharmony_ci	} else
18708c2ecf20Sopenharmony_ci		read_unlock(&scr->lock);
18718c2ecf20Sopenharmony_ci	return 0;
18728c2ecf20Sopenharmony_ci}
18738c2ecf20Sopenharmony_ci
18748c2ecf20Sopenharmony_cistatic int
18758c2ecf20Sopenharmony_ciunw_valid(const struct unw_frame_info *info, unsigned long* p)
18768c2ecf20Sopenharmony_ci{
18778c2ecf20Sopenharmony_ci	unsigned long loc = (unsigned long)p;
18788c2ecf20Sopenharmony_ci	return (loc >= info->regstk.limit && loc < info->regstk.top) ||
18798c2ecf20Sopenharmony_ci	       (loc >= info->memstk.top && loc < info->memstk.limit);
18808c2ecf20Sopenharmony_ci}
18818c2ecf20Sopenharmony_ci
18828c2ecf20Sopenharmony_ciint
18838c2ecf20Sopenharmony_ciunw_unwind (struct unw_frame_info *info)
18848c2ecf20Sopenharmony_ci{
18858c2ecf20Sopenharmony_ci	unsigned long prev_ip, prev_sp, prev_bsp;
18868c2ecf20Sopenharmony_ci	unsigned long ip, pr, num_regs;
18878c2ecf20Sopenharmony_ci	STAT(unsigned long start, flags;)
18888c2ecf20Sopenharmony_ci	int retval;
18898c2ecf20Sopenharmony_ci
18908c2ecf20Sopenharmony_ci	STAT(local_irq_save(flags); ++unw.stat.api.unwinds; start = ia64_get_itc());
18918c2ecf20Sopenharmony_ci
18928c2ecf20Sopenharmony_ci	prev_ip = info->ip;
18938c2ecf20Sopenharmony_ci	prev_sp = info->sp;
18948c2ecf20Sopenharmony_ci	prev_bsp = info->bsp;
18958c2ecf20Sopenharmony_ci
18968c2ecf20Sopenharmony_ci	/* validate the return IP pointer */
18978c2ecf20Sopenharmony_ci	if (!unw_valid(info, info->rp_loc)) {
18988c2ecf20Sopenharmony_ci		/* FIXME: should really be level 0 but it occurs too often. KAO */
18998c2ecf20Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: failed to locate return link (ip=0x%lx)!\n",
19008c2ecf20Sopenharmony_ci			   __func__, info->ip);
19018c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19028c2ecf20Sopenharmony_ci		return -1;
19038c2ecf20Sopenharmony_ci	}
19048c2ecf20Sopenharmony_ci	/* restore the ip */
19058c2ecf20Sopenharmony_ci	ip = info->ip = *info->rp_loc;
19068c2ecf20Sopenharmony_ci	if (ip < GATE_ADDR) {
19078c2ecf20Sopenharmony_ci		UNW_DPRINT(2, "unwind.%s: reached user-space (ip=0x%lx)\n", __func__, ip);
19088c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19098c2ecf20Sopenharmony_ci		return -1;
19108c2ecf20Sopenharmony_ci	}
19118c2ecf20Sopenharmony_ci
19128c2ecf20Sopenharmony_ci	/* validate the previous stack frame pointer */
19138c2ecf20Sopenharmony_ci	if (!unw_valid(info, info->pfs_loc)) {
19148c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: failed to locate ar.pfs!\n", __func__);
19158c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19168c2ecf20Sopenharmony_ci		return -1;
19178c2ecf20Sopenharmony_ci	}
19188c2ecf20Sopenharmony_ci	/* restore the cfm: */
19198c2ecf20Sopenharmony_ci	info->cfm_loc = info->pfs_loc;
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ci	/* restore the bsp: */
19228c2ecf20Sopenharmony_ci	pr = info->pr;
19238c2ecf20Sopenharmony_ci	num_regs = 0;
19248c2ecf20Sopenharmony_ci	if ((info->flags & UNW_FLAG_INTERRUPT_FRAME)) {
19258c2ecf20Sopenharmony_ci		info->pt = info->sp + 16;
19268c2ecf20Sopenharmony_ci		if ((pr & (1UL << PRED_NON_SYSCALL)) != 0)
19278c2ecf20Sopenharmony_ci			num_regs = *info->cfm_loc & 0x7f;		/* size of frame */
19288c2ecf20Sopenharmony_ci		info->pfs_loc =
19298c2ecf20Sopenharmony_ci			(unsigned long *) (info->pt + offsetof(struct pt_regs, ar_pfs));
19308c2ecf20Sopenharmony_ci		UNW_DPRINT(3, "unwind.%s: interrupt_frame pt 0x%lx\n", __func__, info->pt);
19318c2ecf20Sopenharmony_ci	} else
19328c2ecf20Sopenharmony_ci		num_regs = (*info->cfm_loc >> 7) & 0x7f;	/* size of locals */
19338c2ecf20Sopenharmony_ci	info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->bsp, -num_regs);
19348c2ecf20Sopenharmony_ci	if (info->bsp < info->regstk.limit || info->bsp > info->regstk.top) {
19358c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bsp (0x%lx) out of range [0x%lx-0x%lx]\n",
19368c2ecf20Sopenharmony_ci			__func__, info->bsp, info->regstk.limit, info->regstk.top);
19378c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19388c2ecf20Sopenharmony_ci		return -1;
19398c2ecf20Sopenharmony_ci	}
19408c2ecf20Sopenharmony_ci
19418c2ecf20Sopenharmony_ci	/* restore the sp: */
19428c2ecf20Sopenharmony_ci	info->sp = info->psp;
19438c2ecf20Sopenharmony_ci	if (info->sp < info->memstk.top || info->sp > info->memstk.limit) {
19448c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: sp (0x%lx) out of range [0x%lx-0x%lx]\n",
19458c2ecf20Sopenharmony_ci			__func__, info->sp, info->memstk.top, info->memstk.limit);
19468c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19478c2ecf20Sopenharmony_ci		return -1;
19488c2ecf20Sopenharmony_ci	}
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_ci	if (info->ip == prev_ip && info->sp == prev_sp && info->bsp == prev_bsp) {
19518c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ip, sp, bsp unchanged; stopping here (ip=0x%lx)\n",
19528c2ecf20Sopenharmony_ci			   __func__, ip);
19538c2ecf20Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19548c2ecf20Sopenharmony_ci		return -1;
19558c2ecf20Sopenharmony_ci	}
19568c2ecf20Sopenharmony_ci
19578c2ecf20Sopenharmony_ci	/* as we unwind, the saved ar.unat becomes the primary unat: */
19588c2ecf20Sopenharmony_ci	info->pri_unat_loc = info->unat_loc;
19598c2ecf20Sopenharmony_ci
19608c2ecf20Sopenharmony_ci	/* finally, restore the predicates: */
19618c2ecf20Sopenharmony_ci	unw_get_pr(info, &info->pr);
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	retval = find_save_locs(info);
19648c2ecf20Sopenharmony_ci	STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
19658c2ecf20Sopenharmony_ci	return retval;
19668c2ecf20Sopenharmony_ci}
19678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_unwind);
19688c2ecf20Sopenharmony_ci
19698c2ecf20Sopenharmony_ciint
19708c2ecf20Sopenharmony_ciunw_unwind_to_user (struct unw_frame_info *info)
19718c2ecf20Sopenharmony_ci{
19728c2ecf20Sopenharmony_ci	unsigned long ip, sp, pr = info->pr;
19738c2ecf20Sopenharmony_ci
19748c2ecf20Sopenharmony_ci	do {
19758c2ecf20Sopenharmony_ci		unw_get_sp(info, &sp);
19768c2ecf20Sopenharmony_ci		if ((long)((unsigned long)info->task + IA64_STK_OFFSET - sp)
19778c2ecf20Sopenharmony_ci		    < IA64_PT_REGS_SIZE) {
19788c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ran off the top of the kernel stack\n",
19798c2ecf20Sopenharmony_ci				   __func__);
19808c2ecf20Sopenharmony_ci			break;
19818c2ecf20Sopenharmony_ci		}
19828c2ecf20Sopenharmony_ci		if (unw_is_intr_frame(info) &&
19838c2ecf20Sopenharmony_ci		    (pr & (1UL << PRED_USER_STACK)))
19848c2ecf20Sopenharmony_ci			return 0;
19858c2ecf20Sopenharmony_ci		if (unw_get_pr (info, &pr) < 0) {
19868c2ecf20Sopenharmony_ci			unw_get_rp(info, &ip);
19878c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: failed to read "
19888c2ecf20Sopenharmony_ci				   "predicate register (ip=0x%lx)\n",
19898c2ecf20Sopenharmony_ci				__func__, ip);
19908c2ecf20Sopenharmony_ci			return -1;
19918c2ecf20Sopenharmony_ci		}
19928c2ecf20Sopenharmony_ci	} while (unw_unwind(info) >= 0);
19938c2ecf20Sopenharmony_ci	unw_get_ip(info, &ip);
19948c2ecf20Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: failed to unwind to user-level (ip=0x%lx)\n",
19958c2ecf20Sopenharmony_ci		   __func__, ip);
19968c2ecf20Sopenharmony_ci	return -1;
19978c2ecf20Sopenharmony_ci}
19988c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_unwind_to_user);
19998c2ecf20Sopenharmony_ci
20008c2ecf20Sopenharmony_cistatic void
20018c2ecf20Sopenharmony_ciinit_frame_info (struct unw_frame_info *info, struct task_struct *t,
20028c2ecf20Sopenharmony_ci		 struct switch_stack *sw, unsigned long stktop)
20038c2ecf20Sopenharmony_ci{
20048c2ecf20Sopenharmony_ci	unsigned long rbslimit, rbstop, stklimit;
20058c2ecf20Sopenharmony_ci	STAT(unsigned long start, flags;)
20068c2ecf20Sopenharmony_ci
20078c2ecf20Sopenharmony_ci	STAT(local_irq_save(flags); ++unw.stat.api.inits; start = ia64_get_itc());
20088c2ecf20Sopenharmony_ci
20098c2ecf20Sopenharmony_ci	/*
20108c2ecf20Sopenharmony_ci	 * Subtle stuff here: we _could_ unwind through the switch_stack frame but we
20118c2ecf20Sopenharmony_ci	 * don't want to do that because it would be slow as each preserved register would
20128c2ecf20Sopenharmony_ci	 * have to be processed.  Instead, what we do here is zero out the frame info and
20138c2ecf20Sopenharmony_ci	 * start the unwind process at the function that created the switch_stack frame.
20148c2ecf20Sopenharmony_ci	 * When a preserved value in switch_stack needs to be accessed, run_script() will
20158c2ecf20Sopenharmony_ci	 * initialize the appropriate pointer on demand.
20168c2ecf20Sopenharmony_ci	 */
20178c2ecf20Sopenharmony_ci	memset(info, 0, sizeof(*info));
20188c2ecf20Sopenharmony_ci
20198c2ecf20Sopenharmony_ci	rbslimit = (unsigned long) t + IA64_RBS_OFFSET;
20208c2ecf20Sopenharmony_ci	stklimit = (unsigned long) t + IA64_STK_OFFSET;
20218c2ecf20Sopenharmony_ci
20228c2ecf20Sopenharmony_ci	rbstop   = sw->ar_bspstore;
20238c2ecf20Sopenharmony_ci	if (rbstop > stklimit || rbstop < rbslimit)
20248c2ecf20Sopenharmony_ci		rbstop = rbslimit;
20258c2ecf20Sopenharmony_ci
20268c2ecf20Sopenharmony_ci	if (stktop <= rbstop)
20278c2ecf20Sopenharmony_ci		stktop = rbstop;
20288c2ecf20Sopenharmony_ci	if (stktop > stklimit)
20298c2ecf20Sopenharmony_ci		stktop = stklimit;
20308c2ecf20Sopenharmony_ci
20318c2ecf20Sopenharmony_ci	info->regstk.limit = rbslimit;
20328c2ecf20Sopenharmony_ci	info->regstk.top   = rbstop;
20338c2ecf20Sopenharmony_ci	info->memstk.limit = stklimit;
20348c2ecf20Sopenharmony_ci	info->memstk.top   = stktop;
20358c2ecf20Sopenharmony_ci	info->task = t;
20368c2ecf20Sopenharmony_ci	info->sw  = sw;
20378c2ecf20Sopenharmony_ci	info->sp = info->psp = stktop;
20388c2ecf20Sopenharmony_ci	info->pr = sw->pr;
20398c2ecf20Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s:\n"
20408c2ecf20Sopenharmony_ci		   "  task   0x%lx\n"
20418c2ecf20Sopenharmony_ci		   "  rbs = [0x%lx-0x%lx)\n"
20428c2ecf20Sopenharmony_ci		   "  stk = [0x%lx-0x%lx)\n"
20438c2ecf20Sopenharmony_ci		   "  pr     0x%lx\n"
20448c2ecf20Sopenharmony_ci		   "  sw     0x%lx\n"
20458c2ecf20Sopenharmony_ci		   "  sp     0x%lx\n",
20468c2ecf20Sopenharmony_ci		   __func__, (unsigned long) t, rbslimit, rbstop, stktop, stklimit,
20478c2ecf20Sopenharmony_ci		   info->pr, (unsigned long) info->sw, info->sp);
20488c2ecf20Sopenharmony_ci	STAT(unw.stat.api.init_time += ia64_get_itc() - start; local_irq_restore(flags));
20498c2ecf20Sopenharmony_ci}
20508c2ecf20Sopenharmony_ci
20518c2ecf20Sopenharmony_civoid
20528c2ecf20Sopenharmony_ciunw_init_frame_info (struct unw_frame_info *info, struct task_struct *t, struct switch_stack *sw)
20538c2ecf20Sopenharmony_ci{
20548c2ecf20Sopenharmony_ci	unsigned long sol;
20558c2ecf20Sopenharmony_ci
20568c2ecf20Sopenharmony_ci	init_frame_info(info, t, sw, (unsigned long) (sw + 1) - 16);
20578c2ecf20Sopenharmony_ci	info->cfm_loc = &sw->ar_pfs;
20588c2ecf20Sopenharmony_ci	sol = (*info->cfm_loc >> 7) & 0x7f;
20598c2ecf20Sopenharmony_ci	info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->regstk.top, -sol);
20608c2ecf20Sopenharmony_ci	info->ip = sw->b0;
20618c2ecf20Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s:\n"
20628c2ecf20Sopenharmony_ci		   "  bsp    0x%lx\n"
20638c2ecf20Sopenharmony_ci		   "  sol    0x%lx\n"
20648c2ecf20Sopenharmony_ci		   "  ip     0x%lx\n",
20658c2ecf20Sopenharmony_ci		   __func__, info->bsp, sol, info->ip);
20668c2ecf20Sopenharmony_ci	find_save_locs(info);
20678c2ecf20Sopenharmony_ci}
20688c2ecf20Sopenharmony_ci
20698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_init_frame_info);
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_civoid
20728c2ecf20Sopenharmony_ciunw_init_from_blocked_task (struct unw_frame_info *info, struct task_struct *t)
20738c2ecf20Sopenharmony_ci{
20748c2ecf20Sopenharmony_ci	struct switch_stack *sw = (struct switch_stack *) (t->thread.ksp + 16);
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	UNW_DPRINT(1, "unwind.%s\n", __func__);
20778c2ecf20Sopenharmony_ci	unw_init_frame_info(info, t, sw);
20788c2ecf20Sopenharmony_ci}
20798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(unw_init_from_blocked_task);
20808c2ecf20Sopenharmony_ci
20818c2ecf20Sopenharmony_cistatic void
20828c2ecf20Sopenharmony_ciinit_unwind_table (struct unw_table *table, const char *name, unsigned long segment_base,
20838c2ecf20Sopenharmony_ci		   unsigned long gp, const void *table_start, const void *table_end)
20848c2ecf20Sopenharmony_ci{
20858c2ecf20Sopenharmony_ci	const struct unw_table_entry *start = table_start, *end = table_end;
20868c2ecf20Sopenharmony_ci
20878c2ecf20Sopenharmony_ci	table->name = name;
20888c2ecf20Sopenharmony_ci	table->segment_base = segment_base;
20898c2ecf20Sopenharmony_ci	table->gp = gp;
20908c2ecf20Sopenharmony_ci	table->start = segment_base + start[0].start_offset;
20918c2ecf20Sopenharmony_ci	table->end = segment_base + end[-1].end_offset;
20928c2ecf20Sopenharmony_ci	table->array = start;
20938c2ecf20Sopenharmony_ci	table->length = end - start;
20948c2ecf20Sopenharmony_ci}
20958c2ecf20Sopenharmony_ci
20968c2ecf20Sopenharmony_civoid *
20978c2ecf20Sopenharmony_ciunw_add_unwind_table (const char *name, unsigned long segment_base, unsigned long gp,
20988c2ecf20Sopenharmony_ci		      const void *table_start, const void *table_end)
20998c2ecf20Sopenharmony_ci{
21008c2ecf20Sopenharmony_ci	const struct unw_table_entry *start = table_start, *end = table_end;
21018c2ecf20Sopenharmony_ci	struct unw_table *table;
21028c2ecf20Sopenharmony_ci	unsigned long flags;
21038c2ecf20Sopenharmony_ci
21048c2ecf20Sopenharmony_ci	if (end - start <= 0) {
21058c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ignoring attempt to insert empty unwind table\n",
21068c2ecf20Sopenharmony_ci			   __func__);
21078c2ecf20Sopenharmony_ci		return NULL;
21088c2ecf20Sopenharmony_ci	}
21098c2ecf20Sopenharmony_ci
21108c2ecf20Sopenharmony_ci	table = kmalloc(sizeof(*table), GFP_USER);
21118c2ecf20Sopenharmony_ci	if (!table)
21128c2ecf20Sopenharmony_ci		return NULL;
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	init_unwind_table(table, name, segment_base, gp, table_start, table_end);
21158c2ecf20Sopenharmony_ci
21168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&unw.lock, flags);
21178c2ecf20Sopenharmony_ci	{
21188c2ecf20Sopenharmony_ci		/* keep kernel unwind table at the front (it's searched most commonly): */
21198c2ecf20Sopenharmony_ci		table->next = unw.tables->next;
21208c2ecf20Sopenharmony_ci		unw.tables->next = table;
21218c2ecf20Sopenharmony_ci	}
21228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&unw.lock, flags);
21238c2ecf20Sopenharmony_ci
21248c2ecf20Sopenharmony_ci	return table;
21258c2ecf20Sopenharmony_ci}
21268c2ecf20Sopenharmony_ci
21278c2ecf20Sopenharmony_civoid
21288c2ecf20Sopenharmony_ciunw_remove_unwind_table (void *handle)
21298c2ecf20Sopenharmony_ci{
21308c2ecf20Sopenharmony_ci	struct unw_table *table, *prev;
21318c2ecf20Sopenharmony_ci	struct unw_script *tmp;
21328c2ecf20Sopenharmony_ci	unsigned long flags;
21338c2ecf20Sopenharmony_ci	long index;
21348c2ecf20Sopenharmony_ci
21358c2ecf20Sopenharmony_ci	if (!handle) {
21368c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ignoring attempt to remove non-existent unwind table\n",
21378c2ecf20Sopenharmony_ci			   __func__);
21388c2ecf20Sopenharmony_ci		return;
21398c2ecf20Sopenharmony_ci	}
21408c2ecf20Sopenharmony_ci
21418c2ecf20Sopenharmony_ci	table = handle;
21428c2ecf20Sopenharmony_ci	if (table == &unw.kernel_table) {
21438c2ecf20Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: sorry, freeing the kernel's unwind table is a "
21448c2ecf20Sopenharmony_ci			   "no-can-do!\n", __func__);
21458c2ecf20Sopenharmony_ci		return;
21468c2ecf20Sopenharmony_ci	}
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_ci	spin_lock_irqsave(&unw.lock, flags);
21498c2ecf20Sopenharmony_ci	{
21508c2ecf20Sopenharmony_ci		/* first, delete the table: */
21518c2ecf20Sopenharmony_ci
21528c2ecf20Sopenharmony_ci		for (prev = (struct unw_table *) &unw.tables; prev; prev = prev->next)
21538c2ecf20Sopenharmony_ci			if (prev->next == table)
21548c2ecf20Sopenharmony_ci				break;
21558c2ecf20Sopenharmony_ci		if (!prev) {
21568c2ecf20Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: failed to find unwind table %p\n",
21578c2ecf20Sopenharmony_ci				   __func__, (void *) table);
21588c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&unw.lock, flags);
21598c2ecf20Sopenharmony_ci			return;
21608c2ecf20Sopenharmony_ci		}
21618c2ecf20Sopenharmony_ci		prev->next = table->next;
21628c2ecf20Sopenharmony_ci	}
21638c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&unw.lock, flags);
21648c2ecf20Sopenharmony_ci
21658c2ecf20Sopenharmony_ci	/* next, remove hash table entries for this table */
21668c2ecf20Sopenharmony_ci
21678c2ecf20Sopenharmony_ci	for (index = 0; index < UNW_HASH_SIZE; ++index) {
21688c2ecf20Sopenharmony_ci		tmp = unw.cache + unw.hash[index];
21698c2ecf20Sopenharmony_ci		if (unw.hash[index] >= UNW_CACHE_SIZE
21708c2ecf20Sopenharmony_ci		    || tmp->ip < table->start || tmp->ip >= table->end)
21718c2ecf20Sopenharmony_ci			continue;
21728c2ecf20Sopenharmony_ci
21738c2ecf20Sopenharmony_ci		write_lock(&tmp->lock);
21748c2ecf20Sopenharmony_ci		{
21758c2ecf20Sopenharmony_ci			if (tmp->ip >= table->start && tmp->ip < table->end) {
21768c2ecf20Sopenharmony_ci				unw.hash[index] = tmp->coll_chain;
21778c2ecf20Sopenharmony_ci				tmp->ip = 0;
21788c2ecf20Sopenharmony_ci			}
21798c2ecf20Sopenharmony_ci		}
21808c2ecf20Sopenharmony_ci		write_unlock(&tmp->lock);
21818c2ecf20Sopenharmony_ci	}
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	kfree(table);
21848c2ecf20Sopenharmony_ci}
21858c2ecf20Sopenharmony_ci
21868c2ecf20Sopenharmony_cistatic int __init
21878c2ecf20Sopenharmony_cicreate_gate_table (void)
21888c2ecf20Sopenharmony_ci{
21898c2ecf20Sopenharmony_ci	const struct unw_table_entry *entry, *start, *end;
21908c2ecf20Sopenharmony_ci	unsigned long *lp, segbase = GATE_ADDR;
21918c2ecf20Sopenharmony_ci	size_t info_size, size;
21928c2ecf20Sopenharmony_ci	char *info;
21938c2ecf20Sopenharmony_ci	Elf64_Phdr *punw = NULL, *phdr = (Elf64_Phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
21948c2ecf20Sopenharmony_ci	int i;
21958c2ecf20Sopenharmony_ci
21968c2ecf20Sopenharmony_ci	for (i = 0; i < GATE_EHDR->e_phnum; ++i, ++phdr)
21978c2ecf20Sopenharmony_ci		if (phdr->p_type == PT_IA_64_UNWIND) {
21988c2ecf20Sopenharmony_ci			punw = phdr;
21998c2ecf20Sopenharmony_ci			break;
22008c2ecf20Sopenharmony_ci		}
22018c2ecf20Sopenharmony_ci
22028c2ecf20Sopenharmony_ci	if (!punw) {
22038c2ecf20Sopenharmony_ci		printk("%s: failed to find gate DSO's unwind table!\n", __func__);
22048c2ecf20Sopenharmony_ci		return 0;
22058c2ecf20Sopenharmony_ci	}
22068c2ecf20Sopenharmony_ci
22078c2ecf20Sopenharmony_ci	start = (const struct unw_table_entry *) punw->p_vaddr;
22088c2ecf20Sopenharmony_ci	end = (struct unw_table_entry *) ((char *) start + punw->p_memsz);
22098c2ecf20Sopenharmony_ci	size  = 0;
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_ci	unw_add_unwind_table("linux-gate.so", segbase, 0, start, end);
22128c2ecf20Sopenharmony_ci
22138c2ecf20Sopenharmony_ci	for (entry = start; entry < end; ++entry)
22148c2ecf20Sopenharmony_ci		size += 3*8 + 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
22158c2ecf20Sopenharmony_ci	size += 8;	/* reserve space for "end of table" marker */
22168c2ecf20Sopenharmony_ci
22178c2ecf20Sopenharmony_ci	unw.gate_table = kmalloc(size, GFP_KERNEL);
22188c2ecf20Sopenharmony_ci	if (!unw.gate_table) {
22198c2ecf20Sopenharmony_ci		unw.gate_table_size = 0;
22208c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: unable to create unwind data for gate page!\n", __func__);
22218c2ecf20Sopenharmony_ci		return 0;
22228c2ecf20Sopenharmony_ci	}
22238c2ecf20Sopenharmony_ci	unw.gate_table_size = size;
22248c2ecf20Sopenharmony_ci
22258c2ecf20Sopenharmony_ci	lp = unw.gate_table;
22268c2ecf20Sopenharmony_ci	info = (char *) unw.gate_table + size;
22278c2ecf20Sopenharmony_ci
22288c2ecf20Sopenharmony_ci	for (entry = start; entry < end; ++entry, lp += 3) {
22298c2ecf20Sopenharmony_ci		info_size = 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
22308c2ecf20Sopenharmony_ci		info -= info_size;
22318c2ecf20Sopenharmony_ci		memcpy(info, (char *) segbase + entry->info_offset, info_size);
22328c2ecf20Sopenharmony_ci
22338c2ecf20Sopenharmony_ci		lp[0] = segbase + entry->start_offset;		/* start */
22348c2ecf20Sopenharmony_ci		lp[1] = segbase + entry->end_offset;		/* end */
22358c2ecf20Sopenharmony_ci		lp[2] = info - (char *) unw.gate_table;		/* info */
22368c2ecf20Sopenharmony_ci	}
22378c2ecf20Sopenharmony_ci	*lp = 0;	/* end-of-table marker */
22388c2ecf20Sopenharmony_ci	return 0;
22398c2ecf20Sopenharmony_ci}
22408c2ecf20Sopenharmony_ci
22418c2ecf20Sopenharmony_ci__initcall(create_gate_table);
22428c2ecf20Sopenharmony_ci
22438c2ecf20Sopenharmony_civoid __init
22448c2ecf20Sopenharmony_ciunw_init (void)
22458c2ecf20Sopenharmony_ci{
22468c2ecf20Sopenharmony_ci	extern char __gp[];
22478c2ecf20Sopenharmony_ci	extern void unw_hash_index_t_is_too_narrow (void);
22488c2ecf20Sopenharmony_ci	long i, off;
22498c2ecf20Sopenharmony_ci
22508c2ecf20Sopenharmony_ci	if (8*sizeof(unw_hash_index_t) < UNW_LOG_HASH_SIZE)
22518c2ecf20Sopenharmony_ci		unw_hash_index_t_is_too_narrow();
22528c2ecf20Sopenharmony_ci
22538c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PRI_UNAT_GR]] = SW(CALLER_UNAT);
22548c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_BSPSTORE]] = SW(AR_BSPSTORE);
22558c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PFS]] = SW(AR_PFS);
22568c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_RP]] = SW(B0);
22578c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_UNAT]] = SW(CALLER_UNAT);
22588c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PR]] = SW(PR);
22598c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_LC]] = SW(AR_LC);
22608c2ecf20Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_FPSR]] = SW(AR_FPSR);
22618c2ecf20Sopenharmony_ci	for (i = UNW_REG_R4, off = SW(R4); i <= UNW_REG_R7; ++i, off += 8)
22628c2ecf20Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
22638c2ecf20Sopenharmony_ci	for (i = UNW_REG_B1, off = SW(B1); i <= UNW_REG_B5; ++i, off += 8)
22648c2ecf20Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
22658c2ecf20Sopenharmony_ci	for (i = UNW_REG_F2, off = SW(F2); i <= UNW_REG_F5; ++i, off += 16)
22668c2ecf20Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
22678c2ecf20Sopenharmony_ci	for (i = UNW_REG_F16, off = SW(F16); i <= UNW_REG_F31; ++i, off += 16)
22688c2ecf20Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci	for (i = 0; i < UNW_CACHE_SIZE; ++i) {
22718c2ecf20Sopenharmony_ci		if (i > 0)
22728c2ecf20Sopenharmony_ci			unw.cache[i].lru_chain = (i - 1);
22738c2ecf20Sopenharmony_ci		unw.cache[i].coll_chain = -1;
22748c2ecf20Sopenharmony_ci		rwlock_init(&unw.cache[i].lock);
22758c2ecf20Sopenharmony_ci	}
22768c2ecf20Sopenharmony_ci	unw.lru_head = UNW_CACHE_SIZE - 1;
22778c2ecf20Sopenharmony_ci	unw.lru_tail = 0;
22788c2ecf20Sopenharmony_ci
22798c2ecf20Sopenharmony_ci	init_unwind_table(&unw.kernel_table, "kernel", KERNEL_START, (unsigned long) __gp,
22808c2ecf20Sopenharmony_ci			  __start_unwind, __end_unwind);
22818c2ecf20Sopenharmony_ci}
22828c2ecf20Sopenharmony_ci
22838c2ecf20Sopenharmony_ci/*
22848c2ecf20Sopenharmony_ci * DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED
22858c2ecf20Sopenharmony_ci *
22868c2ecf20Sopenharmony_ci *	This system call has been deprecated.  The new and improved way to get
22878c2ecf20Sopenharmony_ci *	at the kernel's unwind info is via the gate DSO.  The address of the
22888c2ecf20Sopenharmony_ci *	ELF header for this DSO is passed to user-level via AT_SYSINFO_EHDR.
22898c2ecf20Sopenharmony_ci *
22908c2ecf20Sopenharmony_ci * DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED
22918c2ecf20Sopenharmony_ci *
22928c2ecf20Sopenharmony_ci * This system call copies the unwind data into the buffer pointed to by BUF and returns
22938c2ecf20Sopenharmony_ci * the size of the unwind data.  If BUF_SIZE is smaller than the size of the unwind data
22948c2ecf20Sopenharmony_ci * or if BUF is NULL, nothing is copied, but the system call still returns the size of the
22958c2ecf20Sopenharmony_ci * unwind data.
22968c2ecf20Sopenharmony_ci *
22978c2ecf20Sopenharmony_ci * The first portion of the unwind data contains an unwind table and rest contains the
22988c2ecf20Sopenharmony_ci * associated unwind info (in no particular order).  The unwind table consists of a table
22998c2ecf20Sopenharmony_ci * of entries of the form:
23008c2ecf20Sopenharmony_ci *
23018c2ecf20Sopenharmony_ci *	u64 start;	(64-bit address of start of function)
23028c2ecf20Sopenharmony_ci *	u64 end;	(64-bit address of start of function)
23038c2ecf20Sopenharmony_ci *	u64 info;	(BUF-relative offset to unwind info)
23048c2ecf20Sopenharmony_ci *
23058c2ecf20Sopenharmony_ci * The end of the unwind table is indicated by an entry with a START address of zero.
23068c2ecf20Sopenharmony_ci *
23078c2ecf20Sopenharmony_ci * Please see the IA-64 Software Conventions and Runtime Architecture manual for details
23088c2ecf20Sopenharmony_ci * on the format of the unwind info.
23098c2ecf20Sopenharmony_ci *
23108c2ecf20Sopenharmony_ci * ERRORS
23118c2ecf20Sopenharmony_ci *	EFAULT	BUF points outside your accessible address space.
23128c2ecf20Sopenharmony_ci */
23138c2ecf20Sopenharmony_ciasmlinkage long
23148c2ecf20Sopenharmony_cisys_getunwind (void __user *buf, size_t buf_size)
23158c2ecf20Sopenharmony_ci{
23168c2ecf20Sopenharmony_ci	if (buf && buf_size >= unw.gate_table_size)
23178c2ecf20Sopenharmony_ci		if (copy_to_user(buf, unw.gate_table, unw.gate_table_size) != 0)
23188c2ecf20Sopenharmony_ci			return -EFAULT;
23198c2ecf20Sopenharmony_ci	return unw.gate_table_size;
23208c2ecf20Sopenharmony_ci}
2321