Linux Audio

Check our new training course

Loading...
v4.10.11
 
   1/*
   2 * Copyright (C) 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
   3 * Copyright (C) 2002-2006 Novell, Inc.
   4 *	Jan Beulich <jbeulich@novell.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 * A simple API for unwinding kernel stacks.  This is used for
  11 * debugging and error reporting purposes.  The kernel doesn't need
  12 * full-blown stack unwinding with all the bells and whistles, so there
  13 * is not much point in implementing the full Dwarf2 unwind API.
  14 */
  15
  16#include <linux/sched.h>
  17#include <linux/module.h>
  18#include <linux/bootmem.h>
  19#include <linux/sort.h>
  20#include <linux/slab.h>
  21#include <linux/stop_machine.h>
  22#include <linux/uaccess.h>
  23#include <linux/ptrace.h>
  24#include <asm/sections.h>
  25#include <asm/unaligned.h>
  26#include <asm/unwind.h>
  27
  28extern char __start_unwind[], __end_unwind[];
  29/* extern const u8 __start_unwind_hdr[], __end_unwind_hdr[];*/
  30
  31/* #define UNWIND_DEBUG */
  32
  33#ifdef UNWIND_DEBUG
  34int dbg_unw;
  35#define unw_debug(fmt, ...)			\
  36do {						\
  37	if (dbg_unw)				\
  38		pr_info(fmt, ##__VA_ARGS__);	\
  39} while (0);
  40#else
  41#define unw_debug(fmt, ...)
  42#endif
  43
  44#define MAX_STACK_DEPTH 8
  45
  46#define EXTRA_INFO(f) { \
  47		BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \
  48				% FIELD_SIZEOF(struct unwind_frame_info, f)) \
  49				+ offsetof(struct unwind_frame_info, f) \
  50				/ FIELD_SIZEOF(struct unwind_frame_info, f), \
  51				FIELD_SIZEOF(struct unwind_frame_info, f) \
  52	}
  53#define PTREGS_INFO(f) EXTRA_INFO(regs.f)
  54
  55static const struct {
  56	unsigned offs:BITS_PER_LONG / 2;
  57	unsigned width:BITS_PER_LONG / 2;
  58} reg_info[] = {
  59UNW_REGISTER_INFO};
  60
  61#undef PTREGS_INFO
  62#undef EXTRA_INFO
  63
  64#ifndef REG_INVALID
  65#define REG_INVALID(r) (reg_info[r].width == 0)
  66#endif
  67
  68#define DW_CFA_nop                          0x00
  69#define DW_CFA_set_loc                      0x01
  70#define DW_CFA_advance_loc1                 0x02
  71#define DW_CFA_advance_loc2                 0x03
  72#define DW_CFA_advance_loc4                 0x04
  73#define DW_CFA_offset_extended              0x05
  74#define DW_CFA_restore_extended             0x06
  75#define DW_CFA_undefined                    0x07
  76#define DW_CFA_same_value                   0x08
  77#define DW_CFA_register                     0x09
  78#define DW_CFA_remember_state               0x0a
  79#define DW_CFA_restore_state                0x0b
  80#define DW_CFA_def_cfa                      0x0c
  81#define DW_CFA_def_cfa_register             0x0d
  82#define DW_CFA_def_cfa_offset               0x0e
  83#define DW_CFA_def_cfa_expression           0x0f
  84#define DW_CFA_expression                   0x10
  85#define DW_CFA_offset_extended_sf           0x11
  86#define DW_CFA_def_cfa_sf                   0x12
  87#define DW_CFA_def_cfa_offset_sf            0x13
  88#define DW_CFA_val_offset                   0x14
  89#define DW_CFA_val_offset_sf                0x15
  90#define DW_CFA_val_expression               0x16
  91#define DW_CFA_lo_user                      0x1c
  92#define DW_CFA_GNU_window_save              0x2d
  93#define DW_CFA_GNU_args_size                0x2e
  94#define DW_CFA_GNU_negative_offset_extended 0x2f
  95#define DW_CFA_hi_user                      0x3f
  96
  97#define DW_EH_PE_FORM     0x07
  98#define DW_EH_PE_native   0x00
  99#define DW_EH_PE_leb128   0x01
 100#define DW_EH_PE_data2    0x02
 101#define DW_EH_PE_data4    0x03
 102#define DW_EH_PE_data8    0x04
 103#define DW_EH_PE_signed   0x08
 104#define DW_EH_PE_ADJUST   0x70
 105#define DW_EH_PE_abs      0x00
 106#define DW_EH_PE_pcrel    0x10
 107#define DW_EH_PE_textrel  0x20
 108#define DW_EH_PE_datarel  0x30
 109#define DW_EH_PE_funcrel  0x40
 110#define DW_EH_PE_aligned  0x50
 111#define DW_EH_PE_indirect 0x80
 112#define DW_EH_PE_omit     0xff
 113
 114#define CIE_ID	0
 115
 116typedef unsigned long uleb128_t;
 117typedef signed long sleb128_t;
 118
 119static struct unwind_table {
 120	struct {
 121		unsigned long pc;
 122		unsigned long range;
 123	} core, init;
 124	const void *address;
 125	unsigned long size;
 126	const unsigned char *header;
 127	unsigned long hdrsz;
 128	struct unwind_table *link;
 129	const char *name;
 130} root_table;
 131
 132struct unwind_item {
 133	enum item_location {
 134		Nowhere,
 135		Memory,
 136		Register,
 137		Value
 138	} where;
 139	uleb128_t value;
 140};
 141
 142struct unwind_state {
 143	uleb128_t loc, org;
 144	const u8 *cieStart, *cieEnd;
 145	uleb128_t codeAlign;
 146	sleb128_t dataAlign;
 147	struct cfa {
 148		uleb128_t reg, offs;
 149	} cfa;
 150	struct unwind_item regs[ARRAY_SIZE(reg_info)];
 151	unsigned stackDepth:8;
 152	unsigned version:8;
 153	const u8 *label;
 154	const u8 *stack[MAX_STACK_DEPTH];
 155};
 156
 157static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
 158
 159static struct unwind_table *find_table(unsigned long pc)
 160{
 161	struct unwind_table *table;
 162
 163	for (table = &root_table; table; table = table->link)
 164		if ((pc >= table->core.pc
 165		     && pc < table->core.pc + table->core.range)
 166		    || (pc >= table->init.pc
 167			&& pc < table->init.pc + table->init.range))
 168			break;
 169
 170	return table;
 171}
 172
 173static unsigned long read_pointer(const u8 **pLoc,
 174				  const void *end, signed ptrType);
 175static void init_unwind_hdr(struct unwind_table *table,
 176			    void *(*alloc) (unsigned long));
 177
 178/*
 179 * wrappers for header alloc (vs. calling one vs. other at call site)
 180 * to elide section mismatches warnings
 181 */
 182static void *__init unw_hdr_alloc_early(unsigned long sz)
 183{
 184	return __alloc_bootmem_nopanic(sz, sizeof(unsigned int),
 185				       MAX_DMA_ADDRESS);
 186}
 187
 188static void *unw_hdr_alloc(unsigned long sz)
 189{
 190	return kmalloc(sz, GFP_KERNEL);
 191}
 192
 193static void init_unwind_table(struct unwind_table *table, const char *name,
 194			      const void *core_start, unsigned long core_size,
 195			      const void *init_start, unsigned long init_size,
 196			      const void *table_start, unsigned long table_size,
 197			      const u8 *header_start, unsigned long header_size)
 198{
 199	const u8 *ptr = header_start + 4;
 200	const u8 *end = header_start + header_size;
 201
 202	table->core.pc = (unsigned long)core_start;
 203	table->core.range = core_size;
 204	table->init.pc = (unsigned long)init_start;
 205	table->init.range = init_size;
 206	table->address = table_start;
 207	table->size = table_size;
 208
 209	/* See if the linker provided table looks valid. */
 210	if (header_size <= 4
 211	    || header_start[0] != 1
 212	    || (void *)read_pointer(&ptr, end, header_start[1]) != table_start
 213	    || header_start[2] == DW_EH_PE_omit
 214	    || read_pointer(&ptr, end, header_start[2]) <= 0
 215	    || header_start[3] == DW_EH_PE_omit)
 216		header_start = NULL;
 217
 
 
 
 
 218	table->hdrsz = header_size;
 219	smp_wmb();
 220	table->header = header_start;
 221	table->link = NULL;
 222	table->name = name;
 223}
 224
 225void __init arc_unwind_init(void)
 226{
 227	init_unwind_table(&root_table, "kernel", _text, _end - _text, NULL, 0,
 228			  __start_unwind, __end_unwind - __start_unwind,
 229			  NULL, 0);
 230	  /*__start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);*/
 231
 232	init_unwind_hdr(&root_table, unw_hdr_alloc_early);
 233}
 234
 235static const u32 bad_cie, not_fde;
 236static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *);
 237static const u32 *__cie_for_fde(const u32 *fde);
 238static signed fde_pointer_type(const u32 *cie);
 239
 240struct eh_frame_hdr_table_entry {
 241	unsigned long start, fde;
 242};
 243
 244static int cmp_eh_frame_hdr_table_entries(const void *p1, const void *p2)
 245{
 246	const struct eh_frame_hdr_table_entry *e1 = p1;
 247	const struct eh_frame_hdr_table_entry *e2 = p2;
 248
 249	return (e1->start > e2->start) - (e1->start < e2->start);
 250}
 251
 252static void swap_eh_frame_hdr_table_entries(void *p1, void *p2, int size)
 253{
 254	struct eh_frame_hdr_table_entry *e1 = p1;
 255	struct eh_frame_hdr_table_entry *e2 = p2;
 256	unsigned long v;
 257
 258	v = e1->start;
 259	e1->start = e2->start;
 260	e2->start = v;
 261	v = e1->fde;
 262	e1->fde = e2->fde;
 263	e2->fde = v;
 264}
 265
 266static void init_unwind_hdr(struct unwind_table *table,
 267			    void *(*alloc) (unsigned long))
 268{
 269	const u8 *ptr;
 270	unsigned long tableSize = table->size, hdrSize;
 271	unsigned n;
 272	const u32 *fde;
 273	struct {
 274		u8 version;
 275		u8 eh_frame_ptr_enc;
 276		u8 fde_count_enc;
 277		u8 table_enc;
 278		unsigned long eh_frame_ptr;
 279		unsigned int fde_count;
 280		struct eh_frame_hdr_table_entry table[];
 281	} __attribute__ ((__packed__)) *header;
 282
 283	if (table->header)
 284		return;
 285
 286	if (table->hdrsz)
 287		pr_warn(".eh_frame_hdr for '%s' present but unusable\n",
 288			table->name);
 289
 290	if (tableSize & (sizeof(*fde) - 1))
 291		return;
 292
 293	for (fde = table->address, n = 0;
 294	     tableSize > sizeof(*fde) && tableSize - sizeof(*fde) >= *fde;
 295	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
 296		const u32 *cie = cie_for_fde(fde, table);
 297		signed ptrType;
 298
 299		if (cie == &not_fde)
 300			continue;
 301		if (cie == NULL || cie == &bad_cie)
 302			goto ret_err;
 303		ptrType = fde_pointer_type(cie);
 304		if (ptrType < 0)
 305			goto ret_err;
 306
 307		ptr = (const u8 *)(fde + 2);
 308		if (!read_pointer(&ptr, (const u8 *)(fde + 1) + *fde,
 309								ptrType)) {
 310			/* FIXME_Rajesh We have 4 instances of null addresses
 311			 * instead of the initial loc addr
 312			 * return;
 313			 */
 314			WARN(1, "unwinder: FDE->initial_location NULL %p\n",
 315				(const u8 *)(fde + 1) + *fde);
 316		}
 317		++n;
 318	}
 319
 320	if (tableSize || !n)
 321		goto ret_err;
 322
 323	hdrSize = 4 + sizeof(unsigned long) + sizeof(unsigned int)
 324	    + 2 * n * sizeof(unsigned long);
 325
 326	header = alloc(hdrSize);
 327	if (!header)
 328		goto ret_err;
 329
 330	header->version = 1;
 331	header->eh_frame_ptr_enc = DW_EH_PE_abs | DW_EH_PE_native;
 332	header->fde_count_enc = DW_EH_PE_abs | DW_EH_PE_data4;
 333	header->table_enc = DW_EH_PE_abs | DW_EH_PE_native;
 334	put_unaligned((unsigned long)table->address, &header->eh_frame_ptr);
 335	BUILD_BUG_ON(offsetof(typeof(*header), fde_count)
 336		     % __alignof(typeof(header->fde_count)));
 337	header->fde_count = n;
 338
 339	BUILD_BUG_ON(offsetof(typeof(*header), table)
 340		     % __alignof(typeof(*header->table)));
 341	for (fde = table->address, tableSize = table->size, n = 0;
 342	     tableSize;
 343	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
 344		const u32 *cie = __cie_for_fde(fde);
 345
 346		if (fde[1] == CIE_ID)
 347			continue;	/* this is a CIE */
 348		ptr = (const u8 *)(fde + 2);
 349		header->table[n].start = read_pointer(&ptr,
 350						      (const u8 *)(fde + 1) +
 351						      *fde,
 352						      fde_pointer_type(cie));
 353		header->table[n].fde = (unsigned long)fde;
 354		++n;
 355	}
 356	WARN_ON(n != header->fde_count);
 357
 358	sort(header->table,
 359	     n,
 360	     sizeof(*header->table),
 361	     cmp_eh_frame_hdr_table_entries, swap_eh_frame_hdr_table_entries);
 362
 363	table->hdrsz = hdrSize;
 364	smp_wmb();
 365	table->header = (const void *)header;
 366	return;
 367
 368ret_err:
 369	panic("Attention !!! Dwarf FDE parsing errors\n");;
 370}
 371
 372#ifdef CONFIG_MODULES
 
 
 
 
 373
 374static struct unwind_table *last_table;
 375
 376/* Must be called with module_mutex held. */
 377void *unwind_add_table(struct module *module, const void *table_start,
 378		       unsigned long table_size)
 379{
 380	struct unwind_table *table;
 381
 382	if (table_size <= 0)
 383		return NULL;
 384
 385	table = kmalloc(sizeof(*table), GFP_KERNEL);
 386	if (!table)
 387		return NULL;
 388
 389	init_unwind_table(table, module->name,
 390			  module->core_layout.base, module->core_layout.size,
 391			  module->init_layout.base, module->init_layout.size,
 392			  table_start, table_size,
 393			  NULL, 0);
 394
 395	init_unwind_hdr(table, unw_hdr_alloc);
 396
 397#ifdef UNWIND_DEBUG
 398	unw_debug("Table added for [%s] %lx %lx\n",
 399		module->name, table->core.pc, table->core.range);
 400#endif
 401	if (last_table)
 402		last_table->link = table;
 403	else
 404		root_table.link = table;
 405	last_table = table;
 406
 407	return table;
 408}
 409
 410struct unlink_table_info {
 411	struct unwind_table *table;
 412	int init_only;
 413};
 414
 415static int unlink_table(void *arg)
 416{
 417	struct unlink_table_info *info = arg;
 418	struct unwind_table *table = info->table, *prev;
 419
 420	for (prev = &root_table; prev->link && prev->link != table;
 421	     prev = prev->link)
 422		;
 423
 424	if (prev->link) {
 425		if (info->init_only) {
 426			table->init.pc = 0;
 427			table->init.range = 0;
 428			info->table = NULL;
 429		} else {
 430			prev->link = table->link;
 431			if (!prev->link)
 432				last_table = prev;
 433		}
 434	} else
 435		info->table = NULL;
 436
 437	return 0;
 438}
 439
 440/* Must be called with module_mutex held. */
 441void unwind_remove_table(void *handle, int init_only)
 442{
 443	struct unwind_table *table = handle;
 444	struct unlink_table_info info;
 445
 446	if (!table || table == &root_table)
 447		return;
 448
 449	if (init_only && table == last_table) {
 450		table->init.pc = 0;
 451		table->init.range = 0;
 452		return;
 453	}
 454
 455	info.table = table;
 456	info.init_only = init_only;
 457
 458	unlink_table(&info); /* XXX: SMP */
 459	kfree(table->header);
 460	kfree(table);
 461}
 462
 463#endif /* CONFIG_MODULES */
 464
 465static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
 466{
 467	const u8 *cur = *pcur;
 468	uleb128_t value;
 469	unsigned shift;
 470
 471	for (shift = 0, value = 0; cur < end; shift += 7) {
 472		if (shift + 7 > 8 * sizeof(value)
 473		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
 474			cur = end + 1;
 475			break;
 476		}
 477		value |= (uleb128_t) (*cur & 0x7f) << shift;
 478		if (!(*cur++ & 0x80))
 479			break;
 480	}
 481	*pcur = cur;
 482
 483	return value;
 484}
 485
 486static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
 487{
 488	const u8 *cur = *pcur;
 489	sleb128_t value;
 490	unsigned shift;
 491
 492	for (shift = 0, value = 0; cur < end; shift += 7) {
 493		if (shift + 7 > 8 * sizeof(value)
 494		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
 495			cur = end + 1;
 496			break;
 497		}
 498		value |= (sleb128_t) (*cur & 0x7f) << shift;
 499		if (!(*cur & 0x80)) {
 500			value |= -(*cur++ & 0x40) << shift;
 501			break;
 502		}
 503	}
 504	*pcur = cur;
 505
 506	return value;
 507}
 508
 509static const u32 *__cie_for_fde(const u32 *fde)
 510{
 511	const u32 *cie;
 512
 513	cie = fde + 1 - fde[1] / sizeof(*fde);
 514
 515	return cie;
 516}
 517
 518static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *table)
 519{
 520	const u32 *cie;
 521
 522	if (!*fde || (*fde & (sizeof(*fde) - 1)))
 523		return &bad_cie;
 524
 525	if (fde[1] == CIE_ID)
 526		return &not_fde;	/* this is a CIE */
 527
 528	if ((fde[1] & (sizeof(*fde) - 1)))
 529/* || fde[1] > (unsigned long)(fde + 1) - (unsigned long)table->address) */
 530		return NULL;	/* this is not a valid FDE */
 531
 532	cie = __cie_for_fde(fde);
 533
 534	if (*cie <= sizeof(*cie) + 4 || *cie >= fde[1] - sizeof(*fde)
 535	    || (*cie & (sizeof(*cie) - 1))
 536	    || (cie[1] != CIE_ID))
 537		return NULL;	/* this is not a (valid) CIE */
 538	return cie;
 539}
 540
 541static unsigned long read_pointer(const u8 **pLoc, const void *end,
 542				  signed ptrType)
 543{
 544	unsigned long value = 0;
 545	union {
 546		const u8 *p8;
 547		const u16 *p16u;
 548		const s16 *p16s;
 549		const u32 *p32u;
 550		const s32 *p32s;
 551		const unsigned long *pul;
 552	} ptr;
 553
 554	if (ptrType < 0 || ptrType == DW_EH_PE_omit)
 555		return 0;
 556	ptr.p8 = *pLoc;
 557	switch (ptrType & DW_EH_PE_FORM) {
 558	case DW_EH_PE_data2:
 559		if (end < (const void *)(ptr.p16u + 1))
 560			return 0;
 561		if (ptrType & DW_EH_PE_signed)
 562			value = get_unaligned((u16 *) ptr.p16s++);
 563		else
 564			value = get_unaligned((u16 *) ptr.p16u++);
 565		break;
 566	case DW_EH_PE_data4:
 567#ifdef CONFIG_64BIT
 568		if (end < (const void *)(ptr.p32u + 1))
 569			return 0;
 570		if (ptrType & DW_EH_PE_signed)
 571			value = get_unaligned(ptr.p32s++);
 572		else
 573			value = get_unaligned(ptr.p32u++);
 574		break;
 575	case DW_EH_PE_data8:
 576		BUILD_BUG_ON(sizeof(u64) != sizeof(value));
 577#else
 578		BUILD_BUG_ON(sizeof(u32) != sizeof(value));
 579#endif
 
 580	case DW_EH_PE_native:
 581		if (end < (const void *)(ptr.pul + 1))
 582			return 0;
 583		value = get_unaligned((unsigned long *)ptr.pul++);
 584		break;
 585	case DW_EH_PE_leb128:
 586		BUILD_BUG_ON(sizeof(uleb128_t) > sizeof(value));
 587		value = ptrType & DW_EH_PE_signed ? get_sleb128(&ptr.p8, end)
 588		    : get_uleb128(&ptr.p8, end);
 589		if ((const void *)ptr.p8 > end)
 590			return 0;
 591		break;
 592	default:
 593		return 0;
 594	}
 595	switch (ptrType & DW_EH_PE_ADJUST) {
 596	case DW_EH_PE_abs:
 597		break;
 598	case DW_EH_PE_pcrel:
 599		value += (unsigned long)*pLoc;
 600		break;
 601	default:
 602		return 0;
 603	}
 604	if ((ptrType & DW_EH_PE_indirect)
 605	    && __get_user(value, (unsigned long __user *)value))
 606		return 0;
 607	*pLoc = ptr.p8;
 608
 609	return value;
 610}
 611
 612static signed fde_pointer_type(const u32 *cie)
 613{
 614	const u8 *ptr = (const u8 *)(cie + 2);
 615	unsigned version = *ptr;
 616
 617	if (*++ptr) {
 618		const char *aug;
 619		const u8 *end = (const u8 *)(cie + 1) + *cie;
 620		uleb128_t len;
 621
 622		/* check if augmentation size is first (and thus present) */
 623		if (*ptr != 'z')
 624			return -1;
 625
 626		/* check if augmentation string is nul-terminated */
 627		aug = (const void *)ptr;
 628		ptr = memchr(aug, 0, end - ptr);
 629		if (ptr == NULL)
 630			return -1;
 631
 632		++ptr;		/* skip terminator */
 633		get_uleb128(&ptr, end);	/* skip code alignment */
 634		get_sleb128(&ptr, end);	/* skip data alignment */
 635		/* skip return address column */
 636		version <= 1 ? (void) ++ptr : (void)get_uleb128(&ptr, end);
 637		len = get_uleb128(&ptr, end);	/* augmentation length */
 638
 639		if (ptr + len < ptr || ptr + len > end)
 640			return -1;
 641
 642		end = ptr + len;
 643		while (*++aug) {
 644			if (ptr >= end)
 645				return -1;
 646			switch (*aug) {
 647			case 'L':
 648				++ptr;
 649				break;
 650			case 'P':{
 651					signed ptrType = *ptr++;
 652
 653					if (!read_pointer(&ptr, end, ptrType)
 654					    || ptr > end)
 655						return -1;
 656				}
 657				break;
 658			case 'R':
 659				return *ptr;
 660			default:
 661				return -1;
 662			}
 663		}
 664	}
 665	return DW_EH_PE_native | DW_EH_PE_abs;
 666}
 667
 668static int advance_loc(unsigned long delta, struct unwind_state *state)
 669{
 670	state->loc += delta * state->codeAlign;
 671
 672	/* FIXME_Rajesh: Probably we are defining for the initial range as well;
 673	   return delta > 0;
 674	 */
 675	unw_debug("delta %3lu => loc 0x%lx: ", delta, state->loc);
 676	return 1;
 677}
 678
 679static void set_rule(uleb128_t reg, enum item_location where, uleb128_t value,
 680		     struct unwind_state *state)
 681{
 682	if (reg < ARRAY_SIZE(state->regs)) {
 683		state->regs[reg].where = where;
 684		state->regs[reg].value = value;
 685
 686#ifdef UNWIND_DEBUG
 687		unw_debug("r%lu: ", reg);
 688		switch (where) {
 689		case Nowhere:
 690			unw_debug("s ");
 691			break;
 692		case Memory:
 693			unw_debug("c(%lu) ", value);
 694			break;
 695		case Register:
 696			unw_debug("r(%lu) ", value);
 697			break;
 698		case Value:
 699			unw_debug("v(%lu) ", value);
 700			break;
 701		default:
 702			break;
 703		}
 704#endif
 705	}
 706}
 707
 708static int processCFI(const u8 *start, const u8 *end, unsigned long targetLoc,
 709		      signed ptrType, struct unwind_state *state)
 710{
 711	union {
 712		const u8 *p8;
 713		const u16 *p16;
 714		const u32 *p32;
 715	} ptr;
 716	int result = 1;
 717	u8 opcode;
 718
 719	if (start != state->cieStart) {
 720		state->loc = state->org;
 721		result =
 722		    processCFI(state->cieStart, state->cieEnd, 0, ptrType,
 723			       state);
 724		if (targetLoc == 0 && state->label == NULL)
 725			return result;
 726	}
 727	for (ptr.p8 = start; result && ptr.p8 < end;) {
 728		switch (*ptr.p8 >> 6) {
 729			uleb128_t value;
 730
 731		case 0:
 732			opcode = *ptr.p8++;
 733
 734			switch (opcode) {
 735			case DW_CFA_nop:
 736				unw_debug("cfa nop ");
 737				break;
 738			case DW_CFA_set_loc:
 739				state->loc = read_pointer(&ptr.p8, end,
 740							  ptrType);
 741				if (state->loc == 0)
 742					result = 0;
 743				unw_debug("cfa_set_loc: 0x%lx ", state->loc);
 744				break;
 745			case DW_CFA_advance_loc1:
 746				unw_debug("\ncfa advance loc1:");
 747				result = ptr.p8 < end
 748				    && advance_loc(*ptr.p8++, state);
 749				break;
 750			case DW_CFA_advance_loc2:
 751				value = *ptr.p8++;
 752				value += *ptr.p8++ << 8;
 753				unw_debug("\ncfa advance loc2:");
 754				result = ptr.p8 <= end + 2
 755				    /* && advance_loc(*ptr.p16++, state); */
 756				    && advance_loc(value, state);
 757				break;
 758			case DW_CFA_advance_loc4:
 759				unw_debug("\ncfa advance loc4:");
 760				result = ptr.p8 <= end + 4
 761				    && advance_loc(*ptr.p32++, state);
 762				break;
 763			case DW_CFA_offset_extended:
 764				value = get_uleb128(&ptr.p8, end);
 765				unw_debug("cfa_offset_extended: ");
 766				set_rule(value, Memory,
 767					 get_uleb128(&ptr.p8, end), state);
 768				break;
 769			case DW_CFA_val_offset:
 770				value = get_uleb128(&ptr.p8, end);
 771				set_rule(value, Value,
 772					 get_uleb128(&ptr.p8, end), state);
 773				break;
 774			case DW_CFA_offset_extended_sf:
 775				value = get_uleb128(&ptr.p8, end);
 776				set_rule(value, Memory,
 777					 get_sleb128(&ptr.p8, end), state);
 778				break;
 779			case DW_CFA_val_offset_sf:
 780				value = get_uleb128(&ptr.p8, end);
 781				set_rule(value, Value,
 782					 get_sleb128(&ptr.p8, end), state);
 783				break;
 784			case DW_CFA_restore_extended:
 785				unw_debug("cfa_restore_extended: ");
 786			case DW_CFA_undefined:
 787				unw_debug("cfa_undefined: ");
 788			case DW_CFA_same_value:
 789				unw_debug("cfa_same_value: ");
 790				set_rule(get_uleb128(&ptr.p8, end), Nowhere, 0,
 791					 state);
 792				break;
 793			case DW_CFA_register:
 794				unw_debug("cfa_register: ");
 795				value = get_uleb128(&ptr.p8, end);
 796				set_rule(value,
 797					 Register,
 798					 get_uleb128(&ptr.p8, end), state);
 799				break;
 800			case DW_CFA_remember_state:
 801				unw_debug("cfa_remember_state: ");
 802				if (ptr.p8 == state->label) {
 803					state->label = NULL;
 804					return 1;
 805				}
 806				if (state->stackDepth >= MAX_STACK_DEPTH)
 807					return 0;
 808				state->stack[state->stackDepth++] = ptr.p8;
 809				break;
 810			case DW_CFA_restore_state:
 811				unw_debug("cfa_restore_state: ");
 812				if (state->stackDepth) {
 813					const uleb128_t loc = state->loc;
 814					const u8 *label = state->label;
 815
 816					state->label =
 817					    state->stack[state->stackDepth - 1];
 818					memcpy(&state->cfa, &badCFA,
 819					       sizeof(state->cfa));
 820					memset(state->regs, 0,
 821					       sizeof(state->regs));
 822					state->stackDepth = 0;
 823					result =
 824					    processCFI(start, end, 0, ptrType,
 825						       state);
 826					state->loc = loc;
 827					state->label = label;
 828				} else
 829					return 0;
 830				break;
 831			case DW_CFA_def_cfa:
 832				state->cfa.reg = get_uleb128(&ptr.p8, end);
 833				unw_debug("cfa_def_cfa: r%lu ", state->cfa.reg);
 834				/*nobreak*/
 835			case DW_CFA_def_cfa_offset:
 836				state->cfa.offs = get_uleb128(&ptr.p8, end);
 837				unw_debug("cfa_def_cfa_offset: 0x%lx ",
 838					  state->cfa.offs);
 839				break;
 840			case DW_CFA_def_cfa_sf:
 841				state->cfa.reg = get_uleb128(&ptr.p8, end);
 842				/*nobreak */
 843			case DW_CFA_def_cfa_offset_sf:
 844				state->cfa.offs = get_sleb128(&ptr.p8, end)
 845				    * state->dataAlign;
 846				break;
 847			case DW_CFA_def_cfa_register:
 848				unw_debug("cfa_def_cfa_regsiter: ");
 849				state->cfa.reg = get_uleb128(&ptr.p8, end);
 850				break;
 851				/*todo case DW_CFA_def_cfa_expression: */
 852				/*todo case DW_CFA_expression: */
 853				/*todo case DW_CFA_val_expression: */
 854			case DW_CFA_GNU_args_size:
 855				get_uleb128(&ptr.p8, end);
 856				break;
 857			case DW_CFA_GNU_negative_offset_extended:
 858				value = get_uleb128(&ptr.p8, end);
 859				set_rule(value,
 860					 Memory,
 861					 (uleb128_t) 0 - get_uleb128(&ptr.p8,
 862								     end),
 863					 state);
 864				break;
 865			case DW_CFA_GNU_window_save:
 866			default:
 867				unw_debug("UNKNOWN OPCODE 0x%x\n", opcode);
 868				result = 0;
 869				break;
 870			}
 871			break;
 872		case 1:
 873			unw_debug("\ncfa_adv_loc: ");
 874			result = advance_loc(*ptr.p8++ & 0x3f, state);
 875			break;
 876		case 2:
 877			unw_debug("cfa_offset: ");
 878			value = *ptr.p8++ & 0x3f;
 879			set_rule(value, Memory, get_uleb128(&ptr.p8, end),
 880				 state);
 881			break;
 882		case 3:
 883			unw_debug("cfa_restore: ");
 884			set_rule(*ptr.p8++ & 0x3f, Nowhere, 0, state);
 885			break;
 886		}
 887
 888		if (ptr.p8 > end)
 889			result = 0;
 890		if (result && targetLoc != 0 && targetLoc < state->loc)
 891			return 1;
 892	}
 893
 894	return result && ptr.p8 == end && (targetLoc == 0 || (
 895		/*todo While in theory this should apply, gcc in practice omits
 896		  everything past the function prolog, and hence the location
 897		  never reaches the end of the function.
 898		targetLoc < state->loc && */  state->label == NULL));
 899}
 900
 901/* Unwind to previous to frame.  Returns 0 if successful, negative
 902 * number in case of an error. */
 903int arc_unwind(struct unwind_frame_info *frame)
 904{
 905#define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
 906	const u32 *fde = NULL, *cie = NULL;
 907	const u8 *ptr = NULL, *end = NULL;
 908	unsigned long pc = UNW_PC(frame) - frame->call_frame;
 909	unsigned long startLoc = 0, endLoc = 0, cfa;
 910	unsigned i;
 911	signed ptrType = -1;
 912	uleb128_t retAddrReg = 0;
 913	const struct unwind_table *table;
 914	struct unwind_state state;
 915	unsigned long *fptr;
 916	unsigned long addr;
 917
 918	unw_debug("\n\nUNWIND FRAME:\n");
 919	unw_debug("PC: 0x%lx BLINK: 0x%lx, SP: 0x%lx, FP: 0x%x\n",
 920		  UNW_PC(frame), UNW_BLINK(frame), UNW_SP(frame),
 921		  UNW_FP(frame));
 922
 923	if (UNW_PC(frame) == 0)
 924		return -EINVAL;
 925
 926#ifdef UNWIND_DEBUG
 927	{
 928		unsigned long *sptr = (unsigned long *)UNW_SP(frame);
 929		unw_debug("\nStack Dump:\n");
 930		for (i = 0; i < 20; i++, sptr++)
 931			unw_debug("0x%p:  0x%lx\n", sptr, *sptr);
 932		unw_debug("\n");
 933	}
 934#endif
 935
 936	table = find_table(pc);
 937	if (table != NULL
 938	    && !(table->size & (sizeof(*fde) - 1))) {
 939		const u8 *hdr = table->header;
 940		unsigned long tableSize;
 941
 942		smp_rmb();
 943		if (hdr && hdr[0] == 1) {
 944			switch (hdr[3] & DW_EH_PE_FORM) {
 945			case DW_EH_PE_native:
 946				tableSize = sizeof(unsigned long);
 947				break;
 948			case DW_EH_PE_data2:
 949				tableSize = 2;
 950				break;
 951			case DW_EH_PE_data4:
 952				tableSize = 4;
 953				break;
 954			case DW_EH_PE_data8:
 955				tableSize = 8;
 956				break;
 957			default:
 958				tableSize = 0;
 959				break;
 960			}
 961			ptr = hdr + 4;
 962			end = hdr + table->hdrsz;
 963			if (tableSize && read_pointer(&ptr, end, hdr[1])
 964			    == (unsigned long)table->address
 965			    && (i = read_pointer(&ptr, end, hdr[2])) > 0
 966			    && i == (end - ptr) / (2 * tableSize)
 967			    && !((end - ptr) % (2 * tableSize))) {
 968				do {
 969					const u8 *cur =
 970					    ptr + (i / 2) * (2 * tableSize);
 971
 972					startLoc = read_pointer(&cur,
 973								cur + tableSize,
 974								hdr[3]);
 975					if (pc < startLoc)
 976						i /= 2;
 977					else {
 978						ptr = cur - tableSize;
 979						i = (i + 1) / 2;
 980					}
 981				} while (startLoc && i > 1);
 982				if (i == 1
 983				    && (startLoc = read_pointer(&ptr,
 984								ptr + tableSize,
 985								hdr[3])) != 0
 986				    && pc >= startLoc)
 987					fde = (void *)read_pointer(&ptr,
 988								   ptr +
 989								   tableSize,
 990								   hdr[3]);
 991			}
 992		}
 993
 994		if (fde != NULL) {
 995			cie = cie_for_fde(fde, table);
 996			ptr = (const u8 *)(fde + 2);
 997			if (cie != NULL
 998			    && cie != &bad_cie
 999			    && cie != &not_fde
1000			    && (ptrType = fde_pointer_type(cie)) >= 0
1001			    && read_pointer(&ptr,
1002					    (const u8 *)(fde + 1) + *fde,
1003					    ptrType) == startLoc) {
1004				if (!(ptrType & DW_EH_PE_indirect))
1005					ptrType &=
1006					    DW_EH_PE_FORM | DW_EH_PE_signed;
1007				endLoc =
1008				    startLoc + read_pointer(&ptr,
1009							    (const u8 *)(fde +
1010									 1) +
1011							    *fde, ptrType);
1012				if (pc >= endLoc) {
1013					fde = NULL;
1014					cie = NULL;
1015				}
1016			} else {
1017				fde = NULL;
1018				cie = NULL;
1019			}
1020		}
1021	}
1022	if (cie != NULL) {
1023		memset(&state, 0, sizeof(state));
1024		state.cieEnd = ptr;	/* keep here temporarily */
1025		ptr = (const u8 *)(cie + 2);
1026		end = (const u8 *)(cie + 1) + *cie;
1027		frame->call_frame = 1;
1028		if (*++ptr) {
1029			/* check if augmentation size is first (thus present) */
1030			if (*ptr == 'z') {
1031				while (++ptr < end && *ptr) {
1032					switch (*ptr) {
1033					/* chk for ignorable or already handled
1034					 * nul-terminated augmentation string */
1035					case 'L':
1036					case 'P':
1037					case 'R':
1038						continue;
1039					case 'S':
1040						frame->call_frame = 0;
1041						continue;
1042					default:
1043						break;
1044					}
1045					break;
1046				}
1047			}
1048			if (ptr >= end || *ptr)
1049				cie = NULL;
1050		}
1051		++ptr;
1052	}
1053	if (cie != NULL) {
1054		/* get code aligment factor */
1055		state.codeAlign = get_uleb128(&ptr, end);
1056		/* get data aligment factor */
1057		state.dataAlign = get_sleb128(&ptr, end);
1058		if (state.codeAlign == 0 || state.dataAlign == 0 || ptr >= end)
1059			cie = NULL;
1060		else {
1061			retAddrReg =
1062			    state.version <= 1 ? *ptr++ : get_uleb128(&ptr,
1063								      end);
1064			unw_debug("CIE Frame Info:\n");
1065			unw_debug("return Address register 0x%lx\n",
1066				  retAddrReg);
1067			unw_debug("data Align: %ld\n", state.dataAlign);
1068			unw_debug("code Align: %lu\n", state.codeAlign);
1069			/* skip augmentation */
1070			if (((const char *)(cie + 2))[1] == 'z') {
1071				uleb128_t augSize = get_uleb128(&ptr, end);
1072
1073				ptr += augSize;
1074			}
1075			if (ptr > end || retAddrReg >= ARRAY_SIZE(reg_info)
1076			    || REG_INVALID(retAddrReg)
1077			    || reg_info[retAddrReg].width !=
1078			    sizeof(unsigned long))
1079				cie = NULL;
1080		}
1081	}
1082	if (cie != NULL) {
1083		state.cieStart = ptr;
1084		ptr = state.cieEnd;
1085		state.cieEnd = end;
1086		end = (const u8 *)(fde + 1) + *fde;
1087		/* skip augmentation */
1088		if (((const char *)(cie + 2))[1] == 'z') {
1089			uleb128_t augSize = get_uleb128(&ptr, end);
1090
1091			if ((ptr += augSize) > end)
1092				fde = NULL;
1093		}
1094	}
1095	if (cie == NULL || fde == NULL) {
1096#ifdef CONFIG_FRAME_POINTER
1097		unsigned long top, bottom;
1098
1099		top = STACK_TOP_UNW(frame->task);
1100		bottom = STACK_BOTTOM_UNW(frame->task);
1101#if FRAME_RETADDR_OFFSET < 0
1102		if (UNW_SP(frame) < top && UNW_FP(frame) <= UNW_SP(frame)
1103		    && bottom < UNW_FP(frame)
1104#else
1105		if (UNW_SP(frame) > top && UNW_FP(frame) >= UNW_SP(frame)
1106		    && bottom > UNW_FP(frame)
1107#endif
1108		    && !((UNW_SP(frame) | UNW_FP(frame))
1109			 & (sizeof(unsigned long) - 1))) {
1110			unsigned long link;
1111
1112			if (!__get_user(link, (unsigned long *)
1113					(UNW_FP(frame) + FRAME_LINK_OFFSET))
1114#if FRAME_RETADDR_OFFSET < 0
1115			    && link > bottom && link < UNW_FP(frame)
1116#else
1117			    && link > UNW_FP(frame) && link < bottom
1118#endif
1119			    && !(link & (sizeof(link) - 1))
1120			    && !__get_user(UNW_PC(frame),
1121					   (unsigned long *)(UNW_FP(frame)
1122						+ FRAME_RETADDR_OFFSET)))
1123			{
1124				UNW_SP(frame) =
1125				    UNW_FP(frame) + FRAME_RETADDR_OFFSET
1126#if FRAME_RETADDR_OFFSET < 0
1127				    -
1128#else
1129				    +
1130#endif
1131				    sizeof(UNW_PC(frame));
1132				UNW_FP(frame) = link;
1133				return 0;
1134			}
1135		}
1136#endif
1137		return -ENXIO;
1138	}
1139	state.org = startLoc;
1140	memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
1141
1142	unw_debug("\nProcess instructions\n");
1143
1144	/* process instructions
1145	 * For ARC, we optimize by having blink(retAddrReg) with
1146	 * the sameValue in the leaf function, so we should not check
1147	 * state.regs[retAddrReg].where == Nowhere
1148	 */
1149	if (!processCFI(ptr, end, pc, ptrType, &state)
1150	    || state.loc > endLoc
1151/*	   || state.regs[retAddrReg].where == Nowhere */
1152	    || state.cfa.reg >= ARRAY_SIZE(reg_info)
1153	    || reg_info[state.cfa.reg].width != sizeof(unsigned long)
1154	    || state.cfa.offs % sizeof(unsigned long))
1155		return -EIO;
1156
1157#ifdef UNWIND_DEBUG
1158	unw_debug("\n");
1159
1160	unw_debug("\nRegister State Based on the rules parsed from FDE:\n");
1161	for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1162
1163		if (REG_INVALID(i))
1164			continue;
1165
1166		switch (state.regs[i].where) {
1167		case Nowhere:
1168			break;
1169		case Memory:
1170			unw_debug(" r%d: c(%lu),", i, state.regs[i].value);
1171			break;
1172		case Register:
1173			unw_debug(" r%d: r(%lu),", i, state.regs[i].value);
1174			break;
1175		case Value:
1176			unw_debug(" r%d: v(%lu),", i, state.regs[i].value);
1177			break;
1178		}
1179	}
1180
1181	unw_debug("\n");
1182#endif
1183
1184	/* update frame */
1185#ifndef CONFIG_AS_CFI_SIGNAL_FRAME
1186	if (frame->call_frame
1187	    && !UNW_DEFAULT_RA(state.regs[retAddrReg], state.dataAlign))
1188		frame->call_frame = 0;
1189#endif
1190	cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs;
1191	startLoc = min_t(unsigned long, UNW_SP(frame), cfa);
1192	endLoc = max_t(unsigned long, UNW_SP(frame), cfa);
1193	if (STACK_LIMIT(startLoc) != STACK_LIMIT(endLoc)) {
1194		startLoc = min(STACK_LIMIT(cfa), cfa);
1195		endLoc = max(STACK_LIMIT(cfa), cfa);
1196	}
1197
1198	unw_debug("\nCFA reg: 0x%lx, offset: 0x%lx =>  0x%lx\n",
1199		  state.cfa.reg, state.cfa.offs, cfa);
1200
1201	for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1202		if (REG_INVALID(i)) {
1203			if (state.regs[i].where == Nowhere)
1204				continue;
1205			return -EIO;
1206		}
1207		switch (state.regs[i].where) {
1208		default:
1209			break;
1210		case Register:
1211			if (state.regs[i].value >= ARRAY_SIZE(reg_info)
1212			    || REG_INVALID(state.regs[i].value)
1213			    || reg_info[i].width >
1214			    reg_info[state.regs[i].value].width)
1215				return -EIO;
1216			switch (reg_info[state.regs[i].value].width) {
1217			case sizeof(u8):
1218				state.regs[i].value =
1219				FRAME_REG(state.regs[i].value, const u8);
1220				break;
1221			case sizeof(u16):
1222				state.regs[i].value =
1223				FRAME_REG(state.regs[i].value, const u16);
1224				break;
1225			case sizeof(u32):
1226				state.regs[i].value =
1227				FRAME_REG(state.regs[i].value, const u32);
1228				break;
1229#ifdef CONFIG_64BIT
1230			case sizeof(u64):
1231				state.regs[i].value =
1232				FRAME_REG(state.regs[i].value, const u64);
1233				break;
1234#endif
1235			default:
1236				return -EIO;
1237			}
1238			break;
1239		}
1240	}
1241
1242	unw_debug("\nRegister state after evaluation with realtime Stack:\n");
1243	fptr = (unsigned long *)(&frame->regs);
1244	for (i = 0; i < ARRAY_SIZE(state.regs); ++i, fptr++) {
1245
1246		if (REG_INVALID(i))
1247			continue;
1248		switch (state.regs[i].where) {
1249		case Nowhere:
1250			if (reg_info[i].width != sizeof(UNW_SP(frame))
1251			    || &FRAME_REG(i, __typeof__(UNW_SP(frame)))
1252			    != &UNW_SP(frame))
1253				continue;
1254			UNW_SP(frame) = cfa;
1255			break;
1256		case Register:
1257			switch (reg_info[i].width) {
1258			case sizeof(u8):
1259				FRAME_REG(i, u8) = state.regs[i].value;
1260				break;
1261			case sizeof(u16):
1262				FRAME_REG(i, u16) = state.regs[i].value;
1263				break;
1264			case sizeof(u32):
1265				FRAME_REG(i, u32) = state.regs[i].value;
1266				break;
1267#ifdef CONFIG_64BIT
1268			case sizeof(u64):
1269				FRAME_REG(i, u64) = state.regs[i].value;
1270				break;
1271#endif
1272			default:
1273				return -EIO;
1274			}
1275			break;
1276		case Value:
1277			if (reg_info[i].width != sizeof(unsigned long))
1278				return -EIO;
1279			FRAME_REG(i, unsigned long) = cfa + state.regs[i].value
1280			    * state.dataAlign;
1281			break;
1282		case Memory:
1283			addr = cfa + state.regs[i].value * state.dataAlign;
1284
1285			if ((state.regs[i].value * state.dataAlign)
1286			    % sizeof(unsigned long)
1287			    || addr < startLoc
1288			    || addr + sizeof(unsigned long) < addr
1289			    || addr + sizeof(unsigned long) > endLoc)
1290					return -EIO;
1291
1292			switch (reg_info[i].width) {
1293			case sizeof(u8):
1294				__get_user(FRAME_REG(i, u8),
1295					   (u8 __user *)addr);
1296				break;
1297			case sizeof(u16):
1298				__get_user(FRAME_REG(i, u16),
1299					   (u16 __user *)addr);
1300				break;
1301			case sizeof(u32):
1302				__get_user(FRAME_REG(i, u32),
1303					   (u32 __user *)addr);
1304				break;
1305#ifdef CONFIG_64BIT
1306			case sizeof(u64):
1307				__get_user(FRAME_REG(i, u64),
1308					   (u64 __user *)addr);
1309				break;
1310#endif
1311			default:
1312				return -EIO;
1313			}
1314
1315			break;
1316		}
1317		unw_debug("r%d: 0x%lx ", i, *fptr);
1318	}
1319
1320	return 0;
1321#undef FRAME_REG
1322}
1323EXPORT_SYMBOL(arc_unwind);
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (C) 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
   4 * Copyright (C) 2002-2006 Novell, Inc.
   5 *	Jan Beulich <jbeulich@novell.com>
   6 *
 
 
 
 
   7 * A simple API for unwinding kernel stacks.  This is used for
   8 * debugging and error reporting purposes.  The kernel doesn't need
   9 * full-blown stack unwinding with all the bells and whistles, so there
  10 * is not much point in implementing the full Dwarf2 unwind API.
  11 */
  12
  13#include <linux/sched.h>
  14#include <linux/module.h>
  15#include <linux/memblock.h>
  16#include <linux/sort.h>
  17#include <linux/slab.h>
  18#include <linux/stop_machine.h>
  19#include <linux/uaccess.h>
  20#include <linux/ptrace.h>
  21#include <asm/sections.h>
  22#include <asm/unaligned.h>
  23#include <asm/unwind.h>
  24
  25extern char __start_unwind[], __end_unwind[];
  26/* extern const u8 __start_unwind_hdr[], __end_unwind_hdr[];*/
  27
  28/* #define UNWIND_DEBUG */
  29
  30#ifdef UNWIND_DEBUG
  31int dbg_unw;
  32#define unw_debug(fmt, ...)			\
  33do {						\
  34	if (dbg_unw)				\
  35		pr_info(fmt, ##__VA_ARGS__);	\
  36} while (0);
  37#else
  38#define unw_debug(fmt, ...)
  39#endif
  40
  41#define MAX_STACK_DEPTH 8
  42
  43#define EXTRA_INFO(f) { \
  44		BUILD_BUG_ON_ZERO(offsetof(struct unwind_frame_info, f) \
  45				% sizeof_field(struct unwind_frame_info, f)) \
  46				+ offsetof(struct unwind_frame_info, f) \
  47				/ sizeof_field(struct unwind_frame_info, f), \
  48				sizeof_field(struct unwind_frame_info, f) \
  49	}
  50#define PTREGS_INFO(f) EXTRA_INFO(regs.f)
  51
  52static const struct {
  53	unsigned offs:BITS_PER_LONG / 2;
  54	unsigned width:BITS_PER_LONG / 2;
  55} reg_info[] = {
  56UNW_REGISTER_INFO};
  57
  58#undef PTREGS_INFO
  59#undef EXTRA_INFO
  60
  61#ifndef REG_INVALID
  62#define REG_INVALID(r) (reg_info[r].width == 0)
  63#endif
  64
  65#define DW_CFA_nop                          0x00
  66#define DW_CFA_set_loc                      0x01
  67#define DW_CFA_advance_loc1                 0x02
  68#define DW_CFA_advance_loc2                 0x03
  69#define DW_CFA_advance_loc4                 0x04
  70#define DW_CFA_offset_extended              0x05
  71#define DW_CFA_restore_extended             0x06
  72#define DW_CFA_undefined                    0x07
  73#define DW_CFA_same_value                   0x08
  74#define DW_CFA_register                     0x09
  75#define DW_CFA_remember_state               0x0a
  76#define DW_CFA_restore_state                0x0b
  77#define DW_CFA_def_cfa                      0x0c
  78#define DW_CFA_def_cfa_register             0x0d
  79#define DW_CFA_def_cfa_offset               0x0e
  80#define DW_CFA_def_cfa_expression           0x0f
  81#define DW_CFA_expression                   0x10
  82#define DW_CFA_offset_extended_sf           0x11
  83#define DW_CFA_def_cfa_sf                   0x12
  84#define DW_CFA_def_cfa_offset_sf            0x13
  85#define DW_CFA_val_offset                   0x14
  86#define DW_CFA_val_offset_sf                0x15
  87#define DW_CFA_val_expression               0x16
  88#define DW_CFA_lo_user                      0x1c
  89#define DW_CFA_GNU_window_save              0x2d
  90#define DW_CFA_GNU_args_size                0x2e
  91#define DW_CFA_GNU_negative_offset_extended 0x2f
  92#define DW_CFA_hi_user                      0x3f
  93
  94#define DW_EH_PE_FORM     0x07
  95#define DW_EH_PE_native   0x00
  96#define DW_EH_PE_leb128   0x01
  97#define DW_EH_PE_data2    0x02
  98#define DW_EH_PE_data4    0x03
  99#define DW_EH_PE_data8    0x04
 100#define DW_EH_PE_signed   0x08
 101#define DW_EH_PE_ADJUST   0x70
 102#define DW_EH_PE_abs      0x00
 103#define DW_EH_PE_pcrel    0x10
 104#define DW_EH_PE_textrel  0x20
 105#define DW_EH_PE_datarel  0x30
 106#define DW_EH_PE_funcrel  0x40
 107#define DW_EH_PE_aligned  0x50
 108#define DW_EH_PE_indirect 0x80
 109#define DW_EH_PE_omit     0xff
 110
 111#define CIE_ID	0
 112
 113typedef unsigned long uleb128_t;
 114typedef signed long sleb128_t;
 115
 116static struct unwind_table {
 117	struct {
 118		unsigned long pc;
 119		unsigned long range;
 120	} core, init;
 121	const void *address;
 122	unsigned long size;
 123	const unsigned char *header;
 124	unsigned long hdrsz;
 125	struct unwind_table *link;
 126	const char *name;
 127} root_table;
 128
 129struct unwind_item {
 130	enum item_location {
 131		Nowhere,
 132		Memory,
 133		Register,
 134		Value
 135	} where;
 136	uleb128_t value;
 137};
 138
 139struct unwind_state {
 140	uleb128_t loc, org;
 141	const u8 *cieStart, *cieEnd;
 142	uleb128_t codeAlign;
 143	sleb128_t dataAlign;
 144	struct cfa {
 145		uleb128_t reg, offs;
 146	} cfa;
 147	struct unwind_item regs[ARRAY_SIZE(reg_info)];
 148	unsigned stackDepth:8;
 149	unsigned version:8;
 150	const u8 *label;
 151	const u8 *stack[MAX_STACK_DEPTH];
 152};
 153
 154static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
 155
 156static struct unwind_table *find_table(unsigned long pc)
 157{
 158	struct unwind_table *table;
 159
 160	for (table = &root_table; table; table = table->link)
 161		if ((pc >= table->core.pc
 162		     && pc < table->core.pc + table->core.range)
 163		    || (pc >= table->init.pc
 164			&& pc < table->init.pc + table->init.range))
 165			break;
 166
 167	return table;
 168}
 169
 170static unsigned long read_pointer(const u8 **pLoc,
 171				  const void *end, signed ptrType);
 172static void init_unwind_hdr(struct unwind_table *table,
 173			    void *(*alloc) (unsigned long));
 174
 175/*
 176 * wrappers for header alloc (vs. calling one vs. other at call site)
 177 * to elide section mismatches warnings
 178 */
 179static void *__init unw_hdr_alloc_early(unsigned long sz)
 180{
 181	return memblock_alloc_from(sz, sizeof(unsigned int), MAX_DMA_ADDRESS);
 
 
 
 
 
 
 182}
 183
 184static void init_unwind_table(struct unwind_table *table, const char *name,
 185			      const void *core_start, unsigned long core_size,
 186			      const void *init_start, unsigned long init_size,
 187			      const void *table_start, unsigned long table_size,
 188			      const u8 *header_start, unsigned long header_size)
 189{
 
 
 
 190	table->core.pc = (unsigned long)core_start;
 191	table->core.range = core_size;
 192	table->init.pc = (unsigned long)init_start;
 193	table->init.range = init_size;
 194	table->address = table_start;
 195	table->size = table_size;
 196	/* To avoid the pointer addition with NULL pointer.*/
 197	if (header_start != NULL) {
 198		const u8 *ptr = header_start + 4;
 199		const u8 *end = header_start + header_size;
 200		/* See if the linker provided table looks valid. */
 201		if (header_size <= 4
 202		|| header_start[0] != 1
 203		|| (void *)read_pointer(&ptr, end, header_start[1])
 204				!= table_start
 205		|| header_start[2] == DW_EH_PE_omit
 206		|| read_pointer(&ptr, end, header_start[2]) <= 0
 207		|| header_start[3] == DW_EH_PE_omit)
 208			header_start = NULL;
 209	}
 210	table->hdrsz = header_size;
 211	smp_wmb();
 212	table->header = header_start;
 213	table->link = NULL;
 214	table->name = name;
 215}
 216
 217void __init arc_unwind_init(void)
 218{
 219	init_unwind_table(&root_table, "kernel", _text, _end - _text, NULL, 0,
 220			  __start_unwind, __end_unwind - __start_unwind,
 221			  NULL, 0);
 222	  /*__start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);*/
 223
 224	init_unwind_hdr(&root_table, unw_hdr_alloc_early);
 225}
 226
 227static const u32 bad_cie, not_fde;
 228static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *);
 229static const u32 *__cie_for_fde(const u32 *fde);
 230static signed fde_pointer_type(const u32 *cie);
 231
 232struct eh_frame_hdr_table_entry {
 233	unsigned long start, fde;
 234};
 235
 236static int cmp_eh_frame_hdr_table_entries(const void *p1, const void *p2)
 237{
 238	const struct eh_frame_hdr_table_entry *e1 = p1;
 239	const struct eh_frame_hdr_table_entry *e2 = p2;
 240
 241	return (e1->start > e2->start) - (e1->start < e2->start);
 242}
 243
 244static void swap_eh_frame_hdr_table_entries(void *p1, void *p2, int size)
 245{
 246	struct eh_frame_hdr_table_entry *e1 = p1;
 247	struct eh_frame_hdr_table_entry *e2 = p2;
 248	unsigned long v;
 249
 250	v = e1->start;
 251	e1->start = e2->start;
 252	e2->start = v;
 253	v = e1->fde;
 254	e1->fde = e2->fde;
 255	e2->fde = v;
 256}
 257
 258static void init_unwind_hdr(struct unwind_table *table,
 259			    void *(*alloc) (unsigned long))
 260{
 261	const u8 *ptr;
 262	unsigned long tableSize = table->size, hdrSize;
 263	unsigned int n;
 264	const u32 *fde;
 265	struct {
 266		u8 version;
 267		u8 eh_frame_ptr_enc;
 268		u8 fde_count_enc;
 269		u8 table_enc;
 270		unsigned long eh_frame_ptr;
 271		unsigned int fde_count;
 272		struct eh_frame_hdr_table_entry table[];
 273	} __attribute__ ((__packed__)) *header;
 274
 275	if (table->header)
 276		return;
 277
 278	if (table->hdrsz)
 279		pr_warn(".eh_frame_hdr for '%s' present but unusable\n",
 280			table->name);
 281
 282	if (tableSize & (sizeof(*fde) - 1))
 283		return;
 284
 285	for (fde = table->address, n = 0;
 286	     tableSize > sizeof(*fde) && tableSize - sizeof(*fde) >= *fde;
 287	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
 288		const u32 *cie = cie_for_fde(fde, table);
 289		signed ptrType;
 290
 291		if (cie == &not_fde)
 292			continue;
 293		if (cie == NULL || cie == &bad_cie)
 294			goto ret_err;
 295		ptrType = fde_pointer_type(cie);
 296		if (ptrType < 0)
 297			goto ret_err;
 298
 299		ptr = (const u8 *)(fde + 2);
 300		if (!read_pointer(&ptr, (const u8 *)(fde + 1) + *fde,
 301								ptrType)) {
 302			/* FIXME_Rajesh We have 4 instances of null addresses
 303			 * instead of the initial loc addr
 304			 * return;
 305			 */
 306			WARN(1, "unwinder: FDE->initial_location NULL %p\n",
 307				(const u8 *)(fde + 1) + *fde);
 308		}
 309		++n;
 310	}
 311
 312	if (tableSize || !n)
 313		goto ret_err;
 314
 315	hdrSize = 4 + sizeof(unsigned long) + sizeof(unsigned int)
 316	    + 2 * n * sizeof(unsigned long);
 317
 318	header = alloc(hdrSize);
 319	if (!header)
 320		goto ret_err;
 321
 322	header->version = 1;
 323	header->eh_frame_ptr_enc = DW_EH_PE_abs | DW_EH_PE_native;
 324	header->fde_count_enc = DW_EH_PE_abs | DW_EH_PE_data4;
 325	header->table_enc = DW_EH_PE_abs | DW_EH_PE_native;
 326	put_unaligned((unsigned long)table->address, &header->eh_frame_ptr);
 327	BUILD_BUG_ON(offsetof(typeof(*header), fde_count)
 328		     % __alignof(typeof(header->fde_count)));
 329	header->fde_count = n;
 330
 331	BUILD_BUG_ON(offsetof(typeof(*header), table)
 332		     % __alignof(typeof(*header->table)));
 333	for (fde = table->address, tableSize = table->size, n = 0;
 334	     tableSize;
 335	     tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
 336		const u32 *cie = __cie_for_fde(fde);
 337
 338		if (fde[1] == CIE_ID)
 339			continue;	/* this is a CIE */
 340		ptr = (const u8 *)(fde + 2);
 341		header->table[n].start = read_pointer(&ptr,
 342						      (const u8 *)(fde + 1) +
 343						      *fde,
 344						      fde_pointer_type(cie));
 345		header->table[n].fde = (unsigned long)fde;
 346		++n;
 347	}
 348	WARN_ON(n != header->fde_count);
 349
 350	sort(header->table,
 351	     n,
 352	     sizeof(*header->table),
 353	     cmp_eh_frame_hdr_table_entries, swap_eh_frame_hdr_table_entries);
 354
 355	table->hdrsz = hdrSize;
 356	smp_wmb();
 357	table->header = (const void *)header;
 358	return;
 359
 360ret_err:
 361	panic("Attention !!! Dwarf FDE parsing errors\n");
 362}
 363
 364#ifdef CONFIG_MODULES
 365static void *unw_hdr_alloc(unsigned long sz)
 366{
 367	return kmalloc(sz, GFP_KERNEL);
 368}
 369
 370static struct unwind_table *last_table;
 371
 372/* Must be called with module_mutex held. */
 373void *unwind_add_table(struct module *module, const void *table_start,
 374		       unsigned long table_size)
 375{
 376	struct unwind_table *table;
 377
 378	if (table_size <= 0)
 379		return NULL;
 380
 381	table = kmalloc(sizeof(*table), GFP_KERNEL);
 382	if (!table)
 383		return NULL;
 384
 385	init_unwind_table(table, module->name,
 386			  module->core_layout.base, module->core_layout.size,
 387			  module->init_layout.base, module->init_layout.size,
 388			  table_start, table_size,
 389			  NULL, 0);
 390
 391	init_unwind_hdr(table, unw_hdr_alloc);
 392
 393#ifdef UNWIND_DEBUG
 394	unw_debug("Table added for [%s] %lx %lx\n",
 395		module->name, table->core.pc, table->core.range);
 396#endif
 397	if (last_table)
 398		last_table->link = table;
 399	else
 400		root_table.link = table;
 401	last_table = table;
 402
 403	return table;
 404}
 405
 406struct unlink_table_info {
 407	struct unwind_table *table;
 408	int init_only;
 409};
 410
 411static int unlink_table(void *arg)
 412{
 413	struct unlink_table_info *info = arg;
 414	struct unwind_table *table = info->table, *prev;
 415
 416	for (prev = &root_table; prev->link && prev->link != table;
 417	     prev = prev->link)
 418		;
 419
 420	if (prev->link) {
 421		if (info->init_only) {
 422			table->init.pc = 0;
 423			table->init.range = 0;
 424			info->table = NULL;
 425		} else {
 426			prev->link = table->link;
 427			if (!prev->link)
 428				last_table = prev;
 429		}
 430	} else
 431		info->table = NULL;
 432
 433	return 0;
 434}
 435
 436/* Must be called with module_mutex held. */
 437void unwind_remove_table(void *handle, int init_only)
 438{
 439	struct unwind_table *table = handle;
 440	struct unlink_table_info info;
 441
 442	if (!table || table == &root_table)
 443		return;
 444
 445	if (init_only && table == last_table) {
 446		table->init.pc = 0;
 447		table->init.range = 0;
 448		return;
 449	}
 450
 451	info.table = table;
 452	info.init_only = init_only;
 453
 454	unlink_table(&info); /* XXX: SMP */
 455	kfree(table->header);
 456	kfree(table);
 457}
 458
 459#endif /* CONFIG_MODULES */
 460
 461static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
 462{
 463	const u8 *cur = *pcur;
 464	uleb128_t value;
 465	unsigned int shift;
 466
 467	for (shift = 0, value = 0; cur < end; shift += 7) {
 468		if (shift + 7 > 8 * sizeof(value)
 469		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
 470			cur = end + 1;
 471			break;
 472		}
 473		value |= (uleb128_t) (*cur & 0x7f) << shift;
 474		if (!(*cur++ & 0x80))
 475			break;
 476	}
 477	*pcur = cur;
 478
 479	return value;
 480}
 481
 482static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
 483{
 484	const u8 *cur = *pcur;
 485	sleb128_t value;
 486	unsigned int shift;
 487
 488	for (shift = 0, value = 0; cur < end; shift += 7) {
 489		if (shift + 7 > 8 * sizeof(value)
 490		    && (*cur & 0x7fU) >= (1U << (8 * sizeof(value) - shift))) {
 491			cur = end + 1;
 492			break;
 493		}
 494		value |= (sleb128_t) (*cur & 0x7f) << shift;
 495		if (!(*cur & 0x80)) {
 496			value |= -(*cur++ & 0x40) << shift;
 497			break;
 498		}
 499	}
 500	*pcur = cur;
 501
 502	return value;
 503}
 504
 505static const u32 *__cie_for_fde(const u32 *fde)
 506{
 507	const u32 *cie;
 508
 509	cie = fde + 1 - fde[1] / sizeof(*fde);
 510
 511	return cie;
 512}
 513
 514static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *table)
 515{
 516	const u32 *cie;
 517
 518	if (!*fde || (*fde & (sizeof(*fde) - 1)))
 519		return &bad_cie;
 520
 521	if (fde[1] == CIE_ID)
 522		return &not_fde;	/* this is a CIE */
 523
 524	if ((fde[1] & (sizeof(*fde) - 1)))
 525/* || fde[1] > (unsigned long)(fde + 1) - (unsigned long)table->address) */
 526		return NULL;	/* this is not a valid FDE */
 527
 528	cie = __cie_for_fde(fde);
 529
 530	if (*cie <= sizeof(*cie) + 4 || *cie >= fde[1] - sizeof(*fde)
 531	    || (*cie & (sizeof(*cie) - 1))
 532	    || (cie[1] != CIE_ID))
 533		return NULL;	/* this is not a (valid) CIE */
 534	return cie;
 535}
 536
 537static unsigned long read_pointer(const u8 **pLoc, const void *end,
 538				  signed ptrType)
 539{
 540	unsigned long value = 0;
 541	union {
 542		const u8 *p8;
 543		const u16 *p16u;
 544		const s16 *p16s;
 545		const u32 *p32u;
 546		const s32 *p32s;
 547		const unsigned long *pul;
 548	} ptr;
 549
 550	if (ptrType < 0 || ptrType == DW_EH_PE_omit)
 551		return 0;
 552	ptr.p8 = *pLoc;
 553	switch (ptrType & DW_EH_PE_FORM) {
 554	case DW_EH_PE_data2:
 555		if (end < (const void *)(ptr.p16u + 1))
 556			return 0;
 557		if (ptrType & DW_EH_PE_signed)
 558			value = get_unaligned((u16 *) ptr.p16s++);
 559		else
 560			value = get_unaligned((u16 *) ptr.p16u++);
 561		break;
 562	case DW_EH_PE_data4:
 563#ifdef CONFIG_64BIT
 564		if (end < (const void *)(ptr.p32u + 1))
 565			return 0;
 566		if (ptrType & DW_EH_PE_signed)
 567			value = get_unaligned(ptr.p32s++);
 568		else
 569			value = get_unaligned(ptr.p32u++);
 570		break;
 571	case DW_EH_PE_data8:
 572		BUILD_BUG_ON(sizeof(u64) != sizeof(value));
 573#else
 574		BUILD_BUG_ON(sizeof(u32) != sizeof(value));
 575#endif
 576		fallthrough;
 577	case DW_EH_PE_native:
 578		if (end < (const void *)(ptr.pul + 1))
 579			return 0;
 580		value = get_unaligned((unsigned long *)ptr.pul++);
 581		break;
 582	case DW_EH_PE_leb128:
 583		BUILD_BUG_ON(sizeof(uleb128_t) > sizeof(value));
 584		value = ptrType & DW_EH_PE_signed ? get_sleb128(&ptr.p8, end)
 585		    : get_uleb128(&ptr.p8, end);
 586		if ((const void *)ptr.p8 > end)
 587			return 0;
 588		break;
 589	default:
 590		return 0;
 591	}
 592	switch (ptrType & DW_EH_PE_ADJUST) {
 593	case DW_EH_PE_abs:
 594		break;
 595	case DW_EH_PE_pcrel:
 596		value += (unsigned long)*pLoc;
 597		break;
 598	default:
 599		return 0;
 600	}
 601	if ((ptrType & DW_EH_PE_indirect)
 602	    && __get_user(value, (unsigned long __user *)value))
 603		return 0;
 604	*pLoc = ptr.p8;
 605
 606	return value;
 607}
 608
 609static signed fde_pointer_type(const u32 *cie)
 610{
 611	const u8 *ptr = (const u8 *)(cie + 2);
 612	unsigned int version = *ptr;
 613
 614	if (*++ptr) {
 615		const char *aug;
 616		const u8 *end = (const u8 *)(cie + 1) + *cie;
 617		uleb128_t len;
 618
 619		/* check if augmentation size is first (and thus present) */
 620		if (*ptr != 'z')
 621			return -1;
 622
 623		/* check if augmentation string is nul-terminated */
 624		aug = (const void *)ptr;
 625		ptr = memchr(aug, 0, end - ptr);
 626		if (ptr == NULL)
 627			return -1;
 628
 629		++ptr;		/* skip terminator */
 630		get_uleb128(&ptr, end);	/* skip code alignment */
 631		get_sleb128(&ptr, end);	/* skip data alignment */
 632		/* skip return address column */
 633		version <= 1 ? (void) ++ptr : (void)get_uleb128(&ptr, end);
 634		len = get_uleb128(&ptr, end);	/* augmentation length */
 635
 636		if (ptr + len < ptr || ptr + len > end)
 637			return -1;
 638
 639		end = ptr + len;
 640		while (*++aug) {
 641			if (ptr >= end)
 642				return -1;
 643			switch (*aug) {
 644			case 'L':
 645				++ptr;
 646				break;
 647			case 'P':{
 648					signed ptrType = *ptr++;
 649
 650					if (!read_pointer(&ptr, end, ptrType)
 651					    || ptr > end)
 652						return -1;
 653				}
 654				break;
 655			case 'R':
 656				return *ptr;
 657			default:
 658				return -1;
 659			}
 660		}
 661	}
 662	return DW_EH_PE_native | DW_EH_PE_abs;
 663}
 664
 665static int advance_loc(unsigned long delta, struct unwind_state *state)
 666{
 667	state->loc += delta * state->codeAlign;
 668
 669	/* FIXME_Rajesh: Probably we are defining for the initial range as well;
 670	   return delta > 0;
 671	 */
 672	unw_debug("delta %3lu => loc 0x%lx: ", delta, state->loc);
 673	return 1;
 674}
 675
 676static void set_rule(uleb128_t reg, enum item_location where, uleb128_t value,
 677		     struct unwind_state *state)
 678{
 679	if (reg < ARRAY_SIZE(state->regs)) {
 680		state->regs[reg].where = where;
 681		state->regs[reg].value = value;
 682
 683#ifdef UNWIND_DEBUG
 684		unw_debug("r%lu: ", reg);
 685		switch (where) {
 686		case Nowhere:
 687			unw_debug("s ");
 688			break;
 689		case Memory:
 690			unw_debug("c(%lu) ", value);
 691			break;
 692		case Register:
 693			unw_debug("r(%lu) ", value);
 694			break;
 695		case Value:
 696			unw_debug("v(%lu) ", value);
 697			break;
 698		default:
 699			break;
 700		}
 701#endif
 702	}
 703}
 704
 705static int processCFI(const u8 *start, const u8 *end, unsigned long targetLoc,
 706		      signed ptrType, struct unwind_state *state)
 707{
 708	union {
 709		const u8 *p8;
 710		const u16 *p16;
 711		const u32 *p32;
 712	} ptr;
 713	int result = 1;
 714	u8 opcode;
 715
 716	if (start != state->cieStart) {
 717		state->loc = state->org;
 718		result =
 719		    processCFI(state->cieStart, state->cieEnd, 0, ptrType,
 720			       state);
 721		if (targetLoc == 0 && state->label == NULL)
 722			return result;
 723	}
 724	for (ptr.p8 = start; result && ptr.p8 < end;) {
 725		switch (*ptr.p8 >> 6) {
 726			uleb128_t value;
 727
 728		case 0:
 729			opcode = *ptr.p8++;
 730
 731			switch (opcode) {
 732			case DW_CFA_nop:
 733				unw_debug("cfa nop ");
 734				break;
 735			case DW_CFA_set_loc:
 736				state->loc = read_pointer(&ptr.p8, end,
 737							  ptrType);
 738				if (state->loc == 0)
 739					result = 0;
 740				unw_debug("cfa_set_loc: 0x%lx ", state->loc);
 741				break;
 742			case DW_CFA_advance_loc1:
 743				unw_debug("\ncfa advance loc1:");
 744				result = ptr.p8 < end
 745				    && advance_loc(*ptr.p8++, state);
 746				break;
 747			case DW_CFA_advance_loc2:
 748				value = *ptr.p8++;
 749				value += *ptr.p8++ << 8;
 750				unw_debug("\ncfa advance loc2:");
 751				result = ptr.p8 <= end + 2
 752				    /* && advance_loc(*ptr.p16++, state); */
 753				    && advance_loc(value, state);
 754				break;
 755			case DW_CFA_advance_loc4:
 756				unw_debug("\ncfa advance loc4:");
 757				result = ptr.p8 <= end + 4
 758				    && advance_loc(*ptr.p32++, state);
 759				break;
 760			case DW_CFA_offset_extended:
 761				value = get_uleb128(&ptr.p8, end);
 762				unw_debug("cfa_offset_extended: ");
 763				set_rule(value, Memory,
 764					 get_uleb128(&ptr.p8, end), state);
 765				break;
 766			case DW_CFA_val_offset:
 767				value = get_uleb128(&ptr.p8, end);
 768				set_rule(value, Value,
 769					 get_uleb128(&ptr.p8, end), state);
 770				break;
 771			case DW_CFA_offset_extended_sf:
 772				value = get_uleb128(&ptr.p8, end);
 773				set_rule(value, Memory,
 774					 get_sleb128(&ptr.p8, end), state);
 775				break;
 776			case DW_CFA_val_offset_sf:
 777				value = get_uleb128(&ptr.p8, end);
 778				set_rule(value, Value,
 779					 get_sleb128(&ptr.p8, end), state);
 780				break;
 781			case DW_CFA_restore_extended:
 782				unw_debug("cfa_restore_extended: ");
 783			case DW_CFA_undefined:
 784				unw_debug("cfa_undefined: ");
 785			case DW_CFA_same_value:
 786				unw_debug("cfa_same_value: ");
 787				set_rule(get_uleb128(&ptr.p8, end), Nowhere, 0,
 788					 state);
 789				break;
 790			case DW_CFA_register:
 791				unw_debug("cfa_register: ");
 792				value = get_uleb128(&ptr.p8, end);
 793				set_rule(value,
 794					 Register,
 795					 get_uleb128(&ptr.p8, end), state);
 796				break;
 797			case DW_CFA_remember_state:
 798				unw_debug("cfa_remember_state: ");
 799				if (ptr.p8 == state->label) {
 800					state->label = NULL;
 801					return 1;
 802				}
 803				if (state->stackDepth >= MAX_STACK_DEPTH)
 804					return 0;
 805				state->stack[state->stackDepth++] = ptr.p8;
 806				break;
 807			case DW_CFA_restore_state:
 808				unw_debug("cfa_restore_state: ");
 809				if (state->stackDepth) {
 810					const uleb128_t loc = state->loc;
 811					const u8 *label = state->label;
 812
 813					state->label =
 814					    state->stack[state->stackDepth - 1];
 815					memcpy(&state->cfa, &badCFA,
 816					       sizeof(state->cfa));
 817					memset(state->regs, 0,
 818					       sizeof(state->regs));
 819					state->stackDepth = 0;
 820					result =
 821					    processCFI(start, end, 0, ptrType,
 822						       state);
 823					state->loc = loc;
 824					state->label = label;
 825				} else
 826					return 0;
 827				break;
 828			case DW_CFA_def_cfa:
 829				state->cfa.reg = get_uleb128(&ptr.p8, end);
 830				unw_debug("cfa_def_cfa: r%lu ", state->cfa.reg);
 831				fallthrough;
 832			case DW_CFA_def_cfa_offset:
 833				state->cfa.offs = get_uleb128(&ptr.p8, end);
 834				unw_debug("cfa_def_cfa_offset: 0x%lx ",
 835					  state->cfa.offs);
 836				break;
 837			case DW_CFA_def_cfa_sf:
 838				state->cfa.reg = get_uleb128(&ptr.p8, end);
 839				fallthrough;
 840			case DW_CFA_def_cfa_offset_sf:
 841				state->cfa.offs = get_sleb128(&ptr.p8, end)
 842				    * state->dataAlign;
 843				break;
 844			case DW_CFA_def_cfa_register:
 845				unw_debug("cfa_def_cfa_register: ");
 846				state->cfa.reg = get_uleb128(&ptr.p8, end);
 847				break;
 848				/*todo case DW_CFA_def_cfa_expression: */
 849				/*todo case DW_CFA_expression: */
 850				/*todo case DW_CFA_val_expression: */
 851			case DW_CFA_GNU_args_size:
 852				get_uleb128(&ptr.p8, end);
 853				break;
 854			case DW_CFA_GNU_negative_offset_extended:
 855				value = get_uleb128(&ptr.p8, end);
 856				set_rule(value,
 857					 Memory,
 858					 (uleb128_t) 0 - get_uleb128(&ptr.p8,
 859								     end),
 860					 state);
 861				break;
 862			case DW_CFA_GNU_window_save:
 863			default:
 864				unw_debug("UNKNOWN OPCODE 0x%x\n", opcode);
 865				result = 0;
 866				break;
 867			}
 868			break;
 869		case 1:
 870			unw_debug("\ncfa_adv_loc: ");
 871			result = advance_loc(*ptr.p8++ & 0x3f, state);
 872			break;
 873		case 2:
 874			unw_debug("cfa_offset: ");
 875			value = *ptr.p8++ & 0x3f;
 876			set_rule(value, Memory, get_uleb128(&ptr.p8, end),
 877				 state);
 878			break;
 879		case 3:
 880			unw_debug("cfa_restore: ");
 881			set_rule(*ptr.p8++ & 0x3f, Nowhere, 0, state);
 882			break;
 883		}
 884
 885		if (ptr.p8 > end)
 886			result = 0;
 887		if (result && targetLoc != 0 && targetLoc < state->loc)
 888			return 1;
 889	}
 890
 891	return result && ptr.p8 == end && (targetLoc == 0 || (
 892		/*todo While in theory this should apply, gcc in practice omits
 893		  everything past the function prolog, and hence the location
 894		  never reaches the end of the function.
 895		targetLoc < state->loc && */  state->label == NULL));
 896}
 897
 898/* Unwind to previous to frame.  Returns 0 if successful, negative
 899 * number in case of an error. */
 900int arc_unwind(struct unwind_frame_info *frame)
 901{
 902#define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
 903	const u32 *fde = NULL, *cie = NULL;
 904	const u8 *ptr = NULL, *end = NULL;
 905	unsigned long pc = UNW_PC(frame) - frame->call_frame;
 906	unsigned long startLoc = 0, endLoc = 0, cfa;
 907	unsigned int i;
 908	signed ptrType = -1;
 909	uleb128_t retAddrReg = 0;
 910	const struct unwind_table *table;
 911	struct unwind_state state;
 912	unsigned long *fptr;
 913	unsigned long addr;
 914
 915	unw_debug("\n\nUNWIND FRAME:\n");
 916	unw_debug("PC: 0x%lx BLINK: 0x%lx, SP: 0x%lx, FP: 0x%x\n",
 917		  UNW_PC(frame), UNW_BLINK(frame), UNW_SP(frame),
 918		  UNW_FP(frame));
 919
 920	if (UNW_PC(frame) == 0)
 921		return -EINVAL;
 922
 923#ifdef UNWIND_DEBUG
 924	{
 925		unsigned long *sptr = (unsigned long *)UNW_SP(frame);
 926		unw_debug("\nStack Dump:\n");
 927		for (i = 0; i < 20; i++, sptr++)
 928			unw_debug("0x%p:  0x%lx\n", sptr, *sptr);
 929		unw_debug("\n");
 930	}
 931#endif
 932
 933	table = find_table(pc);
 934	if (table != NULL
 935	    && !(table->size & (sizeof(*fde) - 1))) {
 936		const u8 *hdr = table->header;
 937		unsigned long tableSize;
 938
 939		smp_rmb();
 940		if (hdr && hdr[0] == 1) {
 941			switch (hdr[3] & DW_EH_PE_FORM) {
 942			case DW_EH_PE_native:
 943				tableSize = sizeof(unsigned long);
 944				break;
 945			case DW_EH_PE_data2:
 946				tableSize = 2;
 947				break;
 948			case DW_EH_PE_data4:
 949				tableSize = 4;
 950				break;
 951			case DW_EH_PE_data8:
 952				tableSize = 8;
 953				break;
 954			default:
 955				tableSize = 0;
 956				break;
 957			}
 958			ptr = hdr + 4;
 959			end = hdr + table->hdrsz;
 960			if (tableSize && read_pointer(&ptr, end, hdr[1])
 961			    == (unsigned long)table->address
 962			    && (i = read_pointer(&ptr, end, hdr[2])) > 0
 963			    && i == (end - ptr) / (2 * tableSize)
 964			    && !((end - ptr) % (2 * tableSize))) {
 965				do {
 966					const u8 *cur =
 967					    ptr + (i / 2) * (2 * tableSize);
 968
 969					startLoc = read_pointer(&cur,
 970								cur + tableSize,
 971								hdr[3]);
 972					if (pc < startLoc)
 973						i /= 2;
 974					else {
 975						ptr = cur - tableSize;
 976						i = (i + 1) / 2;
 977					}
 978				} while (startLoc && i > 1);
 979				if (i == 1
 980				    && (startLoc = read_pointer(&ptr,
 981								ptr + tableSize,
 982								hdr[3])) != 0
 983				    && pc >= startLoc)
 984					fde = (void *)read_pointer(&ptr,
 985								   ptr +
 986								   tableSize,
 987								   hdr[3]);
 988			}
 989		}
 990
 991		if (fde != NULL) {
 992			cie = cie_for_fde(fde, table);
 993			ptr = (const u8 *)(fde + 2);
 994			if (cie != NULL
 995			    && cie != &bad_cie
 996			    && cie != &not_fde
 997			    && (ptrType = fde_pointer_type(cie)) >= 0
 998			    && read_pointer(&ptr,
 999					    (const u8 *)(fde + 1) + *fde,
1000					    ptrType) == startLoc) {
1001				if (!(ptrType & DW_EH_PE_indirect))
1002					ptrType &=
1003					    DW_EH_PE_FORM | DW_EH_PE_signed;
1004				endLoc =
1005				    startLoc + read_pointer(&ptr,
1006							    (const u8 *)(fde +
1007									 1) +
1008							    *fde, ptrType);
1009				if (pc >= endLoc) {
1010					fde = NULL;
1011					cie = NULL;
1012				}
1013			} else {
1014				fde = NULL;
1015				cie = NULL;
1016			}
1017		}
1018	}
1019	if (cie != NULL) {
1020		memset(&state, 0, sizeof(state));
1021		state.cieEnd = ptr;	/* keep here temporarily */
1022		ptr = (const u8 *)(cie + 2);
1023		end = (const u8 *)(cie + 1) + *cie;
1024		frame->call_frame = 1;
1025		if (*++ptr) {
1026			/* check if augmentation size is first (thus present) */
1027			if (*ptr == 'z') {
1028				while (++ptr < end && *ptr) {
1029					switch (*ptr) {
1030					/* chk for ignorable or already handled
1031					 * nul-terminated augmentation string */
1032					case 'L':
1033					case 'P':
1034					case 'R':
1035						continue;
1036					case 'S':
1037						frame->call_frame = 0;
1038						continue;
1039					default:
1040						break;
1041					}
1042					break;
1043				}
1044			}
1045			if (ptr >= end || *ptr)
1046				cie = NULL;
1047		}
1048		++ptr;
1049	}
1050	if (cie != NULL) {
1051		/* get code alignment factor */
1052		state.codeAlign = get_uleb128(&ptr, end);
1053		/* get data alignment factor */
1054		state.dataAlign = get_sleb128(&ptr, end);
1055		if (state.codeAlign == 0 || state.dataAlign == 0 || ptr >= end)
1056			cie = NULL;
1057		else {
1058			retAddrReg =
1059			    state.version <= 1 ? *ptr++ : get_uleb128(&ptr,
1060								      end);
1061			unw_debug("CIE Frame Info:\n");
1062			unw_debug("return Address register 0x%lx\n",
1063				  retAddrReg);
1064			unw_debug("data Align: %ld\n", state.dataAlign);
1065			unw_debug("code Align: %lu\n", state.codeAlign);
1066			/* skip augmentation */
1067			if (((const char *)(cie + 2))[1] == 'z') {
1068				uleb128_t augSize = get_uleb128(&ptr, end);
1069
1070				ptr += augSize;
1071			}
1072			if (ptr > end || retAddrReg >= ARRAY_SIZE(reg_info)
1073			    || REG_INVALID(retAddrReg)
1074			    || reg_info[retAddrReg].width !=
1075			    sizeof(unsigned long))
1076				cie = NULL;
1077		}
1078	}
1079	if (cie != NULL) {
1080		state.cieStart = ptr;
1081		ptr = state.cieEnd;
1082		state.cieEnd = end;
1083		end = (const u8 *)(fde + 1) + *fde;
1084		/* skip augmentation */
1085		if (((const char *)(cie + 2))[1] == 'z') {
1086			uleb128_t augSize = get_uleb128(&ptr, end);
1087
1088			if ((ptr += augSize) > end)
1089				fde = NULL;
1090		}
1091	}
1092	if (cie == NULL || fde == NULL) {
1093#ifdef CONFIG_FRAME_POINTER
1094		unsigned long top, bottom;
1095
1096		top = STACK_TOP_UNW(frame->task);
1097		bottom = STACK_BOTTOM_UNW(frame->task);
1098#if FRAME_RETADDR_OFFSET < 0
1099		if (UNW_SP(frame) < top && UNW_FP(frame) <= UNW_SP(frame)
1100		    && bottom < UNW_FP(frame)
1101#else
1102		if (UNW_SP(frame) > top && UNW_FP(frame) >= UNW_SP(frame)
1103		    && bottom > UNW_FP(frame)
1104#endif
1105		    && !((UNW_SP(frame) | UNW_FP(frame))
1106			 & (sizeof(unsigned long) - 1))) {
1107			unsigned long link;
1108
1109			if (!__get_user(link, (unsigned long *)
1110					(UNW_FP(frame) + FRAME_LINK_OFFSET))
1111#if FRAME_RETADDR_OFFSET < 0
1112			    && link > bottom && link < UNW_FP(frame)
1113#else
1114			    && link > UNW_FP(frame) && link < bottom
1115#endif
1116			    && !(link & (sizeof(link) - 1))
1117			    && !__get_user(UNW_PC(frame),
1118					   (unsigned long *)(UNW_FP(frame)
1119						+ FRAME_RETADDR_OFFSET)))
1120			{
1121				UNW_SP(frame) =
1122				    UNW_FP(frame) + FRAME_RETADDR_OFFSET
1123#if FRAME_RETADDR_OFFSET < 0
1124				    -
1125#else
1126				    +
1127#endif
1128				    sizeof(UNW_PC(frame));
1129				UNW_FP(frame) = link;
1130				return 0;
1131			}
1132		}
1133#endif
1134		return -ENXIO;
1135	}
1136	state.org = startLoc;
1137	memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
1138
1139	unw_debug("\nProcess instructions\n");
1140
1141	/* process instructions
1142	 * For ARC, we optimize by having blink(retAddrReg) with
1143	 * the sameValue in the leaf function, so we should not check
1144	 * state.regs[retAddrReg].where == Nowhere
1145	 */
1146	if (!processCFI(ptr, end, pc, ptrType, &state)
1147	    || state.loc > endLoc
1148/*	   || state.regs[retAddrReg].where == Nowhere */
1149	    || state.cfa.reg >= ARRAY_SIZE(reg_info)
1150	    || reg_info[state.cfa.reg].width != sizeof(unsigned long)
1151	    || state.cfa.offs % sizeof(unsigned long))
1152		return -EIO;
1153
1154#ifdef UNWIND_DEBUG
1155	unw_debug("\n");
1156
1157	unw_debug("\nRegister State Based on the rules parsed from FDE:\n");
1158	for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1159
1160		if (REG_INVALID(i))
1161			continue;
1162
1163		switch (state.regs[i].where) {
1164		case Nowhere:
1165			break;
1166		case Memory:
1167			unw_debug(" r%d: c(%lu),", i, state.regs[i].value);
1168			break;
1169		case Register:
1170			unw_debug(" r%d: r(%lu),", i, state.regs[i].value);
1171			break;
1172		case Value:
1173			unw_debug(" r%d: v(%lu),", i, state.regs[i].value);
1174			break;
1175		}
1176	}
1177
1178	unw_debug("\n");
1179#endif
1180
1181	/* update frame */
 
1182	if (frame->call_frame
1183	    && !UNW_DEFAULT_RA(state.regs[retAddrReg], state.dataAlign))
1184		frame->call_frame = 0;
 
1185	cfa = FRAME_REG(state.cfa.reg, unsigned long) + state.cfa.offs;
1186	startLoc = min_t(unsigned long, UNW_SP(frame), cfa);
1187	endLoc = max_t(unsigned long, UNW_SP(frame), cfa);
1188	if (STACK_LIMIT(startLoc) != STACK_LIMIT(endLoc)) {
1189		startLoc = min(STACK_LIMIT(cfa), cfa);
1190		endLoc = max(STACK_LIMIT(cfa), cfa);
1191	}
1192
1193	unw_debug("\nCFA reg: 0x%lx, offset: 0x%lx =>  0x%lx\n",
1194		  state.cfa.reg, state.cfa.offs, cfa);
1195
1196	for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1197		if (REG_INVALID(i)) {
1198			if (state.regs[i].where == Nowhere)
1199				continue;
1200			return -EIO;
1201		}
1202		switch (state.regs[i].where) {
1203		default:
1204			break;
1205		case Register:
1206			if (state.regs[i].value >= ARRAY_SIZE(reg_info)
1207			    || REG_INVALID(state.regs[i].value)
1208			    || reg_info[i].width >
1209			    reg_info[state.regs[i].value].width)
1210				return -EIO;
1211			switch (reg_info[state.regs[i].value].width) {
1212			case sizeof(u8):
1213				state.regs[i].value =
1214				FRAME_REG(state.regs[i].value, const u8);
1215				break;
1216			case sizeof(u16):
1217				state.regs[i].value =
1218				FRAME_REG(state.regs[i].value, const u16);
1219				break;
1220			case sizeof(u32):
1221				state.regs[i].value =
1222				FRAME_REG(state.regs[i].value, const u32);
1223				break;
1224#ifdef CONFIG_64BIT
1225			case sizeof(u64):
1226				state.regs[i].value =
1227				FRAME_REG(state.regs[i].value, const u64);
1228				break;
1229#endif
1230			default:
1231				return -EIO;
1232			}
1233			break;
1234		}
1235	}
1236
1237	unw_debug("\nRegister state after evaluation with realtime Stack:\n");
1238	fptr = (unsigned long *)(&frame->regs);
1239	for (i = 0; i < ARRAY_SIZE(state.regs); ++i, fptr++) {
1240
1241		if (REG_INVALID(i))
1242			continue;
1243		switch (state.regs[i].where) {
1244		case Nowhere:
1245			if (reg_info[i].width != sizeof(UNW_SP(frame))
1246			    || &FRAME_REG(i, __typeof__(UNW_SP(frame)))
1247			    != &UNW_SP(frame))
1248				continue;
1249			UNW_SP(frame) = cfa;
1250			break;
1251		case Register:
1252			switch (reg_info[i].width) {
1253			case sizeof(u8):
1254				FRAME_REG(i, u8) = state.regs[i].value;
1255				break;
1256			case sizeof(u16):
1257				FRAME_REG(i, u16) = state.regs[i].value;
1258				break;
1259			case sizeof(u32):
1260				FRAME_REG(i, u32) = state.regs[i].value;
1261				break;
1262#ifdef CONFIG_64BIT
1263			case sizeof(u64):
1264				FRAME_REG(i, u64) = state.regs[i].value;
1265				break;
1266#endif
1267			default:
1268				return -EIO;
1269			}
1270			break;
1271		case Value:
1272			if (reg_info[i].width != sizeof(unsigned long))
1273				return -EIO;
1274			FRAME_REG(i, unsigned long) = cfa + state.regs[i].value
1275			    * state.dataAlign;
1276			break;
1277		case Memory:
1278			addr = cfa + state.regs[i].value * state.dataAlign;
1279
1280			if ((state.regs[i].value * state.dataAlign)
1281			    % sizeof(unsigned long)
1282			    || addr < startLoc
1283			    || addr + sizeof(unsigned long) < addr
1284			    || addr + sizeof(unsigned long) > endLoc)
1285					return -EIO;
1286
1287			switch (reg_info[i].width) {
1288			case sizeof(u8):
1289				__get_user(FRAME_REG(i, u8),
1290					   (u8 __user *)addr);
1291				break;
1292			case sizeof(u16):
1293				__get_user(FRAME_REG(i, u16),
1294					   (u16 __user *)addr);
1295				break;
1296			case sizeof(u32):
1297				__get_user(FRAME_REG(i, u32),
1298					   (u32 __user *)addr);
1299				break;
1300#ifdef CONFIG_64BIT
1301			case sizeof(u64):
1302				__get_user(FRAME_REG(i, u64),
1303					   (u64 __user *)addr);
1304				break;
1305#endif
1306			default:
1307				return -EIO;
1308			}
1309
1310			break;
1311		}
1312		unw_debug("r%d: 0x%lx ", i, *fptr);
1313	}
1314
1315	return 0;
1316#undef FRAME_REG
1317}
1318EXPORT_SYMBOL(arc_unwind);