Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Kernel support for the ptrace() and syscall tracing interfaces. |
| 3 | * |
| 4 | * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc. |
| 5 | * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx> |
| 6 | * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org> |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 7 | * Copyright (C) 2008-2016 Helge Deller <deller@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/mm.h> |
| 13 | #include <linux/smp.h> |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 14 | #include <linux/elf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/errno.h> |
| 16 | #include <linux/ptrace.h> |
Kyle McMartin | d6b5877 | 2009-04-26 23:53:34 -0400 | [diff] [blame] | 17 | #include <linux/tracehook.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/user.h> |
| 19 | #include <linux/personality.h> |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 20 | #include <linux/regset.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/security.h> |
Helge Deller | fe5c873 | 2014-09-21 21:01:15 +0200 | [diff] [blame] | 22 | #include <linux/seccomp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/compat.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 24 | #include <linux/signal.h> |
Helge Deller | 527973c | 2013-10-15 19:25:46 +0200 | [diff] [blame] | 25 | #include <linux/audit.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include <asm/uaccess.h> |
| 28 | #include <asm/pgtable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/processor.h> |
Sam Ravnborg | 0013a85 | 2005-09-09 20:57:26 +0200 | [diff] [blame] | 30 | #include <asm/asm-offsets.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* PSW bits we allow the debugger to modify */ |
John David Anglin | 34360f0 | 2012-12-28 23:18:01 +0000 | [diff] [blame] | 33 | #define USER_PSW_BITS (PSW_N | PSW_B | PSW_V | PSW_CB) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Helge Deller | fc79168 | 2016-04-13 22:44:54 +0200 | [diff] [blame] | 35 | #define CREATE_TRACE_POINTS |
| 36 | #include <trace/events/syscalls.h> |
| 37 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 38 | /* |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 39 | * These are our native regset flavors. |
| 40 | */ |
| 41 | enum parisc_regset { |
| 42 | REGSET_GENERAL, |
| 43 | REGSET_FP |
| 44 | }; |
| 45 | |
| 46 | /* |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 47 | * Called by kernel/ptrace.c when detaching.. |
| 48 | * |
| 49 | * Make sure single step bits etc are not set. |
| 50 | */ |
| 51 | void ptrace_disable(struct task_struct *task) |
| 52 | { |
Kyle McMartin | ecd3d4b | 2009-09-27 23:03:02 -0400 | [diff] [blame] | 53 | clear_tsk_thread_flag(task, TIF_SINGLESTEP); |
| 54 | clear_tsk_thread_flag(task, TIF_BLOCKSTEP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 56 | /* make sure the trap bits are not set */ |
| 57 | pa_psw(task)->r = 0; |
| 58 | pa_psw(task)->t = 0; |
| 59 | pa_psw(task)->h = 0; |
| 60 | pa_psw(task)->l = 0; |
| 61 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 63 | /* |
| 64 | * The following functions are called by ptrace_resume() when |
| 65 | * enabling or disabling single/block tracing. |
| 66 | */ |
| 67 | void user_disable_single_step(struct task_struct *task) |
| 68 | { |
| 69 | ptrace_disable(task); |
| 70 | } |
| 71 | |
| 72 | void user_enable_single_step(struct task_struct *task) |
| 73 | { |
Kyle McMartin | ecd3d4b | 2009-09-27 23:03:02 -0400 | [diff] [blame] | 74 | clear_tsk_thread_flag(task, TIF_BLOCKSTEP); |
| 75 | set_tsk_thread_flag(task, TIF_SINGLESTEP); |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 76 | |
| 77 | if (pa_psw(task)->n) { |
| 78 | struct siginfo si; |
| 79 | |
| 80 | /* Nullified, just crank over the queue. */ |
| 81 | task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1]; |
| 82 | task_regs(task)->iasq[0] = task_regs(task)->iasq[1]; |
| 83 | task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4; |
| 84 | pa_psw(task)->n = 0; |
| 85 | pa_psw(task)->x = 0; |
| 86 | pa_psw(task)->y = 0; |
| 87 | pa_psw(task)->z = 0; |
| 88 | pa_psw(task)->b = 0; |
| 89 | ptrace_disable(task); |
| 90 | /* Don't wake up the task, but let the |
| 91 | parent know something happened. */ |
| 92 | si.si_code = TRAP_TRACE; |
| 93 | si.si_addr = (void __user *) (task_regs(task)->iaoq[0] & ~3); |
| 94 | si.si_signo = SIGTRAP; |
| 95 | si.si_errno = 0; |
| 96 | force_sig_info(SIGTRAP, &si, task); |
| 97 | /* notify_parent(task, SIGCHLD); */ |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | /* Enable recovery counter traps. The recovery counter |
| 102 | * itself will be set to zero on a task switch. If the |
| 103 | * task is suspended on a syscall then the syscall return |
| 104 | * path will overwrite the recovery counter with a suitable |
| 105 | * value such that it traps once back in user space. We |
| 106 | * disable interrupts in the tasks PSW here also, to avoid |
| 107 | * interrupts while the recovery counter is decrementing. |
| 108 | */ |
| 109 | pa_psw(task)->r = 1; |
| 110 | pa_psw(task)->t = 0; |
| 111 | pa_psw(task)->h = 0; |
| 112 | pa_psw(task)->l = 0; |
| 113 | } |
| 114 | |
| 115 | void user_enable_block_step(struct task_struct *task) |
| 116 | { |
Kyle McMartin | ecd3d4b | 2009-09-27 23:03:02 -0400 | [diff] [blame] | 117 | clear_tsk_thread_flag(task, TIF_SINGLESTEP); |
| 118 | set_tsk_thread_flag(task, TIF_BLOCKSTEP); |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 119 | |
| 120 | /* Enable taken branch trap. */ |
| 121 | pa_psw(task)->r = 0; |
| 122 | pa_psw(task)->t = 1; |
| 123 | pa_psw(task)->h = 0; |
| 124 | pa_psw(task)->l = 0; |
| 125 | } |
| 126 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 127 | long arch_ptrace(struct task_struct *child, long request, |
| 128 | unsigned long addr, unsigned long data) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 129 | { |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 130 | unsigned long __user *datap = (unsigned long __user *)data; |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 131 | unsigned long tmp; |
| 132 | long ret = -EIO; |
| 133 | |
| 134 | switch (request) { |
| 135 | |
| 136 | /* Read the word at location addr in the USER area. For ptraced |
| 137 | processes, the kernel saves all regs on a syscall. */ |
| 138 | case PTRACE_PEEKUSR: |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 139 | if ((addr & (sizeof(unsigned long)-1)) || |
| 140 | addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 141 | break; |
| 142 | tmp = *(unsigned long *) ((char *) task_regs(child) + addr); |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 143 | ret = put_user(tmp, datap); |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 144 | break; |
| 145 | |
| 146 | /* Write the word at location addr in the USER area. This will need |
| 147 | to change when the kernel no longer saves all regs on a syscall. |
| 148 | FIXME. There is a problem at the moment in that r3-r18 are only |
| 149 | saved if the process is ptraced on syscall entry, and even then |
| 150 | those values are overwritten by actual register values on syscall |
| 151 | exit. */ |
| 152 | case PTRACE_POKEUSR: |
| 153 | /* Some register values written here may be ignored in |
| 154 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with |
| 155 | * r31/r31+4, and not with the values in pt_regs. |
| 156 | */ |
| 157 | if (addr == PT_PSW) { |
| 158 | /* Allow writing to Nullify, Divide-step-correction, |
| 159 | * and carry/borrow bits. |
| 160 | * BEWARE, if you set N, and then single step, it won't |
| 161 | * stop on the nullified instruction. |
| 162 | */ |
| 163 | data &= USER_PSW_BITS; |
| 164 | task_regs(child)->gr[0] &= ~USER_PSW_BITS; |
| 165 | task_regs(child)->gr[0] |= data; |
| 166 | ret = 0; |
| 167 | break; |
| 168 | } |
| 169 | |
Namhyung Kim | 9b05a69 | 2010-10-27 15:33:47 -0700 | [diff] [blame] | 170 | if ((addr & (sizeof(unsigned long)-1)) || |
| 171 | addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 172 | break; |
Helge Deller | d0c4b05 | 2019-07-16 21:43:11 +0200 | [diff] [blame] | 173 | if (addr == PT_IAOQ0 || addr == PT_IAOQ1) { |
| 174 | data |= 3; /* ensure userspace privilege */ |
| 175 | } |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 176 | if ((addr >= PT_GR1 && addr <= PT_GR31) || |
| 177 | addr == PT_IAOQ0 || addr == PT_IAOQ1 || |
| 178 | (addr >= PT_FR0 && addr <= PT_FR31 + 4) || |
| 179 | addr == PT_SAR) { |
| 180 | *(unsigned long *) ((char *) task_regs(child) + addr) = data; |
| 181 | ret = 0; |
| 182 | } |
| 183 | break; |
| 184 | |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 185 | case PTRACE_GETREGS: /* Get all gp regs from the child. */ |
| 186 | return copy_regset_to_user(child, |
| 187 | task_user_regset_view(current), |
| 188 | REGSET_GENERAL, |
| 189 | 0, sizeof(struct user_regs_struct), |
| 190 | datap); |
| 191 | |
| 192 | case PTRACE_SETREGS: /* Set all gp regs in the child. */ |
| 193 | return copy_regset_from_user(child, |
| 194 | task_user_regset_view(current), |
| 195 | REGSET_GENERAL, |
| 196 | 0, sizeof(struct user_regs_struct), |
| 197 | datap); |
| 198 | |
| 199 | case PTRACE_GETFPREGS: /* Get the child FPU state. */ |
| 200 | return copy_regset_to_user(child, |
| 201 | task_user_regset_view(current), |
| 202 | REGSET_FP, |
| 203 | 0, sizeof(struct user_fp_struct), |
| 204 | datap); |
| 205 | |
| 206 | case PTRACE_SETFPREGS: /* Set the child FPU state. */ |
| 207 | return copy_regset_from_user(child, |
| 208 | task_user_regset_view(current), |
| 209 | REGSET_FP, |
| 210 | 0, sizeof(struct user_fp_struct), |
| 211 | datap); |
| 212 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 213 | default: |
| 214 | ret = ptrace_request(child, request, addr, data); |
| 215 | break; |
| 216 | } |
| 217 | |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | #ifdef CONFIG_COMPAT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | /* This function is needed to translate 32 bit pt_regs offsets in to |
| 225 | * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel |
| 226 | * will request offset 12 if it wants gr3, but the lower 32 bits of |
| 227 | * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4). |
| 228 | * This code relies on a 32 bit pt_regs being comprised of 32 bit values |
| 229 | * except for the fp registers which (a) are 64 bits, and (b) follow |
| 230 | * the gr registers at the start of pt_regs. The 32 bit pt_regs should |
| 231 | * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[] |
| 232 | * being 64 bit in both cases. |
| 233 | */ |
| 234 | |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 235 | static compat_ulong_t translate_usr_offset(compat_ulong_t offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
Helge Deller | d0c4b05 | 2019-07-16 21:43:11 +0200 | [diff] [blame] | 237 | compat_ulong_t pos; |
| 238 | |
| 239 | if (offset < 32*4) /* gr[0..31] */ |
| 240 | pos = offset * 2 + 4; |
| 241 | else if (offset < 32*4+32*8) /* fr[0] ... fr[31] */ |
| 242 | pos = (offset - 32*4) + PT_FR0; |
| 243 | else if (offset < sizeof(struct pt_regs)/2 + 32*4) /* sr[0] ... ipsw */ |
| 244 | pos = (offset - 32*4 - 32*8) * 2 + PT_SR0 + 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | else |
Helge Deller | d0c4b05 | 2019-07-16 21:43:11 +0200 | [diff] [blame] | 246 | pos = sizeof(struct pt_regs); |
| 247 | |
| 248 | return pos; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 251 | long compat_arch_ptrace(struct task_struct *child, compat_long_t request, |
| 252 | compat_ulong_t addr, compat_ulong_t data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | { |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 254 | compat_uint_t tmp; |
| 255 | long ret = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | switch (request) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 259 | case PTRACE_PEEKUSR: |
| 260 | if (addr & (sizeof(compat_uint_t)-1)) |
| 261 | break; |
| 262 | addr = translate_usr_offset(addr); |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 263 | if (addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 264 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 266 | tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr); |
| 267 | ret = put_user(tmp, (compat_uint_t *) (unsigned long) data); |
| 268 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
| 270 | /* Write the word at location addr in the USER area. This will need |
| 271 | to change when the kernel no longer saves all regs on a syscall. |
| 272 | FIXME. There is a problem at the moment in that r3-r18 are only |
| 273 | saved if the process is ptraced on syscall entry, and even then |
| 274 | those values are overwritten by actual register values on syscall |
| 275 | exit. */ |
| 276 | case PTRACE_POKEUSR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | /* Some register values written here may be ignored in |
| 278 | * entry.S:syscall_restore_rfi; e.g. iaoq is written with |
| 279 | * r31/r31+4, and not with the values in pt_regs. |
| 280 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (addr == PT_PSW) { |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 282 | /* Since PT_PSW==0, it is valid for 32 bit processes |
| 283 | * under 64 bit kernels as well. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | */ |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 285 | ret = arch_ptrace(child, request, addr, data); |
| 286 | } else { |
| 287 | if (addr & (sizeof(compat_uint_t)-1)) |
| 288 | break; |
| 289 | addr = translate_usr_offset(addr); |
Helge Deller | ed79b86 | 2008-11-20 10:54:09 +0100 | [diff] [blame] | 290 | if (addr >= sizeof(struct pt_regs)) |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 291 | break; |
Helge Deller | d0c4b05 | 2019-07-16 21:43:11 +0200 | [diff] [blame] | 292 | if (addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4) { |
| 293 | data |= 3; /* ensure userspace privilege */ |
| 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (addr >= PT_FR0 && addr <= PT_FR31 + 4) { |
| 296 | /* Special case, fp regs are 64 bits anyway */ |
Helge Deller | d0c4b05 | 2019-07-16 21:43:11 +0200 | [diff] [blame] | 297 | *(__u32 *) ((char *) task_regs(child) + addr) = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | ret = 0; |
| 299 | } |
| 300 | else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) || |
| 301 | addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 || |
| 302 | addr == PT_SAR+4) { |
| 303 | /* Zero the top 32 bits */ |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 304 | *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0; |
| 305 | *(__u32 *) ((char *) task_regs(child) + addr) = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | ret = 0; |
| 307 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 309 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
| 311 | default: |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 312 | ret = compat_ptrace_request(child, request, addr, data); |
| 313 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | return ret; |
| 317 | } |
Helge Deller | 81e192d | 2008-10-17 18:48:36 +0000 | [diff] [blame] | 318 | #endif |
| 319 | |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame] | 320 | long do_syscall_trace_enter(struct pt_regs *regs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Dmitry V. Levin | e9f0613 | 2019-02-16 16:10:39 +0300 | [diff] [blame] | 322 | if (test_thread_flag(TIF_SYSCALL_TRACE)) { |
| 323 | int rc = tracehook_report_syscall_entry(regs); |
| 324 | |
Helge Deller | 98e8b6c | 2016-01-19 16:08:49 +0100 | [diff] [blame] | 325 | /* |
Dmitry V. Levin | e9f0613 | 2019-02-16 16:10:39 +0300 | [diff] [blame] | 326 | * As tracesys_next does not set %r28 to -ENOSYS |
| 327 | * when %r20 is set to -1, initialize it here. |
Helge Deller | 98e8b6c | 2016-01-19 16:08:49 +0100 | [diff] [blame] | 328 | */ |
Dmitry V. Levin | e9f0613 | 2019-02-16 16:10:39 +0300 | [diff] [blame] | 329 | regs->gr[28] = -ENOSYS; |
| 330 | |
| 331 | if (rc) { |
| 332 | /* |
| 333 | * A nonzero return code from |
| 334 | * tracehook_report_syscall_entry() tells us |
| 335 | * to prevent the syscall execution. Skip |
| 336 | * the syscall call and the syscall restart handling. |
| 337 | * |
| 338 | * Note that the tracer may also just change |
| 339 | * regs->gr[20] to an invalid syscall number, |
| 340 | * that is handled by tracesys_next. |
| 341 | */ |
| 342 | regs->gr[20] = -1UL; |
| 343 | return -1; |
| 344 | } |
Helge Deller | 98e8b6c | 2016-01-19 16:08:49 +0100 | [diff] [blame] | 345 | } |
Kees Cook | 375f018 | 2016-06-02 13:15:52 -0700 | [diff] [blame] | 346 | |
| 347 | /* Do the secure computing check after ptrace. */ |
| 348 | if (secure_computing(NULL) == -1) |
| 349 | return -1; |
| 350 | |
Helge Deller | fc79168 | 2016-04-13 22:44:54 +0200 | [diff] [blame] | 351 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 352 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
| 353 | trace_sys_enter(regs, regs->gr[20]); |
| 354 | #endif |
Kyle McMartin | d6b5877 | 2009-04-26 23:53:34 -0400 | [diff] [blame] | 355 | |
Helge Deller | 527973c | 2013-10-15 19:25:46 +0200 | [diff] [blame] | 356 | #ifdef CONFIG_64BIT |
| 357 | if (!is_compat_task()) |
Eric Paris | 9139740 | 2014-03-11 13:29:28 -0400 | [diff] [blame] | 358 | audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25], |
| 359 | regs->gr[24], regs->gr[23]); |
Helge Deller | 527973c | 2013-10-15 19:25:46 +0200 | [diff] [blame] | 360 | else |
| 361 | #endif |
Eric Paris | 9139740 | 2014-03-11 13:29:28 -0400 | [diff] [blame] | 362 | audit_syscall_entry(regs->gr[20] & 0xffffffff, |
Helge Deller | 527973c | 2013-10-15 19:25:46 +0200 | [diff] [blame] | 363 | regs->gr[26] & 0xffffffff, |
| 364 | regs->gr[25] & 0xffffffff, |
| 365 | regs->gr[24] & 0xffffffff, |
| 366 | regs->gr[23] & 0xffffffff); |
| 367 | |
Helge Deller | 910cd32 | 2016-03-30 14:14:31 +0200 | [diff] [blame] | 368 | /* |
| 369 | * Sign extend the syscall number to 64bit since it may have been |
| 370 | * modified by a compat ptrace call |
| 371 | */ |
| 372 | return (int) ((u32) regs->gr[20]); |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void do_syscall_trace_exit(struct pt_regs *regs) |
| 376 | { |
Kyle McMartin | ecd3d4b | 2009-09-27 23:03:02 -0400 | [diff] [blame] | 377 | int stepping = test_thread_flag(TIF_SINGLESTEP) || |
| 378 | test_thread_flag(TIF_BLOCKSTEP); |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame] | 379 | |
Helge Deller | 527973c | 2013-10-15 19:25:46 +0200 | [diff] [blame] | 380 | audit_syscall_exit(regs); |
| 381 | |
Helge Deller | fc79168 | 2016-04-13 22:44:54 +0200 | [diff] [blame] | 382 | #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS |
| 383 | if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT))) |
| 384 | trace_sys_exit(regs, regs->gr[20]); |
| 385 | #endif |
| 386 | |
Kyle McMartin | 2798af1 | 2009-07-05 14:36:16 -0400 | [diff] [blame] | 387 | if (stepping || test_thread_flag(TIF_SYSCALL_TRACE)) |
| 388 | tracehook_report_syscall_exit(regs, stepping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 390 | |
| 391 | |
| 392 | /* |
| 393 | * regset functions. |
| 394 | */ |
| 395 | |
| 396 | static int fpr_get(struct task_struct *target, |
| 397 | const struct user_regset *regset, |
| 398 | unsigned int pos, unsigned int count, |
| 399 | void *kbuf, void __user *ubuf) |
| 400 | { |
| 401 | struct pt_regs *regs = task_regs(target); |
| 402 | __u64 *k = kbuf; |
| 403 | __u64 __user *u = ubuf; |
| 404 | __u64 reg; |
| 405 | |
| 406 | pos /= sizeof(reg); |
| 407 | count /= sizeof(reg); |
| 408 | |
| 409 | if (kbuf) |
| 410 | for (; count > 0 && pos < ELF_NFPREG; --count) |
| 411 | *k++ = regs->fr[pos++]; |
| 412 | else |
| 413 | for (; count > 0 && pos < ELF_NFPREG; --count) |
| 414 | if (__put_user(regs->fr[pos++], u++)) |
| 415 | return -EFAULT; |
| 416 | |
| 417 | kbuf = k; |
| 418 | ubuf = u; |
| 419 | pos *= sizeof(reg); |
| 420 | count *= sizeof(reg); |
| 421 | return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 422 | ELF_NFPREG * sizeof(reg), -1); |
| 423 | } |
| 424 | |
| 425 | static int fpr_set(struct task_struct *target, |
| 426 | const struct user_regset *regset, |
| 427 | unsigned int pos, unsigned int count, |
| 428 | const void *kbuf, const void __user *ubuf) |
| 429 | { |
| 430 | struct pt_regs *regs = task_regs(target); |
| 431 | const __u64 *k = kbuf; |
| 432 | const __u64 __user *u = ubuf; |
| 433 | __u64 reg; |
| 434 | |
| 435 | pos /= sizeof(reg); |
| 436 | count /= sizeof(reg); |
| 437 | |
| 438 | if (kbuf) |
| 439 | for (; count > 0 && pos < ELF_NFPREG; --count) |
| 440 | regs->fr[pos++] = *k++; |
| 441 | else |
| 442 | for (; count > 0 && pos < ELF_NFPREG; --count) { |
| 443 | if (__get_user(reg, u++)) |
| 444 | return -EFAULT; |
| 445 | regs->fr[pos++] = reg; |
| 446 | } |
| 447 | |
| 448 | kbuf = k; |
| 449 | ubuf = u; |
| 450 | pos *= sizeof(reg); |
| 451 | count *= sizeof(reg); |
| 452 | return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, |
| 453 | ELF_NFPREG * sizeof(reg), -1); |
| 454 | } |
| 455 | |
| 456 | #define RI(reg) (offsetof(struct user_regs_struct,reg) / sizeof(long)) |
| 457 | |
| 458 | static unsigned long get_reg(struct pt_regs *regs, int num) |
| 459 | { |
| 460 | switch (num) { |
| 461 | case RI(gr[0]) ... RI(gr[31]): return regs->gr[num - RI(gr[0])]; |
| 462 | case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])]; |
| 463 | case RI(iasq[0]): return regs->iasq[0]; |
| 464 | case RI(iasq[1]): return regs->iasq[1]; |
| 465 | case RI(iaoq[0]): return regs->iaoq[0]; |
| 466 | case RI(iaoq[1]): return regs->iaoq[1]; |
| 467 | case RI(sar): return regs->sar; |
| 468 | case RI(iir): return regs->iir; |
| 469 | case RI(isr): return regs->isr; |
| 470 | case RI(ior): return regs->ior; |
| 471 | case RI(ipsw): return regs->ipsw; |
| 472 | case RI(cr27): return regs->cr27; |
| 473 | case RI(cr0): return mfctl(0); |
| 474 | case RI(cr24): return mfctl(24); |
| 475 | case RI(cr25): return mfctl(25); |
| 476 | case RI(cr26): return mfctl(26); |
| 477 | case RI(cr28): return mfctl(28); |
| 478 | case RI(cr29): return mfctl(29); |
| 479 | case RI(cr30): return mfctl(30); |
| 480 | case RI(cr31): return mfctl(31); |
| 481 | case RI(cr8): return mfctl(8); |
| 482 | case RI(cr9): return mfctl(9); |
| 483 | case RI(cr12): return mfctl(12); |
| 484 | case RI(cr13): return mfctl(13); |
| 485 | case RI(cr10): return mfctl(10); |
| 486 | case RI(cr15): return mfctl(15); |
| 487 | default: return 0; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | static void set_reg(struct pt_regs *regs, int num, unsigned long val) |
| 492 | { |
| 493 | switch (num) { |
| 494 | case RI(gr[0]): /* |
| 495 | * PSW is in gr[0]. |
| 496 | * Allow writing to Nullify, Divide-step-correction, |
| 497 | * and carry/borrow bits. |
| 498 | * BEWARE, if you set N, and then single step, it won't |
| 499 | * stop on the nullified instruction. |
| 500 | */ |
| 501 | val &= USER_PSW_BITS; |
| 502 | regs->gr[0] &= ~USER_PSW_BITS; |
| 503 | regs->gr[0] |= val; |
| 504 | return; |
| 505 | case RI(gr[1]) ... RI(gr[31]): |
| 506 | regs->gr[num - RI(gr[0])] = val; |
| 507 | return; |
| 508 | case RI(iaoq[0]): |
| 509 | case RI(iaoq[1]): |
Helge Deller | e7193d4 | 2019-07-04 03:44:17 +0200 | [diff] [blame] | 510 | /* set 2 lowest bits to ensure userspace privilege: */ |
| 511 | regs->iaoq[num - RI(iaoq[0])] = val | 3; |
Helge Deller | 64e2a42 | 2016-04-01 22:40:53 +0200 | [diff] [blame] | 512 | return; |
| 513 | case RI(sar): regs->sar = val; |
| 514 | return; |
| 515 | default: return; |
| 516 | #if 0 |
| 517 | /* do not allow to change any of the following registers (yet) */ |
| 518 | case RI(sr[0]) ... RI(sr[7]): return regs->sr[num - RI(sr[0])]; |
| 519 | case RI(iasq[0]): return regs->iasq[0]; |
| 520 | case RI(iasq[1]): return regs->iasq[1]; |
| 521 | case RI(iir): return regs->iir; |
| 522 | case RI(isr): return regs->isr; |
| 523 | case RI(ior): return regs->ior; |
| 524 | case RI(ipsw): return regs->ipsw; |
| 525 | case RI(cr27): return regs->cr27; |
| 526 | case cr0, cr24, cr25, cr26, cr27, cr28, cr29, cr30, cr31; |
| 527 | case cr8, cr9, cr12, cr13, cr10, cr15; |
| 528 | #endif |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | static int gpr_get(struct task_struct *target, |
| 533 | const struct user_regset *regset, |
| 534 | unsigned int pos, unsigned int count, |
| 535 | void *kbuf, void __user *ubuf) |
| 536 | { |
| 537 | struct pt_regs *regs = task_regs(target); |
| 538 | unsigned long *k = kbuf; |
| 539 | unsigned long __user *u = ubuf; |
| 540 | unsigned long reg; |
| 541 | |
| 542 | pos /= sizeof(reg); |
| 543 | count /= sizeof(reg); |
| 544 | |
| 545 | if (kbuf) |
| 546 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 547 | *k++ = get_reg(regs, pos++); |
| 548 | else |
| 549 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 550 | if (__put_user(get_reg(regs, pos++), u++)) |
| 551 | return -EFAULT; |
| 552 | kbuf = k; |
| 553 | ubuf = u; |
| 554 | pos *= sizeof(reg); |
| 555 | count *= sizeof(reg); |
| 556 | return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 557 | ELF_NGREG * sizeof(reg), -1); |
| 558 | } |
| 559 | |
| 560 | static int gpr_set(struct task_struct *target, |
| 561 | const struct user_regset *regset, |
| 562 | unsigned int pos, unsigned int count, |
| 563 | const void *kbuf, const void __user *ubuf) |
| 564 | { |
| 565 | struct pt_regs *regs = task_regs(target); |
| 566 | const unsigned long *k = kbuf; |
| 567 | const unsigned long __user *u = ubuf; |
| 568 | unsigned long reg; |
| 569 | |
| 570 | pos /= sizeof(reg); |
| 571 | count /= sizeof(reg); |
| 572 | |
| 573 | if (kbuf) |
| 574 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 575 | set_reg(regs, pos++, *k++); |
| 576 | else |
| 577 | for (; count > 0 && pos < ELF_NGREG; --count) { |
| 578 | if (__get_user(reg, u++)) |
| 579 | return -EFAULT; |
| 580 | set_reg(regs, pos++, reg); |
| 581 | } |
| 582 | |
| 583 | kbuf = k; |
| 584 | ubuf = u; |
| 585 | pos *= sizeof(reg); |
| 586 | count *= sizeof(reg); |
| 587 | return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, |
| 588 | ELF_NGREG * sizeof(reg), -1); |
| 589 | } |
| 590 | |
| 591 | static const struct user_regset native_regsets[] = { |
| 592 | [REGSET_GENERAL] = { |
| 593 | .core_note_type = NT_PRSTATUS, .n = ELF_NGREG, |
| 594 | .size = sizeof(long), .align = sizeof(long), |
| 595 | .get = gpr_get, .set = gpr_set |
| 596 | }, |
| 597 | [REGSET_FP] = { |
| 598 | .core_note_type = NT_PRFPREG, .n = ELF_NFPREG, |
| 599 | .size = sizeof(__u64), .align = sizeof(__u64), |
| 600 | .get = fpr_get, .set = fpr_set |
| 601 | } |
| 602 | }; |
| 603 | |
| 604 | static const struct user_regset_view user_parisc_native_view = { |
| 605 | .name = "parisc", .e_machine = ELF_ARCH, .ei_osabi = ELFOSABI_LINUX, |
| 606 | .regsets = native_regsets, .n = ARRAY_SIZE(native_regsets) |
| 607 | }; |
| 608 | |
| 609 | #ifdef CONFIG_64BIT |
| 610 | #include <linux/compat.h> |
| 611 | |
| 612 | static int gpr32_get(struct task_struct *target, |
| 613 | const struct user_regset *regset, |
| 614 | unsigned int pos, unsigned int count, |
| 615 | void *kbuf, void __user *ubuf) |
| 616 | { |
| 617 | struct pt_regs *regs = task_regs(target); |
| 618 | compat_ulong_t *k = kbuf; |
| 619 | compat_ulong_t __user *u = ubuf; |
| 620 | compat_ulong_t reg; |
| 621 | |
| 622 | pos /= sizeof(reg); |
| 623 | count /= sizeof(reg); |
| 624 | |
| 625 | if (kbuf) |
| 626 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 627 | *k++ = get_reg(regs, pos++); |
| 628 | else |
| 629 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 630 | if (__put_user((compat_ulong_t) get_reg(regs, pos++), u++)) |
| 631 | return -EFAULT; |
| 632 | |
| 633 | kbuf = k; |
| 634 | ubuf = u; |
| 635 | pos *= sizeof(reg); |
| 636 | count *= sizeof(reg); |
| 637 | return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, |
| 638 | ELF_NGREG * sizeof(reg), -1); |
| 639 | } |
| 640 | |
| 641 | static int gpr32_set(struct task_struct *target, |
| 642 | const struct user_regset *regset, |
| 643 | unsigned int pos, unsigned int count, |
| 644 | const void *kbuf, const void __user *ubuf) |
| 645 | { |
| 646 | struct pt_regs *regs = task_regs(target); |
| 647 | const compat_ulong_t *k = kbuf; |
| 648 | const compat_ulong_t __user *u = ubuf; |
| 649 | compat_ulong_t reg; |
| 650 | |
| 651 | pos /= sizeof(reg); |
| 652 | count /= sizeof(reg); |
| 653 | |
| 654 | if (kbuf) |
| 655 | for (; count > 0 && pos < ELF_NGREG; --count) |
| 656 | set_reg(regs, pos++, *k++); |
| 657 | else |
| 658 | for (; count > 0 && pos < ELF_NGREG; --count) { |
| 659 | if (__get_user(reg, u++)) |
| 660 | return -EFAULT; |
| 661 | set_reg(regs, pos++, reg); |
| 662 | } |
| 663 | |
| 664 | kbuf = k; |
| 665 | ubuf = u; |
| 666 | pos *= sizeof(reg); |
| 667 | count *= sizeof(reg); |
| 668 | return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, |
| 669 | ELF_NGREG * sizeof(reg), -1); |
| 670 | } |
| 671 | |
| 672 | /* |
| 673 | * These are the regset flavors matching the 32bit native set. |
| 674 | */ |
| 675 | static const struct user_regset compat_regsets[] = { |
| 676 | [REGSET_GENERAL] = { |
| 677 | .core_note_type = NT_PRSTATUS, .n = ELF_NGREG, |
| 678 | .size = sizeof(compat_long_t), .align = sizeof(compat_long_t), |
| 679 | .get = gpr32_get, .set = gpr32_set |
| 680 | }, |
| 681 | [REGSET_FP] = { |
| 682 | .core_note_type = NT_PRFPREG, .n = ELF_NFPREG, |
| 683 | .size = sizeof(__u64), .align = sizeof(__u64), |
| 684 | .get = fpr_get, .set = fpr_set |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | static const struct user_regset_view user_parisc_compat_view = { |
| 689 | .name = "parisc", .e_machine = EM_PARISC, .ei_osabi = ELFOSABI_LINUX, |
| 690 | .regsets = compat_regsets, .n = ARRAY_SIZE(compat_regsets) |
| 691 | }; |
| 692 | #endif /* CONFIG_64BIT */ |
| 693 | |
| 694 | const struct user_regset_view *task_user_regset_view(struct task_struct *task) |
| 695 | { |
| 696 | BUILD_BUG_ON(sizeof(struct user_regs_struct)/sizeof(long) != ELF_NGREG); |
| 697 | BUILD_BUG_ON(sizeof(struct user_fp_struct)/sizeof(__u64) != ELF_NFPREG); |
| 698 | #ifdef CONFIG_64BIT |
| 699 | if (is_compat_task()) |
| 700 | return &user_parisc_compat_view; |
| 701 | #endif |
| 702 | return &user_parisc_native_view; |
| 703 | } |