perf_callchain.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <[email protected]>
  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 frame_tail __user *tail;
  56. perf_callchain_store(entry, regs->ARM_pc);
  57. if (!current->mm)
  58. return;
  59. tail = (struct frame_tail __user *)regs->ARM_fp - 1;
  60. while ((entry->nr < entry->max_stack) &&
  61. tail && !((unsigned long)tail & 0x3))
  62. tail = user_backtrace(tail, entry);
  63. }
  64. /*
  65. * Gets called by walk_stackframe() for every stackframe. This will be called
  66. * whist unwinding the stackframe and is like a subroutine return so we use
  67. * the PC.
  68. */
  69. static int
  70. callchain_trace(struct stackframe *fr,
  71. void *data)
  72. {
  73. struct perf_callchain_entry_ctx *entry = data;
  74. perf_callchain_store(entry, fr->pc);
  75. return 0;
  76. }
  77. void
  78. perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
  79. {
  80. struct stackframe fr;
  81. arm_get_current_stackframe(regs, &fr);
  82. walk_stackframe(&fr, callchain_trace, entry);
  83. }
  84. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  85. {
  86. return instruction_pointer(regs);
  87. }
  88. unsigned long perf_misc_flags(struct pt_regs *regs)
  89. {
  90. int misc = 0;
  91. if (user_mode(regs))
  92. misc |= PERF_RECORD_MISC_USER;
  93. else
  94. misc |= PERF_RECORD_MISC_KERNEL;
  95. return misc;
  96. }