common.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * @file common.c
  3. *
  4. * @remark Copyright 2004 Oprofile Authors
  5. * @remark Copyright 2010 ARM Ltd.
  6. * @remark Read the file COPYING
  7. *
  8. * @author Zwane Mwaikambo
  9. * @author Will Deacon [move to perf]
  10. */
  11. #include <linux/cpumask.h>
  12. #include <linux/init.h>
  13. #include <linux/mutex.h>
  14. #include <linux/oprofile.h>
  15. #include <linux/perf_event.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/slab.h>
  18. #include <asm/stacktrace.h>
  19. #include <linux/uaccess.h>
  20. #include <asm/perf_event.h>
  21. #include <asm/ptrace.h>
  22. #ifdef CONFIG_HW_PERF_EVENTS
  23. char *op_name_from_perf_id(void)
  24. {
  25. enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
  26. switch (id) {
  27. case ARM_PERF_PMU_ID_XSCALE1:
  28. return "arm/xscale1";
  29. case ARM_PERF_PMU_ID_XSCALE2:
  30. return "arm/xscale2";
  31. case ARM_PERF_PMU_ID_V6:
  32. return "arm/armv6";
  33. case ARM_PERF_PMU_ID_V6MP:
  34. return "arm/mpcore";
  35. case ARM_PERF_PMU_ID_CA8:
  36. return "arm/armv7";
  37. case ARM_PERF_PMU_ID_CA9:
  38. return "arm/armv7-ca9";
  39. default:
  40. return NULL;
  41. }
  42. }
  43. #endif
  44. static int report_trace(struct stackframe *frame, void *d)
  45. {
  46. unsigned int *depth = d;
  47. if (*depth) {
  48. oprofile_add_trace(frame->pc);
  49. (*depth)--;
  50. }
  51. return *depth == 0;
  52. }
  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 frame_tail *)(xxx->fp)-1
  58. */
  59. struct frame_tail {
  60. struct frame_tail *fp;
  61. unsigned long sp;
  62. unsigned long lr;
  63. } __attribute__((packed));
  64. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  65. {
  66. struct frame_tail buftail[2];
  67. /* Also check accessibility of one struct frame_tail beyond */
  68. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  69. return NULL;
  70. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  71. return NULL;
  72. oprofile_add_trace(buftail[0].lr);
  73. /* frame pointers should strictly progress back up the stack
  74. * (towards higher addresses) */
  75. if (tail + 1 >= buftail[0].fp)
  76. return NULL;
  77. return buftail[0].fp-1;
  78. }
  79. static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  80. {
  81. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  82. if (!user_mode(regs)) {
  83. struct stackframe frame;
  84. frame.fp = regs->ARM_fp;
  85. frame.sp = regs->ARM_sp;
  86. frame.lr = regs->ARM_lr;
  87. frame.pc = regs->ARM_pc;
  88. walk_stackframe(&frame, report_trace, &depth);
  89. return;
  90. }
  91. while (depth-- && tail && !((unsigned long) tail & 3))
  92. tail = user_backtrace(tail);
  93. }
  94. int __init oprofile_arch_init(struct oprofile_operations *ops)
  95. {
  96. /* provide backtrace support also in timer mode: */
  97. ops->backtrace = arm_backtrace;
  98. return oprofile_perf_init(ops);
  99. }
  100. void __exit oprofile_arch_exit(void)
  101. {
  102. oprofile_perf_exit();
  103. }