38
41
static struct kmem_cache *dwarf_reg_cachep;
39
42
static mempool_t *dwarf_reg_pool;
41
static LIST_HEAD(dwarf_cie_list);
44
static struct rb_root cie_root;
42
45
static DEFINE_SPINLOCK(dwarf_cie_lock);
44
static LIST_HEAD(dwarf_fde_list);
47
static struct rb_root fde_root;
45
48
static DEFINE_SPINLOCK(dwarf_fde_lock);
47
50
static struct dwarf_cie *cached_cie;
52
static unsigned int dwarf_unwinder_ready;
50
55
* dwarf_frame_alloc_reg - allocate memory for a DWARF register
51
56
* @frame: the DWARF frame whose list of registers we insert on
317
list_for_each_entry(cie, &dwarf_cie_list, link) {
318
if (cie->cie_pointer == cie_ptr) {
324
struct dwarf_cie *cie_tmp;
326
cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
329
if (cie_ptr == cie_tmp->cie_pointer) {
331
cached_cie = cie_tmp;
334
if (cie_ptr < cie_tmp->cie_pointer)
335
rb_node = &(*rb_node)->rb_left;
337
rb_node = &(*rb_node)->rb_right;
324
/* Couldn't find the entry in the list. */
325
if (&cie->link == &dwarf_cie_list)
328
342
spin_unlock_irqrestore(&dwarf_cie_lock, flags);
336
350
struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)
338
struct dwarf_fde *fde;
352
struct rb_node **rb_node = &fde_root.rb_node;
353
struct dwarf_fde *fde = NULL;
339
354
unsigned long flags;
341
356
spin_lock_irqsave(&dwarf_fde_lock, flags);
343
list_for_each_entry(fde, &dwarf_fde_list, link) {
344
unsigned long start, end;
346
start = fde->initial_location;
347
end = fde->initial_location + fde->address_range;
349
if (pc >= start && pc < end)
359
struct dwarf_fde *fde_tmp;
360
unsigned long tmp_start, tmp_end;
362
fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
365
tmp_start = fde_tmp->initial_location;
366
tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
368
if (pc < tmp_start) {
369
rb_node = &(*rb_node)->rb_left;
375
rb_node = &(*rb_node)->rb_right;
353
/* Couldn't find the entry in the list. */
354
if (&fde->link == &dwarf_fde_list)
357
380
spin_unlock_irqrestore(&dwarf_fde_lock, flags);
533
* dwarf_unwind_stack - recursively unwind the stack
556
* dwarf_free_frame - free the memory allocated for @frame
557
* @frame: the frame to free
559
void dwarf_free_frame(struct dwarf_frame *frame)
561
dwarf_frame_free_regs(frame);
562
mempool_free(frame, dwarf_frame_pool);
565
extern void ret_from_irq(void);
568
* dwarf_unwind_stack - unwind the stack
534
570
* @pc: address of the function to unwind
535
571
* @prev: struct dwarf_frame of the previous stackframe on the callstack
548
584
unsigned long addr;
551
* If this is the first invocation of this recursive function we
552
* need get the contents of a physical register to get the CFA
553
* in order to begin the virtual unwinding of the stack.
587
* If we've been called in to before initialization has
588
* completed, bail out immediately.
590
if (!dwarf_unwinder_ready)
594
* If we're starting at the top of the stack we need get the
595
* contents of a physical register to get the CFA in order to
596
* begin the virtual unwinding of the stack.
555
598
* NOTE: the return address is guaranteed to be setup by the
556
599
* time this function makes its first function call.
667
709
addr = frame->cfa + reg->addr;
668
710
frame->return_addr = __raw_readl(addr);
713
* Ah, the joys of unwinding through interrupts.
715
* Interrupts are tricky - the DWARF info needs to be _really_
716
* accurate and unfortunately I'm seeing a lot of bogus DWARF
717
* info. For example, I've seen interrupts occur in epilogues
718
* just after the frame pointer (r14) had been restored. The
719
* problem was that the DWARF info claimed that the CFA could be
720
* reached by using the value of the frame pointer before it was
723
* So until the compiler can be trusted to produce reliable
724
* DWARF info when it really matters, let's stop unwinding once
725
* we've calculated the function that was interrupted.
727
if (prev && prev->pc == (unsigned long)ret_from_irq)
728
frame->return_addr = 0;
673
dwarf_frame_free_regs(frame);
674
mempool_free(frame, dwarf_frame_pool);
733
dwarf_free_frame(frame);
678
737
static int dwarf_parse_cie(void *entry, void *p, unsigned long len,
738
unsigned char *end, struct module *mod)
740
struct rb_node **rb_node = &cie_root.rb_node;
741
struct rb_node *parent = *rb_node;
681
742
struct dwarf_cie *cie;
682
743
unsigned long flags;
775
836
/* Add to list */
776
837
spin_lock_irqsave(&dwarf_cie_lock, flags);
777
list_add_tail(&cie->link, &dwarf_cie_list);
840
struct dwarf_cie *cie_tmp;
842
cie_tmp = rb_entry(*rb_node, struct dwarf_cie, node);
846
if (cie->cie_pointer < cie_tmp->cie_pointer)
847
rb_node = &parent->rb_left;
848
else if (cie->cie_pointer >= cie_tmp->cie_pointer)
849
rb_node = &parent->rb_right;
854
rb_link_node(&cie->node, parent, rb_node);
855
rb_insert_color(&cie->node, &cie_root);
857
#ifdef CONFIG_MODULES
859
list_add_tail(&cie->link, &mod->arch.cie_list);
778
862
spin_unlock_irqrestore(&dwarf_cie_lock, flags);
834
920
/* Add to list. */
835
921
spin_lock_irqsave(&dwarf_fde_lock, flags);
836
list_add_tail(&fde->link, &dwarf_fde_list);
924
struct dwarf_fde *fde_tmp;
925
unsigned long tmp_start, tmp_end;
926
unsigned long start, end;
928
fde_tmp = rb_entry(*rb_node, struct dwarf_fde, node);
930
start = fde->initial_location;
931
end = fde->initial_location + fde->address_range;
933
tmp_start = fde_tmp->initial_location;
934
tmp_end = fde_tmp->initial_location + fde_tmp->address_range;
938
if (start < tmp_start)
939
rb_node = &parent->rb_left;
940
else if (start >= tmp_end)
941
rb_node = &parent->rb_right;
946
rb_link_node(&fde->node, parent, rb_node);
947
rb_insert_color(&fde->node, &fde_root);
949
#ifdef CONFIG_MODULES
951
list_add_tail(&fde->link, &mod->arch.fde_list);
837
954
spin_unlock_irqrestore(&dwarf_fde_lock, flags);
878
996
static void dwarf_unwinder_cleanup(void)
880
struct dwarf_cie *cie;
881
struct dwarf_fde *fde;
998
struct rb_node **fde_rb_node = &fde_root.rb_node;
999
struct rb_node **cie_rb_node = &cie_root.rb_node;
884
1002
* Deallocate all the memory allocated for the DWARF unwinder.
885
1003
* Traverse all the FDE/CIE lists and remove and free all the
886
1004
* memory associated with those data structures.
888
list_for_each_entry(cie, &dwarf_cie_list, link)
1006
while (*fde_rb_node) {
1007
struct dwarf_fde *fde;
1009
fde = rb_entry(*fde_rb_node, struct dwarf_fde, node);
1010
rb_erase(*fde_rb_node, &fde_root);
1014
while (*cie_rb_node) {
1015
struct dwarf_cie *cie;
1017
cie = rb_entry(*cie_rb_node, struct dwarf_cie, node);
1018
rb_erase(*cie_rb_node, &cie_root);
891
list_for_each_entry(fde, &dwarf_fde_list, link)
894
1022
kmem_cache_destroy(dwarf_reg_cachep);
895
1023
kmem_cache_destroy(dwarf_frame_cachep);
899
* dwarf_unwinder_init - initialise the dwarf unwinder
1027
* dwarf_parse_section - parse DWARF section
1028
* @eh_frame_start: start address of the .eh_frame section
1029
* @eh_frame_end: end address of the .eh_frame section
1030
* @mod: the kernel module containing the .eh_frame section
901
* Build the data structures describing the .dwarf_frame section to
902
* make it easier to lookup CIE and FDE entries. Because the
903
* .eh_frame section is packed as tightly as possible it is not
904
* easy to lookup the FDE for a given PC, so we build a list of FDE
905
* and CIE entries that make it easier.
1032
* Parse the information in a .eh_frame section.
907
static int __init dwarf_unwinder_init(void)
1034
static int dwarf_parse_section(char *eh_frame_start, char *eh_frame_end,
910
1038
void *p, *entry;
911
1039
int count, err = 0;
1040
unsigned long len = 0;
913
1041
unsigned int c_entries, f_entries;
914
1042
unsigned char *end;
915
INIT_LIST_HEAD(&dwarf_cie_list);
916
INIT_LIST_HEAD(&dwarf_fde_list);
920
entry = &__start_eh_frame;
922
dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
923
sizeof(struct dwarf_frame), 0,
924
SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
926
dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
927
sizeof(struct dwarf_reg), 0,
928
SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
930
dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
935
dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
940
while ((char *)entry < __stop_eh_frame) {
1046
entry = eh_frame_start;
1048
while ((char *)entry < eh_frame_end) {
943
1051
count = dwarf_entry_len(p, &len);
962
1071
if (entry_type == DW_EH_FRAME_CIE) {
963
err = dwarf_parse_cie(entry, p, len, end);
1072
err = dwarf_parse_cie(entry, p, len, end, mod);
969
err = dwarf_parse_fde(entry, entry_type, p, len, end);
1078
err = dwarf_parse_fde(entry, entry_type, p, len,
979
1089
printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n",
980
1090
c_entries, f_entries);
1098
#ifdef CONFIG_MODULES
1099
int module_dwarf_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,
1102
unsigned int i, err;
1103
unsigned long start, end;
1104
char *secstrings = (void *)hdr + sechdrs[hdr->e_shstrndx].sh_offset;
1108
for (i = 1; i < hdr->e_shnum; i++) {
1109
/* Alloc bit cleared means "ignore it." */
1110
if ((sechdrs[i].sh_flags & SHF_ALLOC)
1111
&& !strcmp(secstrings+sechdrs[i].sh_name, ".eh_frame")) {
1112
start = sechdrs[i].sh_addr;
1113
end = start + sechdrs[i].sh_size;
1118
/* Did we find the .eh_frame section? */
1119
if (i != hdr->e_shnum) {
1120
INIT_LIST_HEAD(&me->arch.cie_list);
1121
INIT_LIST_HEAD(&me->arch.fde_list);
1122
err = dwarf_parse_section((char *)start, (char *)end, me);
1124
printk(KERN_WARNING "%s: failed to parse DWARF info\n",
1134
* module_dwarf_cleanup - remove FDE/CIEs associated with @mod
1135
* @mod: the module that is being unloaded
1137
* Remove any FDEs and CIEs from the global lists that came from
1138
* @mod's .eh_frame section because @mod is being unloaded.
1140
void module_dwarf_cleanup(struct module *mod)
1142
struct dwarf_fde *fde, *ftmp;
1143
struct dwarf_cie *cie, *ctmp;
1144
unsigned long flags;
1146
spin_lock_irqsave(&dwarf_cie_lock, flags);
1148
list_for_each_entry_safe(cie, ctmp, &mod->arch.cie_list, link) {
1149
list_del(&cie->link);
1150
rb_erase(&cie->node, &cie_root);
1154
spin_unlock_irqrestore(&dwarf_cie_lock, flags);
1156
spin_lock_irqsave(&dwarf_fde_lock, flags);
1158
list_for_each_entry_safe(fde, ftmp, &mod->arch.fde_list, link) {
1159
list_del(&fde->link);
1160
rb_erase(&fde->node, &fde_root);
1164
spin_unlock_irqrestore(&dwarf_fde_lock, flags);
1166
#endif /* CONFIG_MODULES */
1169
* dwarf_unwinder_init - initialise the dwarf unwinder
1171
* Build the data structures describing the .dwarf_frame section to
1172
* make it easier to lookup CIE and FDE entries. Because the
1173
* .eh_frame section is packed as tightly as possible it is not
1174
* easy to lookup the FDE for a given PC, so we build a list of FDE
1175
* and CIE entries that make it easier.
1177
static int __init dwarf_unwinder_init(void)
1181
dwarf_frame_cachep = kmem_cache_create("dwarf_frames",
1182
sizeof(struct dwarf_frame), 0,
1183
SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
1185
dwarf_reg_cachep = kmem_cache_create("dwarf_regs",
1186
sizeof(struct dwarf_reg), 0,
1187
SLAB_PANIC | SLAB_HWCACHE_ALIGN | SLAB_NOTRACK, NULL);
1189
dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,
1192
dwarf_frame_cachep);
1193
if (!dwarf_frame_pool)
1196
dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,
1200
if (!dwarf_reg_pool)
1203
err = dwarf_parse_section(__start_eh_frame, __stop_eh_frame, NULL);
982
1207
err = unwinder_register(&dwarf_unwinder);
1211
dwarf_unwinder_ready = 1;
989
1216
printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);
990
1217
dwarf_unwinder_cleanup();
993
1220
early_initcall(dwarf_unwinder_init);