unwind_frame.c 10 KB

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