unwind_frame.c 11 KB

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