perf_callchain.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2019 Hangzhou C-SKY Microsystems co.,ltd. */
  3. #include <linux/perf_event.h>
  4. #include <linux/uaccess.h>
  5. /* Kernel callchain */
  6. struct stackframe {
  7. unsigned long fp;
  8. unsigned long ra;
  9. };
  10. /*
  11. * Get the return address for a single stackframe and return a pointer to the
  12. * next frame tail.
  13. */
  14. static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,
  15. unsigned long fp, unsigned long reg_ra)
  16. {
  17. struct stackframe buftail;
  18. unsigned long ra = 0;
  19. unsigned long __user *user_frame_tail =
  20. (unsigned long __user *)(fp - sizeof(struct stackframe));
  21. /* Check accessibility of one struct frame_tail beyond */
  22. if (!access_ok(user_frame_tail, sizeof(buftail)))
  23. return 0;
  24. if (__copy_from_user_inatomic(&buftail, user_frame_tail,
  25. sizeof(buftail)))
  26. return 0;
  27. if (reg_ra != 0)
  28. ra = reg_ra;
  29. else
  30. ra = buftail.ra;
  31. fp = buftail.fp;
  32. if (ra != 0)
  33. perf_callchain_store(entry, ra);
  34. else
  35. return 0;
  36. return fp;
  37. }
  38. /*
  39. * This will be called when the target is in user mode
  40. * This function will only be called when we use
  41. * "PERF_SAMPLE_CALLCHAIN" in
  42. * kernel/events/core.c:perf_prepare_sample()
  43. *
  44. * How to trigger perf_callchain_[user/kernel] :
  45. * $ perf record -e cpu-clock --call-graph fp ./program
  46. * $ perf report --call-graph
  47. *
  48. * On RISC-V platform, the program being sampled and the C library
  49. * need to be compiled with -fno-omit-frame-pointer, otherwise
  50. * the user stack will not contain function frame.
  51. */
  52. void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
  53. struct pt_regs *regs)
  54. {
  55. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  56. unsigned long fp = 0;
  57. /* RISC-V does not support perf in guest mode. */
  58. if (guest_cbs && guest_cbs->is_in_guest())
  59. return;
  60. fp = regs->s0;
  61. perf_callchain_store(entry, regs->epc);
  62. fp = user_backtrace(entry, fp, regs->ra);
  63. while (fp && !(fp & 0x3) && entry->nr < entry->max_stack)
  64. fp = user_backtrace(entry, fp, 0);
  65. }
  66. bool fill_callchain(unsigned long pc, unsigned long regs, void *entry)
  67. {
  68. return perf_callchain_store(entry, pc) == 0;
  69. }
  70. void notrace walk_stackframe(struct task_struct *task,
  71. struct pt_regs *regs, bool (*fn)(unsigned long, unsigned long, void *), void *arg);
  72. void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
  73. struct pt_regs *regs)
  74. {
  75. struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
  76. /* RISC-V does not support perf in guest mode. */
  77. if (guest_cbs && guest_cbs->is_in_guest()) {
  78. pr_warn("RISC-V does not support perf in guest mode!");
  79. return;
  80. }
  81. walk_stackframe(NULL, regs, fill_callchain, entry);
  82. }