1 // SPDX-License-Identifier: GPL-2.0-only
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>
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.
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>
25 extern char __start_unwind[], __end_unwind[];
26 /* extern const u8 __start_unwind_hdr[], __end_unwind_hdr[];*/
28 /* #define UNWIND_DEBUG */
32 #define unw_debug(fmt, ...) \
35 pr_info(fmt, ##__VA_ARGS__); \
38 #define unw_debug(fmt, ...)
41 #define MAX_STACK_DEPTH 8
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) \
50 #define PTREGS_INFO(f) EXTRA_INFO(regs.f)
53 unsigned offs:BITS_PER_LONG / 2;
54 unsigned width:BITS_PER_LONG / 2;
62 #define REG_INVALID(r) (reg_info[r].width == 0)
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
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
113 typedef unsigned long uleb128_t;
114 typedef signed long sleb128_t;
116 static struct unwind_table {
123 const unsigned char *header;
125 struct unwind_table *link;
139 struct unwind_state {
141 const u8 *cieStart, *cieEnd;
147 struct unwind_item regs[ARRAY_SIZE(reg_info)];
148 unsigned stackDepth:8;
151 const u8 *stack[MAX_STACK_DEPTH];
154 static const struct cfa badCFA = { ARRAY_SIZE(reg_info), 1 };
156 static struct unwind_table *find_table(unsigned long pc)
158 struct unwind_table *table;
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))
170 static unsigned long read_pointer(const u8 **pLoc,
171 const void *end, signed ptrType);
172 static void init_unwind_hdr(struct unwind_table *table,
173 void *(*alloc) (unsigned long));
176 * wrappers for header alloc (vs. calling one vs. other at call site)
177 * to elide section mismatches warnings
179 static void *__init unw_hdr_alloc_early(unsigned long sz)
181 return memblock_alloc_from(sz, sizeof(unsigned int), MAX_DMA_ADDRESS);
184 static 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)
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. */
202 || header_start[0] != 1
203 || (void *)read_pointer(&ptr, end, header_start[1])
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)
210 table->hdrsz = header_size;
212 table->header = header_start;
217 void __init arc_unwind_init(void)
219 init_unwind_table(&root_table, "kernel", _text, _end - _text, NULL, 0,
220 __start_unwind, __end_unwind - __start_unwind,
222 /*__start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);*/
224 init_unwind_hdr(&root_table, unw_hdr_alloc_early);
227 static const u32 bad_cie, not_fde;
228 static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *);
229 static const u32 *__cie_for_fde(const u32 *fde);
230 static signed fde_pointer_type(const u32 *cie);
232 struct eh_frame_hdr_table_entry {
233 unsigned long start, fde;
236 static int cmp_eh_frame_hdr_table_entries(const void *p1, const void *p2)
238 const struct eh_frame_hdr_table_entry *e1 = p1;
239 const struct eh_frame_hdr_table_entry *e2 = p2;
241 return (e1->start > e2->start) - (e1->start < e2->start);
244 static void swap_eh_frame_hdr_table_entries(void *p1, void *p2, int size)
246 struct eh_frame_hdr_table_entry *e1 = p1;
247 struct eh_frame_hdr_table_entry *e2 = p2;
251 e1->start = e2->start;
258 static void init_unwind_hdr(struct unwind_table *table,
259 void *(*alloc) (unsigned long))
262 unsigned long tableSize = table->size, hdrSize;
270 unsigned long eh_frame_ptr;
271 unsigned int fde_count;
272 struct eh_frame_hdr_table_entry table[];
273 } __attribute__ ((__packed__)) *header;
279 pr_warn(".eh_frame_hdr for '%s' present but unusable\n",
282 if (tableSize & (sizeof(*fde) - 1))
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);
293 if (cie == NULL || cie == &bad_cie)
295 ptrType = fde_pointer_type(cie);
299 ptr = (const u8 *)(fde + 2);
300 if (!read_pointer(&ptr, (const u8 *)(fde + 1) + *fde,
302 /* FIXME_Rajesh We have 4 instances of null addresses
303 * instead of the initial loc addr
306 WARN(1, "unwinder: FDE->initial_location NULL %p\n",
307 (const u8 *)(fde + 1) + *fde);
315 hdrSize = 4 + sizeof(unsigned long) + sizeof(unsigned int)
316 + 2 * n * sizeof(unsigned long);
318 header = alloc(hdrSize);
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;
331 BUILD_BUG_ON(offsetof(typeof(*header), table)
332 % __alignof(typeof(*header->table)));
333 for (fde = table->address, tableSize = table->size, n = 0;
335 tableSize -= sizeof(*fde) + *fde, fde += 1 + *fde / sizeof(*fde)) {
336 const u32 *cie = __cie_for_fde(fde);
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) +
344 fde_pointer_type(cie));
345 header->table[n].fde = (unsigned long)fde;
348 WARN_ON(n != header->fde_count);
352 sizeof(*header->table),
353 cmp_eh_frame_hdr_table_entries, swap_eh_frame_hdr_table_entries);
355 table->hdrsz = hdrSize;
357 table->header = (const void *)header;
361 panic("Attention !!! Dwarf FDE parsing errors\n");
364 #ifdef CONFIG_MODULES
365 static void *unw_hdr_alloc(unsigned long sz)
367 return kmalloc(sz, GFP_KERNEL);
370 static struct unwind_table *last_table;
372 /* Must be called with module_mutex held. */
373 void *unwind_add_table(struct module *module, const void *table_start,
374 unsigned long table_size)
376 struct unwind_table *table;
381 table = kmalloc(sizeof(*table), GFP_KERNEL);
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,
391 init_unwind_hdr(table, unw_hdr_alloc);
394 unw_debug("Table added for [%s] %lx %lx\n",
395 module->name, table->core.pc, table->core.range);
398 last_table->link = table;
400 root_table.link = table;
406 struct unlink_table_info {
407 struct unwind_table *table;
411 static int unlink_table(void *arg)
413 struct unlink_table_info *info = arg;
414 struct unwind_table *table = info->table, *prev;
416 for (prev = &root_table; prev->link && prev->link != table;
421 if (info->init_only) {
423 table->init.range = 0;
426 prev->link = table->link;
436 /* Must be called with module_mutex held. */
437 void unwind_remove_table(void *handle, int init_only)
439 struct unwind_table *table = handle;
440 struct unlink_table_info info;
442 if (!table || table == &root_table)
445 if (init_only && table == last_table) {
447 table->init.range = 0;
452 info.init_only = init_only;
454 unlink_table(&info); /* XXX: SMP */
455 kfree(table->header);
459 #endif /* CONFIG_MODULES */
461 static uleb128_t get_uleb128(const u8 **pcur, const u8 *end)
463 const u8 *cur = *pcur;
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))) {
473 value |= (uleb128_t) (*cur & 0x7f) << shift;
474 if (!(*cur++ & 0x80))
482 static sleb128_t get_sleb128(const u8 **pcur, const u8 *end)
484 const u8 *cur = *pcur;
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))) {
494 value |= (sleb128_t) (*cur & 0x7f) << shift;
495 if (!(*cur & 0x80)) {
496 value |= -(*cur++ & 0x40) << shift;
505 static const u32 *__cie_for_fde(const u32 *fde)
509 cie = fde + 1 - fde[1] / sizeof(*fde);
514 static const u32 *cie_for_fde(const u32 *fde, const struct unwind_table *table)
518 if (!*fde || (*fde & (sizeof(*fde) - 1)))
521 if (fde[1] == CIE_ID)
522 return ¬_fde; /* this is a CIE */
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 */
528 cie = __cie_for_fde(fde);
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 */
537 static unsigned long read_pointer(const u8 **pLoc, const void *end,
540 unsigned long value = 0;
547 const unsigned long *pul;
550 if (ptrType < 0 || ptrType == DW_EH_PE_omit)
553 switch (ptrType & DW_EH_PE_FORM) {
555 if (end < (const void *)(ptr.p16u + 1))
557 if (ptrType & DW_EH_PE_signed)
558 value = get_unaligned((u16 *) ptr.p16s++);
560 value = get_unaligned((u16 *) ptr.p16u++);
564 if (end < (const void *)(ptr.p32u + 1))
566 if (ptrType & DW_EH_PE_signed)
567 value = get_unaligned(ptr.p32s++);
569 value = get_unaligned(ptr.p32u++);
572 BUILD_BUG_ON(sizeof(u64) != sizeof(value));
574 BUILD_BUG_ON(sizeof(u32) != sizeof(value));
577 case DW_EH_PE_native:
578 if (end < (const void *)(ptr.pul + 1))
580 value = get_unaligned((unsigned long *)ptr.pul++);
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)
592 switch (ptrType & DW_EH_PE_ADJUST) {
596 value += (unsigned long)*pLoc;
601 if ((ptrType & DW_EH_PE_indirect)
602 && __get_user(value, (unsigned long __user *)value))
609 static signed fde_pointer_type(const u32 *cie)
611 const u8 *ptr = (const u8 *)(cie + 2);
612 unsigned version = *ptr;
616 const u8 *end = (const u8 *)(cie + 1) + *cie;
619 /* check if augmentation size is first (and thus present) */
623 /* check if augmentation string is nul-terminated */
624 aug = (const void *)ptr;
625 ptr = memchr(aug, 0, end - ptr);
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 */
636 if (ptr + len < ptr || ptr + len > end)
648 signed ptrType = *ptr++;
650 if (!read_pointer(&ptr, end, ptrType)
662 return DW_EH_PE_native | DW_EH_PE_abs;
665 static int advance_loc(unsigned long delta, struct unwind_state *state)
667 state->loc += delta * state->codeAlign;
669 /* FIXME_Rajesh: Probably we are defining for the initial range as well;
672 unw_debug("delta %3lu => loc 0x%lx: ", delta, state->loc);
676 static void set_rule(uleb128_t reg, enum item_location where, uleb128_t value,
677 struct unwind_state *state)
679 if (reg < ARRAY_SIZE(state->regs)) {
680 state->regs[reg].where = where;
681 state->regs[reg].value = value;
684 unw_debug("r%lu: ", reg);
690 unw_debug("c(%lu) ", value);
693 unw_debug("r(%lu) ", value);
696 unw_debug("v(%lu) ", value);
705 static int processCFI(const u8 *start, const u8 *end, unsigned long targetLoc,
706 signed ptrType, struct unwind_state *state)
716 if (start != state->cieStart) {
717 state->loc = state->org;
719 processCFI(state->cieStart, state->cieEnd, 0, ptrType,
721 if (targetLoc == 0 && state->label == NULL)
724 for (ptr.p8 = start; result && ptr.p8 < end;) {
725 switch (*ptr.p8 >> 6) {
733 unw_debug("cfa nop ");
736 state->loc = read_pointer(&ptr.p8, end,
740 unw_debug("cfa_set_loc: 0x%lx ", state->loc);
742 case DW_CFA_advance_loc1:
743 unw_debug("\ncfa advance loc1:");
744 result = ptr.p8 < end
745 && advance_loc(*ptr.p8++, state);
747 case DW_CFA_advance_loc2:
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);
755 case DW_CFA_advance_loc4:
756 unw_debug("\ncfa advance loc4:");
757 result = ptr.p8 <= end + 4
758 && advance_loc(*ptr.p32++, state);
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);
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);
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);
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);
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,
790 case DW_CFA_register:
791 unw_debug("cfa_register: ");
792 value = get_uleb128(&ptr.p8, end);
795 get_uleb128(&ptr.p8, end), state);
797 case DW_CFA_remember_state:
798 unw_debug("cfa_remember_state: ");
799 if (ptr.p8 == state->label) {
803 if (state->stackDepth >= MAX_STACK_DEPTH)
805 state->stack[state->stackDepth++] = ptr.p8;
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;
814 state->stack[state->stackDepth - 1];
815 memcpy(&state->cfa, &badCFA,
817 memset(state->regs, 0,
818 sizeof(state->regs));
819 state->stackDepth = 0;
821 processCFI(start, end, 0, ptrType,
824 state->label = label;
829 state->cfa.reg = get_uleb128(&ptr.p8, end);
830 unw_debug("cfa_def_cfa: r%lu ", state->cfa.reg);
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 ",
837 case DW_CFA_def_cfa_sf:
838 state->cfa.reg = get_uleb128(&ptr.p8, end);
840 case DW_CFA_def_cfa_offset_sf:
841 state->cfa.offs = get_sleb128(&ptr.p8, end)
844 case DW_CFA_def_cfa_register:
845 unw_debug("cfa_def_cfa_register: ");
846 state->cfa.reg = get_uleb128(&ptr.p8, end);
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);
854 case DW_CFA_GNU_negative_offset_extended:
855 value = get_uleb128(&ptr.p8, end);
858 (uleb128_t) 0 - get_uleb128(&ptr.p8,
862 case DW_CFA_GNU_window_save:
864 unw_debug("UNKNOWN OPCODE 0x%x\n", opcode);
870 unw_debug("\ncfa_adv_loc: ");
871 result = advance_loc(*ptr.p8++ & 0x3f, state);
874 unw_debug("cfa_offset: ");
875 value = *ptr.p8++ & 0x3f;
876 set_rule(value, Memory, get_uleb128(&ptr.p8, end),
880 unw_debug("cfa_restore: ");
881 set_rule(*ptr.p8++ & 0x3f, Nowhere, 0, state);
887 if (result && targetLoc != 0 && targetLoc < state->loc)
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));
898 /* Unwind to previous to frame. Returns 0 if successful, negative
899 * number in case of an error. */
900 int arc_unwind(struct unwind_frame_info *frame)
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;
909 uleb128_t retAddrReg = 0;
910 const struct unwind_table *table;
911 struct unwind_state state;
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),
920 if (UNW_PC(frame) == 0)
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);
933 table = find_table(pc);
935 && !(table->size & (sizeof(*fde) - 1))) {
936 const u8 *hdr = table->header;
937 unsigned long tableSize;
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);
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))) {
967 ptr + (i / 2) * (2 * tableSize);
969 startLoc = read_pointer(&cur,
975 ptr = cur - tableSize;
978 } while (startLoc && i > 1);
980 && (startLoc = read_pointer(&ptr,
984 fde = (void *)read_pointer(&ptr,
992 cie = cie_for_fde(fde, table);
993 ptr = (const u8 *)(fde + 2);
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))
1003 DW_EH_PE_FORM | DW_EH_PE_signed;
1005 startLoc + read_pointer(&ptr,
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;
1026 /* check if augmentation size is first (thus present) */
1028 while (++ptr < end && *ptr) {
1030 /* chk for ignorable or already handled
1031 * nul-terminated augmentation string */
1037 frame->call_frame = 0;
1045 if (ptr >= end || *ptr)
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)
1059 state.version <= 1 ? *ptr++ : get_uleb128(&ptr,
1061 unw_debug("CIE Frame Info:\n");
1062 unw_debug("return Address register 0x%lx\n",
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);
1072 if (ptr > end || retAddrReg >= ARRAY_SIZE(reg_info)
1073 || REG_INVALID(retAddrReg)
1074 || reg_info[retAddrReg].width !=
1075 sizeof(unsigned long))
1080 state.cieStart = ptr;
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);
1088 if ((ptr += augSize) > end)
1092 if (cie == NULL || fde == NULL) {
1093 #ifdef CONFIG_FRAME_POINTER
1094 unsigned long top, bottom;
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)
1102 if (UNW_SP(frame) > top && UNW_FP(frame) >= UNW_SP(frame)
1103 && bottom > UNW_FP(frame)
1105 && !((UNW_SP(frame) | UNW_FP(frame))
1106 & (sizeof(unsigned long) - 1))) {
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)
1114 && link > UNW_FP(frame) && link < bottom
1116 && !(link & (sizeof(link) - 1))
1117 && !__get_user(UNW_PC(frame),
1118 (unsigned long *)(UNW_FP(frame)
1119 + FRAME_RETADDR_OFFSET)))
1122 UNW_FP(frame) + FRAME_RETADDR_OFFSET
1123 #if FRAME_RETADDR_OFFSET < 0
1128 sizeof(UNW_PC(frame));
1129 UNW_FP(frame) = link;
1136 state.org = startLoc;
1137 memcpy(&state.cfa, &badCFA, sizeof(state.cfa));
1139 unw_debug("\nProcess instructions\n");
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
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))
1157 unw_debug("\nRegister State Based on the rules parsed from FDE:\n");
1158 for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1163 switch (state.regs[i].where) {
1167 unw_debug(" r%d: c(%lu),", i, state.regs[i].value);
1170 unw_debug(" r%d: r(%lu),", i, state.regs[i].value);
1173 unw_debug(" r%d: v(%lu),", i, state.regs[i].value);
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);
1193 unw_debug("\nCFA reg: 0x%lx, offset: 0x%lx => 0x%lx\n",
1194 state.cfa.reg, state.cfa.offs, cfa);
1196 for (i = 0; i < ARRAY_SIZE(state.regs); ++i) {
1197 if (REG_INVALID(i)) {
1198 if (state.regs[i].where == Nowhere)
1202 switch (state.regs[i].where) {
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)
1211 switch (reg_info[state.regs[i].value].width) {
1213 state.regs[i].value =
1214 FRAME_REG(state.regs[i].value, const u8);
1217 state.regs[i].value =
1218 FRAME_REG(state.regs[i].value, const u16);
1221 state.regs[i].value =
1222 FRAME_REG(state.regs[i].value, const u32);
1226 state.regs[i].value =
1227 FRAME_REG(state.regs[i].value, const u64);
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++) {
1243 switch (state.regs[i].where) {
1245 if (reg_info[i].width != sizeof(UNW_SP(frame))
1246 || &FRAME_REG(i, __typeof__(UNW_SP(frame)))
1249 UNW_SP(frame) = cfa;
1252 switch (reg_info[i].width) {
1254 FRAME_REG(i, u8) = state.regs[i].value;
1257 FRAME_REG(i, u16) = state.regs[i].value;
1260 FRAME_REG(i, u32) = state.regs[i].value;
1264 FRAME_REG(i, u64) = state.regs[i].value;
1272 if (reg_info[i].width != sizeof(unsigned long))
1274 FRAME_REG(i, unsigned long) = cfa + state.regs[i].value
1278 addr = cfa + state.regs[i].value * state.dataAlign;
1280 if ((state.regs[i].value * state.dataAlign)
1281 % sizeof(unsigned long)
1283 || addr + sizeof(unsigned long) < addr
1284 || addr + sizeof(unsigned long) > endLoc)
1287 switch (reg_info[i].width) {
1289 __get_user(FRAME_REG(i, u8),
1293 __get_user(FRAME_REG(i, u16),
1294 (u16 __user *)addr);
1297 __get_user(FRAME_REG(i, u32),
1298 (u32 __user *)addr);
1302 __get_user(FRAME_REG(i, u64),
1303 (u64 __user *)addr);
1312 unw_debug("r%d: 0x%lx ", i, *fptr);
1318 EXPORT_SYMBOL(arc_unwind);