common.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/mutex.h>
  16. #include <linux/oprofile.h>
  17. #include <linux/perf_event.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <asm/stacktrace.h>
  21. #include <linux/uaccess.h>
  22. #include <asm/perf_event.h>
  23. #include <asm/ptrace.h>
  24. #ifdef CONFIG_HW_PERF_EVENTS
  25. char *op_name_from_perf_id(void)
  26. {
  27. enum arm_perf_pmu_ids id = armpmu_get_pmu_id();
  28. switch (id) {
  29. case ARM_PERF_PMU_ID_XSCALE1:
  30. return "arm/xscale1";
  31. case ARM_PERF_PMU_ID_XSCALE2:
  32. return "arm/xscale2";
  33. case ARM_PERF_PMU_ID_V6:
  34. return "arm/armv6";
  35. case ARM_PERF_PMU_ID_V6MP:
  36. return "arm/mpcore";
  37. case ARM_PERF_PMU_ID_CA8:
  38. return "arm/armv7";
  39. case ARM_PERF_PMU_ID_CA9:
  40. return "arm/armv7-ca9";
  41. default:
  42. return NULL;
  43. }
  44. }
  45. static int report_trace(struct stackframe *frame, void *d)
  46. {
  47. unsigned int *depth = d;
  48. if (*depth) {
  49. oprofile_add_trace(frame->pc);
  50. (*depth)--;
  51. }
  52. return *depth == 0;
  53. }
  54. /*
  55. * The registers we're interested in are at the end of the variable
  56. * length saved register structure. The fp points at the end of this
  57. * structure so the address of this struct is:
  58. * (struct frame_tail *)(xxx->fp)-1
  59. */
  60. struct frame_tail {
  61. struct frame_tail *fp;
  62. unsigned long sp;
  63. unsigned long lr;
  64. } __attribute__((packed));
  65. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  66. {
  67. struct frame_tail buftail[2];
  68. /* Also check accessibility of one struct frame_tail beyond */
  69. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  70. return NULL;
  71. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  72. return NULL;
  73. oprofile_add_trace(buftail[0].lr);
  74. /* frame pointers should strictly progress back up the stack
  75. * (towards higher addresses) */
  76. if (tail >= buftail[0].fp)
  77. return NULL;
  78. return buftail[0].fp-1;
  79. }
  80. static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  81. {
  82. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  83. if (!user_mode(regs)) {
  84. struct stackframe frame;
  85. frame.fp = regs->ARM_fp;
  86. frame.sp = regs->ARM_sp;
  87. frame.lr = regs->ARM_lr;
  88. frame.pc = regs->ARM_pc;
  89. walk_stackframe(&frame, report_trace, &depth);
  90. return;
  91. }
  92. while (depth-- && tail && !((unsigned long) tail & 3))
  93. tail = user_backtrace(tail);
  94. }
  95. int __init oprofile_arch_init(struct oprofile_operations *ops)
  96. {
  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. }
  104. #else
  105. int __init oprofile_arch_init(struct oprofile_operations *ops)
  106. {
  107. pr_info("oprofile: hardware counters not available\n");
  108. return -ENODEV;
  109. }
  110. void __exit oprofile_arch_exit(void) {}
  111. #endif /* CONFIG_HW_PERF_EVENTS */