1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * S390 version 4 * Copyright IBM Corp. 1999, 2000 5 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) 6 */ 7 #ifndef _S390_PTRACE_H 8 #define _S390_PTRACE_H 9 10 #include <linux/bits.h> 11 #include <uapi/asm/ptrace.h> 12 #include <asm/thread_info.h> 13 #include <asm/tpi.h> 14 15 #define PIF_SYSCALL 0 /* inside a system call */ 16 #define PIF_SYSCALL_RET_SET 2 /* return value was set via ptrace */ 17 #define PIF_GUEST_FAULT 3 /* indicates program check in sie64a */ 18 #define PIF_FTRACE_FULL_REGS 4 /* all register contents valid (ftrace) */ 19 20 #define _PIF_SYSCALL BIT(PIF_SYSCALL) 21 #define _PIF_SYSCALL_RET_SET BIT(PIF_SYSCALL_RET_SET) 22 #define _PIF_GUEST_FAULT BIT(PIF_GUEST_FAULT) 23 #define _PIF_FTRACE_FULL_REGS BIT(PIF_FTRACE_FULL_REGS) 24 25 #define PSW32_MASK_PER _AC(0x40000000, UL) 26 #define PSW32_MASK_DAT _AC(0x04000000, UL) 27 #define PSW32_MASK_IO _AC(0x02000000, UL) 28 #define PSW32_MASK_EXT _AC(0x01000000, UL) 29 #define PSW32_MASK_KEY _AC(0x00F00000, UL) 30 #define PSW32_MASK_BASE _AC(0x00080000, UL) /* Always one */ 31 #define PSW32_MASK_MCHECK _AC(0x00040000, UL) 32 #define PSW32_MASK_WAIT _AC(0x00020000, UL) 33 #define PSW32_MASK_PSTATE _AC(0x00010000, UL) 34 #define PSW32_MASK_ASC _AC(0x0000C000, UL) 35 #define PSW32_MASK_CC _AC(0x00003000, UL) 36 #define PSW32_MASK_PM _AC(0x00000f00, UL) 37 #define PSW32_MASK_RI _AC(0x00000080, UL) 38 39 #define PSW32_ADDR_AMODE _AC(0x80000000, UL) 40 #define PSW32_ADDR_INSN _AC(0x7FFFFFFF, UL) 41 42 #define PSW32_DEFAULT_KEY ((PAGE_DEFAULT_ACC) << 20) 43 44 #define PSW32_ASC_PRIMARY _AC(0x00000000, UL) 45 #define PSW32_ASC_ACCREG _AC(0x00004000, UL) 46 #define PSW32_ASC_SECONDARY _AC(0x00008000, UL) 47 #define PSW32_ASC_HOME _AC(0x0000C000, UL) 48 49 #define PSW_DEFAULT_KEY ((PAGE_DEFAULT_ACC) << 52) 50 51 #define PSW_KERNEL_BITS (PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_ASC_HOME | \ 52 PSW_MASK_EA | PSW_MASK_BA | PSW_MASK_DAT) 53 #define PSW_USER_BITS (PSW_MASK_DAT | PSW_MASK_IO | PSW_MASK_EXT | \ 54 PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_MCHECK | \ 55 PSW_MASK_PSTATE | PSW_ASC_PRIMARY) 56 57 #ifndef __ASSEMBLER__ 58 59 struct psw_bits { 60 unsigned long : 1; 61 unsigned long per : 1; /* PER-Mask */ 62 unsigned long : 3; 63 unsigned long dat : 1; /* DAT Mode */ 64 unsigned long io : 1; /* Input/Output Mask */ 65 unsigned long ext : 1; /* External Mask */ 66 unsigned long key : 4; /* PSW Key */ 67 unsigned long : 1; 68 unsigned long mcheck : 1; /* Machine-Check Mask */ 69 unsigned long wait : 1; /* Wait State */ 70 unsigned long pstate : 1; /* Problem State */ 71 unsigned long as : 2; /* Address Space Control */ 72 unsigned long cc : 2; /* Condition Code */ 73 unsigned long pm : 4; /* Program Mask */ 74 unsigned long ri : 1; /* Runtime Instrumentation */ 75 unsigned long : 6; 76 unsigned long eaba : 2; /* Addressing Mode */ 77 unsigned long : 31; 78 unsigned long ia : 64; /* Instruction Address */ 79 }; 80 81 enum { 82 PSW_BITS_AMODE_24BIT = 0, 83 PSW_BITS_AMODE_31BIT = 1, 84 PSW_BITS_AMODE_64BIT = 3 85 }; 86 87 enum { 88 PSW_BITS_AS_PRIMARY = 0, 89 PSW_BITS_AS_ACCREG = 1, 90 PSW_BITS_AS_SECONDARY = 2, 91 PSW_BITS_AS_HOME = 3 92 }; 93 94 #define psw_bits(__psw) (*({ \ 95 typecheck(psw_t, __psw); \ 96 &(*(struct psw_bits *)(&(__psw))); \ 97 })) 98 99 typedef struct { 100 unsigned int mask; 101 unsigned int addr; 102 } psw_t32 __aligned(8); 103 104 #define PGM_INT_CODE_MASK 0x7f 105 #define PGM_INT_CODE_PER 0x80 106 107 /* 108 * The pt_regs struct defines the way the registers are stored on 109 * the stack during a system call. 110 */ 111 struct pt_regs { 112 union { 113 user_pt_regs user_regs; 114 struct { 115 unsigned long args[1]; 116 psw_t psw; 117 unsigned long gprs[NUM_GPRS]; 118 }; 119 }; 120 unsigned long orig_gpr2; 121 union { 122 struct { 123 unsigned int int_code; 124 unsigned int int_parm; 125 unsigned long int_parm_long; 126 }; 127 struct tpi_info tpi_info; 128 }; 129 unsigned long flags; 130 unsigned long last_break; 131 }; 132 133 /* 134 * Program event recording (PER) register set. 135 */ 136 struct per_regs { 137 unsigned long control; /* PER control bits */ 138 unsigned long start; /* PER starting address */ 139 unsigned long end; /* PER ending address */ 140 }; 141 142 /* 143 * PER event contains information about the cause of the last PER exception. 144 */ 145 struct per_event { 146 unsigned short cause; /* PER code, ATMID and AI */ 147 unsigned long address; /* PER address */ 148 unsigned char paid; /* PER access identification */ 149 }; 150 151 /* 152 * Simplified per_info structure used to decode the ptrace user space ABI. 153 */ 154 struct per_struct_kernel { 155 unsigned long cr9; /* PER control bits */ 156 unsigned long cr10; /* PER starting address */ 157 unsigned long cr11; /* PER ending address */ 158 unsigned long bits; /* Obsolete software bits */ 159 unsigned long starting_addr; /* User specified start address */ 160 unsigned long ending_addr; /* User specified end address */ 161 unsigned short perc_atmid; /* PER trap ATMID */ 162 unsigned long address; /* PER trap instruction address */ 163 unsigned char access_id; /* PER trap access identification */ 164 }; 165 166 #define PER_EVENT_MASK 0xEB000000UL 167 168 #define PER_EVENT_BRANCH 0x80000000UL 169 #define PER_EVENT_IFETCH 0x40000000UL 170 #define PER_EVENT_STORE 0x20000000UL 171 #define PER_EVENT_STORE_REAL 0x08000000UL 172 #define PER_EVENT_TRANSACTION_END 0x02000000UL 173 #define PER_EVENT_NULLIFICATION 0x01000000UL 174 175 #define PER_CONTROL_MASK 0x00e00000UL 176 177 #define PER_CONTROL_BRANCH_ADDRESS 0x00800000UL 178 #define PER_CONTROL_SUSPENSION 0x00400000UL 179 #define PER_CONTROL_ALTERATION 0x00200000UL 180 181 static inline void set_pt_regs_flag(struct pt_regs *regs, int flag) 182 { 183 regs->flags |= (1UL << flag); 184 } 185 186 static inline void clear_pt_regs_flag(struct pt_regs *regs, int flag) 187 { 188 regs->flags &= ~(1UL << flag); 189 } 190 191 static inline int test_pt_regs_flag(struct pt_regs *regs, int flag) 192 { 193 return !!(regs->flags & (1UL << flag)); 194 } 195 196 static inline int test_and_clear_pt_regs_flag(struct pt_regs *regs, int flag) 197 { 198 int ret = test_pt_regs_flag(regs, flag); 199 200 clear_pt_regs_flag(regs, flag); 201 return ret; 202 } 203 204 struct task_struct; 205 206 void update_cr_regs(struct task_struct *task); 207 208 /* 209 * These are defined as per linux/ptrace.h, which see. 210 */ 211 #define arch_has_single_step() (1) 212 #define arch_has_block_step() (1) 213 214 #define user_mode(regs) (((regs)->psw.mask & PSW_MASK_PSTATE) != 0) 215 #define instruction_pointer(regs) ((regs)->psw.addr) 216 #define user_stack_pointer(regs)((regs)->gprs[15]) 217 #define profile_pc(regs) instruction_pointer(regs) 218 219 static inline long regs_return_value(struct pt_regs *regs) 220 { 221 return regs->gprs[2]; 222 } 223 224 static inline void instruction_pointer_set(struct pt_regs *regs, 225 unsigned long val) 226 { 227 regs->psw.addr = val; 228 } 229 230 int regs_query_register_offset(const char *name); 231 const char *regs_query_register_name(unsigned int offset); 232 233 static __always_inline unsigned long kernel_stack_pointer(struct pt_regs *regs) 234 { 235 return regs->gprs[15]; 236 } 237 238 static __always_inline unsigned long regs_get_register(struct pt_regs *regs, unsigned int offset) 239 { 240 if (offset >= NUM_GPRS) 241 return 0; 242 return regs->gprs[offset]; 243 } 244 245 static __always_inline int regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) 246 { 247 unsigned long ksp = kernel_stack_pointer(regs); 248 249 return (addr & ~(THREAD_SIZE - 1)) == (ksp & ~(THREAD_SIZE - 1)); 250 } 251 252 /** 253 * regs_get_kernel_stack_nth() - get Nth entry of the stack 254 * @regs:pt_regs which contains kernel stack pointer. 255 * @n:stack entry number. 256 * 257 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which 258 * is specifined by @regs. If the @n th entry is NOT in the kernel stack, 259 * this returns 0. 260 */ 261 static __always_inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) 262 { 263 unsigned long addr; 264 265 addr = kernel_stack_pointer(regs) + n * sizeof(long); 266 if (!regs_within_kernel_stack(regs, addr)) 267 return 0; 268 return READ_ONCE_NOCHECK(*(unsigned long *)addr); 269 } 270 271 /** 272 * regs_get_kernel_argument() - get Nth function argument in kernel 273 * @regs: pt_regs of that context 274 * @n: function argument number (start from 0) 275 * 276 * regs_get_kernel_argument() returns @n th argument of the function call. 277 */ 278 static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs, 279 unsigned int n) 280 { 281 unsigned int argoffset = STACK_FRAME_OVERHEAD / sizeof(long); 282 283 #define NR_REG_ARGUMENTS 5 284 if (n < NR_REG_ARGUMENTS) 285 return regs_get_register(regs, 2 + n); 286 n -= NR_REG_ARGUMENTS; 287 return regs_get_kernel_stack_nth(regs, argoffset + n); 288 } 289 290 static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc) 291 { 292 regs->gprs[2] = rc; 293 } 294 295 #endif /* __ASSEMBLER__ */ 296 #endif /* _S390_PTRACE_H */ 297