unwind.c 13 KB

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