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