unwind_orc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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/ptrace.h>
  6. #include <asm/stacktrace.h>
  7. #include <asm/unwind.h>
  8. #include <asm/orc_types.h>
  9. #include <asm/orc_lookup.h>
  10. #define orc_warn(fmt, ...) \
  11. printk_deferred_once(KERN_WARNING "WARNING: " fmt, ##__VA_ARGS__)
  12. #define orc_warn_current(args...) \
  13. ({ \
  14. if (state->task == current && !state->error) \
  15. orc_warn(args); \
  16. })
  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. static inline unsigned long orc_ip(const int *ip)
  24. {
  25. return (unsigned long)ip + *ip;
  26. }
  27. static struct orc_entry *__orc_find(int *ip_table, struct orc_entry *u_table,
  28. unsigned int num_entries, unsigned long ip)
  29. {
  30. int *first = ip_table;
  31. int *last = ip_table + num_entries - 1;
  32. int *mid = first, *found = first;
  33. if (!num_entries)
  34. return NULL;
  35. /*
  36. * Do a binary range search to find the rightmost duplicate of a given
  37. * starting address. Some entries are section terminators which are
  38. * "weak" entries for ensuring there are no gaps. They should be
  39. * ignored when they conflict with a real entry.
  40. */
  41. while (first <= last) {
  42. mid = first + ((last - first) / 2);
  43. if (orc_ip(mid) <= ip) {
  44. found = mid;
  45. first = mid + 1;
  46. } else
  47. last = mid - 1;
  48. }
  49. return u_table + (found - ip_table);
  50. }
  51. #ifdef CONFIG_MODULES
  52. static struct orc_entry *orc_module_find(unsigned long ip)
  53. {
  54. struct module *mod;
  55. mod = __module_address(ip);
  56. if (!mod || !mod->arch.orc_unwind || !mod->arch.orc_unwind_ip)
  57. return NULL;
  58. return __orc_find(mod->arch.orc_unwind_ip, mod->arch.orc_unwind,
  59. mod->arch.num_orcs, ip);
  60. }
  61. #else
  62. static struct orc_entry *orc_module_find(unsigned long ip)
  63. {
  64. return NULL;
  65. }
  66. #endif
  67. #ifdef CONFIG_DYNAMIC_FTRACE
  68. static struct orc_entry *orc_find(unsigned long ip);
  69. /*
  70. * Ftrace dynamic trampolines do not have orc entries of their own.
  71. * But they are copies of the ftrace entries that are static and
  72. * defined in ftrace_*.S, which do have orc entries.
  73. *
  74. * If the unwinder comes across a ftrace trampoline, then find the
  75. * ftrace function that was used to create it, and use that ftrace
  76. * function's orc entry, as the placement of the return code in
  77. * the stack will be identical.
  78. */
  79. static struct orc_entry *orc_ftrace_find(unsigned long ip)
  80. {
  81. struct ftrace_ops *ops;
  82. unsigned long tramp_addr, offset;
  83. ops = ftrace_ops_trampoline(ip);
  84. if (!ops)
  85. return NULL;
  86. /* Set tramp_addr to the start of the code copied by the trampoline */
  87. if (ops->flags & FTRACE_OPS_FL_SAVE_REGS)
  88. tramp_addr = (unsigned long)ftrace_regs_caller;
  89. else
  90. tramp_addr = (unsigned long)ftrace_caller;
  91. /* Now place tramp_addr to the location within the trampoline ip is at */
  92. offset = ip - ops->trampoline;
  93. tramp_addr += offset;
  94. /* Prevent unlikely recursion */
  95. if (ip == tramp_addr)
  96. return NULL;
  97. return orc_find(tramp_addr);
  98. }
  99. #else
  100. static struct orc_entry *orc_ftrace_find(unsigned long ip)
  101. {
  102. return NULL;
  103. }
  104. #endif
  105. /*
  106. * If we crash with IP==0, the last successfully executed instruction
  107. * was probably an indirect function call with a NULL function pointer,
  108. * and we don't have unwind information for NULL.
  109. * This hardcoded ORC entry for IP==0 allows us to unwind from a NULL function
  110. * pointer into its parent and then continue normally from there.
  111. */
  112. static struct orc_entry null_orc_entry = {
  113. .sp_offset = sizeof(long),
  114. .sp_reg = ORC_REG_SP,
  115. .bp_reg = ORC_REG_UNDEFINED,
  116. .type = UNWIND_HINT_TYPE_CALL
  117. };
  118. /* Fake frame pointer entry -- used as a fallback for generated code */
  119. static struct orc_entry orc_fp_entry = {
  120. .type = UNWIND_HINT_TYPE_CALL,
  121. .sp_reg = ORC_REG_BP,
  122. .sp_offset = 16,
  123. .bp_reg = ORC_REG_PREV_SP,
  124. .bp_offset = -16,
  125. .end = 0,
  126. };
  127. static struct orc_entry *orc_find(unsigned long ip)
  128. {
  129. static struct orc_entry *orc;
  130. if (ip == 0)
  131. return &null_orc_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. struct orc_entry *orc_a, *orc_b;
  169. struct orc_entry orc_tmp;
  170. int *a = _a, *b = _b, tmp;
  171. int delta = _b - _a;
  172. /* Swap the .orc_unwind_ip entries: */
  173. tmp = *a;
  174. *a = *b + delta;
  175. *b = tmp - delta;
  176. /* Swap the corresponding .orc_unwind entries: */
  177. orc_a = cur_orc_table + (a - cur_orc_ip_table);
  178. orc_b = cur_orc_table + (b - cur_orc_ip_table);
  179. orc_tmp = *orc_a;
  180. *orc_a = *orc_b;
  181. *orc_b = orc_tmp;
  182. }
  183. static int orc_sort_cmp(const void *_a, const void *_b)
  184. {
  185. struct orc_entry *orc_a;
  186. const int *a = _a, *b = _b;
  187. unsigned long a_val = orc_ip(a);
  188. unsigned long b_val = orc_ip(b);
  189. if (a_val > b_val)
  190. return 1;
  191. if (a_val < b_val)
  192. return -1;
  193. /*
  194. * The "weak" section terminator entries need to always be on the left
  195. * to ensure the lookup code skips them in favor of real entries.
  196. * These terminator entries exist to handle any gaps created by
  197. * whitelisted .o files which didn't get objtool generation.
  198. */
  199. orc_a = cur_orc_table + (a - cur_orc_ip_table);
  200. return orc_a->sp_reg == ORC_REG_UNDEFINED && !orc_a->end ? -1 : 1;
  201. }
  202. void unwind_module_init(struct module *mod, void *_orc_ip, size_t orc_ip_size,
  203. void *_orc, size_t orc_size)
  204. {
  205. int *orc_ip = _orc_ip;
  206. struct orc_entry *orc = _orc;
  207. unsigned int num_entries = orc_ip_size / sizeof(int);
  208. WARN_ON_ONCE(orc_ip_size % sizeof(int) != 0 ||
  209. orc_size % sizeof(*orc) != 0 ||
  210. num_entries != orc_size / sizeof(*orc));
  211. /*
  212. * The 'cur_orc_*' globals allow the orc_sort_swap() callback to
  213. * associate an .orc_unwind_ip table entry with its corresponding
  214. * .orc_unwind entry so they can both be swapped.
  215. */
  216. mutex_lock(&sort_mutex);
  217. cur_orc_ip_table = orc_ip;
  218. cur_orc_table = orc;
  219. sort(orc_ip, num_entries, sizeof(int), orc_sort_cmp, orc_sort_swap);
  220. mutex_unlock(&sort_mutex);
  221. mod->arch.orc_unwind_ip = orc_ip;
  222. mod->arch.orc_unwind = orc;
  223. mod->arch.num_orcs = num_entries;
  224. }
  225. #endif
  226. void __init unwind_init(void)
  227. {
  228. size_t orc_ip_size = (void *)__stop_orc_unwind_ip - (void *)__start_orc_unwind_ip;
  229. size_t orc_size = (void *)__stop_orc_unwind - (void *)__start_orc_unwind;
  230. size_t num_entries = orc_ip_size / sizeof(int);
  231. struct orc_entry *orc;
  232. int i;
  233. if (!num_entries || orc_ip_size % sizeof(int) != 0 ||
  234. orc_size % sizeof(struct orc_entry) != 0 ||
  235. num_entries != orc_size / sizeof(struct orc_entry)) {
  236. orc_warn("WARNING: Bad or missing .orc_unwind table. Disabling unwinder.\n");
  237. return;
  238. }
  239. /*
  240. * Note, the orc_unwind and orc_unwind_ip tables were already
  241. * sorted at build time via the 'sorttable' tool.
  242. * It's ready for binary search straight away, no need to sort it.
  243. */
  244. /* Initialize the fast lookup table: */
  245. lookup_num_blocks = orc_lookup_end - orc_lookup;
  246. for (i = 0; i < lookup_num_blocks-1; i++) {
  247. orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind,
  248. num_entries,
  249. LOOKUP_START_IP + (LOOKUP_BLOCK_SIZE * i));
  250. if (!orc) {
  251. orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
  252. return;
  253. }
  254. orc_lookup[i] = orc - __start_orc_unwind;
  255. }
  256. /* Initialize the ending block: */
  257. orc = __orc_find(__start_orc_unwind_ip, __start_orc_unwind, num_entries,
  258. LOOKUP_STOP_IP);
  259. if (!orc) {
  260. orc_warn("WARNING: Corrupt .orc_unwind table. Disabling unwinder.\n");
  261. return;
  262. }
  263. orc_lookup[lookup_num_blocks-1] = orc - __start_orc_unwind;
  264. orc_init = true;
  265. }
  266. unsigned long unwind_get_return_address(struct unwind_state *state)
  267. {
  268. if (unwind_done(state))
  269. return 0;
  270. return __kernel_text_address(state->ip) ? state->ip : 0;
  271. }
  272. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  273. unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
  274. {
  275. if (unwind_done(state))
  276. return NULL;
  277. if (state->regs)
  278. return &state->regs->ip;
  279. if (state->sp)
  280. return (unsigned long *)state->sp - 1;
  281. return NULL;
  282. }
  283. static bool stack_access_ok(struct unwind_state *state, unsigned long _addr,
  284. size_t len)
  285. {
  286. struct stack_info *info = &state->stack_info;
  287. void *addr = (void *)_addr;
  288. if (on_stack(info, addr, len))
  289. return true;
  290. return !get_stack_info(addr, state->task, info, &state->stack_mask) &&
  291. on_stack(info, addr, len);
  292. }
  293. static bool deref_stack_reg(struct unwind_state *state, unsigned long addr,
  294. unsigned long *val)
  295. {
  296. if (!stack_access_ok(state, addr, sizeof(long)))
  297. return false;
  298. *val = READ_ONCE_NOCHECK(*(unsigned long *)addr);
  299. return true;
  300. }
  301. static bool deref_stack_regs(struct unwind_state *state, unsigned long addr,
  302. unsigned long *ip, unsigned long *sp)
  303. {
  304. struct pt_regs *regs = (struct pt_regs *)addr;
  305. /* x86-32 support will be more complicated due to the &regs->sp hack */
  306. BUILD_BUG_ON(IS_ENABLED(CONFIG_X86_32));
  307. if (!stack_access_ok(state, addr, sizeof(struct pt_regs)))
  308. return false;
  309. *ip = READ_ONCE_NOCHECK(regs->ip);
  310. *sp = READ_ONCE_NOCHECK(regs->sp);
  311. return true;
  312. }
  313. static bool deref_stack_iret_regs(struct unwind_state *state, unsigned long addr,
  314. unsigned long *ip, unsigned long *sp)
  315. {
  316. struct pt_regs *regs = (void *)addr - IRET_FRAME_OFFSET;
  317. if (!stack_access_ok(state, addr, IRET_FRAME_SIZE))
  318. return false;
  319. *ip = READ_ONCE_NOCHECK(regs->ip);
  320. *sp = READ_ONCE_NOCHECK(regs->sp);
  321. return true;
  322. }
  323. /*
  324. * If state->regs is non-NULL, and points to a full pt_regs, just get the reg
  325. * value from state->regs.
  326. *
  327. * Otherwise, if state->regs just points to IRET regs, and the previous frame
  328. * had full regs, it's safe to get the value from the previous regs. This can
  329. * happen when early/late IRQ entry code gets interrupted by an NMI.
  330. */
  331. static bool get_reg(struct unwind_state *state, unsigned int reg_off,
  332. unsigned long *val)
  333. {
  334. unsigned int reg = reg_off/8;
  335. if (!state->regs)
  336. return false;
  337. if (state->full_regs) {
  338. *val = READ_ONCE_NOCHECK(((unsigned long *)state->regs)[reg]);
  339. return true;
  340. }
  341. if (state->prev_regs) {
  342. *val = READ_ONCE_NOCHECK(((unsigned long *)state->prev_regs)[reg]);
  343. return true;
  344. }
  345. return false;
  346. }
  347. bool unwind_next_frame(struct unwind_state *state)
  348. {
  349. unsigned long ip_p, sp, tmp, orig_ip = state->ip, prev_sp = state->sp;
  350. enum stack_type prev_type = state->stack_info.type;
  351. struct orc_entry *orc;
  352. bool indirect = false;
  353. if (unwind_done(state))
  354. return false;
  355. /* Don't let modules unload while we're reading their ORC data. */
  356. preempt_disable();
  357. /* End-of-stack check for user tasks: */
  358. if (state->regs && user_mode(state->regs))
  359. goto the_end;
  360. /*
  361. * Find the orc_entry associated with the text address.
  362. *
  363. * For a call frame (as opposed to a signal frame), state->ip points to
  364. * the instruction after the call. That instruction's stack layout
  365. * could be different from the call instruction's layout, for example
  366. * if the call was to a noreturn function. So get the ORC data for the
  367. * call instruction itself.
  368. */
  369. orc = orc_find(state->signal ? state->ip : state->ip - 1);
  370. if (!orc) {
  371. /*
  372. * As a fallback, try to assume this code uses a frame pointer.
  373. * This is useful for generated code, like BPF, which ORC
  374. * doesn't know about. This is just a guess, so the rest of
  375. * the unwind is no longer considered reliable.
  376. */
  377. orc = &orc_fp_entry;
  378. state->error = true;
  379. }
  380. /* End-of-stack check for kernel threads: */
  381. if (orc->sp_reg == ORC_REG_UNDEFINED) {
  382. if (!orc->end)
  383. goto err;
  384. goto the_end;
  385. }
  386. /* Find the previous frame's stack: */
  387. switch (orc->sp_reg) {
  388. case ORC_REG_SP:
  389. sp = state->sp + orc->sp_offset;
  390. break;
  391. case ORC_REG_BP:
  392. sp = state->bp + orc->sp_offset;
  393. break;
  394. case ORC_REG_SP_INDIRECT:
  395. sp = state->sp;
  396. indirect = true;
  397. break;
  398. case ORC_REG_BP_INDIRECT:
  399. sp = state->bp + orc->sp_offset;
  400. indirect = true;
  401. break;
  402. case ORC_REG_R10:
  403. if (!get_reg(state, offsetof(struct pt_regs, r10), &sp)) {
  404. orc_warn_current("missing R10 value at %pB\n",
  405. (void *)state->ip);
  406. goto err;
  407. }
  408. break;
  409. case ORC_REG_R13:
  410. if (!get_reg(state, offsetof(struct pt_regs, r13), &sp)) {
  411. orc_warn_current("missing R13 value at %pB\n",
  412. (void *)state->ip);
  413. goto err;
  414. }
  415. break;
  416. case ORC_REG_DI:
  417. if (!get_reg(state, offsetof(struct pt_regs, di), &sp)) {
  418. orc_warn_current("missing RDI value at %pB\n",
  419. (void *)state->ip);
  420. goto err;
  421. }
  422. break;
  423. case ORC_REG_DX:
  424. if (!get_reg(state, offsetof(struct pt_regs, dx), &sp)) {
  425. orc_warn_current("missing DX value at %pB\n",
  426. (void *)state->ip);
  427. goto err;
  428. }
  429. break;
  430. default:
  431. orc_warn("unknown SP base reg %d at %pB\n",
  432. orc->sp_reg, (void *)state->ip);
  433. goto err;
  434. }
  435. if (indirect) {
  436. if (!deref_stack_reg(state, sp, &sp))
  437. goto err;
  438. if (orc->sp_reg == ORC_REG_SP_INDIRECT)
  439. sp += orc->sp_offset;
  440. }
  441. /* Find IP, SP and possibly regs: */
  442. switch (orc->type) {
  443. case UNWIND_HINT_TYPE_CALL:
  444. ip_p = sp - sizeof(long);
  445. if (!deref_stack_reg(state, ip_p, &state->ip))
  446. goto err;
  447. state->ip = unwind_recover_ret_addr(state, state->ip,
  448. (unsigned long *)ip_p);
  449. state->sp = sp;
  450. state->regs = NULL;
  451. state->prev_regs = NULL;
  452. state->signal = false;
  453. break;
  454. case UNWIND_HINT_TYPE_REGS:
  455. if (!deref_stack_regs(state, sp, &state->ip, &state->sp)) {
  456. orc_warn_current("can't access registers at %pB\n",
  457. (void *)orig_ip);
  458. goto err;
  459. }
  460. /*
  461. * There is a small chance to interrupt at the entry of
  462. * arch_rethook_trampoline() where the ORC info doesn't exist.
  463. * That point is right after the RET to arch_rethook_trampoline()
  464. * which was modified return address.
  465. * At that point, the @addr_p of the unwind_recover_rethook()
  466. * (this has to point the address of the stack entry storing
  467. * the modified return address) must be "SP - (a stack entry)"
  468. * because SP is incremented by the RET.
  469. */
  470. state->ip = unwind_recover_rethook(state, state->ip,
  471. (unsigned long *)(state->sp - sizeof(long)));
  472. state->regs = (struct pt_regs *)sp;
  473. state->prev_regs = NULL;
  474. state->full_regs = true;
  475. state->signal = true;
  476. break;
  477. case UNWIND_HINT_TYPE_REGS_PARTIAL:
  478. if (!deref_stack_iret_regs(state, sp, &state->ip, &state->sp)) {
  479. orc_warn_current("can't access iret registers at %pB\n",
  480. (void *)orig_ip);
  481. goto err;
  482. }
  483. /* See UNWIND_HINT_TYPE_REGS case comment. */
  484. state->ip = unwind_recover_rethook(state, state->ip,
  485. (unsigned long *)(state->sp - sizeof(long)));
  486. if (state->full_regs)
  487. state->prev_regs = state->regs;
  488. state->regs = (void *)sp - IRET_FRAME_OFFSET;
  489. state->full_regs = false;
  490. state->signal = true;
  491. break;
  492. default:
  493. orc_warn("unknown .orc_unwind entry type %d at %pB\n",
  494. orc->type, (void *)orig_ip);
  495. goto err;
  496. }
  497. /* Find BP: */
  498. switch (orc->bp_reg) {
  499. case ORC_REG_UNDEFINED:
  500. if (get_reg(state, offsetof(struct pt_regs, bp), &tmp))
  501. state->bp = tmp;
  502. break;
  503. case ORC_REG_PREV_SP:
  504. if (!deref_stack_reg(state, sp + orc->bp_offset, &state->bp))
  505. goto err;
  506. break;
  507. case ORC_REG_BP:
  508. if (!deref_stack_reg(state, state->bp + orc->bp_offset, &state->bp))
  509. goto err;
  510. break;
  511. default:
  512. orc_warn("unknown BP base reg %d for ip %pB\n",
  513. orc->bp_reg, (void *)orig_ip);
  514. goto err;
  515. }
  516. /* Prevent a recursive loop due to bad ORC data: */
  517. if (state->stack_info.type == prev_type &&
  518. on_stack(&state->stack_info, (void *)state->sp, sizeof(long)) &&
  519. state->sp <= prev_sp) {
  520. orc_warn_current("stack going in the wrong direction? at %pB\n",
  521. (void *)orig_ip);
  522. goto err;
  523. }
  524. preempt_enable();
  525. return true;
  526. err:
  527. state->error = true;
  528. the_end:
  529. preempt_enable();
  530. state->stack_info.type = STACK_TYPE_UNKNOWN;
  531. return false;
  532. }
  533. EXPORT_SYMBOL_GPL(unwind_next_frame);
  534. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  535. struct pt_regs *regs, unsigned long *first_frame)
  536. {
  537. memset(state, 0, sizeof(*state));
  538. state->task = task;
  539. if (!orc_init)
  540. goto err;
  541. /*
  542. * Refuse to unwind the stack of a task while it's executing on another
  543. * CPU. This check is racy, but that's ok: the unwinder has other
  544. * checks to prevent it from going off the rails.
  545. */
  546. if (task_on_another_cpu(task))
  547. goto err;
  548. if (regs) {
  549. if (user_mode(regs))
  550. goto the_end;
  551. state->ip = regs->ip;
  552. state->sp = regs->sp;
  553. state->bp = regs->bp;
  554. state->regs = regs;
  555. state->full_regs = true;
  556. state->signal = true;
  557. } else if (task == current) {
  558. asm volatile("lea (%%rip), %0\n\t"
  559. "mov %%rsp, %1\n\t"
  560. "mov %%rbp, %2\n\t"
  561. : "=r" (state->ip), "=r" (state->sp),
  562. "=r" (state->bp));
  563. } else {
  564. struct inactive_task_frame *frame = (void *)task->thread.sp;
  565. state->sp = task->thread.sp + sizeof(*frame);
  566. state->bp = READ_ONCE_NOCHECK(frame->bp);
  567. state->ip = READ_ONCE_NOCHECK(frame->ret_addr);
  568. state->signal = (void *)state->ip == ret_from_fork;
  569. }
  570. if (get_stack_info((unsigned long *)state->sp, state->task,
  571. &state->stack_info, &state->stack_mask)) {
  572. /*
  573. * We weren't on a valid stack. It's possible that
  574. * we overflowed a valid stack into a guard page.
  575. * See if the next page up is valid so that we can
  576. * generate some kind of backtrace if this happens.
  577. */
  578. void *next_page = (void *)PAGE_ALIGN((unsigned long)state->sp);
  579. state->error = true;
  580. if (get_stack_info(next_page, state->task, &state->stack_info,
  581. &state->stack_mask))
  582. return;
  583. }
  584. /*
  585. * The caller can provide the address of the first frame directly
  586. * (first_frame) or indirectly (regs->sp) to indicate which stack frame
  587. * to start unwinding at. Skip ahead until we reach it.
  588. */
  589. /* When starting from regs, skip the regs frame: */
  590. if (regs) {
  591. unwind_next_frame(state);
  592. return;
  593. }
  594. /* Otherwise, skip ahead to the user-specified starting frame: */
  595. while (!unwind_done(state) &&
  596. (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
  597. state->sp <= (unsigned long)first_frame))
  598. unwind_next_frame(state);
  599. return;
  600. err:
  601. state->error = true;
  602. the_end:
  603. state->stack_info.type = STACK_TYPE_UNKNOWN;
  604. }
  605. EXPORT_SYMBOL_GPL(__unwind_start);