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
40 /* On interrupt, gs and __gsh store the vector number. */
43 /* On interrupt, this is the error code. */
44 unsigned long orig_ax
;
58 * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
59 * unless syscall needs a complete, fully filled "struct pt_regs".
67 /* These regs are callee-clobbered. Always saved on kernel entry. */
78 * On syscall entry, this is syscall#. On CPU exception, this is error code.
79 * On hw interrupt, it's IRQ number:
81 unsigned long orig_ax
;
82 /* Return frame for iretq */
88 /* top of stack page */
91 #endif /* !__i386__ */
93 #ifdef CONFIG_PARAVIRT
94 #include <asm/paravirt_types.h>
100 extern unsigned long profile_pc(struct pt_regs
*regs
);
101 #define profile_pc profile_pc
104 convert_ip_to_linear(struct task_struct
*child
, struct pt_regs
*regs
);
105 extern void send_sigtrap(struct task_struct
*tsk
, struct pt_regs
*regs
,
106 int error_code
, int si_code
);
109 static inline unsigned long regs_return_value(struct pt_regs
*regs
)
114 static inline void regs_set_return_value(struct pt_regs
*regs
, unsigned long rc
)
120 * user_mode(regs) determines whether a register set came from user
121 * mode. On x86_32, this is true if V8086 mode was enabled OR if the
122 * register set was from protected mode with RPL-3 CS value. This
123 * tricky test checks that with one comparison.
125 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
128 static inline int user_mode(struct pt_regs
*regs
)
131 return ((regs
->cs
& SEGMENT_RPL_MASK
) | (regs
->flags
& X86_VM_MASK
)) >= USER_RPL
;
133 return !!(regs
->cs
& 3);
137 static inline int v8086_mode(struct pt_regs
*regs
)
140 return (regs
->flags
& X86_VM_MASK
);
142 return 0; /* No V86 mode support in long mode */
146 static inline bool user_64bit_mode(struct pt_regs
*regs
)
149 #ifndef CONFIG_PARAVIRT_XXL
151 * On non-paravirt systems, this is the only long mode CPL 3
152 * selector. We do not allow long mode selectors in the LDT.
154 return regs
->cs
== __USER_CS
;
156 /* Headers are too twisted for this to go in paravirt.h. */
157 return regs
->cs
== __USER_CS
|| regs
->cs
== pv_info
.extra_user_64bit_cs
;
159 #else /* !CONFIG_X86_64 */
165 #define current_user_stack_pointer() current_pt_regs()->sp
166 #define compat_user_stack_pointer() current_pt_regs()->sp
170 extern unsigned long kernel_stack_pointer(struct pt_regs
*regs
);
172 static inline unsigned long kernel_stack_pointer(struct pt_regs
*regs
)
178 #define GET_IP(regs) ((regs)->ip)
179 #define GET_FP(regs) ((regs)->bp)
180 #define GET_USP(regs) ((regs)->sp)
182 #include <asm-generic/ptrace.h>
184 /* Query offset/name of register from its name/offset */
185 extern int regs_query_register_offset(const char *name
);
186 extern const char *regs_query_register_name(unsigned int offset
);
187 #define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
190 * regs_get_register() - get register value from its offset
191 * @regs: pt_regs from which register value is gotten.
192 * @offset: offset number of the register.
194 * regs_get_register returns the value of a register. The @offset is the
195 * offset of the register in struct pt_regs address which specified by @regs.
196 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
198 static inline unsigned long regs_get_register(struct pt_regs
*regs
,
201 if (unlikely(offset
> MAX_REG_OFFSET
))
205 * Traps from the kernel do not save sp and ss.
206 * Use the helper function to retrieve sp.
208 if (offset
== offsetof(struct pt_regs
, sp
) &&
209 regs
->cs
== __KERNEL_CS
)
210 return kernel_stack_pointer(regs
);
212 /* The selector fields are 16-bit. */
213 if (offset
== offsetof(struct pt_regs
, cs
) ||
214 offset
== offsetof(struct pt_regs
, ss
) ||
215 offset
== offsetof(struct pt_regs
, ds
) ||
216 offset
== offsetof(struct pt_regs
, es
) ||
217 offset
== offsetof(struct pt_regs
, fs
) ||
218 offset
== offsetof(struct pt_regs
, gs
)) {
219 return *(u16
*)((unsigned long)regs
+ offset
);
223 return *(unsigned long *)((unsigned long)regs
+ offset
);
227 * regs_within_kernel_stack() - check the address in the stack
228 * @regs: pt_regs which contains kernel stack pointer.
229 * @addr: address which is checked.
231 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
232 * If @addr is within the kernel stack, it returns true. If not, returns false.
234 static inline int regs_within_kernel_stack(struct pt_regs
*regs
,
237 return ((addr
& ~(THREAD_SIZE
- 1)) ==
238 (kernel_stack_pointer(regs
) & ~(THREAD_SIZE
- 1)));
242 * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack
243 * @regs: pt_regs which contains kernel stack pointer.
244 * @n: stack entry number.
246 * regs_get_kernel_stack_nth() returns the address of the @n th entry of the
247 * kernel stack which is specified by @regs. If the @n th entry is NOT in
248 * the kernel stack, this returns NULL.
250 static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs
*regs
, unsigned int n
)
252 unsigned long *addr
= (unsigned long *)kernel_stack_pointer(regs
);
255 if (regs_within_kernel_stack(regs
, (unsigned long)addr
))
261 /* To avoid include hell, we can't include uaccess.h */
262 extern long probe_kernel_read(void *dst
, const void *src
, size_t size
);
265 * regs_get_kernel_stack_nth() - get Nth entry of the stack
266 * @regs: pt_regs which contains kernel stack pointer.
267 * @n: stack entry number.
269 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
270 * is specified by @regs. If the @n th entry is NOT in the kernel stack
273 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs
*regs
,
280 addr
= regs_get_kernel_stack_nth_addr(regs
, n
);
282 ret
= probe_kernel_read(&val
, addr
, sizeof(val
));
290 * regs_get_kernel_argument() - get Nth function argument in kernel
291 * @regs: pt_regs of that context
292 * @n: function argument number (start from 0)
294 * regs_get_argument() returns @n th argument of the function call.
295 * Note that this chooses most probably assignment, in some case
296 * it can be incorrect.
297 * This is expected to be called from kprobes or ftrace with regs
298 * where the top of stack is the return address.
300 static inline unsigned long regs_get_kernel_argument(struct pt_regs
*regs
,
303 static const unsigned int argument_offs
[] = {
305 offsetof(struct pt_regs
, ax
),
306 offsetof(struct pt_regs
, cx
),
307 offsetof(struct pt_regs
, dx
),
308 #define NR_REG_ARGUMENTS 3
310 offsetof(struct pt_regs
, di
),
311 offsetof(struct pt_regs
, si
),
312 offsetof(struct pt_regs
, dx
),
313 offsetof(struct pt_regs
, cx
),
314 offsetof(struct pt_regs
, r8
),
315 offsetof(struct pt_regs
, r9
),
316 #define NR_REG_ARGUMENTS 6
320 if (n
>= NR_REG_ARGUMENTS
) {
321 n
-= NR_REG_ARGUMENTS
- 1;
322 return regs_get_kernel_stack_nth(regs
, n
);
324 return regs_get_register(regs
, argument_offs
[n
]);
327 #define arch_has_single_step() (1)
328 #ifdef CONFIG_X86_DEBUGCTLMSR
329 #define arch_has_block_step() (1)
331 #define arch_has_block_step() (boot_cpu_data.x86 >= 6)
334 #define ARCH_HAS_USER_SINGLE_STEP_REPORT
337 * When hitting ptrace_stop(), we cannot return using SYSRET because
338 * that does not restore the full CPU state, only a minimal set. The
339 * ptracer can change arbitrary register values, which is usually okay
340 * because the usual ptrace stops run off the signal delivery path which
341 * forces IRET; however, ptrace_event() stops happen in arbitrary places
342 * in the kernel and don't force IRET path.
344 * So force IRET path after a ptrace stop.
346 #define arch_ptrace_stop_needed(code, info) \
353 extern int do_get_thread_area(struct task_struct
*p
, int idx
,
354 struct user_desc __user
*info
);
355 extern int do_set_thread_area(struct task_struct
*p
, int idx
,
356 struct user_desc __user
*info
, int can_allocate
);
358 #endif /* !__ASSEMBLY__ */
359 #endif /* _ASM_X86_PTRACE_H */