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>
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.
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.
16 #include <linux/sched.h>
17 #include <linux/module.h>
18 #include <linux/memblock.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>
28 extern char __start_unwind
[], __end_unwind
[];
29 /* extern const u8 __start_unwind_hdr[], __end_unwind_hdr[];*/
31 /* #define UNWIND_DEBUG */
35 #define unw_debug(fmt, ...) \
38 pr_info(fmt, ##__VA_ARGS__); \
41 #define unw_debug(fmt, ...)
44 #define MAX_STACK_DEPTH 8
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) \
53 #define PTREGS_INFO(f) EXTRA_INFO(regs.f)
56 unsigned offs
:BITS_PER_LONG
/ 2;
57 unsigned width
:BITS_PER_LONG
/ 2;
65 #define REG_INVALID(r) (reg_info[r].width == 0)
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
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
116 typedef unsigned long uleb128_t
;
117 typedef signed long sleb128_t
;
119 static struct unwind_table
{
126 const unsigned char *header
;
128 struct unwind_table
*link
;
142 struct unwind_state
{
144 const u8
*cieStart
, *cieEnd
;
150 struct unwind_item regs
[ARRAY_SIZE(reg_info
)];
151 unsigned stackDepth
:8;
154 const u8
*stack
[MAX_STACK_DEPTH
];
157 static const struct cfa badCFA
= { ARRAY_SIZE(reg_info
), 1 };
159 static struct unwind_table
*find_table(unsigned long pc
)
161 struct unwind_table
*table
;
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
))
173 static unsigned long read_pointer(const u8
**pLoc
,
174 const void *end
, signed ptrType
);
175 static void init_unwind_hdr(struct unwind_table
*table
,
176 void *(*alloc
) (unsigned long));
179 * wrappers for header alloc (vs. calling one vs. other at call site)
180 * to elide section mismatches warnings
182 static void *__init
unw_hdr_alloc_early(unsigned long sz
)
184 return memblock_alloc_from(sz
, sizeof(unsigned int), MAX_DMA_ADDRESS
);
187 static void *unw_hdr_alloc(unsigned long sz
)
189 return kmalloc(sz
, GFP_KERNEL
);
192 static void init_unwind_table(struct unwind_table
*table
, const char *name
,
193 const void *core_start
, unsigned long core_size
,
194 const void *init_start
, unsigned long init_size
,
195 const void *table_start
, unsigned long table_size
,
196 const u8
*header_start
, unsigned long header_size
)
198 const u8
*ptr
= header_start
+ 4;
199 const u8
*end
= header_start
+ header_size
;
201 table
->core
.pc
= (unsigned long)core_start
;
202 table
->core
.range
= core_size
;
203 table
->init
.pc
= (unsigned long)init_start
;
204 table
->init
.range
= init_size
;
205 table
->address
= table_start
;
206 table
->size
= table_size
;
208 /* See if the linker provided table looks valid. */
210 || header_start
[0] != 1
211 || (void *)read_pointer(&ptr
, end
, header_start
[1]) != table_start
212 || header_start
[2] == DW_EH_PE_omit
213 || read_pointer(&ptr
, end
, header_start
[2]) <= 0
214 || header_start
[3] == DW_EH_PE_omit
)
217 table
->hdrsz
= header_size
;
219 table
->header
= header_start
;
224 void __init
arc_unwind_init(void)
226 init_unwind_table(&root_table
, "kernel", _text
, _end
- _text
, NULL
, 0,
227 __start_unwind
, __end_unwind
- __start_unwind
,
229 /*__start_unwind_hdr, __end_unwind_hdr - __start_unwind_hdr);*/
231 init_unwind_hdr(&root_table
, unw_hdr_alloc_early
);
234 static const u32 bad_cie
, not_fde
;
235 static const u32
*cie_for_fde(const u32
*fde
, const struct unwind_table
*);
236 static const u32
*__cie_for_fde(const u32
*fde
);
237 static signed fde_pointer_type(const u32
*cie
);
239 struct eh_frame_hdr_table_entry
{
240 unsigned long start
, fde
;
243 static int cmp_eh_frame_hdr_table_entries(const void *p1
, const void *p2
)
245 const struct eh_frame_hdr_table_entry
*e1
= p1
;
246 const struct eh_frame_hdr_table_entry
*e2
= p2
;
248 return (e1
->start
> e2
->start
) - (e1
->start
< e2
->start
);
251 static void swap_eh_frame_hdr_table_entries(void *p1
, void *p2
, int size
)
253 struct eh_frame_hdr_table_entry
*e1
= p1
;
254 struct eh_frame_hdr_table_entry
*e2
= p2
;
258 e1
->start
= e2
->start
;
265 static void init_unwind_hdr(struct unwind_table
*table
,
266 void *(*alloc
) (unsigned long))
269 unsigned long tableSize
= table
->size
, hdrSize
;
277 unsigned long eh_frame_ptr
;
278 unsigned int fde_count
;
279 struct eh_frame_hdr_table_entry table
[];
280 } __attribute__ ((__packed__
)) *header
;
286 pr_warn(".eh_frame_hdr for '%s' present but unusable\n",
289 if (tableSize
& (sizeof(*fde
) - 1))
292 for (fde
= table
->address
, n
= 0;
293 tableSize
> sizeof(*fde
) && tableSize
- sizeof(*fde
) >= *fde
;
294 tableSize
-= sizeof(*fde
) + *fde
, fde
+= 1 + *fde
/ sizeof(*fde
)) {
295 const u32
*cie
= cie_for_fde(fde
, table
);
300 if (cie
== NULL
|| cie
== &bad_cie
)
302 ptrType
= fde_pointer_type(cie
);
306 ptr
= (const u8
*)(fde
+ 2);
307 if (!read_pointer(&ptr
, (const u8
*)(fde
+ 1) + *fde
,
309 /* FIXME_Rajesh We have 4 instances of null addresses
310 * instead of the initial loc addr
313 WARN(1, "unwinder: FDE->initial_location NULL %p\n",
314 (const u8
*)(fde
+ 1) + *fde
);
322 hdrSize
= 4 + sizeof(unsigned long) + sizeof(unsigned int)
323 + 2 * n
* sizeof(unsigned long);
325 header
= alloc(hdrSize
);
330 header
->eh_frame_ptr_enc
= DW_EH_PE_abs
| DW_EH_PE_native
;
331 header
->fde_count_enc
= DW_EH_PE_abs
| DW_EH_PE_data4
;
332 header
->table_enc
= DW_EH_PE_abs
| DW_EH_PE_native
;
333 put_unaligned((unsigned long)table
->address
, &header
->eh_frame_ptr
);
334 BUILD_BUG_ON(offsetof(typeof(*header
), fde_count
)
335 % __alignof(typeof(header
->fde_count
)));
336 header
->fde_count
= n
;
338 BUILD_BUG_ON(offsetof(typeof(*header
), table
)
339 % __alignof(typeof(*header
->table
)));
340 for (fde
= table
->address
, tableSize
= table
->size
, n
= 0;
342 tableSize
-= sizeof(*fde
) + *fde
, fde
+= 1 + *fde
/ sizeof(*fde
)) {
343 const u32
*cie
= __cie_for_fde(fde
);
345 if (fde
[1] == CIE_ID
)
346 continue; /* this is a CIE */
347 ptr
= (const u8
*)(fde
+ 2);
348 header
->table
[n
].start
= read_pointer(&ptr
,
349 (const u8
*)(fde
+ 1) +
351 fde_pointer_type(cie
));
352 header
->table
[n
].fde
= (unsigned long)fde
;
355 WARN_ON(n
!= header
->fde_count
);
359 sizeof(*header
->table
),
360 cmp_eh_frame_hdr_table_entries
, swap_eh_frame_hdr_table_entries
);
362 table
->hdrsz
= hdrSize
;
364 table
->header
= (const void *)header
;
368 panic("Attention !!! Dwarf FDE parsing errors\n");
371 #ifdef CONFIG_MODULES
373 static struct unwind_table
*last_table
;
375 /* Must be called with module_mutex held. */
376 void *unwind_add_table(struct module
*module
, const void *table_start
,
377 unsigned long table_size
)
379 struct unwind_table
*table
;
384 table
= kmalloc(sizeof(*table
), GFP_KERNEL
);
388 init_unwind_table(table
, module
->name
,
389 module
->core_layout
.base
, module
->core_layout
.size
,
390 module
->init_layout
.base
, module
->init_layout
.size
,
391 table_start
, table_size
,
394 init_unwind_hdr(table
, unw_hdr_alloc
);
397 unw_debug("Table added for [%s] %lx %lx\n",
398 module
->name
, table
->core
.pc
, table
->core
.range
);
401 last_table
->link
= table
;
403 root_table
.link
= table
;
409 struct unlink_table_info
{
410 struct unwind_table
*table
;
414 static int unlink_table(void *arg
)
416 struct unlink_table_info
*info
= arg
;
417 struct unwind_table
*table
= info
->table
, *prev
;
419 for (prev
= &root_table
; prev
->link
&& prev
->link
!= table
;
424 if (info
->init_only
) {
426 table
->init
.range
= 0;
429 prev
->link
= table
->link
;
439 /* Must be called with module_mutex held. */
440 void unwind_remove_table(void *handle
, int init_only
)
442 struct unwind_table
*table
= handle
;
443 struct unlink_table_info info
;
445 if (!table
|| table
== &root_table
)
448 if (init_only
&& table
== last_table
) {
450 table
->init
.range
= 0;
455 info
.init_only
= init_only
;
457 unlink_table(&info
); /* XXX: SMP */
458 kfree(table
->header
);
462 #endif /* CONFIG_MODULES */
464 static uleb128_t
get_uleb128(const u8
**pcur
, const u8
*end
)
466 const u8
*cur
= *pcur
;
470 for (shift
= 0, value
= 0; cur
< end
; shift
+= 7) {
471 if (shift
+ 7 > 8 * sizeof(value
)
472 && (*cur
& 0x7fU
) >= (1U << (8 * sizeof(value
) - shift
))) {
476 value
|= (uleb128_t
) (*cur
& 0x7f) << shift
;
477 if (!(*cur
++ & 0x80))
485 static sleb128_t
get_sleb128(const u8
**pcur
, const u8
*end
)
487 const u8
*cur
= *pcur
;
491 for (shift
= 0, value
= 0; cur
< end
; shift
+= 7) {
492 if (shift
+ 7 > 8 * sizeof(value
)
493 && (*cur
& 0x7fU
) >= (1U << (8 * sizeof(value
) - shift
))) {
497 value
|= (sleb128_t
) (*cur
& 0x7f) << shift
;
498 if (!(*cur
& 0x80)) {
499 value
|= -(*cur
++ & 0x40) << shift
;
508 static const u32
*__cie_for_fde(const u32
*fde
)
512 cie
= fde
+ 1 - fde
[1] / sizeof(*fde
);
517 static const u32
*cie_for_fde(const u32
*fde
, const struct unwind_table
*table
)
521 if (!*fde
|| (*fde
& (sizeof(*fde
) - 1)))
524 if (fde
[1] == CIE_ID
)
525 return ¬_fde
; /* this is a CIE */
527 if ((fde
[1] & (sizeof(*fde
) - 1)))
528 /* || fde[1] > (unsigned long)(fde + 1) - (unsigned long)table->address) */
529 return NULL
; /* this is not a valid FDE */
531 cie
= __cie_for_fde(fde
);
533 if (*cie
<= sizeof(*cie
) + 4 || *cie
>= fde
[1] - sizeof(*fde
)
534 || (*cie
& (sizeof(*cie
) - 1))
535 || (cie
[1] != CIE_ID
))
536 return NULL
; /* this is not a (valid) CIE */
540 static unsigned long read_pointer(const u8
**pLoc
, const void *end
,
543 unsigned long value
= 0;
550 const unsigned long *pul
;
553 if (ptrType
< 0 || ptrType
== DW_EH_PE_omit
)
556 switch (ptrType
& DW_EH_PE_FORM
) {
558 if (end
< (const void *)(ptr
.p16u
+ 1))
560 if (ptrType
& DW_EH_PE_signed
)
561 value
= get_unaligned((u16
*) ptr
.p16s
++);
563 value
= get_unaligned((u16
*) ptr
.p16u
++);
567 if (end
< (const void *)(ptr
.p32u
+ 1))
569 if (ptrType
& DW_EH_PE_signed
)
570 value
= get_unaligned(ptr
.p32s
++);
572 value
= get_unaligned(ptr
.p32u
++);
575 BUILD_BUG_ON(sizeof(u64
) != sizeof(value
));
577 BUILD_BUG_ON(sizeof(u32
) != sizeof(value
));
579 case DW_EH_PE_native
:
580 if (end
< (const void *)(ptr
.pul
+ 1))
582 value
= get_unaligned((unsigned long *)ptr
.pul
++);
584 case DW_EH_PE_leb128
:
585 BUILD_BUG_ON(sizeof(uleb128_t
) > sizeof(value
));
586 value
= ptrType
& DW_EH_PE_signed
? get_sleb128(&ptr
.p8
, end
)
587 : get_uleb128(&ptr
.p8
, end
);
588 if ((const void *)ptr
.p8
> end
)
594 switch (ptrType
& DW_EH_PE_ADJUST
) {
598 value
+= (unsigned long)*pLoc
;
603 if ((ptrType
& DW_EH_PE_indirect
)
604 && __get_user(value
, (unsigned long __user
*)value
))
611 static signed fde_pointer_type(const u32
*cie
)
613 const u8
*ptr
= (const u8
*)(cie
+ 2);
614 unsigned version
= *ptr
;
618 const u8
*end
= (const u8
*)(cie
+ 1) + *cie
;
621 /* check if augmentation size is first (and thus present) */
625 /* check if augmentation string is nul-terminated */
626 aug
= (const void *)ptr
;
627 ptr
= memchr(aug
, 0, end
- ptr
);
631 ++ptr
; /* skip terminator */
632 get_uleb128(&ptr
, end
); /* skip code alignment */
633 get_sleb128(&ptr
, end
); /* skip data alignment */
634 /* skip return address column */
635 version
<= 1 ? (void) ++ptr
: (void)get_uleb128(&ptr
, end
);
636 len
= get_uleb128(&ptr
, end
); /* augmentation length */
638 if (ptr
+ len
< ptr
|| ptr
+ len
> end
)
650 signed ptrType
= *ptr
++;
652 if (!read_pointer(&ptr
, end
, ptrType
)
664 return DW_EH_PE_native
| DW_EH_PE_abs
;
667 static int advance_loc(unsigned long delta
, struct unwind_state
*state
)
669 state
->loc
+= delta
* state
->codeAlign
;
671 /* FIXME_Rajesh: Probably we are defining for the initial range as well;
674 unw_debug("delta %3lu => loc 0x%lx: ", delta
, state
->loc
);
678 static void set_rule(uleb128_t reg
, enum item_location where
, uleb128_t value
,
679 struct unwind_state
*state
)
681 if (reg
< ARRAY_SIZE(state
->regs
)) {
682 state
->regs
[reg
].where
= where
;
683 state
->regs
[reg
].value
= value
;
686 unw_debug("r%lu: ", reg
);
692 unw_debug("c(%lu) ", value
);
695 unw_debug("r(%lu) ", value
);
698 unw_debug("v(%lu) ", value
);
707 static int processCFI(const u8
*start
, const u8
*end
, unsigned long targetLoc
,
708 signed ptrType
, struct unwind_state
*state
)
718 if (start
!= state
->cieStart
) {
719 state
->loc
= state
->org
;
721 processCFI(state
->cieStart
, state
->cieEnd
, 0, ptrType
,
723 if (targetLoc
== 0 && state
->label
== NULL
)
726 for (ptr
.p8
= start
; result
&& ptr
.p8
< end
;) {
727 switch (*ptr
.p8
>> 6) {
735 unw_debug("cfa nop ");
738 state
->loc
= read_pointer(&ptr
.p8
, end
,
742 unw_debug("cfa_set_loc: 0x%lx ", state
->loc
);
744 case DW_CFA_advance_loc1
:
745 unw_debug("\ncfa advance loc1:");
746 result
= ptr
.p8
< end
747 && advance_loc(*ptr
.p8
++, state
);
749 case DW_CFA_advance_loc2
:
751 value
+= *ptr
.p8
++ << 8;
752 unw_debug("\ncfa advance loc2:");
753 result
= ptr
.p8
<= end
+ 2
754 /* && advance_loc(*ptr.p16++, state); */
755 && advance_loc(value
, state
);
757 case DW_CFA_advance_loc4
:
758 unw_debug("\ncfa advance loc4:");
759 result
= ptr
.p8
<= end
+ 4
760 && advance_loc(*ptr
.p32
++, state
);
762 case DW_CFA_offset_extended
:
763 value
= get_uleb128(&ptr
.p8
, end
);
764 unw_debug("cfa_offset_extended: ");
765 set_rule(value
, Memory
,
766 get_uleb128(&ptr
.p8
, end
), state
);
768 case DW_CFA_val_offset
:
769 value
= get_uleb128(&ptr
.p8
, end
);
770 set_rule(value
, Value
,
771 get_uleb128(&ptr
.p8
, end
), state
);
773 case DW_CFA_offset_extended_sf
:
774 value
= get_uleb128(&ptr
.p8
, end
);
775 set_rule(value
, Memory
,
776 get_sleb128(&ptr
.p8
, end
), state
);
778 case DW_CFA_val_offset_sf
:
779 value
= get_uleb128(&ptr
.p8
, end
);
780 set_rule(value
, Value
,
781 get_sleb128(&ptr
.p8
, end
), state
);
783 case DW_CFA_restore_extended
:
784 unw_debug("cfa_restore_extended: ");
785 case DW_CFA_undefined
:
786 unw_debug("cfa_undefined: ");
787 case DW_CFA_same_value
:
788 unw_debug("cfa_same_value: ");
789 set_rule(get_uleb128(&ptr
.p8
, end
), Nowhere
, 0,
792 case DW_CFA_register
:
793 unw_debug("cfa_register: ");
794 value
= get_uleb128(&ptr
.p8
, end
);
797 get_uleb128(&ptr
.p8
, end
), state
);
799 case DW_CFA_remember_state
:
800 unw_debug("cfa_remember_state: ");
801 if (ptr
.p8
== state
->label
) {
805 if (state
->stackDepth
>= MAX_STACK_DEPTH
)
807 state
->stack
[state
->stackDepth
++] = ptr
.p8
;
809 case DW_CFA_restore_state
:
810 unw_debug("cfa_restore_state: ");
811 if (state
->stackDepth
) {
812 const uleb128_t loc
= state
->loc
;
813 const u8
*label
= state
->label
;
816 state
->stack
[state
->stackDepth
- 1];
817 memcpy(&state
->cfa
, &badCFA
,
819 memset(state
->regs
, 0,
820 sizeof(state
->regs
));
821 state
->stackDepth
= 0;
823 processCFI(start
, end
, 0, ptrType
,
826 state
->label
= label
;
831 state
->cfa
.reg
= get_uleb128(&ptr
.p8
, end
);
832 unw_debug("cfa_def_cfa: r%lu ", state
->cfa
.reg
);
834 case DW_CFA_def_cfa_offset
:
835 state
->cfa
.offs
= get_uleb128(&ptr
.p8
, end
);
836 unw_debug("cfa_def_cfa_offset: 0x%lx ",
839 case DW_CFA_def_cfa_sf
:
840 state
->cfa
.reg
= get_uleb128(&ptr
.p8
, end
);
842 case DW_CFA_def_cfa_offset_sf
:
843 state
->cfa
.offs
= get_sleb128(&ptr
.p8
, end
)
846 case DW_CFA_def_cfa_register
:
847 unw_debug("cfa_def_cfa_register: ");
848 state
->cfa
.reg
= get_uleb128(&ptr
.p8
, end
);
850 /*todo case DW_CFA_def_cfa_expression: */
851 /*todo case DW_CFA_expression: */
852 /*todo case DW_CFA_val_expression: */
853 case DW_CFA_GNU_args_size
:
854 get_uleb128(&ptr
.p8
, end
);
856 case DW_CFA_GNU_negative_offset_extended
:
857 value
= get_uleb128(&ptr
.p8
, end
);
860 (uleb128_t
) 0 - get_uleb128(&ptr
.p8
,
864 case DW_CFA_GNU_window_save
:
866 unw_debug("UNKNOWN OPCODE 0x%x\n", opcode
);
872 unw_debug("\ncfa_adv_loc: ");
873 result
= advance_loc(*ptr
.p8
++ & 0x3f, state
);
876 unw_debug("cfa_offset: ");
877 value
= *ptr
.p8
++ & 0x3f;
878 set_rule(value
, Memory
, get_uleb128(&ptr
.p8
, end
),
882 unw_debug("cfa_restore: ");
883 set_rule(*ptr
.p8
++ & 0x3f, Nowhere
, 0, state
);
889 if (result
&& targetLoc
!= 0 && targetLoc
< state
->loc
)
893 return result
&& ptr
.p8
== end
&& (targetLoc
== 0 || (
894 /*todo While in theory this should apply, gcc in practice omits
895 everything past the function prolog, and hence the location
896 never reaches the end of the function.
897 targetLoc < state->loc && */ state
->label
== NULL
));
900 /* Unwind to previous to frame. Returns 0 if successful, negative
901 * number in case of an error. */
902 int arc_unwind(struct unwind_frame_info
*frame
)
904 #define FRAME_REG(r, t) (((t *)frame)[reg_info[r].offs])
905 const u32
*fde
= NULL
, *cie
= NULL
;
906 const u8
*ptr
= NULL
, *end
= NULL
;
907 unsigned long pc
= UNW_PC(frame
) - frame
->call_frame
;
908 unsigned long startLoc
= 0, endLoc
= 0, cfa
;
911 uleb128_t retAddrReg
= 0;
912 const struct unwind_table
*table
;
913 struct unwind_state state
;
917 unw_debug("\n\nUNWIND FRAME:\n");
918 unw_debug("PC: 0x%lx BLINK: 0x%lx, SP: 0x%lx, FP: 0x%x\n",
919 UNW_PC(frame
), UNW_BLINK(frame
), UNW_SP(frame
),
922 if (UNW_PC(frame
) == 0)
927 unsigned long *sptr
= (unsigned long *)UNW_SP(frame
);
928 unw_debug("\nStack Dump:\n");
929 for (i
= 0; i
< 20; i
++, sptr
++)
930 unw_debug("0x%p: 0x%lx\n", sptr
, *sptr
);
935 table
= find_table(pc
);
937 && !(table
->size
& (sizeof(*fde
) - 1))) {
938 const u8
*hdr
= table
->header
;
939 unsigned long tableSize
;
942 if (hdr
&& hdr
[0] == 1) {
943 switch (hdr
[3] & DW_EH_PE_FORM
) {
944 case DW_EH_PE_native
:
945 tableSize
= sizeof(unsigned long);
961 end
= hdr
+ table
->hdrsz
;
962 if (tableSize
&& read_pointer(&ptr
, end
, hdr
[1])
963 == (unsigned long)table
->address
964 && (i
= read_pointer(&ptr
, end
, hdr
[2])) > 0
965 && i
== (end
- ptr
) / (2 * tableSize
)
966 && !((end
- ptr
) % (2 * tableSize
))) {
969 ptr
+ (i
/ 2) * (2 * tableSize
);
971 startLoc
= read_pointer(&cur
,
977 ptr
= cur
- tableSize
;
980 } while (startLoc
&& i
> 1);
982 && (startLoc
= read_pointer(&ptr
,
986 fde
= (void *)read_pointer(&ptr
,
994 cie
= cie_for_fde(fde
, table
);
995 ptr
= (const u8
*)(fde
+ 2);
999 && (ptrType
= fde_pointer_type(cie
)) >= 0
1000 && read_pointer(&ptr
,
1001 (const u8
*)(fde
+ 1) + *fde
,
1002 ptrType
) == startLoc
) {
1003 if (!(ptrType
& DW_EH_PE_indirect
))
1005 DW_EH_PE_FORM
| DW_EH_PE_signed
;
1007 startLoc
+ read_pointer(&ptr
,
1022 memset(&state
, 0, sizeof(state
));
1023 state
.cieEnd
= ptr
; /* keep here temporarily */
1024 ptr
= (const u8
*)(cie
+ 2);
1025 end
= (const u8
*)(cie
+ 1) + *cie
;
1026 frame
->call_frame
= 1;
1028 /* check if augmentation size is first (thus present) */
1030 while (++ptr
< end
&& *ptr
) {
1032 /* chk for ignorable or already handled
1033 * nul-terminated augmentation string */
1039 frame
->call_frame
= 0;
1047 if (ptr
>= end
|| *ptr
)
1053 /* get code alignment factor */
1054 state
.codeAlign
= get_uleb128(&ptr
, end
);
1055 /* get data alignment factor */
1056 state
.dataAlign
= get_sleb128(&ptr
, end
);
1057 if (state
.codeAlign
== 0 || state
.dataAlign
== 0 || ptr
>= end
)
1061 state
.version
<= 1 ? *ptr
++ : get_uleb128(&ptr
,
1063 unw_debug("CIE Frame Info:\n");
1064 unw_debug("return Address register 0x%lx\n",
1066 unw_debug("data Align: %ld\n", state
.dataAlign
);
1067 unw_debug("code Align: %lu\n", state
.codeAlign
);
1068 /* skip augmentation */
1069 if (((const char *)(cie
+ 2))[1] == 'z') {
1070 uleb128_t augSize
= get_uleb128(&ptr
, end
);
1074 if (ptr
> end
|| retAddrReg
>= ARRAY_SIZE(reg_info
)
1075 || REG_INVALID(retAddrReg
)
1076 || reg_info
[retAddrReg
].width
!=
1077 sizeof(unsigned long))
1082 state
.cieStart
= ptr
;
1085 end
= (const u8
*)(fde
+ 1) + *fde
;
1086 /* skip augmentation */
1087 if (((const char *)(cie
+ 2))[1] == 'z') {
1088 uleb128_t augSize
= get_uleb128(&ptr
, end
);
1090 if ((ptr
+= augSize
) > end
)
1094 if (cie
== NULL
|| fde
== NULL
) {
1095 #ifdef CONFIG_FRAME_POINTER
1096 unsigned long top
, bottom
;
1098 top
= STACK_TOP_UNW(frame
->task
);
1099 bottom
= STACK_BOTTOM_UNW(frame
->task
);
1100 #if FRAME_RETADDR_OFFSET < 0
1101 if (UNW_SP(frame
) < top
&& UNW_FP(frame
) <= UNW_SP(frame
)
1102 && bottom
< UNW_FP(frame
)
1104 if (UNW_SP(frame
) > top
&& UNW_FP(frame
) >= UNW_SP(frame
)
1105 && bottom
> UNW_FP(frame
)
1107 && !((UNW_SP(frame
) | UNW_FP(frame
))
1108 & (sizeof(unsigned long) - 1))) {
1111 if (!__get_user(link
, (unsigned long *)
1112 (UNW_FP(frame
) + FRAME_LINK_OFFSET
))
1113 #if FRAME_RETADDR_OFFSET < 0
1114 && link
> bottom
&& link
< UNW_FP(frame
)
1116 && link
> UNW_FP(frame
) && link
< bottom
1118 && !(link
& (sizeof(link
) - 1))
1119 && !__get_user(UNW_PC(frame
),
1120 (unsigned long *)(UNW_FP(frame
)
1121 + FRAME_RETADDR_OFFSET
)))
1124 UNW_FP(frame
) + FRAME_RETADDR_OFFSET
1125 #if FRAME_RETADDR_OFFSET < 0
1130 sizeof(UNW_PC(frame
));
1131 UNW_FP(frame
) = link
;
1138 state
.org
= startLoc
;
1139 memcpy(&state
.cfa
, &badCFA
, sizeof(state
.cfa
));
1141 unw_debug("\nProcess instructions\n");
1143 /* process instructions
1144 * For ARC, we optimize by having blink(retAddrReg) with
1145 * the sameValue in the leaf function, so we should not check
1146 * state.regs[retAddrReg].where == Nowhere
1148 if (!processCFI(ptr
, end
, pc
, ptrType
, &state
)
1149 || state
.loc
> endLoc
1150 /* || state.regs[retAddrReg].where == Nowhere */
1151 || state
.cfa
.reg
>= ARRAY_SIZE(reg_info
)
1152 || reg_info
[state
.cfa
.reg
].width
!= sizeof(unsigned long)
1153 || state
.cfa
.offs
% sizeof(unsigned long))
1159 unw_debug("\nRegister State Based on the rules parsed from FDE:\n");
1160 for (i
= 0; i
< ARRAY_SIZE(state
.regs
); ++i
) {
1165 switch (state
.regs
[i
].where
) {
1169 unw_debug(" r%d: c(%lu),", i
, state
.regs
[i
].value
);
1172 unw_debug(" r%d: r(%lu),", i
, state
.regs
[i
].value
);
1175 unw_debug(" r%d: v(%lu),", i
, state
.regs
[i
].value
);
1184 #ifndef CONFIG_AS_CFI_SIGNAL_FRAME
1185 if (frame
->call_frame
1186 && !UNW_DEFAULT_RA(state
.regs
[retAddrReg
], state
.dataAlign
))
1187 frame
->call_frame
= 0;
1189 cfa
= FRAME_REG(state
.cfa
.reg
, unsigned long) + state
.cfa
.offs
;
1190 startLoc
= min_t(unsigned long, UNW_SP(frame
), cfa
);
1191 endLoc
= max_t(unsigned long, UNW_SP(frame
), cfa
);
1192 if (STACK_LIMIT(startLoc
) != STACK_LIMIT(endLoc
)) {
1193 startLoc
= min(STACK_LIMIT(cfa
), cfa
);
1194 endLoc
= max(STACK_LIMIT(cfa
), cfa
);
1197 unw_debug("\nCFA reg: 0x%lx, offset: 0x%lx => 0x%lx\n",
1198 state
.cfa
.reg
, state
.cfa
.offs
, cfa
);
1200 for (i
= 0; i
< ARRAY_SIZE(state
.regs
); ++i
) {
1201 if (REG_INVALID(i
)) {
1202 if (state
.regs
[i
].where
== Nowhere
)
1206 switch (state
.regs
[i
].where
) {
1210 if (state
.regs
[i
].value
>= ARRAY_SIZE(reg_info
)
1211 || REG_INVALID(state
.regs
[i
].value
)
1212 || reg_info
[i
].width
>
1213 reg_info
[state
.regs
[i
].value
].width
)
1215 switch (reg_info
[state
.regs
[i
].value
].width
) {
1217 state
.regs
[i
].value
=
1218 FRAME_REG(state
.regs
[i
].value
, const u8
);
1221 state
.regs
[i
].value
=
1222 FRAME_REG(state
.regs
[i
].value
, const u16
);
1225 state
.regs
[i
].value
=
1226 FRAME_REG(state
.regs
[i
].value
, const u32
);
1230 state
.regs
[i
].value
=
1231 FRAME_REG(state
.regs
[i
].value
, const u64
);
1241 unw_debug("\nRegister state after evaluation with realtime Stack:\n");
1242 fptr
= (unsigned long *)(&frame
->regs
);
1243 for (i
= 0; i
< ARRAY_SIZE(state
.regs
); ++i
, fptr
++) {
1247 switch (state
.regs
[i
].where
) {
1249 if (reg_info
[i
].width
!= sizeof(UNW_SP(frame
))
1250 || &FRAME_REG(i
, __typeof__(UNW_SP(frame
)))
1253 UNW_SP(frame
) = cfa
;
1256 switch (reg_info
[i
].width
) {
1258 FRAME_REG(i
, u8
) = state
.regs
[i
].value
;
1261 FRAME_REG(i
, u16
) = state
.regs
[i
].value
;
1264 FRAME_REG(i
, u32
) = state
.regs
[i
].value
;
1268 FRAME_REG(i
, u64
) = state
.regs
[i
].value
;
1276 if (reg_info
[i
].width
!= sizeof(unsigned long))
1278 FRAME_REG(i
, unsigned long) = cfa
+ state
.regs
[i
].value
1282 addr
= cfa
+ state
.regs
[i
].value
* state
.dataAlign
;
1284 if ((state
.regs
[i
].value
* state
.dataAlign
)
1285 % sizeof(unsigned long)
1287 || addr
+ sizeof(unsigned long) < addr
1288 || addr
+ sizeof(unsigned long) > endLoc
)
1291 switch (reg_info
[i
].width
) {
1293 __get_user(FRAME_REG(i
, u8
),
1297 __get_user(FRAME_REG(i
, u16
),
1298 (u16 __user
*)addr
);
1301 __get_user(FRAME_REG(i
, u32
),
1302 (u32 __user
*)addr
);
1306 __get_user(FRAME_REG(i
, u64
),
1307 (u64 __user
*)addr
);
1316 unw_debug("r%d: 0x%lx ", i
, *fptr
);
1322 EXPORT_SYMBOL(arc_unwind
);