unwind.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. unsigned long flags;
  67. spin_lock_irqsave(&unwind_lock, flags);
  68. list_for_each_entry(table, &unwind_tables, list) {
  69. if (addr >= table->start &&
  70. addr <= table->end)
  71. e = find_unwind_entry_in_table(table, addr);
  72. if (e) {
  73. /* Move-to-front to exploit common traces */
  74. list_move(&table->list, &unwind_tables);
  75. break;
  76. }
  77. }
  78. spin_unlock_irqrestore(&unwind_lock, flags);
  79. }
  80. return e;
  81. }
  82. static void
  83. unwind_table_init(struct unwind_table *table, const char *name,
  84. unsigned long base_addr, unsigned long gp,
  85. void *table_start, void *table_end)
  86. {
  87. struct unwind_table_entry *start = table_start;
  88. struct unwind_table_entry *end =
  89. (struct unwind_table_entry *)table_end - 1;
  90. table->name = name;
  91. table->base_addr = base_addr;
  92. table->gp = gp;
  93. table->start = base_addr + start->region_start;
  94. table->end = base_addr + end->region_end;
  95. table->table = (struct unwind_table_entry *)table_start;
  96. table->length = end - start + 1;
  97. INIT_LIST_HEAD(&table->list);
  98. for (; start <= end; start++) {
  99. if (start < end &&
  100. start->region_end > (start+1)->region_start) {
  101. printk("WARNING: Out of order unwind entry! %p and %p\n", start, start+1);
  102. }
  103. start->region_start += base_addr;
  104. start->region_end += base_addr;
  105. }
  106. }
  107. static int cmp_unwind_table_entry(const void *a, const void *b)
  108. {
  109. return ((const struct unwind_table_entry *)a)->region_start
  110. - ((const struct unwind_table_entry *)b)->region_start;
  111. }
  112. static void
  113. unwind_table_sort(struct unwind_table_entry *start,
  114. struct unwind_table_entry *finish)
  115. {
  116. sort(start, finish - start, sizeof(struct unwind_table_entry),
  117. cmp_unwind_table_entry, NULL);
  118. }
  119. struct unwind_table *
  120. unwind_table_add(const char *name, unsigned long base_addr,
  121. unsigned long gp,
  122. void *start, void *end)
  123. {
  124. struct unwind_table *table;
  125. unsigned long flags;
  126. struct unwind_table_entry *s = (struct unwind_table_entry *)start;
  127. struct unwind_table_entry *e = (struct unwind_table_entry *)end;
  128. unwind_table_sort(s, e);
  129. table = kmalloc(sizeof(struct unwind_table), GFP_USER);
  130. if (table == NULL)
  131. return NULL;
  132. unwind_table_init(table, name, base_addr, gp, start, end);
  133. spin_lock_irqsave(&unwind_lock, flags);
  134. list_add_tail(&table->list, &unwind_tables);
  135. spin_unlock_irqrestore(&unwind_lock, flags);
  136. return table;
  137. }
  138. void unwind_table_remove(struct unwind_table *table)
  139. {
  140. unsigned long flags;
  141. spin_lock_irqsave(&unwind_lock, flags);
  142. list_del(&table->list);
  143. spin_unlock_irqrestore(&unwind_lock, flags);
  144. kfree(table);
  145. }
  146. /* Called from setup_arch to import the kernel unwind info */
  147. int __init unwind_init(void)
  148. {
  149. long start, stop;
  150. register unsigned long gp __asm__ ("r27");
  151. start = (long)&__start___unwind[0];
  152. stop = (long)&__stop___unwind[0];
  153. spin_lock_init(&unwind_lock);
  154. printk("unwind_init: start = 0x%lx, end = 0x%lx, entries = %lu\n",
  155. start, stop,
  156. (stop - start) / sizeof(struct unwind_table_entry));
  157. unwind_table_init(&kernel_unwind_table, "kernel", KERNEL_START,
  158. gp,
  159. &__start___unwind[0], &__stop___unwind[0]);
  160. #if 0
  161. {
  162. int i;
  163. for (i = 0; i < 10; i++)
  164. {
  165. printk("region 0x%x-0x%x\n",
  166. __start___unwind[i].region_start,
  167. __start___unwind[i].region_end);
  168. }
  169. }
  170. #endif
  171. return 0;
  172. }
  173. #ifdef CONFIG_64BIT
  174. #define get_func_addr(fptr) fptr[2]
  175. #else
  176. #define get_func_addr(fptr) fptr[0]
  177. #endif
  178. static int unwind_special(struct unwind_frame_info *info, unsigned long pc, int frame_size)
  179. {
  180. extern void handle_interruption(int, struct pt_regs *);
  181. static unsigned long *hi = (unsigned long *)&handle_interruption;
  182. if (pc == get_func_addr(hi)) {
  183. struct pt_regs *regs = (struct pt_regs *)(info->sp - frame_size - PT_SZ_ALGN);
  184. dbg("Unwinding through handle_interruption()\n");
  185. info->prev_sp = regs->gr[30];
  186. info->prev_ip = regs->iaoq[0];
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static void unwind_frame_regs(struct unwind_frame_info *info)
  192. {
  193. const struct unwind_table_entry *e;
  194. unsigned long npc;
  195. unsigned int insn;
  196. long frame_size = 0;
  197. int looking_for_rp, rpoffset = 0;
  198. e = find_unwind_entry(info->ip);
  199. if (e == NULL) {
  200. unsigned long sp;
  201. dbg("Cannot find unwind entry for 0x%lx; forced unwinding\n", info->ip);
  202. #ifdef CONFIG_KALLSYMS
  203. /* Handle some frequent special cases.... */
  204. {
  205. char symname[KSYM_NAME_LEN];
  206. char *modname;
  207. kallsyms_lookup(info->ip, NULL, NULL, &modname,
  208. symname);
  209. dbg("info->ip = 0x%lx, name = %s\n", info->ip, symname);
  210. if (strcmp(symname, "_switch_to_ret") == 0) {
  211. info->prev_sp = info->sp - CALLEE_SAVE_FRAME_SIZE;
  212. info->prev_ip = *(unsigned long *)(info->prev_sp - RP_OFFSET);
  213. dbg("_switch_to_ret @ %lx - setting "
  214. "prev_sp=%lx prev_ip=%lx\n",
  215. info->ip, info->prev_sp,
  216. info->prev_ip);
  217. return;
  218. } else if (strcmp(symname, "ret_from_kernel_thread") == 0 ||
  219. strcmp(symname, "syscall_exit") == 0) {
  220. info->prev_ip = info->prev_sp = 0;
  221. return;
  222. }
  223. }
  224. #endif
  225. /* Since we are doing the unwinding blind, we don't know if
  226. we are adjusting the stack correctly or extracting the rp
  227. correctly. The rp is checked to see if it belongs to the
  228. kernel text section, if not we assume we don't have a
  229. correct stack frame and we continue to unwind the stack.
  230. This is not quite correct, and will fail for loadable
  231. modules. */
  232. sp = info->sp & ~63;
  233. do {
  234. unsigned long tmp;
  235. info->prev_sp = sp - 64;
  236. info->prev_ip = 0;
  237. if (get_user(tmp, (unsigned long *)(info->prev_sp - RP_OFFSET)))
  238. break;
  239. info->prev_ip = tmp;
  240. sp = info->prev_sp;
  241. } while (!kernel_text_address(info->prev_ip));
  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 & 0xffffc001) == 0x37de0000 ||
  259. (insn & 0xffe00001) == 0x6fc00000) {
  260. /* ldo X(sp), sp, or stwm X,D(sp) */
  261. frame_size += (insn & 0x3fff) >> 1;
  262. dbg("analyzing func @ %lx, insn=%08x @ "
  263. "%lx, frame_size = %ld\n", info->ip,
  264. insn, npc, frame_size);
  265. } else if ((insn & 0xffe00009) == 0x73c00008) {
  266. /* std,ma X,D(sp) */
  267. frame_size += ((insn >> 4) & 0x3ff) << 3;
  268. dbg("analyzing func @ %lx, insn=%08x @ "
  269. "%lx, frame_size = %ld\n", info->ip,
  270. insn, npc, frame_size);
  271. } else if (insn == 0x6bc23fd9) {
  272. /* stw rp,-20(sp) */
  273. rpoffset = 20;
  274. looking_for_rp = 0;
  275. dbg("analyzing func @ %lx, insn=stw rp,"
  276. "-20(sp) @ %lx\n", info->ip, npc);
  277. } else if (insn == 0x0fc212c1) {
  278. /* std rp,-16(sr0,sp) */
  279. rpoffset = 16;
  280. looking_for_rp = 0;
  281. dbg("analyzing func @ %lx, insn=std rp,"
  282. "-16(sp) @ %lx\n", info->ip, npc);
  283. }
  284. }
  285. if (frame_size > e->Total_frame_size << 3)
  286. frame_size = e->Total_frame_size << 3;
  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. }
  353. unsigned long return_address(unsigned int level)
  354. {
  355. struct unwind_frame_info info;
  356. struct pt_regs r;
  357. unsigned long sp;
  358. /* initialize unwind info */
  359. asm volatile ("copy %%r30, %0" : "=r"(sp));
  360. memset(&r, 0, sizeof(struct pt_regs));
  361. r.iaoq[0] = (unsigned long) current_text_addr();
  362. r.gr[2] = (unsigned long) __builtin_return_address(0);
  363. r.gr[30] = sp;
  364. unwind_frame_init(&info, current, &r);
  365. /* unwind stack */
  366. ++level;
  367. do {
  368. if (unwind_once(&info) < 0 || info.ip == 0)
  369. return 0;
  370. if (!kernel_text_address(info.ip))
  371. return 0;
  372. } while (info.ip && level--);
  373. return info.ip;
  374. }