perf_callchain.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ARM callchain support
  4. *
  5. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  6. * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
  7. *
  8. * This code is based on the ARM OProfile backtrace code.
  9. */
  10. #include <linux/perf_event.h>
  11. #include <linux/uaccess.h>
  12. #include <asm/stacktrace.h>
  13. /*
  14. * The registers we're interested in are at the end of the variable
  15. * length saved register structure. The fp points at the end of this
  16. * structure so the address of this struct is:
  17. * (struct frame_tail *)(xxx->fp)-1
  18. *
  19. * This code has been adapted from the ARM OProfile support.
  20. */
  21. struct frame_tail {
  22. struct frame_tail __user *fp;
  23. unsigned long sp;
  24. unsigned long lr;
  25. } __attribute__((packed));
  26. /*
  27. * Get the return address for a single stackframe and return a pointer to the
  28. * next frame tail.
  29. */
  30. static struct frame_tail __user *
  31. user_backtrace(struct frame_tail __user *tail,
  32. struct perf_callchain_entry_ctx *entry)
  33. {
  34. struct frame_tail buftail;
  35. unsigned long err;
  36. if (!access_ok(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 + 1 >= buftail.fp)
  49. return NULL;
  50. return buftail.fp - 1;
  51. }
  52. void
  53. perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  54. {
  55. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  56. struct frame_tail __user *tail;
  57. if (guest_cbs && guest_cbs->is_in_guest()) {
  58. /* We don't support guest os callchain now */
  59. return;
  60. }
  61. perf_callchain_store(entry, regs->ARM_pc);
  62. if (!current->mm)
  63. return;
  64. tail = (struct frame_tail __user *)regs->ARM_fp - 1;
  65. while ((entry->nr < entry->max_stack) &&
  66. tail && !((unsigned long)tail & 0x3))
  67. tail = user_backtrace(tail, entry);
  68. }
  69. /*
  70. * Gets called by walk_stackframe() for every stackframe. This will be called
  71. * whist unwinding the stackframe and is like a subroutine return so we use
  72. * the PC.
  73. */
  74. static int
  75. callchain_trace(struct stackframe *fr,
  76. void *data)
  77. {
  78. struct perf_callchain_entry_ctx *entry = data;
  79. perf_callchain_store(entry, fr->pc);
  80. return 0;
  81. }
  82. void
  83. perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  84. {
  85. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  86. struct stackframe fr;
  87. if (guest_cbs && guest_cbs->is_in_guest()) {
  88. /* We don't support guest os callchain now */
  89. return;
  90. }
  91. arm_get_current_stackframe(regs, &fr);
  92. walk_stackframe(&fr, callchain_trace, entry);
  93. }
  94. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  95. {
  96. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  97. if (guest_cbs && guest_cbs->is_in_guest())
  98. return guest_cbs->get_guest_ip();
  99. return instruction_pointer(regs);
  100. }
  101. unsigned long perf_misc_flags(struct pt_regs *regs)
  102. {
  103. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  104. int misc = 0;
  105. if (guest_cbs && guest_cbs->is_in_guest()) {
  106. if (guest_cbs->is_user_mode())
  107. misc |= PERF_RECORD_MISC_GUEST_USER;
  108. else
  109. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  110. } else {
  111. if (user_mode(regs))
  112. misc |= PERF_RECORD_MISC_USER;
  113. else
  114. misc |= PERF_RECORD_MISC_KERNEL;
  115. }
  116. return misc;
  117. }