Merge existing fixes from spi/for-6.1 into new branch
[linux-2.6-block.git] / arch / powerpc / include / asm / ptrace.h
CommitLineData
2874c5fd 1/* SPDX-License-Identifier: GPL-2.0-or-later */
1da177e4
LT
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
da80d460 17 * the PT_* values below. This simplifies arch/powerpc/kernel/ptrace.c.
1da177e4 18 */
c3617f72
DH
19#ifndef _ASM_POWERPC_PTRACE_H
20#define _ASM_POWERPC_PTRACE_H
1da177e4 21
d72500f9 22#include <linux/err.h>
c3617f72 23#include <uapi/asm/ptrace.h>
ec0c464c 24#include <asm/asm-const.h>
806c0e6e 25#include <asm/reg.h>
1da177e4 26
002af939
ME
27#ifndef __ASSEMBLY__
28struct pt_regs
29{
30 union {
31 struct user_pt_regs user_regs;
32 struct {
33 unsigned long gpr[32];
34 unsigned long nip;
35 unsigned long msr;
36 unsigned long orig_gpr3;
37 unsigned long ctr;
38 unsigned long link;
39 unsigned long xer;
40 unsigned long ccr;
41#ifdef CONFIG_PPC64
42 unsigned long softe;
43#else
44 unsigned long mq;
45#endif
46 unsigned long trap;
4872cbd0
XS
47 union {
48 unsigned long dar;
49 unsigned long dear;
50 };
4f8e78c0
XS
51 union {
52 unsigned long dsisr;
53 unsigned long esr;
54 };
002af939
ME
55 unsigned long result;
56 };
57 };
b09049c5 58#if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)
de78a9c4
CL
59 union {
60 struct {
4c2de74c 61#ifdef CONFIG_PPC64
de78a9c4 62 unsigned long ppr;
13799748 63 unsigned long exit_result;
de78a9c4 64#endif
227ae625 65 union {
de78a9c4 66#ifdef CONFIG_PPC_KUAP
227ae625 67 unsigned long kuap;
4c2de74c 68#endif
227ae625
AK
69#ifdef CONFIG_PPC_PKEY
70 unsigned long amr;
71#endif
72 };
8e560921
AK
73#ifdef CONFIG_PPC_PKEY
74 unsigned long iamr;
75#endif
de78a9c4 76 };
8e560921 77 unsigned long __pad[4]; /* Maintain 16 byte interrupt stack alignment */
de78a9c4 78 };
b09049c5 79#endif
b5cfc9cd
CL
80#if defined(CONFIG_PPC32) && defined(CONFIG_BOOKE)
81 struct { /* Must be a multiple of 16 bytes */
82 unsigned long mas0;
83 unsigned long mas1;
84 unsigned long mas2;
85 unsigned long mas3;
86 unsigned long mas6;
87 unsigned long mas7;
88 unsigned long srr0;
89 unsigned long srr1;
90 unsigned long csrr0;
91 unsigned long csrr1;
92 unsigned long dsrr0;
93 unsigned long dsrr1;
94 };
95#endif
002af939
ME
96};
97#endif
a0987224 98
e3de1e29
ME
99
100#define STACK_FRAME_WITH_PT_REGS (STACK_FRAME_OVERHEAD + sizeof(struct pt_regs))
101
19c95df1
ME
102// Always displays as "REGS" in memory dumps
103#ifdef CONFIG_CPU_BIG_ENDIAN
bbd71709 104#define STACK_FRAME_REGS_MARKER ASM_CONST(0x52454753)
19c95df1
ME
105#else
106#define STACK_FRAME_REGS_MARKER ASM_CONST(0x53474552)
107#endif
17773afd 108
da80d460
SR
109#ifdef __powerpc64__
110
573ebfa6
PM
111/*
112 * Size of redzone that userspace is allowed to use below the stack
113 * pointer. This is 288 in the 64-bit big-endian ELF ABI, and 512 in
114 * the new ELFv2 little-endian ABI, so we allow the larger amount.
115 *
116 * For kernel code we allow a 288-byte redzone, in order to conserve
117 * kernel stack space; gcc currently only uses 288 bytes, and will
118 * hopefully allow explicit control of the redzone size in future.
119 */
120#define USER_REDZONE_SIZE 512
121#define KERNEL_REDZONE_SIZE 288
122
da80d460 123#define STACK_FRAME_OVERHEAD 112 /* size of minimum stack frame */
ec2b36b9 124#define STACK_FRAME_LR_SAVE 2 /* Location of LR in stack frame */
ec2b36b9 125#define STACK_INT_FRAME_SIZE (sizeof(struct pt_regs) + \
573ebfa6 126 STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)
ec2b36b9 127#define STACK_FRAME_MARKER 12
da80d460 128
7d40aff8 129#ifdef CONFIG_PPC64_ELF_ABI_V2
85101af1
AB
130#define STACK_FRAME_MIN_SIZE 32
131#else
132#define STACK_FRAME_MIN_SIZE STACK_FRAME_OVERHEAD
133#endif
134
da80d460
SR
135/* Size of dummy stack frame allocated when calling signal handler. */
136#define __SIGNAL_FRAMESIZE 128
137#define __SIGNAL_FRAMESIZE32 64
138
139#else /* __powerpc64__ */
140
573ebfa6
PM
141#define USER_REDZONE_SIZE 0
142#define KERNEL_REDZONE_SIZE 0
da80d460 143#define STACK_FRAME_OVERHEAD 16 /* size of minimum stack frame */
ec2b36b9 144#define STACK_FRAME_LR_SAVE 1 /* Location of LR in stack frame */
ec2b36b9
BH
145#define STACK_INT_FRAME_SIZE (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
146#define STACK_FRAME_MARKER 2
85101af1 147#define STACK_FRAME_MIN_SIZE STACK_FRAME_OVERHEAD
da80d460
SR
148
149/* Size of stack frame allocated when calling signal handler. */
150#define __SIGNAL_FRAMESIZE 64
151
152#endif /* __powerpc64__ */
a0987224 153
da80d460 154#ifndef __ASSEMBLY__
59dc5bfc
NP
155#include <asm/paca.h>
156
157#ifdef CONFIG_SMP
158extern unsigned long profile_pc(struct pt_regs *regs);
159#else
160#define profile_pc(regs) instruction_pointer(regs)
161#endif
162
163long do_syscall_trace_enter(struct pt_regs *regs);
164void do_syscall_trace_leave(struct pt_regs *regs);
165
5f0f95f1 166static inline void set_return_regs_changed(void)
59dc5bfc 167{
59dc5bfc
NP
168#ifdef CONFIG_PPC_BOOK3S_64
169 local_paca->hsrr_valid = 0;
170 local_paca->srr_valid = 0;
171#endif
172}
173
5f0f95f1 174static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
59dc5bfc 175{
5f0f95f1 176 regs->nip = ip;
cae46446 177 set_return_regs_changed();
59dc5bfc
NP
178}
179
5f0f95f1 180static inline void regs_set_return_msr(struct pt_regs *regs, unsigned long msr)
59dc5bfc 181{
5f0f95f1 182 regs->msr = msr;
cae46446 183 set_return_regs_changed();
59dc5bfc
NP
184}
185
186static inline void regs_add_return_ip(struct pt_regs *regs, long offset)
187{
188 regs_set_return_ip(regs, regs->nip + offset);
189}
da80d460 190
b42dfdea
CH
191static inline unsigned long instruction_pointer(struct pt_regs *regs)
192{
193 return regs->nip;
194}
195
196static inline void instruction_pointer_set(struct pt_regs *regs,
197 unsigned long val)
198{
59dc5bfc 199 regs_set_return_ip(regs, val);
b42dfdea
CH
200}
201
202static inline unsigned long user_stack_pointer(struct pt_regs *regs)
203{
204 return regs->gpr[1];
205}
206
207static inline unsigned long frame_pointer(struct pt_regs *regs)
208{
209 return 0;
210}
e6289427 211
da80d460 212#define user_mode(regs) (((regs)->msr & MSR_PR) != 0)
1da177e4
LT
213
214#define force_successful_syscall_return() \
da80d460 215 do { \
401d1f02 216 set_thread_flag(TIF_NOERROR); \
da80d460 217 } while(0)
1da177e4 218
be6abfa7 219#define current_pt_regs() \
b72cc2e7 220 ((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1)
feb9df34 221
8f6cc75a
NP
222/*
223 * The 4 low bits (0xf) are available as flags to overload the trap word,
224 * because interrupt vectors have minimum alignment of 0x10. TRAP_FLAGS_MASK
225 * must cover the bits used as flags, including bit 0 which is used as the
226 * "norestart" bit.
227 */
feb9df34 228#ifdef __powerpc64__
8f6cc75a 229#define TRAP_FLAGS_MASK 0x1
feb9df34 230#else
1da177e4 231/*
8dc7f022 232 * On 4xx we use bit 1 in the trap word to indicate whether the exception
da80d460 233 * is a critical exception (1 means it is).
1da177e4 234 */
8f6cc75a 235#define TRAP_FLAGS_MASK 0xf
47c0bd1a
BH
236#define IS_CRITICAL_EXC(regs) (((regs)->trap & 2) != 0)
237#define IS_MCHECK_EXC(regs) (((regs)->trap & 4) != 0)
663276b7 238#define IS_DEBUG_EXC(regs) (((regs)->trap & 8) != 0)
da80d460 239#endif /* __powerpc64__ */
8f6cc75a 240#define TRAP(regs) ((regs)->trap & ~TRAP_FLAGS_MASK)
a0987224 241
93c043e3 242static __always_inline void set_trap(struct pt_regs *regs, unsigned long val)
db30144b
NP
243{
244 regs->trap = (regs->trap & TRAP_FLAGS_MASK) | (val & ~TRAP_FLAGS_MASK);
245}
246
7fa95f9a
NP
247static inline bool trap_is_scv(struct pt_regs *regs)
248{
249 return (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x3000);
250}
251
b966f227
CL
252static inline bool trap_is_unsupported_scv(struct pt_regs *regs)
253{
254 return IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x7ff0;
255}
256
912237ea
NP
257static inline bool trap_is_syscall(struct pt_regs *regs)
258{
7fa95f9a 259 return (trap_is_scv(regs) || TRAP(regs) == 0xc00);
912237ea
NP
260}
261
4e0e45b0
NP
262static inline bool trap_norestart(struct pt_regs *regs)
263{
8f6cc75a 264 return regs->trap & 0x1;
4e0e45b0
NP
265}
266
627b72be 267static __always_inline void set_trap_norestart(struct pt_regs *regs)
4e0e45b0 268{
8f6cc75a 269 regs->trap |= 0x1;
4e0e45b0
NP
270}
271
d72500f9
NP
272#define kernel_stack_pointer(regs) ((regs)->gpr[1])
273static inline int is_syscall_success(struct pt_regs *regs)
274{
275 if (trap_is_scv(regs))
276 return !IS_ERR_VALUE((unsigned long)regs->gpr[3]);
277 else
278 return !(regs->ccr & 0x10000000);
279}
280
281static inline long regs_return_value(struct pt_regs *regs)
282{
283 if (trap_is_scv(regs))
284 return regs->gpr[3];
285
286 if (is_syscall_success(regs))
287 return regs->gpr[3];
288 else
289 return -regs->gpr[3];
290}
291
292static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
293{
294 regs->gpr[3] = rc;
295}
296
806c0e6e
CL
297static inline bool cpu_has_msr_ri(void)
298{
047a6fd4 299 return !IS_ENABLED(CONFIG_BOOKE_OR_40x);
806c0e6e
CL
300}
301
302static inline bool regs_is_unrecoverable(struct pt_regs *regs)
303{
304 return unlikely(cpu_has_msr_ri() && !(regs->msr & MSR_RI));
305}
306
307static inline void regs_set_recoverable(struct pt_regs *regs)
308{
309 if (cpu_has_msr_ri())
310 regs_set_return_msr(regs, regs->msr | MSR_RI);
311}
312
313static inline void regs_set_unrecoverable(struct pt_regs *regs)
314{
315 if (cpu_has_msr_ri())
316 regs_set_return_msr(regs, regs->msr & ~MSR_RI);
317}
318
2a84b0d7 319#define arch_has_single_step() (1)
12c3f1fd 320#define arch_has_block_step() (true)
efc463ad 321#define ARCH_HAS_USER_SINGLE_STEP_REPORT
25baa35b 322
359e4284
MS
323/*
324 * kprobe-based event tracer support
325 */
326
327#include <linux/stddef.h>
328#include <linux/thread_info.h>
329extern int regs_query_register_offset(const char *name);
330extern const char *regs_query_register_name(unsigned int offset);
331#define MAX_REG_OFFSET (offsetof(struct pt_regs, dsisr))
332
333/**
334 * regs_get_register() - get register value from its offset
335 * @regs: pt_regs from which register value is gotten
336 * @offset: offset number of the register.
337 *
338 * regs_get_register returns the value of a register whose offset from @regs.
339 * The @offset is the offset of the register in struct pt_regs.
340 * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
341 */
342static inline unsigned long regs_get_register(struct pt_regs *regs,
343 unsigned int offset)
344{
345 if (unlikely(offset > MAX_REG_OFFSET))
346 return 0;
347 return *(unsigned long *)((unsigned long)regs + offset);
348}
349
350/**
351 * regs_within_kernel_stack() - check the address in the stack
352 * @regs: pt_regs which contains kernel stack pointer.
353 * @addr: address which is checked.
354 *
355 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
356 * If @addr is within the kernel stack, it returns true. If not, returns false.
357 */
358
359static inline bool regs_within_kernel_stack(struct pt_regs *regs,
360 unsigned long addr)
361{
362 return ((addr & ~(THREAD_SIZE - 1)) ==
363 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
364}
365
366/**
367 * regs_get_kernel_stack_nth() - get Nth entry of the stack
368 * @regs: pt_regs which contains kernel stack pointer.
369 * @n: stack entry number.
370 *
371 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
372 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
373 * this returns 0.
374 */
375static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
376 unsigned int n)
377{
378 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
379 addr += n;
380 if (regs_within_kernel_stack(regs, (unsigned long)addr))
381 return *addr;
382 else
383 return 0;
384}
385
a0987224
AB
386#endif /* __ASSEMBLY__ */
387
da80d460 388#ifndef __powerpc64__
f1763e62
CL
389/* We need PT_SOFTE defined at all time to avoid #ifdefs */
390#define PT_SOFTE PT_MQ
da80d460 391#else /* __powerpc64__ */
a0987224 392#define PT_FPSCR32 (PT_FPR0 + 2*32 + 1) /* each FP reg occupies 2 32-bit userspace slots */
1da177e4
LT
393#define PT_VR0_32 164 /* each Vector reg occupies 4 slots in 32-bit */
394#define PT_VSCR_32 (PT_VR0 + 32*4 + 3)
395#define PT_VRSAVE_32 (PT_VR0 + 33*4)
ce48b210 396#define PT_VSR0_32 300 /* each VSR reg occupies 4 slots in 32-bit */
da80d460 397#endif /* __powerpc64__ */
da80d460 398#endif /* _ASM_POWERPC_PTRACE_H */