perf_callchain.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * arm64 callchain support
  3. *
  4. * Copyright (C) 2015 ARM Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/perf_event.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/stacktrace.h>
  21. struct frame_tail {
  22. struct frame_tail __user *fp;
  23. unsigned long lr;
  24. } __attribute__((packed));
  25. /*
  26. * Get the return address for a single stackframe and return a pointer to the
  27. * next frame tail.
  28. */
  29. static struct frame_tail __user *
  30. user_backtrace(struct frame_tail __user *tail,
  31. struct perf_callchain_entry *entry)
  32. {
  33. struct frame_tail buftail;
  34. unsigned long err;
  35. /* Also check accessibility of one struct frame_tail beyond */
  36. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  37. return NULL;
  38. pagefault_disable();
  39. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  40. pagefault_enable();
  41. if (err)
  42. return NULL;
  43. perf_callchain_store(entry, buftail.lr);
  44. /*
  45. * Frame pointers should strictly progress back up the stack
  46. * (towards higher addresses).
  47. */
  48. if (tail >= buftail.fp)
  49. return NULL;
  50. return buftail.fp;
  51. }
  52. #ifdef CONFIG_COMPAT
  53. /*
  54. * The registers we're interested in are at the end of the variable
  55. * length saved register structure. The fp points at the end of this
  56. * structure so the address of this struct is:
  57. * (struct compat_frame_tail *)(xxx->fp)-1
  58. *
  59. * This code has been adapted from the ARM OProfile support.
  60. */
  61. struct compat_frame_tail {
  62. compat_uptr_t fp; /* a (struct compat_frame_tail *) in compat mode */
  63. u32 sp;
  64. u32 lr;
  65. } __attribute__((packed));
  66. static struct compat_frame_tail __user *
  67. compat_user_backtrace(struct compat_frame_tail __user *tail,
  68. struct perf_callchain_entry *entry)
  69. {
  70. struct compat_frame_tail buftail;
  71. unsigned long err;
  72. /* Also check accessibility of one struct frame_tail beyond */
  73. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  74. return NULL;
  75. pagefault_disable();
  76. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  77. pagefault_enable();
  78. if (err)
  79. return NULL;
  80. perf_callchain_store(entry, buftail.lr);
  81. /*
  82. * Frame pointers should strictly progress back up the stack
  83. * (towards higher addresses).
  84. */
  85. if (tail + 1 >= (struct compat_frame_tail __user *)
  86. compat_ptr(buftail.fp))
  87. return NULL;
  88. return (struct compat_frame_tail __user *)compat_ptr(buftail.fp) - 1;
  89. }
  90. #endif /* CONFIG_COMPAT */
  91. void perf_callchain_user(struct perf_callchain_entry *entry,
  92. struct pt_regs *regs)
  93. {
  94. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  95. /* We don't support guest os callchain now */
  96. return;
  97. }
  98. perf_callchain_store(entry, regs->pc);
  99. if (!compat_user_mode(regs)) {
  100. /* AARCH64 mode */
  101. struct frame_tail __user *tail;
  102. tail = (struct frame_tail __user *)regs->regs[29];
  103. while (entry->nr < PERF_MAX_STACK_DEPTH &&
  104. tail && !((unsigned long)tail & 0xf))
  105. tail = user_backtrace(tail, entry);
  106. } else {
  107. #ifdef CONFIG_COMPAT
  108. /* AARCH32 compat mode */
  109. struct compat_frame_tail __user *tail;
  110. tail = (struct compat_frame_tail __user *)regs->compat_fp - 1;
  111. while ((entry->nr < PERF_MAX_STACK_DEPTH) &&
  112. tail && !((unsigned long)tail & 0x3))
  113. tail = compat_user_backtrace(tail, entry);
  114. #endif
  115. }
  116. }
  117. /*
  118. * Gets called by walk_stackframe() for every stackframe. This will be called
  119. * whist unwinding the stackframe and is like a subroutine return so we use
  120. * the PC.
  121. */
  122. static int callchain_trace(struct stackframe *frame, void *data)
  123. {
  124. struct perf_callchain_entry *entry = data;
  125. perf_callchain_store(entry, frame->pc);
  126. return 0;
  127. }
  128. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  129. struct pt_regs *regs)
  130. {
  131. struct stackframe frame;
  132. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  133. /* We don't support guest os callchain now */
  134. return;
  135. }
  136. frame.fp = regs->regs[29];
  137. frame.sp = regs->sp;
  138. frame.pc = regs->pc;
  139. walk_stackframe(&frame, callchain_trace, entry);
  140. }
  141. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  142. {
  143. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  144. return perf_guest_cbs->get_guest_ip();
  145. return instruction_pointer(regs);
  146. }
  147. unsigned long perf_misc_flags(struct pt_regs *regs)
  148. {
  149. int misc = 0;
  150. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  151. if (perf_guest_cbs->is_user_mode())
  152. misc |= PERF_RECORD_MISC_GUEST_USER;
  153. else
  154. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  155. } else {
  156. if (user_mode(regs))
  157. misc |= PERF_RECORD_MISC_USER;
  158. else
  159. misc |= PERF_RECORD_MISC_KERNEL;
  160. }
  161. return misc;
  162. }