Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (C) 2001 PPC64 Team, IBM Corp
0004  *
0005  * This struct defines the way the registers are stored on the
0006  * kernel stack during a system call or other kernel entry.
0007  *
0008  * this should only contain volatile regs
0009  * since we can keep non-volatile in the thread_struct
0010  * should set this up when only volatiles are saved
0011  * by intr code.
0012  *
0013  * Since this is going on the stack, *CARE MUST BE TAKEN* to insure
0014  * that the overall structure is a multiple of 16 bytes in length.
0015  *
0016  * Note that the offsets of the fields in this struct correspond with
0017  * the PT_* values below.  This simplifies arch/powerpc/kernel/ptrace.c.
0018  */
0019 #ifndef _ASM_POWERPC_PTRACE_H
0020 #define _ASM_POWERPC_PTRACE_H
0021 
0022 #include <linux/err.h>
0023 #include <uapi/asm/ptrace.h>
0024 #include <asm/asm-const.h>
0025 #include <asm/reg.h>
0026 
0027 #ifndef __ASSEMBLY__
0028 struct pt_regs
0029 {
0030     union {
0031         struct user_pt_regs user_regs;
0032         struct {
0033             unsigned long gpr[32];
0034             unsigned long nip;
0035             unsigned long msr;
0036             unsigned long orig_gpr3;
0037             unsigned long ctr;
0038             unsigned long link;
0039             unsigned long xer;
0040             unsigned long ccr;
0041 #ifdef CONFIG_PPC64
0042             unsigned long softe;
0043 #else
0044             unsigned long mq;
0045 #endif
0046             unsigned long trap;
0047             union {
0048                 unsigned long dar;
0049                 unsigned long dear;
0050             };
0051             union {
0052                 unsigned long dsisr;
0053                 unsigned long esr;
0054             };
0055             unsigned long result;
0056         };
0057     };
0058 #if defined(CONFIG_PPC64) || defined(CONFIG_PPC_KUAP)
0059     union {
0060         struct {
0061 #ifdef CONFIG_PPC64
0062             unsigned long ppr;
0063             unsigned long exit_result;
0064 #endif
0065             union {
0066 #ifdef CONFIG_PPC_KUAP
0067                 unsigned long kuap;
0068 #endif
0069 #ifdef CONFIG_PPC_PKEY
0070                 unsigned long amr;
0071 #endif
0072             };
0073 #ifdef CONFIG_PPC_PKEY
0074             unsigned long iamr;
0075 #endif
0076         };
0077         unsigned long __pad[4]; /* Maintain 16 byte interrupt stack alignment */
0078     };
0079 #endif
0080 #if defined(CONFIG_PPC32) && defined(CONFIG_BOOKE)
0081     struct { /* Must be a multiple of 16 bytes */
0082         unsigned long mas0;
0083         unsigned long mas1;
0084         unsigned long mas2;
0085         unsigned long mas3;
0086         unsigned long mas6;
0087         unsigned long mas7;
0088         unsigned long srr0;
0089         unsigned long srr1;
0090         unsigned long csrr0;
0091         unsigned long csrr1;
0092         unsigned long dsrr0;
0093         unsigned long dsrr1;
0094     };
0095 #endif
0096 };
0097 #endif
0098 
0099 
0100 #define STACK_FRAME_WITH_PT_REGS (STACK_FRAME_OVERHEAD + sizeof(struct pt_regs))
0101 
0102 #ifdef __powerpc64__
0103 
0104 /*
0105  * Size of redzone that userspace is allowed to use below the stack
0106  * pointer.  This is 288 in the 64-bit big-endian ELF ABI, and 512 in
0107  * the new ELFv2 little-endian ABI, so we allow the larger amount.
0108  *
0109  * For kernel code we allow a 288-byte redzone, in order to conserve
0110  * kernel stack space; gcc currently only uses 288 bytes, and will
0111  * hopefully allow explicit control of the redzone size in future.
0112  */
0113 #define USER_REDZONE_SIZE   512
0114 #define KERNEL_REDZONE_SIZE 288
0115 
0116 #define STACK_FRAME_OVERHEAD    112 /* size of minimum stack frame */
0117 #define STACK_FRAME_LR_SAVE 2   /* Location of LR in stack frame */
0118 #define STACK_FRAME_REGS_MARKER ASM_CONST(0x7265677368657265)
0119 #define STACK_INT_FRAME_SIZE    (sizeof(struct pt_regs) + \
0120                  STACK_FRAME_OVERHEAD + KERNEL_REDZONE_SIZE)
0121 #define STACK_FRAME_MARKER  12
0122 
0123 #ifdef CONFIG_PPC64_ELF_ABI_V2
0124 #define STACK_FRAME_MIN_SIZE    32
0125 #else
0126 #define STACK_FRAME_MIN_SIZE    STACK_FRAME_OVERHEAD
0127 #endif
0128 
0129 /* Size of dummy stack frame allocated when calling signal handler. */
0130 #define __SIGNAL_FRAMESIZE  128
0131 #define __SIGNAL_FRAMESIZE32    64
0132 
0133 #else /* __powerpc64__ */
0134 
0135 #define USER_REDZONE_SIZE   0
0136 #define KERNEL_REDZONE_SIZE 0
0137 #define STACK_FRAME_OVERHEAD    16  /* size of minimum stack frame */
0138 #define STACK_FRAME_LR_SAVE 1   /* Location of LR in stack frame */
0139 #define STACK_FRAME_REGS_MARKER ASM_CONST(0x72656773)
0140 #define STACK_INT_FRAME_SIZE    (sizeof(struct pt_regs) + STACK_FRAME_OVERHEAD)
0141 #define STACK_FRAME_MARKER  2
0142 #define STACK_FRAME_MIN_SIZE    STACK_FRAME_OVERHEAD
0143 
0144 /* Size of stack frame allocated when calling signal handler. */
0145 #define __SIGNAL_FRAMESIZE  64
0146 
0147 #endif /* __powerpc64__ */
0148 
0149 #ifndef __ASSEMBLY__
0150 #include <asm/paca.h>
0151 
0152 #ifdef CONFIG_SMP
0153 extern unsigned long profile_pc(struct pt_regs *regs);
0154 #else
0155 #define profile_pc(regs) instruction_pointer(regs)
0156 #endif
0157 
0158 long do_syscall_trace_enter(struct pt_regs *regs);
0159 void do_syscall_trace_leave(struct pt_regs *regs);
0160 
0161 static inline void set_return_regs_changed(void)
0162 {
0163 #ifdef CONFIG_PPC_BOOK3S_64
0164     local_paca->hsrr_valid = 0;
0165     local_paca->srr_valid = 0;
0166 #endif
0167 }
0168 
0169 static inline void regs_set_return_ip(struct pt_regs *regs, unsigned long ip)
0170 {
0171     regs->nip = ip;
0172     set_return_regs_changed();
0173 }
0174 
0175 static inline void regs_set_return_msr(struct pt_regs *regs, unsigned long msr)
0176 {
0177     regs->msr = msr;
0178     set_return_regs_changed();
0179 }
0180 
0181 static inline void regs_add_return_ip(struct pt_regs *regs, long offset)
0182 {
0183     regs_set_return_ip(regs, regs->nip + offset);
0184 }
0185 
0186 static inline unsigned long instruction_pointer(struct pt_regs *regs)
0187 {
0188     return regs->nip;
0189 }
0190 
0191 static inline void instruction_pointer_set(struct pt_regs *regs,
0192         unsigned long val)
0193 {
0194     regs_set_return_ip(regs, val);
0195 }
0196 
0197 static inline unsigned long user_stack_pointer(struct pt_regs *regs)
0198 {
0199     return regs->gpr[1];
0200 }
0201 
0202 static inline unsigned long frame_pointer(struct pt_regs *regs)
0203 {
0204     return 0;
0205 }
0206 
0207 #define user_mode(regs) (((regs)->msr & MSR_PR) != 0)
0208 
0209 #define force_successful_syscall_return()   \
0210     do { \
0211         set_thread_flag(TIF_NOERROR); \
0212     } while(0)
0213 
0214 #define current_pt_regs() \
0215     ((struct pt_regs *)((unsigned long)task_stack_page(current) + THREAD_SIZE) - 1)
0216 
0217 /*
0218  * The 4 low bits (0xf) are available as flags to overload the trap word,
0219  * because interrupt vectors have minimum alignment of 0x10. TRAP_FLAGS_MASK
0220  * must cover the bits used as flags, including bit 0 which is used as the
0221  * "norestart" bit.
0222  */
0223 #ifdef __powerpc64__
0224 #define TRAP_FLAGS_MASK     0x1
0225 #else
0226 /*
0227  * On 4xx we use bit 1 in the trap word to indicate whether the exception
0228  * is a critical exception (1 means it is).
0229  */
0230 #define TRAP_FLAGS_MASK     0xf
0231 #define IS_CRITICAL_EXC(regs)   (((regs)->trap & 2) != 0)
0232 #define IS_MCHECK_EXC(regs) (((regs)->trap & 4) != 0)
0233 #define IS_DEBUG_EXC(regs)  (((regs)->trap & 8) != 0)
0234 #endif /* __powerpc64__ */
0235 #define TRAP(regs)      ((regs)->trap & ~TRAP_FLAGS_MASK)
0236 
0237 static __always_inline void set_trap(struct pt_regs *regs, unsigned long val)
0238 {
0239     regs->trap = (regs->trap & TRAP_FLAGS_MASK) | (val & ~TRAP_FLAGS_MASK);
0240 }
0241 
0242 static inline bool trap_is_scv(struct pt_regs *regs)
0243 {
0244     return (IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x3000);
0245 }
0246 
0247 static inline bool trap_is_unsupported_scv(struct pt_regs *regs)
0248 {
0249     return IS_ENABLED(CONFIG_PPC_BOOK3S_64) && TRAP(regs) == 0x7ff0;
0250 }
0251 
0252 static inline bool trap_is_syscall(struct pt_regs *regs)
0253 {
0254     return (trap_is_scv(regs) || TRAP(regs) == 0xc00);
0255 }
0256 
0257 static inline bool trap_norestart(struct pt_regs *regs)
0258 {
0259     return regs->trap & 0x1;
0260 }
0261 
0262 static __always_inline void set_trap_norestart(struct pt_regs *regs)
0263 {
0264     regs->trap |= 0x1;
0265 }
0266 
0267 #define kernel_stack_pointer(regs) ((regs)->gpr[1])
0268 static inline int is_syscall_success(struct pt_regs *regs)
0269 {
0270     if (trap_is_scv(regs))
0271         return !IS_ERR_VALUE((unsigned long)regs->gpr[3]);
0272     else
0273         return !(regs->ccr & 0x10000000);
0274 }
0275 
0276 static inline long regs_return_value(struct pt_regs *regs)
0277 {
0278     if (trap_is_scv(regs))
0279         return regs->gpr[3];
0280 
0281     if (is_syscall_success(regs))
0282         return regs->gpr[3];
0283     else
0284         return -regs->gpr[3];
0285 }
0286 
0287 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
0288 {
0289     regs->gpr[3] = rc;
0290 }
0291 
0292 static inline bool cpu_has_msr_ri(void)
0293 {
0294     return !IS_ENABLED(CONFIG_BOOKE_OR_40x);
0295 }
0296 
0297 static inline bool regs_is_unrecoverable(struct pt_regs *regs)
0298 {
0299     return unlikely(cpu_has_msr_ri() && !(regs->msr & MSR_RI));
0300 }
0301 
0302 static inline void regs_set_recoverable(struct pt_regs *regs)
0303 {
0304     if (cpu_has_msr_ri())
0305         regs_set_return_msr(regs, regs->msr | MSR_RI);
0306 }
0307 
0308 static inline void regs_set_unrecoverable(struct pt_regs *regs)
0309 {
0310     if (cpu_has_msr_ri())
0311         regs_set_return_msr(regs, regs->msr & ~MSR_RI);
0312 }
0313 
0314 #define arch_has_single_step()  (1)
0315 #define arch_has_block_step()   (true)
0316 #define ARCH_HAS_USER_SINGLE_STEP_REPORT
0317 
0318 /*
0319  * kprobe-based event tracer support
0320  */
0321 
0322 #include <linux/stddef.h>
0323 #include <linux/thread_info.h>
0324 extern int regs_query_register_offset(const char *name);
0325 extern const char *regs_query_register_name(unsigned int offset);
0326 #define MAX_REG_OFFSET (offsetof(struct pt_regs, dsisr))
0327 
0328 /**
0329  * regs_get_register() - get register value from its offset
0330  * @regs:      pt_regs from which register value is gotten
0331  * @offset:    offset number of the register.
0332  *
0333  * regs_get_register returns the value of a register whose offset from @regs.
0334  * The @offset is the offset of the register in struct pt_regs.
0335  * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
0336  */
0337 static inline unsigned long regs_get_register(struct pt_regs *regs,
0338                         unsigned int offset)
0339 {
0340     if (unlikely(offset > MAX_REG_OFFSET))
0341         return 0;
0342     return *(unsigned long *)((unsigned long)regs + offset);
0343 }
0344 
0345 /**
0346  * regs_within_kernel_stack() - check the address in the stack
0347  * @regs:      pt_regs which contains kernel stack pointer.
0348  * @addr:      address which is checked.
0349  *
0350  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
0351  * If @addr is within the kernel stack, it returns true. If not, returns false.
0352  */
0353 
0354 static inline bool regs_within_kernel_stack(struct pt_regs *regs,
0355                         unsigned long addr)
0356 {
0357     return ((addr & ~(THREAD_SIZE - 1))  ==
0358         (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
0359 }
0360 
0361 /**
0362  * regs_get_kernel_stack_nth() - get Nth entry of the stack
0363  * @regs:   pt_regs which contains kernel stack pointer.
0364  * @n:      stack entry number.
0365  *
0366  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
0367  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
0368  * this returns 0.
0369  */
0370 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
0371                               unsigned int n)
0372 {
0373     unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
0374     addr += n;
0375     if (regs_within_kernel_stack(regs, (unsigned long)addr))
0376         return *addr;
0377     else
0378         return 0;
0379 }
0380 
0381 #endif /* __ASSEMBLY__ */
0382 
0383 #ifndef __powerpc64__
0384 /* We need PT_SOFTE defined at all time to avoid #ifdefs */
0385 #define PT_SOFTE PT_MQ
0386 #else /* __powerpc64__ */
0387 #define PT_FPSCR32 (PT_FPR0 + 2*32 + 1) /* each FP reg occupies 2 32-bit userspace slots */
0388 #define PT_VR0_32 164   /* each Vector reg occupies 4 slots in 32-bit */
0389 #define PT_VSCR_32 (PT_VR0 + 32*4 + 3)
0390 #define PT_VRSAVE_32 (PT_VR0 + 33*4)
0391 #define PT_VSR0_32 300  /* each VSR reg occupies 4 slots in 32-bit */
0392 #endif /* __powerpc64__ */
0393 #endif /* _ASM_POWERPC_PTRACE_H */