1 /* ptrace.c: Sparc process tracing support.
3 * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
9 * Added Linux support -miguel (weird, eh?, the original code was meant
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/security.h>
22 #include <linux/seccomp.h>
23 #include <linux/audit.h>
24 #include <linux/signal.h>
27 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/psrcompat.h>
31 #include <asm/visasm.h>
32 #include <asm/spitfire.h>
34 #include <asm/cpudata.h>
36 /* Returning from ptrace is a bit tricky because the syscall return
37 * low level code assumes any value returned which is negative and
38 * is a valid errno will mean setting the condition codes to indicate
39 * an error return. This doesn't work, so we have this hook.
41 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
43 regs->u_regs[UREG_I0] = error;
44 regs->tstate |= (TSTATE_ICARRY | TSTATE_XCARRY);
45 regs->tpc = regs->tnpc;
49 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
51 regs->u_regs[UREG_I0] = value;
52 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
53 regs->tpc = regs->tnpc;
58 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, void __user *addr)
60 if (test_thread_flag(TIF_32BIT)) {
61 if (put_user(value, (unsigned int __user *) addr)) {
62 pt_error_return(regs, EFAULT);
66 if (put_user(value, (long __user *) addr)) {
67 pt_error_return(regs, EFAULT);
71 regs->u_regs[UREG_I0] = 0;
72 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
73 regs->tpc = regs->tnpc;
78 pt_os_succ_return (struct pt_regs *regs, unsigned long val, void __user *addr)
80 if (current->personality == PER_SUNOS)
81 pt_succ_return (regs, val);
83 pt_succ_return_linux (regs, val, addr);
86 /* #define ALLOW_INIT_TRACING */
87 /* #define DEBUG_PTRACE */
91 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
92 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
93 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
94 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
95 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
96 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
97 /* 24 */ "SYSCALL", ""
102 * Called by kernel/ptrace.c when detaching..
104 * Make sure single step bits etc are not set.
106 void ptrace_disable(struct task_struct *child)
111 /* To get the necessary page struct, access_process_vm() first calls
112 * get_user_pages(). This has done a flush_dcache_page() on the
113 * accessed page. Then our caller (copy_{to,from}_user_page()) did
114 * to memcpy to read/write the data from that page.
116 * Now, the only thing we have to do is:
117 * 1) flush the D-cache if it's possible than an illegal alias
119 * 2) flush the I-cache if this is pre-cheetah and we did a write
121 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
122 unsigned long uaddr, void *kaddr,
123 unsigned long len, int write)
125 BUG_ON(len > PAGE_SIZE);
127 if (tlb_type == hypervisor)
130 #ifdef DCACHE_ALIASING_POSSIBLE
131 /* If bit 13 of the kernel address we used to access the
132 * user page is the same as the virtual address that page
133 * is mapped to in the user's address space, we can skip the
136 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
137 unsigned long start = __pa(kaddr);
138 unsigned long end = start + len;
139 unsigned long dcache_line_size;
141 dcache_line_size = local_cpu_data().dcache_line_size;
143 if (tlb_type == spitfire) {
144 for (; start < end; start += dcache_line_size)
145 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
147 start &= ~(dcache_line_size - 1);
148 for (; start < end; start += dcache_line_size)
149 __asm__ __volatile__(
150 "stxa %%g0, [%0] %1\n\t"
154 "i" (ASI_DCACHE_INVALIDATE));
158 if (write && tlb_type == spitfire) {
159 unsigned long start = (unsigned long) kaddr;
160 unsigned long end = start + len;
161 unsigned long icache_line_size;
163 icache_line_size = local_cpu_data().icache_line_size;
165 for (; start < end; start += icache_line_size)
170 asmlinkage void do_ptrace(struct pt_regs *regs)
172 int request = regs->u_regs[UREG_I0];
173 pid_t pid = regs->u_regs[UREG_I1];
174 unsigned long addr = regs->u_regs[UREG_I2];
175 unsigned long data = regs->u_regs[UREG_I3];
176 unsigned long addr2 = regs->u_regs[UREG_I4];
177 struct task_struct *child;
180 if (test_thread_flag(TIF_32BIT)) {
181 addr &= 0xffffffffUL;
182 data &= 0xffffffffUL;
183 addr2 &= 0xffffffffUL;
190 if ((request >= 0) && (request <= 24))
195 if (request == PTRACE_POKEDATA && data == 0x91d02001){
196 printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
199 printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
200 s, request, pid, addr, data, addr2);
203 if (request == PTRACE_TRACEME) {
204 ret = ptrace_traceme();
205 pt_succ_return(regs, 0);
209 child = ptrace_get_task_struct(pid);
211 ret = PTR_ERR(child);
212 pt_error_return(regs, -ret);
216 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
217 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
218 if (ptrace_attach(child)) {
219 pt_error_return(regs, EPERM);
222 pt_succ_return(regs, 0);
226 ret = ptrace_check_attach(child, request == PTRACE_KILL);
228 pt_error_return(regs, -ret);
232 if (!(test_thread_flag(TIF_32BIT)) &&
233 ((request == PTRACE_READDATA64) ||
234 (request == PTRACE_WRITEDATA64) ||
235 (request == PTRACE_READTEXT64) ||
236 (request == PTRACE_WRITETEXT64) ||
237 (request == PTRACE_PEEKTEXT64) ||
238 (request == PTRACE_POKETEXT64) ||
239 (request == PTRACE_PEEKDATA64) ||
240 (request == PTRACE_POKEDATA64))) {
241 addr = regs->u_regs[UREG_G2];
242 addr2 = regs->u_regs[UREG_G3];
243 request -= 30; /* wheee... */
247 case PTRACE_PEEKTEXT: /* read word at location addr. */
248 case PTRACE_PEEKDATA: {
254 if (test_thread_flag(TIF_32BIT)) {
255 copied = access_process_vm(child, addr,
256 &tmp32, sizeof(tmp32), 0);
257 tmp64 = (unsigned long) tmp32;
258 if (copied == sizeof(tmp32))
261 copied = access_process_vm(child, addr,
262 &tmp64, sizeof(tmp64), 0);
263 if (copied == sizeof(tmp64))
267 pt_error_return(regs, -res);
269 pt_os_succ_return(regs, tmp64, (void __user *) data);
273 case PTRACE_POKETEXT: /* write the word at location addr. */
274 case PTRACE_POKEDATA: {
277 int copied, res = -EIO;
279 if (test_thread_flag(TIF_32BIT)) {
281 copied = access_process_vm(child, addr,
282 &tmp32, sizeof(tmp32), 1);
283 if (copied == sizeof(tmp32))
287 copied = access_process_vm(child, addr,
288 &tmp64, sizeof(tmp64), 1);
289 if (copied == sizeof(tmp64))
293 pt_error_return(regs, -res);
295 pt_succ_return(regs, res);
299 case PTRACE_GETREGS: {
300 struct pt_regs32 __user *pregs =
301 (struct pt_regs32 __user *) addr;
302 struct pt_regs *cregs = task_pt_regs(child);
305 if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
306 __put_user(cregs->tpc, (&pregs->pc)) ||
307 __put_user(cregs->tnpc, (&pregs->npc)) ||
308 __put_user(cregs->y, (&pregs->y))) {
309 pt_error_return(regs, EFAULT);
312 for (rval = 1; rval < 16; rval++)
313 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
314 pt_error_return(regs, EFAULT);
317 pt_succ_return(regs, 0);
319 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
324 case PTRACE_GETREGS64: {
325 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
326 struct pt_regs *cregs = task_pt_regs(child);
327 unsigned long tpc = cregs->tpc;
330 if ((task_thread_info(child)->flags & _TIF_32BIT) != 0)
332 if (__put_user(cregs->tstate, (&pregs->tstate)) ||
333 __put_user(tpc, (&pregs->tpc)) ||
334 __put_user(cregs->tnpc, (&pregs->tnpc)) ||
335 __put_user(cregs->y, (&pregs->y))) {
336 pt_error_return(regs, EFAULT);
339 for (rval = 1; rval < 16; rval++)
340 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
341 pt_error_return(regs, EFAULT);
344 pt_succ_return(regs, 0);
346 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
351 case PTRACE_SETREGS: {
352 struct pt_regs32 __user *pregs =
353 (struct pt_regs32 __user *) addr;
354 struct pt_regs *cregs = task_pt_regs(child);
355 unsigned int psr, pc, npc, y;
358 /* Must be careful, tracing process can only set certain
361 if (__get_user(psr, (&pregs->psr)) ||
362 __get_user(pc, (&pregs->pc)) ||
363 __get_user(npc, (&pregs->npc)) ||
364 __get_user(y, (&pregs->y))) {
365 pt_error_return(regs, EFAULT);
368 cregs->tstate &= ~(TSTATE_ICC);
369 cregs->tstate |= psr_to_tstate_icc(psr);
370 if (!((pc | npc) & 3)) {
375 for (i = 1; i < 16; i++) {
376 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
377 pt_error_return(regs, EFAULT);
381 pt_succ_return(regs, 0);
385 case PTRACE_SETREGS64: {
386 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
387 struct pt_regs *cregs = task_pt_regs(child);
388 unsigned long tstate, tpc, tnpc, y;
391 /* Must be careful, tracing process can only set certain
394 if (__get_user(tstate, (&pregs->tstate)) ||
395 __get_user(tpc, (&pregs->tpc)) ||
396 __get_user(tnpc, (&pregs->tnpc)) ||
397 __get_user(y, (&pregs->y))) {
398 pt_error_return(regs, EFAULT);
401 if ((task_thread_info(child)->flags & _TIF_32BIT) != 0) {
405 tstate &= (TSTATE_ICC | TSTATE_XCC);
406 cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
407 cregs->tstate |= tstate;
408 if (!((tpc | tnpc) & 3)) {
413 for (i = 1; i < 16; i++) {
414 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
415 pt_error_return(regs, EFAULT);
419 pt_succ_return(regs, 0);
423 case PTRACE_GETFPREGS: {
425 unsigned int regs[32];
431 unsigned int insnaddr;
435 struct fps __user *fps = (struct fps __user *) addr;
436 unsigned long *fpregs = task_thread_info(child)->fpregs;
438 if (copy_to_user(&fps->regs[0], fpregs,
439 (32 * sizeof(unsigned int))) ||
440 __put_user(task_thread_info(child)->xfsr[0], (&fps->fsr)) ||
441 __put_user(0, (&fps->fpqd)) ||
442 __put_user(0, (&fps->flags)) ||
443 __put_user(0, (&fps->extra)) ||
444 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) {
445 pt_error_return(regs, EFAULT);
448 pt_succ_return(regs, 0);
452 case PTRACE_GETFPREGS64: {
454 unsigned int regs[64];
457 struct fps __user *fps = (struct fps __user *) addr;
458 unsigned long *fpregs = task_thread_info(child)->fpregs;
460 if (copy_to_user(&fps->regs[0], fpregs,
461 (64 * sizeof(unsigned int))) ||
462 __put_user(task_thread_info(child)->xfsr[0], (&fps->fsr))) {
463 pt_error_return(regs, EFAULT);
466 pt_succ_return(regs, 0);
470 case PTRACE_SETFPREGS: {
472 unsigned int regs[32];
478 unsigned int insnaddr;
482 struct fps __user *fps = (struct fps __user *) addr;
483 unsigned long *fpregs = task_thread_info(child)->fpregs;
486 if (copy_from_user(fpregs, &fps->regs[0],
487 (32 * sizeof(unsigned int))) ||
488 __get_user(fsr, (&fps->fsr))) {
489 pt_error_return(regs, EFAULT);
492 task_thread_info(child)->xfsr[0] &= 0xffffffff00000000UL;
493 task_thread_info(child)->xfsr[0] |= fsr;
494 if (!(task_thread_info(child)->fpsaved[0] & FPRS_FEF))
495 task_thread_info(child)->gsr[0] = 0;
496 task_thread_info(child)->fpsaved[0] |= (FPRS_FEF | FPRS_DL);
497 pt_succ_return(regs, 0);
501 case PTRACE_SETFPREGS64: {
503 unsigned int regs[64];
506 struct fps __user *fps = (struct fps __user *) addr;
507 unsigned long *fpregs = task_thread_info(child)->fpregs;
509 if (copy_from_user(fpregs, &fps->regs[0],
510 (64 * sizeof(unsigned int))) ||
511 __get_user(task_thread_info(child)->xfsr[0], (&fps->fsr))) {
512 pt_error_return(regs, EFAULT);
515 if (!(task_thread_info(child)->fpsaved[0] & FPRS_FEF))
516 task_thread_info(child)->gsr[0] = 0;
517 task_thread_info(child)->fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU);
518 pt_succ_return(regs, 0);
522 case PTRACE_READTEXT:
523 case PTRACE_READDATA: {
524 int res = ptrace_readdata(child, addr,
525 (char __user *)addr2, data);
527 pt_succ_return(regs, 0);
532 pt_error_return(regs, -res);
536 case PTRACE_WRITETEXT:
537 case PTRACE_WRITEDATA: {
538 int res = ptrace_writedata(child, (char __user *) addr2,
541 pt_succ_return(regs, 0);
546 pt_error_return(regs, -res);
549 case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
552 case PTRACE_CONT: { /* restart after signal. */
553 if (!valid_signal(data)) {
554 pt_error_return(regs, EIO);
558 if (request == PTRACE_SYSCALL) {
559 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
561 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
564 child->exit_code = data;
566 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm,
567 child->pid, child->exit_code,
568 task_pt_regs(child)->tpc,
569 task_pt_regs(child)->tnpc);
572 wake_up_process(child);
573 pt_succ_return(regs, 0);
578 * make the child exit. Best I can do is send it a sigkill.
579 * perhaps it should be put in the status that it wants to
583 if (child->exit_state == EXIT_ZOMBIE) { /* already dead */
584 pt_succ_return(regs, 0);
587 child->exit_code = SIGKILL;
588 wake_up_process(child);
589 pt_succ_return(regs, 0);
593 case PTRACE_SUNDETACH: { /* detach a process that was attached. */
594 int error = ptrace_detach(child, data);
596 pt_error_return(regs, EIO);
599 pt_succ_return(regs, 0);
603 /* PTRACE_DUMPCORE unsupported... */
606 int err = ptrace_request(child, request, addr, data);
608 pt_error_return(regs, -err);
610 pt_succ_return(regs, 0);
616 put_task_struct(child);
621 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
623 /* do the secure computing check first */
624 secure_computing(regs->u_regs[UREG_G1]);
626 if (unlikely(current->audit_context) && syscall_exit_p) {
627 unsigned long tstate = regs->tstate;
628 int result = AUDITSC_SUCCESS;
630 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
631 result = AUDITSC_FAILURE;
633 audit_syscall_exit(current, result, regs->u_regs[UREG_I0]);
636 if (!(current->ptrace & PT_PTRACED))
639 if (!test_thread_flag(TIF_SYSCALL_TRACE))
642 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
646 * this isn't the same as continuing with a signal, but it will do
647 * for normal use. strace only continues with a signal if the
648 * stopping signal is not SIGTRAP. -brl
650 if (current->exit_code) {
651 send_sig(current->exit_code, current, 1);
652 current->exit_code = 0;
656 if (unlikely(current->audit_context) && !syscall_exit_p)
657 audit_syscall_entry(current,
658 (test_thread_flag(TIF_32BIT) ?
661 regs->u_regs[UREG_G1],
662 regs->u_regs[UREG_I0],
663 regs->u_regs[UREG_I1],
664 regs->u_regs[UREG_I2],
665 regs->u_regs[UREG_I3]);