Linux Audio

Check our new training course

Loading...
v4.6
 
  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
 11struct pt_regs {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 12	unsigned long bx;
 13	unsigned long cx;
 14	unsigned long dx;
 15	unsigned long si;
 16	unsigned long di;
 17	unsigned long bp;
 18	unsigned long ax;
 19	unsigned long ds;
 20	unsigned long es;
 21	unsigned long fs;
 22	unsigned long gs;
 
 
 
 
 
 
 
 
 
 23	unsigned long orig_ax;
 24	unsigned long ip;
 25	unsigned long cs;
 
 26	unsigned long flags;
 27	unsigned long sp;
 28	unsigned long ss;
 
 29};
 30
 31#else /* __i386__ */
 32
 33struct pt_regs {
 34/*
 35 * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
 36 * unless syscall needs a complete, fully filled "struct pt_regs".
 37 */
 38	unsigned long r15;
 39	unsigned long r14;
 40	unsigned long r13;
 41	unsigned long r12;
 42	unsigned long bp;
 43	unsigned long bx;
 44/* These regs are callee-clobbered. Always saved on kernel entry. */
 45	unsigned long r11;
 46	unsigned long r10;
 47	unsigned long r9;
 48	unsigned long r8;
 49	unsigned long ax;
 50	unsigned long cx;
 51	unsigned long dx;
 52	unsigned long si;
 53	unsigned long di;
 54/*
 55 * On syscall entry, this is syscall#. On CPU exception, this is error code.
 56 * On hw interrupt, it's IRQ number:
 57 */
 58	unsigned long orig_ax;
 59/* Return frame for iretq */
 60	unsigned long ip;
 61	unsigned long cs;
 62	unsigned long flags;
 63	unsigned long sp;
 64	unsigned long ss;
 65/* top of stack page */
 66};
 67
 68#endif /* !__i386__ */
 69
 70#ifdef CONFIG_PARAVIRT
 71#include <asm/paravirt_types.h>
 72#endif
 73
 
 
 74struct cpuinfo_x86;
 75struct task_struct;
 76
 77extern unsigned long profile_pc(struct pt_regs *regs);
 78#define profile_pc profile_pc
 79
 80extern unsigned long
 81convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
 82extern void send_sigtrap(struct task_struct *tsk, struct pt_regs *regs,
 83			 int error_code, int si_code);
 84
 85
 86extern unsigned long syscall_trace_enter_phase1(struct pt_regs *, u32 arch);
 87extern long syscall_trace_enter_phase2(struct pt_regs *, u32 arch,
 88				       unsigned long phase1_result);
 89
 90extern long syscall_trace_enter(struct pt_regs *);
 91
 92static inline unsigned long regs_return_value(struct pt_regs *regs)
 93{
 94	return regs->ax;
 95}
 96
 
 
 
 
 
 97/*
 98 * user_mode(regs) determines whether a register set came from user
 99 * mode.  On x86_32, this is true if V8086 mode was enabled OR if the
100 * register set was from protected mode with RPL-3 CS value.  This
101 * tricky test checks that with one comparison.
102 *
103 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
104 * the extra check.
105 */
106static inline int user_mode(struct pt_regs *regs)
107{
108#ifdef CONFIG_X86_32
109	return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
110#else
111	return !!(regs->cs & 3);
112#endif
113}
114
115static inline int v8086_mode(struct pt_regs *regs)
116{
117#ifdef CONFIG_X86_32
118	return (regs->flags & X86_VM_MASK);
119#else
120	return 0;	/* No V86 mode support in long mode */
121#endif
122}
123
124#ifdef CONFIG_X86_64
125static inline bool user_64bit_mode(struct pt_regs *regs)
126{
127#ifndef CONFIG_PARAVIRT
 
128	/*
129	 * On non-paravirt systems, this is the only long mode CPL 3
130	 * selector.  We do not allow long mode selectors in the LDT.
131	 */
132	return regs->cs == __USER_CS;
133#else
134	/* Headers are too twisted for this to go in paravirt.h. */
135	return regs->cs == __USER_CS || regs->cs == pv_info.extra_user_64bit_cs;
136#endif
 
 
 
137}
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139#define current_user_stack_pointer()	current_pt_regs()->sp
140#define compat_user_stack_pointer()	current_pt_regs()->sp
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141#endif
142
143#ifdef CONFIG_X86_32
144extern unsigned long kernel_stack_pointer(struct pt_regs *regs);
145#else
146static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
147{
148	return regs->sp;
149}
150#endif
151
152#define GET_IP(regs) ((regs)->ip)
153#define GET_FP(regs) ((regs)->bp)
154#define GET_USP(regs) ((regs)->sp)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
156#include <asm-generic/ptrace.h>
 
 
 
