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