perf_regs.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. void perf_get_regs_user(struct perf_regs *regs_user,
  73. struct pt_regs *regs,
  74. struct pt_regs *regs_user_copy)
  75. {
  76. regs_user->regs = task_pt_regs(current);
  77. regs_user->abi = perf_reg_abi(current);
  78. }
  79. #else /* CONFIG_X86_64 */
  80. #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \
  81. (1ULL << PERF_REG_X86_ES) | \
  82. (1ULL << PERF_REG_X86_FS) | \
  83. (1ULL << PERF_REG_X86_GS))
  84. int perf_reg_validate(u64 mask)
  85. {
  86. if (!mask || mask & REG_RESERVED)
  87. return -EINVAL;
  88. if (mask & REG_NOSUPPORT)
  89. return -EINVAL;
  90. return 0;
  91. }
  92. u64 perf_reg_abi(struct task_struct *task)
  93. {
  94. if (test_tsk_thread_flag(task, TIF_IA32))
  95. return PERF_SAMPLE_REGS_ABI_32;
  96. else
  97. return PERF_SAMPLE_REGS_ABI_64;
  98. }
  99. void perf_get_regs_user(struct perf_regs *regs_user,
  100. struct pt_regs *regs,
  101. struct pt_regs *regs_user_copy)
  102. {
  103. struct pt_regs *user_regs = task_pt_regs(current);
  104. /*
  105. * If we're in an NMI that interrupted task_pt_regs setup, then
  106. * we can't sample user regs at all. This check isn't really
  107. * sufficient, though, as we could be in an NMI inside an interrupt
  108. * that happened during task_pt_regs setup.
  109. */
  110. if (regs->sp > (unsigned long)&user_regs->r11 &&
  111. regs->sp <= (unsigned long)(user_regs + 1)) {
  112. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  113. regs_user->regs = NULL;
  114. return;
  115. }
  116. /*
  117. * These registers are always saved on 64-bit syscall entry.
  118. * On 32-bit entry points, they are saved too except r8..r11.
  119. */
  120. regs_user_copy->ip = user_regs->ip;
  121. regs_user_copy->ax = user_regs->ax;
  122. regs_user_copy->cx = user_regs->cx;
  123. regs_user_copy->dx = user_regs->dx;
  124. regs_user_copy->si = user_regs->si;
  125. regs_user_copy->di = user_regs->di;
  126. regs_user_copy->r8 = user_regs->r8;
  127. regs_user_copy->r9 = user_regs->r9;
  128. regs_user_copy->r10 = user_regs->r10;
  129. regs_user_copy->r11 = user_regs->r11;
  130. regs_user_copy->orig_ax = user_regs->orig_ax;
  131. regs_user_copy->flags = user_regs->flags;
  132. regs_user_copy->sp = user_regs->sp;
  133. regs_user_copy->cs = user_regs->cs;
  134. regs_user_copy->ss = user_regs->ss;
  135. /*
  136. * Most system calls don't save these registers, don't report them.
  137. */
  138. regs_user_copy->bx = -1;
  139. regs_user_copy->bp = -1;
  140. regs_user_copy->r12 = -1;
  141. regs_user_copy->r13 = -1;
  142. regs_user_copy->r14 = -1;
  143. regs_user_copy->r15 = -1;
  144. /*
  145. * For this to be at all useful, we need a reasonable guess for
  146. * the ABI. Be careful: we're in NMI context, and we're
  147. * considering current to be the current task, so we should
  148. * be careful not to look at any other percpu variables that might
  149. * change during context switches.
  150. */
  151. regs_user->abi = user_64bit_mode(user_regs) ?
  152. PERF_SAMPLE_REGS_ABI_64 : PERF_SAMPLE_REGS_ABI_32;
  153. regs_user->regs = regs_user_copy;
  154. }
  155. #endif /* CONFIG_X86_32 */