common.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /*
  24. * OProfile has a curious naming scheme for the ARM PMUs, but they are
  25. * part of the user ABI so we need to map from the perf PMU name for
  26. * supported PMUs.
  27. */
  28. static struct op_perf_name {
  29. char *perf_name;
  30. char *op_name;
  31. } op_perf_name_map[] = {
  32. { "armv5_xscale1", "arm/xscale1" },
  33. { "armv5_xscale2", "arm/xscale2" },
  34. { "armv6_1136", "arm/armv6" },
  35. { "armv6_1156", "arm/armv6" },
  36. { "armv6_1176", "arm/armv6" },
  37. { "armv6_11mpcore", "arm/mpcore" },
  38. { "armv7_cortex_a8", "arm/armv7" },
  39. { "armv7_cortex_a9", "arm/armv7-ca9" },
  40. };
  41. char *op_name_from_perf_id(void)
  42. {
  43. int i;
  44. struct op_perf_name names;
  45. const char *perf_name = perf_pmu_name();
  46. for (i = 0; i < ARRAY_SIZE(op_perf_name_map); ++i) {
  47. names = op_perf_name_map[i];
  48. if (!strcmp(names.perf_name, perf_name))
  49. return names.op_name;
  50. }
  51. return NULL;
  52. }
  53. #endif
  54. static int report_trace(struct stackframe *frame, void *d)
  55. {
  56. unsigned int *depth = d;
  57. if (*depth) {
  58. oprofile_add_trace(frame->pc);
  59. (*depth)--;
  60. }
  61. return *depth == 0;
  62. }
  63. /*
  64. * The registers we're interested in are at the end of the variable
  65. * length saved register structure. The fp points at the end of this
  66. * structure so the address of this struct is:
  67. * (struct frame_tail *)(xxx->fp)-1
  68. */
  69. struct frame_tail {
  70. struct frame_tail *fp;
  71. unsigned long sp;
  72. unsigned long lr;
  73. } __attribute__((packed));
  74. static struct frame_tail* user_backtrace(struct frame_tail *tail)
  75. {
  76. struct frame_tail buftail[2];
  77. /* Also check accessibility of one struct frame_tail beyond */
  78. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  79. return NULL;
  80. if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail)))
  81. return NULL;
  82. oprofile_add_trace(buftail[0].lr);
  83. /* frame pointers should strictly progress back up the stack
  84. * (towards higher addresses) */
  85. if (tail + 1 >= buftail[0].fp)
  86. return NULL;
  87. return buftail[0].fp-1;
  88. }
  89. static void arm_backtrace(struct pt_regs * const regs, unsigned int depth)
  90. {
  91. struct frame_tail *tail = ((struct frame_tail *) regs->ARM_fp) - 1;
  92. if (!user_mode(regs)) {
  93. struct stackframe frame;
  94. arm_get_current_stackframe(regs, &frame);
  95. walk_stackframe(&frame, report_trace, &depth);
  96. return;
  97. }
  98. while (depth-- && tail && !((unsigned long) tail & 3))
  99. tail = user_backtrace(tail);
  100. }
  101. int __init oprofile_arch_init(struct oprofile_operations *ops)
  102. {
  103. /* provide backtrace support also in timer mode: */
  104. ops->backtrace = arm_backtrace;
  105. return oprofile_perf_init(ops);
  106. }
  107. void oprofile_arch_exit(void)
  108. {
  109. oprofile_perf_exit();
  110. }