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