157
158/* Query offset/name of register from its name/offset */
159extern int regs_query_register_offset(const char *name);
160extern const char *regs_query_register_name(unsigned int offset);
161#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
162
163/**
164 * regs_get_register() - get register value from its offset
165 * @regs:	pt_regs from which register value is gotten.
166 * @offset:	offset number of the register.
167 *
168 * regs_get_register returns the value of a register. The @offset is the
169 * offset of the register in struct pt_regs address which specified by @regs.
170 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
171 */
172static inline unsigned long regs_get_register(struct pt_regs *regs,
173					      unsigned int offset)
174{
175	if (unlikely(offset > MAX_REG_OFFSET))
176		return 0;
177#ifdef CONFIG_X86_32
178	/*
179	 * Traps from the kernel do not save sp and ss.
180	 * Use the helper function to retrieve sp.
181	 */
182	if (offset == offsetof(struct pt_regs, sp) &&
183	    regs->cs == __KERNEL_CS)
184		return kernel_stack_pointer(regs);
 
 
 
185#endif
186	return *(unsigned long *)((unsigned long)regs + offset);
187}
188
189/**
190 * regs_within_kernel_stack() - check the address in the stack
191 * @regs:	pt_regs which contains kernel stack pointer.
192 * @addr:	address which is checked.
193 *
194 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
195 * If @addr is within the kernel stack, it returns true. If not, returns false.
196 */
197static inline int regs_within_kernel_stack(struct pt_regs *regs,
198					   unsigned long addr)
199{
200	return ((addr & ~(THREAD_SIZE - 1))  ==
201		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
202}
203
204/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205 * regs_get_kernel_stack_nth() - get Nth entry of the stack
206 * @regs:	pt_regs which contains kernel stack pointer.
207 * @n:		stack entry number.
208 *
209 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
210 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
211 * this returns 0.
212 */
213static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
214						      unsigned int n)
215{
216	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
217	addr += n;
218	if (regs_within_kernel_stack(regs, (unsigned long)addr))
219		return *addr;
220	else
221		return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222}
223
224#define arch_has_single_step()	(1)
225#ifdef CONFIG_X86_DEBUGCTLMSR
226#define arch_has_block_step()	(1)
227#else
228#define arch_has_block_step()	(boot_cpu_data.x86 >= 6)
229#endif
230
231#define ARCH_HAS_USER_SINGLE_STEP_INFO
232
233/*
234 * When hitting ptrace_stop(), we cannot return using SYSRET because
235 * that does not restore the full CPU state, only a minimal set.  The
236 * ptracer can change arbitrary register values, which is usually okay
237 * because the usual ptrace stops run off the signal delivery path which
238 * forces IRET; however, ptrace_event() stops happen in arbitrary places
239 * in the kernel and don't force IRET path.
240 *
241 * So force IRET path after a ptrace stop.
242 */
243#define arch_ptrace_stop_needed(code, info)				\
244({									\
245	force_iret();							\
246	false;								\
247})
248
249struct user_desc;
250extern int do_get_thread_area(struct task_struct *p, int idx,
251			      struct user_desc __user *info);
252extern int do_set_thread_area(struct task_struct *p, int idx,
253			      struct user_desc __user *info, int can_allocate);
 
 
 
 
 
 
254
255#endif /* !__ASSEMBLY__ */
256#endif /* _ASM_X86_PTRACE_H */
v6.8
  1/* SPDX-License-Identifier: GPL-2.0 */
  2#ifndef _ASM_X86_PTRACE_H
  3#define _ASM_X86_PTRACE_H
  4
  5#include <asm/segment.h>
  6#include <asm/page_types.h>
  7#include <uapi/asm/ptrace.h>
  8
  9#ifndef __ASSEMBLY__
 10#ifdef __i386__
 11
 12struct pt_regs {
 13	/*
 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	 */
 27	unsigned long bx;
 28	unsigned long cx;
 29	unsigned long dx;
 30	unsigned long si;
 31	unsigned long di;
 32	unsigned long bp;
 33	unsigned long ax;
 34	unsigned short ds;
 35	unsigned short __dsh;
 36	unsigned short es;
 37	unsigned short __esh;
 38	unsigned short fs;
 39	unsigned short __fsh;
 40	/*
 41	 * On interrupt, gs and __gsh store the vector number.  They never
 42	 * store gs any more.
 43	 */
 44	unsigned short gs;
 45	unsigned short __gsh;
 46	/* On interrupt, this is the error code. */
 47	unsigned long orig_ax;
 48	unsigned long ip;
 49	unsigned short cs;
 50	unsigned short __csh;
 51	unsigned long flags;
 52	unsigned long sp;
 53	unsigned short ss;
 54	unsigned short __ssh;
 55};
 56
 57#else /* __i386__ */
 58
 59struct pt_regs {
 60/*
 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 */
 64	unsigned long r15;
 65	unsigned long r14;
 66	unsigned long r13;
 67	unsigned long r12;
 68	unsigned long bp;
 69	unsigned long bx;
 70/* These regs are callee-clobbered. Always saved on kernel entry. */
 71	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;
 80/*
 81 * On syscall entry, this is syscall#. On CPU exception, this is error code.
 82 * On hw interrupt, it's IRQ number:
 83 */
 84	unsigned long orig_ax;
 85/* Return frame for iretq */
 86	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};
 93
 94#endif /* !__i386__ */
 95
 96#ifdef CONFIG_PARAVIRT
 97#include <asm/paravirt_types.h>
 98#endif
 99
