perf_regs.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/errno.h>
  3. #include <linux/kernel.h>
  4. #include <linux/sched.h>
  5. #include <linux/sched/task_stack.h>
  6. #include <linux/perf_event.h>
  7. #include <linux/bug.h>
  8. #include <linux/stddef.h>
  9. #include <asm/perf_regs.h>
  10. #include <asm/ptrace.h>
  11. #ifdef CONFIG_X86_32
  12. #define PERF_REG_X86_MAX PERF_REG_X86_32_MAX
  13. #else
  14. #define PERF_REG_X86_MAX PERF_REG_X86_64_MAX
  15. #endif
  16. #define PT_REGS_OFFSET(id, r) [id] = offsetof(struct pt_regs, r)
  17. static unsigned int pt_regs_offset[PERF_REG_X86_MAX] = {
  18. PT_REGS_OFFSET(PERF_REG_X86_AX, ax),
  19. PT_REGS_OFFSET(PERF_REG_X86_BX, bx),
  20. PT_REGS_OFFSET(PERF_REG_X86_CX, cx),
  21. PT_REGS_OFFSET(PERF_REG_X86_DX, dx),
  22. PT_REGS_OFFSET(PERF_REG_X86_SI, si),
  23. PT_REGS_OFFSET(PERF_REG_X86_DI, di),
  24. PT_REGS_OFFSET(PERF_REG_X86_BP, bp),
  25. PT_REGS_OFFSET(PERF_REG_X86_SP, sp),
  26. PT_REGS_OFFSET(PERF_REG_X86_IP, ip),
  27. PT_REGS_OFFSET(PERF_REG_X86_FLAGS, flags),
  28. PT_REGS_OFFSET(PERF_REG_X86_CS, cs),
  29. PT_REGS_OFFSET(PERF_REG_X86_SS, ss),
  30. #ifdef CONFIG_X86_32
  31. PT_REGS_OFFSET(PERF_REG_X86_DS, ds),
  32. PT_REGS_OFFSET(PERF_REG_X86_ES, es),
  33. PT_REGS_OFFSET(PERF_REG_X86_FS, fs),
  34. PT_REGS_OFFSET(PERF_REG_X86_GS, gs),
  35. #else
  36. /*
  37. * The pt_regs struct does not store
  38. * ds, es, fs, gs in 64 bit mode.
  39. */
  40. (unsigned int) -1,
  41. (unsigned int) -1,
  42. (unsigned int) -1,
  43. (unsigned int) -1,
  44. #endif
  45. #ifdef CONFIG_X86_64
  46. PT_REGS_OFFSET(PERF_REG_X86_R8, r8),
  47. PT_REGS_OFFSET(PERF_REG_X86_R9, r9),
  48. PT_REGS_OFFSET(PERF_REG_X86_R10, r10),
  49. PT_REGS_OFFSET(PERF_REG_X86_R11, r11),
  50. PT_REGS_OFFSET(PERF_REG_X86_R12, r12),
  51. PT_REGS_OFFSET(PERF_REG_X86_R13, r13),
  52. PT_REGS_OFFSET(PERF_REG_X86_R14, r14),
  53. PT_REGS_OFFSET(PERF_REG_X86_R15, r15),
  54. #endif
  55. };
  56. u64 perf_reg_value(struct pt_regs *regs, int idx)
  57. {
  58. struct x86_perf_regs *perf_regs;
  59. if (idx >= PERF_REG_X86_XMM0 && idx < PERF_REG_X86_XMM_MAX) {
  60. perf_regs = container_of(regs, struct x86_perf_regs, regs);
  61. if (!perf_regs->xmm_regs)
  62. return 0;
  63. return perf_regs->xmm_regs[idx - PERF_REG_X86_XMM0];
  64. }
  65. if (WARN_ON_ONCE(idx >= ARRAY_SIZE(pt_regs_offset)))
  66. return 0;
  67. return regs_get_register(regs, pt_regs_offset[idx]);
  68. }
  69. #define PERF_REG_X86_RESERVED (((1ULL << PERF_REG_X86_XMM0) - 1) & \
  70. ~((1ULL << PERF_REG_X86_MAX) - 1))
  71. #ifdef CONFIG_X86_32
  72. #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_R8) | \
  73. (1ULL << PERF_REG_X86_R9) | \
  74. (1ULL << PERF_REG_X86_R10) | \
  75. (1ULL << PERF_REG_X86_R11) | \
  76. (1ULL << PERF_REG_X86_R12) | \
  77. (1ULL << PERF_REG_X86_R13) | \
  78. (1ULL << PERF_REG_X86_R14) | \
  79. (1ULL << PERF_REG_X86_R15))
  80. int perf_reg_validate(u64 mask)
  81. {
  82. if (!mask || (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)))
  83. return -EINVAL;
  84. return 0;
  85. }
  86. u64 perf_reg_abi(struct task_struct *task)
  87. {
  88. return PERF_SAMPLE_REGS_ABI_32;
  89. }
  90. void perf_get_regs_user(struct perf_regs *regs_user,
  91. struct pt_regs *regs)
  92. {
  93. regs_user->regs = task_pt_regs(current);
  94. regs_user->abi = perf_reg_abi(current);
  95. }
  96. #else /* CONFIG_X86_64 */
  97. #define REG_NOSUPPORT ((1ULL << PERF_REG_X86_DS) | \
  98. (1ULL << PERF_REG_X86_ES) | \
  99. (1ULL << PERF_REG_X86_FS) | \
  100. (1ULL << PERF_REG_X86_GS))
  101. int perf_reg_validate(u64 mask)
  102. {
  103. if (!mask || (mask & (REG_NOSUPPORT | PERF_REG_X86_RESERVED)))
  104. return -EINVAL;
  105. return 0;
  106. }
  107. u64 perf_reg_abi(struct task_struct *task)
  108. {
  109. if (test_tsk_thread_flag(task, TIF_IA32))
  110. return PERF_SAMPLE_REGS_ABI_32;
  111. else
  112. return PERF_SAMPLE_REGS_ABI_64;
  113. }
  114. static DEFINE_PER_CPU(struct pt_regs, nmi_user_regs);
  115. void perf_get_regs_user(struct perf_regs *regs_user,
  116. struct pt_regs *regs)
  117. {
  118. struct pt_regs *regs_user_copy = this_cpu_ptr(&nmi_user_regs);
  119. struct pt_regs *user_regs = task_pt_regs(current);
  120. if (!in_nmi()) {
  121. regs_user->regs = user_regs;
  122. regs_user->abi = perf_reg_abi(current);
  123. return;
  124. }
  125. /*
  126. * If we're in an NMI that interrupted task_pt_regs setup, then
  127. * we can't sample user regs at all. This check isn't really
  128. * sufficient, though, as we could be in an NMI inside an interrupt
  129. * that happened during task_pt_regs setup.
  130. */
  131. if (regs->sp > (unsigned long)&user_regs->r11 &&
  132. regs->sp <= (unsigned long)(user_regs + 1)) {
  133. regs_user->abi = PERF_SAMPLE_REGS_ABI_NONE;
  134. regs_user->regs = NULL;
  135. return;
  136. }
  137. /*
  138. * These registers are always saved on 64-bit syscall entry.
  139. * On 32-bit entry points, they are saved too except r8..r11.
  140. */
  141. regs_user_copy->ip = user_regs->ip;
  142. regs_user_copy->ax = user_regs->ax;
  143. regs_user_copy->cx = user_regs->cx;
  144. regs_user_copy->dx = user_regs->dx;
  145. regs_user_copy->si = user_regs->si;
  146. regs_user_copy->di = user_regs->di;
  147. regs_user_copy->r8 = user_regs->r8;
  148. regs_user_copy->r9 = user_regs->r9;
  149. regs_user_copy->r10 = user_regs->r10;
  150. regs_user_copy->r11 = user_regs->r11;
  151. regs_user_copy->orig_ax = user_regs->orig_ax;
  152. regs_user_copy->flags = user_regs->flags;
  153. regs_user_copy->sp = user_regs->sp;
  154. regs_user_copy->cs = user_regs->cs;
  155. regs_user_copy->ss = user_regs->ss;
  156. /*
  157. * Store user space frame-pointer value on sample
  158. * to facilitate stack unwinding for cases when
  159. * user space executable code has such support
  160. * enabled at compile time:
  161. */
  162. regs_user_copy->bp = user_regs->bp;
  163. regs_user_copy->bx = -1;
  164. regs_user_copy->r12 = -1;
  165. regs_user_copy->r13 = -1;
  166. regs_user_copy->r14 = -1;
  167. regs_user_copy->r15 = -1;
  168. /*
  169. * For this to be at all useful, we need a reasonable guess for
  170. * the ABI. Be careful: we're in NMI context, and we're
  171. * considering current to be the current task, so we should
  172. * be careful not to look at any other percpu variables that might
  173. * change during context switches.
  174. */
  175. regs_user->abi = user_64bit_mode(user_regs) ?
  176. PERF_SAMPLE_REGS_ABI_64 : PERF_SAMPLE_REGS_ABI_32;
  177. regs_user->regs = regs_user_copy;
  178. }
  179. #endif /* CONFIG_X86_32 */