Merge branch 'master'
[linux-2.6/verdex.git] / arch / powerpc / kernel / ptrace.c
blob400793c7130466bc535f02fff0acc32db9b833c2
1 /*
2 * PowerPC version
3 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
5 * Derived from "arch/m68k/kernel/ptrace.c"
6 * Copyright (C) 1994 by Hamish Macdonald
7 * Taken from linux/kernel/ptrace.c and modified for M680x0.
8 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
10 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
11 * and Paul Mackerras (paulus@samba.org).
13 * This file is subject to the terms and conditions of the GNU General
14 * Public License. See the file README.legal in the main directory of
15 * this archive for more details.
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 #include <linux/security.h>
28 #include <linux/signal.h>
29 #include <linux/seccomp.h>
30 #include <linux/audit.h>
31 #ifdef CONFIG_PPC32
32 #include <linux/module.h>
33 #endif
35 #include <asm/uaccess.h>
36 #include <asm/page.h>
37 #include <asm/pgtable.h>
38 #include <asm/system.h>
40 #ifdef CONFIG_PPC64
41 #include "ptrace-common.h"
42 #endif
44 #ifdef CONFIG_PPC32
46 * Set of msr bits that gdb can change on behalf of a process.
48 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
49 #define MSR_DEBUGCHANGE 0
50 #else
51 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
52 #endif
53 #endif /* CONFIG_PPC32 */
56 * does not yet catch signals sent when the child dies.
57 * in exit.c or in signal.c.
60 #ifdef CONFIG_PPC32
62 * Get contents of register REGNO in task TASK.
64 static inline unsigned long get_reg(struct task_struct *task, int regno)
66 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
67 && task->thread.regs != NULL)
68 return ((unsigned long *)task->thread.regs)[regno];
69 return (0);
73 * Write contents of register REGNO in task TASK.
75 static inline int put_reg(struct task_struct *task, int regno,
76 unsigned long data)
78 if (regno <= PT_MQ && task->thread.regs != NULL) {
79 if (regno == PT_MSR)
80 data = (data & MSR_DEBUGCHANGE)
81 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
82 ((unsigned long *)task->thread.regs)[regno] = data;
83 return 0;
85 return -EIO;
88 #ifdef CONFIG_ALTIVEC
90 * Get contents of AltiVec register state in task TASK
92 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
94 int i, j;
96 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
97 return -EFAULT;
99 /* copy AltiVec registers VR[0] .. VR[31] */
100 for (i = 0; i < 32; i++)
101 for (j = 0; j < 4; j++, data++)
102 if (__put_user(task->thread.vr[i].u[j], data))
103 return -EFAULT;
105 /* copy VSCR */
106 for (i = 0; i < 4; i++, data++)
107 if (__put_user(task->thread.vscr.u[i], data))
108 return -EFAULT;
110 /* copy VRSAVE */
111 if (__put_user(task->thread.vrsave, data))
112 return -EFAULT;
114 return 0;
118 * Write contents of AltiVec register state into task TASK.
120 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
122 int i, j;
124 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
125 return -EFAULT;
127 /* copy AltiVec registers VR[0] .. VR[31] */
128 for (i = 0; i < 32; i++)
129 for (j = 0; j < 4; j++, data++)
130 if (__get_user(task->thread.vr[i].u[j], data))
131 return -EFAULT;
133 /* copy VSCR */
134 for (i = 0; i < 4; i++, data++)
135 if (__get_user(task->thread.vscr.u[i], data))
136 return -EFAULT;
138 /* copy VRSAVE */
139 if (__get_user(task->thread.vrsave, data))
140 return -EFAULT;
142 return 0;
144 #endif
146 #ifdef CONFIG_SPE
149 * For get_evrregs/set_evrregs functions 'data' has the following layout:
151 * struct {
152 * u32 evr[32];
153 * u64 acc;
154 * u32 spefscr;
159 * Get contents of SPE register state in task TASK.
161 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
163 int i;
165 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
166 return -EFAULT;
168 /* copy SPEFSCR */
169 if (__put_user(task->thread.spefscr, &data[34]))
170 return -EFAULT;
172 /* copy SPE registers EVR[0] .. EVR[31] */
173 for (i = 0; i < 32; i++, data++)
174 if (__put_user(task->thread.evr[i], data))
175 return -EFAULT;
177 /* copy ACC */
178 if (__put_user64(task->thread.acc, (unsigned long long *)data))
179 return -EFAULT;
181 return 0;
185 * Write contents of SPE register state into task TASK.
187 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
189 int i;
191 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
192 return -EFAULT;
194 /* copy SPEFSCR */
195 if (__get_user(task->thread.spefscr, &data[34]))
196 return -EFAULT;
198 /* copy SPE registers EVR[0] .. EVR[31] */
199 for (i = 0; i < 32; i++, data++)
200 if (__get_user(task->thread.evr[i], data))
201 return -EFAULT;
202 /* copy ACC */
203 if (__get_user64(task->thread.acc, (unsigned long long*)data))
204 return -EFAULT;
206 return 0;
208 #endif /* CONFIG_SPE */
210 static inline void
211 set_single_step(struct task_struct *task)
213 struct pt_regs *regs = task->thread.regs;
215 if (regs != NULL) {
216 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
217 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
218 regs->msr |= MSR_DE;
219 #else
220 regs->msr |= MSR_SE;
221 #endif
225 static inline void
226 clear_single_step(struct task_struct *task)
228 struct pt_regs *regs = task->thread.regs;
230 if (regs != NULL) {
231 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
232 task->thread.dbcr0 = 0;
233 regs->msr &= ~MSR_DE;
234 #else
235 regs->msr &= ~MSR_SE;
236 #endif
239 #endif /* CONFIG_PPC32 */
242 * Called by kernel/ptrace.c when detaching..
244 * Make sure single step bits etc are not set.
246 void ptrace_disable(struct task_struct *child)
248 /* make sure the single step bit is not set. */
249 clear_single_step(child);
252 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
254 int ret = -EPERM;
256 switch (request) {
257 /* when I and D space are separate, these will need to be fixed. */
258 case PTRACE_PEEKTEXT: /* read word at location addr. */
259 case PTRACE_PEEKDATA: {
260 unsigned long tmp;
261 int copied;
263 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
264 ret = -EIO;
265 if (copied != sizeof(tmp))
266 break;
267 ret = put_user(tmp,(unsigned long __user *) data);
268 break;
271 /* read the word at location addr in the USER area. */
272 case PTRACE_PEEKUSR: {
273 unsigned long index, tmp;
275 ret = -EIO;
276 /* convert to index and check */
277 #ifdef CONFIG_PPC32
278 index = (unsigned long) addr >> 2;
279 if ((addr & 3) || (index > PT_FPSCR)
280 || (child->thread.regs == NULL))
281 #else
282 index = (unsigned long) addr >> 3;
283 if ((addr & 7) || (index > PT_FPSCR))
284 #endif
285 break;
287 #ifdef CONFIG_PPC32
288 CHECK_FULL_REGS(child->thread.regs);
289 #endif
290 if (index < PT_FPR0) {
291 tmp = get_reg(child, (int) index);
292 } else {
293 flush_fp_to_thread(child);
294 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
296 ret = put_user(tmp,(unsigned long __user *) data);
297 break;
300 /* If I and D space are separate, this will have to be fixed. */
301 case PTRACE_POKETEXT: /* write the word at location addr. */
302 case PTRACE_POKEDATA:
303 ret = 0;
304 if (access_process_vm(child, addr, &data, sizeof(data), 1)
305 == sizeof(data))
306 break;
307 ret = -EIO;
308 break;
310 /* write the word at location addr in the USER area */
311 case PTRACE_POKEUSR: {
312 unsigned long index;
314 ret = -EIO;
315 /* convert to index and check */
316 #ifdef CONFIG_PPC32
317 index = (unsigned long) addr >> 2;
318 if ((addr & 3) || (index > PT_FPSCR)
319 || (child->thread.regs == NULL))
320 #else
321 index = (unsigned long) addr >> 3;
322 if ((addr & 7) || (index > PT_FPSCR))
323 #endif
324 break;
326 #ifdef CONFIG_PPC32
327 CHECK_FULL_REGS(child->thread.regs);
328 #endif
329 if (index == PT_ORIG_R3)
330 break;
331 if (index < PT_FPR0) {
332 ret = put_reg(child, index, data);
333 } else {
334 flush_fp_to_thread(child);
335 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
336 ret = 0;
338 break;
341 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
342 case PTRACE_CONT: { /* restart after signal. */
343 ret = -EIO;
344 if (!valid_signal(data))
345 break;
346 if (request == PTRACE_SYSCALL)
347 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
348 else
349 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
350 child->exit_code = data;
351 /* make sure the single step bit is not set. */
352 clear_single_step(child);
353 wake_up_process(child);
354 ret = 0;
355 break;
359 * make the child exit. Best I can do is send it a sigkill.
360 * perhaps it should be put in the status that it wants to
361 * exit.
363 case PTRACE_KILL: {
364 ret = 0;
365 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
366 break;
367 child->exit_code = SIGKILL;
368 /* make sure the single step bit is not set. */
369 clear_single_step(child);
370 wake_up_process(child);
371 break;
374 case PTRACE_SINGLESTEP: { /* set the trap flag. */
375 ret = -EIO;
376 if (!valid_signal(data))
377 break;
378 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
379 set_single_step(child);
380 child->exit_code = data;
381 /* give it a chance to run. */
382 wake_up_process(child);
383 ret = 0;
384 break;
387 #ifdef CONFIG_PPC64
388 case PTRACE_GET_DEBUGREG: {
389 ret = -EINVAL;
390 /* We only support one DABR and no IABRS at the moment */
391 if (addr > 0)
392 break;
393 ret = put_user(child->thread.dabr,
394 (unsigned long __user *)data);
395 break;
398 case PTRACE_SET_DEBUGREG:
399 ret = ptrace_set_debugreg(child, addr, data);
400 break;
401 #endif
403 case PTRACE_DETACH:
404 ret = ptrace_detach(child, data);
405 break;
407 #ifdef CONFIG_PPC64
408 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
409 int i;
410 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
411 unsigned long __user *tmp = (unsigned long __user *)addr;
413 for (i = 0; i < 32; i++) {
414 ret = put_user(*reg, tmp);
415 if (ret)
416 break;
417 reg++;
418 tmp++;
420 break;
423 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
424 int i;
425 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
426 unsigned long __user *tmp = (unsigned long __user *)addr;
428 for (i = 0; i < 32; i++) {
429 ret = get_user(*reg, tmp);
430 if (ret)
431 break;
432 reg++;
433 tmp++;
435 break;
438 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
439 int i;
440 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
441 unsigned long __user *tmp = (unsigned long __user *)addr;
443 flush_fp_to_thread(child);
445 for (i = 0; i < 32; i++) {
446 ret = put_user(*reg, tmp);
447 if (ret)
448 break;
449 reg++;
450 tmp++;
452 break;
455 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
456 int i;
457 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
458 unsigned long __user *tmp = (unsigned long __user *)addr;
460 flush_fp_to_thread(child);
462 for (i = 0; i < 32; i++) {
463 ret = get_user(*reg, tmp);
464 if (ret)
465 break;
466 reg++;
467 tmp++;
469 break;
471 #endif /* CONFIG_PPC64 */
473 #ifdef CONFIG_ALTIVEC
474 case PTRACE_GETVRREGS:
475 /* Get the child altivec register state. */
476 flush_altivec_to_thread(child);
477 ret = get_vrregs((unsigned long __user *)data, child);
478 break;
480 case PTRACE_SETVRREGS:
481 /* Set the child altivec register state. */
482 flush_altivec_to_thread(child);
483 ret = set_vrregs(child, (unsigned long __user *)data);
484 break;
485 #endif
486 #ifdef CONFIG_SPE
487 case PTRACE_GETEVRREGS:
488 /* Get the child spe register state. */
489 if (child->thread.regs->msr & MSR_SPE)
490 giveup_spe(child);
491 ret = get_evrregs((unsigned long __user *)data, child);
492 break;
494 case PTRACE_SETEVRREGS:
495 /* Set the child spe register state. */
496 /* this is to clear the MSR_SPE bit to force a reload
497 * of register state from memory */
498 if (child->thread.regs->msr & MSR_SPE)
499 giveup_spe(child);
500 ret = set_evrregs(child, (unsigned long __user *)data);
501 break;
502 #endif
504 default:
505 ret = ptrace_request(child, request, addr, data);
506 break;
509 return ret;
512 static void do_syscall_trace(void)
514 /* the 0x80 provides a way for the tracing parent to distinguish
515 between a syscall stop and SIGTRAP delivery */
516 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
517 ? 0x80 : 0));
520 * this isn't the same as continuing with a signal, but it will do
521 * for normal use. strace only continues with a signal if the
522 * stopping signal is not SIGTRAP. -brl
524 if (current->exit_code) {
525 send_sig(current->exit_code, current, 1);
526 current->exit_code = 0;
530 void do_syscall_trace_enter(struct pt_regs *regs)
532 #ifdef CONFIG_PPC64
533 secure_computing(regs->gpr[0]);
534 #endif
536 if (test_thread_flag(TIF_SYSCALL_TRACE)
537 && (current->ptrace & PT_PTRACED))
538 do_syscall_trace();
540 if (unlikely(current->audit_context))
541 audit_syscall_entry(current,
542 #ifdef CONFIG_PPC32
543 AUDIT_ARCH_PPC,
544 #else
545 test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
546 #endif
547 regs->gpr[0],
548 regs->gpr[3], regs->gpr[4],
549 regs->gpr[5], regs->gpr[6]);
552 void do_syscall_trace_leave(struct pt_regs *regs)
554 #ifdef CONFIG_PPC32
555 secure_computing(regs->gpr[0]);
556 #endif
558 if (unlikely(current->audit_context))
559 audit_syscall_exit(current,
560 (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
561 regs->result);
563 if ((test_thread_flag(TIF_SYSCALL_TRACE)
564 #ifdef CONFIG_PPC64
565 || test_thread_flag(TIF_SINGLESTEP)
566 #endif
568 && (current->ptrace & PT_PTRACED))
569 do_syscall_trace();
572 #ifdef CONFIG_PPC32
573 EXPORT_SYMBOL(do_syscall_trace_enter);
574 EXPORT_SYMBOL(do_syscall_trace_leave);
575 #endif