backtrace.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/oprofile.h>
  3. #include <linux/sched.h>
  4. #include <linux/mm.h>
  5. #include <linux/uaccess.h>
  6. #include <asm/ptrace.h>
  7. #include <asm/stacktrace.h>
  8. #include <linux/stacktrace.h>
  9. #include <linux/kernel.h>
  10. #include <asm/sections.h>
  11. #include <asm/inst.h>
  12. struct stackframe {
  13. unsigned long sp;
  14. unsigned long pc;
  15. unsigned long ra;
  16. };
  17. static inline int get_mem(unsigned long addr, unsigned long *result)
  18. {
  19. unsigned long *address = (unsigned long *) addr;
  20. if (!access_ok(address, sizeof(unsigned long)))
  21. return -1;
  22. if (__copy_from_user_inatomic(result, address, sizeof(unsigned long)))
  23. return -3;
  24. return 0;
  25. }
  26. /*
  27. * These two instruction helpers were taken from process.c
  28. */
  29. static inline int is_ra_save_ins(union mips_instruction *ip)
  30. {
  31. /* sw / sd $ra, offset($sp) */
  32. return (ip->i_format.opcode == sw_op || ip->i_format.opcode == sd_op)
  33. && ip->i_format.rs == 29 && ip->i_format.rt == 31;
  34. }
  35. static inline int is_sp_move_ins(union mips_instruction *ip)
  36. {
  37. /* addiu/daddiu sp,sp,-imm */
  38. if (ip->i_format.rs != 29 || ip->i_format.rt != 29)
  39. return 0;
  40. if (ip->i_format.opcode == addiu_op || ip->i_format.opcode == daddiu_op)
  41. return 1;
  42. return 0;
  43. }
  44. /*
  45. * Looks for specific instructions that mark the end of a function.
  46. * This usually means we ran into the code area of the previous function.
  47. */
  48. static inline int is_end_of_function_marker(union mips_instruction *ip)
  49. {
  50. /* jr ra */
  51. if (ip->r_format.func == jr_op && ip->r_format.rs == 31)
  52. return 1;
  53. /* lui gp */
  54. if (ip->i_format.opcode == lui_op && ip->i_format.rt == 28)
  55. return 1;
  56. return 0;
  57. }
  58. /*
  59. * TODO for userspace stack unwinding:
  60. * - handle cases where the stack is adjusted inside a function
  61. * (generally doesn't happen)
  62. * - find optimal value for max_instr_check
  63. * - try to find a better way to handle leaf functions
  64. */
  65. static inline int unwind_user_frame(struct stackframe *old_frame,
  66. const unsigned int max_instr_check)
  67. {
  68. struct stackframe new_frame = *old_frame;
  69. off_t ra_offset = 0;
  70. size_t stack_size = 0;
  71. unsigned long addr;
  72. if (old_frame->pc == 0 || old_frame->sp == 0 || old_frame->ra == 0)
  73. return -9;
  74. for (addr = new_frame.pc; (addr + max_instr_check > new_frame.pc)
  75. && (!ra_offset || !stack_size); --addr) {
  76. union mips_instruction ip;
  77. if (get_mem(addr, (unsigned long *) &ip))
  78. return -11;
  79. if (is_sp_move_ins(&ip)) {
  80. int stack_adjustment = ip.i_format.simmediate;
  81. if (stack_adjustment > 0)
  82. /* This marks the end of the previous function,
  83. which means we overran. */
  84. break;
  85. stack_size = (unsigned long) stack_adjustment;
  86. } else if (is_ra_save_ins(&ip)) {
  87. int ra_slot = ip.i_format.simmediate;
  88. if (ra_slot < 0)
  89. /* This shouldn't happen. */
  90. break;
  91. ra_offset = ra_slot;
  92. } else if (is_end_of_function_marker(&ip))
  93. break;
  94. }
  95. if (!ra_offset || !stack_size)
  96. goto done;
  97. if (ra_offset) {
  98. new_frame.ra = old_frame->sp + ra_offset;
  99. if (get_mem(new_frame.ra, &(new_frame.ra)))
  100. return -13;
  101. }
  102. if (stack_size) {
  103. new_frame.sp = old_frame->sp + stack_size;
  104. if (get_mem(new_frame.sp, &(new_frame.sp)))
  105. return -14;
  106. }
  107. if (new_frame.sp > old_frame->sp)
  108. return -2;
  109. done:
  110. new_frame.pc = old_frame->ra;
  111. *old_frame = new_frame;
  112. return 0;
  113. }
  114. static inline void do_user_backtrace(unsigned long low_addr,
  115. struct stackframe *frame,
  116. unsigned int depth)
  117. {
  118. const unsigned int max_instr_check = 512;
  119. const unsigned long high_addr = low_addr + THREAD_SIZE;
  120. while (depth-- && !unwind_user_frame(frame, max_instr_check)) {
  121. oprofile_add_trace(frame->ra);
  122. if (frame->sp < low_addr || frame->sp > high_addr)
  123. break;
  124. }
  125. }
  126. #ifndef CONFIG_KALLSYMS
  127. static inline void do_kernel_backtrace(unsigned long low_addr,
  128. struct stackframe *frame,
  129. unsigned int depth) { }
  130. #else
  131. static inline void do_kernel_backtrace(unsigned long low_addr,
  132. struct stackframe *frame,
  133. unsigned int depth)
  134. {
  135. while (depth-- && frame->pc) {
  136. frame->pc = unwind_stack_by_address(low_addr,
  137. &(frame->sp),
  138. frame->pc,
  139. &(frame->ra));
  140. oprofile_add_trace(frame->ra);
  141. }
  142. }
  143. #endif
  144. void notrace op_mips_backtrace(struct pt_regs *const regs, unsigned int depth)
  145. {
  146. struct stackframe frame = { .sp = regs->regs[29],
  147. .pc = regs->cp0_epc,
  148. .ra = regs->regs[31] };
  149. const int userspace = user_mode(regs);
  150. const unsigned long low_addr = ALIGN(frame.sp, THREAD_SIZE);
  151. if (userspace)
  152. do_user_backtrace(low_addr, &frame, depth);
  153. else
  154. do_kernel_backtrace(low_addr, &frame, depth);
  155. }