1 /* 2 * arch/arm/kernel/unwind.c 3 * 4 * Copyright (C) 2008 ARM Limited 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 * 19 * 20 * Stack unwinding support for ARM 21 * 22 * An ARM EABI version of gcc is required to generate the unwind 23 * tables. For information about the structure of the unwind tables, 24 * see "Exception Handling ABI for the ARM Architecture" at: 25 * 26 * http://infocenter.arm.com/help/topic/com.arm.doc.subset.swdev.abi/index.html 27 */ 28 29 #include <linux/kernel.h> 30 #include <linux/init.h> 31 #include <linux/module.h> 32 #include <linux/sched.h> 33 #include <linux/slab.h> 34 #include <linux/spinlock.h> 35 #include <linux/list.h> 36 37 #include <asm/stacktrace.h> 38 #include <asm/traps.h> 39 #include <asm/unwind.h> 40 41 /* Dummy functions to avoid linker complaints */ 42 void __aeabi_unwind_cpp_pr0(void) 43 { 44 }; 45 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr0); 46 47 void __aeabi_unwind_cpp_pr1(void) 48 { 49 }; 50 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr1); 51 52 void __aeabi_unwind_cpp_pr2(void) 53 { 54 }; 55 EXPORT_SYMBOL(__aeabi_unwind_cpp_pr2); 56 57 struct unwind_ctrl_block { 58 unsigned long vrs[16]; /* virtual register set */ 59 unsigned long *insn; /* pointer to the current instructions word */ 60 int entries; /* number of entries left to interpret */ 61 int byte; /* current byte number in the instructions word */ 62 }; 63 64 enum regs { 65 FP = 11, 66 SP = 13, 67 LR = 14, 68 PC = 15 69 }; 70 71 extern struct unwind_idx __start_unwind_idx[]; 72 extern struct unwind_idx __stop_unwind_idx[]; 73 74 static DEFINE_SPINLOCK(unwind_lock); 75 static LIST_HEAD(unwind_tables); 76 77 /* Convert a prel31 symbol to an absolute address */ 78 #define prel31_to_addr(ptr) \ 79 ({ \ 80 /* sign-extend to 32 bits */ \ 81 long offset = (((long)*(ptr)) << 1) >> 1; \ 82 (unsigned long)(ptr) + offset; \ 83 }) 84 85 /* 86 * Binary search in the unwind index. The entries entries are 87 * guaranteed to be sorted in ascending order by the linker. 88 */ 89 static struct unwind_idx *search_index(unsigned long addr, 90 struct unwind_idx *first, 91 struct unwind_idx *last) 92 { 93 pr_debug("%s(%08lx, %p, %p)\n", __func__, addr, first, last); 94 95 if (addr < first->addr) { 96 pr_warning("unwind: Unknown symbol address %08lx\n", addr); 97 return NULL; 98 } else if (addr >= last->addr) 99 return last; 100 101 while (first < last - 1) { 102 struct unwind_idx *mid = first + ((last - first + 1) >> 1); 103 104 if (addr < mid->addr) 105 last = mid; 106 else 107 first = mid; 108 } 109 110 return first; 111 } 112 113 static struct unwind_idx *unwind_find_idx(unsigned long addr) 114 { 115 struct unwind_idx *idx = NULL; 116 unsigned long flags; 117 118 pr_debug("%s(%08lx)\n", __func__, addr); 119 120 if (core_kernel_text(addr)) 121 /* main unwind table */ 122 idx = search_index(addr, __start_unwind_idx, 123 __stop_unwind_idx - 1); 124 else { 125 /* module unwind tables */ 126 struct unwind_table *table; 127 128 spin_lock_irqsave(&unwind_lock, flags); 129 list_for_each_entry(table, &unwind_tables, list) { 130 if (addr >= table->begin_addr && 131 addr < table->end_addr) { 132 idx = search_index(addr, table->start, 133 table->stop - 1); 134 break; 135 } 136 } 137 spin_unlock_irqrestore(&unwind_lock, flags); 138 } 139 140 pr_debug("%s: idx = %p\n", __func__, idx); 141 return idx; 142 } 143 144 static unsigned long unwind_get_byte(struct unwind_ctrl_block *ctrl) 145 { 146 unsigned long ret; 147 148 if (ctrl->entries <= 0) { 149 pr_warning("unwind: Corrupt unwind table\n"); 150 return 0; 151 } 152 153 ret = (*ctrl->insn >> (ctrl->byte * 8)) & 0xff; 154 155 if (ctrl->byte == 0) { 156 ctrl->insn++; 157 ctrl->entries--; 158 ctrl->byte = 3; 159 } else 160 ctrl->byte--; 161 162 return ret; 163 } 164 165 /* 166 * Execute the current unwind instruction. 167 */ 168 static int unwind_exec_insn(struct unwind_ctrl_block *ctrl) 169 { 170 unsigned long insn = unwind_get_byte(ctrl); 171 172 pr_debug("%s: insn = %08lx\n", __func__, insn); 173 174 if ((insn & 0xc0) == 0x00) 175 ctrl->vrs[SP] += ((insn & 0x3f) << 2) + 4; 176 else if ((insn & 0xc0) == 0x40) 177 ctrl->vrs[SP] -= ((insn & 0x3f) << 2) + 4; 178 else if ((insn & 0xf0) == 0x80) { 179 unsigned long mask; 180 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; 181 int load_sp, reg = 4; 182 183 insn = (insn << 8) | unwind_get_byte(ctrl); 184 mask = insn & 0x0fff; 185 if (mask == 0) { 186 pr_warning("unwind: 'Refuse to unwind' instruction %04lx\n", 187 insn); 188 return -URC_FAILURE; 189 } 190 191 /* pop R4-R15 according to mask */ 192 load_sp = mask & (1 << (13 - 4)); 193 while (mask) { 194 if (mask & 1) 195 ctrl->vrs[reg] = *vsp++; 196 mask >>= 1; 197 reg++; 198 } 199 if (!load_sp) 200 ctrl->vrs[SP] = (unsigned long)vsp; 201 } else if ((insn & 0xf0) == 0x90 && 202 (insn & 0x0d) != 0x0d) 203 ctrl->vrs[SP] = ctrl->vrs[insn & 0x0f]; 204 else if ((insn & 0xf0) == 0xa0) { 205 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; 206 int reg; 207 208 /* pop R4-R[4+bbb] */ 209 for (reg = 4; reg <= 4 + (insn & 7); reg++) 210 ctrl->vrs[reg] = *vsp++; 211 if (insn & 0x80) 212 ctrl->vrs[14] = *vsp++; 213 ctrl->vrs[SP] = (unsigned long)vsp; 214 } else if (insn == 0xb0) { 215 if (ctrl->vrs[PC] == 0) 216 ctrl->vrs[PC] = ctrl->vrs[LR]; 217 /* no further processing */ 218 ctrl->entries = 0; 219 } else if (insn == 0xb1) { 220 unsigned long mask = unwind_get_byte(ctrl); 221 unsigned long *vsp = (unsigned long *)ctrl->vrs[SP]; 222 int reg = 0; 223 224 if (mask == 0 || mask & 0xf0) { 225 pr_warning("unwind: Spare encoding %04lx\n", 226 (insn << 8) | mask); 227 return -URC_FAILURE; 228 } 229 230 /* pop R0-R3 according to mask */ 231 while (mask) { 232 if (mask & 1) 233 ctrl->vrs[reg] = *vsp++; 234 mask >>= 1; 235 reg++; 236 } 237 ctrl->vrs[SP] = (unsigned long)vsp; 238 } else if (insn == 0xb2) { 239 unsigned long uleb128 = unwind_get_byte(ctrl); 240 241 ctrl->vrs[SP] += 0x204 + (uleb128 << 2); 242 } else { 243 pr_warning("unwind: Unhandled instruction %02lx\n", insn); 244 return -URC_FAILURE; 245 } 246 247 pr_debug("%s: fp = %08lx sp = %08lx lr = %08lx pc = %08lx\n", __func__, 248 ctrl->vrs[FP], ctrl->vrs[SP], ctrl->vrs[LR], ctrl->vrs[PC]); 249 250 return URC_OK; 251 } 252 253 /* 254 * Unwind a single frame starting with *sp for the symbol at *pc. It 255 * updates the *pc and *sp with the new values. 256 */ 257 int unwind_frame(struct stackframe *frame) 258 { 259 unsigned long high, low; 260 struct unwind_idx *idx; 261 struct unwind_ctrl_block ctrl; 262 263 /* only go to a higher address on the stack */ 264 low = frame->sp; 265 high = ALIGN(low, THREAD_SIZE) + THREAD_SIZE; 266 267 pr_debug("%s(pc = %08lx lr = %08lx sp = %08lx)\n", __func__, 268 frame->pc, frame->lr, frame->sp); 269 270 if (!kernel_text_address(frame->pc)) 271 return -URC_FAILURE; 272 273 idx = unwind_find_idx(frame->pc); 274 if (!idx) { 275 pr_warning("unwind: Index not found %08lx\n", frame->pc); 276 return -URC_FAILURE; 277 } 278 279 ctrl.vrs[FP] = frame->fp; 280 ctrl.vrs[SP] = frame->sp; 281 ctrl.vrs[LR] = frame->lr; 282 ctrl.vrs[PC] = 0; 283 284 if (idx->insn == 1) 285 /* can't unwind */ 286 return -URC_FAILURE; 287 else if ((idx->insn & 0x80000000) == 0) 288 /* prel31 to the unwind table */ 289 ctrl.insn = (unsigned long *)prel31_to_addr(&idx->insn); 290 else if ((idx->insn & 0xff000000) == 0x80000000) 291 /* only personality routine 0 supported in the index */ 292 ctrl.insn = &idx->insn; 293 else { 294 pr_warning("unwind: Unsupported personality routine %08lx in the index at %p\n", 295 idx->insn, idx); 296 return -URC_FAILURE; 297 } 298 299 /* check the personality routine */ 300 if ((*ctrl.insn & 0xff000000) == 0x80000000) { 301 ctrl.byte = 2; 302 ctrl.entries = 1; 303 } else if ((*ctrl.insn & 0xff000000) == 0x81000000) { 304 ctrl.byte = 1; 305 ctrl.entries = 1 + ((*ctrl.insn & 0x00ff0000) >> 16); 306 } else { 307 pr_warning("unwind: Unsupported personality routine %08lx at %p\n", 308 *ctrl.insn, ctrl.insn); 309 return -URC_FAILURE; 310 } 311 312 while (ctrl.entries > 0) { 313 int urc = unwind_exec_insn(&ctrl); 314 if (urc < 0) 315 return urc; 316 if (ctrl.vrs[SP] < low || ctrl.vrs[SP] >= high) 317 return -URC_FAILURE; 318 } 319 320 if (ctrl.vrs[PC] == 0) 321 ctrl.vrs[PC] = ctrl.vrs[LR]; 322 323 /* check for infinite loop */ 324 if (frame->pc == ctrl.vrs[PC]) 325 return -URC_FAILURE; 326 327 frame->fp = ctrl.vrs[FP]; 328 frame->sp = ctrl.vrs[SP]; 329 frame->lr = ctrl.vrs[LR]; 330 frame->pc = ctrl.vrs[PC]; 331 332 return URC_OK; 333 } 334 335 void unwind_backtrace(struct pt_regs *regs, struct task_struct *tsk) 336 { 337 struct stackframe frame; 338 register unsigned long current_sp asm ("sp"); 339 340 pr_debug("%s(regs = %p tsk = %p)\n", __func__, regs, tsk); 341 342 if (!tsk) 343 tsk = current; 344 345 if (regs) { 346 frame.fp = regs->ARM_fp; 347 frame.sp = regs->ARM_sp; 348 frame.lr = regs->ARM_lr; 349 frame.pc = regs->ARM_pc; 350 } else if (tsk == current) { 351 frame.fp = (unsigned long)__builtin_frame_address(0); 352 frame.sp = current_sp; 353 frame.lr = (unsigned long)__builtin_return_address(0); 354 frame.pc = (unsigned long)unwind_backtrace; 355 } else { 356 /* task blocked in __switch_to */ 357 frame.fp = thread_saved_fp(tsk); 358 frame.sp = thread_saved_sp(tsk); 359 /* 360 * The function calling __switch_to cannot be a leaf function 361 * so LR is recovered from the stack. 362 */ 363 frame.lr = 0; 364 frame.pc = thread_saved_pc(tsk); 365 } 366 367 while (1) { 368 int urc; 369 unsigned long where = frame.pc; 370 371 urc = unwind_frame(&frame); 372 if (urc < 0) 373 break; 374 dump_backtrace_entry(where, frame.pc, frame.sp - 4); 375 } 376 } 377 378 struct unwind_table *unwind_table_add(unsigned long start, unsigned long size, 379 unsigned long text_addr, 380 unsigned long text_size) 381 { 382 unsigned long flags; 383 struct unwind_idx *idx; 384 struct unwind_table *tab = kmalloc(sizeof(*tab), GFP_KERNEL); 385 386 pr_debug("%s(%08lx, %08lx, %08lx, %08lx)\n", __func__, start, size, 387 text_addr, text_size); 388 389 if (!tab) 390 return tab; 391 392 tab->start = (struct unwind_idx *)start; 393 tab->stop = (struct unwind_idx *)(start + size); 394 tab->begin_addr = text_addr; 395 tab->end_addr = text_addr + text_size; 396 397 /* Convert the symbol addresses to absolute values */ 398 for (idx = tab->start; idx < tab->stop; idx++) 399 idx->addr = prel31_to_addr(&idx->addr); 400 401 spin_lock_irqsave(&unwind_lock, flags); 402 list_add_tail(&tab->list, &unwind_tables); 403 spin_unlock_irqrestore(&unwind_lock, flags); 404 405 return tab; 406 } 407 408 void unwind_table_del(struct unwind_table *tab) 409 { 410 unsigned long flags; 411 412 if (!tab) 413 return; 414 415 spin_lock_irqsave(&unwind_lock, flags); 416 list_del(&tab->list); 417 spin_unlock_irqrestore(&unwind_lock, flags); 418 419 kfree(tab); 420 } 421 422 int __init unwind_init(void) 423 { 424 struct unwind_idx *idx; 425 426 /* Convert the symbol addresses to absolute values */ 427 for (idx = __start_unwind_idx; idx < __stop_unwind_idx; idx++) 428 idx->addr = prel31_to_addr(&idx->addr); 429 430 pr_debug("unwind: ARM stack unwinding initialised\n"); 431 432 return 0; 433 } 434