| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/ppc64/kernel/ptrace.c |
| 3 | * |
| 4 | * PowerPC version |
| 5 | * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org) |
| 6 | * |
| 7 | * Derived from "arch/m68k/kernel/ptrace.c" |
| 8 | * Copyright (C) 1994 by Hamish Macdonald |
| 9 | * Taken from linux/kernel/ptrace.c and modified for M680x0. |
| 10 | * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds |
| 11 | * |
| 12 | * Modified by Cort Dougan (cort@hq.fsmlabs.com) |
| 13 | * and Paul Mackerras (paulus@linuxcare.com.au). |
| 14 | * |
| 15 | * This file is subject to the terms and conditions of the GNU General |
| 16 | * Public License. See the file README.legal in the main directory of |
| 17 | * this archive for more details. |
| 18 | */ |
| 19 | |
| Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 20 | #include <linux/config.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/sched.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/smp.h> |
| 25 | #include <linux/smp_lock.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/ptrace.h> |
| 28 | #include <linux/user.h> |
| 29 | #include <linux/security.h> |
| 30 | #include <linux/audit.h> |
| 31 | #include <linux/seccomp.h> |
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 32 | #include <linux/signal.h> |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include <asm/uaccess.h> |
| 35 | #include <asm/page.h> |
| 36 | #include <asm/pgtable.h> |
| 37 | #include <asm/system.h> |
| 38 | #include <asm/ptrace-common.h> |
| 39 | |
| 40 | /* |
| 41 | * does not yet catch signals sent when the child dies. |
| 42 | * in exit.c or in signal.c. |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * Called by kernel/ptrace.c when detaching.. |
| 47 | * |
| 48 | * Make sure single step bits etc are not set. |
| 49 | */ |
| 50 | void ptrace_disable(struct task_struct *child) |
| 51 | { |
| 52 | /* make sure the single step bit is not set. */ |
| 53 | clear_single_step(child); |
| 54 | } |
| 55 | |
| 56 | int sys_ptrace(long request, long pid, long addr, long data) |
| 57 | { |
| 58 | struct task_struct *child; |
| 59 | int ret = -EPERM; |
| 60 | |
| 61 | lock_kernel(); |
| 62 | if (request == PTRACE_TRACEME) { |
| 63 | /* are we already being traced? */ |
| 64 | if (current->ptrace & PT_PTRACED) |
| 65 | goto out; |
| 66 | ret = security_ptrace(current->parent, current); |
| 67 | if (ret) |
| 68 | goto out; |
| 69 | /* set the ptrace bit in the process flags. */ |
| 70 | current->ptrace |= PT_PTRACED; |
| 71 | ret = 0; |
| 72 | goto out; |
| 73 | } |
| 74 | ret = -ESRCH; |
| 75 | read_lock(&tasklist_lock); |
| 76 | child = find_task_by_pid(pid); |
| 77 | if (child) |
| 78 | get_task_struct(child); |
| 79 | read_unlock(&tasklist_lock); |
| 80 | if (!child) |
| 81 | goto out; |
| 82 | |
| 83 | ret = -EPERM; |
| 84 | if (pid == 1) /* you may not mess with init */ |
| 85 | goto out_tsk; |
| 86 | |
| 87 | if (request == PTRACE_ATTACH) { |
| 88 | ret = ptrace_attach(child); |
| 89 | goto out_tsk; |
| 90 | } |
| 91 | |
| 92 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 93 | if (ret < 0) |
| 94 | goto out_tsk; |
| 95 | |
| 96 | switch (request) { |
| 97 | /* when I and D space are separate, these will need to be fixed. */ |
| 98 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 99 | case PTRACE_PEEKDATA: { |
| 100 | unsigned long tmp; |
| 101 | int copied; |
| 102 | |
| 103 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 104 | ret = -EIO; |
| 105 | if (copied != sizeof(tmp)) |
| 106 | break; |
| 107 | ret = put_user(tmp,(unsigned long __user *) data); |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | /* read the word at location addr in the USER area. */ |
| 112 | case PTRACE_PEEKUSR: { |
| 113 | unsigned long index; |
| 114 | unsigned long tmp; |
| 115 | |
| 116 | ret = -EIO; |
| 117 | /* convert to index and check */ |
| 118 | index = (unsigned long) addr >> 3; |
| 119 | if ((addr & 7) || (index > PT_FPSCR)) |
| 120 | break; |
| 121 | |
| 122 | if (index < PT_FPR0) { |
| 123 | tmp = get_reg(child, (int)index); |
| 124 | } else { |
| 125 | flush_fp_to_thread(child); |
| 126 | tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0]; |
| 127 | } |
| 128 | ret = put_user(tmp,(unsigned long __user *) data); |
| 129 | break; |
| 130 | } |
| 131 | |
| 132 | /* If I and D space are separate, this will have to be fixed. */ |
| 133 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 134 | case PTRACE_POKEDATA: |
| 135 | ret = 0; |
| 136 | if (access_process_vm(child, addr, &data, sizeof(data), 1) |
| 137 | == sizeof(data)) |
| 138 | break; |
| 139 | ret = -EIO; |
| 140 | break; |
| 141 | |
| 142 | /* write the word at location addr in the USER area */ |
| 143 | case PTRACE_POKEUSR: { |
| 144 | unsigned long index; |
| 145 | |
| 146 | ret = -EIO; |
| 147 | /* convert to index and check */ |
| 148 | index = (unsigned long) addr >> 3; |
| 149 | if ((addr & 7) || (index > PT_FPSCR)) |
| 150 | break; |
| 151 | |
| 152 | if (index == PT_ORIG_R3) |
| 153 | break; |
| 154 | if (index < PT_FPR0) { |
| 155 | ret = put_reg(child, index, data); |
| 156 | } else { |
| 157 | flush_fp_to_thread(child); |
| 158 | ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data; |
| 159 | ret = 0; |
| 160 | } |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 165 | case PTRACE_CONT: { /* restart after signal. */ |
| 166 | ret = -EIO; |
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 167 | if (!valid_signal(data)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | break; |
| 169 | if (request == PTRACE_SYSCALL) |
| 170 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 171 | else |
| 172 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 173 | child->exit_code = data; |
| 174 | /* make sure the single step bit is not set. */ |
| 175 | clear_single_step(child); |
| 176 | wake_up_process(child); |
| 177 | ret = 0; |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | /* |
| 182 | * make the child exit. Best I can do is send it a sigkill. |
| 183 | * perhaps it should be put in the status that it wants to |
| 184 | * exit. |
| 185 | */ |
| 186 | case PTRACE_KILL: { |
| 187 | ret = 0; |
| 188 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 189 | break; |
| 190 | child->exit_code = SIGKILL; |
| 191 | /* make sure the single step bit is not set. */ |
| 192 | clear_single_step(child); |
| 193 | wake_up_process(child); |
| 194 | break; |
| 195 | } |
| 196 | |
| 197 | case PTRACE_SINGLESTEP: { /* set the trap flag. */ |
| 198 | ret = -EIO; |
| Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 199 | if (!valid_signal(data)) |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | break; |
| 201 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 202 | set_single_step(child); |
| 203 | child->exit_code = data; |
| 204 | /* give it a chance to run. */ |
| 205 | wake_up_process(child); |
| 206 | ret = 0; |
| 207 | break; |
| 208 | } |
| 209 | |
| Anton Blanchard | fd9648d | 2005-09-10 16:01:11 +1000 | [diff] [blame] | 210 | case PTRACE_GET_DEBUGREG: { |
| 211 | ret = -EINVAL; |
| 212 | /* We only support one DABR and no IABRS at the moment */ |
| 213 | if (addr > 0) |
| 214 | break; |
| 215 | ret = put_user(child->thread.dabr, |
| 216 | (unsigned long __user *)data); |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | case PTRACE_SET_DEBUGREG: |
| 221 | ret = ptrace_set_debugreg(child, addr, data); |
| 222 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | case PTRACE_DETACH: |
| 224 | ret = ptrace_detach(child, data); |
| 225 | break; |
| 226 | |
| 227 | case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */ |
| 228 | int i; |
| 229 | unsigned long *reg = &((unsigned long *)child->thread.regs)[0]; |
| 230 | unsigned long __user *tmp = (unsigned long __user *)addr; |
| 231 | |
| 232 | for (i = 0; i < 32; i++) { |
| 233 | ret = put_user(*reg, tmp); |
| 234 | if (ret) |
| 235 | break; |
| 236 | reg++; |
| 237 | tmp++; |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | |
| 242 | case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */ |
| 243 | int i; |
| 244 | unsigned long *reg = &((unsigned long *)child->thread.regs)[0]; |
| 245 | unsigned long __user *tmp = (unsigned long __user *)addr; |
| 246 | |
| 247 | for (i = 0; i < 32; i++) { |
| 248 | ret = get_user(*reg, tmp); |
| 249 | if (ret) |
| 250 | break; |
| 251 | reg++; |
| 252 | tmp++; |
| 253 | } |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */ |
| 258 | int i; |
| 259 | unsigned long *reg = &((unsigned long *)child->thread.fpr)[0]; |
| 260 | unsigned long __user *tmp = (unsigned long __user *)addr; |
| 261 | |
| 262 | flush_fp_to_thread(child); |
| 263 | |
| 264 | for (i = 0; i < 32; i++) { |
| 265 | ret = put_user(*reg, tmp); |
| 266 | if (ret) |
| 267 | break; |
| 268 | reg++; |
| 269 | tmp++; |
| 270 | } |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */ |
| 275 | int i; |
| 276 | unsigned long *reg = &((unsigned long *)child->thread.fpr)[0]; |
| 277 | unsigned long __user *tmp = (unsigned long __user *)addr; |
| 278 | |
| 279 | flush_fp_to_thread(child); |
| 280 | |
| 281 | for (i = 0; i < 32; i++) { |
| 282 | ret = get_user(*reg, tmp); |
| 283 | if (ret) |
| 284 | break; |
| 285 | reg++; |
| 286 | tmp++; |
| 287 | } |
| 288 | break; |
| 289 | } |
| 290 | |
| Robert Jennings | 962bca7 | 2005-09-10 16:01:07 +1000 | [diff] [blame] | 291 | #ifdef CONFIG_ALTIVEC |
| 292 | case PTRACE_GETVRREGS: |
| 293 | /* Get the child altivec register state. */ |
| 294 | flush_altivec_to_thread(child); |
| 295 | ret = get_vrregs((unsigned long __user *)data, child); |
| 296 | break; |
| 297 | |
| 298 | case PTRACE_SETVRREGS: |
| 299 | /* Set the child altivec register state. */ |
| 300 | flush_altivec_to_thread(child); |
| 301 | ret = set_vrregs(child, (unsigned long __user *)data); |
| 302 | break; |
| 303 | #endif |
| 304 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | default: |
| 306 | ret = ptrace_request(child, request, addr, data); |
| 307 | break; |
| 308 | } |
| 309 | out_tsk: |
| 310 | put_task_struct(child); |
| 311 | out: |
| 312 | unlock_kernel(); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static void do_syscall_trace(void) |
| 317 | { |
| 318 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 319 | between a syscall stop and SIGTRAP delivery */ |
| 320 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 321 | ? 0x80 : 0)); |
| 322 | |
| 323 | /* |
| 324 | * this isn't the same as continuing with a signal, but it will do |
| 325 | * for normal use. strace only continues with a signal if the |
| 326 | * stopping signal is not SIGTRAP. -brl |
| 327 | */ |
| 328 | if (current->exit_code) { |
| 329 | send_sig(current->exit_code, current, 1); |
| 330 | current->exit_code = 0; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void do_syscall_trace_enter(struct pt_regs *regs) |
| 335 | { |
| Andrea Arcangeli | 7286aa9 | 2005-06-23 22:00:45 -0700 | [diff] [blame] | 336 | secure_computing(regs->gpr[0]); |
| 337 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | if (test_thread_flag(TIF_SYSCALL_TRACE) |
| 339 | && (current->ptrace & PT_PTRACED)) |
| 340 | do_syscall_trace(); |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 341 | |
| 342 | if (unlikely(current->audit_context)) |
| 343 | audit_syscall_entry(current, |
| 344 | test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64, |
| 345 | regs->gpr[0], |
| 346 | regs->gpr[3], regs->gpr[4], |
| 347 | regs->gpr[5], regs->gpr[6]); |
| 348 | |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | void do_syscall_trace_leave(struct pt_regs *regs) |
| 352 | { |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | if (unlikely(current->audit_context)) |
| 2fd6f58 | 2005-04-29 16:08:28 +0100 | [diff] [blame] | 354 | audit_syscall_exit(current, |
| 355 | (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS, |
| 356 | regs->result); |
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | if ((test_thread_flag(TIF_SYSCALL_TRACE) |
| 359 | || test_thread_flag(TIF_SINGLESTEP)) |
| 360 | && (current->ptrace & PT_PTRACED)) |
| 361 | do_syscall_trace(); |
| 362 | } |