unwind.c 11 KB

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