xref: /linux/arch/powerpc/include/asm/ptrace.h (revision 4872cbd0ca35ca5b20d52e2539e7e1950f126e7b)
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 <linux/err.h>
23 #include <uapi/asm/ptrace.h>
24 #include <asm/asm-const.h>
25 
26 #ifndef __ASSEMBLY__
27 struct pt_regs
28 {
29 	union {
30 		struct user_pt_regs user_regs;
31 		struct {
32 			unsigned long gpr[32];
33 			unsigned long nip;
34 			unsigned long msr;
35 			unsigned long orig_gpr3;
36 			unsigned long ctr;
37 			unsigned long link;
38 			unsigned long xer;
39 			unsigned long ccr;
40 #ifdef CONFIG_PPC64
41 			unsigned long softe;
42 #else
43 			unsigned long mq;
44 #endif
45 			unsigned long trap;
46 			union {
47 				unsigned long dar;
48 				unsigned long dear;
49 			};
50 			union {
51 				unsigned long dsisr;
52 				unsigned long esr;
53 			};
54 			unsigned long result;
55 		};
56 	};
57 #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)
58 	union {
59 		struct {
60 #ifdef CONFIG_PPC64
61 			unsigned long ppr;
62 			unsigned long exit_result;
63 #endif
64 			union {
65 #ifdef CONFIG_PPC_KUAP
66 				unsigned long kuap;
67 #endif
68 #ifdef CONFIG_PPC_PKEY
69 				unsigned long amr;
70 #endif
71 			};
72 #ifdef CONFIG_PPC_PKEY
73 			unsigned long iamr;
74 #endif
75 		};
76 		unsigned long __pad[4];	/* Maintain 16 byte interrupt stack alignment */
77 	};
78 #endif
79 };
80 #endif
81 
82 
83 #define STACK_FRAME_WITH_PT_REGS (STACK_FRAME_OVERHEAD + sizeof(struct pt_regs))
84 
85 #ifdef __powerpc64__
86 
87 /*
88  * Size of redzone that userspace is allowed to use below the stack
89  * pointer.  This is 288 in the 64-bit big-endian ELF ABI, and 512 in
90  * the new ELFv2 little-endian ABI, so we allow the larger amount.
91  *
92  * For kernel code we allow a 288-byte redzone, in order to conserve
93  * kernel stack space; gcc currently only uses 288 bytes, and will
94  * hopefully allow explicit control of the redzone size in future.
95  */
96 #define USER_REDZONE_SIZE	512
97 #define KERNEL_REDZONE_SIZE	288
98 
99 #define STACK_FRAME_OVERHEAD	112	/* size of minimum stack frame */
100 #define STACK_FRAME_LR_SAVE	2	/* Location of LR in stack frame */
101 #define STACK_FRAME_REGS_MARKER	ASM_CONST(0x7265677368657265)
102 #define STACK_INT_FRAME_SIZE	(sizeof(struct pt_regs) + \
103 				 STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)
104 #define STACK_FRAME_MARKER	12
105 
106 #ifdef PPC64_ELF_ABI_v2
107 #define STACK_FRAME_MIN_SIZE	32
108 #else
109 #define STACK_FRAME_MIN_SIZE	STACK_FRAME_OVERHEAD
110 #endif
111 
112 /* Size of dummy stack frame allocated when calling signal handler. */
113 #define __SIGNAL_FRAMESIZE	128
114 #define __SIGNAL_FRAMESIZE32	64
115 
116 #else /* __powerpc64__ */
117 
118 #define USER_REDZONE_SIZE	0
119 #define KERNEL_REDZONE_SIZE	0
120 #define STACK_FRAME_OVERHEAD	16	/* size of minimum stack frame */
121 #define STACK_FRAME_LR_SAVE	1	/* Location of LR in stack frame */
122 #define STACK_FRAME_REGS_MARKER	ASM_CONST(0x72656773)
123 #define STACK_INT_FRAME_SIZE	(sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
124 #define STACK_FRAME_MARKER	2
125 #define STACK_FRAME_MIN_SIZE	STACK_FRAME_OVERHEAD
126 
127 /* Size of stack frame allocated when calling signal handler. */
128 #define __SIGNAL_FRAMESIZE	64
129 
130 #endif /* __powerpc64__ */
131 
132 #ifndef __ASSEMBLY__
133 #include <asm/paca.h>
134 
135 #ifdef CONFIG_SMP
136 extern unsigned long profile_pc(struct pt_regs *regs);
137 #else
138 #define profile_pc(regs) instruction_pointer(regs)
139 #endif
140 
141 long do_syscall_trace_enter(struct pt_regs *regs);
142 void do_syscall_trace_leave(struct pt_regs *regs);
143 
144 static inline void set_return_regs_changed(void)
145 {
146 #ifdef CONFIG_PPC_BOOK3S_64
147 	local_paca->hsrr_valid = 0;
148 	local_paca->srr_valid = 0;
149 #endif
150 }
151 
152 static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
153 {
154 	regs->nip = ip;
155 	set_return_regs_changed();
156 }
157 
158 static inline void regs_set_return_msr(struct pt_regs *regs, unsigned long msr)
159 {
160 	regs->msr = msr;
161 	set_return_regs_changed();
162 }
163 
164 static inline void regs_add_return_ip(struct pt_regs *regs, long offset)
165 {
166 	regs_set_return_ip(regs, regs->nip + offset);
167 }
168 
169 static inline unsigned long instruction_pointer(struct pt_regs *regs)
170 {
171 	return regs->nip;
172 }
173 
174 static inline void instruction_pointer_set(struct pt_regs *regs,
175 		unsigned long val)
176 {
177 	regs_set_return_ip(regs, val);
178 }
179 
180 static inline unsigned long user_stack_pointer(struct pt_regs *regs)
181 {
182 	return regs->gpr[1];
183 }
184 
185 static inline unsigned long frame_pointer(struct pt_regs *regs)
186 {
187 	return 0;
188 }
189 
190 #define user_mode(regs) (((regs)->msr & MSR_PR) != 0)
191 
192 #define force_successful_syscall_return()   \
193 	do { \
194 		set_thread_flag(TIF_NOERROR); \
195 	} while(0)
196 
197 #define current_pt_regs() \
198 	((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1)
199 
200 /*
201  * The 4 low bits (0xf) are available as flags to overload the trap word,
202  * because interrupt vectors have minimum alignment of 0x10. TRAP_FLAGS_MASK
203  * must cover the bits used as flags, including bit 0 which is used as the
204  * "norestart" bit.
205  */
206 #ifdef __powerpc64__
207 #define TRAP_FLAGS_MASK		0x1
208 #else
209 /*
210  * On 4xx we use bit 1 in the trap word to indicate whether the exception
211  * is a critical exception (1 means it is).
212  */
213 #define TRAP_FLAGS_MASK		0xf
214 #define IS_CRITICAL_EXC(regs)	(((regs)->trap & 2) != 0)
215 #define IS_MCHECK_EXC(regs)	(((regs)->trap & 4) != 0)
216 #define IS_DEBUG_EXC(regs)	(((regs)->trap & 8) != 0)
217 #endif /* __powerpc64__ */
218 #define TRAP(regs)		((regs)->trap & ~TRAP_FLAGS_MASK)
219 
220 static __always_inline void set_trap(struct pt_regs *regs, unsigned long val)
221 {
222 	regs->trap = (regs->trap & TRAP_FLAGS_MASK) | (val & ~TRAP_FLAGS_MASK);
223 }
224 
225 static inline bool trap_is_scv(struct pt_regs *regs)
226 {
227 	return (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x3000);
228 }
229 
230 static inline bool trap_is_unsupported_scv(struct pt_regs *regs)
231 {
232 	return IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x7ff0;
233 }
234 
235 static inline bool trap_is_syscall(struct pt_regs *regs)
236 {
237 	return (trap_is_scv(regs) || TRAP(regs) == 0xc00);
238 }
239 
240 static inline bool trap_norestart(struct pt_regs *regs)
241 {
242 	return regs->trap & 0x1;
243 }
244 
245 static __always_inline void set_trap_norestart(struct pt_regs *regs)
246 {
247 	regs->trap |= 0x1;
248 }
249 
250 #define kernel_stack_pointer(regs) ((regs)->gpr[1])
251 static inline int is_syscall_success(struct pt_regs *regs)
252 {
253 	if (trap_is_scv(regs))
254 		return !IS_ERR_VALUE((unsigned long)regs->gpr[3]);
255 	else
256 		return !(regs->ccr & 0x10000000);
257 }
258 
259 static inline long regs_return_value(struct pt_regs *regs)
260 {
261 	if (trap_is_scv(regs))
262 		return regs->gpr[3];
263 
264 	if (is_syscall_success(regs))
265 		return regs->gpr[3];
266 	else
267 		return -regs->gpr[3];
268 }
269 
270 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
271 {
272 	regs->gpr[3] = rc;
273 }
274 
275 #define arch_has_single_step()	(1)
276 #define arch_has_block_step()	(true)
277 #define ARCH_HAS_USER_SINGLE_STEP_REPORT
278 
279 /*
280  * kprobe-based event tracer support
281  */
282 
283 #include <linux/stddef.h>
284 #include <linux/thread_info.h>
285 extern int regs_query_register_offset(const char *name);
286 extern const char *regs_query_register_name(unsigned int offset);
287 #define MAX_REG_OFFSET (offsetof(struct pt_regs, dsisr))
288 
289 /**
290  * regs_get_register() - get register value from its offset
291  * @regs:	   pt_regs from which register value is gotten
292  * @offset:    offset number of the register.
293  *
294  * regs_get_register returns the value of a register whose offset from @regs.
295  * The @offset is the offset of the register in struct pt_regs.
296  * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
297  */
298 static inline unsigned long regs_get_register(struct pt_regs *regs,
299 						unsigned int offset)
300 {
301 	if (unlikely(offset > MAX_REG_OFFSET))
302 		return 0;
303 	return *(unsigned long *)((unsigned long)regs + offset);
304 }
305 
306 /**
307  * regs_within_kernel_stack() - check the address in the stack
308  * @regs:      pt_regs which contains kernel stack pointer.
309  * @addr:      address which is checked.
310  *
311  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
312  * If @addr is within the kernel stack, it returns true. If not, returns false.
313  */
314 
315 static inline bool regs_within_kernel_stack(struct pt_regs *regs,
316 						unsigned long addr)
317 {
318 	return ((addr & ~(THREAD_SIZE - 1))  ==
319 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
320 }
321 
322 /**
323  * regs_get_kernel_stack_nth() - get Nth entry of the stack
324  * @regs:	pt_regs which contains kernel stack pointer.
325  * @n:		stack entry number.
326  *
327  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
328  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
329  * this returns 0.
330  */
331 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
332 						      unsigned int n)
333 {
334 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
335 	addr += n;
336 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
337 		return *addr;
338 	else
339 		return 0;
340 }
341 
342 #endif /* __ASSEMBLY__ */
343 
344 #ifndef __powerpc64__
345 /* We need PT_SOFTE defined at all time to avoid #ifdefs */
346 #define PT_SOFTE PT_MQ
347 #else /* __powerpc64__ */
348 #define PT_FPSCR32 (PT_FPR0 + 2*32 + 1)	/* each FP reg occupies 2 32-bit userspace slots */
349 #define PT_VR0_32 164	/* each Vector reg occupies 4 slots in 32-bit */
350 #define PT_VSCR_32 (PT_VR0 + 32*4 + 3)
351 #define PT_VRSAVE_32 (PT_VR0 + 33*4)
352 #define PT_VSR0_32 300 	/* each VSR reg occupies 4 slots in 32-bit */
353 #endif /* __powerpc64__ */
354 #endif /* _ASM_POWERPC_PTRACE_H */
355