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