perf_regs.c 4.9 KB

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