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