1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * OpenRISC ptrace.c 4 * 5 * Linux architectural port borrowing liberally from similar works of 6 * others. All original copyrights apply as per the original source 7 * declaration. 8 * 9 * Modifications for the OpenRISC architecture: 10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com> 11 * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com> 12 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se> 13 */ 14 15 #include <linux/kernel.h> 16 #include <linux/sched.h> 17 #include <linux/sched/task_stack.h> 18 #include <linux/string.h> 19 20 #include <linux/mm.h> 21 #include <linux/errno.h> 22 #include <linux/ptrace.h> 23 #include <linux/audit.h> 24 #include <linux/regset.h> 25 #include <linux/elf.h> 26 27 #include <asm/thread_info.h> 28 #include <asm/page.h> 29 30 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs); 31 32 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs); 33 34 /* 35 * Copy the thread state to a regset that can be interpreted by userspace. 36 * 37 * It doesn't matter what our internal pt_regs structure looks like. The 38 * important thing is that we export a consistent view of the thread state 39 * to userspace. As such, we need to make sure that the regset remains 40 * ABI compatible as defined by the struct user_regs_struct: 41 * 42 * (Each item is a 32-bit word) 43 * r0 = 0 (exported for clarity) 44 * 31 GPRS r1-r31 45 * PC (Program counter) 46 * SR (Supervision register) 47 */ 48 static int genregs_get(struct task_struct *target, 49 const struct user_regset *regset, 50 struct membuf to) 51 { 52 const struct pt_regs *regs = task_pt_regs(target); 53 54 /* r0 */ 55 membuf_zero(&to, 4); 56 membuf_write(&to, regs->gpr + 1, 31 * 4); 57 membuf_store(&to, regs->pc); 58 return membuf_store(&to, regs->sr); 59 } 60 61 /* 62 * Set the thread state from a regset passed in via ptrace 63 */ 64 static int genregs_set(struct task_struct *target, 65 const struct user_regset *regset, 66 unsigned int pos, unsigned int count, 67 const void *kbuf, const void __user * ubuf) 68 { 69 struct pt_regs *regs = task_pt_regs(target); 70 int ret; 71 72 /* ignore r0 */ 73 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, 4); 74 /* r1 - r31 */ 75 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 76 regs->gpr+1, 4, 4*32); 77 /* PC */ 78 if (!ret) 79 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, 80 ®s->pc, 4*32, 4*33); 81 /* 82 * Skip SR and padding... userspace isn't allowed to changes bits in 83 * the Supervision register 84 */ 85 if (!ret) 86 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 4*33, -1); 87 88 return ret; 89 } 90 91 #ifdef CONFIG_FPU 92 /* 93 * As OpenRISC shares GPRs and floating point registers we don't need to export 94 * the floating point registers again. So here we only export the fpcsr special 95 * purpose register. 96 */ 97 static int fpregs_get(struct task_struct *target, 98 const struct user_regset *regset, 99 struct membuf to) 100 { 101 return membuf_store(&to, target->thread.fpcsr); 102 } 103 104 static int fpregs_set(struct task_struct *target, 105 const struct user_regset *regset, 106 unsigned int pos, unsigned int count, 107 const void *kbuf, const void __user *ubuf) 108 { 109 /* FPCSR */ 110 return user_regset_copyin(&pos, &count, &kbuf, &ubuf, 111 &target->thread.fpcsr, 0, 4); 112 } 113 #endif 114 115 /* 116 * Define the register sets available on OpenRISC under Linux 117 */ 118 enum or1k_regset { 119 REGSET_GENERAL, 120 #ifdef CONFIG_FPU 121 REGSET_FPU, 122 #endif 123 }; 124 125 static const struct user_regset or1k_regsets[] = { 126 [REGSET_GENERAL] = { 127 .core_note_type = NT_PRSTATUS, 128 .n = ELF_NGREG, 129 .size = sizeof(long), 130 .align = sizeof(long), 131 .regset_get = genregs_get, 132 .set = genregs_set, 133 }, 134 #ifdef CONFIG_FPU 135 [REGSET_FPU] = { 136 .core_note_type = NT_PRFPREG, 137 .n = sizeof(struct __or1k_fpu_state) / sizeof(long), 138 .size = sizeof(long), 139 .align = sizeof(long), 140 .regset_get = fpregs_get, 141 .set = fpregs_set, 142 }, 143 #endif 144 }; 145 146 static const struct user_regset_view user_or1k_native_view = { 147 .name = "or1k", 148 .e_machine = EM_OPENRISC, 149 .regsets = or1k_regsets, 150 .n = ARRAY_SIZE(or1k_regsets), 151 }; 152 153 const struct user_regset_view *task_user_regset_view(struct task_struct *task) 154 { 155 return &user_or1k_native_view; 156 } 157 158 /* 159 * does not yet catch signals sent when the child dies. 160 * in exit.c or in signal.c. 161 */ 162 163 struct pt_regs_offset { 164 const char *name; 165 int offset; 166 }; 167 168 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)} 169 #define REG_OFFSET_END {.name = NULL, .offset = 0} 170 171 static const struct pt_regs_offset regoffset_table[] = { 172 REG_OFFSET_NAME(sr), 173 REG_OFFSET_NAME(sp), 174 REG_OFFSET_NAME(gpr2), 175 REG_OFFSET_NAME(gpr3), 176 REG_OFFSET_NAME(gpr4), 177 REG_OFFSET_NAME(gpr5), 178 REG_OFFSET_NAME(gpr6), 179 REG_OFFSET_NAME(gpr7), 180 REG_OFFSET_NAME(gpr8), 181 REG_OFFSET_NAME(gpr9), 182 REG_OFFSET_NAME(gpr10), 183 REG_OFFSET_NAME(gpr11), 184 REG_OFFSET_NAME(gpr12), 185 REG_OFFSET_NAME(gpr13), 186 REG_OFFSET_NAME(gpr14), 187 REG_OFFSET_NAME(gpr15), 188 REG_OFFSET_NAME(gpr16), 189 REG_OFFSET_NAME(gpr17), 190 REG_OFFSET_NAME(gpr18), 191 REG_OFFSET_NAME(gpr19), 192 REG_OFFSET_NAME(gpr20), 193 REG_OFFSET_NAME(gpr21), 194 REG_OFFSET_NAME(gpr22), 195 REG_OFFSET_NAME(gpr23), 196 REG_OFFSET_NAME(gpr24), 197 REG_OFFSET_NAME(gpr25), 198 REG_OFFSET_NAME(gpr26), 199 REG_OFFSET_NAME(gpr27), 200 REG_OFFSET_NAME(gpr28), 201 REG_OFFSET_NAME(gpr29), 202 REG_OFFSET_NAME(gpr30), 203 REG_OFFSET_NAME(gpr31), 204 REG_OFFSET_NAME(pc), 205 REG_OFFSET_NAME(orig_gpr11), 206 REG_OFFSET_END, 207 }; 208 209 /** 210 * regs_query_register_offset() - query register offset from its name 211 * @name: the name of a register 212 * 213 * regs_query_register_offset() returns the offset of a register in struct 214 * pt_regs from its name. If the name is invalid, this returns -EINVAL; 215 */ 216 int regs_query_register_offset(const char *name) 217 { 218 const struct pt_regs_offset *roff; 219 220 for (roff = regoffset_table; roff->name != NULL; roff++) 221 if (!strcmp(roff->name, name)) 222 return roff->offset; 223 return -EINVAL; 224 } 225 226 /** 227 * regs_within_kernel_stack() - check the address in the stack 228 * @regs: pt_regs which contains kernel stack pointer. 229 * @addr: address which is checked. 230 * 231 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s). 232 * If @addr is within the kernel stack, it returns true. If not, returns false. 233 */ 234 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr) 235 { 236 return (addr & ~(THREAD_SIZE - 1)) == 237 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)); 238 } 239 240 /** 241 * regs_get_kernel_stack_nth() - get Nth entry of the stack 242 * @regs: pt_regs which contains kernel stack pointer. 243 * @n: stack entry number. 244 * 245 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which 246 * is specified by @regs. If the @n th entry is NOT in the kernel stack, 247 * this returns 0. 248 */ 249 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n) 250 { 251 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs); 252 253 addr += n; 254 if (regs_within_kernel_stack(regs, (unsigned long)addr)) 255 return *addr; 256 else 257 return 0; 258 } 259 260 /* 261 * Called by kernel/ptrace.c when detaching.. 262 * 263 * Make sure the single step bit is not set. 264 */ 265 void ptrace_disable(struct task_struct *child) 266 { 267 pr_debug("ptrace_disable(): TODO\n"); 268 269 user_disable_single_step(child); 270 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 271 } 272 273 long arch_ptrace(struct task_struct *child, long request, unsigned long addr, 274 unsigned long data) 275 { 276 int ret; 277 278 switch (request) { 279 default: 280 ret = ptrace_request(child, request, addr, data); 281 break; 282 } 283 284 return ret; 285 } 286 287 /* 288 * Notification of system call entry/exit 289 * - triggered by current->work.syscall_trace 290 */ 291 asmlinkage long do_syscall_trace_enter(struct pt_regs *regs) 292 { 293 long ret = 0; 294 295 if (test_thread_flag(TIF_SYSCALL_TRACE) && 296 ptrace_report_syscall_entry(regs)) 297 /* 298 * Tracing decided this syscall should not happen. 299 * We'll return a bogus call number to get an ENOSYS 300 * error, but leave the original number in <something>. 301 */ 302 ret = -1L; 303 304 audit_syscall_entry(regs->gpr[11], regs->gpr[3], regs->gpr[4], 305 regs->gpr[5], regs->gpr[6]); 306 307 return ret ? : regs->gpr[11]; 308 } 309 310 asmlinkage void do_syscall_trace_leave(struct pt_regs *regs) 311 { 312 int step; 313 314 audit_syscall_exit(regs); 315 316 step = test_thread_flag(TIF_SINGLESTEP); 317 if (step || test_thread_flag(TIF_SYSCALL_TRACE)) 318 ptrace_report_syscall_exit(regs, step); 319 } 320