xref: /kernel/linux/linux-6.6/arch/ia64/kernel/unwind.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 1999-2004 Hewlett-Packard Co
462306a36Sopenharmony_ci *	David Mosberger-Tang <davidm@hpl.hp.com>
562306a36Sopenharmony_ci * Copyright (C) 2003 Fenghua Yu <fenghua.yu@intel.com>
662306a36Sopenharmony_ci * 	- Change pt_regs_off() to make it less dependent on pt_regs structure.
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci/*
962306a36Sopenharmony_ci * This file implements call frame unwind support for the Linux
1062306a36Sopenharmony_ci * kernel.  Parsing and processing the unwind information is
1162306a36Sopenharmony_ci * time-consuming, so this implementation translates the unwind
1262306a36Sopenharmony_ci * descriptors into unwind scripts.  These scripts are very simple
1362306a36Sopenharmony_ci * (basically a sequence of assignments) and efficient to execute.
1462306a36Sopenharmony_ci * They are cached for later re-use.  Each script is specific for a
1562306a36Sopenharmony_ci * given instruction pointer address and the set of predicate values
1662306a36Sopenharmony_ci * that the script depends on (most unwind descriptors are
1762306a36Sopenharmony_ci * unconditional and scripts often do not depend on predicates at
1862306a36Sopenharmony_ci * all).  This code is based on the unwind conventions described in
1962306a36Sopenharmony_ci * the "IA-64 Software Conventions and Runtime Architecture" manual.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * SMP conventions:
2262306a36Sopenharmony_ci *	o updates to the global unwind data (in structure "unw") are serialized
2362306a36Sopenharmony_ci *	  by the unw.lock spinlock
2462306a36Sopenharmony_ci *	o each unwind script has its own read-write lock; a thread must acquire
2562306a36Sopenharmony_ci *	  a read lock before executing a script and must acquire a write lock
2662306a36Sopenharmony_ci *	  before modifying a script
2762306a36Sopenharmony_ci *	o if both the unw.lock spinlock and a script's read-write lock must be
2862306a36Sopenharmony_ci *	  acquired, then the read-write lock must be acquired first.
2962306a36Sopenharmony_ci */
3062306a36Sopenharmony_ci#include <linux/module.h>
3162306a36Sopenharmony_ci#include <linux/memblock.h>
3262306a36Sopenharmony_ci#include <linux/elf.h>
3362306a36Sopenharmony_ci#include <linux/kernel.h>
3462306a36Sopenharmony_ci#include <linux/sched.h>
3562306a36Sopenharmony_ci#include <linux/slab.h>
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include <asm/unwind.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include <asm/delay.h>
4062306a36Sopenharmony_ci#include <asm/page.h>
4162306a36Sopenharmony_ci#include <asm/ptrace.h>
4262306a36Sopenharmony_ci#include <asm/ptrace_offsets.h>
4362306a36Sopenharmony_ci#include <asm/rse.h>
4462306a36Sopenharmony_ci#include <asm/sections.h>
4562306a36Sopenharmony_ci#include <linux/uaccess.h>
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#include "entry.h"
4862306a36Sopenharmony_ci#include "unwind_i.h"
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define UNW_LOG_CACHE_SIZE	7	/* each unw_script is ~256 bytes in size */
5162306a36Sopenharmony_ci#define UNW_CACHE_SIZE		(1 << UNW_LOG_CACHE_SIZE)
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define UNW_LOG_HASH_SIZE	(UNW_LOG_CACHE_SIZE + 1)
5462306a36Sopenharmony_ci#define UNW_HASH_SIZE		(1 << UNW_LOG_HASH_SIZE)
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci#define UNW_STATS	0	/* WARNING: this disabled interrupts for long time-spans!! */
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#ifdef UNW_DEBUG
5962306a36Sopenharmony_ci  static unsigned int unw_debug_level = UNW_DEBUG;
6062306a36Sopenharmony_ci#  define UNW_DEBUG_ON(n)	unw_debug_level >= n
6162306a36Sopenharmony_ci   /* Do not code a printk level, not all debug lines end in newline */
6262306a36Sopenharmony_ci#  define UNW_DPRINT(n, ...)  if (UNW_DEBUG_ON(n)) printk(__VA_ARGS__)
6362306a36Sopenharmony_ci#  undef inline
6462306a36Sopenharmony_ci#  define inline
6562306a36Sopenharmony_ci#else /* !UNW_DEBUG */
6662306a36Sopenharmony_ci#  define UNW_DEBUG_ON(n)  0
6762306a36Sopenharmony_ci#  define UNW_DPRINT(n, ...)
6862306a36Sopenharmony_ci#endif /* UNW_DEBUG */
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#if UNW_STATS
7162306a36Sopenharmony_ci# define STAT(x...)	x
7262306a36Sopenharmony_ci#else
7362306a36Sopenharmony_ci# define STAT(x...)
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#define alloc_reg_state()	kmalloc(sizeof(struct unw_reg_state), GFP_ATOMIC)
7762306a36Sopenharmony_ci#define free_reg_state(usr)	kfree(usr)
7862306a36Sopenharmony_ci#define alloc_labeled_state()	kmalloc(sizeof(struct unw_labeled_state), GFP_ATOMIC)
7962306a36Sopenharmony_ci#define free_labeled_state(usr)	kfree(usr)
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_citypedef unsigned long unw_word;
8262306a36Sopenharmony_citypedef unsigned char unw_hash_index_t;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_cistatic struct {
8562306a36Sopenharmony_ci	spinlock_t lock;			/* spinlock for unwind data */
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci	/* list of unwind tables (one per load-module) */
8862306a36Sopenharmony_ci	struct unw_table *tables;
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	unsigned long r0;			/* constant 0 for r0 */
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	/* table of registers that prologues can save (and order in which they're saved): */
9362306a36Sopenharmony_ci	const unsigned char save_order[8];
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	/* maps a preserved register index (preg_index) to corresponding switch_stack offset: */
9662306a36Sopenharmony_ci	unsigned short sw_off[sizeof(struct unw_frame_info) / 8];
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci	unsigned short lru_head;		/* index of lead-recently used script */
9962306a36Sopenharmony_ci	unsigned short lru_tail;		/* index of most-recently used script */
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* index into unw_frame_info for preserved register i */
10262306a36Sopenharmony_ci	unsigned short preg_index[UNW_NUM_REGS];
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	short pt_regs_offsets[32];
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* unwind table for the kernel: */
10762306a36Sopenharmony_ci	struct unw_table kernel_table;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	/* unwind table describing the gate page (kernel code that is mapped into user space): */
11062306a36Sopenharmony_ci	size_t gate_table_size;
11162306a36Sopenharmony_ci	unsigned long *gate_table;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/* hash table that maps instruction pointer to script index: */
11462306a36Sopenharmony_ci	unsigned short hash[UNW_HASH_SIZE];
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/* script cache: */
11762306a36Sopenharmony_ci	struct unw_script cache[UNW_CACHE_SIZE];
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci# ifdef UNW_DEBUG
12062306a36Sopenharmony_ci	const char *preg_name[UNW_NUM_REGS];
12162306a36Sopenharmony_ci# endif
12262306a36Sopenharmony_ci# if UNW_STATS
12362306a36Sopenharmony_ci	struct {
12462306a36Sopenharmony_ci		struct {
12562306a36Sopenharmony_ci			int lookups;
12662306a36Sopenharmony_ci			int hinted_hits;
12762306a36Sopenharmony_ci			int normal_hits;
12862306a36Sopenharmony_ci			int collision_chain_traversals;
12962306a36Sopenharmony_ci		} cache;
13062306a36Sopenharmony_ci		struct {
13162306a36Sopenharmony_ci			unsigned long build_time;
13262306a36Sopenharmony_ci			unsigned long run_time;
13362306a36Sopenharmony_ci			unsigned long parse_time;
13462306a36Sopenharmony_ci			int builds;
13562306a36Sopenharmony_ci			int news;
13662306a36Sopenharmony_ci			int collisions;
13762306a36Sopenharmony_ci			int runs;
13862306a36Sopenharmony_ci		} script;
13962306a36Sopenharmony_ci		struct {
14062306a36Sopenharmony_ci			unsigned long init_time;
14162306a36Sopenharmony_ci			unsigned long unwind_time;
14262306a36Sopenharmony_ci			int inits;
14362306a36Sopenharmony_ci			int unwinds;
14462306a36Sopenharmony_ci		} api;
14562306a36Sopenharmony_ci	} stat;
14662306a36Sopenharmony_ci# endif
14762306a36Sopenharmony_ci} unw = {
14862306a36Sopenharmony_ci	.tables = &unw.kernel_table,
14962306a36Sopenharmony_ci	.lock = __SPIN_LOCK_UNLOCKED(unw.lock),
15062306a36Sopenharmony_ci	.save_order = {
15162306a36Sopenharmony_ci		UNW_REG_RP, UNW_REG_PFS, UNW_REG_PSP, UNW_REG_PR,
15262306a36Sopenharmony_ci		UNW_REG_UNAT, UNW_REG_LC, UNW_REG_FPSR, UNW_REG_PRI_UNAT_GR
15362306a36Sopenharmony_ci	},
15462306a36Sopenharmony_ci	.preg_index = {
15562306a36Sopenharmony_ci		offsetof(struct unw_frame_info, pri_unat_loc)/8,	/* PRI_UNAT_GR */
15662306a36Sopenharmony_ci		offsetof(struct unw_frame_info, pri_unat_loc)/8,	/* PRI_UNAT_MEM */
15762306a36Sopenharmony_ci		offsetof(struct unw_frame_info, bsp_loc)/8,
15862306a36Sopenharmony_ci		offsetof(struct unw_frame_info, bspstore_loc)/8,
15962306a36Sopenharmony_ci		offsetof(struct unw_frame_info, pfs_loc)/8,
16062306a36Sopenharmony_ci		offsetof(struct unw_frame_info, rnat_loc)/8,
16162306a36Sopenharmony_ci		offsetof(struct unw_frame_info, psp)/8,
16262306a36Sopenharmony_ci		offsetof(struct unw_frame_info, rp_loc)/8,
16362306a36Sopenharmony_ci		offsetof(struct unw_frame_info, r4)/8,
16462306a36Sopenharmony_ci		offsetof(struct unw_frame_info, r5)/8,
16562306a36Sopenharmony_ci		offsetof(struct unw_frame_info, r6)/8,
16662306a36Sopenharmony_ci		offsetof(struct unw_frame_info, r7)/8,
16762306a36Sopenharmony_ci		offsetof(struct unw_frame_info, unat_loc)/8,
16862306a36Sopenharmony_ci		offsetof(struct unw_frame_info, pr_loc)/8,
16962306a36Sopenharmony_ci		offsetof(struct unw_frame_info, lc_loc)/8,
17062306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fpsr_loc)/8,
17162306a36Sopenharmony_ci		offsetof(struct unw_frame_info, b1_loc)/8,
17262306a36Sopenharmony_ci		offsetof(struct unw_frame_info, b2_loc)/8,
17362306a36Sopenharmony_ci		offsetof(struct unw_frame_info, b3_loc)/8,
17462306a36Sopenharmony_ci		offsetof(struct unw_frame_info, b4_loc)/8,
17562306a36Sopenharmony_ci		offsetof(struct unw_frame_info, b5_loc)/8,
17662306a36Sopenharmony_ci		offsetof(struct unw_frame_info, f2_loc)/8,
17762306a36Sopenharmony_ci		offsetof(struct unw_frame_info, f3_loc)/8,
17862306a36Sopenharmony_ci		offsetof(struct unw_frame_info, f4_loc)/8,
17962306a36Sopenharmony_ci		offsetof(struct unw_frame_info, f5_loc)/8,
18062306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[16 - 16])/8,
18162306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[17 - 16])/8,
18262306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[18 - 16])/8,
18362306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[19 - 16])/8,
18462306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[20 - 16])/8,
18562306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[21 - 16])/8,
18662306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[22 - 16])/8,
18762306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[23 - 16])/8,
18862306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[24 - 16])/8,
18962306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[25 - 16])/8,
19062306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[26 - 16])/8,
19162306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[27 - 16])/8,
19262306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[28 - 16])/8,
19362306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[29 - 16])/8,
19462306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[30 - 16])/8,
19562306a36Sopenharmony_ci		offsetof(struct unw_frame_info, fr_loc[31 - 16])/8,
19662306a36Sopenharmony_ci	},
19762306a36Sopenharmony_ci	.pt_regs_offsets = {
19862306a36Sopenharmony_ci		[0] = -1,
19962306a36Sopenharmony_ci		offsetof(struct pt_regs,  r1),
20062306a36Sopenharmony_ci		offsetof(struct pt_regs,  r2),
20162306a36Sopenharmony_ci		offsetof(struct pt_regs,  r3),
20262306a36Sopenharmony_ci		[4] = -1, [5] = -1, [6] = -1, [7] = -1,
20362306a36Sopenharmony_ci		offsetof(struct pt_regs,  r8),
20462306a36Sopenharmony_ci		offsetof(struct pt_regs,  r9),
20562306a36Sopenharmony_ci		offsetof(struct pt_regs, r10),
20662306a36Sopenharmony_ci		offsetof(struct pt_regs, r11),
20762306a36Sopenharmony_ci		offsetof(struct pt_regs, r12),
20862306a36Sopenharmony_ci		offsetof(struct pt_regs, r13),
20962306a36Sopenharmony_ci		offsetof(struct pt_regs, r14),
21062306a36Sopenharmony_ci		offsetof(struct pt_regs, r15),
21162306a36Sopenharmony_ci		offsetof(struct pt_regs, r16),
21262306a36Sopenharmony_ci		offsetof(struct pt_regs, r17),
21362306a36Sopenharmony_ci		offsetof(struct pt_regs, r18),
21462306a36Sopenharmony_ci		offsetof(struct pt_regs, r19),
21562306a36Sopenharmony_ci		offsetof(struct pt_regs, r20),
21662306a36Sopenharmony_ci		offsetof(struct pt_regs, r21),
21762306a36Sopenharmony_ci		offsetof(struct pt_regs, r22),
21862306a36Sopenharmony_ci		offsetof(struct pt_regs, r23),
21962306a36Sopenharmony_ci		offsetof(struct pt_regs, r24),
22062306a36Sopenharmony_ci		offsetof(struct pt_regs, r25),
22162306a36Sopenharmony_ci		offsetof(struct pt_regs, r26),
22262306a36Sopenharmony_ci		offsetof(struct pt_regs, r27),
22362306a36Sopenharmony_ci		offsetof(struct pt_regs, r28),
22462306a36Sopenharmony_ci		offsetof(struct pt_regs, r29),
22562306a36Sopenharmony_ci		offsetof(struct pt_regs, r30),
22662306a36Sopenharmony_ci		offsetof(struct pt_regs, r31),
22762306a36Sopenharmony_ci	},
22862306a36Sopenharmony_ci	.hash = { [0 ... UNW_HASH_SIZE - 1] = -1 },
22962306a36Sopenharmony_ci#ifdef UNW_DEBUG
23062306a36Sopenharmony_ci	.preg_name = {
23162306a36Sopenharmony_ci		"pri_unat_gr", "pri_unat_mem", "bsp", "bspstore", "ar.pfs", "ar.rnat", "psp", "rp",
23262306a36Sopenharmony_ci		"r4", "r5", "r6", "r7",
23362306a36Sopenharmony_ci		"ar.unat", "pr", "ar.lc", "ar.fpsr",
23462306a36Sopenharmony_ci		"b1", "b2", "b3", "b4", "b5",
23562306a36Sopenharmony_ci		"f2", "f3", "f4", "f5",
23662306a36Sopenharmony_ci		"f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
23762306a36Sopenharmony_ci		"f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
23862306a36Sopenharmony_ci	}
23962306a36Sopenharmony_ci#endif
24062306a36Sopenharmony_ci};
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistatic inline int
24362306a36Sopenharmony_ciread_only (void *addr)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	return (unsigned long) ((char *) addr - (char *) &unw.r0) < sizeof(unw.r0);
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/*
24962306a36Sopenharmony_ci * Returns offset of rREG in struct pt_regs.
25062306a36Sopenharmony_ci */
25162306a36Sopenharmony_cistatic inline unsigned long
25262306a36Sopenharmony_cipt_regs_off (unsigned long reg)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	short off = -1;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	if (reg < ARRAY_SIZE(unw.pt_regs_offsets))
25762306a36Sopenharmony_ci		off = unw.pt_regs_offsets[reg];
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	if (off < 0) {
26062306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bad scratch reg r%lu\n", __func__, reg);
26162306a36Sopenharmony_ci		off = 0;
26262306a36Sopenharmony_ci	}
26362306a36Sopenharmony_ci	return (unsigned long) off;
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cistatic inline struct pt_regs *
26762306a36Sopenharmony_ciget_scratch_regs (struct unw_frame_info *info)
26862306a36Sopenharmony_ci{
26962306a36Sopenharmony_ci	if (!info->pt) {
27062306a36Sopenharmony_ci		/* This should not happen with valid unwind info.  */
27162306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bad unwind info: resetting info->pt\n", __func__);
27262306a36Sopenharmony_ci		if (info->flags & UNW_FLAG_INTERRUPT_FRAME)
27362306a36Sopenharmony_ci			info->pt = (unsigned long) ((struct pt_regs *) info->psp - 1);
27462306a36Sopenharmony_ci		else
27562306a36Sopenharmony_ci			info->pt = info->sp - 16;
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s: sp 0x%lx pt 0x%lx\n", __func__, info->sp, info->pt);
27862306a36Sopenharmony_ci	return (struct pt_regs *) info->pt;
27962306a36Sopenharmony_ci}
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci/* Unwind accessors.  */
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ciint
28462306a36Sopenharmony_ciunw_access_gr (struct unw_frame_info *info, int regnum, unsigned long *val, char *nat, int write)
28562306a36Sopenharmony_ci{
28662306a36Sopenharmony_ci	unsigned long *addr, *nat_addr, nat_mask = 0, dummy_nat;
28762306a36Sopenharmony_ci	struct unw_ireg *ireg;
28862306a36Sopenharmony_ci	struct pt_regs *pt;
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci	if ((unsigned) regnum - 1 >= 127) {
29162306a36Sopenharmony_ci		if (regnum == 0 && !write) {
29262306a36Sopenharmony_ci			*val = 0;	/* read r0 always returns 0 */
29362306a36Sopenharmony_ci			*nat = 0;
29462306a36Sopenharmony_ci			return 0;
29562306a36Sopenharmony_ci		}
29662306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent r%u\n",
29762306a36Sopenharmony_ci			   __func__, regnum);
29862306a36Sopenharmony_ci		return -1;
29962306a36Sopenharmony_ci	}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	if (regnum < 32) {
30262306a36Sopenharmony_ci		if (regnum >= 4 && regnum <= 7) {
30362306a36Sopenharmony_ci			/* access a preserved register */
30462306a36Sopenharmony_ci			ireg = &info->r4 + (regnum - 4);
30562306a36Sopenharmony_ci			addr = ireg->loc;
30662306a36Sopenharmony_ci			if (addr) {
30762306a36Sopenharmony_ci				nat_addr = addr + ireg->nat.off;
30862306a36Sopenharmony_ci				switch (ireg->nat.type) {
30962306a36Sopenharmony_ci				      case UNW_NAT_VAL:
31062306a36Sopenharmony_ci					/* simulate getf.sig/setf.sig */
31162306a36Sopenharmony_ci					if (write) {
31262306a36Sopenharmony_ci						if (*nat) {
31362306a36Sopenharmony_ci							/* write NaTVal and be done with it */
31462306a36Sopenharmony_ci							addr[0] = 0;
31562306a36Sopenharmony_ci							addr[1] = 0x1fffe;
31662306a36Sopenharmony_ci							return 0;
31762306a36Sopenharmony_ci						}
31862306a36Sopenharmony_ci						addr[1] = 0x1003e;
31962306a36Sopenharmony_ci					} else {
32062306a36Sopenharmony_ci						if (addr[0] == 0 && addr[1] == 0x1ffe) {
32162306a36Sopenharmony_ci							/* return NaT and be done with it */
32262306a36Sopenharmony_ci							*val = 0;
32362306a36Sopenharmony_ci							*nat = 1;
32462306a36Sopenharmony_ci							return 0;
32562306a36Sopenharmony_ci						}
32662306a36Sopenharmony_ci					}
32762306a36Sopenharmony_ci					fallthrough;
32862306a36Sopenharmony_ci				      case UNW_NAT_NONE:
32962306a36Sopenharmony_ci					dummy_nat = 0;
33062306a36Sopenharmony_ci					nat_addr = &dummy_nat;
33162306a36Sopenharmony_ci					break;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci				      case UNW_NAT_MEMSTK:
33462306a36Sopenharmony_ci					nat_mask = (1UL << ((long) addr & 0x1f8)/8);
33562306a36Sopenharmony_ci					break;
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci				      case UNW_NAT_REGSTK:
33862306a36Sopenharmony_ci					nat_addr = ia64_rse_rnat_addr(addr);
33962306a36Sopenharmony_ci					if ((unsigned long) addr < info->regstk.limit
34062306a36Sopenharmony_ci					    || (unsigned long) addr >= info->regstk.top)
34162306a36Sopenharmony_ci					{
34262306a36Sopenharmony_ci						UNW_DPRINT(0, "unwind.%s: %p outside of regstk "
34362306a36Sopenharmony_ci							"[0x%lx-0x%lx)\n",
34462306a36Sopenharmony_ci							__func__, (void *) addr,
34562306a36Sopenharmony_ci							info->regstk.limit,
34662306a36Sopenharmony_ci							info->regstk.top);
34762306a36Sopenharmony_ci						return -1;
34862306a36Sopenharmony_ci					}
34962306a36Sopenharmony_ci					if ((unsigned long) nat_addr >= info->regstk.top)
35062306a36Sopenharmony_ci						nat_addr = &info->sw->ar_rnat;
35162306a36Sopenharmony_ci					nat_mask = (1UL << ia64_rse_slot_num(addr));
35262306a36Sopenharmony_ci					break;
35362306a36Sopenharmony_ci				}
35462306a36Sopenharmony_ci			} else {
35562306a36Sopenharmony_ci				addr = &info->sw->r4 + (regnum - 4);
35662306a36Sopenharmony_ci				nat_addr = &info->sw->ar_unat;
35762306a36Sopenharmony_ci				nat_mask = (1UL << ((long) addr & 0x1f8)/8);
35862306a36Sopenharmony_ci			}
35962306a36Sopenharmony_ci		} else {
36062306a36Sopenharmony_ci			/* access a scratch register */
36162306a36Sopenharmony_ci			pt = get_scratch_regs(info);
36262306a36Sopenharmony_ci			addr = (unsigned long *) ((unsigned long)pt + pt_regs_off(regnum));
36362306a36Sopenharmony_ci			if (info->pri_unat_loc)
36462306a36Sopenharmony_ci				nat_addr = info->pri_unat_loc;
36562306a36Sopenharmony_ci			else
36662306a36Sopenharmony_ci				nat_addr = &info->sw->caller_unat;
36762306a36Sopenharmony_ci			nat_mask = (1UL << ((long) addr & 0x1f8)/8);
36862306a36Sopenharmony_ci		}
36962306a36Sopenharmony_ci	} else {
37062306a36Sopenharmony_ci		/* access a stacked register */
37162306a36Sopenharmony_ci		addr = ia64_rse_skip_regs((unsigned long *) info->bsp, regnum - 32);
37262306a36Sopenharmony_ci		nat_addr = ia64_rse_rnat_addr(addr);
37362306a36Sopenharmony_ci		if ((unsigned long) addr < info->regstk.limit
37462306a36Sopenharmony_ci		    || (unsigned long) addr >= info->regstk.top)
37562306a36Sopenharmony_ci		{
37662306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to access register outside "
37762306a36Sopenharmony_ci				   "of rbs\n",  __func__);
37862306a36Sopenharmony_ci			return -1;
37962306a36Sopenharmony_ci		}
38062306a36Sopenharmony_ci		if ((unsigned long) nat_addr >= info->regstk.top)
38162306a36Sopenharmony_ci			nat_addr = &info->sw->ar_rnat;
38262306a36Sopenharmony_ci		nat_mask = (1UL << ia64_rse_slot_num(addr));
38362306a36Sopenharmony_ci	}
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	if (write) {
38662306a36Sopenharmony_ci		if (read_only(addr)) {
38762306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
38862306a36Sopenharmony_ci				__func__);
38962306a36Sopenharmony_ci		} else {
39062306a36Sopenharmony_ci			*addr = *val;
39162306a36Sopenharmony_ci			if (*nat)
39262306a36Sopenharmony_ci				*nat_addr |= nat_mask;
39362306a36Sopenharmony_ci			else
39462306a36Sopenharmony_ci				*nat_addr &= ~nat_mask;
39562306a36Sopenharmony_ci		}
39662306a36Sopenharmony_ci	} else {
39762306a36Sopenharmony_ci		if ((*nat_addr & nat_mask) == 0) {
39862306a36Sopenharmony_ci			*val = *addr;
39962306a36Sopenharmony_ci			*nat = 0;
40062306a36Sopenharmony_ci		} else {
40162306a36Sopenharmony_ci			*val = 0;	/* if register is a NaT, *addr may contain kernel data! */
40262306a36Sopenharmony_ci			*nat = 1;
40362306a36Sopenharmony_ci		}
40462306a36Sopenharmony_ci	}
40562306a36Sopenharmony_ci	return 0;
40662306a36Sopenharmony_ci}
40762306a36Sopenharmony_ciEXPORT_SYMBOL(unw_access_gr);
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ciint
41062306a36Sopenharmony_ciunw_access_br (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
41162306a36Sopenharmony_ci{
41262306a36Sopenharmony_ci	unsigned long *addr;
41362306a36Sopenharmony_ci	struct pt_regs *pt;
41462306a36Sopenharmony_ci
41562306a36Sopenharmony_ci	switch (regnum) {
41662306a36Sopenharmony_ci		/* scratch: */
41762306a36Sopenharmony_ci	      case 0: pt = get_scratch_regs(info); addr = &pt->b0; break;
41862306a36Sopenharmony_ci	      case 6: pt = get_scratch_regs(info); addr = &pt->b6; break;
41962306a36Sopenharmony_ci	      case 7: pt = get_scratch_regs(info); addr = &pt->b7; break;
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci		/* preserved: */
42262306a36Sopenharmony_ci	      case 1: case 2: case 3: case 4: case 5:
42362306a36Sopenharmony_ci		addr = *(&info->b1_loc + (regnum - 1));
42462306a36Sopenharmony_ci		if (!addr)
42562306a36Sopenharmony_ci			addr = &info->sw->b1 + (regnum - 1);
42662306a36Sopenharmony_ci		break;
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	      default:
42962306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent b%u\n",
43062306a36Sopenharmony_ci			   __func__, regnum);
43162306a36Sopenharmony_ci		return -1;
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci	if (write)
43462306a36Sopenharmony_ci		if (read_only(addr)) {
43562306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
43662306a36Sopenharmony_ci				__func__);
43762306a36Sopenharmony_ci		} else
43862306a36Sopenharmony_ci			*addr = *val;
43962306a36Sopenharmony_ci	else
44062306a36Sopenharmony_ci		*val = *addr;
44162306a36Sopenharmony_ci	return 0;
44262306a36Sopenharmony_ci}
44362306a36Sopenharmony_ciEXPORT_SYMBOL(unw_access_br);
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ciint
44662306a36Sopenharmony_ciunw_access_fr (struct unw_frame_info *info, int regnum, struct ia64_fpreg *val, int write)
44762306a36Sopenharmony_ci{
44862306a36Sopenharmony_ci	struct ia64_fpreg *addr = NULL;
44962306a36Sopenharmony_ci	struct pt_regs *pt;
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	if ((unsigned) (regnum - 2) >= 126) {
45262306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent f%u\n",
45362306a36Sopenharmony_ci			   __func__, regnum);
45462306a36Sopenharmony_ci		return -1;
45562306a36Sopenharmony_ci	}
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	if (regnum <= 5) {
45862306a36Sopenharmony_ci		addr = *(&info->f2_loc + (regnum - 2));
45962306a36Sopenharmony_ci		if (!addr)
46062306a36Sopenharmony_ci			addr = &info->sw->f2 + (regnum - 2);
46162306a36Sopenharmony_ci	} else if (regnum <= 15) {
46262306a36Sopenharmony_ci		if (regnum <= 11) {
46362306a36Sopenharmony_ci			pt = get_scratch_regs(info);
46462306a36Sopenharmony_ci			addr = &pt->f6  + (regnum - 6);
46562306a36Sopenharmony_ci		}
46662306a36Sopenharmony_ci		else
46762306a36Sopenharmony_ci			addr = &info->sw->f12 + (regnum - 12);
46862306a36Sopenharmony_ci	} else if (regnum <= 31) {
46962306a36Sopenharmony_ci		addr = info->fr_loc[regnum - 16];
47062306a36Sopenharmony_ci		if (!addr)
47162306a36Sopenharmony_ci			addr = &info->sw->f16 + (regnum - 16);
47262306a36Sopenharmony_ci	} else {
47362306a36Sopenharmony_ci		struct task_struct *t = info->task;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci		if (write)
47662306a36Sopenharmony_ci			ia64_sync_fph(t);
47762306a36Sopenharmony_ci		else
47862306a36Sopenharmony_ci			ia64_flush_fph(t);
47962306a36Sopenharmony_ci		addr = t->thread.fph + (regnum - 32);
48062306a36Sopenharmony_ci	}
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci	if (write)
48362306a36Sopenharmony_ci		if (read_only(addr)) {
48462306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
48562306a36Sopenharmony_ci				__func__);
48662306a36Sopenharmony_ci		} else
48762306a36Sopenharmony_ci			*addr = *val;
48862306a36Sopenharmony_ci	else
48962306a36Sopenharmony_ci		*val = *addr;
49062306a36Sopenharmony_ci	return 0;
49162306a36Sopenharmony_ci}
49262306a36Sopenharmony_ciEXPORT_SYMBOL(unw_access_fr);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ciint
49562306a36Sopenharmony_ciunw_access_ar (struct unw_frame_info *info, int regnum, unsigned long *val, int write)
49662306a36Sopenharmony_ci{
49762306a36Sopenharmony_ci	unsigned long *addr;
49862306a36Sopenharmony_ci	struct pt_regs *pt;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	switch (regnum) {
50162306a36Sopenharmony_ci	      case UNW_AR_BSP:
50262306a36Sopenharmony_ci		addr = info->bsp_loc;
50362306a36Sopenharmony_ci		if (!addr)
50462306a36Sopenharmony_ci			addr = &info->sw->ar_bspstore;
50562306a36Sopenharmony_ci		break;
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci	      case UNW_AR_BSPSTORE:
50862306a36Sopenharmony_ci		addr = info->bspstore_loc;
50962306a36Sopenharmony_ci		if (!addr)
51062306a36Sopenharmony_ci			addr = &info->sw->ar_bspstore;
51162306a36Sopenharmony_ci		break;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	      case UNW_AR_PFS:
51462306a36Sopenharmony_ci		addr = info->pfs_loc;
51562306a36Sopenharmony_ci		if (!addr)
51662306a36Sopenharmony_ci			addr = &info->sw->ar_pfs;
51762306a36Sopenharmony_ci		break;
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ci	      case UNW_AR_RNAT:
52062306a36Sopenharmony_ci		addr = info->rnat_loc;
52162306a36Sopenharmony_ci		if (!addr)
52262306a36Sopenharmony_ci			addr = &info->sw->ar_rnat;
52362306a36Sopenharmony_ci		break;
52462306a36Sopenharmony_ci
52562306a36Sopenharmony_ci	      case UNW_AR_UNAT:
52662306a36Sopenharmony_ci		addr = info->unat_loc;
52762306a36Sopenharmony_ci		if (!addr)
52862306a36Sopenharmony_ci			addr = &info->sw->caller_unat;
52962306a36Sopenharmony_ci		break;
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci	      case UNW_AR_LC:
53262306a36Sopenharmony_ci		addr = info->lc_loc;
53362306a36Sopenharmony_ci		if (!addr)
53462306a36Sopenharmony_ci			addr = &info->sw->ar_lc;
53562306a36Sopenharmony_ci		break;
53662306a36Sopenharmony_ci
53762306a36Sopenharmony_ci	      case UNW_AR_EC:
53862306a36Sopenharmony_ci		if (!info->cfm_loc)
53962306a36Sopenharmony_ci			return -1;
54062306a36Sopenharmony_ci		if (write)
54162306a36Sopenharmony_ci			*info->cfm_loc =
54262306a36Sopenharmony_ci				(*info->cfm_loc & ~(0x3fUL << 52)) | ((*val & 0x3f) << 52);
54362306a36Sopenharmony_ci		else
54462306a36Sopenharmony_ci			*val = (*info->cfm_loc >> 52) & 0x3f;
54562306a36Sopenharmony_ci		return 0;
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci	      case UNW_AR_FPSR:
54862306a36Sopenharmony_ci		addr = info->fpsr_loc;
54962306a36Sopenharmony_ci		if (!addr)
55062306a36Sopenharmony_ci			addr = &info->sw->ar_fpsr;
55162306a36Sopenharmony_ci		break;
55262306a36Sopenharmony_ci
55362306a36Sopenharmony_ci	      case UNW_AR_RSC:
55462306a36Sopenharmony_ci		pt = get_scratch_regs(info);
55562306a36Sopenharmony_ci		addr = &pt->ar_rsc;
55662306a36Sopenharmony_ci		break;
55762306a36Sopenharmony_ci
55862306a36Sopenharmony_ci	      case UNW_AR_CCV:
55962306a36Sopenharmony_ci		pt = get_scratch_regs(info);
56062306a36Sopenharmony_ci		addr = &pt->ar_ccv;
56162306a36Sopenharmony_ci		break;
56262306a36Sopenharmony_ci
56362306a36Sopenharmony_ci	      case UNW_AR_CSD:
56462306a36Sopenharmony_ci		pt = get_scratch_regs(info);
56562306a36Sopenharmony_ci		addr = &pt->ar_csd;
56662306a36Sopenharmony_ci		break;
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci	      case UNW_AR_SSD:
56962306a36Sopenharmony_ci		pt = get_scratch_regs(info);
57062306a36Sopenharmony_ci		addr = &pt->ar_ssd;
57162306a36Sopenharmony_ci		break;
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	      default:
57462306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: trying to access non-existent ar%u\n",
57562306a36Sopenharmony_ci			   __func__, regnum);
57662306a36Sopenharmony_ci		return -1;
57762306a36Sopenharmony_ci	}
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci	if (write) {
58062306a36Sopenharmony_ci		if (read_only(addr)) {
58162306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
58262306a36Sopenharmony_ci				__func__);
58362306a36Sopenharmony_ci		} else
58462306a36Sopenharmony_ci			*addr = *val;
58562306a36Sopenharmony_ci	} else
58662306a36Sopenharmony_ci		*val = *addr;
58762306a36Sopenharmony_ci	return 0;
58862306a36Sopenharmony_ci}
58962306a36Sopenharmony_ciEXPORT_SYMBOL(unw_access_ar);
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ciint
59262306a36Sopenharmony_ciunw_access_pr (struct unw_frame_info *info, unsigned long *val, int write)
59362306a36Sopenharmony_ci{
59462306a36Sopenharmony_ci	unsigned long *addr;
59562306a36Sopenharmony_ci
59662306a36Sopenharmony_ci	addr = info->pr_loc;
59762306a36Sopenharmony_ci	if (!addr)
59862306a36Sopenharmony_ci		addr = &info->sw->pr;
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	if (write) {
60162306a36Sopenharmony_ci		if (read_only(addr)) {
60262306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ignoring attempt to write read-only location\n",
60362306a36Sopenharmony_ci				__func__);
60462306a36Sopenharmony_ci		} else
60562306a36Sopenharmony_ci			*addr = *val;
60662306a36Sopenharmony_ci	} else
60762306a36Sopenharmony_ci		*val = *addr;
60862306a36Sopenharmony_ci	return 0;
60962306a36Sopenharmony_ci}
61062306a36Sopenharmony_ciEXPORT_SYMBOL(unw_access_pr);
61162306a36Sopenharmony_ci
61262306a36Sopenharmony_ci
61362306a36Sopenharmony_ci/* Routines to manipulate the state stack.  */
61462306a36Sopenharmony_ci
61562306a36Sopenharmony_cistatic inline void
61662306a36Sopenharmony_cipush (struct unw_state_record *sr)
61762306a36Sopenharmony_ci{
61862306a36Sopenharmony_ci	struct unw_reg_state *rs;
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci	rs = alloc_reg_state();
62162306a36Sopenharmony_ci	if (!rs) {
62262306a36Sopenharmony_ci		printk(KERN_ERR "unwind: cannot stack reg state!\n");
62362306a36Sopenharmony_ci		return;
62462306a36Sopenharmony_ci	}
62562306a36Sopenharmony_ci	memcpy(rs, &sr->curr, sizeof(*rs));
62662306a36Sopenharmony_ci	sr->curr.next = rs;
62762306a36Sopenharmony_ci}
62862306a36Sopenharmony_ci
62962306a36Sopenharmony_cistatic void
63062306a36Sopenharmony_cipop (struct unw_state_record *sr)
63162306a36Sopenharmony_ci{
63262306a36Sopenharmony_ci	struct unw_reg_state *rs = sr->curr.next;
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci	if (!rs) {
63562306a36Sopenharmony_ci		printk(KERN_ERR "unwind: stack underflow!\n");
63662306a36Sopenharmony_ci		return;
63762306a36Sopenharmony_ci	}
63862306a36Sopenharmony_ci	memcpy(&sr->curr, rs, sizeof(*rs));
63962306a36Sopenharmony_ci	free_reg_state(rs);
64062306a36Sopenharmony_ci}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci/* Make a copy of the state stack.  Non-recursive to avoid stack overflows.  */
64362306a36Sopenharmony_cistatic struct unw_reg_state *
64462306a36Sopenharmony_cidup_state_stack (struct unw_reg_state *rs)
64562306a36Sopenharmony_ci{
64662306a36Sopenharmony_ci	struct unw_reg_state *copy, *prev = NULL, *first = NULL;
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci	while (rs) {
64962306a36Sopenharmony_ci		copy = alloc_reg_state();
65062306a36Sopenharmony_ci		if (!copy) {
65162306a36Sopenharmony_ci			printk(KERN_ERR "unwind.dup_state_stack: out of memory\n");
65262306a36Sopenharmony_ci			return NULL;
65362306a36Sopenharmony_ci		}
65462306a36Sopenharmony_ci		memcpy(copy, rs, sizeof(*copy));
65562306a36Sopenharmony_ci		if (first)
65662306a36Sopenharmony_ci			prev->next = copy;
65762306a36Sopenharmony_ci		else
65862306a36Sopenharmony_ci			first = copy;
65962306a36Sopenharmony_ci		rs = rs->next;
66062306a36Sopenharmony_ci		prev = copy;
66162306a36Sopenharmony_ci	}
66262306a36Sopenharmony_ci	return first;
66362306a36Sopenharmony_ci}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_ci/* Free all stacked register states (but not RS itself).  */
66662306a36Sopenharmony_cistatic void
66762306a36Sopenharmony_cifree_state_stack (struct unw_reg_state *rs)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	struct unw_reg_state *p, *next;
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci	for (p = rs->next; p != NULL; p = next) {
67262306a36Sopenharmony_ci		next = p->next;
67362306a36Sopenharmony_ci		free_reg_state(p);
67462306a36Sopenharmony_ci	}
67562306a36Sopenharmony_ci	rs->next = NULL;
67662306a36Sopenharmony_ci}
67762306a36Sopenharmony_ci
67862306a36Sopenharmony_ci/* Unwind decoder routines */
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_cistatic enum unw_register_index __attribute_const__
68162306a36Sopenharmony_cidecode_abreg (unsigned char abreg, int memory)
68262306a36Sopenharmony_ci{
68362306a36Sopenharmony_ci	switch (abreg) {
68462306a36Sopenharmony_ci	      case 0x04 ... 0x07: return UNW_REG_R4 + (abreg - 0x04);
68562306a36Sopenharmony_ci	      case 0x22 ... 0x25: return UNW_REG_F2 + (abreg - 0x22);
68662306a36Sopenharmony_ci	      case 0x30 ... 0x3f: return UNW_REG_F16 + (abreg - 0x30);
68762306a36Sopenharmony_ci	      case 0x41 ... 0x45: return UNW_REG_B1 + (abreg - 0x41);
68862306a36Sopenharmony_ci	      case 0x60: return UNW_REG_PR;
68962306a36Sopenharmony_ci	      case 0x61: return UNW_REG_PSP;
69062306a36Sopenharmony_ci	      case 0x62: return memory ? UNW_REG_PRI_UNAT_MEM : UNW_REG_PRI_UNAT_GR;
69162306a36Sopenharmony_ci	      case 0x63: return UNW_REG_RP;
69262306a36Sopenharmony_ci	      case 0x64: return UNW_REG_BSP;
69362306a36Sopenharmony_ci	      case 0x65: return UNW_REG_BSPSTORE;
69462306a36Sopenharmony_ci	      case 0x66: return UNW_REG_RNAT;
69562306a36Sopenharmony_ci	      case 0x67: return UNW_REG_UNAT;
69662306a36Sopenharmony_ci	      case 0x68: return UNW_REG_FPSR;
69762306a36Sopenharmony_ci	      case 0x69: return UNW_REG_PFS;
69862306a36Sopenharmony_ci	      case 0x6a: return UNW_REG_LC;
69962306a36Sopenharmony_ci	      default:
70062306a36Sopenharmony_ci		break;
70162306a36Sopenharmony_ci	}
70262306a36Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: bad abreg=0x%x\n", __func__, abreg);
70362306a36Sopenharmony_ci	return UNW_REG_LC;
70462306a36Sopenharmony_ci}
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_cistatic void
70762306a36Sopenharmony_ciset_reg (struct unw_reg_info *reg, enum unw_where where, int when, unsigned long val)
70862306a36Sopenharmony_ci{
70962306a36Sopenharmony_ci	reg->val = val;
71062306a36Sopenharmony_ci	reg->where = where;
71162306a36Sopenharmony_ci	if (reg->when == UNW_WHEN_NEVER)
71262306a36Sopenharmony_ci		reg->when = when;
71362306a36Sopenharmony_ci}
71462306a36Sopenharmony_ci
71562306a36Sopenharmony_cistatic void
71662306a36Sopenharmony_cialloc_spill_area (unsigned long *offp, unsigned long regsize,
71762306a36Sopenharmony_ci		  struct unw_reg_info *lo, struct unw_reg_info *hi)
71862306a36Sopenharmony_ci{
71962306a36Sopenharmony_ci	struct unw_reg_info *reg;
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci	for (reg = hi; reg >= lo; --reg) {
72262306a36Sopenharmony_ci		if (reg->where == UNW_WHERE_SPILL_HOME) {
72362306a36Sopenharmony_ci			reg->where = UNW_WHERE_PSPREL;
72462306a36Sopenharmony_ci			*offp -= regsize;
72562306a36Sopenharmony_ci			reg->val = *offp;
72662306a36Sopenharmony_ci		}
72762306a36Sopenharmony_ci	}
72862306a36Sopenharmony_ci}
72962306a36Sopenharmony_ci
73062306a36Sopenharmony_cistatic inline void
73162306a36Sopenharmony_cispill_next_when (struct unw_reg_info **regp, struct unw_reg_info *lim, unw_word t)
73262306a36Sopenharmony_ci{
73362306a36Sopenharmony_ci	struct unw_reg_info *reg;
73462306a36Sopenharmony_ci
73562306a36Sopenharmony_ci	for (reg = *regp; reg <= lim; ++reg) {
73662306a36Sopenharmony_ci		if (reg->where == UNW_WHERE_SPILL_HOME) {
73762306a36Sopenharmony_ci			reg->when = t;
73862306a36Sopenharmony_ci			*regp = reg + 1;
73962306a36Sopenharmony_ci			return;
74062306a36Sopenharmony_ci		}
74162306a36Sopenharmony_ci	}
74262306a36Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: excess spill!\n",  __func__);
74362306a36Sopenharmony_ci}
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_cistatic inline void
74662306a36Sopenharmony_cifinish_prologue (struct unw_state_record *sr)
74762306a36Sopenharmony_ci{
74862306a36Sopenharmony_ci	struct unw_reg_info *reg;
74962306a36Sopenharmony_ci	unsigned long off;
75062306a36Sopenharmony_ci	int i;
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci	/*
75362306a36Sopenharmony_ci	 * First, resolve implicit register save locations (see Section "11.4.2.3 Rules
75462306a36Sopenharmony_ci	 * for Using Unwind Descriptors", rule 3):
75562306a36Sopenharmony_ci	 */
75662306a36Sopenharmony_ci	for (i = 0; i < (int) ARRAY_SIZE(unw.save_order); ++i) {
75762306a36Sopenharmony_ci		reg = sr->curr.reg + unw.save_order[i];
75862306a36Sopenharmony_ci		if (reg->where == UNW_WHERE_GR_SAVE) {
75962306a36Sopenharmony_ci			reg->where = UNW_WHERE_GR;
76062306a36Sopenharmony_ci			reg->val = sr->gr_save_loc++;
76162306a36Sopenharmony_ci		}
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	/*
76562306a36Sopenharmony_ci	 * Next, compute when the fp, general, and branch registers get
76662306a36Sopenharmony_ci	 * saved.  This must come before alloc_spill_area() because
76762306a36Sopenharmony_ci	 * we need to know which registers are spilled to their home
76862306a36Sopenharmony_ci	 * locations.
76962306a36Sopenharmony_ci	 */
77062306a36Sopenharmony_ci	if (sr->imask) {
77162306a36Sopenharmony_ci		unsigned char kind, mask = 0, *cp = sr->imask;
77262306a36Sopenharmony_ci		int t;
77362306a36Sopenharmony_ci		static const unsigned char limit[3] = {
77462306a36Sopenharmony_ci			UNW_REG_F31, UNW_REG_R7, UNW_REG_B5
77562306a36Sopenharmony_ci		};
77662306a36Sopenharmony_ci		struct unw_reg_info *(regs[3]);
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_ci		regs[0] = sr->curr.reg + UNW_REG_F2;
77962306a36Sopenharmony_ci		regs[1] = sr->curr.reg + UNW_REG_R4;
78062306a36Sopenharmony_ci		regs[2] = sr->curr.reg + UNW_REG_B1;
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci		for (t = 0; t < sr->region_len; ++t) {
78362306a36Sopenharmony_ci			if ((t & 3) == 0)
78462306a36Sopenharmony_ci				mask = *cp++;
78562306a36Sopenharmony_ci			kind = (mask >> 2*(3-(t & 3))) & 3;
78662306a36Sopenharmony_ci			if (kind > 0)
78762306a36Sopenharmony_ci				spill_next_when(&regs[kind - 1], sr->curr.reg + limit[kind - 1],
78862306a36Sopenharmony_ci						sr->region_start + t);
78962306a36Sopenharmony_ci		}
79062306a36Sopenharmony_ci	}
79162306a36Sopenharmony_ci	/*
79262306a36Sopenharmony_ci	 * Next, lay out the memory stack spill area:
79362306a36Sopenharmony_ci	 */
79462306a36Sopenharmony_ci	if (sr->any_spills) {
79562306a36Sopenharmony_ci		off = sr->spill_offset;
79662306a36Sopenharmony_ci		alloc_spill_area(&off, 16, sr->curr.reg + UNW_REG_F2, sr->curr.reg + UNW_REG_F31);
79762306a36Sopenharmony_ci		alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_B1, sr->curr.reg + UNW_REG_B5);
79862306a36Sopenharmony_ci		alloc_spill_area(&off,  8, sr->curr.reg + UNW_REG_R4, sr->curr.reg + UNW_REG_R7);
79962306a36Sopenharmony_ci	}
80062306a36Sopenharmony_ci}
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci/*
80362306a36Sopenharmony_ci * Region header descriptors.
80462306a36Sopenharmony_ci */
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_cistatic void
80762306a36Sopenharmony_cidesc_prologue (int body, unw_word rlen, unsigned char mask, unsigned char grsave,
80862306a36Sopenharmony_ci	       struct unw_state_record *sr)
80962306a36Sopenharmony_ci{
81062306a36Sopenharmony_ci	int i, region_start;
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci	if (!(sr->in_body || sr->first_region))
81362306a36Sopenharmony_ci		finish_prologue(sr);
81462306a36Sopenharmony_ci	sr->first_region = 0;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	/* check if we're done: */
81762306a36Sopenharmony_ci	if (sr->when_target < sr->region_start + sr->region_len) {
81862306a36Sopenharmony_ci		sr->done = 1;
81962306a36Sopenharmony_ci		return;
82062306a36Sopenharmony_ci	}
82162306a36Sopenharmony_ci
82262306a36Sopenharmony_ci	region_start = sr->region_start + sr->region_len;
82362306a36Sopenharmony_ci
82462306a36Sopenharmony_ci	for (i = 0; i < sr->epilogue_count; ++i)
82562306a36Sopenharmony_ci		pop(sr);
82662306a36Sopenharmony_ci	sr->epilogue_count = 0;
82762306a36Sopenharmony_ci	sr->epilogue_start = UNW_WHEN_NEVER;
82862306a36Sopenharmony_ci
82962306a36Sopenharmony_ci	sr->region_start = region_start;
83062306a36Sopenharmony_ci	sr->region_len = rlen;
83162306a36Sopenharmony_ci	sr->in_body = body;
83262306a36Sopenharmony_ci
83362306a36Sopenharmony_ci	if (!body) {
83462306a36Sopenharmony_ci		push(sr);
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci		for (i = 0; i < 4; ++i) {
83762306a36Sopenharmony_ci			if (mask & 0x8)
83862306a36Sopenharmony_ci				set_reg(sr->curr.reg + unw.save_order[i], UNW_WHERE_GR,
83962306a36Sopenharmony_ci					sr->region_start + sr->region_len - 1, grsave++);
84062306a36Sopenharmony_ci			mask <<= 1;
84162306a36Sopenharmony_ci		}
84262306a36Sopenharmony_ci		sr->gr_save_loc = grsave;
84362306a36Sopenharmony_ci		sr->any_spills = 0;
84462306a36Sopenharmony_ci		sr->imask = NULL;
84562306a36Sopenharmony_ci		sr->spill_offset = 0x10;	/* default to psp+16 */
84662306a36Sopenharmony_ci	}
84762306a36Sopenharmony_ci}
84862306a36Sopenharmony_ci
84962306a36Sopenharmony_ci/*
85062306a36Sopenharmony_ci * Prologue descriptors.
85162306a36Sopenharmony_ci */
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_cistatic inline void
85462306a36Sopenharmony_cidesc_abi (unsigned char abi, unsigned char context, struct unw_state_record *sr)
85562306a36Sopenharmony_ci{
85662306a36Sopenharmony_ci	if (abi == 3 && context == 'i') {
85762306a36Sopenharmony_ci		sr->flags |= UNW_FLAG_INTERRUPT_FRAME;
85862306a36Sopenharmony_ci		UNW_DPRINT(3, "unwind.%s: interrupt frame\n",  __func__);
85962306a36Sopenharmony_ci	}
86062306a36Sopenharmony_ci	else
86162306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind%s: ignoring unwabi(abi=0x%x,context=0x%x)\n",
86262306a36Sopenharmony_ci				__func__, abi, context);
86362306a36Sopenharmony_ci}
86462306a36Sopenharmony_ci
86562306a36Sopenharmony_cistatic inline void
86662306a36Sopenharmony_cidesc_br_gr (unsigned char brmask, unsigned char gr, struct unw_state_record *sr)
86762306a36Sopenharmony_ci{
86862306a36Sopenharmony_ci	int i;
86962306a36Sopenharmony_ci
87062306a36Sopenharmony_ci	for (i = 0; i < 5; ++i) {
87162306a36Sopenharmony_ci		if (brmask & 1)
87262306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_GR,
87362306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, gr++);
87462306a36Sopenharmony_ci		brmask >>= 1;
87562306a36Sopenharmony_ci	}
87662306a36Sopenharmony_ci}
87762306a36Sopenharmony_ci
87862306a36Sopenharmony_cistatic inline void
87962306a36Sopenharmony_cidesc_br_mem (unsigned char brmask, struct unw_state_record *sr)
88062306a36Sopenharmony_ci{
88162306a36Sopenharmony_ci	int i;
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci	for (i = 0; i < 5; ++i) {
88462306a36Sopenharmony_ci		if (brmask & 1) {
88562306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_B1 + i, UNW_WHERE_SPILL_HOME,
88662306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
88762306a36Sopenharmony_ci			sr->any_spills = 1;
88862306a36Sopenharmony_ci		}
88962306a36Sopenharmony_ci		brmask >>= 1;
89062306a36Sopenharmony_ci	}
89162306a36Sopenharmony_ci}
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_cistatic inline void
89462306a36Sopenharmony_cidesc_frgr_mem (unsigned char grmask, unw_word frmask, struct unw_state_record *sr)
89562306a36Sopenharmony_ci{
89662306a36Sopenharmony_ci	int i;
89762306a36Sopenharmony_ci
89862306a36Sopenharmony_ci	for (i = 0; i < 4; ++i) {
89962306a36Sopenharmony_ci		if ((grmask & 1) != 0) {
90062306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
90162306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
90262306a36Sopenharmony_ci			sr->any_spills = 1;
90362306a36Sopenharmony_ci		}
90462306a36Sopenharmony_ci		grmask >>= 1;
90562306a36Sopenharmony_ci	}
90662306a36Sopenharmony_ci	for (i = 0; i < 20; ++i) {
90762306a36Sopenharmony_ci		if ((frmask & 1) != 0) {
90862306a36Sopenharmony_ci			int base = (i < 4) ? UNW_REG_F2 : UNW_REG_F16 - 4;
90962306a36Sopenharmony_ci			set_reg(sr->curr.reg + base + i, UNW_WHERE_SPILL_HOME,
91062306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
91162306a36Sopenharmony_ci			sr->any_spills = 1;
91262306a36Sopenharmony_ci		}
91362306a36Sopenharmony_ci		frmask >>= 1;
91462306a36Sopenharmony_ci	}
91562306a36Sopenharmony_ci}
91662306a36Sopenharmony_ci
91762306a36Sopenharmony_cistatic inline void
91862306a36Sopenharmony_cidesc_fr_mem (unsigned char frmask, struct unw_state_record *sr)
91962306a36Sopenharmony_ci{
92062306a36Sopenharmony_ci	int i;
92162306a36Sopenharmony_ci
92262306a36Sopenharmony_ci	for (i = 0; i < 4; ++i) {
92362306a36Sopenharmony_ci		if ((frmask & 1) != 0) {
92462306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_F2 + i, UNW_WHERE_SPILL_HOME,
92562306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
92662306a36Sopenharmony_ci			sr->any_spills = 1;
92762306a36Sopenharmony_ci		}
92862306a36Sopenharmony_ci		frmask >>= 1;
92962306a36Sopenharmony_ci	}
93062306a36Sopenharmony_ci}
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_cistatic inline void
93362306a36Sopenharmony_cidesc_gr_gr (unsigned char grmask, unsigned char gr, struct unw_state_record *sr)
93462306a36Sopenharmony_ci{
93562306a36Sopenharmony_ci	int i;
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci	for (i = 0; i < 4; ++i) {
93862306a36Sopenharmony_ci		if ((grmask & 1) != 0)
93962306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_GR,
94062306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, gr++);
94162306a36Sopenharmony_ci		grmask >>= 1;
94262306a36Sopenharmony_ci	}
94362306a36Sopenharmony_ci}
94462306a36Sopenharmony_ci
94562306a36Sopenharmony_cistatic inline void
94662306a36Sopenharmony_cidesc_gr_mem (unsigned char grmask, struct unw_state_record *sr)
94762306a36Sopenharmony_ci{
94862306a36Sopenharmony_ci	int i;
94962306a36Sopenharmony_ci
95062306a36Sopenharmony_ci	for (i = 0; i < 4; ++i) {
95162306a36Sopenharmony_ci		if ((grmask & 1) != 0) {
95262306a36Sopenharmony_ci			set_reg(sr->curr.reg + UNW_REG_R4 + i, UNW_WHERE_SPILL_HOME,
95362306a36Sopenharmony_ci				sr->region_start + sr->region_len - 1, 0);
95462306a36Sopenharmony_ci			sr->any_spills = 1;
95562306a36Sopenharmony_ci		}
95662306a36Sopenharmony_ci		grmask >>= 1;
95762306a36Sopenharmony_ci	}
95862306a36Sopenharmony_ci}
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_cistatic inline void
96162306a36Sopenharmony_cidesc_mem_stack_f (unw_word t, unw_word size, struct unw_state_record *sr)
96262306a36Sopenharmony_ci{
96362306a36Sopenharmony_ci	set_reg(sr->curr.reg + UNW_REG_PSP, UNW_WHERE_NONE,
96462306a36Sopenharmony_ci		sr->region_start + min_t(int, t, sr->region_len - 1), 16*size);
96562306a36Sopenharmony_ci}
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_cistatic inline void
96862306a36Sopenharmony_cidesc_mem_stack_v (unw_word t, struct unw_state_record *sr)
96962306a36Sopenharmony_ci{
97062306a36Sopenharmony_ci	sr->curr.reg[UNW_REG_PSP].when = sr->region_start + min_t(int, t, sr->region_len - 1);
97162306a36Sopenharmony_ci}
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_cistatic inline void
97462306a36Sopenharmony_cidesc_reg_gr (unsigned char reg, unsigned char dst, struct unw_state_record *sr)
97562306a36Sopenharmony_ci{
97662306a36Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_GR, sr->region_start + sr->region_len - 1, dst);
97762306a36Sopenharmony_ci}
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_cistatic inline void
98062306a36Sopenharmony_cidesc_reg_psprel (unsigned char reg, unw_word pspoff, struct unw_state_record *sr)
98162306a36Sopenharmony_ci{
98262306a36Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_PSPREL, sr->region_start + sr->region_len - 1,
98362306a36Sopenharmony_ci		0x10 - 4*pspoff);
98462306a36Sopenharmony_ci}
98562306a36Sopenharmony_ci
98662306a36Sopenharmony_cistatic inline void
98762306a36Sopenharmony_cidesc_reg_sprel (unsigned char reg, unw_word spoff, struct unw_state_record *sr)
98862306a36Sopenharmony_ci{
98962306a36Sopenharmony_ci	set_reg(sr->curr.reg + reg, UNW_WHERE_SPREL, sr->region_start + sr->region_len - 1,
99062306a36Sopenharmony_ci		4*spoff);
99162306a36Sopenharmony_ci}
99262306a36Sopenharmony_ci
99362306a36Sopenharmony_cistatic inline void
99462306a36Sopenharmony_cidesc_rp_br (unsigned char dst, struct unw_state_record *sr)
99562306a36Sopenharmony_ci{
99662306a36Sopenharmony_ci	sr->return_link_reg = dst;
99762306a36Sopenharmony_ci}
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_cistatic inline void
100062306a36Sopenharmony_cidesc_reg_when (unsigned char regnum, unw_word t, struct unw_state_record *sr)
100162306a36Sopenharmony_ci{
100262306a36Sopenharmony_ci	struct unw_reg_info *reg = sr->curr.reg + regnum;
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci	if (reg->where == UNW_WHERE_NONE)
100562306a36Sopenharmony_ci		reg->where = UNW_WHERE_GR_SAVE;
100662306a36Sopenharmony_ci	reg->when = sr->region_start + min_t(int, t, sr->region_len - 1);
100762306a36Sopenharmony_ci}
100862306a36Sopenharmony_ci
100962306a36Sopenharmony_cistatic inline void
101062306a36Sopenharmony_cidesc_spill_base (unw_word pspoff, struct unw_state_record *sr)
101162306a36Sopenharmony_ci{
101262306a36Sopenharmony_ci	sr->spill_offset = 0x10 - 4*pspoff;
101362306a36Sopenharmony_ci}
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_cistatic inline unsigned char *
101662306a36Sopenharmony_cidesc_spill_mask (unsigned char *imaskp, struct unw_state_record *sr)
101762306a36Sopenharmony_ci{
101862306a36Sopenharmony_ci	sr->imask = imaskp;
101962306a36Sopenharmony_ci	return imaskp + (2*sr->region_len + 7)/8;
102062306a36Sopenharmony_ci}
102162306a36Sopenharmony_ci
102262306a36Sopenharmony_ci/*
102362306a36Sopenharmony_ci * Body descriptors.
102462306a36Sopenharmony_ci */
102562306a36Sopenharmony_cistatic inline void
102662306a36Sopenharmony_cidesc_epilogue (unw_word t, unw_word ecount, struct unw_state_record *sr)
102762306a36Sopenharmony_ci{
102862306a36Sopenharmony_ci	sr->epilogue_start = sr->region_start + sr->region_len - 1 - t;
102962306a36Sopenharmony_ci	sr->epilogue_count = ecount + 1;
103062306a36Sopenharmony_ci}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_cistatic inline void
103362306a36Sopenharmony_cidesc_copy_state (unw_word label, struct unw_state_record *sr)
103462306a36Sopenharmony_ci{
103562306a36Sopenharmony_ci	struct unw_labeled_state *ls;
103662306a36Sopenharmony_ci
103762306a36Sopenharmony_ci	for (ls = sr->labeled_states; ls; ls = ls->next) {
103862306a36Sopenharmony_ci		if (ls->label == label) {
103962306a36Sopenharmony_ci			free_state_stack(&sr->curr);
104062306a36Sopenharmony_ci			memcpy(&sr->curr, &ls->saved_state, sizeof(sr->curr));
104162306a36Sopenharmony_ci			sr->curr.next = dup_state_stack(ls->saved_state.next);
104262306a36Sopenharmony_ci			return;
104362306a36Sopenharmony_ci		}
104462306a36Sopenharmony_ci	}
104562306a36Sopenharmony_ci	printk(KERN_ERR "unwind: failed to find state labeled 0x%lx\n", label);
104662306a36Sopenharmony_ci}
104762306a36Sopenharmony_ci
104862306a36Sopenharmony_cistatic inline void
104962306a36Sopenharmony_cidesc_label_state (unw_word label, struct unw_state_record *sr)
105062306a36Sopenharmony_ci{
105162306a36Sopenharmony_ci	struct unw_labeled_state *ls;
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	ls = alloc_labeled_state();
105462306a36Sopenharmony_ci	if (!ls) {
105562306a36Sopenharmony_ci		printk(KERN_ERR "unwind.desc_label_state(): out of memory\n");
105662306a36Sopenharmony_ci		return;
105762306a36Sopenharmony_ci	}
105862306a36Sopenharmony_ci	ls->label = label;
105962306a36Sopenharmony_ci	memcpy(&ls->saved_state, &sr->curr, sizeof(ls->saved_state));
106062306a36Sopenharmony_ci	ls->saved_state.next = dup_state_stack(sr->curr.next);
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci	/* insert into list of labeled states: */
106362306a36Sopenharmony_ci	ls->next = sr->labeled_states;
106462306a36Sopenharmony_ci	sr->labeled_states = ls;
106562306a36Sopenharmony_ci}
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci/*
106862306a36Sopenharmony_ci * General descriptors.
106962306a36Sopenharmony_ci */
107062306a36Sopenharmony_ci
107162306a36Sopenharmony_cistatic inline int
107262306a36Sopenharmony_cidesc_is_active (unsigned char qp, unw_word t, struct unw_state_record *sr)
107362306a36Sopenharmony_ci{
107462306a36Sopenharmony_ci	if (sr->when_target <= sr->region_start + min_t(int, t, sr->region_len - 1))
107562306a36Sopenharmony_ci		return 0;
107662306a36Sopenharmony_ci	if (qp > 0) {
107762306a36Sopenharmony_ci		if ((sr->pr_val & (1UL << qp)) == 0)
107862306a36Sopenharmony_ci			return 0;
107962306a36Sopenharmony_ci		sr->pr_mask |= (1UL << qp);
108062306a36Sopenharmony_ci	}
108162306a36Sopenharmony_ci	return 1;
108262306a36Sopenharmony_ci}
108362306a36Sopenharmony_ci
108462306a36Sopenharmony_cistatic inline void
108562306a36Sopenharmony_cidesc_restore_p (unsigned char qp, unw_word t, unsigned char abreg, struct unw_state_record *sr)
108662306a36Sopenharmony_ci{
108762306a36Sopenharmony_ci	struct unw_reg_info *r;
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
109062306a36Sopenharmony_ci		return;
109162306a36Sopenharmony_ci
109262306a36Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 0);
109362306a36Sopenharmony_ci	r->where = UNW_WHERE_NONE;
109462306a36Sopenharmony_ci	r->when = UNW_WHEN_NEVER;
109562306a36Sopenharmony_ci	r->val = 0;
109662306a36Sopenharmony_ci}
109762306a36Sopenharmony_ci
109862306a36Sopenharmony_cistatic inline void
109962306a36Sopenharmony_cidesc_spill_reg_p (unsigned char qp, unw_word t, unsigned char abreg, unsigned char x,
110062306a36Sopenharmony_ci		     unsigned char ytreg, struct unw_state_record *sr)
110162306a36Sopenharmony_ci{
110262306a36Sopenharmony_ci	enum unw_where where = UNW_WHERE_GR;
110362306a36Sopenharmony_ci	struct unw_reg_info *r;
110462306a36Sopenharmony_ci
110562306a36Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
110662306a36Sopenharmony_ci		return;
110762306a36Sopenharmony_ci
110862306a36Sopenharmony_ci	if (x)
110962306a36Sopenharmony_ci		where = UNW_WHERE_BR;
111062306a36Sopenharmony_ci	else if (ytreg & 0x80)
111162306a36Sopenharmony_ci		where = UNW_WHERE_FR;
111262306a36Sopenharmony_ci
111362306a36Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 0);
111462306a36Sopenharmony_ci	r->where = where;
111562306a36Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
111662306a36Sopenharmony_ci	r->val = (ytreg & 0x7f);
111762306a36Sopenharmony_ci}
111862306a36Sopenharmony_ci
111962306a36Sopenharmony_cistatic inline void
112062306a36Sopenharmony_cidesc_spill_psprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word pspoff,
112162306a36Sopenharmony_ci		     struct unw_state_record *sr)
112262306a36Sopenharmony_ci{
112362306a36Sopenharmony_ci	struct unw_reg_info *r;
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
112662306a36Sopenharmony_ci		return;
112762306a36Sopenharmony_ci
112862306a36Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 1);
112962306a36Sopenharmony_ci	r->where = UNW_WHERE_PSPREL;
113062306a36Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
113162306a36Sopenharmony_ci	r->val = 0x10 - 4*pspoff;
113262306a36Sopenharmony_ci}
113362306a36Sopenharmony_ci
113462306a36Sopenharmony_cistatic inline void
113562306a36Sopenharmony_cidesc_spill_sprel_p (unsigned char qp, unw_word t, unsigned char abreg, unw_word spoff,
113662306a36Sopenharmony_ci		       struct unw_state_record *sr)
113762306a36Sopenharmony_ci{
113862306a36Sopenharmony_ci	struct unw_reg_info *r;
113962306a36Sopenharmony_ci
114062306a36Sopenharmony_ci	if (!desc_is_active(qp, t, sr))
114162306a36Sopenharmony_ci		return;
114262306a36Sopenharmony_ci
114362306a36Sopenharmony_ci	r = sr->curr.reg + decode_abreg(abreg, 1);
114462306a36Sopenharmony_ci	r->where = UNW_WHERE_SPREL;
114562306a36Sopenharmony_ci	r->when = sr->region_start + min_t(int, t, sr->region_len - 1);
114662306a36Sopenharmony_ci	r->val = 4*spoff;
114762306a36Sopenharmony_ci}
114862306a36Sopenharmony_ci
114962306a36Sopenharmony_ci#define UNW_DEC_BAD_CODE(code)			printk(KERN_ERR "unwind: unknown code 0x%02x\n", \
115062306a36Sopenharmony_ci						       code);
115162306a36Sopenharmony_ci
115262306a36Sopenharmony_ci/*
115362306a36Sopenharmony_ci * region headers:
115462306a36Sopenharmony_ci */
115562306a36Sopenharmony_ci#define UNW_DEC_PROLOGUE_GR(fmt,r,m,gr,arg)	desc_prologue(0,r,m,gr,arg)
115662306a36Sopenharmony_ci#define UNW_DEC_PROLOGUE(fmt,b,r,arg)		desc_prologue(b,r,0,32,arg)
115762306a36Sopenharmony_ci/*
115862306a36Sopenharmony_ci * prologue descriptors:
115962306a36Sopenharmony_ci */
116062306a36Sopenharmony_ci#define UNW_DEC_ABI(fmt,a,c,arg)		desc_abi(a,c,arg)
116162306a36Sopenharmony_ci#define UNW_DEC_BR_GR(fmt,b,g,arg)		desc_br_gr(b,g,arg)
116262306a36Sopenharmony_ci#define UNW_DEC_BR_MEM(fmt,b,arg)		desc_br_mem(b,arg)
116362306a36Sopenharmony_ci#define UNW_DEC_FRGR_MEM(fmt,g,f,arg)		desc_frgr_mem(g,f,arg)
116462306a36Sopenharmony_ci#define UNW_DEC_FR_MEM(fmt,f,arg)		desc_fr_mem(f,arg)
116562306a36Sopenharmony_ci#define UNW_DEC_GR_GR(fmt,m,g,arg)		desc_gr_gr(m,g,arg)
116662306a36Sopenharmony_ci#define UNW_DEC_GR_MEM(fmt,m,arg)		desc_gr_mem(m,arg)
116762306a36Sopenharmony_ci#define UNW_DEC_MEM_STACK_F(fmt,t,s,arg)	desc_mem_stack_f(t,s,arg)
116862306a36Sopenharmony_ci#define UNW_DEC_MEM_STACK_V(fmt,t,arg)		desc_mem_stack_v(t,arg)
116962306a36Sopenharmony_ci#define UNW_DEC_REG_GR(fmt,r,d,arg)		desc_reg_gr(r,d,arg)
117062306a36Sopenharmony_ci#define UNW_DEC_REG_PSPREL(fmt,r,o,arg)		desc_reg_psprel(r,o,arg)
117162306a36Sopenharmony_ci#define UNW_DEC_REG_SPREL(fmt,r,o,arg)		desc_reg_sprel(r,o,arg)
117262306a36Sopenharmony_ci#define UNW_DEC_REG_WHEN(fmt,r,t,arg)		desc_reg_when(r,t,arg)
117362306a36Sopenharmony_ci#define UNW_DEC_PRIUNAT_WHEN_GR(fmt,t,arg)	desc_reg_when(UNW_REG_PRI_UNAT_GR,t,arg)
117462306a36Sopenharmony_ci#define UNW_DEC_PRIUNAT_WHEN_MEM(fmt,t,arg)	desc_reg_when(UNW_REG_PRI_UNAT_MEM,t,arg)
117562306a36Sopenharmony_ci#define UNW_DEC_PRIUNAT_GR(fmt,r,arg)		desc_reg_gr(UNW_REG_PRI_UNAT_GR,r,arg)
117662306a36Sopenharmony_ci#define UNW_DEC_PRIUNAT_PSPREL(fmt,o,arg)	desc_reg_psprel(UNW_REG_PRI_UNAT_MEM,o,arg)
117762306a36Sopenharmony_ci#define UNW_DEC_PRIUNAT_SPREL(fmt,o,arg)	desc_reg_sprel(UNW_REG_PRI_UNAT_MEM,o,arg)
117862306a36Sopenharmony_ci#define UNW_DEC_RP_BR(fmt,d,arg)		desc_rp_br(d,arg)
117962306a36Sopenharmony_ci#define UNW_DEC_SPILL_BASE(fmt,o,arg)		desc_spill_base(o,arg)
118062306a36Sopenharmony_ci#define UNW_DEC_SPILL_MASK(fmt,m,arg)		(m = desc_spill_mask(m,arg))
118162306a36Sopenharmony_ci/*
118262306a36Sopenharmony_ci * body descriptors:
118362306a36Sopenharmony_ci */
118462306a36Sopenharmony_ci#define UNW_DEC_EPILOGUE(fmt,t,c,arg)		desc_epilogue(t,c,arg)
118562306a36Sopenharmony_ci#define UNW_DEC_COPY_STATE(fmt,l,arg)		desc_copy_state(l,arg)
118662306a36Sopenharmony_ci#define UNW_DEC_LABEL_STATE(fmt,l,arg)		desc_label_state(l,arg)
118762306a36Sopenharmony_ci/*
118862306a36Sopenharmony_ci * general unwind descriptors:
118962306a36Sopenharmony_ci */
119062306a36Sopenharmony_ci#define UNW_DEC_SPILL_REG_P(f,p,t,a,x,y,arg)	desc_spill_reg_p(p,t,a,x,y,arg)
119162306a36Sopenharmony_ci#define UNW_DEC_SPILL_REG(f,t,a,x,y,arg)	desc_spill_reg_p(0,t,a,x,y,arg)
119262306a36Sopenharmony_ci#define UNW_DEC_SPILL_PSPREL_P(f,p,t,a,o,arg)	desc_spill_psprel_p(p,t,a,o,arg)
119362306a36Sopenharmony_ci#define UNW_DEC_SPILL_PSPREL(f,t,a,o,arg)	desc_spill_psprel_p(0,t,a,o,arg)
119462306a36Sopenharmony_ci#define UNW_DEC_SPILL_SPREL_P(f,p,t,a,o,arg)	desc_spill_sprel_p(p,t,a,o,arg)
119562306a36Sopenharmony_ci#define UNW_DEC_SPILL_SPREL(f,t,a,o,arg)	desc_spill_sprel_p(0,t,a,o,arg)
119662306a36Sopenharmony_ci#define UNW_DEC_RESTORE_P(f,p,t,a,arg)		desc_restore_p(p,t,a,arg)
119762306a36Sopenharmony_ci#define UNW_DEC_RESTORE(f,t,a,arg)		desc_restore_p(0,t,a,arg)
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci#include "unwind_decoder.c"
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_ci
120262306a36Sopenharmony_ci/* Unwind scripts. */
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_cistatic inline unw_hash_index_t
120562306a36Sopenharmony_cihash (unsigned long ip)
120662306a36Sopenharmony_ci{
120762306a36Sopenharmony_ci	/* magic number = ((sqrt(5)-1)/2)*2^64 */
120862306a36Sopenharmony_ci	static const unsigned long hashmagic = 0x9e3779b97f4a7c16UL;
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	return (ip >> 4) * hashmagic >> (64 - UNW_LOG_HASH_SIZE);
121162306a36Sopenharmony_ci}
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_cistatic inline long
121462306a36Sopenharmony_cicache_match (struct unw_script *script, unsigned long ip, unsigned long pr)
121562306a36Sopenharmony_ci{
121662306a36Sopenharmony_ci	read_lock(&script->lock);
121762306a36Sopenharmony_ci	if (ip == script->ip && ((pr ^ script->pr_val) & script->pr_mask) == 0)
121862306a36Sopenharmony_ci		/* keep the read lock... */
121962306a36Sopenharmony_ci		return 1;
122062306a36Sopenharmony_ci	read_unlock(&script->lock);
122162306a36Sopenharmony_ci	return 0;
122262306a36Sopenharmony_ci}
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_cistatic inline struct unw_script *
122562306a36Sopenharmony_ciscript_lookup (struct unw_frame_info *info)
122662306a36Sopenharmony_ci{
122762306a36Sopenharmony_ci	struct unw_script *script = unw.cache + info->hint;
122862306a36Sopenharmony_ci	unsigned short index;
122962306a36Sopenharmony_ci	unsigned long ip, pr;
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	if (UNW_DEBUG_ON(0))
123262306a36Sopenharmony_ci		return NULL;	/* Always regenerate scripts in debug mode */
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci	STAT(++unw.stat.cache.lookups);
123562306a36Sopenharmony_ci
123662306a36Sopenharmony_ci	ip = info->ip;
123762306a36Sopenharmony_ci	pr = info->pr;
123862306a36Sopenharmony_ci
123962306a36Sopenharmony_ci	if (cache_match(script, ip, pr)) {
124062306a36Sopenharmony_ci		STAT(++unw.stat.cache.hinted_hits);
124162306a36Sopenharmony_ci		return script;
124262306a36Sopenharmony_ci	}
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	index = unw.hash[hash(ip)];
124562306a36Sopenharmony_ci	if (index >= UNW_CACHE_SIZE)
124662306a36Sopenharmony_ci		return NULL;
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_ci	script = unw.cache + index;
124962306a36Sopenharmony_ci	while (1) {
125062306a36Sopenharmony_ci		if (cache_match(script, ip, pr)) {
125162306a36Sopenharmony_ci			/* update hint; no locking required as single-word writes are atomic */
125262306a36Sopenharmony_ci			STAT(++unw.stat.cache.normal_hits);
125362306a36Sopenharmony_ci			unw.cache[info->prev_script].hint = script - unw.cache;
125462306a36Sopenharmony_ci			return script;
125562306a36Sopenharmony_ci		}
125662306a36Sopenharmony_ci		if (script->coll_chain >= UNW_HASH_SIZE)
125762306a36Sopenharmony_ci			return NULL;
125862306a36Sopenharmony_ci		script = unw.cache + script->coll_chain;
125962306a36Sopenharmony_ci		STAT(++unw.stat.cache.collision_chain_traversals);
126062306a36Sopenharmony_ci	}
126162306a36Sopenharmony_ci}
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci/*
126462306a36Sopenharmony_ci * On returning, a write lock for the SCRIPT is still being held.
126562306a36Sopenharmony_ci */
126662306a36Sopenharmony_cistatic inline struct unw_script *
126762306a36Sopenharmony_ciscript_new (unsigned long ip)
126862306a36Sopenharmony_ci{
126962306a36Sopenharmony_ci	struct unw_script *script, *prev, *tmp;
127062306a36Sopenharmony_ci	unw_hash_index_t index;
127162306a36Sopenharmony_ci	unsigned short head;
127262306a36Sopenharmony_ci
127362306a36Sopenharmony_ci	STAT(++unw.stat.script.news);
127462306a36Sopenharmony_ci
127562306a36Sopenharmony_ci	/*
127662306a36Sopenharmony_ci	 * Can't (easily) use cmpxchg() here because of ABA problem
127762306a36Sopenharmony_ci	 * that is intrinsic in cmpxchg()...
127862306a36Sopenharmony_ci	 */
127962306a36Sopenharmony_ci	head = unw.lru_head;
128062306a36Sopenharmony_ci	script = unw.cache + head;
128162306a36Sopenharmony_ci	unw.lru_head = script->lru_chain;
128262306a36Sopenharmony_ci
128362306a36Sopenharmony_ci	/*
128462306a36Sopenharmony_ci	 * We'd deadlock here if we interrupted a thread that is holding a read lock on
128562306a36Sopenharmony_ci	 * script->lock.  Thus, if the write_trylock() fails, we simply bail out.  The
128662306a36Sopenharmony_ci	 * alternative would be to disable interrupts whenever we hold a read-lock, but
128762306a36Sopenharmony_ci	 * that seems silly.
128862306a36Sopenharmony_ci	 */
128962306a36Sopenharmony_ci	if (!write_trylock(&script->lock))
129062306a36Sopenharmony_ci		return NULL;
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	/* re-insert script at the tail of the LRU chain: */
129362306a36Sopenharmony_ci	unw.cache[unw.lru_tail].lru_chain = head;
129462306a36Sopenharmony_ci	unw.lru_tail = head;
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci	/* remove the old script from the hash table (if it's there): */
129762306a36Sopenharmony_ci	if (script->ip) {
129862306a36Sopenharmony_ci		index = hash(script->ip);
129962306a36Sopenharmony_ci		tmp = unw.cache + unw.hash[index];
130062306a36Sopenharmony_ci		prev = NULL;
130162306a36Sopenharmony_ci		while (1) {
130262306a36Sopenharmony_ci			if (tmp == script) {
130362306a36Sopenharmony_ci				if (prev)
130462306a36Sopenharmony_ci					prev->coll_chain = tmp->coll_chain;
130562306a36Sopenharmony_ci				else
130662306a36Sopenharmony_ci					unw.hash[index] = tmp->coll_chain;
130762306a36Sopenharmony_ci				break;
130862306a36Sopenharmony_ci			} else
130962306a36Sopenharmony_ci				prev = tmp;
131062306a36Sopenharmony_ci			if (tmp->coll_chain >= UNW_CACHE_SIZE)
131162306a36Sopenharmony_ci			/* old script wasn't in the hash-table */
131262306a36Sopenharmony_ci				break;
131362306a36Sopenharmony_ci			tmp = unw.cache + tmp->coll_chain;
131462306a36Sopenharmony_ci		}
131562306a36Sopenharmony_ci	}
131662306a36Sopenharmony_ci
131762306a36Sopenharmony_ci	/* enter new script in the hash table */
131862306a36Sopenharmony_ci	index = hash(ip);
131962306a36Sopenharmony_ci	script->coll_chain = unw.hash[index];
132062306a36Sopenharmony_ci	unw.hash[index] = script - unw.cache;
132162306a36Sopenharmony_ci
132262306a36Sopenharmony_ci	script->ip = ip;	/* set new IP while we're holding the locks */
132362306a36Sopenharmony_ci
132462306a36Sopenharmony_ci	STAT(if (script->coll_chain < UNW_CACHE_SIZE) ++unw.stat.script.collisions);
132562306a36Sopenharmony_ci
132662306a36Sopenharmony_ci	script->flags = 0;
132762306a36Sopenharmony_ci	script->hint = 0;
132862306a36Sopenharmony_ci	script->count = 0;
132962306a36Sopenharmony_ci	return script;
133062306a36Sopenharmony_ci}
133162306a36Sopenharmony_ci
133262306a36Sopenharmony_cistatic void
133362306a36Sopenharmony_ciscript_finalize (struct unw_script *script, struct unw_state_record *sr)
133462306a36Sopenharmony_ci{
133562306a36Sopenharmony_ci	script->pr_mask = sr->pr_mask;
133662306a36Sopenharmony_ci	script->pr_val = sr->pr_val;
133762306a36Sopenharmony_ci	/*
133862306a36Sopenharmony_ci	 * We could down-grade our write-lock on script->lock here but
133962306a36Sopenharmony_ci	 * the rwlock API doesn't offer atomic lock downgrading, so
134062306a36Sopenharmony_ci	 * we'll just keep the write-lock and release it later when
134162306a36Sopenharmony_ci	 * we're done using the script.
134262306a36Sopenharmony_ci	 */
134362306a36Sopenharmony_ci}
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_cistatic inline void
134662306a36Sopenharmony_ciscript_emit (struct unw_script *script, struct unw_insn insn)
134762306a36Sopenharmony_ci{
134862306a36Sopenharmony_ci	if (script->count >= UNW_MAX_SCRIPT_LEN) {
134962306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: script exceeds maximum size of %u instructions!\n",
135062306a36Sopenharmony_ci			__func__, UNW_MAX_SCRIPT_LEN);
135162306a36Sopenharmony_ci		return;
135262306a36Sopenharmony_ci	}
135362306a36Sopenharmony_ci	script->insn[script->count++] = insn;
135462306a36Sopenharmony_ci}
135562306a36Sopenharmony_ci
135662306a36Sopenharmony_cistatic inline void
135762306a36Sopenharmony_ciemit_nat_info (struct unw_state_record *sr, int i, struct unw_script *script)
135862306a36Sopenharmony_ci{
135962306a36Sopenharmony_ci	struct unw_reg_info *r = sr->curr.reg + i;
136062306a36Sopenharmony_ci	enum unw_insn_opcode opc;
136162306a36Sopenharmony_ci	struct unw_insn insn;
136262306a36Sopenharmony_ci	unsigned long val = 0;
136362306a36Sopenharmony_ci
136462306a36Sopenharmony_ci	switch (r->where) {
136562306a36Sopenharmony_ci	      case UNW_WHERE_GR:
136662306a36Sopenharmony_ci		if (r->val >= 32) {
136762306a36Sopenharmony_ci			/* register got spilled to a stacked register */
136862306a36Sopenharmony_ci			opc = UNW_INSN_SETNAT_TYPE;
136962306a36Sopenharmony_ci			val = UNW_NAT_REGSTK;
137062306a36Sopenharmony_ci		} else
137162306a36Sopenharmony_ci			/* register got spilled to a scratch register */
137262306a36Sopenharmony_ci			opc = UNW_INSN_SETNAT_MEMSTK;
137362306a36Sopenharmony_ci		break;
137462306a36Sopenharmony_ci
137562306a36Sopenharmony_ci	      case UNW_WHERE_FR:
137662306a36Sopenharmony_ci		opc = UNW_INSN_SETNAT_TYPE;
137762306a36Sopenharmony_ci		val = UNW_NAT_VAL;
137862306a36Sopenharmony_ci		break;
137962306a36Sopenharmony_ci
138062306a36Sopenharmony_ci	      case UNW_WHERE_BR:
138162306a36Sopenharmony_ci		opc = UNW_INSN_SETNAT_TYPE;
138262306a36Sopenharmony_ci		val = UNW_NAT_NONE;
138362306a36Sopenharmony_ci		break;
138462306a36Sopenharmony_ci
138562306a36Sopenharmony_ci	      case UNW_WHERE_PSPREL:
138662306a36Sopenharmony_ci	      case UNW_WHERE_SPREL:
138762306a36Sopenharmony_ci		opc = UNW_INSN_SETNAT_MEMSTK;
138862306a36Sopenharmony_ci		break;
138962306a36Sopenharmony_ci
139062306a36Sopenharmony_ci	      default:
139162306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: don't know how to emit nat info for where = %u\n",
139262306a36Sopenharmony_ci			   __func__, r->where);
139362306a36Sopenharmony_ci		return;
139462306a36Sopenharmony_ci	}
139562306a36Sopenharmony_ci	insn.opc = opc;
139662306a36Sopenharmony_ci	insn.dst = unw.preg_index[i];
139762306a36Sopenharmony_ci	insn.val = val;
139862306a36Sopenharmony_ci	script_emit(script, insn);
139962306a36Sopenharmony_ci}
140062306a36Sopenharmony_ci
140162306a36Sopenharmony_cistatic void
140262306a36Sopenharmony_cicompile_reg (struct unw_state_record *sr, int i, struct unw_script *script)
140362306a36Sopenharmony_ci{
140462306a36Sopenharmony_ci	struct unw_reg_info *r = sr->curr.reg + i;
140562306a36Sopenharmony_ci	enum unw_insn_opcode opc;
140662306a36Sopenharmony_ci	unsigned long val, rval;
140762306a36Sopenharmony_ci	struct unw_insn insn;
140862306a36Sopenharmony_ci	long need_nat_info;
140962306a36Sopenharmony_ci
141062306a36Sopenharmony_ci	if (r->where == UNW_WHERE_NONE || r->when >= sr->when_target)
141162306a36Sopenharmony_ci		return;
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci	opc = UNW_INSN_MOVE;
141462306a36Sopenharmony_ci	val = rval = r->val;
141562306a36Sopenharmony_ci	need_nat_info = (i >= UNW_REG_R4 && i <= UNW_REG_R7);
141662306a36Sopenharmony_ci
141762306a36Sopenharmony_ci	switch (r->where) {
141862306a36Sopenharmony_ci	      case UNW_WHERE_GR:
141962306a36Sopenharmony_ci		if (rval >= 32) {
142062306a36Sopenharmony_ci			opc = UNW_INSN_MOVE_STACKED;
142162306a36Sopenharmony_ci			val = rval - 32;
142262306a36Sopenharmony_ci		} else if (rval >= 4 && rval <= 7) {
142362306a36Sopenharmony_ci			if (need_nat_info) {
142462306a36Sopenharmony_ci				opc = UNW_INSN_MOVE2;
142562306a36Sopenharmony_ci				need_nat_info = 0;
142662306a36Sopenharmony_ci			}
142762306a36Sopenharmony_ci			val = unw.preg_index[UNW_REG_R4 + (rval - 4)];
142862306a36Sopenharmony_ci		} else if (rval == 0) {
142962306a36Sopenharmony_ci			opc = UNW_INSN_MOVE_CONST;
143062306a36Sopenharmony_ci			val = 0;
143162306a36Sopenharmony_ci		} else {
143262306a36Sopenharmony_ci			/* register got spilled to a scratch register */
143362306a36Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
143462306a36Sopenharmony_ci			val = pt_regs_off(rval);
143562306a36Sopenharmony_ci		}
143662306a36Sopenharmony_ci		break;
143762306a36Sopenharmony_ci
143862306a36Sopenharmony_ci	      case UNW_WHERE_FR:
143962306a36Sopenharmony_ci		if (rval <= 5)
144062306a36Sopenharmony_ci			val = unw.preg_index[UNW_REG_F2  + (rval -  2)];
144162306a36Sopenharmony_ci		else if (rval >= 16 && rval <= 31)
144262306a36Sopenharmony_ci			val = unw.preg_index[UNW_REG_F16 + (rval - 16)];
144362306a36Sopenharmony_ci		else {
144462306a36Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
144562306a36Sopenharmony_ci			if (rval <= 11)
144662306a36Sopenharmony_ci				val = offsetof(struct pt_regs, f6) + 16*(rval - 6);
144762306a36Sopenharmony_ci			else
144862306a36Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: kernel may not touch f%lu\n",
144962306a36Sopenharmony_ci					   __func__, rval);
145062306a36Sopenharmony_ci		}
145162306a36Sopenharmony_ci		break;
145262306a36Sopenharmony_ci
145362306a36Sopenharmony_ci	      case UNW_WHERE_BR:
145462306a36Sopenharmony_ci		if (rval >= 1 && rval <= 5)
145562306a36Sopenharmony_ci			val = unw.preg_index[UNW_REG_B1 + (rval - 1)];
145662306a36Sopenharmony_ci		else {
145762306a36Sopenharmony_ci			opc = UNW_INSN_MOVE_SCRATCH;
145862306a36Sopenharmony_ci			if (rval == 0)
145962306a36Sopenharmony_ci				val = offsetof(struct pt_regs, b0);
146062306a36Sopenharmony_ci			else if (rval == 6)
146162306a36Sopenharmony_ci				val = offsetof(struct pt_regs, b6);
146262306a36Sopenharmony_ci			else
146362306a36Sopenharmony_ci				val = offsetof(struct pt_regs, b7);
146462306a36Sopenharmony_ci		}
146562306a36Sopenharmony_ci		break;
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_ci	      case UNW_WHERE_SPREL:
146862306a36Sopenharmony_ci		opc = UNW_INSN_ADD_SP;
146962306a36Sopenharmony_ci		break;
147062306a36Sopenharmony_ci
147162306a36Sopenharmony_ci	      case UNW_WHERE_PSPREL:
147262306a36Sopenharmony_ci		opc = UNW_INSN_ADD_PSP;
147362306a36Sopenharmony_ci		break;
147462306a36Sopenharmony_ci
147562306a36Sopenharmony_ci	      default:
147662306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind%s: register %u has unexpected `where' value of %u\n",
147762306a36Sopenharmony_ci			   __func__, i, r->where);
147862306a36Sopenharmony_ci		break;
147962306a36Sopenharmony_ci	}
148062306a36Sopenharmony_ci	insn.opc = opc;
148162306a36Sopenharmony_ci	insn.dst = unw.preg_index[i];
148262306a36Sopenharmony_ci	insn.val = val;
148362306a36Sopenharmony_ci	script_emit(script, insn);
148462306a36Sopenharmony_ci	if (need_nat_info)
148562306a36Sopenharmony_ci		emit_nat_info(sr, i, script);
148662306a36Sopenharmony_ci
148762306a36Sopenharmony_ci	if (i == UNW_REG_PSP) {
148862306a36Sopenharmony_ci		/*
148962306a36Sopenharmony_ci		 * info->psp must contain the _value_ of the previous
149062306a36Sopenharmony_ci		 * sp, not it's save location.  We get this by
149162306a36Sopenharmony_ci		 * dereferencing the value we just stored in
149262306a36Sopenharmony_ci		 * info->psp:
149362306a36Sopenharmony_ci		 */
149462306a36Sopenharmony_ci		insn.opc = UNW_INSN_LOAD;
149562306a36Sopenharmony_ci		insn.dst = insn.val = unw.preg_index[UNW_REG_PSP];
149662306a36Sopenharmony_ci		script_emit(script, insn);
149762306a36Sopenharmony_ci	}
149862306a36Sopenharmony_ci}
149962306a36Sopenharmony_ci
150062306a36Sopenharmony_cistatic inline const struct unw_table_entry *
150162306a36Sopenharmony_cilookup (struct unw_table *table, unsigned long rel_ip)
150262306a36Sopenharmony_ci{
150362306a36Sopenharmony_ci	const struct unw_table_entry *e = NULL;
150462306a36Sopenharmony_ci	unsigned long lo, hi, mid;
150562306a36Sopenharmony_ci
150662306a36Sopenharmony_ci	/* do a binary search for right entry: */
150762306a36Sopenharmony_ci	for (lo = 0, hi = table->length; lo < hi; ) {
150862306a36Sopenharmony_ci		mid = (lo + hi) / 2;
150962306a36Sopenharmony_ci		e = &table->array[mid];
151062306a36Sopenharmony_ci		if (rel_ip < e->start_offset)
151162306a36Sopenharmony_ci			hi = mid;
151262306a36Sopenharmony_ci		else if (rel_ip >= e->end_offset)
151362306a36Sopenharmony_ci			lo = mid + 1;
151462306a36Sopenharmony_ci		else
151562306a36Sopenharmony_ci			break;
151662306a36Sopenharmony_ci	}
151762306a36Sopenharmony_ci	if (rel_ip < e->start_offset || rel_ip >= e->end_offset)
151862306a36Sopenharmony_ci		return NULL;
151962306a36Sopenharmony_ci	return e;
152062306a36Sopenharmony_ci}
152162306a36Sopenharmony_ci
152262306a36Sopenharmony_ci/*
152362306a36Sopenharmony_ci * Build an unwind script that unwinds from state OLD_STATE to the
152462306a36Sopenharmony_ci * entrypoint of the function that called OLD_STATE.
152562306a36Sopenharmony_ci */
152662306a36Sopenharmony_cistatic inline struct unw_script *
152762306a36Sopenharmony_cibuild_script (struct unw_frame_info *info)
152862306a36Sopenharmony_ci{
152962306a36Sopenharmony_ci	const struct unw_table_entry *e = NULL;
153062306a36Sopenharmony_ci	struct unw_script *script = NULL;
153162306a36Sopenharmony_ci	struct unw_labeled_state *ls, *next;
153262306a36Sopenharmony_ci	unsigned long ip = info->ip;
153362306a36Sopenharmony_ci	struct unw_state_record sr;
153462306a36Sopenharmony_ci	struct unw_table *table, *prev;
153562306a36Sopenharmony_ci	struct unw_reg_info *r;
153662306a36Sopenharmony_ci	struct unw_insn insn;
153762306a36Sopenharmony_ci	u8 *dp, *desc_end;
153862306a36Sopenharmony_ci	u64 hdr;
153962306a36Sopenharmony_ci	int i;
154062306a36Sopenharmony_ci	STAT(unsigned long start, parse_start;)
154162306a36Sopenharmony_ci
154262306a36Sopenharmony_ci	STAT(++unw.stat.script.builds; start = ia64_get_itc());
154362306a36Sopenharmony_ci
154462306a36Sopenharmony_ci	/* build state record */
154562306a36Sopenharmony_ci	memset(&sr, 0, sizeof(sr));
154662306a36Sopenharmony_ci	for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
154762306a36Sopenharmony_ci		r->when = UNW_WHEN_NEVER;
154862306a36Sopenharmony_ci	sr.pr_val = info->pr;
154962306a36Sopenharmony_ci
155062306a36Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s: ip 0x%lx\n", __func__, ip);
155162306a36Sopenharmony_ci	script = script_new(ip);
155262306a36Sopenharmony_ci	if (!script) {
155362306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: failed to create unwind script\n",  __func__);
155462306a36Sopenharmony_ci		STAT(unw.stat.script.build_time += ia64_get_itc() - start);
155562306a36Sopenharmony_ci		return NULL;
155662306a36Sopenharmony_ci	}
155762306a36Sopenharmony_ci	unw.cache[info->prev_script].hint = script - unw.cache;
155862306a36Sopenharmony_ci
155962306a36Sopenharmony_ci	/* search the kernels and the modules' unwind tables for IP: */
156062306a36Sopenharmony_ci
156162306a36Sopenharmony_ci	STAT(parse_start = ia64_get_itc());
156262306a36Sopenharmony_ci
156362306a36Sopenharmony_ci	prev = NULL;
156462306a36Sopenharmony_ci	for (table = unw.tables; table; table = table->next) {
156562306a36Sopenharmony_ci		if (ip >= table->start && ip < table->end) {
156662306a36Sopenharmony_ci			/*
156762306a36Sopenharmony_ci			 * Leave the kernel unwind table at the very front,
156862306a36Sopenharmony_ci			 * lest moving it breaks some assumption elsewhere.
156962306a36Sopenharmony_ci			 * Otherwise, move the matching table to the second
157062306a36Sopenharmony_ci			 * position in the list so that traversals can benefit
157162306a36Sopenharmony_ci			 * from commonality in backtrace paths.
157262306a36Sopenharmony_ci			 */
157362306a36Sopenharmony_ci			if (prev && prev != unw.tables) {
157462306a36Sopenharmony_ci				/* unw is safe - we're already spinlocked */
157562306a36Sopenharmony_ci				prev->next = table->next;
157662306a36Sopenharmony_ci				table->next = unw.tables->next;
157762306a36Sopenharmony_ci				unw.tables->next = table;
157862306a36Sopenharmony_ci			}
157962306a36Sopenharmony_ci			e = lookup(table, ip - table->segment_base);
158062306a36Sopenharmony_ci			break;
158162306a36Sopenharmony_ci		}
158262306a36Sopenharmony_ci		prev = table;
158362306a36Sopenharmony_ci	}
158462306a36Sopenharmony_ci	if (!e) {
158562306a36Sopenharmony_ci		/* no info, return default unwinder (leaf proc, no mem stack, no saved regs)  */
158662306a36Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: no unwind info for ip=0x%lx (prev ip=0x%lx)\n",
158762306a36Sopenharmony_ci			__func__, ip, unw.cache[info->prev_script].ip);
158862306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
158962306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].when = -1;
159062306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].val = 0;
159162306a36Sopenharmony_ci		compile_reg(&sr, UNW_REG_RP, script);
159262306a36Sopenharmony_ci		script_finalize(script, &sr);
159362306a36Sopenharmony_ci		STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
159462306a36Sopenharmony_ci		STAT(unw.stat.script.build_time += ia64_get_itc() - start);
159562306a36Sopenharmony_ci		return script;
159662306a36Sopenharmony_ci	}
159762306a36Sopenharmony_ci
159862306a36Sopenharmony_ci	sr.when_target = (3*((ip & ~0xfUL) - (table->segment_base + e->start_offset))/16
159962306a36Sopenharmony_ci			  + (ip & 0xfUL));
160062306a36Sopenharmony_ci	hdr = *(u64 *) (table->segment_base + e->info_offset);
160162306a36Sopenharmony_ci	dp =   (u8 *)  (table->segment_base + e->info_offset + 8);
160262306a36Sopenharmony_ci	desc_end = dp + 8*UNW_LENGTH(hdr);
160362306a36Sopenharmony_ci
160462306a36Sopenharmony_ci	while (!sr.done && dp < desc_end)
160562306a36Sopenharmony_ci		dp = unw_decode(dp, sr.in_body, &sr);
160662306a36Sopenharmony_ci
160762306a36Sopenharmony_ci	if (sr.when_target > sr.epilogue_start) {
160862306a36Sopenharmony_ci		/*
160962306a36Sopenharmony_ci		 * sp has been restored and all values on the memory stack below
161062306a36Sopenharmony_ci		 * psp also have been restored.
161162306a36Sopenharmony_ci		 */
161262306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].val = 0;
161362306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].where = UNW_WHERE_NONE;
161462306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_PSP].when = UNW_WHEN_NEVER;
161562306a36Sopenharmony_ci		for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r)
161662306a36Sopenharmony_ci			if ((r->where == UNW_WHERE_PSPREL && r->val <= 0x10)
161762306a36Sopenharmony_ci			    || r->where == UNW_WHERE_SPREL)
161862306a36Sopenharmony_ci			{
161962306a36Sopenharmony_ci				r->val = 0;
162062306a36Sopenharmony_ci				r->where = UNW_WHERE_NONE;
162162306a36Sopenharmony_ci				r->when = UNW_WHEN_NEVER;
162262306a36Sopenharmony_ci			}
162362306a36Sopenharmony_ci	}
162462306a36Sopenharmony_ci
162562306a36Sopenharmony_ci	script->flags = sr.flags;
162662306a36Sopenharmony_ci
162762306a36Sopenharmony_ci	/*
162862306a36Sopenharmony_ci	 * If RP did't get saved, generate entry for the return link
162962306a36Sopenharmony_ci	 * register.
163062306a36Sopenharmony_ci	 */
163162306a36Sopenharmony_ci	if (sr.curr.reg[UNW_REG_RP].when >= sr.when_target) {
163262306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].where = UNW_WHERE_BR;
163362306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].when = -1;
163462306a36Sopenharmony_ci		sr.curr.reg[UNW_REG_RP].val = sr.return_link_reg;
163562306a36Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: using default for rp at ip=0x%lx where=%d val=0x%lx\n",
163662306a36Sopenharmony_ci			   __func__, ip, sr.curr.reg[UNW_REG_RP].where,
163762306a36Sopenharmony_ci			   sr.curr.reg[UNW_REG_RP].val);
163862306a36Sopenharmony_ci	}
163962306a36Sopenharmony_ci
164062306a36Sopenharmony_ci#ifdef UNW_DEBUG
164162306a36Sopenharmony_ci	UNW_DPRINT(1, "unwind.%s: state record for func 0x%lx, t=%u:\n",
164262306a36Sopenharmony_ci		__func__, table->segment_base + e->start_offset, sr.when_target);
164362306a36Sopenharmony_ci	for (r = sr.curr.reg; r < sr.curr.reg + UNW_NUM_REGS; ++r) {
164462306a36Sopenharmony_ci		if (r->where != UNW_WHERE_NONE || r->when != UNW_WHEN_NEVER) {
164562306a36Sopenharmony_ci			UNW_DPRINT(1, "  %s <- ", unw.preg_name[r - sr.curr.reg]);
164662306a36Sopenharmony_ci			switch (r->where) {
164762306a36Sopenharmony_ci			      case UNW_WHERE_GR:     UNW_DPRINT(1, "r%lu", r->val); break;
164862306a36Sopenharmony_ci			      case UNW_WHERE_FR:     UNW_DPRINT(1, "f%lu", r->val); break;
164962306a36Sopenharmony_ci			      case UNW_WHERE_BR:     UNW_DPRINT(1, "b%lu", r->val); break;
165062306a36Sopenharmony_ci			      case UNW_WHERE_SPREL:  UNW_DPRINT(1, "[sp+0x%lx]", r->val); break;
165162306a36Sopenharmony_ci			      case UNW_WHERE_PSPREL: UNW_DPRINT(1, "[psp+0x%lx]", r->val); break;
165262306a36Sopenharmony_ci			      case UNW_WHERE_NONE:
165362306a36Sopenharmony_ci				UNW_DPRINT(1, "%s+0x%lx", unw.preg_name[r - sr.curr.reg], r->val);
165462306a36Sopenharmony_ci				break;
165562306a36Sopenharmony_ci
165662306a36Sopenharmony_ci			      default:
165762306a36Sopenharmony_ci				UNW_DPRINT(1, "BADWHERE(%d)", r->where);
165862306a36Sopenharmony_ci				break;
165962306a36Sopenharmony_ci			}
166062306a36Sopenharmony_ci			UNW_DPRINT(1, "\t\t%d\n", r->when);
166162306a36Sopenharmony_ci		}
166262306a36Sopenharmony_ci	}
166362306a36Sopenharmony_ci#endif
166462306a36Sopenharmony_ci
166562306a36Sopenharmony_ci	STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
166662306a36Sopenharmony_ci
166762306a36Sopenharmony_ci	/* translate state record into unwinder instructions: */
166862306a36Sopenharmony_ci
166962306a36Sopenharmony_ci	/*
167062306a36Sopenharmony_ci	 * First, set psp if we're dealing with a fixed-size frame;
167162306a36Sopenharmony_ci	 * subsequent instructions may depend on this value.
167262306a36Sopenharmony_ci	 */
167362306a36Sopenharmony_ci	if (sr.when_target > sr.curr.reg[UNW_REG_PSP].when
167462306a36Sopenharmony_ci	    && (sr.curr.reg[UNW_REG_PSP].where == UNW_WHERE_NONE)
167562306a36Sopenharmony_ci	    && sr.curr.reg[UNW_REG_PSP].val != 0) {
167662306a36Sopenharmony_ci		/* new psp is sp plus frame size */
167762306a36Sopenharmony_ci		insn.opc = UNW_INSN_ADD;
167862306a36Sopenharmony_ci		insn.dst = offsetof(struct unw_frame_info, psp)/8;
167962306a36Sopenharmony_ci		insn.val = sr.curr.reg[UNW_REG_PSP].val;	/* frame size */
168062306a36Sopenharmony_ci		script_emit(script, insn);
168162306a36Sopenharmony_ci	}
168262306a36Sopenharmony_ci
168362306a36Sopenharmony_ci	/* determine where the primary UNaT is: */
168462306a36Sopenharmony_ci	if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
168562306a36Sopenharmony_ci		i = UNW_REG_PRI_UNAT_MEM;
168662306a36Sopenharmony_ci	else if (sr.when_target < sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when)
168762306a36Sopenharmony_ci		i = UNW_REG_PRI_UNAT_GR;
168862306a36Sopenharmony_ci	else if (sr.curr.reg[UNW_REG_PRI_UNAT_MEM].when > sr.curr.reg[UNW_REG_PRI_UNAT_GR].when)
168962306a36Sopenharmony_ci		i = UNW_REG_PRI_UNAT_MEM;
169062306a36Sopenharmony_ci	else
169162306a36Sopenharmony_ci		i = UNW_REG_PRI_UNAT_GR;
169262306a36Sopenharmony_ci
169362306a36Sopenharmony_ci	compile_reg(&sr, i, script);
169462306a36Sopenharmony_ci
169562306a36Sopenharmony_ci	for (i = UNW_REG_BSP; i < UNW_NUM_REGS; ++i)
169662306a36Sopenharmony_ci		compile_reg(&sr, i, script);
169762306a36Sopenharmony_ci
169862306a36Sopenharmony_ci	/* free labeled register states & stack: */
169962306a36Sopenharmony_ci
170062306a36Sopenharmony_ci	STAT(parse_start = ia64_get_itc());
170162306a36Sopenharmony_ci	for (ls = sr.labeled_states; ls; ls = next) {
170262306a36Sopenharmony_ci		next = ls->next;
170362306a36Sopenharmony_ci		free_state_stack(&ls->saved_state);
170462306a36Sopenharmony_ci		free_labeled_state(ls);
170562306a36Sopenharmony_ci	}
170662306a36Sopenharmony_ci	free_state_stack(&sr.curr);
170762306a36Sopenharmony_ci	STAT(unw.stat.script.parse_time += ia64_get_itc() - parse_start);
170862306a36Sopenharmony_ci
170962306a36Sopenharmony_ci	script_finalize(script, &sr);
171062306a36Sopenharmony_ci	STAT(unw.stat.script.build_time += ia64_get_itc() - start);
171162306a36Sopenharmony_ci	return script;
171262306a36Sopenharmony_ci}
171362306a36Sopenharmony_ci
171462306a36Sopenharmony_ci/*
171562306a36Sopenharmony_ci * Apply the unwinding actions represented by OPS and update SR to
171662306a36Sopenharmony_ci * reflect the state that existed upon entry to the function that this
171762306a36Sopenharmony_ci * unwinder represents.
171862306a36Sopenharmony_ci */
171962306a36Sopenharmony_cistatic inline void
172062306a36Sopenharmony_cirun_script (struct unw_script *script, struct unw_frame_info *state)
172162306a36Sopenharmony_ci{
172262306a36Sopenharmony_ci	struct unw_insn *ip, *limit, next_insn;
172362306a36Sopenharmony_ci	unsigned long opc, dst, val, off;
172462306a36Sopenharmony_ci	unsigned long *s = (unsigned long *) state;
172562306a36Sopenharmony_ci	STAT(unsigned long start;)
172662306a36Sopenharmony_ci
172762306a36Sopenharmony_ci	STAT(++unw.stat.script.runs; start = ia64_get_itc());
172862306a36Sopenharmony_ci	state->flags = script->flags;
172962306a36Sopenharmony_ci	ip = script->insn;
173062306a36Sopenharmony_ci	limit = script->insn + script->count;
173162306a36Sopenharmony_ci	next_insn = *ip;
173262306a36Sopenharmony_ci
173362306a36Sopenharmony_ci	while (ip++ < limit) {
173462306a36Sopenharmony_ci		opc = next_insn.opc;
173562306a36Sopenharmony_ci		dst = next_insn.dst;
173662306a36Sopenharmony_ci		val = next_insn.val;
173762306a36Sopenharmony_ci		next_insn = *ip;
173862306a36Sopenharmony_ci
173962306a36Sopenharmony_ci	  redo:
174062306a36Sopenharmony_ci		switch (opc) {
174162306a36Sopenharmony_ci		      case UNW_INSN_ADD:
174262306a36Sopenharmony_ci			s[dst] += val;
174362306a36Sopenharmony_ci			break;
174462306a36Sopenharmony_ci
174562306a36Sopenharmony_ci		      case UNW_INSN_MOVE2:
174662306a36Sopenharmony_ci			if (!s[val])
174762306a36Sopenharmony_ci				goto lazy_init;
174862306a36Sopenharmony_ci			s[dst+1] = s[val+1];
174962306a36Sopenharmony_ci			s[dst] = s[val];
175062306a36Sopenharmony_ci			break;
175162306a36Sopenharmony_ci
175262306a36Sopenharmony_ci		      case UNW_INSN_MOVE:
175362306a36Sopenharmony_ci			if (!s[val])
175462306a36Sopenharmony_ci				goto lazy_init;
175562306a36Sopenharmony_ci			s[dst] = s[val];
175662306a36Sopenharmony_ci			break;
175762306a36Sopenharmony_ci
175862306a36Sopenharmony_ci		      case UNW_INSN_MOVE_SCRATCH:
175962306a36Sopenharmony_ci			if (state->pt) {
176062306a36Sopenharmony_ci				s[dst] = (unsigned long) get_scratch_regs(state) + val;
176162306a36Sopenharmony_ci			} else {
176262306a36Sopenharmony_ci				s[dst] = 0;
176362306a36Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: no state->pt, dst=%ld, val=%ld\n",
176462306a36Sopenharmony_ci					   __func__, dst, val);
176562306a36Sopenharmony_ci			}
176662306a36Sopenharmony_ci			break;
176762306a36Sopenharmony_ci
176862306a36Sopenharmony_ci		      case UNW_INSN_MOVE_CONST:
176962306a36Sopenharmony_ci			if (val == 0)
177062306a36Sopenharmony_ci				s[dst] = (unsigned long) &unw.r0;
177162306a36Sopenharmony_ci			else {
177262306a36Sopenharmony_ci				s[dst] = 0;
177362306a36Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: UNW_INSN_MOVE_CONST bad val=%ld\n",
177462306a36Sopenharmony_ci					   __func__, val);
177562306a36Sopenharmony_ci			}
177662306a36Sopenharmony_ci			break;
177762306a36Sopenharmony_ci
177862306a36Sopenharmony_ci
177962306a36Sopenharmony_ci		      case UNW_INSN_MOVE_STACKED:
178062306a36Sopenharmony_ci			s[dst] = (unsigned long) ia64_rse_skip_regs((unsigned long *)state->bsp,
178162306a36Sopenharmony_ci								    val);
178262306a36Sopenharmony_ci			break;
178362306a36Sopenharmony_ci
178462306a36Sopenharmony_ci		      case UNW_INSN_ADD_PSP:
178562306a36Sopenharmony_ci			s[dst] = state->psp + val;
178662306a36Sopenharmony_ci			break;
178762306a36Sopenharmony_ci
178862306a36Sopenharmony_ci		      case UNW_INSN_ADD_SP:
178962306a36Sopenharmony_ci			s[dst] = state->sp + val;
179062306a36Sopenharmony_ci			break;
179162306a36Sopenharmony_ci
179262306a36Sopenharmony_ci		      case UNW_INSN_SETNAT_MEMSTK:
179362306a36Sopenharmony_ci			if (!state->pri_unat_loc)
179462306a36Sopenharmony_ci				state->pri_unat_loc = &state->sw->caller_unat;
179562306a36Sopenharmony_ci			/* register off. is a multiple of 8, so the least 3 bits (type) are 0 */
179662306a36Sopenharmony_ci			s[dst+1] = ((unsigned long) state->pri_unat_loc - s[dst]) | UNW_NAT_MEMSTK;
179762306a36Sopenharmony_ci			break;
179862306a36Sopenharmony_ci
179962306a36Sopenharmony_ci		      case UNW_INSN_SETNAT_TYPE:
180062306a36Sopenharmony_ci			s[dst+1] = val;
180162306a36Sopenharmony_ci			break;
180262306a36Sopenharmony_ci
180362306a36Sopenharmony_ci		      case UNW_INSN_LOAD:
180462306a36Sopenharmony_ci#ifdef UNW_DEBUG
180562306a36Sopenharmony_ci			if ((s[val] & (local_cpu_data->unimpl_va_mask | 0x7)) != 0
180662306a36Sopenharmony_ci			    || s[val] < TASK_SIZE)
180762306a36Sopenharmony_ci			{
180862306a36Sopenharmony_ci				UNW_DPRINT(0, "unwind.%s: rejecting bad psp=0x%lx\n",
180962306a36Sopenharmony_ci					   __func__, s[val]);
181062306a36Sopenharmony_ci				break;
181162306a36Sopenharmony_ci			}
181262306a36Sopenharmony_ci#endif
181362306a36Sopenharmony_ci			s[dst] = *(unsigned long *) s[val];
181462306a36Sopenharmony_ci			break;
181562306a36Sopenharmony_ci		}
181662306a36Sopenharmony_ci	}
181762306a36Sopenharmony_ci	STAT(unw.stat.script.run_time += ia64_get_itc() - start);
181862306a36Sopenharmony_ci	return;
181962306a36Sopenharmony_ci
182062306a36Sopenharmony_ci  lazy_init:
182162306a36Sopenharmony_ci	off = unw.sw_off[val];
182262306a36Sopenharmony_ci	s[val] = (unsigned long) state->sw + off;
182362306a36Sopenharmony_ci	if (off >= offsetof(struct switch_stack, r4) && off <= offsetof(struct switch_stack, r7))
182462306a36Sopenharmony_ci		/*
182562306a36Sopenharmony_ci		 * We're initializing a general register: init NaT info, too.  Note that
182662306a36Sopenharmony_ci		 * the offset is a multiple of 8 which gives us the 3 bits needed for
182762306a36Sopenharmony_ci		 * the type field.
182862306a36Sopenharmony_ci		 */
182962306a36Sopenharmony_ci		s[val+1] = (offsetof(struct switch_stack, ar_unat) - off) | UNW_NAT_MEMSTK;
183062306a36Sopenharmony_ci	goto redo;
183162306a36Sopenharmony_ci}
183262306a36Sopenharmony_ci
183362306a36Sopenharmony_cistatic int
183462306a36Sopenharmony_cifind_save_locs (struct unw_frame_info *info)
183562306a36Sopenharmony_ci{
183662306a36Sopenharmony_ci	int have_write_lock = 0;
183762306a36Sopenharmony_ci	struct unw_script *scr;
183862306a36Sopenharmony_ci	unsigned long flags = 0;
183962306a36Sopenharmony_ci
184062306a36Sopenharmony_ci	if ((info->ip & (local_cpu_data->unimpl_va_mask | 0xf)) || info->ip < TASK_SIZE) {
184162306a36Sopenharmony_ci		/* don't let obviously bad addresses pollute the cache */
184262306a36Sopenharmony_ci		/* FIXME: should really be level 0 but it occurs too often. KAO */
184362306a36Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: rejecting bad ip=0x%lx\n", __func__, info->ip);
184462306a36Sopenharmony_ci		info->rp_loc = NULL;
184562306a36Sopenharmony_ci		return -1;
184662306a36Sopenharmony_ci	}
184762306a36Sopenharmony_ci
184862306a36Sopenharmony_ci	scr = script_lookup(info);
184962306a36Sopenharmony_ci	if (!scr) {
185062306a36Sopenharmony_ci		spin_lock_irqsave(&unw.lock, flags);
185162306a36Sopenharmony_ci		scr = build_script(info);
185262306a36Sopenharmony_ci		if (!scr) {
185362306a36Sopenharmony_ci			spin_unlock_irqrestore(&unw.lock, flags);
185462306a36Sopenharmony_ci			UNW_DPRINT(0,
185562306a36Sopenharmony_ci				   "unwind.%s: failed to locate/build unwind script for ip %lx\n",
185662306a36Sopenharmony_ci				   __func__, info->ip);
185762306a36Sopenharmony_ci			return -1;
185862306a36Sopenharmony_ci		}
185962306a36Sopenharmony_ci		have_write_lock = 1;
186062306a36Sopenharmony_ci	}
186162306a36Sopenharmony_ci	info->hint = scr->hint;
186262306a36Sopenharmony_ci	info->prev_script = scr - unw.cache;
186362306a36Sopenharmony_ci
186462306a36Sopenharmony_ci	run_script(scr, info);
186562306a36Sopenharmony_ci
186662306a36Sopenharmony_ci	if (have_write_lock) {
186762306a36Sopenharmony_ci		write_unlock(&scr->lock);
186862306a36Sopenharmony_ci		spin_unlock_irqrestore(&unw.lock, flags);
186962306a36Sopenharmony_ci	} else
187062306a36Sopenharmony_ci		read_unlock(&scr->lock);
187162306a36Sopenharmony_ci	return 0;
187262306a36Sopenharmony_ci}
187362306a36Sopenharmony_ci
187462306a36Sopenharmony_cistatic int
187562306a36Sopenharmony_ciunw_valid(const struct unw_frame_info *info, unsigned long* p)
187662306a36Sopenharmony_ci{
187762306a36Sopenharmony_ci	unsigned long loc = (unsigned long)p;
187862306a36Sopenharmony_ci	return (loc >= info->regstk.limit && loc < info->regstk.top) ||
187962306a36Sopenharmony_ci	       (loc >= info->memstk.top && loc < info->memstk.limit);
188062306a36Sopenharmony_ci}
188162306a36Sopenharmony_ci
188262306a36Sopenharmony_ciint
188362306a36Sopenharmony_ciunw_unwind (struct unw_frame_info *info)
188462306a36Sopenharmony_ci{
188562306a36Sopenharmony_ci	unsigned long prev_ip, prev_sp, prev_bsp;
188662306a36Sopenharmony_ci	unsigned long ip, pr, num_regs;
188762306a36Sopenharmony_ci	STAT(unsigned long start, flags;)
188862306a36Sopenharmony_ci	int retval;
188962306a36Sopenharmony_ci
189062306a36Sopenharmony_ci	STAT(local_irq_save(flags); ++unw.stat.api.unwinds; start = ia64_get_itc());
189162306a36Sopenharmony_ci
189262306a36Sopenharmony_ci	prev_ip = info->ip;
189362306a36Sopenharmony_ci	prev_sp = info->sp;
189462306a36Sopenharmony_ci	prev_bsp = info->bsp;
189562306a36Sopenharmony_ci
189662306a36Sopenharmony_ci	/* validate the return IP pointer */
189762306a36Sopenharmony_ci	if (!unw_valid(info, info->rp_loc)) {
189862306a36Sopenharmony_ci		/* FIXME: should really be level 0 but it occurs too often. KAO */
189962306a36Sopenharmony_ci		UNW_DPRINT(1, "unwind.%s: failed to locate return link (ip=0x%lx)!\n",
190062306a36Sopenharmony_ci			   __func__, info->ip);
190162306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
190262306a36Sopenharmony_ci		return -1;
190362306a36Sopenharmony_ci	}
190462306a36Sopenharmony_ci	/* restore the ip */
190562306a36Sopenharmony_ci	ip = info->ip = *info->rp_loc;
190662306a36Sopenharmony_ci	if (ip < GATE_ADDR) {
190762306a36Sopenharmony_ci		UNW_DPRINT(2, "unwind.%s: reached user-space (ip=0x%lx)\n", __func__, ip);
190862306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
190962306a36Sopenharmony_ci		return -1;
191062306a36Sopenharmony_ci	}
191162306a36Sopenharmony_ci
191262306a36Sopenharmony_ci	/* validate the previous stack frame pointer */
191362306a36Sopenharmony_ci	if (!unw_valid(info, info->pfs_loc)) {
191462306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: failed to locate ar.pfs!\n", __func__);
191562306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
191662306a36Sopenharmony_ci		return -1;
191762306a36Sopenharmony_ci	}
191862306a36Sopenharmony_ci	/* restore the cfm: */
191962306a36Sopenharmony_ci	info->cfm_loc = info->pfs_loc;
192062306a36Sopenharmony_ci
192162306a36Sopenharmony_ci	/* restore the bsp: */
192262306a36Sopenharmony_ci	pr = info->pr;
192362306a36Sopenharmony_ci	num_regs = 0;
192462306a36Sopenharmony_ci	if ((info->flags & UNW_FLAG_INTERRUPT_FRAME)) {
192562306a36Sopenharmony_ci		info->pt = info->sp + 16;
192662306a36Sopenharmony_ci		if ((pr & (1UL << PRED_NON_SYSCALL)) != 0)
192762306a36Sopenharmony_ci			num_regs = *info->cfm_loc & 0x7f;		/* size of frame */
192862306a36Sopenharmony_ci		info->pfs_loc =
192962306a36Sopenharmony_ci			(unsigned long *) (info->pt + offsetof(struct pt_regs, ar_pfs));
193062306a36Sopenharmony_ci		UNW_DPRINT(3, "unwind.%s: interrupt_frame pt 0x%lx\n", __func__, info->pt);
193162306a36Sopenharmony_ci	} else
193262306a36Sopenharmony_ci		num_regs = (*info->cfm_loc >> 7) & 0x7f;	/* size of locals */
193362306a36Sopenharmony_ci	info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->bsp, -num_regs);
193462306a36Sopenharmony_ci	if (info->bsp < info->regstk.limit || info->bsp > info->regstk.top) {
193562306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: bsp (0x%lx) out of range [0x%lx-0x%lx]\n",
193662306a36Sopenharmony_ci			__func__, info->bsp, info->regstk.limit, info->regstk.top);
193762306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
193862306a36Sopenharmony_ci		return -1;
193962306a36Sopenharmony_ci	}
194062306a36Sopenharmony_ci
194162306a36Sopenharmony_ci	/* restore the sp: */
194262306a36Sopenharmony_ci	info->sp = info->psp;
194362306a36Sopenharmony_ci	if (info->sp < info->memstk.top || info->sp > info->memstk.limit) {
194462306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: sp (0x%lx) out of range [0x%lx-0x%lx]\n",
194562306a36Sopenharmony_ci			__func__, info->sp, info->memstk.top, info->memstk.limit);
194662306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
194762306a36Sopenharmony_ci		return -1;
194862306a36Sopenharmony_ci	}
194962306a36Sopenharmony_ci
195062306a36Sopenharmony_ci	if (info->ip == prev_ip && info->sp == prev_sp && info->bsp == prev_bsp) {
195162306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ip, sp, bsp unchanged; stopping here (ip=0x%lx)\n",
195262306a36Sopenharmony_ci			   __func__, ip);
195362306a36Sopenharmony_ci		STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
195462306a36Sopenharmony_ci		return -1;
195562306a36Sopenharmony_ci	}
195662306a36Sopenharmony_ci
195762306a36Sopenharmony_ci	/* as we unwind, the saved ar.unat becomes the primary unat: */
195862306a36Sopenharmony_ci	info->pri_unat_loc = info->unat_loc;
195962306a36Sopenharmony_ci
196062306a36Sopenharmony_ci	/* finally, restore the predicates: */
196162306a36Sopenharmony_ci	unw_get_pr(info, &info->pr);
196262306a36Sopenharmony_ci
196362306a36Sopenharmony_ci	retval = find_save_locs(info);
196462306a36Sopenharmony_ci	STAT(unw.stat.api.unwind_time += ia64_get_itc() - start; local_irq_restore(flags));
196562306a36Sopenharmony_ci	return retval;
196662306a36Sopenharmony_ci}
196762306a36Sopenharmony_ciEXPORT_SYMBOL(unw_unwind);
196862306a36Sopenharmony_ci
196962306a36Sopenharmony_ciint
197062306a36Sopenharmony_ciunw_unwind_to_user (struct unw_frame_info *info)
197162306a36Sopenharmony_ci{
197262306a36Sopenharmony_ci	unsigned long ip, sp, pr = info->pr;
197362306a36Sopenharmony_ci
197462306a36Sopenharmony_ci	do {
197562306a36Sopenharmony_ci		unw_get_sp(info, &sp);
197662306a36Sopenharmony_ci		if ((long)((unsigned long)info->task + IA64_STK_OFFSET - sp)
197762306a36Sopenharmony_ci		    < IA64_PT_REGS_SIZE) {
197862306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: ran off the top of the kernel stack\n",
197962306a36Sopenharmony_ci				   __func__);
198062306a36Sopenharmony_ci			break;
198162306a36Sopenharmony_ci		}
198262306a36Sopenharmony_ci		if (unw_is_intr_frame(info) &&
198362306a36Sopenharmony_ci		    (pr & (1UL << PRED_USER_STACK)))
198462306a36Sopenharmony_ci			return 0;
198562306a36Sopenharmony_ci		if (unw_get_pr (info, &pr) < 0) {
198662306a36Sopenharmony_ci			unw_get_rp(info, &ip);
198762306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: failed to read "
198862306a36Sopenharmony_ci				   "predicate register (ip=0x%lx)\n",
198962306a36Sopenharmony_ci				__func__, ip);
199062306a36Sopenharmony_ci			return -1;
199162306a36Sopenharmony_ci		}
199262306a36Sopenharmony_ci	} while (unw_unwind(info) >= 0);
199362306a36Sopenharmony_ci	unw_get_ip(info, &ip);
199462306a36Sopenharmony_ci	UNW_DPRINT(0, "unwind.%s: failed to unwind to user-level (ip=0x%lx)\n",
199562306a36Sopenharmony_ci		   __func__, ip);
199662306a36Sopenharmony_ci	return -1;
199762306a36Sopenharmony_ci}
199862306a36Sopenharmony_ciEXPORT_SYMBOL(unw_unwind_to_user);
199962306a36Sopenharmony_ci
200062306a36Sopenharmony_cistatic void
200162306a36Sopenharmony_ciinit_frame_info (struct unw_frame_info *info, struct task_struct *t,
200262306a36Sopenharmony_ci		 struct switch_stack *sw, unsigned long stktop)
200362306a36Sopenharmony_ci{
200462306a36Sopenharmony_ci	unsigned long rbslimit, rbstop, stklimit;
200562306a36Sopenharmony_ci	STAT(unsigned long start, flags;)
200662306a36Sopenharmony_ci
200762306a36Sopenharmony_ci	STAT(local_irq_save(flags); ++unw.stat.api.inits; start = ia64_get_itc());
200862306a36Sopenharmony_ci
200962306a36Sopenharmony_ci	/*
201062306a36Sopenharmony_ci	 * Subtle stuff here: we _could_ unwind through the switch_stack frame but we
201162306a36Sopenharmony_ci	 * don't want to do that because it would be slow as each preserved register would
201262306a36Sopenharmony_ci	 * have to be processed.  Instead, what we do here is zero out the frame info and
201362306a36Sopenharmony_ci	 * start the unwind process at the function that created the switch_stack frame.
201462306a36Sopenharmony_ci	 * When a preserved value in switch_stack needs to be accessed, run_script() will
201562306a36Sopenharmony_ci	 * initialize the appropriate pointer on demand.
201662306a36Sopenharmony_ci	 */
201762306a36Sopenharmony_ci	memset(info, 0, sizeof(*info));
201862306a36Sopenharmony_ci
201962306a36Sopenharmony_ci	rbslimit = (unsigned long) t + IA64_RBS_OFFSET;
202062306a36Sopenharmony_ci	stklimit = (unsigned long) t + IA64_STK_OFFSET;
202162306a36Sopenharmony_ci
202262306a36Sopenharmony_ci	rbstop   = sw->ar_bspstore;
202362306a36Sopenharmony_ci	if (rbstop > stklimit || rbstop < rbslimit)
202462306a36Sopenharmony_ci		rbstop = rbslimit;
202562306a36Sopenharmony_ci
202662306a36Sopenharmony_ci	if (stktop <= rbstop)
202762306a36Sopenharmony_ci		stktop = rbstop;
202862306a36Sopenharmony_ci	if (stktop > stklimit)
202962306a36Sopenharmony_ci		stktop = stklimit;
203062306a36Sopenharmony_ci
203162306a36Sopenharmony_ci	info->regstk.limit = rbslimit;
203262306a36Sopenharmony_ci	info->regstk.top   = rbstop;
203362306a36Sopenharmony_ci	info->memstk.limit = stklimit;
203462306a36Sopenharmony_ci	info->memstk.top   = stktop;
203562306a36Sopenharmony_ci	info->task = t;
203662306a36Sopenharmony_ci	info->sw  = sw;
203762306a36Sopenharmony_ci	info->sp = info->psp = stktop;
203862306a36Sopenharmony_ci	info->pr = sw->pr;
203962306a36Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s:\n"
204062306a36Sopenharmony_ci		   "  task   0x%lx\n"
204162306a36Sopenharmony_ci		   "  rbs = [0x%lx-0x%lx)\n"
204262306a36Sopenharmony_ci		   "  stk = [0x%lx-0x%lx)\n"
204362306a36Sopenharmony_ci		   "  pr     0x%lx\n"
204462306a36Sopenharmony_ci		   "  sw     0x%lx\n"
204562306a36Sopenharmony_ci		   "  sp     0x%lx\n",
204662306a36Sopenharmony_ci		   __func__, (unsigned long) t, rbslimit, rbstop, stktop, stklimit,
204762306a36Sopenharmony_ci		   info->pr, (unsigned long) info->sw, info->sp);
204862306a36Sopenharmony_ci	STAT(unw.stat.api.init_time += ia64_get_itc() - start; local_irq_restore(flags));
204962306a36Sopenharmony_ci}
205062306a36Sopenharmony_ci
205162306a36Sopenharmony_civoid
205262306a36Sopenharmony_ciunw_init_frame_info (struct unw_frame_info *info, struct task_struct *t, struct switch_stack *sw)
205362306a36Sopenharmony_ci{
205462306a36Sopenharmony_ci	unsigned long sol;
205562306a36Sopenharmony_ci
205662306a36Sopenharmony_ci	init_frame_info(info, t, sw, (unsigned long) (sw + 1) - 16);
205762306a36Sopenharmony_ci	info->cfm_loc = &sw->ar_pfs;
205862306a36Sopenharmony_ci	sol = (*info->cfm_loc >> 7) & 0x7f;
205962306a36Sopenharmony_ci	info->bsp = (unsigned long) ia64_rse_skip_regs((unsigned long *) info->regstk.top, -sol);
206062306a36Sopenharmony_ci	info->ip = sw->b0;
206162306a36Sopenharmony_ci	UNW_DPRINT(3, "unwind.%s:\n"
206262306a36Sopenharmony_ci		   "  bsp    0x%lx\n"
206362306a36Sopenharmony_ci		   "  sol    0x%lx\n"
206462306a36Sopenharmony_ci		   "  ip     0x%lx\n",
206562306a36Sopenharmony_ci		   __func__, info->bsp, sol, info->ip);
206662306a36Sopenharmony_ci	find_save_locs(info);
206762306a36Sopenharmony_ci}
206862306a36Sopenharmony_ci
206962306a36Sopenharmony_ciEXPORT_SYMBOL(unw_init_frame_info);
207062306a36Sopenharmony_ci
207162306a36Sopenharmony_civoid
207262306a36Sopenharmony_ciunw_init_from_blocked_task (struct unw_frame_info *info, struct task_struct *t)
207362306a36Sopenharmony_ci{
207462306a36Sopenharmony_ci	struct switch_stack *sw = (struct switch_stack *) (t->thread.ksp + 16);
207562306a36Sopenharmony_ci
207662306a36Sopenharmony_ci	UNW_DPRINT(1, "unwind.%s\n", __func__);
207762306a36Sopenharmony_ci	unw_init_frame_info(info, t, sw);
207862306a36Sopenharmony_ci}
207962306a36Sopenharmony_ciEXPORT_SYMBOL(unw_init_from_blocked_task);
208062306a36Sopenharmony_ci
208162306a36Sopenharmony_cistatic void
208262306a36Sopenharmony_ciinit_unwind_table (struct unw_table *table, const char *name, unsigned long segment_base,
208362306a36Sopenharmony_ci		   unsigned long gp, const void *table_start, const void *table_end)
208462306a36Sopenharmony_ci{
208562306a36Sopenharmony_ci	const struct unw_table_entry *start = table_start, *end = table_end;
208662306a36Sopenharmony_ci
208762306a36Sopenharmony_ci	table->name = name;
208862306a36Sopenharmony_ci	table->segment_base = segment_base;
208962306a36Sopenharmony_ci	table->gp = gp;
209062306a36Sopenharmony_ci	table->start = segment_base + start[0].start_offset;
209162306a36Sopenharmony_ci	table->end = segment_base + end[-1].end_offset;
209262306a36Sopenharmony_ci	table->array = start;
209362306a36Sopenharmony_ci	table->length = end - start;
209462306a36Sopenharmony_ci}
209562306a36Sopenharmony_ci
209662306a36Sopenharmony_civoid *
209762306a36Sopenharmony_ciunw_add_unwind_table (const char *name, unsigned long segment_base, unsigned long gp,
209862306a36Sopenharmony_ci		      const void *table_start, const void *table_end)
209962306a36Sopenharmony_ci{
210062306a36Sopenharmony_ci	const struct unw_table_entry *start = table_start, *end = table_end;
210162306a36Sopenharmony_ci	struct unw_table *table;
210262306a36Sopenharmony_ci	unsigned long flags;
210362306a36Sopenharmony_ci
210462306a36Sopenharmony_ci	if (end - start <= 0) {
210562306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ignoring attempt to insert empty unwind table\n",
210662306a36Sopenharmony_ci			   __func__);
210762306a36Sopenharmony_ci		return NULL;
210862306a36Sopenharmony_ci	}
210962306a36Sopenharmony_ci
211062306a36Sopenharmony_ci	table = kmalloc(sizeof(*table), GFP_USER);
211162306a36Sopenharmony_ci	if (!table)
211262306a36Sopenharmony_ci		return NULL;
211362306a36Sopenharmony_ci
211462306a36Sopenharmony_ci	init_unwind_table(table, name, segment_base, gp, table_start, table_end);
211562306a36Sopenharmony_ci
211662306a36Sopenharmony_ci	spin_lock_irqsave(&unw.lock, flags);
211762306a36Sopenharmony_ci	{
211862306a36Sopenharmony_ci		/* keep kernel unwind table at the front (it's searched most commonly): */
211962306a36Sopenharmony_ci		table->next = unw.tables->next;
212062306a36Sopenharmony_ci		unw.tables->next = table;
212162306a36Sopenharmony_ci	}
212262306a36Sopenharmony_ci	spin_unlock_irqrestore(&unw.lock, flags);
212362306a36Sopenharmony_ci
212462306a36Sopenharmony_ci	return table;
212562306a36Sopenharmony_ci}
212662306a36Sopenharmony_ci
212762306a36Sopenharmony_civoid
212862306a36Sopenharmony_ciunw_remove_unwind_table (void *handle)
212962306a36Sopenharmony_ci{
213062306a36Sopenharmony_ci	struct unw_table *table, *prev;
213162306a36Sopenharmony_ci	struct unw_script *tmp;
213262306a36Sopenharmony_ci	unsigned long flags;
213362306a36Sopenharmony_ci	long index;
213462306a36Sopenharmony_ci
213562306a36Sopenharmony_ci	if (!handle) {
213662306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: ignoring attempt to remove non-existent unwind table\n",
213762306a36Sopenharmony_ci			   __func__);
213862306a36Sopenharmony_ci		return;
213962306a36Sopenharmony_ci	}
214062306a36Sopenharmony_ci
214162306a36Sopenharmony_ci	table = handle;
214262306a36Sopenharmony_ci	if (table == &unw.kernel_table) {
214362306a36Sopenharmony_ci		UNW_DPRINT(0, "unwind.%s: sorry, freeing the kernel's unwind table is a "
214462306a36Sopenharmony_ci			   "no-can-do!\n", __func__);
214562306a36Sopenharmony_ci		return;
214662306a36Sopenharmony_ci	}
214762306a36Sopenharmony_ci
214862306a36Sopenharmony_ci	spin_lock_irqsave(&unw.lock, flags);
214962306a36Sopenharmony_ci	{
215062306a36Sopenharmony_ci		/* first, delete the table: */
215162306a36Sopenharmony_ci
215262306a36Sopenharmony_ci		for (prev = (struct unw_table *) &unw.tables; prev; prev = prev->next)
215362306a36Sopenharmony_ci			if (prev->next == table)
215462306a36Sopenharmony_ci				break;
215562306a36Sopenharmony_ci		if (!prev) {
215662306a36Sopenharmony_ci			UNW_DPRINT(0, "unwind.%s: failed to find unwind table %p\n",
215762306a36Sopenharmony_ci				   __func__, (void *) table);
215862306a36Sopenharmony_ci			spin_unlock_irqrestore(&unw.lock, flags);
215962306a36Sopenharmony_ci			return;
216062306a36Sopenharmony_ci		}
216162306a36Sopenharmony_ci		prev->next = table->next;
216262306a36Sopenharmony_ci	}
216362306a36Sopenharmony_ci	spin_unlock_irqrestore(&unw.lock, flags);
216462306a36Sopenharmony_ci
216562306a36Sopenharmony_ci	/* next, remove hash table entries for this table */
216662306a36Sopenharmony_ci
216762306a36Sopenharmony_ci	for (index = 0; index < UNW_HASH_SIZE; ++index) {
216862306a36Sopenharmony_ci		tmp = unw.cache + unw.hash[index];
216962306a36Sopenharmony_ci		if (unw.hash[index] >= UNW_CACHE_SIZE
217062306a36Sopenharmony_ci		    || tmp->ip < table->start || tmp->ip >= table->end)
217162306a36Sopenharmony_ci			continue;
217262306a36Sopenharmony_ci
217362306a36Sopenharmony_ci		write_lock(&tmp->lock);
217462306a36Sopenharmony_ci		{
217562306a36Sopenharmony_ci			if (tmp->ip >= table->start && tmp->ip < table->end) {
217662306a36Sopenharmony_ci				unw.hash[index] = tmp->coll_chain;
217762306a36Sopenharmony_ci				tmp->ip = 0;
217862306a36Sopenharmony_ci			}
217962306a36Sopenharmony_ci		}
218062306a36Sopenharmony_ci		write_unlock(&tmp->lock);
218162306a36Sopenharmony_ci	}
218262306a36Sopenharmony_ci
218362306a36Sopenharmony_ci	kfree(table);
218462306a36Sopenharmony_ci}
218562306a36Sopenharmony_ci
218662306a36Sopenharmony_cistatic int __init
218762306a36Sopenharmony_cicreate_gate_table (void)
218862306a36Sopenharmony_ci{
218962306a36Sopenharmony_ci	const struct unw_table_entry *entry, *start, *end;
219062306a36Sopenharmony_ci	unsigned long *lp, segbase = GATE_ADDR;
219162306a36Sopenharmony_ci	size_t info_size, size;
219262306a36Sopenharmony_ci	char *info;
219362306a36Sopenharmony_ci	Elf64_Phdr *punw = NULL, *phdr = (Elf64_Phdr *) (GATE_ADDR + GATE_EHDR->e_phoff);
219462306a36Sopenharmony_ci	int i;
219562306a36Sopenharmony_ci
219662306a36Sopenharmony_ci	for (i = 0; i < GATE_EHDR->e_phnum; ++i, ++phdr)
219762306a36Sopenharmony_ci		if (phdr->p_type == PT_IA_64_UNWIND) {
219862306a36Sopenharmony_ci			punw = phdr;
219962306a36Sopenharmony_ci			break;
220062306a36Sopenharmony_ci		}
220162306a36Sopenharmony_ci
220262306a36Sopenharmony_ci	if (!punw) {
220362306a36Sopenharmony_ci		printk("%s: failed to find gate DSO's unwind table!\n", __func__);
220462306a36Sopenharmony_ci		return 0;
220562306a36Sopenharmony_ci	}
220662306a36Sopenharmony_ci
220762306a36Sopenharmony_ci	start = (const struct unw_table_entry *) punw->p_vaddr;
220862306a36Sopenharmony_ci	end = (struct unw_table_entry *) ((char *) start + punw->p_memsz);
220962306a36Sopenharmony_ci	size  = 0;
221062306a36Sopenharmony_ci
221162306a36Sopenharmony_ci	unw_add_unwind_table("linux-gate.so", segbase, 0, start, end);
221262306a36Sopenharmony_ci
221362306a36Sopenharmony_ci	for (entry = start; entry < end; ++entry)
221462306a36Sopenharmony_ci		size += 3*8 + 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
221562306a36Sopenharmony_ci	size += 8;	/* reserve space for "end of table" marker */
221662306a36Sopenharmony_ci
221762306a36Sopenharmony_ci	unw.gate_table = kmalloc(size, GFP_KERNEL);
221862306a36Sopenharmony_ci	if (!unw.gate_table) {
221962306a36Sopenharmony_ci		unw.gate_table_size = 0;
222062306a36Sopenharmony_ci		printk(KERN_ERR "%s: unable to create unwind data for gate page!\n", __func__);
222162306a36Sopenharmony_ci		return 0;
222262306a36Sopenharmony_ci	}
222362306a36Sopenharmony_ci	unw.gate_table_size = size;
222462306a36Sopenharmony_ci
222562306a36Sopenharmony_ci	lp = unw.gate_table;
222662306a36Sopenharmony_ci	info = (char *) unw.gate_table + size;
222762306a36Sopenharmony_ci
222862306a36Sopenharmony_ci	for (entry = start; entry < end; ++entry, lp += 3) {
222962306a36Sopenharmony_ci		info_size = 8 + 8*UNW_LENGTH(*(u64 *) (segbase + entry->info_offset));
223062306a36Sopenharmony_ci		info -= info_size;
223162306a36Sopenharmony_ci		memcpy(info, (char *) segbase + entry->info_offset, info_size);
223262306a36Sopenharmony_ci
223362306a36Sopenharmony_ci		lp[0] = segbase + entry->start_offset;		/* start */
223462306a36Sopenharmony_ci		lp[1] = segbase + entry->end_offset;		/* end */
223562306a36Sopenharmony_ci		lp[2] = info - (char *) unw.gate_table;		/* info */
223662306a36Sopenharmony_ci	}
223762306a36Sopenharmony_ci	*lp = 0;	/* end-of-table marker */
223862306a36Sopenharmony_ci	return 0;
223962306a36Sopenharmony_ci}
224062306a36Sopenharmony_ci
224162306a36Sopenharmony_ci__initcall(create_gate_table);
224262306a36Sopenharmony_ci
224362306a36Sopenharmony_civoid __init
224462306a36Sopenharmony_ciunw_init (void)
224562306a36Sopenharmony_ci{
224662306a36Sopenharmony_ci	extern char __gp[];
224762306a36Sopenharmony_ci	extern void unw_hash_index_t_is_too_narrow (void);
224862306a36Sopenharmony_ci	long i, off;
224962306a36Sopenharmony_ci
225062306a36Sopenharmony_ci	if (8*sizeof(unw_hash_index_t) < UNW_LOG_HASH_SIZE)
225162306a36Sopenharmony_ci		unw_hash_index_t_is_too_narrow();
225262306a36Sopenharmony_ci
225362306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PRI_UNAT_GR]] = SW(CALLER_UNAT);
225462306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_BSPSTORE]] = SW(AR_BSPSTORE);
225562306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PFS]] = SW(AR_PFS);
225662306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_RP]] = SW(B0);
225762306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_UNAT]] = SW(CALLER_UNAT);
225862306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_PR]] = SW(PR);
225962306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_LC]] = SW(AR_LC);
226062306a36Sopenharmony_ci	unw.sw_off[unw.preg_index[UNW_REG_FPSR]] = SW(AR_FPSR);
226162306a36Sopenharmony_ci	for (i = UNW_REG_R4, off = SW(R4); i <= UNW_REG_R7; ++i, off += 8)
226262306a36Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
226362306a36Sopenharmony_ci	for (i = UNW_REG_B1, off = SW(B1); i <= UNW_REG_B5; ++i, off += 8)
226462306a36Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
226562306a36Sopenharmony_ci	for (i = UNW_REG_F2, off = SW(F2); i <= UNW_REG_F5; ++i, off += 16)
226662306a36Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
226762306a36Sopenharmony_ci	for (i = UNW_REG_F16, off = SW(F16); i <= UNW_REG_F31; ++i, off += 16)
226862306a36Sopenharmony_ci		unw.sw_off[unw.preg_index[i]] = off;
226962306a36Sopenharmony_ci
227062306a36Sopenharmony_ci	for (i = 0; i < UNW_CACHE_SIZE; ++i) {
227162306a36Sopenharmony_ci		if (i > 0)
227262306a36Sopenharmony_ci			unw.cache[i].lru_chain = (i - 1);
227362306a36Sopenharmony_ci		unw.cache[i].coll_chain = -1;
227462306a36Sopenharmony_ci		rwlock_init(&unw.cache[i].lock);
227562306a36Sopenharmony_ci	}
227662306a36Sopenharmony_ci	unw.lru_head = UNW_CACHE_SIZE - 1;
227762306a36Sopenharmony_ci	unw.lru_tail = 0;
227862306a36Sopenharmony_ci
227962306a36Sopenharmony_ci	init_unwind_table(&unw.kernel_table, "kernel", KERNEL_START, (unsigned long) __gp,
228062306a36Sopenharmony_ci			  __start_unwind, __end_unwind);
228162306a36Sopenharmony_ci}
228262306a36Sopenharmony_ci
228362306a36Sopenharmony_ci/*
228462306a36Sopenharmony_ci * DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED
228562306a36Sopenharmony_ci *
228662306a36Sopenharmony_ci *	This system call has been deprecated.  The new and improved way to get
228762306a36Sopenharmony_ci *	at the kernel's unwind info is via the gate DSO.  The address of the
228862306a36Sopenharmony_ci *	ELF header for this DSO is passed to user-level via AT_SYSINFO_EHDR.
228962306a36Sopenharmony_ci *
229062306a36Sopenharmony_ci * DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED DEPRECATED
229162306a36Sopenharmony_ci *
229262306a36Sopenharmony_ci * This system call copies the unwind data into the buffer pointed to by BUF and returns
229362306a36Sopenharmony_ci * the size of the unwind data.  If BUF_SIZE is smaller than the size of the unwind data
229462306a36Sopenharmony_ci * or if BUF is NULL, nothing is copied, but the system call still returns the size of the
229562306a36Sopenharmony_ci * unwind data.
229662306a36Sopenharmony_ci *
229762306a36Sopenharmony_ci * The first portion of the unwind data contains an unwind table and rest contains the
229862306a36Sopenharmony_ci * associated unwind info (in no particular order).  The unwind table consists of a table
229962306a36Sopenharmony_ci * of entries of the form:
230062306a36Sopenharmony_ci *
230162306a36Sopenharmony_ci *	u64 start;	(64-bit address of start of function)
230262306a36Sopenharmony_ci *	u64 end;	(64-bit address of start of function)
230362306a36Sopenharmony_ci *	u64 info;	(BUF-relative offset to unwind info)
230462306a36Sopenharmony_ci *
230562306a36Sopenharmony_ci * The end of the unwind table is indicated by an entry with a START address of zero.
230662306a36Sopenharmony_ci *
230762306a36Sopenharmony_ci * Please see the IA-64 Software Conventions and Runtime Architecture manual for details
230862306a36Sopenharmony_ci * on the format of the unwind info.
230962306a36Sopenharmony_ci *
231062306a36Sopenharmony_ci * ERRORS
231162306a36Sopenharmony_ci *	EFAULT	BUF points outside your accessible address space.
231262306a36Sopenharmony_ci */
231362306a36Sopenharmony_ciasmlinkage long
231462306a36Sopenharmony_cisys_getunwind (void __user *buf, size_t buf_size)
231562306a36Sopenharmony_ci{
231662306a36Sopenharmony_ci	if (buf && buf_size >= unw.gate_table_size)
231762306a36Sopenharmony_ci		if (copy_to_user(buf, unw.gate_table, unw.gate_table_size) != 0)
231862306a36Sopenharmony_ci			return -EFAULT;
231962306a36Sopenharmony_ci	return unw.gate_table_size;
232062306a36Sopenharmony_ci}
2321