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