ptrace.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_PTRACE_H
  3. #define _ASM_X86_PTRACE_H
  4. #include <asm/segment.h>
  5. #include <asm/page_types.h>
  6. #include <uapi/asm/ptrace.h>
  7. #ifndef __ASSEMBLY__
  8. #ifdef __i386__
  9. struct pt_regs {
  10. /*
  11. * NB: 32-bit x86 CPUs are inconsistent as what happens in the
  12. * following cases (where %seg represents a segment register):
  13. *
  14. * - pushl %seg: some do a 16-bit write and leave the high
  15. * bits alone
  16. * - movl %seg, [mem]: some do a 16-bit write despite the movl
  17. * - IDT entry: some (e.g. 486) will leave the high bits of CS
  18. * and (if applicable) SS undefined.
  19. *
  20. * Fortunately, x86-32 doesn't read the high bits on POP or IRET,
  21. * so we can just treat all of the segment registers as 16-bit
  22. * values.
  23. */
  24. unsigned long bx;
  25. unsigned long cx;
  26. unsigned long dx;
  27. unsigned long si;
  28. unsigned long di;
  29. unsigned long bp;
  30. unsigned long ax;
  31. unsigned short ds;
  32. unsigned short __dsh;
  33. unsigned short es;
  34. unsigned short __esh;
  35. unsigned short fs;
  36. unsigned short __fsh;
  37. /* On interrupt, gs and __gsh store the vector number. */
  38. unsigned short gs;
  39. unsigned short __gsh;
  40. /* On interrupt, this is the error code. */
  41. unsigned long orig_ax;
  42. unsigned long ip;
  43. unsigned short cs;
  44. unsigned short __csh;
  45. unsigned long flags;
  46. unsigned long sp;
  47. unsigned short ss;
  48. unsigned short __ssh;
  49. };
  50. #else /* __i386__ */
  51. struct pt_regs {
  52. /*
  53. * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
  54. * unless syscall needs a complete, fully filled "struct pt_regs".
  55. */
  56. unsigned long r15;
  57. unsigned long r14;
  58. unsigned long r13;
  59. unsigned long r12;
  60. unsigned long bp;
  61. unsigned long bx;
  62. /* These regs are callee-clobbered. Always saved on kernel entry. */
  63. unsigned long r11;
  64. unsigned long r10;
  65. unsigned long r9;
  66. unsigned long r8;
  67. unsigned long ax;
  68. unsigned long cx;
  69. unsigned long dx;
  70. unsigned long si;
  71. unsigned long di;
  72. /*
  73. * On syscall entry, this is syscall#. On CPU exception, this is error code.
  74. * On hw interrupt, it's IRQ number:
  75. */
  76. unsigned long orig_ax;
  77. /* Return frame for iretq */
  78. unsigned long ip;
  79. unsigned long cs;
  80. unsigned long flags;
  81. unsigned long sp;
  82. unsigned long ss;
  83. /* top of stack page */
  84. };
  85. #endif /* !__i386__ */
  86. #ifdef CONFIG_PARAVIRT
  87. #include <asm/paravirt_types.h>
  88. #endif
  89. struct cpuinfo_x86;
  90. struct task_struct;
  91. extern unsigned long profile_pc(struct pt_regs *regs);
  92. #define profile_pc profile_pc
  93. extern unsigned long
  94. convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
  95. extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
  96. int error_code, int si_code);
  97. static inline unsigned long regs_return_value(struct pt_regs *regs)
  98. {
  99. return regs->ax;
  100. }
  101. static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
  102. {
  103. regs->ax = rc;
  104. }
  105. /*
  106. * user_mode(regs) determines whether a register set came from user
  107. * mode. On x86_32, this is true if V8086 mode was enabled OR if the
  108. * register set was from protected mode with RPL-3 CS value. This
  109. * tricky test checks that with one comparison.
  110. *
  111. * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
  112. * the extra check.
  113. */
  114. static inline int user_mode(struct pt_regs *regs)
  115. {
  116. #ifdef CONFIG_X86_32
  117. return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
  118. #else
  119. return !!(regs->cs & 3);
  120. #endif
  121. }
  122. static inline int v8086_mode(struct pt_regs *regs)
  123. {
  124. #ifdef CONFIG_X86_32
  125. return (regs->flags & X86_VM_MASK);
  126. #else
  127. return 0; /* No V86 mode support in long mode */
  128. #endif
  129. }
  130. static inline bool user_64bit_mode(struct pt_regs *regs)
  131. {
  132. #ifdef CONFIG_X86_64
  133. #ifndef CONFIG_PARAVIRT_XXL
  134. /*
  135. * On non-paravirt systems, this is the only long mode CPL 3
  136. * selector. We do not allow long mode selectors in the LDT.
  137. */
  138. return regs->cs == __USER_CS;
  139. #else
  140. /* Headers are too twisted for this to go in paravirt.h. */
  141. return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;
  142. #endif
  143. #else /* !CONFIG_X86_64 */
  144. return false;
  145. #endif
  146. }
  147. #ifdef CONFIG_X86_64
  148. #define current_user_stack_pointer() current_pt_regs()->sp
  149. #define compat_user_stack_pointer() current_pt_regs()->sp
  150. #endif
  151. #ifdef CONFIG_X86_32
  152. extern unsigned long kernel_stack_pointer(struct pt_regs *regs);
  153. #else
  154. static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
  155. {
  156. return regs->sp;
  157. }
  158. #endif
  159. #define GET_IP(regs) ((regs)->ip)
  160. #define GET_FP(regs) ((regs)->bp)
  161. #define GET_USP(regs) ((regs)->sp)
  162. #include <asm-generic/ptrace.h>
  163. /* Query offset/name of register from its name/offset */
  164. extern int regs_query_register_offset(const char *name);
  165. extern const char *regs_query_register_name(unsigned int offset);
  166. #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
  167. /**
  168. * regs_get_register() - get register value from its offset
  169. * @regs: pt_regs from which register value is gotten.
  170. * @offset: offset number of the register.
  171. *
  172. * regs_get_register returns the value of a register. The @offset is the
  173. * offset of the register in struct pt_regs address which specified by @regs.
  174. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  175. */
  176. static inline unsigned long regs_get_register(struct pt_regs *regs,
  177. unsigned int offset)
  178. {
  179. if (unlikely(offset > MAX_REG_OFFSET))
  180. return 0;
  181. #ifdef CONFIG_X86_32
  182. /*
  183. * Traps from the kernel do not save sp and ss.
  184. * Use the helper function to retrieve sp.
  185. */
  186. if (offset == offsetof(struct pt_regs, sp) &&
  187. regs->cs == __KERNEL_CS)
  188. return kernel_stack_pointer(regs);
  189. /* The selector fields are 16-bit. */
  190. if (offset == offsetof(struct pt_regs, cs) ||
  191. offset == offsetof(struct pt_regs, ss) ||
  192. offset == offsetof(struct pt_regs, ds) ||
  193. offset == offsetof(struct pt_regs, es) ||
  194. offset == offsetof(struct pt_regs, fs) ||
  195. offset == offsetof(struct pt_regs, gs)) {
  196. return *(u16 *)((unsigned long)regs + offset);
  197. }
  198. #endif
  199. return *(unsigned long *)((unsigned long)regs + offset);
  200. }
  201. /**
  202. * regs_within_kernel_stack() - check the address in the stack
  203. * @regs: pt_regs which contains kernel stack pointer.
  204. * @addr: address which is checked.
  205. *
  206. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  207. * If @addr is within the kernel stack, it returns true. If not, returns false.
  208. */
  209. static inline int regs_within_kernel_stack(struct pt_regs *regs,
  210. unsigned long addr)
  211. {
  212. return ((addr & ~(THREAD_SIZE - 1)) ==
  213. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  214. }
  215. /**
  216. * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack
  217. * @regs: pt_regs which contains kernel stack pointer.
  218. * @n: stack entry number.
  219. *
  220. * regs_get_kernel_stack_nth() returns the address of the @n th entry of the
  221. * kernel stack which is specified by @regs. If the @n th entry is NOT in
  222. * the kernel stack, this returns NULL.
  223. */
  224. static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
  225. {
  226. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  227. addr += n;
  228. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  229. return addr;
  230. else
  231. return NULL;
  232. }
  233. /* To avoid include hell, we can't include uaccess.h */
  234. extern long probe_kernel_read(void *dst, const void *src, size_t size);
  235. /**
  236. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  237. * @regs: pt_regs which contains kernel stack pointer.
  238. * @n: stack entry number.
  239. *
  240. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  241. * is specified by @regs. If the @n th entry is NOT in the kernel stack
  242. * this returns 0.
  243. */
  244. static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
  245. unsigned int n)
  246. {
  247. unsigned long *addr;
  248. unsigned long val;
  249. long ret;
  250. addr = regs_get_kernel_stack_nth_addr(regs, n);
  251. if (addr) {
  252. ret = probe_kernel_read(&val, addr, sizeof(val));
  253. if (!ret)
  254. return val;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * regs_get_kernel_argument() - get Nth function argument in kernel
  260. * @regs: pt_regs of that context
  261. * @n: function argument number (start from 0)
  262. *
  263. * regs_get_argument() returns @n th argument of the function call.
  264. * Note that this chooses most probably assignment, in some case
  265. * it can be incorrect.
  266. * This is expected to be called from kprobes or ftrace with regs
  267. * where the top of stack is the return address.
  268. */
  269. static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
  270. unsigned int n)
  271. {
  272. static const unsigned int argument_offs[] = {
  273. #ifdef __i386__
  274. offsetof(struct pt_regs, ax),
  275. offsetof(struct pt_regs, cx),
  276. offsetof(struct pt_regs, dx),
  277. #define NR_REG_ARGUMENTS 3
  278. #else
  279. offsetof(struct pt_regs, di),
  280. offsetof(struct pt_regs, si),
  281. offsetof(struct pt_regs, dx),
  282. offsetof(struct pt_regs, cx),
  283. offsetof(struct pt_regs, r8),
  284. offsetof(struct pt_regs, r9),
  285. #define NR_REG_ARGUMENTS 6
  286. #endif
  287. };
  288. if (n >= NR_REG_ARGUMENTS) {
  289. n -= NR_REG_ARGUMENTS - 1;
  290. return regs_get_kernel_stack_nth(regs, n);
  291. } else
  292. return regs_get_register(regs, argument_offs[n]);
  293. }
  294. #define arch_has_single_step() (1)
  295. #ifdef CONFIG_X86_DEBUGCTLMSR
  296. #define arch_has_block_step() (1)
  297. #else
  298. #define arch_has_block_step() (boot_cpu_data.x86 >= 6)
  299. #endif
  300. #define ARCH_HAS_USER_SINGLE_STEP_REPORT
  301. /*
  302. * When hitting ptrace_stop(), we cannot return using SYSRET because
  303. * that does not restore the full CPU state, only a minimal set. The
  304. * ptracer can change arbitrary register values, which is usually okay
  305. * because the usual ptrace stops run off the signal delivery path which
  306. * forces IRET; however, ptrace_event() stops happen in arbitrary places
  307. * in the kernel and don't force IRET path.
  308. *
  309. * So force IRET path after a ptrace stop.
  310. */
  311. #define arch_ptrace_stop_needed(code, info) \
  312. ({ \
  313. force_iret(); \
  314. false; \
  315. })
  316. struct user_desc;
  317. extern int do_get_thread_area(struct task_struct *p, int idx,
  318. struct user_desc __user *info);
  319. extern int do_set_thread_area(struct task_struct *p, int idx,
  320. struct user_desc __user *info, int can_allocate);
  321. #endif /* !__ASSEMBLY__ */
  322. #endif /* _ASM_X86_PTRACE_H */