1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_X86_PTRACE_H
3 #define _ASM_X86_PTRACE_H
5 #include <asm/segment.h>
6 #include <asm/page_types.h>
7 #include <uapi/asm/ptrace.h>
14 * NB: 32-bit x86 CPUs are inconsistent as what happens in the
15 * following cases (where %seg represents a segment register):
17 * - pushl %seg: some do a 16-bit write and leave the high
19 * - movl %seg, [mem]: some do a 16-bit write despite the movl
20 * - IDT entry: some (e.g. 486) will leave the high bits of CS
21 * and (if applicable) SS undefined.
23 * Fortunately, x86-32 doesn't read the high bits on POP or IRET,
24 * so we can just treat all of the segment registers as 16-bit
41 * On interrupt, gs and __gsh store the vector number. They never
46 /* On interrupt, this is the error code. */
47 unsigned long orig_ax;
61 * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
62 * unless syscall needs a complete, fully filled "struct pt_regs".
70 /* These regs are callee-clobbered. Always saved on kernel entry. */
81 * On syscall entry, this is syscall#. On CPU exception, this is error code.
82 * On hw interrupt, it's IRQ number:
84 unsigned long orig_ax;
85 /* Return frame for iretq */
91 /* top of stack page */
94 #endif /* !__i386__ */
96 #ifdef CONFIG_PARAVIRT
97 #include <asm/paravirt_types.h>
100 #include <asm/proto.h>
105 extern unsigned long profile_pc(struct pt_regs *regs);
108 convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
109 extern void send_sigtrap(struct pt_regs *regs, int error_code, int si_code);
112 static inline unsigned long regs_return_value(struct pt_regs *regs)
117 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
123 * user_mode(regs) determines whether a register set came from user
124 * mode. On x86_32, this is true if V8086 mode was enabled OR if the
125 * register set was from protected mode with RPL-3 CS value. This
126 * tricky test checks that with one comparison.
128 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
131 static __always_inline int user_mode(struct pt_regs *regs)
134 return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
136 return !!(regs->cs & 3);
140 static __always_inline int v8086_mode(struct pt_regs *regs)
143 return (regs->flags & X86_VM_MASK);
145 return 0; /* No V86 mode support in long mode */
149 static inline bool user_64bit_mode(struct pt_regs *regs)
152 #ifndef CONFIG_PARAVIRT_XXL
154 * On non-paravirt systems, this is the only long mode CPL 3
155 * selector. We do not allow long mode selectors in the LDT.
157 return regs->cs == __USER_CS;
159 /* Headers are too twisted for this to go in paravirt.h. */
160 return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;
162 #else /* !CONFIG_X86_64 */
168 * Determine whether the register set came from any context that is running in
171 static inline bool any_64bit_mode(struct pt_regs *regs)
174 return !user_mode(regs) || user_64bit_mode(regs);
181 #define current_user_stack_pointer() current_pt_regs()->sp
182 #define compat_user_stack_pointer() current_pt_regs()->sp
184 static __always_inline bool ip_within_syscall_gap(struct pt_regs *regs)
186 bool ret = (regs->ip >= (unsigned long)entry_SYSCALL_64 &&
187 regs->ip < (unsigned long)entry_SYSCALL_64_safe_stack);
189 ret = ret || (regs->ip >= (unsigned long)entry_SYSRETQ_unsafe_stack &&
190 regs->ip < (unsigned long)entry_SYSRETQ_end);
191 #ifdef CONFIG_IA32_EMULATION
192 ret = ret || (regs->ip >= (unsigned long)entry_SYSCALL_compat &&
193 regs->ip < (unsigned long)entry_SYSCALL_compat_safe_stack);
194 ret = ret || (regs->ip >= (unsigned long)entry_SYSRETL_compat_unsafe_stack &&
195 regs->ip < (unsigned long)entry_SYSRETL_compat_end);
202 static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
207 static inline unsigned long instruction_pointer(struct pt_regs *regs)
212 static inline void instruction_pointer_set(struct pt_regs *regs,
218 static inline unsigned long frame_pointer(struct pt_regs *regs)
223 static inline unsigned long user_stack_pointer(struct pt_regs *regs)
228 static inline void user_stack_pointer_set(struct pt_regs *regs,
234 static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)
236 return !(regs->flags & X86_EFLAGS_IF);
239 /* Query offset/name of register from its name/offset */
240 extern int regs_query_register_offset(const char *name);
241 extern const char *regs_query_register_name(unsigned int offset);
242 #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
245 * regs_get_register() - get register value from its offset
246 * @regs: pt_regs from which register value is gotten.
247 * @offset: offset number of the register.
249 * regs_get_register returns the value of a register. The @offset is the
250 * offset of the register in struct pt_regs address which specified by @regs.
251 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
253 static inline unsigned long regs_get_register(struct pt_regs *regs,
256 if (unlikely(offset > MAX_REG_OFFSET))
259 /* The selector fields are 16-bit. */
260 if (offset == offsetof(struct pt_regs, cs) ||
261 offset == offsetof(struct pt_regs, ss) ||
262 offset == offsetof(struct pt_regs, ds) ||
263 offset == offsetof(struct pt_regs, es) ||
264 offset == offsetof(struct pt_regs, fs) ||
265 offset == offsetof(struct pt_regs, gs)) {
266 return *(u16 *)((unsigned long)regs + offset);
270 return *(unsigned long *)((unsigned long)regs + offset);
274 * regs_within_kernel_stack() - check the address in the stack
275 * @regs: pt_regs which contains kernel stack pointer.
276 * @addr: address which is checked.
278 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
279 * If @addr is within the kernel stack, it returns true. If not, returns false.
281 static inline int regs_within_kernel_stack(struct pt_regs *regs,
284 return ((addr & ~(THREAD_SIZE - 1)) == (regs->sp & ~(THREAD_SIZE - 1)));
288 * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack
289 * @regs: pt_regs which contains kernel stack pointer.
290 * @n: stack entry number.
292 * regs_get_kernel_stack_nth() returns the address of the @n th entry of the
293 * kernel stack which is specified by @regs. If the @n th entry is NOT in
294 * the kernel stack, this returns NULL.
296 static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
298 unsigned long *addr = (unsigned long *)regs->sp;
301 if (regs_within_kernel_stack(regs, (unsigned long)addr))
307 /* To avoid include hell, we can't include uaccess.h */
308 extern long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
311 * regs_get_kernel_stack_nth() - get Nth entry of the stack
312 * @regs: pt_regs which contains kernel stack pointer.
313 * @n: stack entry number.
315 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
316 * is specified by @regs. If the @n th entry is NOT in the kernel stack
319 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
326 addr = regs_get_kernel_stack_nth_addr(regs, n);
328 ret = copy_from_kernel_nofault(&val, addr, sizeof(val));
336 * regs_get_kernel_argument() - get Nth function argument in kernel
337 * @regs: pt_regs of that context
338 * @n: function argument number (start from 0)
340 * regs_get_argument() returns @n th argument of the function call.
341 * Note that this chooses most probably assignment, in some case
342 * it can be incorrect.
343 * This is expected to be called from kprobes or ftrace with regs
344 * where the top of stack is the return address.
346 static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
349 static const unsigned int argument_offs[] = {
351 offsetof(struct pt_regs, ax),
352 offsetof(struct pt_regs, dx),
353 offsetof(struct pt_regs, cx),
354 #define NR_REG_ARGUMENTS 3
356 offsetof(struct pt_regs, di),
357 offsetof(struct pt_regs, si),
358 offsetof(struct pt_regs, dx),
359 offsetof(struct pt_regs, cx),
360 offsetof(struct pt_regs, r8),
361 offsetof(struct pt_regs, r9),
362 #define NR_REG_ARGUMENTS 6
366 if (n >= NR_REG_ARGUMENTS) {
367 n -= NR_REG_ARGUMENTS - 1;
368 return regs_get_kernel_stack_nth(regs, n);
370 return regs_get_register(regs, argument_offs[n]);
373 #define arch_has_single_step() (1)
374 #ifdef CONFIG_X86_DEBUGCTLMSR
375 #define arch_has_block_step() (1)
377 #define arch_has_block_step() (boot_cpu_data.x86 >= 6)
380 #define ARCH_HAS_USER_SINGLE_STEP_REPORT
383 extern int do_get_thread_area(struct task_struct *p, int idx,
384 struct user_desc __user *info);
385 extern int do_set_thread_area(struct task_struct *p, int idx,
386 struct user_desc __user *info, int can_allocate);
389 # define do_set_thread_area_64(p, s, t) do_arch_prctl_64(p, s, t)
391 # define do_set_thread_area_64(p, s, t) (0)
394 #endif /* !__ASSEMBLY__ */
395 #endif /* _ASM_X86_PTRACE_H */