···265265266266 unsigned long pc;267267268268- struct dwarf_reg *regs;269269- unsigned int num_regs; /* how many regs are allocated? */270270-271271- unsigned int depth; /* what level are we in the callstack? */268268+ struct list_head reg_list;272269273270 unsigned long cfa;274271···289292 * @flags: Describes how to calculate the value of this register290293 */291294struct dwarf_reg {295295+ struct list_head link;296296+297297+ unsigned int number;298298+292299 unsigned long addr;293300 unsigned long flags;294301#define DWARF_REG_OFFSET (1 << 0)295295-};296296-297297-/**298298- * dwarf_stack - a DWARF stack contains a collection of DWARF frames299299- * @depth: the number of frames in the stack300300- * @level: an array of DWARF frames, indexed by stack level301301- *302302- */303303-struct dwarf_stack {304304- unsigned int depth;305305- struct dwarf_frame **level;302302+#define DWARF_VAL_OFFSET (1 << 1)303303+#define DWARF_UNDEFINED (1 << 2)306304};307305308306/*···362370#define DW_EXT_HI 0xffffffff363371#define DW_EXT_DWARF64 DW_EXT_HI364372365365-extern void dwarf_unwinder_init(void);366366-367373extern struct dwarf_frame *dwarf_unwind_stack(unsigned long,368374 struct dwarf_frame *);369369-#endif /* __ASSEMBLY__ */375375+#endif /* !__ASSEMBLY__ */370376371377#define CFI_STARTPROC .cfi_startproc372378#define CFI_ENDPROC .cfi_endproc373379#define CFI_DEF_CFA .cfi_def_cfa374380#define CFI_REGISTER .cfi_register375381#define CFI_REL_OFFSET .cfi_rel_offset382382+#define CFI_UNDEFINED .cfi_undefined376383377384#else378385···385394#define CFI_DEF_CFA CFI_IGNORE386395#define CFI_REGISTER CFI_IGNORE387396#define CFI_REL_OFFSET CFI_IGNORE397397+#define CFI_UNDEFINED CFI_IGNORE388398389399#ifndef __ASSEMBLY__390400static inline void dwarf_unwinder_init(void)
···2222 unsigned long *, const struct stacktrace_ops *,2323 void *);24242525+/*2626+ * Used by fault handling code to signal to the unwinder code that it2727+ * should switch to a different unwinder.2828+ */2929+extern int unwinder_faulted;3030+2531#endif /* _LINUX_UNWINDER_H */
+2
arch/sh/kernel/cpu/sh3/entry.S
···508508 bsr save_regs ! needs original pr value in k3509509 mov #-1, k2 ! default vector kept in k2510510511511+ setup_frame_reg512512+511513 stc sr, r0 ! get status register512514 shlr2 r0513515 and #0x3c, r0
+181-97
arch/sh/kernel/dwarf.c
···1111 *1212 * TODO:1313 * - DWARF64 doesn't work.1414+ * - Registers with DWARF_VAL_OFFSET rules aren't handled properly.1415 */15161617/* #define DEBUG */1718#include <linux/kernel.h>1819#include <linux/io.h>1920#include <linux/list.h>2121+#include <linux/mempool.h>2022#include <linux/mm.h>2123#include <asm/dwarf.h>2224#include <asm/unwinder.h>···2725#include <asm/dwarf.h>2826#include <asm/stacktrace.h>29272828+/* Reserve enough memory for two stack frames */2929+#define DWARF_FRAME_MIN_REQ 23030+/* ... with 4 registers per frame. */3131+#define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4)3232+3333+static struct kmem_cache *dwarf_frame_cachep;3434+static mempool_t *dwarf_frame_pool;3535+3636+static struct kmem_cache *dwarf_reg_cachep;3737+static mempool_t *dwarf_reg_pool;3838+3039static LIST_HEAD(dwarf_cie_list);3131-DEFINE_SPINLOCK(dwarf_cie_lock);4040+static DEFINE_SPINLOCK(dwarf_cie_lock);32413342static LIST_HEAD(dwarf_fde_list);3434-DEFINE_SPINLOCK(dwarf_fde_lock);4343+static DEFINE_SPINLOCK(dwarf_fde_lock);35443645static struct dwarf_cie *cached_cie;37463838-/*3939- * Figure out whether we need to allocate some dwarf registers. If dwarf4040- * registers have already been allocated then we may need to realloc4141- * them. "reg" is a register number that we need to be able to access4242- * after this call.4747+/**4848+ * dwarf_frame_alloc_reg - allocate memory for a DWARF register4949+ * @frame: the DWARF frame whose list of registers we insert on5050+ * @reg_num: the register number4351 *4444- * Register numbers start at zero, therefore we need to allocate space4545- * for "reg" + 1 registers.5252+ * Allocate space for, and initialise, a dwarf reg from5353+ * dwarf_reg_pool and insert it onto the (unsorted) linked-list of5454+ * dwarf registers for @frame.5555+ *5656+ * Return the initialised DWARF reg.4657 */4747-static void dwarf_frame_alloc_regs(struct dwarf_frame *frame,4848- unsigned int reg)5858+static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame,5959+ unsigned int reg_num)4960{5050- struct dwarf_reg *regs;5151- unsigned int num_regs = reg + 1;5252- size_t new_size;5353- size_t old_size;6161+ struct dwarf_reg *reg;54625555- new_size = num_regs * sizeof(*regs);5656- old_size = frame->num_regs * sizeof(*regs);5757-5858- /* Fast path: don't allocate any regs if we've already got enough. */5959- if (frame->num_regs >= num_regs)6060- return;6161-6262- regs = kzalloc(new_size, GFP_ATOMIC);6363- if (!regs) {6464- printk(KERN_WARNING "Unable to allocate DWARF registers\n");6363+ reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC);6464+ if (!reg) {6565+ printk(KERN_WARNING "Unable to allocate a DWARF register\n");6566 /*6667 * Let's just bomb hard here, we have no way to6768 * gracefully recover.6869 */6969- BUG();7070+ UNWINDER_BUG();7071 }71727272- if (frame->regs) {7373- memcpy(regs, frame->regs, old_size);7474- kfree(frame->regs);7373+ reg->number = reg_num;7474+ reg->addr = 0;7575+ reg->flags = 0;7676+7777+ list_add(®->link, &frame->reg_list);7878+7979+ return reg;8080+}8181+8282+static void dwarf_frame_free_regs(struct dwarf_frame *frame)8383+{8484+ struct dwarf_reg *reg, *n;8585+8686+ list_for_each_entry_safe(reg, n, &frame->reg_list, link) {8787+ list_del(®->link);8888+ mempool_free(reg, dwarf_reg_pool);8989+ }9090+}9191+9292+/**9393+ * dwarf_frame_reg - return a DWARF register9494+ * @frame: the DWARF frame to search in for @reg_num9595+ * @reg_num: the register number to search for9696+ *9797+ * Lookup and return the dwarf reg @reg_num for this frame. Return9898+ * NULL if @reg_num is an register invalid number.9999+ */100100+static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame,101101+ unsigned int reg_num)102102+{103103+ struct dwarf_reg *reg;104104+105105+ list_for_each_entry(reg, &frame->reg_list, link) {106106+ if (reg->number == reg_num)107107+ return reg;75108 }761097777- frame->regs = regs;7878- frame->num_regs = num_regs;110110+ return NULL;79111}8011281113/**···232196 break;233197 default:234198 pr_debug("encoding=0x%x\n", (encoding & 0x70));235235- BUG();199199+ UNWINDER_BUG();236200 }237201238202 if ((encoding & 0x07) == 0x00)···247211 break;248212 default:249213 pr_debug("encoding=0x%x\n", encoding);250250- BUG();214214+ UNWINDER_BUG();251215 }252216253217 return count;···300264 */301265static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr)302266{303303- struct dwarf_cie *cie, *n;267267+ struct dwarf_cie *cie;304268 unsigned long flags;305269306270 spin_lock_irqsave(&dwarf_cie_lock, flags);···314278 goto out;315279 }316280317317- list_for_each_entry_safe(cie, n, &dwarf_cie_list, link) {281281+ list_for_each_entry(cie, &dwarf_cie_list, link) {318282 if (cie->cie_pointer == cie_ptr) {319283 cached_cie = cie;320284 break;···335299 */336300struct dwarf_fde *dwarf_lookup_fde(unsigned long pc)337301{302302+ struct dwarf_fde *fde;338303 unsigned long flags;339339- struct dwarf_fde *fde, *n;340304341305 spin_lock_irqsave(&dwarf_fde_lock, flags);342342- list_for_each_entry_safe(fde, n, &dwarf_fde_list, link) {306306+307307+ list_for_each_entry(fde, &dwarf_fde_list, link) {343308 unsigned long start, end;344309345310 start = fde->initial_location;···383346 unsigned char insn;384347 unsigned char *current_insn;385348 unsigned int count, delta, reg, expr_len, offset;349349+ struct dwarf_reg *regp;386350387351 current_insn = insn_start;388352···406368 count = dwarf_read_uleb128(current_insn, &offset);407369 current_insn += count;408370 offset *= cie->data_alignment_factor;409409- dwarf_frame_alloc_regs(frame, reg);410410- frame->regs[reg].addr = offset;411411- frame->regs[reg].flags |= DWARF_REG_OFFSET;371371+ regp = dwarf_frame_alloc_reg(frame, reg);372372+ regp->addr = offset;373373+ regp->flags |= DWARF_REG_OFFSET;412374 continue;413375 /* NOTREACHED */414376 case DW_CFA_restore:···452414 case DW_CFA_undefined:453415 count = dwarf_read_uleb128(current_insn, ®);454416 current_insn += count;417417+ regp = dwarf_frame_alloc_reg(frame, reg);418418+ regp->flags |= DWARF_UNDEFINED;455419 break;456420 case DW_CFA_def_cfa:457421 count = dwarf_read_uleb128(current_insn,···492452 count = dwarf_read_leb128(current_insn, &offset);493453 current_insn += count;494454 offset *= cie->data_alignment_factor;495495- dwarf_frame_alloc_regs(frame, reg);496496- frame->regs[reg].flags |= DWARF_REG_OFFSET;497497- frame->regs[reg].addr = offset;455455+ regp = dwarf_frame_alloc_reg(frame, reg);456456+ regp->flags |= DWARF_REG_OFFSET;457457+ regp->addr = offset;498458 break;499459 case DW_CFA_val_offset:500460 count = dwarf_read_uleb128(current_insn, ®);501461 current_insn += count;502462 count = dwarf_read_leb128(current_insn, &offset);503463 offset *= cie->data_alignment_factor;504504- frame->regs[reg].flags |= DWARF_REG_OFFSET;505505- frame->regs[reg].addr = offset;464464+ regp = dwarf_frame_alloc_reg(frame, reg);465465+ regp->flags |= DWARF_VAL_OFFSET;466466+ regp->addr = offset;506467 break;507468 case DW_CFA_GNU_args_size:508469 count = dwarf_read_uleb128(current_insn, &offset);···514473 current_insn += count;515474 count = dwarf_read_uleb128(current_insn, &offset);516475 offset *= cie->data_alignment_factor;517517- dwarf_frame_alloc_regs(frame, reg);518518- frame->regs[reg].flags |= DWARF_REG_OFFSET;519519- frame->regs[reg].addr = -offset;476476+477477+ regp = dwarf_frame_alloc_reg(frame, reg);478478+ regp->flags |= DWARF_REG_OFFSET;479479+ regp->addr = -offset;520480 break;521481 default:522482 pr_debug("unhandled DWARF instruction 0x%x\n", insn);483483+ UNWINDER_BUG();523484 break;524485 }525486 }···538495 * on the callstack. Each of the lower (older) stack frames are539496 * linked via the "prev" member.540497 */541541-struct dwarf_frame *dwarf_unwind_stack(unsigned long pc,542542- struct dwarf_frame *prev)498498+struct dwarf_frame * dwarf_unwind_stack(unsigned long pc,499499+ struct dwarf_frame *prev)543500{544501 struct dwarf_frame *frame;545502 struct dwarf_cie *cie;546503 struct dwarf_fde *fde;504504+ struct dwarf_reg *reg;547505 unsigned long addr;548548- int i, offset;549506550507 /*551508 * If this is the first invocation of this recursive function we···558515 if (!pc && !prev)559516 pc = (unsigned long)current_text_addr();560517561561- frame = kzalloc(sizeof(*frame), GFP_ATOMIC);562562- if (!frame)563563- return NULL;518518+ frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC);519519+ if (!frame) {520520+ printk(KERN_ERR "Unable to allocate a dwarf frame\n");521521+ UNWINDER_BUG();522522+ }564523524524+ INIT_LIST_HEAD(&frame->reg_list);525525+ frame->flags = 0;565526 frame->prev = prev;527527+ frame->return_addr = 0;566528567529 fde = dwarf_lookup_fde(pc);568530 if (!fde) {···587539 * case above, which sucks because we could print a588540 * warning here.589541 */590590- return NULL;542542+ goto bail;591543 }592544593545 cie = dwarf_lookup_cie(fde->cie_pointer);···607559 switch (frame->flags) {608560 case DWARF_FRAME_CFA_REG_OFFSET:609561 if (prev) {610610- BUG_ON(!prev->regs[frame->cfa_register].flags);562562+ reg = dwarf_frame_reg(prev, frame->cfa_register);563563+ UNWINDER_BUG_ON(!reg);564564+ UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);611565612612- addr = prev->cfa;613613- addr += prev->regs[frame->cfa_register].addr;566566+ addr = prev->cfa + reg->addr;614567 frame->cfa = __raw_readl(addr);615568616569 } else {···628579 frame->cfa += frame->cfa_offset;629580 break;630581 default:631631- BUG();582582+ UNWINDER_BUG();632583 }633584634634- /* If we haven't seen the return address reg, we're screwed. */635635- BUG_ON(!frame->regs[DWARF_ARCH_RA_REG].flags);585585+ reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG);636586637637- for (i = 0; i <= frame->num_regs; i++) {638638- struct dwarf_reg *reg = &frame->regs[i];587587+ /*588588+ * If we haven't seen the return address register or the return589589+ * address column is undefined then we must assume that this is590590+ * the end of the callstack.591591+ */592592+ if (!reg || reg->flags == DWARF_UNDEFINED)593593+ goto bail;639594640640- if (!reg->flags)641641- continue;595595+ UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET);642596643643- offset = reg->addr;644644- offset += frame->cfa;645645- }646646-647647- addr = frame->cfa + frame->regs[DWARF_ARCH_RA_REG].addr;597597+ addr = frame->cfa + reg->addr;648598 frame->return_addr = __raw_readl(addr);649599650650- frame->next = dwarf_unwind_stack(frame->return_addr, frame);651600 return frame;601601+602602+bail:603603+ dwarf_frame_free_regs(frame);604604+ mempool_free(frame, dwarf_frame_pool);605605+ return NULL;652606}653607654608static int dwarf_parse_cie(void *entry, void *p, unsigned long len,···676624 cie->cie_pointer = (unsigned long)entry;677625678626 cie->version = *(char *)p++;679679- BUG_ON(cie->version != 1);627627+ UNWINDER_BUG_ON(cie->version != 1);680628681629 cie->augmentation = p;682630 p += strlen(cie->augmentation) + 1;···706654 count = dwarf_read_uleb128(p, &length);707655 p += count;708656709709- BUG_ON((unsigned char *)p > end);657657+ UNWINDER_BUG_ON((unsigned char *)p > end);710658711659 cie->initial_instructions = p + length;712660 cie->augmentation++;···734682 * routine in the CIE735683 * augmentation.736684 */737737- BUG();685685+ UNWINDER_BUG();738686 } else if (*cie->augmentation == 'S') {739739- BUG();687687+ UNWINDER_BUG();740688 } else {741689 /*742690 * Unknown augmentation. Assume743691 * 'z' augmentation.744692 */745693 p = cie->initial_instructions;746746- BUG_ON(!p);694694+ UNWINDER_BUG_ON(!p);747695 break;748696 }749697 }···760708}761709762710static int dwarf_parse_fde(void *entry, u32 entry_type,763763- void *start, unsigned long len)711711+ void *start, unsigned long len,712712+ unsigned char *end)764713{765714 struct dwarf_fde *fde;766715 struct dwarf_cie *cie;···808755809756 /* Call frame instructions. */810757 fde->instructions = p;811811- fde->end = start + len;758758+ fde->end = end;812759813760 /* Add to list. */814761 spin_lock_irqsave(&dwarf_fde_lock, flags);···818765 return 0;819766}820767821821-static void dwarf_unwinder_dump(struct task_struct *task, struct pt_regs *regs,768768+static void dwarf_unwinder_dump(struct task_struct *task,769769+ struct pt_regs *regs,822770 unsigned long *sp,823823- const struct stacktrace_ops *ops, void *data)771771+ const struct stacktrace_ops *ops,772772+ void *data)824773{825825- struct dwarf_frame *frame;774774+ struct dwarf_frame *frame, *_frame;775775+ unsigned long return_addr;826776827827- frame = dwarf_unwind_stack(0, NULL);777777+ _frame = NULL;778778+ return_addr = 0;828779829829- while (frame && frame->return_addr) {830830- ops->address(data, frame->return_addr, 1);831831- frame = frame->next;780780+ while (1) {781781+ frame = dwarf_unwind_stack(return_addr, _frame);782782+783783+ if (_frame) {784784+ dwarf_frame_free_regs(_frame);785785+ mempool_free(_frame, dwarf_frame_pool);786786+ }787787+788788+ _frame = frame;789789+790790+ if (!frame || !frame->return_addr)791791+ break;792792+793793+ return_addr = frame->return_addr;794794+ ops->address(data, return_addr, 1);832795 }833796}834797···856787857788static void dwarf_unwinder_cleanup(void)858789{859859- struct dwarf_cie *cie, *m;860860- struct dwarf_fde *fde, *n;861861- unsigned long flags;790790+ struct dwarf_cie *cie;791791+ struct dwarf_fde *fde;862792863793 /*864794 * Deallocate all the memory allocated for the DWARF unwinder.865795 * Traverse all the FDE/CIE lists and remove and free all the866796 * memory associated with those data structures.867797 */868868- spin_lock_irqsave(&dwarf_cie_lock, flags);869869- list_for_each_entry_safe(cie, m, &dwarf_cie_list, link)798798+ list_for_each_entry(cie, &dwarf_cie_list, link)870799 kfree(cie);871871- spin_unlock_irqrestore(&dwarf_cie_lock, flags);872800873873- spin_lock_irqsave(&dwarf_fde_lock, flags);874874- list_for_each_entry_safe(fde, n, &dwarf_fde_list, link)801801+ list_for_each_entry(fde, &dwarf_fde_list, link)875802 kfree(fde);876876- spin_unlock_irqrestore(&dwarf_fde_lock, flags);803803+804804+ kmem_cache_destroy(dwarf_reg_cachep);805805+ kmem_cache_destroy(dwarf_frame_cachep);877806}878807879808/**···883816 * easy to lookup the FDE for a given PC, so we build a list of FDE884817 * and CIE entries that make it easier.885818 */886886-void dwarf_unwinder_init(void)819819+static int __init dwarf_unwinder_init(void)887820{888821 u32 entry_type;889822 void *p, *entry;···897830 c_entries = 0;898831 f_entries = 0;899832 entry = &__start_eh_frame;833833+834834+ dwarf_frame_cachep = kmem_cache_create("dwarf_frames",835835+ sizeof(struct dwarf_frame), 0, SLAB_PANIC, NULL);836836+ dwarf_reg_cachep = kmem_cache_create("dwarf_regs",837837+ sizeof(struct dwarf_reg), 0, SLAB_PANIC, NULL);838838+839839+ dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ,840840+ mempool_alloc_slab,841841+ mempool_free_slab,842842+ dwarf_frame_cachep);843843+844844+ dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ,845845+ mempool_alloc_slab,846846+ mempool_free_slab,847847+ dwarf_reg_cachep);900848901849 while ((char *)entry < __stop_eh_frame) {902850 p = entry;···942860 else943861 c_entries++;944862 } else {945945- err = dwarf_parse_fde(entry, entry_type, p, len);863863+ err = dwarf_parse_fde(entry, entry_type, p, len, end);946864 if (err < 0)947865 goto out;948866 else···959877 if (err)960878 goto out;961879962962- return;880880+ return 0;963881964882out:965883 printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err);966884 dwarf_unwinder_cleanup();885885+ return -EINVAL;967886}887887+early_initcall(dwarf_unwinder_init);
-4
arch/sh/kernel/irq.c
···1414#include <asm/processor.h>1515#include <asm/machvec.h>1616#include <asm/uaccess.h>1717-#include <asm/dwarf.h>1817#include <asm/thread_info.h>1918#include <cpu/mmu_context.h>2019···261262 sh_mv.mv_init_irq();262263263264 irq_ctx_init(smp_processor_id());264264-265265- /* This needs to be early, but not too early.. */266266- dwarf_unwinder_init();267265}268266269267#ifdef CONFIG_SPARSE_IRQ
···1111#include <linux/errno.h>1212#include <linux/list.h>1313#include <linux/spinlock.h>1414+#include <linux/module.h>1415#include <asm/unwinder.h>1516#include <asm/atomic.h>1617···5352};54535554static DEFINE_SPINLOCK(unwinder_lock);5656-5757-static atomic_t unwinder_running = ATOMIC_INIT(0);58555956/**6057 * select_unwinder - Select the best registered stack unwinder.···121122 return ret;122123}123124125125+int unwinder_faulted = 0;126126+124127/*125128 * Unwind the call stack and pass information to the stacktrace_ops126129 * functions. Also handle the case where we need to switch to a new···145144 * Hopefully this will give us a semi-reliable stacktrace so we146145 * can diagnose why curr_unwinder->dump() faulted.147146 */148148- if (atomic_inc_return(&unwinder_running) != 1) {147147+ if (unwinder_faulted) {149148 spin_lock_irqsave(&unwinder_lock, flags);150149151151- if (!list_is_singular(&unwinder_list)) {150150+ /* Make sure no one beat us to changing the unwinder */151151+ if (unwinder_faulted && !list_is_singular(&unwinder_list)) {152152 list_del(&curr_unwinder->list);153153 curr_unwinder = select_unwinder();154154+155155+ unwinder_faulted = 0;154156 }155157156158 spin_unlock_irqrestore(&unwinder_lock, flags);157157- atomic_dec(&unwinder_running);158159 }159160160161 curr_unwinder->dump(task, regs, sp, ops, data);161161-162162- atomic_dec(&unwinder_running);163162}163163+EXPORT_SYMBOL_GPL(unwind_stack);