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