ptrace.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /*
  38. * On interrupt, gs and __gsh store the vector number. They never
  39. * store gs any more.
  40. */
  41. unsigned short gs;
  42. unsigned short __gsh;
  43. /* On interrupt, this is the error code. */
  44. unsigned long orig_ax;
  45. unsigned long ip;
  46. unsigned short cs;
  47. unsigned short __csh;
  48. unsigned long flags;
  49. unsigned long sp;
  50. unsigned short ss;
  51. unsigned short __ssh;
  52. };
  53. #else /* __i386__ */
  54. struct pt_regs {
  55. /*
  56. * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
  57. * unless syscall needs a complete, fully filled "struct pt_regs".
  58. */
  59. unsigned long r15;
  60. unsigned long r14;
  61. unsigned long r13;
  62. unsigned long r12;
  63. unsigned long bp;
  64. unsigned long bx;
  65. /* These regs are callee-clobbered. Always saved on kernel entry. */
  66. unsigned long r11;
  67. unsigned long r10;
  68. unsigned long r9;
  69. unsigned long r8;
  70. unsigned long ax;
  71. unsigned long cx;
  72. unsigned long dx;
  73. unsigned long si;
  74. unsigned long di;
  75. /*
  76. * On syscall entry, this is syscall#. On CPU exception, this is error code.
  77. * On hw interrupt, it's IRQ number:
  78. */
  79. unsigned long orig_ax;
  80. /* Return frame for iretq */
  81. unsigned long ip;
  82. unsigned long cs;
  83. unsigned long flags;
  84. unsigned long sp;
  85. unsigned long ss;
  86. /* top of stack page */
  87. };
  88. #endif /* !__i386__ */
  89. #ifdef CONFIG_PARAVIRT
  90. #include <asm/paravirt_types.h>
  91. #endif
  92. #include <asm/proto.h>
  93. struct cpuinfo_x86;
  94. struct task_struct;
  95. extern unsigned long profile_pc(struct pt_regs *regs);
  96. extern unsigned long
  97. convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
  98. extern void send_sigtrap(struct pt_regs *regs, int error_code, int si_code);
  99. static inline unsigned long regs_return_value(struct pt_regs *regs)
  100. {
  101. return regs->ax;
  102. }
  103. static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
  104. {
  105. regs->ax = rc;
  106. }
  107. /*
  108. * user_mode(regs) determines whether a register set came from user
  109. * mode. On x86_32, this is true if V8086 mode was enabled OR if the
  110. * register set was from protected mode with RPL-3 CS value. This
  111. * tricky test checks that with one comparison.
  112. *
  113. * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
  114. * the extra check.
  115. */
  116. static __always_inline int user_mode(struct pt_regs *regs)
  117. {
  118. #ifdef CONFIG_X86_32
  119. return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
  120. #else
  121. return !!(regs->cs & 3);
  122. #endif
  123. }
  124. static __always_inline int v8086_mode(struct pt_regs *regs)
  125. {
  126. #ifdef CONFIG_X86_32
  127. return (regs->flags & X86_VM_MASK);
  128. #else
  129. return 0; /* No V86 mode support in long mode */
  130. #endif
  131. }
  132. static inline bool user_64bit_mode(struct pt_regs *regs)
  133. {
  134. #ifdef CONFIG_X86_64
  135. #ifndef CONFIG_PARAVIRT_XXL
  136. /*
  137. * On non-paravirt systems, this is the only long mode CPL 3
  138. * selector. We do not allow long mode selectors in the LDT.
  139. */
  140. return regs->cs == __USER_CS;
  141. #else
  142. /* Headers are too twisted for this to go in paravirt.h. */
  143. return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;
  144. #endif
  145. #else /* !CONFIG_X86_64 */
  146. return false;
  147. #endif
  148. }
  149. /*
  150. * Determine whether the register set came from any context that is running in
  151. * 64-bit mode.
  152. */
  153. static inline bool any_64bit_mode(struct pt_regs *regs)
  154. {
  155. #ifdef CONFIG_X86_64
  156. return !user_mode(regs) || user_64bit_mode(regs);
  157. #else
  158. return false;
  159. #endif
  160. }
  161. #ifdef CONFIG_X86_64
  162. #define current_user_stack_pointer() current_pt_regs()->sp
  163. #define compat_user_stack_pointer() current_pt_regs()->sp
  164. static __always_inline bool ip_within_syscall_gap(struct pt_regs *regs)
  165. {
  166. bool ret = (regs->ip >= (unsigned long)entry_SYSCALL_64 &&
  167. regs->ip < (unsigned long)entry_SYSCALL_64_safe_stack);
  168. ret = ret || (regs->ip >= (unsigned long)entry_SYSRETQ_unsafe_stack &&
  169. regs->ip < (unsigned long)entry_SYSRETQ_end);
  170. #ifdef CONFIG_IA32_EMULATION
  171. ret = ret || (regs->ip >= (unsigned long)entry_SYSCALL_compat &&
  172. regs->ip < (unsigned long)entry_SYSCALL_compat_safe_stack);
  173. ret = ret || (regs->ip >= (unsigned long)entry_SYSRETL_compat_unsafe_stack &&
  174. regs->ip < (unsigned long)entry_SYSRETL_compat_end);
  175. #endif
  176. return ret;
  177. }
  178. #endif
  179. static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
  180. {
  181. return regs->sp;
  182. }
  183. static inline unsigned long instruction_pointer(struct pt_regs *regs)
  184. {
  185. return regs->ip;
  186. }
  187. static inline void instruction_pointer_set(struct pt_regs *regs,
  188. unsigned long val)
  189. {
  190. regs->ip = val;
  191. }
  192. static inline unsigned long frame_pointer(struct pt_regs *regs)
  193. {
  194. return regs->bp;
  195. }
  196. static inline unsigned long user_stack_pointer(struct pt_regs *regs)
  197. {
  198. return regs->sp;
  199. }
  200. static inline void user_stack_pointer_set(struct pt_regs *regs,
  201. unsigned long val)
  202. {
  203. regs->sp = val;
  204. }
  205. static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)
  206. {
  207. return !(regs->flags & X86_EFLAGS_IF);
  208. }
  209. /* Query offset/name of register from its name/offset */
  210. extern int regs_query_register_offset(const char *name);
  211. extern const char *regs_query_register_name(unsigned int offset);
  212. #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
  213. /**
  214. * regs_get_register() - get register value from its offset
  215. * @regs: pt_regs from which register value is gotten.
  216. * @offset: offset number of the register.
  217. *
  218. * regs_get_register returns the value of a register. The @offset is the
  219. * offset of the register in struct pt_regs address which specified by @regs.
  220. * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
  221. */
  222. static inline unsigned long regs_get_register(struct pt_regs *regs,
  223. unsigned int offset)
  224. {
  225. if (unlikely(offset > MAX_REG_OFFSET))
  226. return 0;
  227. #ifdef CONFIG_X86_32
  228. /* The selector fields are 16-bit. */
  229. if (offset == offsetof(struct pt_regs, cs) ||
  230. offset == offsetof(struct pt_regs, ss) ||
  231. offset == offsetof(struct pt_regs, ds) ||
  232. offset == offsetof(struct pt_regs, es) ||
  233. offset == offsetof(struct pt_regs, fs) ||
  234. offset == offsetof(struct pt_regs, gs)) {
  235. return *(u16 *)((unsigned long)regs + offset);
  236. }
  237. #endif
  238. return *(unsigned long *)((unsigned long)regs + offset);
  239. }
  240. /**
  241. * regs_within_kernel_stack() - check the address in the stack
  242. * @regs: pt_regs which contains kernel stack pointer.
  243. * @addr: address which is checked.
  244. *
  245. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  246. * If @addr is within the kernel stack, it returns true. If not, returns false.
  247. */
  248. static inline int regs_within_kernel_stack(struct pt_regs *regs,
  249. unsigned long addr)
  250. {
  251. return ((addr & ~(THREAD_SIZE - 1)) == (regs->sp & ~(THREAD_SIZE - 1)));
  252. }
  253. /**
  254. * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack
  255. * @regs: pt_regs which contains kernel stack pointer.
  256. * @n: stack entry number.
  257. *
  258. * regs_get_kernel_stack_nth() returns the address of the @n th entry of the
  259. * kernel stack which is specified by @regs. If the @n th entry is NOT in
  260. * the kernel stack, this returns NULL.
  261. */
  262. static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
  263. {
  264. unsigned long *addr = (unsigned long *)regs->sp;
  265. addr += n;
  266. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  267. return addr;
  268. else
  269. return NULL;
  270. }
  271. /* To avoid include hell, we can't include uaccess.h */
  272. extern long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
  273. /**
  274. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  275. * @regs: pt_regs which contains kernel stack pointer.
  276. * @n: stack entry number.
  277. *
  278. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  279. * is specified by @regs. If the @n th entry is NOT in the kernel stack
  280. * this returns 0.
  281. */
  282. static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
  283. unsigned int n)
  284. {
  285. unsigned long *addr;
  286. unsigned long val;
  287. long ret;
  288. addr = regs_get_kernel_stack_nth_addr(regs, n);
  289. if (addr) {
  290. ret = copy_from_kernel_nofault(&val, addr, sizeof(val));
  291. if (!ret)
  292. return val;
  293. }
  294. return 0;
  295. }
  296. /**
  297. * regs_get_kernel_argument() - get Nth function argument in kernel
  298. * @regs: pt_regs of that context
  299. * @n: function argument number (start from 0)
  300. *
  301. * regs_get_argument() returns @n th argument of the function call.
  302. * Note that this chooses most probably assignment, in some case
  303. * it can be incorrect.
  304. * This is expected to be called from kprobes or ftrace with regs
  305. * where the top of stack is the return address.
  306. */
  307. static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
  308. unsigned int n)
  309. {
  310. static const unsigned int argument_offs[] = {
  311. #ifdef __i386__
  312. offsetof(struct pt_regs, ax),
  313. offsetof(struct pt_regs, dx),
  314. offsetof(struct pt_regs, cx),
  315. #define NR_REG_ARGUMENTS 3
  316. #else
  317. offsetof(struct pt_regs, di),
  318. offsetof(struct pt_regs, si),
  319. offsetof(struct pt_regs, dx),
  320. offsetof(struct pt_regs, cx),
  321. offsetof(struct pt_regs, r8),
  322. offsetof(struct pt_regs, r9),
  323. #define NR_REG_ARGUMENTS 6
  324. #endif
  325. };
  326. if (n >= NR_REG_ARGUMENTS) {
  327. n -= NR_REG_ARGUMENTS - 1;
  328. return regs_get_kernel_stack_nth(regs, n);
  329. } else
  330. return regs_get_register(regs, argument_offs[n]);
  331. }
  332. #define arch_has_single_step() (1)
  333. #ifdef CONFIG_X86_DEBUGCTLMSR
  334. #define arch_has_block_step() (1)
  335. #else
  336. #define arch_has_block_step() (boot_cpu_data.x86 >= 6)
  337. #endif
  338. #define ARCH_HAS_USER_SINGLE_STEP_REPORT
  339. struct user_desc;
  340. extern int do_get_thread_area(struct task_struct *p, int idx,
  341. struct user_desc __user *info);
  342. extern int do_set_thread_area(struct task_struct *p, int idx,
  343. struct user_desc __user *info, int can_allocate);
  344. #ifdef CONFIG_X86_64
  345. # define do_set_thread_area_64(p, s, t) do_arch_prctl_64(p, s, t)
  346. #else
  347. # define do_set_thread_area_64(p, s, t) (0)
  348. #endif
  349. #endif /* !__ASSEMBLY__ */
  350. #endif /* _ASM_X86_PTRACE_H */