Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * arch/ppc/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 | |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/sched.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/smp.h> |
| 24 | #include <linux/smp_lock.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/ptrace.h> |
| 27 | #include <linux/user.h> |
| 28 | #include <linux/security.h> |
| 29 | |
| 30 | #include <asm/uaccess.h> |
| 31 | #include <asm/page.h> |
| 32 | #include <asm/pgtable.h> |
| 33 | #include <asm/system.h> |
| 34 | |
| 35 | /* |
| 36 | * Set of msr bits that gdb can change on behalf of a process. |
| 37 | */ |
| 38 | #if defined(CONFIG_40x) || defined(CONFIG_BOOKE) |
| 39 | #define MSR_DEBUGCHANGE 0 |
| 40 | #else |
| 41 | #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE) |
| 42 | #endif |
| 43 | |
| 44 | /* |
| 45 | * does not yet catch signals sent when the child dies. |
| 46 | * in exit.c or in signal.c. |
| 47 | */ |
| 48 | |
| 49 | /* |
| 50 | * Get contents of register REGNO in task TASK. |
| 51 | */ |
| 52 | static inline unsigned long get_reg(struct task_struct *task, int regno) |
| 53 | { |
| 54 | if (regno < sizeof(struct pt_regs) / sizeof(unsigned long) |
| 55 | && task->thread.regs != NULL) |
| 56 | return ((unsigned long *)task->thread.regs)[regno]; |
| 57 | return (0); |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * Write contents of register REGNO in task TASK. |
| 62 | */ |
| 63 | static inline int put_reg(struct task_struct *task, int regno, |
| 64 | unsigned long data) |
| 65 | { |
| 66 | if (regno <= PT_MQ && task->thread.regs != NULL) { |
| 67 | if (regno == PT_MSR) |
| 68 | data = (data & MSR_DEBUGCHANGE) |
| 69 | | (task->thread.regs->msr & ~MSR_DEBUGCHANGE); |
| 70 | ((unsigned long *)task->thread.regs)[regno] = data; |
| 71 | return 0; |
| 72 | } |
| 73 | return -EIO; |
| 74 | } |
| 75 | |
| 76 | #ifdef CONFIG_ALTIVEC |
| 77 | /* |
| 78 | * Get contents of AltiVec register state in task TASK |
| 79 | */ |
| 80 | static inline int get_vrregs(unsigned long __user *data, struct task_struct *task) |
| 81 | { |
| 82 | int i, j; |
| 83 | |
| 84 | if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long))) |
| 85 | return -EFAULT; |
| 86 | |
| 87 | /* copy AltiVec registers VR[0] .. VR[31] */ |
| 88 | for (i = 0; i < 32; i++) |
| 89 | for (j = 0; j < 4; j++, data++) |
| 90 | if (__put_user(task->thread.vr[i].u[j], data)) |
| 91 | return -EFAULT; |
| 92 | |
| 93 | /* copy VSCR */ |
| 94 | for (i = 0; i < 4; i++, data++) |
| 95 | if (__put_user(task->thread.vscr.u[i], data)) |
| 96 | return -EFAULT; |
| 97 | |
| 98 | /* copy VRSAVE */ |
| 99 | if (__put_user(task->thread.vrsave, data)) |
| 100 | return -EFAULT; |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Write contents of AltiVec register state into task TASK. |
| 107 | */ |
| 108 | static inline int set_vrregs(struct task_struct *task, unsigned long __user *data) |
| 109 | { |
| 110 | int i, j; |
| 111 | |
| 112 | if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long))) |
| 113 | return -EFAULT; |
| 114 | |
| 115 | /* copy AltiVec registers VR[0] .. VR[31] */ |
| 116 | for (i = 0; i < 32; i++) |
| 117 | for (j = 0; j < 4; j++, data++) |
| 118 | if (__get_user(task->thread.vr[i].u[j], data)) |
| 119 | return -EFAULT; |
| 120 | |
| 121 | /* copy VSCR */ |
| 122 | for (i = 0; i < 4; i++, data++) |
| 123 | if (__get_user(task->thread.vscr.u[i], data)) |
| 124 | return -EFAULT; |
| 125 | |
| 126 | /* copy VRSAVE */ |
| 127 | if (__get_user(task->thread.vrsave, data)) |
| 128 | return -EFAULT; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #ifdef CONFIG_SPE |
| 135 | |
| 136 | /* |
| 137 | * For get_evrregs/set_evrregs functions 'data' has the following layout: |
| 138 | * |
| 139 | * struct { |
| 140 | * u32 evr[32]; |
| 141 | * u64 acc; |
| 142 | * u32 spefscr; |
| 143 | * } |
| 144 | */ |
| 145 | |
| 146 | /* |
| 147 | * Get contents of SPE register state in task TASK. |
| 148 | */ |
| 149 | static inline int get_evrregs(unsigned long *data, struct task_struct *task) |
| 150 | { |
| 151 | int i; |
| 152 | |
| 153 | if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long))) |
| 154 | return -EFAULT; |
| 155 | |
| 156 | /* copy SPEFSCR */ |
| 157 | if (__put_user(task->thread.spefscr, &data[34])) |
| 158 | return -EFAULT; |
| 159 | |
| 160 | /* copy SPE registers EVR[0] .. EVR[31] */ |
| 161 | for (i = 0; i < 32; i++, data++) |
| 162 | if (__put_user(task->thread.evr[i], data)) |
| 163 | return -EFAULT; |
| 164 | |
| 165 | /* copy ACC */ |
| 166 | if (__put_user64(task->thread.acc, (unsigned long long *)data)) |
| 167 | return -EFAULT; |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Write contents of SPE register state into task TASK. |
| 174 | */ |
| 175 | static inline int set_evrregs(struct task_struct *task, unsigned long *data) |
| 176 | { |
| 177 | int i; |
| 178 | |
| 179 | if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long))) |
| 180 | return -EFAULT; |
| 181 | |
| 182 | /* copy SPEFSCR */ |
| 183 | if (__get_user(task->thread.spefscr, &data[34])) |
| 184 | return -EFAULT; |
| 185 | |
| 186 | /* copy SPE registers EVR[0] .. EVR[31] */ |
| 187 | for (i = 0; i < 32; i++, data++) |
| 188 | if (__get_user(task->thread.evr[i], data)) |
| 189 | return -EFAULT; |
| 190 | /* copy ACC */ |
| 191 | if (__get_user64(task->thread.acc, (unsigned long long*)data)) |
| 192 | return -EFAULT; |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | #endif /* CONFIG_SPE */ |
| 197 | |
| 198 | static inline void |
| 199 | set_single_step(struct task_struct *task) |
| 200 | { |
| 201 | struct pt_regs *regs = task->thread.regs; |
| 202 | |
| 203 | if (regs != NULL) { |
| 204 | #if defined(CONFIG_40x) || defined(CONFIG_BOOKE) |
| 205 | task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC; |
| 206 | regs->msr |= MSR_DE; |
| 207 | #else |
| 208 | regs->msr |= MSR_SE; |
| 209 | #endif |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | static inline void |
| 214 | clear_single_step(struct task_struct *task) |
| 215 | { |
| 216 | struct pt_regs *regs = task->thread.regs; |
| 217 | |
| 218 | if (regs != NULL) { |
| 219 | #if defined(CONFIG_40x) || defined(CONFIG_BOOKE) |
| 220 | task->thread.dbcr0 = 0; |
| 221 | regs->msr &= ~MSR_DE; |
| 222 | #else |
| 223 | regs->msr &= ~MSR_SE; |
| 224 | #endif |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Called by kernel/ptrace.c when detaching.. |
| 230 | * |
| 231 | * Make sure single step bits etc are not set. |
| 232 | */ |
| 233 | void ptrace_disable(struct task_struct *child) |
| 234 | { |
| 235 | /* make sure the single step bit is not set. */ |
| 236 | clear_single_step(child); |
| 237 | } |
| 238 | |
| 239 | int sys_ptrace(long request, long pid, long addr, long data) |
| 240 | { |
| 241 | struct task_struct *child; |
| 242 | int ret = -EPERM; |
| 243 | |
| 244 | lock_kernel(); |
| 245 | if (request == PTRACE_TRACEME) { |
| 246 | /* are we already being traced? */ |
| 247 | if (current->ptrace & PT_PTRACED) |
| 248 | goto out; |
| 249 | ret = security_ptrace(current->parent, current); |
| 250 | if (ret) |
| 251 | goto out; |
| 252 | /* set the ptrace bit in the process flags. */ |
| 253 | current->ptrace |= PT_PTRACED; |
| 254 | ret = 0; |
| 255 | goto out; |
| 256 | } |
| 257 | ret = -ESRCH; |
| 258 | read_lock(&tasklist_lock); |
| 259 | child = find_task_by_pid(pid); |
| 260 | if (child) |
| 261 | get_task_struct(child); |
| 262 | read_unlock(&tasklist_lock); |
| 263 | if (!child) |
| 264 | goto out; |
| 265 | |
| 266 | ret = -EPERM; |
| 267 | if (pid == 1) /* you may not mess with init */ |
| 268 | goto out_tsk; |
| 269 | |
| 270 | if (request == PTRACE_ATTACH) { |
| 271 | ret = ptrace_attach(child); |
| 272 | goto out_tsk; |
| 273 | } |
| 274 | |
| 275 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 276 | if (ret < 0) |
| 277 | goto out_tsk; |
| 278 | |
| 279 | switch (request) { |
| 280 | /* when I and D space are separate, these will need to be fixed. */ |
| 281 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 282 | case PTRACE_PEEKDATA: { |
| 283 | unsigned long tmp; |
| 284 | int copied; |
| 285 | |
| 286 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 287 | ret = -EIO; |
| 288 | if (copied != sizeof(tmp)) |
| 289 | break; |
| 290 | ret = put_user(tmp,(unsigned long __user *) data); |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | /* read the word at location addr in the USER area. */ |
| 295 | /* XXX this will need fixing for 64-bit */ |
| 296 | case PTRACE_PEEKUSR: { |
| 297 | unsigned long index, tmp; |
| 298 | |
| 299 | ret = -EIO; |
| 300 | /* convert to index and check */ |
| 301 | index = (unsigned long) addr >> 2; |
| 302 | if ((addr & 3) || index > PT_FPSCR |
| 303 | || child->thread.regs == NULL) |
| 304 | break; |
| 305 | |
| 306 | CHECK_FULL_REGS(child->thread.regs); |
| 307 | if (index < PT_FPR0) { |
| 308 | tmp = get_reg(child, (int) index); |
| 309 | } else { |
| 310 | preempt_disable(); |
| 311 | if (child->thread.regs->msr & MSR_FP) |
| 312 | giveup_fpu(child); |
| 313 | preempt_enable(); |
| 314 | tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0]; |
| 315 | } |
| 316 | ret = put_user(tmp,(unsigned long __user *) data); |
| 317 | break; |
| 318 | } |
| 319 | |
| 320 | /* If I and D space are separate, this will have to be fixed. */ |
| 321 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 322 | case PTRACE_POKEDATA: |
| 323 | ret = 0; |
| 324 | if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) |
| 325 | break; |
| 326 | ret = -EIO; |
| 327 | break; |
| 328 | |
| 329 | /* write the word at location addr in the USER area */ |
| 330 | case PTRACE_POKEUSR: { |
| 331 | unsigned long index; |
| 332 | |
| 333 | ret = -EIO; |
| 334 | /* convert to index and check */ |
| 335 | index = (unsigned long) addr >> 2; |
| 336 | if ((addr & 3) || index > PT_FPSCR |
| 337 | || child->thread.regs == NULL) |
| 338 | break; |
| 339 | |
| 340 | CHECK_FULL_REGS(child->thread.regs); |
| 341 | if (index == PT_ORIG_R3) |
| 342 | break; |
| 343 | if (index < PT_FPR0) { |
| 344 | ret = put_reg(child, index, data); |
| 345 | } else { |
| 346 | preempt_disable(); |
| 347 | if (child->thread.regs->msr & MSR_FP) |
| 348 | giveup_fpu(child); |
| 349 | preempt_enable(); |
| 350 | ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data; |
| 351 | ret = 0; |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 357 | case PTRACE_CONT: { /* restart after signal. */ |
| 358 | ret = -EIO; |
| 359 | if ((unsigned long) data > _NSIG) |
| 360 | break; |
| 361 | if (request == PTRACE_SYSCALL) { |
| 362 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 363 | } else { |
| 364 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 365 | } |
| 366 | child->exit_code = data; |
| 367 | /* make sure the single step bit is not set. */ |
| 368 | clear_single_step(child); |
| 369 | wake_up_process(child); |
| 370 | ret = 0; |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * make the child exit. Best I can do is send it a sigkill. |
| 376 | * perhaps it should be put in the status that it wants to |
| 377 | * exit. |
| 378 | */ |
| 379 | case PTRACE_KILL: { |
| 380 | ret = 0; |
| 381 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 382 | break; |
| 383 | child->exit_code = SIGKILL; |
| 384 | /* make sure the single step bit is not set. */ |
| 385 | clear_single_step(child); |
| 386 | wake_up_process(child); |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | case PTRACE_SINGLESTEP: { /* set the trap flag. */ |
| 391 | ret = -EIO; |
| 392 | if ((unsigned long) data > _NSIG) |
| 393 | break; |
| 394 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 395 | set_single_step(child); |
| 396 | child->exit_code = data; |
| 397 | /* give it a chance to run. */ |
| 398 | wake_up_process(child); |
| 399 | ret = 0; |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | case PTRACE_DETACH: |
| 404 | ret = ptrace_detach(child, data); |
| 405 | break; |
| 406 | |
| 407 | #ifdef CONFIG_ALTIVEC |
| 408 | case PTRACE_GETVRREGS: |
| 409 | /* Get the child altivec register state. */ |
| 410 | preempt_disable(); |
| 411 | if (child->thread.regs->msr & MSR_VEC) |
| 412 | giveup_altivec(child); |
| 413 | preempt_enable(); |
| 414 | ret = get_vrregs((unsigned long __user *)data, child); |
| 415 | break; |
| 416 | |
| 417 | case PTRACE_SETVRREGS: |
| 418 | /* Set the child altivec register state. */ |
| 419 | /* this is to clear the MSR_VEC bit to force a reload |
| 420 | * of register state from memory */ |
| 421 | preempt_disable(); |
| 422 | if (child->thread.regs->msr & MSR_VEC) |
| 423 | giveup_altivec(child); |
| 424 | preempt_enable(); |
| 425 | ret = set_vrregs(child, (unsigned long __user *)data); |
| 426 | break; |
| 427 | #endif |
| 428 | #ifdef CONFIG_SPE |
| 429 | case PTRACE_GETEVRREGS: |
| 430 | /* Get the child spe register state. */ |
| 431 | if (child->thread.regs->msr & MSR_SPE) |
| 432 | giveup_spe(child); |
| 433 | ret = get_evrregs((unsigned long __user *)data, child); |
| 434 | break; |
| 435 | |
| 436 | case PTRACE_SETEVRREGS: |
| 437 | /* Set the child spe register state. */ |
| 438 | /* this is to clear the MSR_SPE bit to force a reload |
| 439 | * of register state from memory */ |
| 440 | if (child->thread.regs->msr & MSR_SPE) |
| 441 | giveup_spe(child); |
| 442 | ret = set_evrregs(child, (unsigned long __user *)data); |
| 443 | break; |
| 444 | #endif |
| 445 | |
| 446 | default: |
| 447 | ret = ptrace_request(child, request, addr, data); |
| 448 | break; |
| 449 | } |
| 450 | out_tsk: |
| 451 | put_task_struct(child); |
| 452 | out: |
| 453 | unlock_kernel(); |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | void do_syscall_trace(void) |
| 458 | { |
| 459 | if (!test_thread_flag(TIF_SYSCALL_TRACE) |
| 460 | || !(current->ptrace & PT_PTRACED)) |
| 461 | return; |
| 462 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 463 | ? 0x80 : 0)); |
| 464 | |
| 465 | /* |
| 466 | * this isn't the same as continuing with a signal, but it will do |
| 467 | * for normal use. strace only continues with a signal if the |
| 468 | * stopping signal is not SIGTRAP. -brl |
| 469 | */ |
| 470 | if (current->exit_code) { |
| 471 | send_sig(current->exit_code, current, 1); |
| 472 | current->exit_code = 0; |
| 473 | } |
| 474 | } |