stacktrace.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Stack trace management functions
  4. *
  5. * Copyright (C) 2022 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/sched.h>
  8. #include <linux/stacktrace.h>
  9. #include <linux/uaccess.h>
  10. #include <asm/stacktrace.h>
  11. #include <asm/unwind.h>
  12. void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie,
  13. struct task_struct *task, struct pt_regs *regs)
  14. {
  15. unsigned long addr;
  16. struct pt_regs dummyregs;
  17. struct unwind_state state;
  18. if (!regs) {
  19. regs = &dummyregs;
  20. if (task == current) {
  21. regs->regs[3] = (unsigned long)__builtin_frame_address(0);
  22. regs->csr_era = (unsigned long)__builtin_return_address(0);
  23. } else {
  24. regs->regs[3] = thread_saved_fp(task);
  25. regs->csr_era = thread_saved_ra(task);
  26. }
  27. regs->regs[1] = 0;
  28. regs->regs[22] = 0;
  29. }
  30. for (unwind_start(&state, task, regs);
  31. !unwind_done(&state); unwind_next_frame(&state)) {
  32. addr = unwind_get_return_address(&state);
  33. if (!addr || !consume_entry(cookie, addr))
  34. break;
  35. }
  36. }
  37. int arch_stack_walk_reliable(stack_trace_consume_fn consume_entry,
  38. void *cookie, struct task_struct *task)
  39. {
  40. unsigned long addr;
  41. struct pt_regs dummyregs;
  42. struct pt_regs *regs = &dummyregs;
  43. struct unwind_state state;
  44. if (task == current) {
  45. regs->regs[3] = (unsigned long)__builtin_frame_address(0);
  46. regs->csr_era = (unsigned long)__builtin_return_address(0);
  47. regs->regs[22] = 0;
  48. } else {
  49. regs->regs[3] = thread_saved_fp(task);
  50. regs->csr_era = thread_saved_ra(task);
  51. regs->regs[22] = task->thread.reg22;
  52. }
  53. regs->regs[1] = 0;
  54. for (unwind_start(&state, task, regs);
  55. !unwind_done(&state) && !unwind_error(&state); unwind_next_frame(&state)) {
  56. addr = unwind_get_return_address(&state);
  57. /*
  58. * A NULL or invalid return address probably means there's some
  59. * generated code which __kernel_text_address() doesn't know about.
  60. */
  61. if (!addr)
  62. return -EINVAL;
  63. if (!consume_entry(cookie, addr))
  64. return -EINVAL;
  65. }
  66. /* Check for stack corruption */
  67. if (unwind_error(&state))
  68. return -EINVAL;
  69. return 0;
  70. }
  71. static int
  72. copy_stack_frame(unsigned long fp, struct stack_frame *frame)
  73. {
  74. int ret = 1;
  75. unsigned long err;
  76. unsigned long __user *user_frame_tail;
  77. user_frame_tail = (unsigned long *)(fp - sizeof(struct stack_frame));
  78. if (!access_ok(user_frame_tail, sizeof(*frame)))
  79. return 0;
  80. pagefault_disable();
  81. err = (__copy_from_user_inatomic(frame, user_frame_tail, sizeof(*frame)));
  82. if (err || (unsigned long)user_frame_tail >= frame->fp)
  83. ret = 0;
  84. pagefault_enable();
  85. return ret;
  86. }
  87. void arch_stack_walk_user(stack_trace_consume_fn consume_entry, void *cookie,
  88. const struct pt_regs *regs)
  89. {
  90. unsigned long fp = regs->regs[22];
  91. while (fp && !((unsigned long)fp & 0xf)) {
  92. struct stack_frame frame;
  93. frame.fp = 0;
  94. frame.ra = 0;
  95. if (!copy_stack_frame(fp, &frame))
  96. break;
  97. if (!frame.ra)
  98. break;
  99. if (!consume_entry(cookie, frame.ra))
  100. break;
  101. fp = frame.fp;
  102. }
  103. }