backtrace.c 4.4 KB

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