unwind_orc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/objtool.h>
  3. #include <linux/module.h>
  4. #include <linux/sort.h>
  5. #include <asm/exception.h>
  6. #include <asm/orc_header.h>
  7. #include <asm/orc_lookup.h>
  8. #include <asm/orc_types.h>
  9. #include <asm/ptrace.h>
  10. #include <asm/setup.h>
  11. #include <asm/stacktrace.h>
  12. #include <asm/tlb.h>
  13. #include <asm/unwind.h>
  14. ORC_HEADER;
  15. #define orc_warn(fmt, ...) \
  16. printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
  17. extern int __start_orc_unwind_ip[];
  18. extern int __stop_orc_unwind_ip[];
  19. extern struct orc_entry __start_orc_unwind[];
  20. extern struct orc_entry __stop_orc_unwind[];
  21. static bool orc_init __ro_after_init;
  22. static unsigned int lookup_num_blocks __ro_after_init;
  23. /* Fake frame pointer entry -- used as a fallback for generated code */
  24. static struct orc_entry orc_fp_entry = {
  25. .sp_reg = ORC_REG_FP,
  26. .sp_offset = 16,
  27. .fp_reg = ORC_REG_PREV_SP,
  28. .fp_offset = -16,
  29. .ra_reg = ORC_REG_PREV_SP,
  30. .ra_offset = -8,
  31. .type = ORC_TYPE_CALL
  32. };
  33. /*
  34. * If we crash with IP==0, the last successfully executed instruction
  35. * was probably an indirect function call with a NULL function pointer,
  36. * and we don't have unwind information for NULL.
  37. * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
  38. * pointer into its parent and then continue normally from there.
  39. */
  40. static struct orc_entry orc_null_entry = {
  41. .sp_reg = ORC_REG_SP,
  42. .sp_offset = sizeof(long),
  43. .fp_reg = ORC_REG_UNDEFINED,
  44. .type = ORC_TYPE_CALL
  45. };
  46. static inline unsigned long orc_ip(const int *ip)
  47. {
  48. return (unsigned long)ip + *ip;
  49. }
  50. static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
  51. unsigned int num_entries, unsigned long ip)
  52. {
  53. int *first = ip_table;
  54. int *mid = first, *found = first;
  55. int *last = ip_table + num_entries - 1;
  56. if (!num_entries)
  57. return NULL;
  58. /*
  59. * Do a binary range search to find the rightmost duplicate of a given
  60. * starting address. Some entries are section terminators which are
  61. * "weak" entries for ensuring there are no gaps. They should be
  62. * ignored when they conflict with a real entry.
  63. */
  64. while (first <= last) {
  65. mid = first + ((last - first) / 2);
  66. if (orc_ip(mid) <= ip) {
  67. found = mid;
  68. first = mid + 1;
  69. } else
  70. last = mid - 1;
  71. }
  72. return u_table + (found - ip_table);
  73. }
  74. #ifdef CONFIG_MODULES
  75. static struct orc_entry *orc_module_find(unsigned long ip)
  76. {
  77. struct module *mod;
  78. mod = __module_address(ip);
  79. if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
  80. return NULL;
  81. return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind, mod->arch.num_orcs, ip);
  82. }
  83. #else
  84. static struct orc_entry *orc_module_find(unsigned long ip)
  85. {
  86. return NULL;
  87. }
  88. #endif
  89. #ifdef CONFIG_DYNAMIC_FTRACE
  90. static struct orc_entry *orc_find(unsigned long ip);
  91. /*
  92. * Ftrace dynamic trampolines do not have orc entries of their own.
  93. * But they are copies of the ftrace entries that are static and
  94. * defined in ftrace_*.S, which do have orc entries.
  95. *
  96. * If the unwinder comes across a ftrace trampoline, then find the
  97. * ftrace function that was used to create it, and use that ftrace
  98. * function's orc entry, as the placement of the return code in
  99. * the stack will be identical.
  100. */
  101. static struct orc_entry *orc_ftrace_find(unsigned long ip)
  102. {
  103. struct ftrace_ops *ops;
  104. unsigned long tramp_addr, offset;
  105. ops = ftrace_ops_trampoline(ip);
  106. if (!ops)
  107. return NULL;
  108. /* Set tramp_addr to the start of the code copied by the trampoline */
  109. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  110. tramp_addr = (unsigned long)ftrace_regs_caller;
  111. else
  112. tramp_addr = (unsigned long)ftrace_caller;
  113. /* Now place tramp_addr to the location within the trampoline ip is at */
  114. offset = ip - ops->trampoline;
  115. tramp_addr += offset;
  116. /* Prevent unlikely recursion */
  117. if (ip == tramp_addr)
  118. return NULL;
  119. return orc_find(tramp_addr);
  120. }
  121. #else
  122. static struct orc_entry *orc_ftrace_find(unsigned long ip)
  123. {
  124. return NULL;
  125. }
  126. #endif
  127. static struct orc_entry *orc_find(unsigned long ip)
  128. {
  129. static struct orc_entry *orc;
  130. if (ip == 0)
  131. return &orc_null_entry;
  132. /* For non-init vmlinux addresses, use the fast lookup table: */
  133. if (ip >= LOOKUP_START_IP && ip < LOOKUP_STOP_IP) {
  134. unsigned int idx, start, stop;
  135. idx = (ip - LOOKUP_START_IP) / LOOKUP_BLOCK_SIZE;
  136. if (unlikely((idx >= lookup_num_blocks-1))) {
  137. orc_warn("WARNING: bad lookup idx: idx=%u num=%u ip=%pB\n",
  138. idx, lookup_num_blocks, (void *)ip);
  139. return NULL;
  140. }
  141. start = orc_lookup[idx];
  142. stop = orc_lookup[idx + 1] + 1;
  143. if (unlikely((__start_orc_unwind + start >= __stop_orc_unwind) ||
  144. (__start_orc_unwind + stop > __stop_orc_unwind))) {
  145. orc_warn("WARNING: bad lookup value: idx=%u num=%u start=%u stop=%u ip=%pB\n",
  146. idx, lookup_num_blocks, start, stop, (void *)ip);
  147. return NULL;
  148. }
  149. return __orc_find(__start_orc_unwind_ip + start,
  150. __start_orc_unwind + start, stop - start, ip);
  151. }
  152. /* vmlinux .init slow lookup: */
  153. if (is_kernel_inittext(ip))
  154. return __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
  155. __stop_orc_unwind_ip - __start_orc_unwind_ip, ip);
  156. /* Module lookup: */
  157. orc = orc_module_find(ip);
  158. if (orc)
  159. return orc;
  160. return orc_ftrace_find(ip);
  161. }
  162. #ifdef CONFIG_MODULES
  163. static DEFINE_MUTEX(sort_mutex);
  164. static int *cur_orc_ip_table = __start_orc_unwind_ip;
  165. static struct orc_entry *cur_orc_table = __start_orc_unwind;
  166. static void orc_sort_swap(void *_a, void *_b, int size)
  167. {
  168. int delta = _b - _a;
  169. int *a = _a, *b = _b, tmp;
  170. struct orc_entry *orc_a, *orc_b;
  171. /* Swap the .orc_unwind_ip entries: */
  172. tmp = *a;
  173. *a = *b + delta;
  174. *b = tmp - delta;
  175. /* Swap the corresponding .orc_unwind entries: */
  176. orc_a = cur_orc_table + (a - cur_orc_ip_table);
  177. orc_b = cur_orc_table + (b - cur_orc_ip_table);
  178. swap(*orc_a, *orc_b);
  179. }
  180. static int orc_sort_cmp(const void *_a, const void *_b)
  181. {
  182. const int *a = _a, *b = _b;
  183. unsigned long a_val = orc_ip(a);
  184. unsigned long b_val = orc_ip(b);
  185. struct orc_entry *orc_a;
  186. if (a_val > b_val)
  187. return 1;
  188. if (a_val < b_val)
  189. return -1;
  190. /*
  191. * The "weak" section terminator entries need to always be first
  192. * to ensure the lookup code skips them in favor of real entries.
  193. * These terminator entries exist to handle any gaps created by
  194. * whitelisted .o files which didn't get objtool generation.
  195. */
  196. orc_a = cur_orc_table + (a - cur_orc_ip_table);
  197. return orc_a->type == ORC_TYPE_UNDEFINED ? -1 : 1;
  198. }
  199. void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
  200. void *_orc, size_t orc_size)
  201. {
  202. int *orc_ip = _orc_ip;
  203. struct orc_entry *orc = _orc;
  204. unsigned int num_entries = orc_ip_size / sizeof(int);
  205. WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
  206. orc_size % sizeof(*orc) != 0 ||
  207. num_entries != orc_size / sizeof(*orc));
  208. /*
  209. * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
  210. * associate an .orc_unwind_ip table entry with its corresponding
  211. * .orc_unwind entry so they can both be swapped.
  212. */
  213. mutex_lock(&sort_mutex);
  214. cur_orc_ip_table = orc_ip;
  215. cur_orc_table = orc;
  216. sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
  217. mutex_unlock(&sort_mutex);
  218. mod->arch.orc_unwind_ip = orc_ip;
  219. mod->arch.orc_unwind = orc;
  220. mod->arch.num_orcs = num_entries;
  221. }
  222. #endif
  223. void __init unwind_init(void)
  224. {
  225. int i;
  226. size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
  227. size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
  228. size_t num_entries = orc_ip_size / sizeof(int);
  229. struct orc_entry *orc;
  230. if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
  231. orc_size % sizeof(struct orc_entry) != 0 ||
  232. num_entries != orc_size / sizeof(struct orc_entry)) {
  233. orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
  234. return;
  235. }
  236. /*
  237. * Note, the orc_unwind and orc_unwind_ip tables were already
  238. * sorted at build time via the 'sorttable' tool.
  239. * It's ready for binary search straight away, no need to sort it.
  240. */
  241. /* Initialize the fast lookup table: */
  242. lookup_num_blocks = orc_lookup_end - orc_lookup;
  243. for (i = 0; i < lookup_num_blocks-1; i++) {
  244. orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
  245. num_entries, LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
  246. if (!orc) {
  247. orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
  248. return;
  249. }
  250. orc_lookup[i] = orc - __start_orc_unwind;
  251. }
  252. /* Initialize the ending block: */
  253. orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries, LOOKUP_STOP_IP);
  254. if (!orc) {
  255. orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
  256. return;
  257. }
  258. orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
  259. orc_init = true;
  260. }
  261. static inline bool on_stack(struct stack_info *info, unsigned long addr, size_t len)
  262. {
  263. unsigned long begin = info->begin;
  264. unsigned long end = info->end;
  265. return (info->type != STACK_TYPE_UNKNOWN &&
  266. addr >= begin && addr < end && addr + len > begin && addr + len <= end);
  267. }
  268. static bool stack_access_ok(struct unwind_state *state, unsigned long addr, size_t len)
  269. {
  270. struct stack_info *info = &state->stack_info;
  271. if (on_stack(info, addr, len))
  272. return true;
  273. return !get_stack_info(addr, state->task, info) && on_stack(info, addr, len);
  274. }
  275. unsigned long unwind_get_return_address(struct unwind_state *state)
  276. {
  277. return __unwind_get_return_address(state);
  278. }
  279. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  280. void unwind_start(struct unwind_state *state, struct task_struct *task,
  281. struct pt_regs *regs)
  282. {
  283. __unwind_start(state, task, regs);
  284. state->type = UNWINDER_ORC;
  285. if (!unwind_done(state) && !__kernel_text_address(state->pc))
  286. unwind_next_frame(state);
  287. }
  288. EXPORT_SYMBOL_GPL(unwind_start);
  289. static bool is_entry_func(unsigned long addr)
  290. {
  291. extern u32 kernel_entry;
  292. extern u32 kernel_entry_end;
  293. return addr >= (unsigned long)&kernel_entry && addr < (unsigned long)&kernel_entry_end;
  294. }
  295. static inline unsigned long bt_address(unsigned long ra)
  296. {
  297. extern unsigned long eentry;
  298. if (__kernel_text_address(ra))
  299. return ra;
  300. if (__module_text_address(ra))
  301. return ra;
  302. if (ra >= eentry && ra < eentry + EXCCODE_INT_END * VECSIZE) {
  303. unsigned long func;
  304. unsigned long type = (ra - eentry) / VECSIZE;
  305. unsigned long offset = (ra - eentry) % VECSIZE;
  306. switch (type) {
  307. case 0 ... EXCCODE_INT_START - 1:
  308. func = (unsigned long)exception_table[type];
  309. break;
  310. case EXCCODE_INT_START ... EXCCODE_INT_END:
  311. func = (unsigned long)handle_vint;
  312. break;
  313. default:
  314. func = (unsigned long)handle_reserved;
  315. break;
  316. }
  317. return func + offset;
  318. }
  319. return ra;
  320. }
  321. bool unwind_next_frame(struct unwind_state *state)
  322. {
  323. unsigned long *p, pc;
  324. struct pt_regs *regs;
  325. struct orc_entry *orc;
  326. struct stack_info *info = &state->stack_info;
  327. if (unwind_done(state))
  328. return false;
  329. /* Don't let modules unload while we're reading their ORC data. */
  330. preempt_disable();
  331. if (is_entry_func(state->pc))
  332. goto end;
  333. orc = orc_find(state->pc);
  334. if (!orc) {
  335. /*
  336. * As a fallback, try to assume this code uses a frame pointer.
  337. * This is useful for generated code, like BPF, which ORC
  338. * doesn't know about. This is just a guess, so the rest of
  339. * the unwind is no longer considered reliable.
  340. */
  341. orc = &orc_fp_entry;
  342. state->error = true;
  343. } else {
  344. if (orc->type == ORC_TYPE_UNDEFINED)
  345. goto err;
  346. if (orc->type == ORC_TYPE_END_OF_STACK)
  347. goto end;
  348. }
  349. switch (orc->sp_reg) {
  350. case ORC_REG_SP:
  351. if (info->type == STACK_TYPE_IRQ && state->sp == info->end)
  352. orc->type = ORC_TYPE_REGS;
  353. else
  354. state->sp = state->sp + orc->sp_offset;
  355. break;
  356. case ORC_REG_FP:
  357. state->sp = state->fp;
  358. break;
  359. default:
  360. orc_warn("unknown SP base reg %d at %pB\n", orc->sp_reg, (void *)state->pc);
  361. goto err;
  362. }
  363. switch (orc->fp_reg) {
  364. case ORC_REG_PREV_SP:
  365. p = (unsigned long *)(state->sp + orc->fp_offset);
  366. if (!stack_access_ok(state, (unsigned long)p, sizeof(unsigned long)))
  367. goto err;
  368. state->fp = *p;
  369. break;
  370. case ORC_REG_UNDEFINED:
  371. /* Nothing. */
  372. break;
  373. default:
  374. orc_warn("unknown FP base reg %d at %pB\n", orc->fp_reg, (void *)state->pc);
  375. goto err;
  376. }
  377. switch (orc->type) {
  378. case ORC_TYPE_CALL:
  379. if (orc->ra_reg == ORC_REG_PREV_SP) {
  380. p = (unsigned long *)(state->sp + orc->ra_offset);
  381. if (!stack_access_ok(state, (unsigned long)p, sizeof(unsigned long)))
  382. goto err;
  383. pc = unwind_graph_addr(state, *p, state->sp);
  384. pc -= LOONGARCH_INSN_SIZE;
  385. } else if (orc->ra_reg == ORC_REG_UNDEFINED) {
  386. if (!state->ra || state->ra == state->pc)
  387. goto err;
  388. pc = unwind_graph_addr(state, state->ra, state->sp);
  389. pc -= LOONGARCH_INSN_SIZE;
  390. state->ra = 0;
  391. } else {
  392. orc_warn("unknown ra base reg %d at %pB\n", orc->ra_reg, (void *)state->pc);
  393. goto err;
  394. }
  395. break;
  396. case ORC_TYPE_REGS:
  397. if (info->type == STACK_TYPE_IRQ && state->sp == info->end)
  398. regs = (struct pt_regs *)info->next_sp;
  399. else
  400. regs = (struct pt_regs *)state->sp;
  401. if (!stack_access_ok(state, (unsigned long)regs, sizeof(*regs)))
  402. goto err;
  403. if ((info->end == (unsigned long)regs + sizeof(*regs)) &&
  404. !regs->regs[3] && !regs->regs[1])
  405. goto end;
  406. if (user_mode(regs))
  407. goto end;
  408. pc = regs->csr_era;
  409. if (!__kernel_text_address(pc))
  410. goto err;
  411. state->sp = regs->regs[3];
  412. state->ra = regs->regs[1];
  413. state->fp = regs->regs[22];
  414. get_stack_info(state->sp, state->task, info);
  415. break;
  416. default:
  417. orc_warn("unknown .orc_unwind entry type %d at %pB\n", orc->type, (void *)state->pc);
  418. goto err;
  419. }
  420. state->pc = bt_address(pc);
  421. if (!state->pc) {
  422. pr_err("cannot find unwind pc at %pK\n", (void *)pc);
  423. goto err;
  424. }
  425. if (!__kernel_text_address(state->pc))
  426. goto err;
  427. preempt_enable();
  428. return true;
  429. err:
  430. state->error = true;
  431. end:
  432. preempt_enable();
  433. state->stack_info.type = STACK_TYPE_UNKNOWN;
  434. return false;
  435. }
  436. EXPORT_SYMBOL_GPL(unwind_next_frame);