unwind_frame.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/sched.h>
  3. #include <linux/sched/task.h>
  4. #include <linux/sched/task_stack.h>
  5. #include <linux/interrupt.h>
  6. #include <asm/sections.h>
  7. #include <asm/ptrace.h>
  8. #include <asm/bitops.h>
  9. #include <asm/stacktrace.h>
  10. #include <asm/unwind.h>
  11. #define FRAME_HEADER_SIZE (sizeof(long) * 2)
  12. unsigned long unwind_get_return_address(struct unwind_state *state)
  13. {
  14. if (unwind_done(state))
  15. return 0;
  16. return __kernel_text_address(state->ip) ? state->ip : 0;
  17. }
  18. EXPORT_SYMBOL_GPL(unwind_get_return_address);
  19. unsigned long *unwind_get_return_address_ptr(struct unwind_state *state)
  20. {
  21. if (unwind_done(state))
  22. return NULL;
  23. return state->regs ? &state->regs->ip : state->bp + 1;
  24. }
  25. static void unwind_dump(struct unwind_state *state)
  26. {
  27. static bool dumped_before = false;
  28. bool prev_zero, zero = false;
  29. unsigned long word, *sp;
  30. struct stack_info stack_info = {0};
  31. unsigned long visit_mask = 0;
  32. if (dumped_before)
  33. return;
  34. dumped_before = true;
  35. printk_deferred("unwind stack type:%d next_sp:%p mask:0x%lx graph_idx:%d\n",
  36. state->stack_info.type, state->stack_info.next_sp,
  37. state->stack_mask, state->graph_idx);
  38. for (sp = PTR_ALIGN(state->orig_sp, sizeof(long)); sp;
  39. sp = PTR_ALIGN(stack_info.next_sp, sizeof(long))) {
  40. if (get_stack_info(sp, state->task, &stack_info, &visit_mask))
  41. break;
  42. for (; sp < stack_info.end; sp++) {
  43. word = READ_ONCE_NOCHECK(*sp);
  44. prev_zero = zero;
  45. zero = word == 0;
  46. if (zero) {
  47. if (!prev_zero)
  48. printk_deferred("%p: %0*x ...\n",
  49. sp, BITS_PER_LONG/4, 0);
  50. continue;
  51. }
  52. printk_deferred("%p: %0*lx (%pB)\n",
  53. sp, BITS_PER_LONG/4, word, (void *)word);
  54. }
  55. }
  56. }
  57. static bool in_entry_code(unsigned long ip)
  58. {
  59. char *addr = (char *)ip;
  60. return addr >= __entry_text_start && addr < __entry_text_end;
  61. }
  62. static inline unsigned long *last_frame(struct unwind_state *state)
  63. {
  64. return (unsigned long *)task_pt_regs(state->task) - 2;
  65. }
  66. static bool is_last_frame(struct unwind_state *state)
  67. {
  68. return state->bp == last_frame(state);
  69. }
  70. #ifdef CONFIG_X86_32
  71. #define GCC_REALIGN_WORDS 3
  72. #else
  73. #define GCC_REALIGN_WORDS 1
  74. #endif
  75. static inline unsigned long *last_aligned_frame(struct unwind_state *state)
  76. {
  77. return last_frame(state) - GCC_REALIGN_WORDS;
  78. }
  79. static bool is_last_aligned_frame(struct unwind_state *state)
  80. {
  81. unsigned long *last_bp = last_frame(state);
  82. unsigned long *aligned_bp = last_aligned_frame(state);
  83. /*
  84. * GCC can occasionally decide to realign the stack pointer and change
  85. * the offset of the stack frame in the prologue of a function called
  86. * by head/entry code. Examples:
  87. *
  88. * <start_secondary>:
  89. * push %edi
  90. * lea 0x8(%esp),%edi
  91. * and $0xfffffff8,%esp
  92. * pushl -0x4(%edi)
  93. * push %ebp
  94. * mov %esp,%ebp
  95. *
  96. * <x86_64_start_kernel>:
  97. * lea 0x8(%rsp),%r10
  98. * and $0xfffffffffffffff0,%rsp
  99. * pushq -0x8(%r10)
  100. * push %rbp
  101. * mov %rsp,%rbp
  102. *
  103. * After aligning the stack, it pushes a duplicate copy of the return
  104. * address before pushing the frame pointer.
  105. */
  106. return (state->bp == aligned_bp && *(aligned_bp + 1) == *(last_bp + 1));
  107. }
  108. static bool is_last_ftrace_frame(struct unwind_state *state)
  109. {
  110. unsigned long *last_bp = last_frame(state);
  111. unsigned long *last_ftrace_bp = last_bp - 3;
  112. /*
  113. * When unwinding from an ftrace handler of a function called by entry
  114. * code, the stack layout of the last frame is:
  115. *
  116. * bp
  117. * parent ret addr
  118. * bp
  119. * function ret addr
  120. * parent ret addr
  121. * pt_regs
  122. * -----------------
  123. */
  124. return (state->bp == last_ftrace_bp &&
  125. *state->bp == *(state->bp + 2) &&
  126. *(state->bp + 1) == *(state->bp + 4));
  127. }
  128. static bool is_last_task_frame(struct unwind_state *state)
  129. {
  130. return is_last_frame(state) || is_last_aligned_frame(state) ||
  131. is_last_ftrace_frame(state);
  132. }
  133. /*
  134. * This determines if the frame pointer actually contains an encoded pointer to
  135. * pt_regs on the stack. See ENCODE_FRAME_POINTER.
  136. */
  137. #ifdef CONFIG_X86_64
  138. static struct pt_regs *decode_frame_pointer(unsigned long *bp)
  139. {
  140. unsigned long regs = (unsigned long)bp;
  141. if (!(regs & 0x1))
  142. return NULL;
  143. return (struct pt_regs *)(regs & ~0x1);
  144. }
  145. #else
  146. static struct pt_regs *decode_frame_pointer(unsigned long *bp)
  147. {
  148. unsigned long regs = (unsigned long)bp;
  149. if (regs & 0x80000000)
  150. return NULL;
  151. return (struct pt_regs *)(regs | 0x80000000);
  152. }
  153. #endif
  154. static bool update_stack_state(struct unwind_state *state,
  155. unsigned long *next_bp)
  156. {
  157. struct stack_info *info = &state->stack_info;
  158. enum stack_type prev_type = info->type;
  159. struct pt_regs *regs;
  160. unsigned long *frame, *prev_frame_end, *addr_p, addr;
  161. size_t len;
  162. if (state->regs)
  163. prev_frame_end = (void *)state->regs + sizeof(*state->regs);
  164. else
  165. prev_frame_end = (void *)state->bp + FRAME_HEADER_SIZE;
  166. /* Is the next frame pointer an encoded pointer to pt_regs? */
  167. regs = decode_frame_pointer(next_bp);
  168. if (regs) {
  169. frame = (unsigned long *)regs;
  170. len = sizeof(*regs);
  171. state->got_irq = true;
  172. } else {
  173. frame = next_bp;
  174. len = FRAME_HEADER_SIZE;
  175. }
  176. /*
  177. * If the next bp isn't on the current stack, switch to the next one.
  178. *
  179. * We may have to traverse multiple stacks to deal with the possibility
  180. * that info->next_sp could point to an empty stack and the next bp
  181. * could be on a subsequent stack.
  182. */
  183. while (!on_stack(info, frame, len))
  184. if (get_stack_info(info->next_sp, state->task, info,
  185. &state->stack_mask))
  186. return false;
  187. /* Make sure it only unwinds up and doesn't overlap the prev frame: */
  188. if (state->orig_sp && state->stack_info.type == prev_type &&
  189. frame < prev_frame_end)
  190. return false;
  191. /* Move state to the next frame: */
  192. if (regs) {
  193. state->regs = regs;
  194. state->bp = NULL;
  195. } else {
  196. state->bp = next_bp;
  197. state->regs = NULL;
  198. }
  199. /* Save the return address: */
  200. if (state->regs && user_mode(state->regs))
  201. state->ip = 0;
  202. else {
  203. addr_p = unwind_get_return_address_ptr(state);
  204. addr = READ_ONCE_TASK_STACK(state->task, *addr_p);
  205. state->ip = ftrace_graph_ret_addr(state->task, &state->graph_idx,
  206. addr, addr_p);
  207. }
  208. /* Save the original stack pointer for unwind_dump(): */
  209. if (!state->orig_sp)
  210. state->orig_sp = frame;
  211. return true;
  212. }
  213. bool unwind_next_frame(struct unwind_state *state)
  214. {
  215. struct pt_regs *regs;
  216. unsigned long *next_bp;
  217. if (unwind_done(state))
  218. return false;
  219. /* Have we reached the end? */
  220. if (state->regs && user_mode(state->regs))
  221. goto the_end;
  222. if (is_last_task_frame(state)) {
  223. regs = task_pt_regs(state->task);
  224. /*
  225. * kthreads (other than the boot CPU's idle thread) have some
  226. * partial regs at the end of their stack which were placed
  227. * there by copy_thread(). But the regs don't have any
  228. * useful information, so we can skip them.
  229. *
  230. * This user_mode() check is slightly broader than a PF_KTHREAD
  231. * check because it also catches the awkward situation where a
  232. * newly forked kthread transitions into a user task by calling
  233. * kernel_execve(), which eventually clears PF_KTHREAD.
  234. */
  235. if (!user_mode(regs))
  236. goto the_end;
  237. /*
  238. * We're almost at the end, but not quite: there's still the
  239. * syscall regs frame. Entry code doesn't encode the regs
  240. * pointer for syscalls, so we have to set it manually.
  241. */
  242. state->regs = regs;
  243. state->bp = NULL;
  244. state->ip = 0;
  245. return true;
  246. }
  247. /* Get the next frame pointer: */
  248. if (state->next_bp) {
  249. next_bp = state->next_bp;
  250. state->next_bp = NULL;
  251. } else if (state->regs) {
  252. next_bp = (unsigned long *)state->regs->bp;
  253. } else {
  254. next_bp = (unsigned long *)READ_ONCE_TASK_STACK(state->task, *state->bp);
  255. }
  256. /* Move to the next frame if it's safe: */
  257. if (!update_stack_state(state, next_bp))
  258. goto bad_address;
  259. return true;
  260. bad_address:
  261. state->error = true;
  262. /*
  263. * When unwinding a non-current task, the task might actually be
  264. * running on another CPU, in which case it could be modifying its
  265. * stack while we're reading it. This is generally not a problem and
  266. * can be ignored as long as the caller understands that unwinding
  267. * another task will not always succeed.
  268. */
  269. if (state->task != current)
  270. goto the_end;
  271. /*
  272. * Don't warn if the unwinder got lost due to an interrupt in entry
  273. * code or in the C handler before the first frame pointer got set up:
  274. */
  275. if (state->got_irq && in_entry_code(state->ip))
  276. goto the_end;
  277. if (state->regs &&
  278. state->regs->sp >= (unsigned long)last_aligned_frame(state) &&
  279. state->regs->sp < (unsigned long)task_pt_regs(state->task))
  280. goto the_end;
  281. /*
  282. * There are some known frame pointer issues on 32-bit. Disable
  283. * unwinder warnings on 32-bit until it gets objtool support.
  284. */
  285. if (IS_ENABLED(CONFIG_X86_32))
  286. goto the_end;
  287. if (state->task != current)
  288. goto the_end;
  289. if (state->regs) {
  290. printk_deferred_once(KERN_WARNING
  291. "WARNING: kernel stack regs at %p in %s:%d has bad 'bp' value %p\n",
  292. state->regs, state->task->comm,
  293. state->task->pid, next_bp);
  294. unwind_dump(state);
  295. } else {
  296. printk_deferred_once(KERN_WARNING
  297. "WARNING: kernel stack frame pointer at %p in %s:%d has bad value %p\n",
  298. state->bp, state->task->comm,
  299. state->task->pid, next_bp);
  300. unwind_dump(state);
  301. }
  302. the_end:
  303. state->stack_info.type = STACK_TYPE_UNKNOWN;
  304. return false;
  305. }
  306. EXPORT_SYMBOL_GPL(unwind_next_frame);
  307. void __unwind_start(struct unwind_state *state, struct task_struct *task,
  308. struct pt_regs *regs, unsigned long *first_frame)
  309. {
  310. unsigned long *bp;
  311. memset(state, 0, sizeof(*state));
  312. state->task = task;
  313. state->got_irq = (regs);
  314. /* Don't even attempt to start from user mode regs: */
  315. if (regs && user_mode(regs)) {
  316. state->stack_info.type = STACK_TYPE_UNKNOWN;
  317. return;
  318. }
  319. bp = get_frame_pointer(task, regs);
  320. /*
  321. * If we crash with IP==0, the last successfully executed instruction
  322. * was probably an indirect function call with a NULL function pointer.
  323. * That means that SP points into the middle of an incomplete frame:
  324. * *SP is a return pointer, and *(SP-sizeof(unsigned long)) is where we
  325. * would have written a frame pointer if we hadn't crashed.
  326. * Pretend that the frame is complete and that BP points to it, but save
  327. * the real BP so that we can use it when looking for the next frame.
  328. */
  329. if (regs && regs->ip == 0 && (unsigned long *)regs->sp >= first_frame) {
  330. state->next_bp = bp;
  331. bp = ((unsigned long *)regs->sp) - 1;
  332. }
  333. /* Initialize stack info and make sure the frame data is accessible: */
  334. get_stack_info(bp, state->task, &state->stack_info,
  335. &state->stack_mask);
  336. update_stack_state(state, bp);
  337. /*
  338. * The caller can provide the address of the first frame directly
  339. * (first_frame) or indirectly (regs->sp) to indicate which stack frame
  340. * to start unwinding at. Skip ahead until we reach it.
  341. */
  342. while (!unwind_done(state) &&
  343. (!on_stack(&state->stack_info, first_frame, sizeof(long)) ||
  344. (state->next_bp == NULL && state->bp < first_frame)))
  345. unwind_next_frame(state);
  346. }
  347. EXPORT_SYMBOL_GPL(__unwind_start);