perf_regs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include <linux/errno.h>
  2. #include <linux/kernel.h>
  3. #include <linux/sched.h>
  4. #include <linux/perf_event.h>
  5. #include <linux/bug.h>
  6. #include <linux/stddef.h>
  7. #include <asm/perf_regs.h>
  8. #include <asm/ptrace.h>
  9. #ifdef CONFIG_X86_32
  10. #define PERF_REG_X86_MAX PERF_REG_X86_32_MAX
  11. #else
  12. #define PERF_REG_X86_MAX PERF_REG_X86_64_MAX
  13. #endif
  14. #define PT_REGS_OFFSET(id, r) [id] = offsetof(struct pt_regs, r)
  15. static unsigned int pt_regs_offset[PERF_REG_X86_MAX] = {
  16. PT_REGS_OFFSET(PERF_REG_X86_AX, ax),
  17. PT_REGS_OFFSET(PERF_REG_X86_BX, bx),
  18. PT_REGS_OFFSET(PERF_REG_X86_CX, cx),
  19. PT_REGS_OFFSET(PERF_REG_X86_DX, dx),
  20. PT_REGS_OFFSET(PERF_REG_X86_SI, si),
  21. PT_REGS_OFFSET(PERF_REG_X86_DI, di),
  22. PT_REGS_OFFSET(PERF_REG_X86_BP, bp),
  23. PT_REGS_OFFSET(PERF_REG_X86_SP, sp),
  24. PT_REGS_OFFSET(PERF_REG_X86_IP, ip),
  25. PT_REGS_OFFSET(PERF_REG_X86_FLAGS, flags),
  26. PT_REGS_OFFSET(PERF_REG_X86_CS, cs),
  27. PT_REGS_OFFSET(PERF_REG_X86_SS, ss),
  28. #ifdef CONFIG_X86_32
  29. PT_REGS_OFFSET(PERF_REG_X86_DS, ds),
  30. PT_REGS_OFFSET(PERF_REG_X86_ES, es),
  31. PT_REGS_OFFSET(PERF_REG_X86_FS, fs),
  32. PT_REGS_OFFSET(PERF_REG_X86_GS, gs),
  33. #else
  34. /*
  35. * The pt_regs struct does not store
  36. * ds, es, fs, gs in 64 bit mode.
  37. */
  38. (unsigned int) -1,
  39. (unsigned int) -1,
  40. (unsigned int) -1,
  41. (unsigned int) -1,
  42. #endif
  43. #ifdef CONFIG_X86_64
  44. PT_REGS_OFFSET(PERF_REG_X86_R8, r8),
  45. PT_REGS_OFFSET(PERF_REG_X86_R9, r9),
  46. PT_REGS_OFFSET(PERF_REG_X86_R10, r10),
  47. PT_REGS_OFFSET(PERF_REG_X86_R11, r11),
  48. PT_REGS_OFFSET(PERF_REG_X86_R12, r12),
  49. PT_REGS_OFFSET(PERF_REG_X86_R13, r13),
  50. PT_REGS_OFFSET(PERF_REG_X86_R14, r14),
  51. PT_REGS_OFFSET(PERF_REG_X86_R15, r15),
  52. #endif
  53. };
  54. u64 perf_reg_value(struct pt_regs *regs, int idx)
  55. {
  56. if (WARN_ON_ONCE(idx > ARRAY_SIZE(pt_regs_offset)))
  57. return 0;
  58. return regs_get_register(regs, pt_regs_offset[idx]);
  59. }
  60. #define REG_RESERVED (~((1ULL << PERF_REG_X86_MAX) - 1ULL))
  61. #ifdef CONFIG_X86_32
  62. int perf_reg_validate(u64 mask)
  63. {
  64. if (!mask || mask & REG_RESERVED)
  65. return -EINVAL;
  66. return 0;
  67. }
  68. u64 perf_reg_abi(struct task_struct *task)
  69. {
  70. return PERF_SAMPLE_REGS_ABI_32;
  71. }
  72. #else /* CONFIG_X86_64 */
  73. #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \
  74. (1ULL << PERF_REG_X86_ES) | \
  75. (1ULL << PERF_REG_X86_FS) | \
  76. (1ULL << PERF_REG_X86_GS))
  77. int perf_reg_validate(u64 mask)
  78. {
  79. if (!mask || mask & REG_RESERVED)
  80. return -EINVAL;
  81. if (mask & REG_NOSUPPORT)
  82. return -EINVAL;
  83. return 0;
  84. }
  85. u64 perf_reg_abi(struct task_struct *task)
  86. {
  87. if (test_tsk_thread_flag(task, TIF_IA32))
  88. return PERF_SAMPLE_REGS_ABI_32;
  89. else
  90. return PERF_SAMPLE_REGS_ABI_64;
  91. }
  92. #endif /* CONFIG_X86_32 */