xref: /openbmc/linux/arch/powerpc/include/asm/ptrace.h (revision ac8b6f14)
1 /*
2  * Copyright (C) 2001 PPC64 Team, IBM Corp
3  *
4  * This struct defines the way the registers are stored on the
5  * kernel stack during a system call or other kernel entry.
6  *
7  * this should only contain volatile regs
8  * since we can keep non-volatile in the thread_struct
9  * should set this up when only volatiles are saved
10  * by intr code.
11  *
12  * Since this is going on the stack, *CARE MUST BE TAKEN* to insure
13  * that the overall structure is a multiple of 16 bytes in length.
14  *
15  * Note that the offsets of the fields in this struct correspond with
16  * the PT_* values below.  This simplifies arch/powerpc/kernel/ptrace.c.
17  *
18  * This program is free software; you can redistribute it and/or
19  * modify it under the terms of the GNU General Public License
20  * as published by the Free Software Foundation; either version
21  * 2 of the License, or (at your option) any later version.
22  */
23 #ifndef _ASM_POWERPC_PTRACE_H
24 #define _ASM_POWERPC_PTRACE_H
25 
26 #include <uapi/asm/ptrace.h>
27 #include <asm/asm-const.h>
28 
29 #ifndef __ASSEMBLY__
30 struct pt_regs
31 {
32 	union {
33 		struct user_pt_regs user_regs;
34 		struct {
35 			unsigned long gpr[32];
36 			unsigned long nip;
37 			unsigned long msr;
38 			unsigned long orig_gpr3;
39 			unsigned long ctr;
40 			unsigned long link;
41 			unsigned long xer;
42 			unsigned long ccr;
43 #ifdef CONFIG_PPC64
44 			unsigned long softe;
45 #else
46 			unsigned long mq;
47 #endif
48 			unsigned long trap;
49 			unsigned long dar;
50 			unsigned long dsisr;
51 			unsigned long result;
52 		};
53 	};
54 
55 #ifdef CONFIG_PPC64
56 	unsigned long ppr;
57 	unsigned long __pad;	/* Maintain 16 byte interrupt stack alignment */
58 #endif
59 };
60 #endif
61 
62 #ifdef __powerpc64__
63 
64 /*
65  * Size of redzone that userspace is allowed to use below the stack
66  * pointer.  This is 288 in the 64-bit big-endian ELF ABI, and 512 in
67  * the new ELFv2 little-endian ABI, so we allow the larger amount.
68  *
69  * For kernel code we allow a 288-byte redzone, in order to conserve
70  * kernel stack space; gcc currently only uses 288 bytes, and will
71  * hopefully allow explicit control of the redzone size in future.
72  */
73 #define USER_REDZONE_SIZE	512
74 #define KERNEL_REDZONE_SIZE	288
75 
76 #define STACK_FRAME_OVERHEAD	112	/* size of minimum stack frame */
77 #define STACK_FRAME_LR_SAVE	2	/* Location of LR in stack frame */
78 #define STACK_FRAME_REGS_MARKER	ASM_CONST(0x7265677368657265)
79 #define STACK_INT_FRAME_SIZE	(sizeof(struct pt_regs) + \
80 				 STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)
81 #define STACK_FRAME_MARKER	12
82 
83 #ifdef PPC64_ELF_ABI_v2
84 #define STACK_FRAME_MIN_SIZE	32
85 #else
86 #define STACK_FRAME_MIN_SIZE	STACK_FRAME_OVERHEAD
87 #endif
88 
89 /* Size of dummy stack frame allocated when calling signal handler. */
90 #define __SIGNAL_FRAMESIZE	128
91 #define __SIGNAL_FRAMESIZE32	64
92 
93 #else /* __powerpc64__ */
94 
95 #define USER_REDZONE_SIZE	0
96 #define KERNEL_REDZONE_SIZE	0
97 #define STACK_FRAME_OVERHEAD	16	/* size of minimum stack frame */
98 #define STACK_FRAME_LR_SAVE	1	/* Location of LR in stack frame */
99 #define STACK_FRAME_REGS_MARKER	ASM_CONST(0x72656773)
100 #define STACK_INT_FRAME_SIZE	(sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
101 #define STACK_FRAME_MARKER	2
102 #define STACK_FRAME_MIN_SIZE	STACK_FRAME_OVERHEAD
103 
104 /* Size of stack frame allocated when calling signal handler. */
105 #define __SIGNAL_FRAMESIZE	64
106 
107 #endif /* __powerpc64__ */
108 
109 #ifndef __ASSEMBLY__
110 
111 #define GET_IP(regs)		((regs)->nip)
112 #define GET_USP(regs)		((regs)->gpr[1])
113 #define GET_FP(regs)		(0)
114 #define SET_FP(regs, val)
115 
116 #ifdef CONFIG_SMP
117 extern unsigned long profile_pc(struct pt_regs *regs);
118 #define profile_pc profile_pc
119 #endif
120 
121 #include <asm-generic/ptrace.h>
122 
123 #define kernel_stack_pointer(regs) ((regs)->gpr[1])
124 static inline int is_syscall_success(struct pt_regs *regs)
125 {
126 	return !(regs->ccr & 0x10000000);
127 }
128 
129 static inline long regs_return_value(struct pt_regs *regs)
130 {
131 	if (is_syscall_success(regs))
132 		return regs->gpr[3];
133 	else
134 		return -regs->gpr[3];
135 }
136 
137 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
138 {
139 	regs->gpr[3] = rc;
140 }
141 
142 #ifdef __powerpc64__
143 #define user_mode(regs) ((((regs)->msr) >> MSR_PR_LG) & 0x1)
144 #else
145 #define user_mode(regs) (((regs)->msr & MSR_PR) != 0)
146 #endif
147 
148 #define force_successful_syscall_return()   \
149 	do { \
150 		set_thread_flag(TIF_NOERROR); \
151 	} while(0)
152 
153 struct task_struct;
154 extern int ptrace_get_reg(struct task_struct *task, int regno,
155 			  unsigned long *data);
156 extern int ptrace_put_reg(struct task_struct *task, int regno,
157 			  unsigned long data);
158 
159 #define current_pt_regs() \
160 	((struct pt_regs *)((unsigned long)current_thread_info() + THREAD_SIZE) - 1)
161 /*
162  * We use the least-significant bit of the trap field to indicate
163  * whether we have saved the full set of registers, or only a
164  * partial set.  A 1 there means the partial set.
165  * On 4xx we use the next bit to indicate whether the exception
166  * is a critical exception (1 means it is).
167  */
168 #define FULL_REGS(regs)		(((regs)->trap & 1) == 0)
169 #ifndef __powerpc64__
170 #define IS_CRITICAL_EXC(regs)	(((regs)->trap & 2) != 0)
171 #define IS_MCHECK_EXC(regs)	(((regs)->trap & 4) != 0)
172 #define IS_DEBUG_EXC(regs)	(((regs)->trap & 8) != 0)
173 #endif /* ! __powerpc64__ */
174 #define TRAP(regs)		((regs)->trap & ~0xF)
175 #ifdef __powerpc64__
176 #define NV_REG_POISON		0xdeadbeefdeadbeefUL
177 #define CHECK_FULL_REGS(regs)	BUG_ON(regs->trap & 1)
178 #else
179 #define NV_REG_POISON		0xdeadbeef
180 #define CHECK_FULL_REGS(regs)						      \
181 do {									      \
182 	if ((regs)->trap & 1)						      \
183 		printk(KERN_CRIT "%s: partial register set\n", __func__); \
184 } while (0)
185 #endif /* __powerpc64__ */
186 
187 #define arch_has_single_step()	(1)
188 #define arch_has_block_step()	(!cpu_has_feature(CPU_FTR_601))
189 #define ARCH_HAS_USER_SINGLE_STEP_REPORT
190 
191 /*
192  * kprobe-based event tracer support
193  */
194 
195 #include <linux/stddef.h>
196 #include <linux/thread_info.h>
197 extern int regs_query_register_offset(const char *name);
198 extern const char *regs_query_register_name(unsigned int offset);
199 #define MAX_REG_OFFSET (offsetof(struct pt_regs, dsisr))
200 
201 /**
202  * regs_get_register() - get register value from its offset
203  * @regs:	   pt_regs from which register value is gotten
204  * @offset:    offset number of the register.
205  *
206  * regs_get_register returns the value of a register whose offset from @regs.
207  * The @offset is the offset of the register in struct pt_regs.
208  * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
209  */
210 static inline unsigned long regs_get_register(struct pt_regs *regs,
211 						unsigned int offset)
212 {
213 	if (unlikely(offset > MAX_REG_OFFSET))
214 		return 0;
215 	return *(unsigned long *)((unsigned long)regs + offset);
216 }
217 
218 /**
219  * regs_within_kernel_stack() - check the address in the stack
220  * @regs:      pt_regs which contains kernel stack pointer.
221  * @addr:      address which is checked.
222  *
223  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
224  * If @addr is within the kernel stack, it returns true. If not, returns false.
225  */
226 
227 static inline bool regs_within_kernel_stack(struct pt_regs *regs,
228 						unsigned long addr)
229 {
230 	return ((addr & ~(THREAD_SIZE - 1))  ==
231 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
232 }
233 
234 /**
235  * regs_get_kernel_stack_nth() - get Nth entry of the stack
236  * @regs:	pt_regs which contains kernel stack pointer.
237  * @n:		stack entry number.
238  *
239  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
240  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
241  * this returns 0.
242  */
243 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
244 						      unsigned int n)
245 {
246 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
247 	addr += n;
248 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
249 		return *addr;
250 	else
251 		return 0;
252 }
253 
254 #endif /* __ASSEMBLY__ */
255 
256 #ifndef __powerpc64__
257 #else /* __powerpc64__ */
258 #define PT_FPSCR32 (PT_FPR0 + 2*32 + 1)	/* each FP reg occupies 2 32-bit userspace slots */
259 #define PT_VR0_32 164	/* each Vector reg occupies 4 slots in 32-bit */
260 #define PT_VSCR_32 (PT_VR0 + 32*4 + 3)
261 #define PT_VRSAVE_32 (PT_VR0 + 33*4)
262 #define PT_VSR0_32 300 	/* each VSR reg occupies 4 slots in 32-bit */
263 #endif /* __powerpc64__ */
264 #endif /* _ASM_POWERPC_PTRACE_H */
265