perf_regs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * RIP, flags, and the argument registers are usually saved.
  118. * orig_ax is probably okay, too.
  119. */
  120. regs_user_copy->ip = user_regs->ip;
  121. regs_user_copy->cx = user_regs->cx;
  122. regs_user_copy->dx = user_regs->dx;
  123. regs_user_copy->si = user_regs->si;
  124. regs_user_copy->di = user_regs->di;
  125. regs_user_copy->r8 = user_regs->r8;
  126. regs_user_copy->r9 = user_regs->r9;
  127. regs_user_copy->r10 = user_regs->r10;
  128. regs_user_copy->r11 = user_regs->r11;
  129. regs_user_copy->orig_ax = user_regs->orig_ax;
  130. regs_user_copy->flags = user_regs->flags;
  131. /*
  132. * Don't even try to report the "rest" regs.
  133. */
  134. regs_user_copy->bx = -1;
  135. regs_user_copy->bp = -1;
  136. regs_user_copy->r12 = -1;
  137. regs_user_copy->r13 = -1;
  138. regs_user_copy->r14 = -1;
  139. regs_user_copy->r15 = -1;
  140. /*
  141. * For this to be at all useful, we need a reasonable guess for
  142. * sp and the ABI. Be careful: we're in NMI context, and we're
  143. * considering current to be the current task, so we should
  144. * be careful not to look at any other percpu variables that might
  145. * change during context switches.
  146. */
  147. if (IS_ENABLED(CONFIG_IA32_EMULATION) &&
  148. task_thread_info(current)->status & TS_COMPAT) {
  149. /* Easy case: we're in a compat syscall. */
  150. regs_user->abi = PERF_SAMPLE_REGS_ABI_32;
  151. regs_user_copy->sp = user_regs->sp;
  152. regs_user_copy->cs = user_regs->cs;
  153. regs_user_copy->ss = user_regs->ss;
  154. } else if (user_regs->orig_ax != -1) {
  155. /*
  156. * We're probably in a 64-bit syscall.
  157. * Warning: this code is severely racy. At least it's better
  158. * than just blindly copying user_regs.
  159. */
  160. regs_user->abi = PERF_SAMPLE_REGS_ABI_64;
  161. regs_user_copy->sp = this_cpu_read(old_rsp);
  162. regs_user_copy->cs = __USER_CS;
  163. regs_user_copy->ss = __USER_DS;
  164. regs_user_copy->cx = -1; /* usually contains garbage */
  165. } else {
  166. /* We're probably in an interrupt or exception. */
  167. regs_user->abi = user_64bit_mode(user_regs) ?
  168. PERF_SAMPLE_REGS_ABI_64 : PERF_SAMPLE_REGS_ABI_32;
  169. regs_user_copy->sp = user_regs->sp;
  170. regs_user_copy->cs = user_regs->cs;
  171. regs_user_copy->ss = user_regs->ss;
  172. }
  173. regs_user->regs = regs_user_copy;
  174. }
  175. #endif /* CONFIG_X86_32 */