unwind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * Kernel unwinding support
  3. *
  4. * (c) 2002-2004 Randolph Chung <tausq@debian.org>
  5. *
  6. * Derived partially from the IA64 implementation. The PA-RISC
  7. * Runtime Architecture Document is also a useful reference to
  8. * understand what is happening here
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/sort.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/assembly.h>
  18. #include <asm/asm-offsets.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/unwind.h>
  21. /* #define DEBUG 1 */
  22. #ifdef DEBUG
  23. #define dbg(x...) printk(x)
  24. #else
  25. #define dbg(x...)
  26. #endif
  27. #define KERNEL_START (KERNEL_BINARY_TEXT_START)
  28. extern struct unwind_table_entry __start___unwind[];
  29. extern struct unwind_table_entry __stop___unwind[];
  30. static spinlock_t unwind_lock;
  31. /*
  32. * the kernel unwind block is not dynamically allocated so that
  33. * we can call unwind_init as early in the bootup process as
  34. * possible (before the slab allocator is initialized)
  35. */
  36. static struct unwind_table kernel_unwind_table __read_mostly;
  37. static LIST_HEAD(unwind_tables);
  38. static inline const struct unwind_table_entry *
  39. find_unwind_entry_in_table(const struct unwind_table *table, unsigned long addr)
  40. {
  41. const struct unwind_table_entry *e = NULL;
  42. unsigned long lo, hi, mid;
  43. lo = 0;
  44. hi = table->length - 1;
  45. while (lo <= hi) {
  46. mid = (hi - lo) / 2 + lo;
  47. e = &table->table[mid];
  48. if (addr < e->region_start)
  49. hi = mid - 1;
  50. else if (addr > e->region_end)
  51. lo = mid + 1;
  52. else
  53. return e;
  54. }
  55. return NULL;
  56. }
  57. static const struct unwind_table_entry *
  58. find_unwind_entry(unsigned long addr)
  59. {
  60. struct unwind_table *table;
  61. const struct unwind_table_entry *e = NULL;
  62. if (addr >= kernel_unwind_table.start &&
  63. addr <= kernel_unwind_table.end)
  64. e = find_unwind_entry_in_table(&kernel_unwind_table, addr);
  65. else
  66. list_for_each_entry(table, &unwind_tables, list) {
  67. if (addr >= table->start &&
  68. addr <= table->end)
  69. e = find_unwind_entry_in_table(table, addr);
  70. if (e) {
  71. /* Move-to-front to exploit common traces */
  72. list_move(&table->list, &unwind_tables);
  73. break;
  74. }
  75. }
  76. return e;
  77. }
  78. static void
  79. unwind_table_init(struct unwind_table *table, const char *name,
  80. unsigned long base_addr, unsigned long gp,
  81. void *table_start, void *table_end)
  82. {
  83. struct unwind_table_entry *start = table_start;
  84. struct unwind_table_entry *end =
  85. (struct unwind_table_entry *)table_end - 1;
  86. table->name = name;
  87. table->base_addr = base_addr;
  88. table->gp = gp;
  89. table->start = base_addr + start->region_start;
  90. table->end = base_addr + end->region_end;
  91. table->table = (struct unwind_table_entry *)table_start;
  92. table->length = end - start + 1;
  93. INIT_LIST_HEAD(&table->list);
  94. for (; start <= end; start++) {
  95. if (start < end &&
  96. start->region_end > (start+1)->region_start) {
  97. printk("WARNING: Out of order unwind entry! %p and %p\n", start, start+1);
  98. }
  99. start->region_start += base_addr;
  100. start->region_end += base_addr;
  101. }
  102. }
  103. static int cmp_unwind_table_entry(const void *a, const void *b)
  104. {
  105. return ((const struct unwind_table_entry *)a)->region_start
  106. - ((const struct unwind_table_entry *)b)->region_start;
  107. }
  108. static void
  109. unwind_table_sort(struct unwind_table_entry *start,
  110. struct unwind_table_entry *finish)
  111. {
  112. sort(start, finish - start, sizeof(struct unwind_table_entry),
  113. cmp_unwind_table_entry, NULL);
  114. }
  115. struct unwind_table *
  116. unwind_table_add(const char *name, unsigned long base_addr,
  117. unsigned long gp,
  118. void *start, void *end)
  119. {
  120. struct unwind_table *table;
  121. unsigned long flags;
  122. struct unwind_table_entry *s = (struct unwind_table_entry *)start;
  123. struct unwind_table_entry *e = (struct unwind_table_entry *)end;
  124. unwind_table_sort(s, e);
  125. table = kmalloc(sizeof(struct unwind_table), GFP_USER);
  126. if (table == NULL)
  127. return NULL;
  128. unwind_table_init(table, name, base_addr, gp, start, end);
  129. spin_lock_irqsave(&unwind_lock, flags);
  130. list_add_tail(&table->list, &unwind_tables);
  131. spin_unlock_irqrestore(&unwind_lock, flags);
  132. return table;
  133. }
  134. void unwind_table_remove(struct unwind_table *table)
  135. {
  136. unsigned long flags;
  137. spin_lock_irqsave(&unwind_lock, flags);
  138. list_del(&table->list);
  139. spin_unlock_irqrestore(&unwind_lock, flags);
  140. kfree(table);
  141. }
  142. /* Called from setup_arch to import the kernel unwind info */
  143. int __init unwind_init(void)
  144. {
  145. long start, stop;
  146. register unsigned long gp __asm__ ("r27");
  147. start = (long)&__start___unwind[0];
  148. stop = (long)&__stop___unwind[0];
  149. spin_lock_init(&unwind_lock);
  150. printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
  151. start, stop,
  152. (stop - start) / sizeof(struct unwind_table_entry));
  153. unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
  154. gp,
  155. &__start___unwind[0], &__stop___unwind[0]);
  156. #if 0
  157. {
  158. int i;
  159. for (i = 0; i < 10; i++)
  160. {
  161. printk("region 0x%x-0x%x\n",
  162. __start___unwind[i].region_start,
  163. __start___unwind[i].region_end);
  164. }
  165. }
  166. #endif
  167. return 0;
  168. }
  169. #ifdef CONFIG_64BIT
  170. #define get_func_addr(fptr) fptr[2]
  171. #else
  172. #define get_func_addr(fptr) fptr[0]
  173. #endif
  174. static int unwind_special(struct unwind_frame_info *info, unsigned long pc, int frame_size)
  175. {
  176. extern void handle_interruption(int, struct pt_regs *);
  177. static unsigned long *hi = (unsigned long *)&handle_interruption;
  178. if (pc == get_func_addr(hi)) {
  179. struct pt_regs *regs = (struct pt_regs *)(info->sp - frame_size - PT_SZ_ALGN);
  180. dbg("Unwinding through handle_interruption()\n");
  181. info->prev_sp = regs->gr[30];
  182. info->prev_ip = regs->iaoq[0];
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. static void unwind_frame_regs(struct unwind_frame_info *info)
  188. {
  189. const struct unwind_table_entry *e;
  190. unsigned long npc;
  191. unsigned int insn;
  192. long frame_size = 0;
  193. int looking_for_rp, rpoffset = 0;
  194. e = find_unwind_entry(info->ip);
  195. if (e == NULL) {
  196. unsigned long sp;
  197. dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info->ip);
  198. #ifdef CONFIG_KALLSYMS
  199. /* Handle some frequent special cases.... */
  200. {
  201. char symname[KSYM_NAME_LEN];
  202. char *modname;
  203. kallsyms_lookup(info->ip, NULL, NULL, &modname,
  204. symname);
  205. dbg("info->ip = 0x%lx, name = %s\n", info->ip, symname);
  206. if (strcmp(symname, "_switch_to_ret") == 0) {
  207. info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
  208. info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
  209. dbg("_switch_to_ret @ %lx - setting "
  210. "prev_sp=%lx prev_ip=%lx\n",
  211. info->ip, info->prev_sp,
  212. info->prev_ip);
  213. return;
  214. } else if (strcmp(symname, "ret_from_kernel_thread") == 0 ||
  215. strcmp(symname, "syscall_exit") == 0) {
  216. info->prev_ip = info->prev_sp = 0;
  217. return;
  218. }
  219. }
  220. #endif
  221. /* Since we are doing the unwinding blind, we don't know if
  222. we are adjusting the stack correctly or extracting the rp
  223. correctly. The rp is checked to see if it belongs to the
  224. kernel text section, if not we assume we don't have a
  225. correct stack frame and we continue to unwind the stack.
  226. This is not quite correct, and will fail for loadable
  227. modules. */
  228. sp = info->sp & ~63;
  229. do {
  230. unsigned long tmp;
  231. info->prev_sp = sp - 64;
  232. info->prev_ip = 0;
  233. if (get_user(tmp, (unsigned long *)(info->prev_sp - RP_OFFSET)))
  234. break;
  235. info->prev_ip = tmp;
  236. sp = info->prev_sp;
  237. } while (!kernel_text_address(info->prev_ip));
  238. info->rp = 0;
  239. dbg("analyzing func @ %lx with no unwind info, setting "
  240. "prev_sp=%lx prev_ip=%lx\n", info->ip,
  241. info->prev_sp, info->prev_ip);
  242. } else {
  243. dbg("e->start = 0x%x, e->end = 0x%x, Save_SP = %d, "
  244. "Save_RP = %d, Millicode = %d size = %u\n",
  245. e->region_start, e->region_end, e->Save_SP, e->Save_RP,
  246. e->Millicode, e->Total_frame_size);
  247. looking_for_rp = e->Save_RP;
  248. for (npc = e->region_start;
  249. (frame_size < (e->Total_frame_size << 3) ||
  250. looking_for_rp) &&
  251. npc < info->ip;
  252. npc += 4) {
  253. insn = *(unsigned int *)npc;
  254. if ((insn & 0xffffc000) == 0x37de0000 ||
  255. (insn & 0xffe00000) == 0x6fc00000) {
  256. /* ldo X(sp), sp, or stwm X,D(sp) */
  257. frame_size += (insn & 0x1 ? -1 << 13 : 0) |
  258. ((insn & 0x3fff) >> 1);
  259. dbg("analyzing func @ %lx, insn=%08x @ "
  260. "%lx, frame_size = %ld\n", info->ip,
  261. insn, npc, frame_size);
  262. } else if ((insn & 0xffe00008) == 0x73c00008) {
  263. /* std,ma X,D(sp) */
  264. frame_size += (insn & 0x1 ? -1 << 13 : 0) |
  265. (((insn >> 4) & 0x3ff) << 3);
  266. dbg("analyzing func @ %lx, insn=%08x @ "
  267. "%lx, frame_size = %ld\n", info->ip,
  268. insn, npc, frame_size);
  269. } else if (insn == 0x6bc23fd9) {
  270. /* stw rp,-20(sp) */
  271. rpoffset = 20;
  272. looking_for_rp = 0;
  273. dbg("analyzing func @ %lx, insn=stw rp,"
  274. "-20(sp) @ %lx\n", info->ip, npc);
  275. } else if (insn == 0x0fc212c1) {
  276. /* std rp,-16(sr0,sp) */
  277. rpoffset = 16;
  278. looking_for_rp = 0;
  279. dbg("analyzing func @ %lx, insn=std rp,"
  280. "-16(sp) @ %lx\n", info->ip, npc);
  281. }
  282. }
  283. if (!unwind_special(info, e->region_start, frame_size)) {
  284. info->prev_sp = info->sp - frame_size;
  285. if (e->Millicode)
  286. info->rp = info->r31;
  287. else if (rpoffset)
  288. info->rp = *(unsigned long *)(info->prev_sp - rpoffset);
  289. info->prev_ip = info->rp;
  290. info->rp = 0;
  291. }
  292. dbg("analyzing func @ %lx, setting prev_sp=%lx "
  293. "prev_ip=%lx npc=%lx\n", info->ip, info->prev_sp,
  294. info->prev_ip, npc);
  295. }
  296. }
  297. void unwind_frame_init(struct unwind_frame_info *info, struct task_struct *t,
  298. struct pt_regs *regs)
  299. {
  300. memset(info, 0, sizeof(struct unwind_frame_info));
  301. info->t = t;
  302. info->sp = regs->gr[30];
  303. info->ip = regs->iaoq[0];
  304. info->rp = regs->gr[2];
  305. info->r31 = regs->gr[31];
  306. dbg("(%d) Start unwind from sp=%08lx ip=%08lx\n",
  307. t ? (int)t->pid : -1, info->sp, info->ip);
  308. }
  309. void unwind_frame_init_from_blocked_task(struct unwind_frame_info *info, struct task_struct *t)
  310. {
  311. struct pt_regs *r = &t->thread.regs;
  312. struct pt_regs *r2;
  313. r2 = kmalloc(sizeof(struct pt_regs), GFP_ATOMIC);
  314. if (!r2)
  315. return;
  316. *r2 = *r;
  317. r2->gr[30] = r->ksp;
  318. r2->iaoq[0] = r->kpc;
  319. unwind_frame_init(info, t, r2);
  320. kfree(r2);
  321. }
  322. void unwind_frame_init_running(struct unwind_frame_info *info, struct pt_regs *regs)
  323. {
  324. unwind_frame_init(info, current, regs);
  325. }
  326. int unwind_once(struct unwind_frame_info *next_frame)
  327. {
  328. unwind_frame_regs(next_frame);
  329. if (next_frame->prev_sp == 0 ||
  330. next_frame->prev_ip == 0)
  331. return -1;
  332. next_frame->sp = next_frame->prev_sp;
  333. next_frame->ip = next_frame->prev_ip;
  334. next_frame->prev_sp = 0;
  335. next_frame->prev_ip = 0;
  336. dbg("(%d) Continue unwind to sp=%08lx ip=%08lx\n",
  337. next_frame->t ? (int)next_frame->t->pid : -1,
  338. next_frame->sp, next_frame->ip);
  339. return 0;
  340. }
  341. int unwind_to_user(struct unwind_frame_info *info)
  342. {
  343. int ret;
  344. do {
  345. ret = unwind_once(info);
  346. } while (!ret && !(info->ip & 3));
  347. return ret;
  348. }
  349. unsigned long return_address(unsigned int level)
  350. {
  351. struct unwind_frame_info info;
  352. struct pt_regs r;
  353. unsigned long sp;
  354. /* initialize unwind info */
  355. asm volatile ("copy %%r30, %0" : "=r"(sp));
  356. memset(&r, 0, sizeof(struct pt_regs));
  357. r.iaoq[0] = (unsigned long) current_text_addr();
  358. r.gr[2] = (unsigned long) __builtin_return_address(0);
  359. r.gr[30] = sp;
  360. unwind_frame_init(&info, current, &r);
  361. /* unwind stack */
  362. ++level;
  363. do {
  364. if (unwind_once(&info) < 0 || info.ip == 0)
  365. return 0;
  366. if (!kernel_text_address(info.ip))
  367. return 0;
  368. } while (info.ip && level--);
  369. return info.ip;
  370. }