100#include <asm/proto.h>
101
102struct cpuinfo_x86;
103struct task_struct;
104
105extern unsigned long profile_pc(struct pt_regs *regs);
 
106
107extern unsigned long
108convert_ip_to_linear(struct task_struct *child, struct pt_regs *regs);
109extern void send_sigtrap(struct pt_regs *regs, int error_code, int si_code);
 
 
 
 
 
 
110
 
111
112static inline unsigned long regs_return_value(struct pt_regs *regs)
113{
114	return regs->ax;
115}
116
117static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
118{
119	regs->ax = rc;
120}
121
122/*
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.
127 *
128 * On x86_64, vm86 mode is mercifully nonexistent, and we don't need
129 * the extra check.
130 */
131static __always_inline int user_mode(struct pt_regs *regs)
132{
133#ifdef CONFIG_X86_32
134	return ((regs->cs & SEGMENT_RPL_MASK) | (regs->flags & X86_VM_MASK)) >= USER_RPL;
135#else
136	return !!(regs->cs & 3);
137#endif
138}
139
140static __always_inline int v8086_mode(struct pt_regs *regs)
141{
142#ifdef CONFIG_X86_32
143	return (regs->flags & X86_VM_MASK);
144#else
145	return 0;	/* No V86 mode support in long mode */
146#endif
147}
148
 
