blob: b94f615600d57b9bcc9e326839120cf67b1c2b9d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_PTRACE_H
3#define _ASM_X86_PTRACE_H
Thomas Gleixner8fc37f22007-10-23 22:37:24 +02004
Vegard Nossum6330a302008-05-28 09:46:19 +02005#include <asm/segment.h>
Masami Hiramatsub1cf5402009-08-13 16:34:44 -04006#include <asm/page_types.h>
David Howellsaf170c52012-12-14 22:37:13 +00007#include <uapi/asm/ptrace.h>
Markus Metzgereee3af42008-01-30 13:31:09 +01008
Thomas Gleixner8fc37f22007-10-23 22:37:24 +02009#ifndef __ASSEMBLY__
Thomas Gleixner8fc37f22007-10-23 22:37:24 +020010#ifdef __i386__
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010011
12struct pt_regs {
Andy Lutomirski385eca82017-07-28 06:00:30 -070013 /*
14 * NB: 32-bit x86 CPUs are inconsistent as what happens in the
15 * following cases (where %seg represents a segment register):
16 *
17 * - pushl %seg: some do a 16-bit write and leave the high
18 * bits alone
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.
22 *
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
25 * values.
26 */
Harvey Harrison92bc2052008-02-08 12:09:56 -080027 unsigned long bx;
28 unsigned long cx;
29 unsigned long dx;
30 unsigned long si;
31 unsigned long di;
32 unsigned long bp;
Harvey Harrison9902a702008-02-08 12:09:57 -080033 unsigned long ax;
Andy Lutomirski385eca82017-07-28 06:00:30 -070034 unsigned short ds;
35 unsigned short __dsh;
36 unsigned short es;
37 unsigned short __esh;
38 unsigned short fs;
39 unsigned short __fsh;
Andy Lutomirski3fb0fdb2021-02-13 11:19:44 -080040 /*
41 * On interrupt, gs and __gsh store the vector number. They never
42 * store gs any more.
43 */
Andy Lutomirski385eca82017-07-28 06:00:30 -070044 unsigned short gs;
45 unsigned short __gsh;
Jann Horn81fd9c12018-08-28 22:14:19 +020046 /* On interrupt, this is the error code. */
Harvey Harrison9902a702008-02-08 12:09:57 -080047 unsigned long orig_ax;
Harvey Harrison92bc2052008-02-08 12:09:56 -080048 unsigned long ip;
Andy Lutomirski385eca82017-07-28 06:00:30 -070049 unsigned short cs;
50 unsigned short __csh;
Harvey Harrison92bc2052008-02-08 12:09:56 -080051 unsigned long flags;
52 unsigned long sp;
Andy Lutomirski385eca82017-07-28 06:00:30 -070053 unsigned short ss;
54 unsigned short __ssh;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010055};
Thomas Gleixner8fc37f22007-10-23 22:37:24 +020056
Thomas Gleixner8fc37f22007-10-23 22:37:24 +020057#else /* __i386__ */
58
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010059struct pt_regs {
Denys Vlasenkoe90e1472015-02-26 14:40:28 -080060/*
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".
63 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010064 unsigned long r15;
65 unsigned long r14;
66 unsigned long r13;
67 unsigned long r12;
68 unsigned long bp;
69 unsigned long bx;
Denys Vlasenkoe90e1472015-02-26 14:40:28 -080070/* These regs are callee-clobbered. Always saved on kernel entry. */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010071 unsigned long r11;
72 unsigned long r10;
73 unsigned long r9;
74 unsigned long r8;
75 unsigned long ax;
76 unsigned long cx;
77 unsigned long dx;
78 unsigned long si;
79 unsigned long di;
Denys Vlasenkoe90e1472015-02-26 14:40:28 -080080/*
81 * On syscall entry, this is syscall#. On CPU exception, this is error code.
82 * On hw interrupt, it's IRQ number:
83 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010084 unsigned long orig_ax;
Denys Vlasenkoe90e1472015-02-26 14:40:28 -080085/* Return frame for iretq */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +010086 unsigned long ip;
87 unsigned long cs;
88 unsigned long flags;
89 unsigned long sp;
90 unsigned long ss;
91/* top of stack page */
92};
Thomas Gleixner8fc37f22007-10-23 22:37:24 +020093
Thomas Gleixner8fc37f22007-10-23 22:37:24 +020094#endif /* !__i386__ */
Roland McGrathefd1ca52008-01-30 13:30:46 +010095
Andy Lutomirski318f5a22011-08-03 09:31:53 -040096#ifdef CONFIG_PARAVIRT
97#include <asm/paravirt_types.h>
98#endif
Harvey Harrisondbe35332008-01-30 13:33:16 +010099
Joerg Roedel78a81d82021-03-03 15:17:12 +0100100#include <asm/proto.h>
101
Markus Metzger93fa7632008-04-08 11:01:58 +0200102struct cpuinfo_x86;
Harvey Harrisondbe35332008-01-30 13:33:16 +0100103struct task_struct;
104
Harvey Harrisondbe35332008-01-30 13:33:16 +0100105extern unsigned long profile_pc(struct pt_regs *regs);
106
107extern unsigned long
108convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
Eric W. Biederman28d42ea2019-02-05 18:25:11 -0600109extern void send_sigtrap(struct pt_regs *regs, int error_code, int si_code);
Hiroshi Shimamoto72fa50f2008-09-05 16:27:11 -0700110
Andy Lutomirskie0ffbaa2014-09-05 15:13:54 -0700111
Harvey Harrison9902a702008-02-08 12:09:57 -0800112static inline unsigned long regs_return_value(struct pt_regs *regs)
113{
114 return regs->ax;
115}
Harvey Harrisondbe35332008-01-30 13:33:16 +0100116
Josef Bacik9802d862017-12-11 11:36:48 -0500117static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
118{
119 regs->ax = rc;
120}
121
Roland McGrath7f232342008-01-30 13:30:48 +0100122/*
Andy Lutomirskiefa70452015-03-18 18:33:32 -0700123 * 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.
127 *
128 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
129 * the extra check.
Harvey Harrison90d43d72008-01-30 13:33:16 +0100130 */
Thomas Gleixner4979fb52020-01-21 15:53:09 +0100131static __always_inline int user_mode(struct pt_regs *regs)
Harvey Harrison90d43d72008-01-30 13:33:16 +0100132{
133#ifdef CONFIG_X86_32
Jan Beulich7ba554b2015-05-28 09:16:45 +0100134 return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
Harvey Harrison90d43d72008-01-30 13:33:16 +0100135#else
136 return !!(regs->cs & 3);
137#endif
138}
139
Harvey Harrison90d43d72008-01-30 13:33:16 +0100140static inline int v8086_mode(struct pt_regs *regs)
141{
142#ifdef CONFIG_X86_32
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300143 return (regs->flags & X86_VM_MASK);
Harvey Harrison90d43d72008-01-30 13:33:16 +0100144#else
145 return 0; /* No V86 mode support in long mode */
146#endif
147}
148
Andy Lutomirski318f5a22011-08-03 09:31:53 -0400149static inline bool user_64bit_mode(struct pt_regs *regs)
150{
Ricardo Nerie27c3102017-10-27 13:25:30 -0700151#ifdef CONFIG_X86_64
Juergen Gross40181642018-08-28 09:40:22 +0200152#ifndef CONFIG_PARAVIRT_XXL
Andy Lutomirski318f5a22011-08-03 09:31:53 -0400153 /*
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.
156 */
157 return regs->cs == __USER_CS;
158#else
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;
161#endif
Ricardo Nerie27c3102017-10-27 13:25:30 -0700162#else /* !CONFIG_X86_64 */
163 return false;
164#endif
Andy Lutomirski318f5a22011-08-03 09:31:53 -0400165}
Al Viro9b064fc2012-12-14 13:49:35 -0500166
Jann Horn7be44122019-12-19 00:11:47 +0100167/*
168 * Determine whether the register set came from any context that is running in
169 * 64-bit mode.
170 */
171static inline bool any_64bit_mode(struct pt_regs *regs)
172{
173#ifdef CONFIG_X86_64
174 return !user_mode(regs) || user_64bit_mode(regs);
175#else
176 return false;
177#endif
178}
179
Ricardo Nerie27c3102017-10-27 13:25:30 -0700180#ifdef CONFIG_X86_64
Denys Vlasenko263042e2015-03-09 19:39:23 +0100181#define current_user_stack_pointer() current_pt_regs()->sp
182#define compat_user_stack_pointer() current_pt_regs()->sp
Joerg Roedel78a81d82021-03-03 15:17:12 +0100183
184static inline bool ip_within_syscall_gap(struct pt_regs *regs)
185{
186 bool ret = (regs->ip >= (unsigned long)entry_SYSCALL_64 &&
187 regs->ip < (unsigned long)entry_SYSCALL_64_safe_stack);
188
189#ifdef CONFIG_IA32_EMULATION
190 ret = ret || (regs->ip >= (unsigned long)entry_SYSCALL_compat &&
191 regs->ip < (unsigned long)entry_SYSCALL_compat_safe_stack);
192#endif
193
194 return ret;
195}
Andy Lutomirski318f5a22011-08-03 09:31:53 -0400196#endif
197
Masami Hiramatsu7b6c6c72009-05-11 17:03:00 -0400198static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
Harvey Harrison90d43d72008-01-30 13:33:16 +0100199{
Harvey Harrison90d43d72008-01-30 13:33:16 +0100200 return regs->sp;
Harvey Harrison90d43d72008-01-30 13:33:16 +0100201}
202
Christoph Hellwig79f25622019-06-24 07:47:27 +0200203static inline unsigned long instruction_pointer(struct pt_regs *regs)
204{
205 return regs->ip;
206}
Harvey Harrison90d43d72008-01-30 13:33:16 +0100207
Christoph Hellwig79f25622019-06-24 07:47:27 +0200208static inline void instruction_pointer_set(struct pt_regs *regs,
209 unsigned long val)
210{
211 regs->ip = val;
212}
213
214static inline unsigned long frame_pointer(struct pt_regs *regs)
215{
216 return regs->bp;
217}
218
219static inline unsigned long user_stack_pointer(struct pt_regs *regs)
220{
221 return regs->sp;
222}
223
224static inline void user_stack_pointer_set(struct pt_regs *regs,
225 unsigned long val)
226{
227 regs->sp = val;
228}
Roland McGrath68bd0f42008-04-18 17:08:44 -0700229
Thomas Gleixner0bf019e2020-07-23 00:00:03 +0200230static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)
231{
232 return !(regs->flags & X86_EFLAGS_IF);
233}
234
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400235/* Query offset/name of register from its name/offset */
236extern int regs_query_register_offset(const char *name);
237extern const char *regs_query_register_name(unsigned int offset);
238#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
239
240/**
241 * regs_get_register() - get register value from its offset
242 * @regs: pt_regs from which register value is gotten.
243 * @offset: offset number of the register.
244 *
Masami Hiramatsue859cf82009-11-30 19:02:22 -0500245 * regs_get_register returns the value of a register. The @offset is the
246 * offset of the register in struct pt_regs address which specified by @regs.
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400247 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
248 */
249static inline unsigned long regs_get_register(struct pt_regs *regs,
250 unsigned int offset)
251{
252 if (unlikely(offset > MAX_REG_OFFSET))
253 return 0;
Steven Rostedt6c8d8b32012-07-13 15:44:14 -0400254#ifdef CONFIG_X86_32
Andy Lutomirski603e492e2017-08-09 14:39:45 -0700255 /* The selector fields are 16-bit. */
256 if (offset == offsetof(struct pt_regs, cs) ||
257 offset == offsetof(struct pt_regs, ss) ||
258 offset == offsetof(struct pt_regs, ds) ||
259 offset == offsetof(struct pt_regs, es) ||
260 offset == offsetof(struct pt_regs, fs) ||
261 offset == offsetof(struct pt_regs, gs)) {
262 return *(u16 *)((unsigned long)regs + offset);
263
264 }
Steven Rostedt6c8d8b32012-07-13 15:44:14 -0400265#endif
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400266 return *(unsigned long *)((unsigned long)regs + offset);
267}
268
269/**
270 * regs_within_kernel_stack() - check the address in the stack
271 * @regs: pt_regs which contains kernel stack pointer.
272 * @addr: address which is checked.
273 *
Masami Hiramatsue859cf82009-11-30 19:02:22 -0500274 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400275 * If @addr is within the kernel stack, it returns true. If not, returns false.
276 */
277static inline int regs_within_kernel_stack(struct pt_regs *regs,
278 unsigned long addr)
279{
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200280 return ((addr & ~(THREAD_SIZE - 1)) == (regs->sp & ~(THREAD_SIZE - 1)));
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400281}
282
283/**
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400284 * regs_get_kernel_stack_nth_addr() - get the address of the Nth entry on stack
285 * @regs: pt_regs which contains kernel stack pointer.
286 * @n: stack entry number.
287 *
288 * regs_get_kernel_stack_nth() returns the address of the @n th entry of the
289 * kernel stack which is specified by @regs. If the @n th entry is NOT in
290 * the kernel stack, this returns NULL.
291 */
292static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
293{
Peter Zijlstra3c88c692019-05-07 23:25:54 +0200294 unsigned long *addr = (unsigned long *)regs->sp;
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400295
296 addr += n;
297 if (regs_within_kernel_stack(regs, (unsigned long)addr))
298 return addr;
299 else
300 return NULL;
301}
302
303/* To avoid include hell, we can't include uaccess.h */
Christoph Hellwigfe557312020-06-17 09:37:53 +0200304extern long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400305
306/**
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400307 * regs_get_kernel_stack_nth() - get Nth entry of the stack
308 * @regs: pt_regs which contains kernel stack pointer.
309 * @n: stack entry number.
310 *
311 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400312 * is specified by @regs. If the @n th entry is NOT in the kernel stack
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400313 * this returns 0.
314 */
315static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
316 unsigned int n)
317{
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400318 unsigned long *addr;
319 unsigned long val;
320 long ret;
321
322 addr = regs_get_kernel_stack_nth_addr(regs, n);
323 if (addr) {
Christoph Hellwigfe557312020-06-17 09:37:53 +0200324 ret = copy_from_kernel_nofault(&val, addr, sizeof(val));
Steven Rostedt (VMware)c2712b82018-10-17 16:59:51 -0400325 if (!ret)
326 return val;
327 }
328 return 0;
Masami Hiramatsub1cf5402009-08-13 16:34:44 -0400329}
330
Masami Hiramatsu3c88ee194c2018-04-25 21:20:57 +0900331/**
332 * regs_get_kernel_argument() - get Nth function argument in kernel
333 * @regs: pt_regs of that context
334 * @n: function argument number (start from 0)
335 *
336 * regs_get_argument() returns @n th argument of the function call.
337 * Note that this chooses most probably assignment, in some case
338 * it can be incorrect.
339 * This is expected to be called from kprobes or ftrace with regs
340 * where the top of stack is the return address.
341 */
342static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
343 unsigned int n)
344{
345 static const unsigned int argument_offs[] = {
346#ifdef __i386__
347 offsetof(struct pt_regs, ax),
Masami Hiramatsu3c88ee194c2018-04-25 21:20:57 +0900348 offsetof(struct pt_regs, dx),
Vamshi K Sthambamkadi2356bb42020-08-28 17:02:46 +0530349 offsetof(struct pt_regs, cx),
Masami Hiramatsu3c88ee194c2018-04-25 21:20:57 +0900350#define NR_REG_ARGUMENTS 3
351#else
352 offsetof(struct pt_regs, di),
353 offsetof(struct pt_regs, si),
354 offsetof(struct pt_regs, dx),
355 offsetof(struct pt_regs, cx),
356 offsetof(struct pt_regs, r8),
357 offsetof(struct pt_regs, r9),
358#define NR_REG_ARGUMENTS 6
359#endif
360 };
361
362 if (n >= NR_REG_ARGUMENTS) {
363 n -= NR_REG_ARGUMENTS - 1;
364 return regs_get_kernel_stack_nth(regs, n);
365 } else
366 return regs_get_register(regs, argument_offs[n]);
367}
368
Roland McGrath7f232342008-01-30 13:30:48 +0100369#define arch_has_single_step() (1)
Roland McGrath10faa812008-01-30 13:30:54 +0100370#ifdef CONFIG_X86_DEBUGCTLMSR
371#define arch_has_block_step() (1)
372#else
373#define arch_has_block_step() (boot_cpu_data.x86 >= 6)
374#endif
375
Eric W. Biedermanefc463a2018-04-16 14:18:26 -0500376#define ARCH_HAS_USER_SINGLE_STEP_REPORT
Oleg Nesterov7f385512009-12-15 16:47:20 -0800377
Roland McGrathefd1ca52008-01-30 13:30:46 +0100378struct user_desc;
379extern int do_get_thread_area(struct task_struct *p, int idx,
380 struct user_desc __user *info);
381extern int do_set_thread_area(struct task_struct *p, int idx,
382 struct user_desc __user *info, int can_allocate);
383
Thomas Gleixner2fff0712019-11-11 23:03:16 +0100384#ifdef CONFIG_X86_64
385# define do_set_thread_area_64(p, s, t) do_arch_prctl_64(p, s, t)
386#else
387# define do_set_thread_area_64(p, s, t) (0)
388#endif
389
Thomas Gleixner8fc37f22007-10-23 22:37:24 +0200390#endif /* !__ASSEMBLY__ */
H. Peter Anvin1965aae2008-10-22 22:26:29 -0700391#endif /* _ASM_X86_PTRACE_H */