Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Matt Fleming <matt@console-pimps.org> |
| 3 | * |
| 4 | * This file is subject to the terms and conditions of the GNU General Public |
| 5 | * License. See the file "COPYING" in the main directory of this archive |
| 6 | * for more details. |
| 7 | * |
| 8 | * This is an implementation of a DWARF unwinder. Its main purpose is |
| 9 | * for generating stacktrace information. Based on the DWARF 3 |
| 10 | * specification from http://www.dwarfstd.org. |
| 11 | * |
| 12 | * TODO: |
| 13 | * - DWARF64 doesn't work. |
Matt Fleming | 97efbbd | 2009-08-16 15:56:35 +0100 | [diff] [blame] | 14 | * - Registers with DWARF_VAL_OFFSET rules aren't handled properly. |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | /* #define DEBUG */ |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/list.h> |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 21 | #include <linux/mempool.h> |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 22 | #include <linux/mm.h> |
| 23 | #include <asm/dwarf.h> |
| 24 | #include <asm/unwinder.h> |
| 25 | #include <asm/sections.h> |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 27 | #include <asm/dwarf.h> |
| 28 | #include <asm/stacktrace.h> |
| 29 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 30 | /* Reserve enough memory for two stack frames */ |
| 31 | #define DWARF_FRAME_MIN_REQ 2 |
| 32 | /* ... with 4 registers per frame. */ |
| 33 | #define DWARF_REG_MIN_REQ (DWARF_FRAME_MIN_REQ * 4) |
| 34 | |
| 35 | static struct kmem_cache *dwarf_frame_cachep; |
| 36 | static mempool_t *dwarf_frame_pool; |
| 37 | |
| 38 | static struct kmem_cache *dwarf_reg_cachep; |
| 39 | static mempool_t *dwarf_reg_pool; |
| 40 | |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 41 | static LIST_HEAD(dwarf_cie_list); |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 42 | static DEFINE_SPINLOCK(dwarf_cie_lock); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 43 | |
| 44 | static LIST_HEAD(dwarf_fde_list); |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 45 | static DEFINE_SPINLOCK(dwarf_fde_lock); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 46 | |
| 47 | static struct dwarf_cie *cached_cie; |
| 48 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 49 | /** |
| 50 | * dwarf_frame_alloc_reg - allocate memory for a DWARF register |
| 51 | * @frame: the DWARF frame whose list of registers we insert on |
| 52 | * @reg_num: the register number |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 53 | * |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 54 | * Allocate space for, and initialise, a dwarf reg from |
| 55 | * dwarf_reg_pool and insert it onto the (unsorted) linked-list of |
| 56 | * dwarf registers for @frame. |
| 57 | * |
| 58 | * Return the initialised DWARF reg. |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 59 | */ |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 60 | static struct dwarf_reg *dwarf_frame_alloc_reg(struct dwarf_frame *frame, |
| 61 | unsigned int reg_num) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 62 | { |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 63 | struct dwarf_reg *reg; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 64 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 65 | reg = mempool_alloc(dwarf_reg_pool, GFP_ATOMIC); |
| 66 | if (!reg) { |
| 67 | printk(KERN_WARNING "Unable to allocate a DWARF register\n"); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 68 | /* |
| 69 | * Let's just bomb hard here, we have no way to |
| 70 | * gracefully recover. |
| 71 | */ |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 72 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 73 | } |
| 74 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 75 | reg->number = reg_num; |
| 76 | reg->addr = 0; |
| 77 | reg->flags = 0; |
| 78 | |
| 79 | list_add(®->link, &frame->reg_list); |
| 80 | |
| 81 | return reg; |
| 82 | } |
| 83 | |
| 84 | static void dwarf_frame_free_regs(struct dwarf_frame *frame) |
| 85 | { |
| 86 | struct dwarf_reg *reg, *n; |
| 87 | |
| 88 | list_for_each_entry_safe(reg, n, &frame->reg_list, link) { |
| 89 | list_del(®->link); |
| 90 | mempool_free(reg, dwarf_reg_pool); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * dwarf_frame_reg - return a DWARF register |
| 96 | * @frame: the DWARF frame to search in for @reg_num |
| 97 | * @reg_num: the register number to search for |
| 98 | * |
| 99 | * Lookup and return the dwarf reg @reg_num for this frame. Return |
| 100 | * NULL if @reg_num is an register invalid number. |
| 101 | */ |
| 102 | static struct dwarf_reg *dwarf_frame_reg(struct dwarf_frame *frame, |
| 103 | unsigned int reg_num) |
| 104 | { |
| 105 | struct dwarf_reg *reg; |
| 106 | |
| 107 | list_for_each_entry(reg, &frame->reg_list, link) { |
| 108 | if (reg->number == reg_num) |
| 109 | return reg; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 112 | return NULL; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * dwarf_read_addr - read dwarf data |
| 117 | * @src: source address of data |
| 118 | * @dst: destination address to store the data to |
| 119 | * |
| 120 | * Read 'n' bytes from @src, where 'n' is the size of an address on |
| 121 | * the native machine. We return the number of bytes read, which |
| 122 | * should always be 'n'. We also have to be careful when reading |
| 123 | * from @src and writing to @dst, because they can be arbitrarily |
| 124 | * aligned. Return 'n' - the number of bytes read. |
| 125 | */ |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 126 | static inline int dwarf_read_addr(unsigned long *src, unsigned long *dst) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 127 | { |
Paul Mundt | bf43a16 | 2009-08-14 03:06:13 +0900 | [diff] [blame] | 128 | u32 val = get_unaligned(src); |
| 129 | put_unaligned(val, dst); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 130 | return sizeof(unsigned long *); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * dwarf_read_uleb128 - read unsigned LEB128 data |
| 135 | * @addr: the address where the ULEB128 data is stored |
| 136 | * @ret: address to store the result |
| 137 | * |
| 138 | * Decode an unsigned LEB128 encoded datum. The algorithm is taken |
| 139 | * from Appendix C of the DWARF 3 spec. For information on the |
| 140 | * encodings refer to section "7.6 - Variable Length Data". Return |
| 141 | * the number of bytes read. |
| 142 | */ |
| 143 | static inline unsigned long dwarf_read_uleb128(char *addr, unsigned int *ret) |
| 144 | { |
| 145 | unsigned int result; |
| 146 | unsigned char byte; |
| 147 | int shift, count; |
| 148 | |
| 149 | result = 0; |
| 150 | shift = 0; |
| 151 | count = 0; |
| 152 | |
| 153 | while (1) { |
| 154 | byte = __raw_readb(addr); |
| 155 | addr++; |
| 156 | count++; |
| 157 | |
| 158 | result |= (byte & 0x7f) << shift; |
| 159 | shift += 7; |
| 160 | |
| 161 | if (!(byte & 0x80)) |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | *ret = result; |
| 166 | |
| 167 | return count; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * dwarf_read_leb128 - read signed LEB128 data |
| 172 | * @addr: the address of the LEB128 encoded data |
| 173 | * @ret: address to store the result |
| 174 | * |
| 175 | * Decode signed LEB128 data. The algorithm is taken from Appendix |
| 176 | * C of the DWARF 3 spec. Return the number of bytes read. |
| 177 | */ |
| 178 | static inline unsigned long dwarf_read_leb128(char *addr, int *ret) |
| 179 | { |
| 180 | unsigned char byte; |
| 181 | int result, shift; |
| 182 | int num_bits; |
| 183 | int count; |
| 184 | |
| 185 | result = 0; |
| 186 | shift = 0; |
| 187 | count = 0; |
| 188 | |
| 189 | while (1) { |
| 190 | byte = __raw_readb(addr); |
| 191 | addr++; |
| 192 | result |= (byte & 0x7f) << shift; |
| 193 | shift += 7; |
| 194 | count++; |
| 195 | |
| 196 | if (!(byte & 0x80)) |
| 197 | break; |
| 198 | } |
| 199 | |
| 200 | /* The number of bits in a signed integer. */ |
| 201 | num_bits = 8 * sizeof(result); |
| 202 | |
| 203 | if ((shift < num_bits) && (byte & 0x40)) |
| 204 | result |= (-1 << shift); |
| 205 | |
| 206 | *ret = result; |
| 207 | |
| 208 | return count; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * dwarf_read_encoded_value - return the decoded value at @addr |
| 213 | * @addr: the address of the encoded value |
| 214 | * @val: where to write the decoded value |
| 215 | * @encoding: the encoding with which we can decode @addr |
| 216 | * |
| 217 | * GCC emits encoded address in the .eh_frame FDE entries. Decode |
| 218 | * the value at @addr using @encoding. The decoded value is written |
| 219 | * to @val and the number of bytes read is returned. |
| 220 | */ |
| 221 | static int dwarf_read_encoded_value(char *addr, unsigned long *val, |
| 222 | char encoding) |
| 223 | { |
| 224 | unsigned long decoded_addr = 0; |
| 225 | int count = 0; |
| 226 | |
| 227 | switch (encoding & 0x70) { |
| 228 | case DW_EH_PE_absptr: |
| 229 | break; |
| 230 | case DW_EH_PE_pcrel: |
| 231 | decoded_addr = (unsigned long)addr; |
| 232 | break; |
| 233 | default: |
| 234 | pr_debug("encoding=0x%x\n", (encoding & 0x70)); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 235 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if ((encoding & 0x07) == 0x00) |
| 239 | encoding |= DW_EH_PE_udata4; |
| 240 | |
| 241 | switch (encoding & 0x0f) { |
| 242 | case DW_EH_PE_sdata4: |
| 243 | case DW_EH_PE_udata4: |
| 244 | count += 4; |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 245 | decoded_addr += get_unaligned((u32 *)addr); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 246 | __raw_writel(decoded_addr, val); |
| 247 | break; |
| 248 | default: |
| 249 | pr_debug("encoding=0x%x\n", encoding); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 250 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return count; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * dwarf_entry_len - return the length of an FDE or CIE |
| 258 | * @addr: the address of the entry |
| 259 | * @len: the length of the entry |
| 260 | * |
| 261 | * Read the initial_length field of the entry and store the size of |
| 262 | * the entry in @len. We return the number of bytes read. Return a |
| 263 | * count of 0 on error. |
| 264 | */ |
| 265 | static inline int dwarf_entry_len(char *addr, unsigned long *len) |
| 266 | { |
| 267 | u32 initial_len; |
| 268 | int count; |
| 269 | |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 270 | initial_len = get_unaligned((u32 *)addr); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 271 | count = 4; |
| 272 | |
| 273 | /* |
| 274 | * An initial length field value in the range DW_LEN_EXT_LO - |
| 275 | * DW_LEN_EXT_HI indicates an extension, and should not be |
| 276 | * interpreted as a length. The only extension that we currently |
| 277 | * understand is the use of DWARF64 addresses. |
| 278 | */ |
| 279 | if (initial_len >= DW_EXT_LO && initial_len <= DW_EXT_HI) { |
| 280 | /* |
| 281 | * The 64-bit length field immediately follows the |
| 282 | * compulsory 32-bit length field. |
| 283 | */ |
| 284 | if (initial_len == DW_EXT_DWARF64) { |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 285 | *len = get_unaligned((u64 *)addr + 4); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 286 | count = 12; |
| 287 | } else { |
| 288 | printk(KERN_WARNING "Unknown DWARF extension\n"); |
| 289 | count = 0; |
| 290 | } |
| 291 | } else |
| 292 | *len = initial_len; |
| 293 | |
| 294 | return count; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * dwarf_lookup_cie - locate the cie |
| 299 | * @cie_ptr: pointer to help with lookup |
| 300 | */ |
| 301 | static struct dwarf_cie *dwarf_lookup_cie(unsigned long cie_ptr) |
| 302 | { |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 303 | struct dwarf_cie *cie; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 304 | unsigned long flags; |
| 305 | |
| 306 | spin_lock_irqsave(&dwarf_cie_lock, flags); |
| 307 | |
| 308 | /* |
| 309 | * We've cached the last CIE we looked up because chances are |
| 310 | * that the FDE wants this CIE. |
| 311 | */ |
| 312 | if (cached_cie && cached_cie->cie_pointer == cie_ptr) { |
| 313 | cie = cached_cie; |
| 314 | goto out; |
| 315 | } |
| 316 | |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 317 | list_for_each_entry(cie, &dwarf_cie_list, link) { |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 318 | if (cie->cie_pointer == cie_ptr) { |
| 319 | cached_cie = cie; |
| 320 | break; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /* Couldn't find the entry in the list. */ |
| 325 | if (&cie->link == &dwarf_cie_list) |
| 326 | cie = NULL; |
| 327 | out: |
| 328 | spin_unlock_irqrestore(&dwarf_cie_lock, flags); |
| 329 | return cie; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * dwarf_lookup_fde - locate the FDE that covers pc |
| 334 | * @pc: the program counter |
| 335 | */ |
| 336 | struct dwarf_fde *dwarf_lookup_fde(unsigned long pc) |
| 337 | { |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 338 | struct dwarf_fde *fde; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 339 | unsigned long flags; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 340 | |
| 341 | spin_lock_irqsave(&dwarf_fde_lock, flags); |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 342 | |
| 343 | list_for_each_entry(fde, &dwarf_fde_list, link) { |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 344 | unsigned long start, end; |
| 345 | |
| 346 | start = fde->initial_location; |
| 347 | end = fde->initial_location + fde->address_range; |
| 348 | |
| 349 | if (pc >= start && pc < end) |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | /* Couldn't find the entry in the list. */ |
| 354 | if (&fde->link == &dwarf_fde_list) |
| 355 | fde = NULL; |
| 356 | |
| 357 | spin_unlock_irqrestore(&dwarf_fde_lock, flags); |
| 358 | |
| 359 | return fde; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * dwarf_cfa_execute_insns - execute instructions to calculate a CFA |
| 364 | * @insn_start: address of the first instruction |
| 365 | * @insn_end: address of the last instruction |
| 366 | * @cie: the CIE for this function |
| 367 | * @fde: the FDE for this function |
| 368 | * @frame: the instructions calculate the CFA for this frame |
| 369 | * @pc: the program counter of the address we're interested in |
| 370 | * |
| 371 | * Execute the Call Frame instruction sequence starting at |
| 372 | * @insn_start and ending at @insn_end. The instructions describe |
| 373 | * how to calculate the Canonical Frame Address of a stackframe. |
| 374 | * Store the results in @frame. |
| 375 | */ |
| 376 | static int dwarf_cfa_execute_insns(unsigned char *insn_start, |
| 377 | unsigned char *insn_end, |
| 378 | struct dwarf_cie *cie, |
| 379 | struct dwarf_fde *fde, |
| 380 | struct dwarf_frame *frame, |
Matt Fleming | b955873 | 2009-08-15 23:10:57 +0100 | [diff] [blame] | 381 | unsigned long pc) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 382 | { |
| 383 | unsigned char insn; |
| 384 | unsigned char *current_insn; |
| 385 | unsigned int count, delta, reg, expr_len, offset; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 386 | struct dwarf_reg *regp; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 387 | |
| 388 | current_insn = insn_start; |
| 389 | |
Matt Fleming | b955873 | 2009-08-15 23:10:57 +0100 | [diff] [blame] | 390 | while (current_insn < insn_end && frame->pc <= pc) { |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 391 | insn = __raw_readb(current_insn++); |
| 392 | |
| 393 | /* |
| 394 | * Firstly, handle the opcodes that embed their operands |
| 395 | * in the instructions. |
| 396 | */ |
| 397 | switch (DW_CFA_opcode(insn)) { |
| 398 | case DW_CFA_advance_loc: |
| 399 | delta = DW_CFA_operand(insn); |
| 400 | delta *= cie->code_alignment_factor; |
| 401 | frame->pc += delta; |
| 402 | continue; |
| 403 | /* NOTREACHED */ |
| 404 | case DW_CFA_offset: |
| 405 | reg = DW_CFA_operand(insn); |
| 406 | count = dwarf_read_uleb128(current_insn, &offset); |
| 407 | current_insn += count; |
| 408 | offset *= cie->data_alignment_factor; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 409 | regp = dwarf_frame_alloc_reg(frame, reg); |
| 410 | regp->addr = offset; |
| 411 | regp->flags |= DWARF_REG_OFFSET; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 412 | continue; |
| 413 | /* NOTREACHED */ |
| 414 | case DW_CFA_restore: |
| 415 | reg = DW_CFA_operand(insn); |
| 416 | continue; |
| 417 | /* NOTREACHED */ |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Secondly, handle the opcodes that don't embed their |
| 422 | * operands in the instruction. |
| 423 | */ |
| 424 | switch (insn) { |
| 425 | case DW_CFA_nop: |
| 426 | continue; |
| 427 | case DW_CFA_advance_loc1: |
| 428 | delta = *current_insn++; |
| 429 | frame->pc += delta * cie->code_alignment_factor; |
| 430 | break; |
| 431 | case DW_CFA_advance_loc2: |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 432 | delta = get_unaligned((u16 *)current_insn); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 433 | current_insn += 2; |
| 434 | frame->pc += delta * cie->code_alignment_factor; |
| 435 | break; |
| 436 | case DW_CFA_advance_loc4: |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 437 | delta = get_unaligned((u32 *)current_insn); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 438 | current_insn += 4; |
| 439 | frame->pc += delta * cie->code_alignment_factor; |
| 440 | break; |
| 441 | case DW_CFA_offset_extended: |
| 442 | count = dwarf_read_uleb128(current_insn, ®); |
| 443 | current_insn += count; |
| 444 | count = dwarf_read_uleb128(current_insn, &offset); |
| 445 | current_insn += count; |
| 446 | offset *= cie->data_alignment_factor; |
| 447 | break; |
| 448 | case DW_CFA_restore_extended: |
| 449 | count = dwarf_read_uleb128(current_insn, ®); |
| 450 | current_insn += count; |
| 451 | break; |
| 452 | case DW_CFA_undefined: |
| 453 | count = dwarf_read_uleb128(current_insn, ®); |
| 454 | current_insn += count; |
| 455 | break; |
| 456 | case DW_CFA_def_cfa: |
| 457 | count = dwarf_read_uleb128(current_insn, |
| 458 | &frame->cfa_register); |
| 459 | current_insn += count; |
| 460 | count = dwarf_read_uleb128(current_insn, |
| 461 | &frame->cfa_offset); |
| 462 | current_insn += count; |
| 463 | |
| 464 | frame->flags |= DWARF_FRAME_CFA_REG_OFFSET; |
| 465 | break; |
| 466 | case DW_CFA_def_cfa_register: |
| 467 | count = dwarf_read_uleb128(current_insn, |
| 468 | &frame->cfa_register); |
| 469 | current_insn += count; |
| 470 | frame->flags |= DWARF_FRAME_CFA_REG_OFFSET; |
| 471 | break; |
| 472 | case DW_CFA_def_cfa_offset: |
| 473 | count = dwarf_read_uleb128(current_insn, &offset); |
| 474 | current_insn += count; |
| 475 | frame->cfa_offset = offset; |
| 476 | break; |
| 477 | case DW_CFA_def_cfa_expression: |
| 478 | count = dwarf_read_uleb128(current_insn, &expr_len); |
| 479 | current_insn += count; |
| 480 | |
| 481 | frame->cfa_expr = current_insn; |
| 482 | frame->cfa_expr_len = expr_len; |
| 483 | current_insn += expr_len; |
| 484 | |
| 485 | frame->flags |= DWARF_FRAME_CFA_REG_EXP; |
| 486 | break; |
| 487 | case DW_CFA_offset_extended_sf: |
| 488 | count = dwarf_read_uleb128(current_insn, ®); |
| 489 | current_insn += count; |
| 490 | count = dwarf_read_leb128(current_insn, &offset); |
| 491 | current_insn += count; |
| 492 | offset *= cie->data_alignment_factor; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 493 | regp = dwarf_frame_alloc_reg(frame, reg); |
| 494 | regp->flags |= DWARF_REG_OFFSET; |
| 495 | regp->addr = offset; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 496 | break; |
| 497 | case DW_CFA_val_offset: |
| 498 | count = dwarf_read_uleb128(current_insn, ®); |
| 499 | current_insn += count; |
| 500 | count = dwarf_read_leb128(current_insn, &offset); |
| 501 | offset *= cie->data_alignment_factor; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 502 | regp = dwarf_frame_alloc_reg(frame, reg); |
Matt Fleming | 97efbbd | 2009-08-16 15:56:35 +0100 | [diff] [blame] | 503 | regp->flags |= DWARF_VAL_OFFSET; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 504 | regp->addr = offset; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 505 | break; |
Matt Fleming | cd7246f | 2009-08-16 01:44:33 +0100 | [diff] [blame] | 506 | case DW_CFA_GNU_args_size: |
| 507 | count = dwarf_read_uleb128(current_insn, &offset); |
| 508 | current_insn += count; |
| 509 | break; |
| 510 | case DW_CFA_GNU_negative_offset_extended: |
| 511 | count = dwarf_read_uleb128(current_insn, ®); |
| 512 | current_insn += count; |
| 513 | count = dwarf_read_uleb128(current_insn, &offset); |
| 514 | offset *= cie->data_alignment_factor; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 515 | |
| 516 | regp = dwarf_frame_alloc_reg(frame, reg); |
| 517 | regp->flags |= DWARF_REG_OFFSET; |
| 518 | regp->addr = -offset; |
Matt Fleming | cd7246f | 2009-08-16 01:44:33 +0100 | [diff] [blame] | 519 | break; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 520 | default: |
| 521 | pr_debug("unhandled DWARF instruction 0x%x\n", insn); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 522 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * dwarf_unwind_stack - recursively unwind the stack |
| 532 | * @pc: address of the function to unwind |
| 533 | * @prev: struct dwarf_frame of the previous stackframe on the callstack |
| 534 | * |
| 535 | * Return a struct dwarf_frame representing the most recent frame |
| 536 | * on the callstack. Each of the lower (older) stack frames are |
| 537 | * linked via the "prev" member. |
| 538 | */ |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 539 | struct dwarf_frame * dwarf_unwind_stack(unsigned long pc, |
| 540 | struct dwarf_frame *prev) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 541 | { |
| 542 | struct dwarf_frame *frame; |
| 543 | struct dwarf_cie *cie; |
| 544 | struct dwarf_fde *fde; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 545 | struct dwarf_reg *reg; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 546 | unsigned long addr; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 547 | |
| 548 | /* |
| 549 | * If this is the first invocation of this recursive function we |
| 550 | * need get the contents of a physical register to get the CFA |
| 551 | * in order to begin the virtual unwinding of the stack. |
| 552 | * |
Matt Fleming | f826466 | 2009-08-13 20:41:31 +0100 | [diff] [blame] | 553 | * NOTE: the return address is guaranteed to be setup by the |
| 554 | * time this function makes its first function call. |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 555 | */ |
Matt Fleming | b955873 | 2009-08-15 23:10:57 +0100 | [diff] [blame] | 556 | if (!pc && !prev) |
| 557 | pc = (unsigned long)current_text_addr(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 558 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 559 | frame = mempool_alloc(dwarf_frame_pool, GFP_ATOMIC); |
| 560 | if (!frame) { |
| 561 | printk(KERN_ERR "Unable to allocate a dwarf frame\n"); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 562 | UNWINDER_BUG(); |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 563 | } |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 564 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 565 | INIT_LIST_HEAD(&frame->reg_list); |
| 566 | frame->flags = 0; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 567 | frame->prev = prev; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 568 | frame->return_addr = 0; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 569 | |
| 570 | fde = dwarf_lookup_fde(pc); |
| 571 | if (!fde) { |
| 572 | /* |
| 573 | * This is our normal exit path - the one that stops the |
| 574 | * recursion. There's two reasons why we might exit |
| 575 | * here, |
| 576 | * |
| 577 | * a) pc has no asscociated DWARF frame info and so |
| 578 | * we don't know how to unwind this frame. This is |
| 579 | * usually the case when we're trying to unwind a |
| 580 | * frame that was called from some assembly code |
| 581 | * that has no DWARF info, e.g. syscalls. |
| 582 | * |
| 583 | * b) the DEBUG info for pc is bogus. There's |
| 584 | * really no way to distinguish this case from the |
| 585 | * case above, which sucks because we could print a |
| 586 | * warning here. |
| 587 | */ |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 588 | goto bail; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | cie = dwarf_lookup_cie(fde->cie_pointer); |
| 592 | |
| 593 | frame->pc = fde->initial_location; |
| 594 | |
| 595 | /* CIE initial instructions */ |
| 596 | dwarf_cfa_execute_insns(cie->initial_instructions, |
Matt Fleming | f826466 | 2009-08-13 20:41:31 +0100 | [diff] [blame] | 597 | cie->instructions_end, cie, fde, |
Matt Fleming | b955873 | 2009-08-15 23:10:57 +0100 | [diff] [blame] | 598 | frame, pc); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 599 | |
| 600 | /* FDE instructions */ |
| 601 | dwarf_cfa_execute_insns(fde->instructions, fde->end, cie, |
Matt Fleming | b955873 | 2009-08-15 23:10:57 +0100 | [diff] [blame] | 602 | fde, frame, pc); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 603 | |
| 604 | /* Calculate the CFA */ |
| 605 | switch (frame->flags) { |
| 606 | case DWARF_FRAME_CFA_REG_OFFSET: |
| 607 | if (prev) { |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 608 | reg = dwarf_frame_reg(prev, frame->cfa_register); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 609 | UNWINDER_BUG_ON(!reg); |
| 610 | UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 611 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 612 | addr = prev->cfa + reg->addr; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 613 | frame->cfa = __raw_readl(addr); |
| 614 | |
| 615 | } else { |
| 616 | /* |
| 617 | * Again, this is the first invocation of this |
| 618 | * recurisve function. We need to physically |
| 619 | * read the contents of a register in order to |
| 620 | * get the Canonical Frame Address for this |
| 621 | * function. |
| 622 | */ |
| 623 | frame->cfa = dwarf_read_arch_reg(frame->cfa_register); |
| 624 | } |
| 625 | |
| 626 | frame->cfa += frame->cfa_offset; |
| 627 | break; |
| 628 | default: |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 629 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | /* If we haven't seen the return address reg, we're screwed. */ |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 633 | reg = dwarf_frame_reg(frame, DWARF_ARCH_RA_REG); |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 634 | UNWINDER_BUG_ON(!reg); |
| 635 | UNWINDER_BUG_ON(reg->flags != DWARF_REG_OFFSET); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 636 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 637 | addr = frame->cfa + reg->addr; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 638 | frame->return_addr = __raw_readl(addr); |
| 639 | |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 640 | return frame; |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 641 | |
| 642 | bail: |
| 643 | dwarf_frame_free_regs(frame); |
| 644 | mempool_free(frame, dwarf_frame_pool); |
| 645 | return NULL; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static int dwarf_parse_cie(void *entry, void *p, unsigned long len, |
| 649 | unsigned char *end) |
| 650 | { |
| 651 | struct dwarf_cie *cie; |
| 652 | unsigned long flags; |
| 653 | int count; |
| 654 | |
| 655 | cie = kzalloc(sizeof(*cie), GFP_KERNEL); |
| 656 | if (!cie) |
| 657 | return -ENOMEM; |
| 658 | |
| 659 | cie->length = len; |
| 660 | |
| 661 | /* |
| 662 | * Record the offset into the .eh_frame section |
| 663 | * for this CIE. It allows this CIE to be |
| 664 | * quickly and easily looked up from the |
| 665 | * corresponding FDE. |
| 666 | */ |
| 667 | cie->cie_pointer = (unsigned long)entry; |
| 668 | |
| 669 | cie->version = *(char *)p++; |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 670 | UNWINDER_BUG_ON(cie->version != 1); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 671 | |
| 672 | cie->augmentation = p; |
| 673 | p += strlen(cie->augmentation) + 1; |
| 674 | |
| 675 | count = dwarf_read_uleb128(p, &cie->code_alignment_factor); |
| 676 | p += count; |
| 677 | |
| 678 | count = dwarf_read_leb128(p, &cie->data_alignment_factor); |
| 679 | p += count; |
| 680 | |
| 681 | /* |
| 682 | * Which column in the rule table contains the |
| 683 | * return address? |
| 684 | */ |
| 685 | if (cie->version == 1) { |
| 686 | cie->return_address_reg = __raw_readb(p); |
| 687 | p++; |
| 688 | } else { |
| 689 | count = dwarf_read_uleb128(p, &cie->return_address_reg); |
| 690 | p += count; |
| 691 | } |
| 692 | |
| 693 | if (cie->augmentation[0] == 'z') { |
| 694 | unsigned int length, count; |
| 695 | cie->flags |= DWARF_CIE_Z_AUGMENTATION; |
| 696 | |
| 697 | count = dwarf_read_uleb128(p, &length); |
| 698 | p += count; |
| 699 | |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 700 | UNWINDER_BUG_ON((unsigned char *)p > end); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 701 | |
| 702 | cie->initial_instructions = p + length; |
| 703 | cie->augmentation++; |
| 704 | } |
| 705 | |
| 706 | while (*cie->augmentation) { |
| 707 | /* |
| 708 | * "L" indicates a byte showing how the |
| 709 | * LSDA pointer is encoded. Skip it. |
| 710 | */ |
| 711 | if (*cie->augmentation == 'L') { |
| 712 | p++; |
| 713 | cie->augmentation++; |
| 714 | } else if (*cie->augmentation == 'R') { |
| 715 | /* |
| 716 | * "R" indicates a byte showing |
| 717 | * how FDE addresses are |
| 718 | * encoded. |
| 719 | */ |
| 720 | cie->encoding = *(char *)p++; |
| 721 | cie->augmentation++; |
| 722 | } else if (*cie->augmentation == 'P') { |
| 723 | /* |
| 724 | * "R" indicates a personality |
| 725 | * routine in the CIE |
| 726 | * augmentation. |
| 727 | */ |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 728 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 729 | } else if (*cie->augmentation == 'S') { |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 730 | UNWINDER_BUG(); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 731 | } else { |
| 732 | /* |
| 733 | * Unknown augmentation. Assume |
| 734 | * 'z' augmentation. |
| 735 | */ |
| 736 | p = cie->initial_instructions; |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 737 | UNWINDER_BUG_ON(!p); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 738 | break; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | cie->initial_instructions = p; |
| 743 | cie->instructions_end = end; |
| 744 | |
| 745 | /* Add to list */ |
| 746 | spin_lock_irqsave(&dwarf_cie_lock, flags); |
| 747 | list_add_tail(&cie->link, &dwarf_cie_list); |
| 748 | spin_unlock_irqrestore(&dwarf_cie_lock, flags); |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int dwarf_parse_fde(void *entry, u32 entry_type, |
Matt Fleming | 5480675 | 2009-08-20 19:42:34 +0100 | [diff] [blame^] | 754 | void *start, unsigned long len, |
| 755 | unsigned char *end) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 756 | { |
| 757 | struct dwarf_fde *fde; |
| 758 | struct dwarf_cie *cie; |
| 759 | unsigned long flags; |
| 760 | int count; |
| 761 | void *p = start; |
| 762 | |
| 763 | fde = kzalloc(sizeof(*fde), GFP_KERNEL); |
| 764 | if (!fde) |
| 765 | return -ENOMEM; |
| 766 | |
| 767 | fde->length = len; |
| 768 | |
| 769 | /* |
| 770 | * In a .eh_frame section the CIE pointer is the |
| 771 | * delta between the address within the FDE |
| 772 | */ |
| 773 | fde->cie_pointer = (unsigned long)(p - entry_type - 4); |
| 774 | |
| 775 | cie = dwarf_lookup_cie(fde->cie_pointer); |
| 776 | fde->cie = cie; |
| 777 | |
| 778 | if (cie->encoding) |
| 779 | count = dwarf_read_encoded_value(p, &fde->initial_location, |
| 780 | cie->encoding); |
| 781 | else |
| 782 | count = dwarf_read_addr(p, &fde->initial_location); |
| 783 | |
| 784 | p += count; |
| 785 | |
| 786 | if (cie->encoding) |
| 787 | count = dwarf_read_encoded_value(p, &fde->address_range, |
| 788 | cie->encoding & 0x0f); |
| 789 | else |
| 790 | count = dwarf_read_addr(p, &fde->address_range); |
| 791 | |
| 792 | p += count; |
| 793 | |
| 794 | if (fde->cie->flags & DWARF_CIE_Z_AUGMENTATION) { |
| 795 | unsigned int length; |
| 796 | count = dwarf_read_uleb128(p, &length); |
| 797 | p += count + length; |
| 798 | } |
| 799 | |
| 800 | /* Call frame instructions. */ |
| 801 | fde->instructions = p; |
Matt Fleming | 5480675 | 2009-08-20 19:42:34 +0100 | [diff] [blame^] | 802 | fde->end = end; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 803 | |
| 804 | /* Add to list. */ |
| 805 | spin_lock_irqsave(&dwarf_fde_lock, flags); |
| 806 | list_add_tail(&fde->link, &dwarf_fde_list); |
| 807 | spin_unlock_irqrestore(&dwarf_fde_lock, flags); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 812 | static void dwarf_unwinder_dump(struct task_struct *task, |
| 813 | struct pt_regs *regs, |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 814 | unsigned long *sp, |
Matt Fleming | b344e24a | 2009-08-16 21:54:48 +0100 | [diff] [blame] | 815 | const struct stacktrace_ops *ops, |
| 816 | void *data) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 817 | { |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 818 | struct dwarf_frame *frame, *_frame; |
| 819 | unsigned long return_addr; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 820 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 821 | _frame = NULL; |
| 822 | return_addr = 0; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 823 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 824 | while (1) { |
| 825 | frame = dwarf_unwind_stack(return_addr, _frame); |
| 826 | |
| 827 | if (_frame) { |
| 828 | dwarf_frame_free_regs(_frame); |
| 829 | mempool_free(_frame, dwarf_frame_pool); |
| 830 | } |
| 831 | |
| 832 | _frame = frame; |
| 833 | |
| 834 | if (!frame || !frame->return_addr) |
| 835 | break; |
| 836 | |
| 837 | return_addr = frame->return_addr; |
| 838 | ops->address(data, return_addr, 1); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
| 842 | static struct unwinder dwarf_unwinder = { |
| 843 | .name = "dwarf-unwinder", |
| 844 | .dump = dwarf_unwinder_dump, |
| 845 | .rating = 150, |
| 846 | }; |
| 847 | |
| 848 | static void dwarf_unwinder_cleanup(void) |
| 849 | { |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 850 | struct dwarf_cie *cie; |
| 851 | struct dwarf_fde *fde; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 852 | |
| 853 | /* |
| 854 | * Deallocate all the memory allocated for the DWARF unwinder. |
| 855 | * Traverse all the FDE/CIE lists and remove and free all the |
| 856 | * memory associated with those data structures. |
| 857 | */ |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 858 | list_for_each_entry(cie, &dwarf_cie_list, link) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 859 | kfree(cie); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 860 | |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 861 | list_for_each_entry(fde, &dwarf_fde_list, link) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 862 | kfree(fde); |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 863 | |
| 864 | kmem_cache_destroy(dwarf_reg_cachep); |
| 865 | kmem_cache_destroy(dwarf_frame_cachep); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | /** |
| 869 | * dwarf_unwinder_init - initialise the dwarf unwinder |
| 870 | * |
| 871 | * Build the data structures describing the .dwarf_frame section to |
| 872 | * make it easier to lookup CIE and FDE entries. Because the |
| 873 | * .eh_frame section is packed as tightly as possible it is not |
| 874 | * easy to lookup the FDE for a given PC, so we build a list of FDE |
| 875 | * and CIE entries that make it easier. |
| 876 | */ |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 877 | static int __init dwarf_unwinder_init(void) |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 878 | { |
| 879 | u32 entry_type; |
| 880 | void *p, *entry; |
| 881 | int count, err; |
| 882 | unsigned long len; |
| 883 | unsigned int c_entries, f_entries; |
| 884 | unsigned char *end; |
| 885 | INIT_LIST_HEAD(&dwarf_cie_list); |
| 886 | INIT_LIST_HEAD(&dwarf_fde_list); |
| 887 | |
| 888 | c_entries = 0; |
| 889 | f_entries = 0; |
| 890 | entry = &__start_eh_frame; |
| 891 | |
Matt Fleming | fb3f3e7 | 2009-08-16 15:44:08 +0100 | [diff] [blame] | 892 | dwarf_frame_cachep = kmem_cache_create("dwarf_frames", |
| 893 | sizeof(struct dwarf_frame), 0, SLAB_PANIC, NULL); |
| 894 | dwarf_reg_cachep = kmem_cache_create("dwarf_regs", |
| 895 | sizeof(struct dwarf_reg), 0, SLAB_PANIC, NULL); |
| 896 | |
| 897 | dwarf_frame_pool = mempool_create(DWARF_FRAME_MIN_REQ, |
| 898 | mempool_alloc_slab, |
| 899 | mempool_free_slab, |
| 900 | dwarf_frame_cachep); |
| 901 | |
| 902 | dwarf_reg_pool = mempool_create(DWARF_REG_MIN_REQ, |
| 903 | mempool_alloc_slab, |
| 904 | mempool_free_slab, |
| 905 | dwarf_reg_cachep); |
| 906 | |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 907 | while ((char *)entry < __stop_eh_frame) { |
| 908 | p = entry; |
| 909 | |
| 910 | count = dwarf_entry_len(p, &len); |
| 911 | if (count == 0) { |
| 912 | /* |
| 913 | * We read a bogus length field value. There is |
| 914 | * nothing we can do here apart from disabling |
| 915 | * the DWARF unwinder. We can't even skip this |
| 916 | * entry and move to the next one because 'len' |
| 917 | * tells us where our next entry is. |
| 918 | */ |
| 919 | goto out; |
| 920 | } else |
| 921 | p += count; |
| 922 | |
| 923 | /* initial length does not include itself */ |
| 924 | end = p + len; |
| 925 | |
Paul Mundt | 3497447 | 2009-08-14 02:10:59 +0900 | [diff] [blame] | 926 | entry_type = get_unaligned((u32 *)p); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 927 | p += 4; |
| 928 | |
| 929 | if (entry_type == DW_EH_FRAME_CIE) { |
| 930 | err = dwarf_parse_cie(entry, p, len, end); |
| 931 | if (err < 0) |
| 932 | goto out; |
| 933 | else |
| 934 | c_entries++; |
| 935 | } else { |
Matt Fleming | 5480675 | 2009-08-20 19:42:34 +0100 | [diff] [blame^] | 936 | err = dwarf_parse_fde(entry, entry_type, p, len, end); |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 937 | if (err < 0) |
| 938 | goto out; |
| 939 | else |
| 940 | f_entries++; |
| 941 | } |
| 942 | |
| 943 | entry = (char *)entry + len + 4; |
| 944 | } |
| 945 | |
| 946 | printk(KERN_INFO "DWARF unwinder initialised: read %u CIEs, %u FDEs\n", |
| 947 | c_entries, f_entries); |
| 948 | |
| 949 | err = unwinder_register(&dwarf_unwinder); |
| 950 | if (err) |
| 951 | goto out; |
| 952 | |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 953 | return 0; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 954 | |
| 955 | out: |
| 956 | printk(KERN_ERR "Failed to initialise DWARF unwinder: %d\n", err); |
| 957 | dwarf_unwinder_cleanup(); |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 958 | return -EINVAL; |
Matt Fleming | bd35386 | 2009-08-14 01:58:43 +0900 | [diff] [blame] | 959 | } |
Paul Mundt | 97f361e | 2009-08-17 05:07:38 +0900 | [diff] [blame] | 960 | early_initcall(dwarf_unwinder_init); |