149static inline bool user_64bit_mode(struct pt_regs *regs)
150{
151#ifdef CONFIG_X86_64
152#ifndef CONFIG_PARAVIRT_XXL
153	/*
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
162#else /* !CONFIG_X86_64 */
163	return false;
164#endif
165}
166
167/*
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
180#ifdef CONFIG_X86_64
181#define current_user_stack_pointer()	current_pt_regs()->sp
182#define compat_user_stack_pointer()	current_pt_regs()->sp
183
184static __always_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	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);
196#endif
197
198	return ret;
199}
200#endif
201
 
 
 
202static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
203{
204	return regs->sp;
205}
 
206
207static inline unsigned long instruction_pointer(struct pt_regs *regs)
208{
209	return regs->ip;
210}
211
212static inline void instruction_pointer_set(struct pt_regs *regs,
213		unsigned long val)
214{
215	regs->ip = val;
216}
217
218static inline unsigned long frame_pointer(struct pt_regs *regs)
219{
220	return regs->bp;
221}
222
223static inline unsigned long user_stack_pointer(struct pt_regs *regs)
224{
225	return regs->sp;
226}
227
228static inline void user_stack_pointer_set(struct pt_regs *regs,
229		unsigned long val)
230{
231	regs->sp = val;
232}
233
234static __always_inline bool regs_irqs_disabled(struct pt_regs *regs)
235{
236	return !(regs->flags & X86_EFLAGS_IF);
237}
238
239/* Query offset/name of register from its name/offset */
240extern int regs_query_register_offset(const char *name);
241extern const char *regs_query_register_name(unsigned int offset);
242#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
243
244/**
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.
248 *
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.
252 */
253static inline unsigned long regs_get_register(struct pt_regs *regs,
254					      unsigned int offset)
255{
256	if (unlikely(offset > MAX_REG_OFFSET))
257		return 0;
258#ifdef CONFIG_X86_32
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);
267
268	}
269#endif
270	return *(unsigned long *)((unsigned long)regs + offset);
271}
272
273/**
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.
277 *
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.
280 */
281static inline int regs_within_kernel_stack(struct pt_regs *regs,
282					   unsigned long addr)
283{
284	return ((addr & ~(THREAD_SIZE - 1)) == (regs->sp & ~(THREAD_SIZE - 1)));
 
285}
286
287/**
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.
291 *
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.
295 */
296static inline unsigned long *regs_get_kernel_stack_nth_addr(struct pt_regs *regs, unsigned int n)
297{
298	unsigned long *addr = (unsigned long *)regs->sp;
299
300	addr += n;
301	if (regs_within_kernel_stack(regs, (unsigned long)addr))
302		return addr;
303	else
304		return NULL;
305}
306
307/* To avoid include hell, we can't include uaccess.h */
308extern long copy_from_kernel_nofault(void *dst, const void *src, size_t size);
309
310/**
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.
314 *
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
317 * this returns 0.
318 */
319static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
320						      unsigned int n)
321{
322	unsigned long *addr;
323	unsigned long val;
324	long ret;
325
326	addr = regs_get_kernel_stack_nth_addr(regs, n);
327	if (addr) {
328		ret = copy_from_kernel_nofault(&val, addr, sizeof(val));
329		if (!ret)
330			return val;
331	}
332	return 0;
333}
334
335/**
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)
339 *
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.
345 */
346static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
347						     unsigned int n)
348{
349	static const unsigned int argument_offs[] = {
350#ifdef __i386__
351		offsetof(struct pt_regs, ax),
352		offsetof(struct pt_regs, dx),
353		offsetof(struct pt_regs, cx),
354#define NR_REG_ARGUMENTS 3
355#else
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
363#endif
364	};
365
366	if (n >= NR_REG_ARGUMENTS) {
367		n -= NR_REG_ARGUMENTS - 1;
368		return regs_get_kernel_stack_nth(regs, n);
369	} else
370		return regs_get_register(regs, argument_offs[n]);
371}
372
373#define arch_has_single_step()	(1)
374#ifdef CONFIG_X86_DEBUGCTLMSR
375#define arch_has_block_step()	(1)
376#else
377#define arch_has_block_step()	(boot_cpu_data.x86 >= 6)
378#endif
379
380#define ARCH_HAS_USER_SINGLE_STEP_REPORT
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
381
382struct user_desc;
383extern int do_get_thread_area(struct task_struct *p, int idx,
384			      struct user_desc __user *info);
385extern int do_set_thread_area(struct task_struct *p, int idx,
386			      struct user_desc __user *info, int can_allocate);
387
388#ifdef CONFIG_X86_64
389# define do_set_thread_area_64(p, s, t)	do_arch_prctl_64(p, s, t)
390#else
391# define do_set_thread_area_64(p, s, t)	(0)
392#endif
393
394#endif /* !__ASSEMBLY__ */
395#endif /* _ASM_X86_PTRACE_H */