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/kernel.h>
19 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/security.h>
26 #include <linux/signal.h>
27 #include <linux/seccomp.h>
28 #include <linux/audit.h>
30 #include <linux/module.h>
33 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/system.h>
39 * does not yet catch signals sent when the child dies.
40 * in exit.c or in signal.c.
44 * Set of msr bits that gdb can change on behalf of a process.
46 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
47 #define MSR_DEBUGCHANGE 0
49 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
53 * Max register writeable via put_reg
56 #define PT_MAX_PUT_REG PT_MQ
58 #define PT_MAX_PUT_REG PT_CCR
62 * Get contents of register REGNO in task TASK.
64 unsigned long ptrace_get_reg(struct task_struct
*task
, int regno
)
66 unsigned long tmp
= 0;
68 if (task
->thread
.regs
== NULL
)
71 if (regno
== PT_MSR
) {
72 tmp
= ((unsigned long *)task
->thread
.regs
)[PT_MSR
];
73 return tmp
| task
->thread
.fpexc_mode
;
76 if (regno
< (sizeof(struct pt_regs
) / sizeof(unsigned long)))
77 return ((unsigned long *)task
->thread
.regs
)[regno
];
83 * Write contents of register REGNO in task TASK.
85 int ptrace_put_reg(struct task_struct
*task
, int regno
, unsigned long data
)
87 if (task
->thread
.regs
== NULL
)
90 if (regno
<= PT_MAX_PUT_REG
|| regno
== PT_TRAP
) {
92 data
= (data
& MSR_DEBUGCHANGE
)
93 | (task
->thread
.regs
->msr
& ~MSR_DEBUGCHANGE
);
94 /* We prevent mucking around with the reserved area of trap
95 * which are used internally by the kernel
99 ((unsigned long *)task
->thread
.regs
)[regno
] = data
;
106 static int get_fpregs(void __user
*data
, struct task_struct
*task
,
109 unsigned int count
= has_fpscr
? 33 : 32;
111 if (copy_to_user(data
, task
->thread
.fpr
, count
* sizeof(double)))
116 static int set_fpregs(void __user
*data
, struct task_struct
*task
,
119 unsigned int count
= has_fpscr
? 33 : 32;
121 if (copy_from_user(task
->thread
.fpr
, data
, count
* sizeof(double)))
127 #ifdef CONFIG_ALTIVEC
129 * Get/set all the altivec registers vr0..vr31, vscr, vrsave, in one go.
130 * The transfer totals 34 quadword. Quadwords 0-31 contain the
131 * corresponding vector registers. Quadword 32 contains the vscr as the
132 * last word (offset 12) within that quadword. Quadword 33 contains the
133 * vrsave as the first word (offset 0) within the quadword.
135 * This definition of the VMX state is compatible with the current PPC32
136 * ptrace interface. This allows signal handling and ptrace to use the
137 * same structures. This also simplifies the implementation of a bi-arch
138 * (combined (32- and 64-bit) gdb.
142 * Get contents of AltiVec register state in task TASK
144 static int get_vrregs(unsigned long __user
*data
, struct task_struct
*task
)
146 unsigned long regsize
;
148 /* copy AltiVec registers VR[0] .. VR[31] */
149 regsize
= 32 * sizeof(vector128
);
150 if (copy_to_user(data
, task
->thread
.vr
, regsize
))
152 data
+= (regsize
/ sizeof(unsigned long));
155 regsize
= 1 * sizeof(vector128
);
156 if (copy_to_user(data
, &task
->thread
.vscr
, regsize
))
158 data
+= (regsize
/ sizeof(unsigned long));
161 if (put_user(task
->thread
.vrsave
, (u32 __user
*)data
))
168 * Write contents of AltiVec register state into task TASK.
170 static int set_vrregs(struct task_struct
*task
, unsigned long __user
*data
)
172 unsigned long regsize
;
174 /* copy AltiVec registers VR[0] .. VR[31] */
175 regsize
= 32 * sizeof(vector128
);
176 if (copy_from_user(task
->thread
.vr
, data
, regsize
))
178 data
+= (regsize
/ sizeof(unsigned long));
181 regsize
= 1 * sizeof(vector128
);
182 if (copy_from_user(&task
->thread
.vscr
, data
, regsize
))
184 data
+= (regsize
/ sizeof(unsigned long));
187 if (get_user(task
->thread
.vrsave
, (u32 __user
*)data
))
192 #endif /* CONFIG_ALTIVEC */
197 * For get_evrregs/set_evrregs functions 'data' has the following layout:
207 * Get contents of SPE register state in task TASK.
209 static int get_evrregs(unsigned long *data
, struct task_struct
*task
)
213 if (!access_ok(VERIFY_WRITE
, data
, 35 * sizeof(unsigned long)))
217 if (__put_user(task
->thread
.spefscr
, &data
[34]))
220 /* copy SPE registers EVR[0] .. EVR[31] */
221 for (i
= 0; i
< 32; i
++, data
++)
222 if (__put_user(task
->thread
.evr
[i
], data
))
226 if (__put_user64(task
->thread
.acc
, (unsigned long long *)data
))
233 * Write contents of SPE register state into task TASK.
235 static int set_evrregs(struct task_struct
*task
, unsigned long *data
)
239 if (!access_ok(VERIFY_READ
, data
, 35 * sizeof(unsigned long)))
243 if (__get_user(task
->thread
.spefscr
, &data
[34]))
246 /* copy SPE registers EVR[0] .. EVR[31] */
247 for (i
= 0; i
< 32; i
++, data
++)
248 if (__get_user(task
->thread
.evr
[i
], data
))
251 if (__get_user64(task
->thread
.acc
, (unsigned long long*)data
))
256 #endif /* CONFIG_SPE */
259 static void set_single_step(struct task_struct
*task
)
261 struct pt_regs
*regs
= task
->thread
.regs
;
264 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
265 task
->thread
.dbcr0
= DBCR0_IDM
| DBCR0_IC
;
271 set_tsk_thread_flag(task
, TIF_SINGLESTEP
);
274 static void clear_single_step(struct task_struct
*task
)
276 struct pt_regs
*regs
= task
->thread
.regs
;
279 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
280 task
->thread
.dbcr0
= 0;
281 regs
->msr
&= ~MSR_DE
;
283 regs
->msr
&= ~MSR_SE
;
286 clear_tsk_thread_flag(task
, TIF_SINGLESTEP
);
289 static int ptrace_set_debugreg(struct task_struct
*task
, unsigned long addr
,
292 /* We only support one DABR and no IABRS at the moment */
296 /* The bottom 3 bits are flags */
297 if ((data
& ~0x7UL
) >= TASK_SIZE
)
300 /* Ensure translation is on */
301 if (data
&& !(data
& DABR_TRANSLATION
))
304 task
->thread
.dabr
= data
;
309 * Called by kernel/ptrace.c when detaching..
311 * Make sure single step bits etc are not set.
313 void ptrace_disable(struct task_struct
*child
)
315 /* make sure the single step bit is not set. */
316 clear_single_step(child
);
320 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
321 * we mark them as obsolete now, they will be removed in a future version
323 static long arch_ptrace_old(struct task_struct
*child
, long request
, long addr
,
329 case PPC_PTRACE_GETREGS
: { /* Get GPRs 0 - 31. */
331 unsigned long *reg
= &((unsigned long *)child
->thread
.regs
)[0];
332 unsigned long __user
*tmp
= (unsigned long __user
*)addr
;
334 for (i
= 0; i
< 32; i
++) {
335 ret
= put_user(*reg
, tmp
);
344 case PPC_PTRACE_SETREGS
: { /* Set GPRs 0 - 31. */
346 unsigned long *reg
= &((unsigned long *)child
->thread
.regs
)[0];
347 unsigned long __user
*tmp
= (unsigned long __user
*)addr
;
349 for (i
= 0; i
< 32; i
++) {
350 ret
= get_user(*reg
, tmp
);
359 case PPC_PTRACE_GETFPREGS
: { /* Get FPRs 0 - 31. */
360 flush_fp_to_thread(child
);
361 ret
= get_fpregs((void __user
*)addr
, child
, 0);
365 case PPC_PTRACE_SETFPREGS
: { /* Get FPRs 0 - 31. */
366 flush_fp_to_thread(child
);
367 ret
= set_fpregs((void __user
*)addr
, child
, 0);
375 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
380 /* when I and D space are separate, these will need to be fixed. */
381 case PTRACE_PEEKTEXT
: /* read word at location addr. */
382 case PTRACE_PEEKDATA
: {
386 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
388 if (copied
!= sizeof(tmp
))
390 ret
= put_user(tmp
,(unsigned long __user
*) data
);
394 /* read the word at location addr in the USER area. */
395 case PTRACE_PEEKUSR
: {
396 unsigned long index
, tmp
;
399 /* convert to index and check */
401 index
= (unsigned long) addr
>> 2;
402 if ((addr
& 3) || (index
> PT_FPSCR
)
403 || (child
->thread
.regs
== NULL
))
405 index
= (unsigned long) addr
>> 3;
406 if ((addr
& 7) || (index
> PT_FPSCR
))
410 CHECK_FULL_REGS(child
->thread
.regs
);
411 if (index
< PT_FPR0
) {
412 tmp
= ptrace_get_reg(child
, (int) index
);
414 flush_fp_to_thread(child
);
415 tmp
= ((unsigned long *)child
->thread
.fpr
)[index
- PT_FPR0
];
417 ret
= put_user(tmp
,(unsigned long __user
*) data
);
421 /* If I and D space are separate, this will have to be fixed. */
422 case PTRACE_POKETEXT
: /* write the word at location addr. */
423 case PTRACE_POKEDATA
:
425 if (access_process_vm(child
, addr
, &data
, sizeof(data
), 1)
431 /* write the word at location addr in the USER area */
432 case PTRACE_POKEUSR
: {
436 /* convert to index and check */
438 index
= (unsigned long) addr
>> 2;
439 if ((addr
& 3) || (index
> PT_FPSCR
)
440 || (child
->thread
.regs
== NULL
))
442 index
= (unsigned long) addr
>> 3;
443 if ((addr
& 7) || (index
> PT_FPSCR
))
447 CHECK_FULL_REGS(child
->thread
.regs
);
448 if (index
< PT_FPR0
) {
449 ret
= ptrace_put_reg(child
, index
, data
);
451 flush_fp_to_thread(child
);
452 ((unsigned long *)child
->thread
.fpr
)[index
- PT_FPR0
] = data
;
458 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
459 case PTRACE_CONT
: { /* restart after signal. */
461 if (!valid_signal(data
))
463 if (request
== PTRACE_SYSCALL
)
464 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
466 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
467 child
->exit_code
= data
;
468 /* make sure the single step bit is not set. */
469 clear_single_step(child
);
470 wake_up_process(child
);
476 * make the child exit. Best I can do is send it a sigkill.
477 * perhaps it should be put in the status that it wants to
482 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
484 child
->exit_code
= SIGKILL
;
485 /* make sure the single step bit is not set. */
486 clear_single_step(child
);
487 wake_up_process(child
);
491 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
493 if (!valid_signal(data
))
495 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
496 set_single_step(child
);
497 child
->exit_code
= data
;
498 /* give it a chance to run. */
499 wake_up_process(child
);
504 case PTRACE_GET_DEBUGREG
: {
506 /* We only support one DABR and no IABRS at the moment */
509 ret
= put_user(child
->thread
.dabr
,
510 (unsigned long __user
*)data
);
514 case PTRACE_SET_DEBUGREG
:
515 ret
= ptrace_set_debugreg(child
, addr
, data
);
519 ret
= ptrace_detach(child
, data
);
523 case PTRACE_GETREGS64
:
525 case PTRACE_GETREGS
: { /* Get all pt_regs from the child. */
527 if (!access_ok(VERIFY_WRITE
, (void __user
*)data
,
528 sizeof(struct pt_regs
))) {
533 for (ui
= 0; ui
< PT_REGS_COUNT
; ui
++) {
534 ret
|= __put_user(ptrace_get_reg(child
, ui
),
535 (unsigned long __user
*) data
);
536 data
+= sizeof(long);
542 case PTRACE_SETREGS64
:
544 case PTRACE_SETREGS
: { /* Set all gp regs in the child. */
547 if (!access_ok(VERIFY_READ
, (void __user
*)data
,
548 sizeof(struct pt_regs
))) {
553 for (ui
= 0; ui
< PT_REGS_COUNT
; ui
++) {
554 ret
= __get_user(tmp
, (unsigned long __user
*) data
);
557 ptrace_put_reg(child
, ui
, tmp
);
558 data
+= sizeof(long);
563 case PTRACE_GETFPREGS
: { /* Get the child FPU state (FPR0...31 + FPSCR) */
564 flush_fp_to_thread(child
);
565 ret
= get_fpregs((void __user
*)data
, child
, 1);
569 case PTRACE_SETFPREGS
: { /* Set the child FPU state (FPR0...31 + FPSCR) */
570 flush_fp_to_thread(child
);
571 ret
= set_fpregs((void __user
*)data
, child
, 1);
575 #ifdef CONFIG_ALTIVEC
576 case PTRACE_GETVRREGS
:
577 /* Get the child altivec register state. */
578 flush_altivec_to_thread(child
);
579 ret
= get_vrregs((unsigned long __user
*)data
, child
);
582 case PTRACE_SETVRREGS
:
583 /* Set the child altivec register state. */
584 flush_altivec_to_thread(child
);
585 ret
= set_vrregs(child
, (unsigned long __user
*)data
);
589 case PTRACE_GETEVRREGS
:
590 /* Get the child spe register state. */
591 if (child
->thread
.regs
->msr
& MSR_SPE
)
593 ret
= get_evrregs((unsigned long __user
*)data
, child
);
596 case PTRACE_SETEVRREGS
:
597 /* Set the child spe register state. */
598 /* this is to clear the MSR_SPE bit to force a reload
599 * of register state from memory */
600 if (child
->thread
.regs
->msr
& MSR_SPE
)
602 ret
= set_evrregs(child
, (unsigned long __user
*)data
);
606 /* Old reverse args ptrace callss */
607 case PPC_PTRACE_GETREGS
: /* Get GPRs 0 - 31. */
608 case PPC_PTRACE_SETREGS
: /* Set GPRs 0 - 31. */
609 case PPC_PTRACE_GETFPREGS
: /* Get FPRs 0 - 31. */
610 case PPC_PTRACE_SETFPREGS
: /* Get FPRs 0 - 31. */
611 ret
= arch_ptrace_old(child
, request
, addr
, data
);
615 ret
= ptrace_request(child
, request
, addr
, data
);
621 static void do_syscall_trace(void)
623 /* the 0x80 provides a way for the tracing parent to distinguish
624 between a syscall stop and SIGTRAP delivery */
625 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
629 * this isn't the same as continuing with a signal, but it will do
630 * for normal use. strace only continues with a signal if the
631 * stopping signal is not SIGTRAP. -brl
633 if (current
->exit_code
) {
634 send_sig(current
->exit_code
, current
, 1);
635 current
->exit_code
= 0;
639 void do_syscall_trace_enter(struct pt_regs
*regs
)
641 secure_computing(regs
->gpr
[0]);
643 if (test_thread_flag(TIF_SYSCALL_TRACE
)
644 && (current
->ptrace
& PT_PTRACED
))
647 if (unlikely(current
->audit_context
)) {
649 if (!test_thread_flag(TIF_32BIT
))
650 audit_syscall_entry(AUDIT_ARCH_PPC64
,
652 regs
->gpr
[3], regs
->gpr
[4],
653 regs
->gpr
[5], regs
->gpr
[6]);
656 audit_syscall_entry(AUDIT_ARCH_PPC
,
658 regs
->gpr
[3] & 0xffffffff,
659 regs
->gpr
[4] & 0xffffffff,
660 regs
->gpr
[5] & 0xffffffff,
661 regs
->gpr
[6] & 0xffffffff);
665 void do_syscall_trace_leave(struct pt_regs
*regs
)
667 if (unlikely(current
->audit_context
))
668 audit_syscall_exit((regs
->ccr
&0x10000000)?AUDITSC_FAILURE
:AUDITSC_SUCCESS
,
671 if ((test_thread_flag(TIF_SYSCALL_TRACE
)
672 || test_thread_flag(TIF_SINGLESTEP
))
673 && (current
->ptrace
& PT_PTRACED
))