1 /* ptrace.c: Sparc process tracing support.
3 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
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>
25 #include <linux/regset.h>
26 #include <linux/compat.h>
27 #include <linux/elf.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/uaccess.h>
33 #include <asm/psrcompat.h>
34 #include <asm/visasm.h>
35 #include <asm/spitfire.h>
37 #include <asm/cpudata.h>
39 /* #define ALLOW_INIT_TRACING */
42 * Called by kernel/ptrace.c when detaching..
44 * Make sure single step bits etc are not set.
46 void ptrace_disable(struct task_struct *child)
51 /* To get the necessary page struct, access_process_vm() first calls
52 * get_user_pages(). This has done a flush_dcache_page() on the
53 * accessed page. Then our caller (copy_{to,from}_user_page()) did
54 * to memcpy to read/write the data from that page.
56 * Now, the only thing we have to do is:
57 * 1) flush the D-cache if it's possible than an illegal alias
59 * 2) flush the I-cache if this is pre-cheetah and we did a write
61 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
62 unsigned long uaddr, void *kaddr,
63 unsigned long len, int write)
65 BUG_ON(len > PAGE_SIZE);
67 if (tlb_type == hypervisor)
70 #ifdef DCACHE_ALIASING_POSSIBLE
71 /* If bit 13 of the kernel address we used to access the
72 * user page is the same as the virtual address that page
73 * is mapped to in the user's address space, we can skip the
76 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
77 unsigned long start = __pa(kaddr);
78 unsigned long end = start + len;
79 unsigned long dcache_line_size;
81 dcache_line_size = local_cpu_data().dcache_line_size;
83 if (tlb_type == spitfire) {
84 for (; start < end; start += dcache_line_size)
85 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
87 start &= ~(dcache_line_size - 1);
88 for (; start < end; start += dcache_line_size)
90 "stxa %%g0, [%0] %1\n\t"
94 "i" (ASI_DCACHE_INVALIDATE));
98 if (write && tlb_type == spitfire) {
99 unsigned long start = (unsigned long) kaddr;
100 unsigned long end = start + len;
101 unsigned long icache_line_size;
103 icache_line_size = local_cpu_data().icache_line_size;
105 for (; start < end; start += icache_line_size)
115 static int genregs64_get(struct task_struct *target,
116 const struct user_regset *regset,
117 unsigned int pos, unsigned int count,
118 void *kbuf, void __user *ubuf)
120 const struct pt_regs *regs = task_pt_regs(target);
123 if (target == current)
126 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
128 0, 16 * sizeof(u64));
130 unsigned long __user *reg_window = (unsigned long __user *)
131 (regs->u_regs[UREG_I6] + STACK_BIAS);
132 unsigned long window[16];
134 if (copy_from_user(window, reg_window, sizeof(window)))
137 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
144 /* TSTATE, TPC, TNPC */
145 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
152 unsigned long y = regs->y;
154 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
161 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
162 36 * sizeof(u64), -1);
167 static int genregs64_set(struct task_struct *target,
168 const struct user_regset *regset,
169 unsigned int pos, unsigned int count,
170 const void *kbuf, const void __user *ubuf)
172 struct pt_regs *regs = task_pt_regs(target);
175 if (target == current)
178 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
180 0, 16 * sizeof(u64));
181 if (!ret && count > 0) {
182 unsigned long __user *reg_window = (unsigned long __user *)
183 (regs->u_regs[UREG_I6] + STACK_BIAS);
184 unsigned long window[16];
186 if (copy_from_user(window, reg_window, sizeof(window)))
189 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
194 copy_to_user(reg_window, window, sizeof(window)))
198 if (!ret && count > 0) {
199 unsigned long tstate;
202 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
207 /* Only the condition codes can be modified
208 * in the %tstate register.
210 tstate &= (TSTATE_ICC | TSTATE_XCC);
211 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
212 regs->tstate |= tstate;
218 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
227 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
236 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
237 36 * sizeof(u64), -1);
242 static int fpregs64_get(struct task_struct *target,
243 const struct user_regset *regset,
244 unsigned int pos, unsigned int count,
245 void *kbuf, void __user *ubuf)
247 const unsigned long *fpregs = task_thread_info(target)->fpregs;
248 unsigned long fprs, fsr, gsr;
251 if (target == current)
252 save_and_clear_fpu();
254 fprs = task_thread_info(target)->fpsaved[0];
257 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
259 0, 16 * sizeof(u64));
261 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
267 ret = user_regset_copyout(&pos, &count,
273 ret = user_regset_copyout_zero(&pos, &count,
279 if (fprs & FPRS_FEF) {
280 fsr = task_thread_info(target)->xfsr[0];
281 gsr = task_thread_info(target)->gsr[0];
287 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
292 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
297 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
303 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
304 35 * sizeof(u64), -1);
309 static int fpregs64_set(struct task_struct *target,
310 const struct user_regset *regset,
311 unsigned int pos, unsigned int count,
312 const void *kbuf, const void __user *ubuf)
314 unsigned long *fpregs = task_thread_info(target)->fpregs;
318 if (target == current)
319 save_and_clear_fpu();
321 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
323 0, 32 * sizeof(u64));
325 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
326 task_thread_info(target)->xfsr,
330 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
331 task_thread_info(target)->gsr,
335 fprs = task_thread_info(target)->fpsaved[0];
336 if (!ret && count > 0) {
337 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
343 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
344 task_thread_info(target)->fpsaved[0] = fprs;
347 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
348 35 * sizeof(u64), -1);
352 static const struct user_regset sparc64_regsets[] = {
358 * TSTATE, TPC, TNPC, Y
361 .core_note_type = NT_PRSTATUS,
362 .n = 36 * sizeof(u64),
363 .size = sizeof(u64), .align = sizeof(u64),
364 .get = genregs64_get, .set = genregs64_set
373 .core_note_type = NT_PRFPREG,
374 .n = 35 * sizeof(u64),
375 .size = sizeof(u64), .align = sizeof(u64),
376 .get = fpregs64_get, .set = fpregs64_set
380 static const struct user_regset_view user_sparc64_view = {
381 .name = "sparc64", .e_machine = EM_SPARCV9,
382 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
385 static int genregs32_get(struct task_struct *target,
386 const struct user_regset *regset,
387 unsigned int pos, unsigned int count,
388 void *kbuf, void __user *ubuf)
390 const struct pt_regs *regs = task_pt_regs(target);
391 compat_ulong_t __user *reg_window;
392 compat_ulong_t *k = kbuf;
393 compat_ulong_t __user *u = ubuf;
396 if (target == current)
400 count /= sizeof(reg);
403 for (; count > 0 && pos < 16; count--)
404 *k++ = regs->u_regs[pos++];
406 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
407 for (; count > 0 && pos < 32; count--) {
408 if (get_user(*k++, ®_window[pos++]))
412 for (; count > 0 && pos < 16; count--) {
413 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
417 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
418 for (; count > 0 && pos < 32; count--) {
419 if (get_user(reg, ®_window[pos++]) ||
427 reg = tstate_to_psr(regs->tstate);
448 else if (put_user(reg, u++))
455 count *= sizeof(reg);
457 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
458 38 * sizeof(reg), -1);
461 static int genregs32_set(struct task_struct *target,
462 const struct user_regset *regset,
463 unsigned int pos, unsigned int count,
464 const void *kbuf, const void __user *ubuf)
466 struct pt_regs *regs = task_pt_regs(target);
467 compat_ulong_t __user *reg_window;
468 const compat_ulong_t *k = kbuf;
469 const compat_ulong_t __user *u = ubuf;
472 if (target == current)
476 count /= sizeof(reg);
479 for (; count > 0 && pos < 16; count--)
480 regs->u_regs[pos++] = *k++;
482 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
483 for (; count > 0 && pos < 32; count--) {
484 if (put_user(*k++, ®_window[pos++]))
488 for (; count > 0 && pos < 16; count--) {
489 if (get_user(reg, u++))
491 regs->u_regs[pos++] = reg;
494 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
495 for (; count > 0 && pos < 32; count--) {
496 if (get_user(reg, u++) ||
497 put_user(reg, ®_window[pos++]))
502 unsigned long tstate;
506 else if (get_user(reg, u++))
511 tstate = regs->tstate;
512 tstate &= ~(TSTATE_ICC | TSTATE_XCC);
513 tstate |= psr_to_tstate_icc(reg);
514 regs->tstate = tstate;
537 count *= sizeof(reg);
539 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
540 38 * sizeof(reg), -1);
543 static int fpregs32_get(struct task_struct *target,
544 const struct user_regset *regset,
545 unsigned int pos, unsigned int count,
546 void *kbuf, void __user *ubuf)
548 const unsigned long *fpregs = task_thread_info(target)->fpregs;
549 compat_ulong_t enabled;
554 if (target == current)
555 save_and_clear_fpu();
557 fprs = task_thread_info(target)->fpsaved[0];
558 if (fprs & FPRS_FEF) {
559 fsr = task_thread_info(target)->xfsr[0];
566 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
568 0, 32 * sizeof(u32));
571 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
575 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
583 val = (enabled << 8) | (8 << 16);
584 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
591 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
592 35 * sizeof(u32), -1);
597 static int fpregs32_set(struct task_struct *target,
598 const struct user_regset *regset,
599 unsigned int pos, unsigned int count,
600 const void *kbuf, const void __user *ubuf)
602 unsigned long *fpregs = task_thread_info(target)->fpregs;
606 if (target == current)
607 save_and_clear_fpu();
609 fprs = task_thread_info(target)->fpsaved[0];
611 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
613 0, 32 * sizeof(u32));
615 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
618 if (!ret && count > 0) {
622 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
627 val = task_thread_info(target)->xfsr[0];
628 val &= 0xffffffff00000000UL;
630 task_thread_info(target)->xfsr[0] = val;
634 fprs |= (FPRS_FEF | FPRS_DL);
635 task_thread_info(target)->fpsaved[0] = fprs;
638 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
639 34 * sizeof(u32), -1);
643 static const struct user_regset sparc32_regsets[] = {
649 * PSR, PC, nPC, Y, WIM, TBR
652 .core_note_type = NT_PRSTATUS,
653 .n = 38 * sizeof(u32),
654 .size = sizeof(u32), .align = sizeof(u32),
655 .get = genregs32_get, .set = genregs32_set
661 * FPU QUEUE COUNT (8-bit char)
662 * FPU QUEUE ENTRYSIZE (8-bit char)
663 * FPU ENABLED (8-bit char)
665 * FPU QUEUE (64 32-bit ints)
668 .core_note_type = NT_PRFPREG,
669 .n = 99 * sizeof(u32),
670 .size = sizeof(u32), .align = sizeof(u32),
671 .get = fpregs32_get, .set = fpregs32_set
675 static const struct user_regset_view user_sparc32_view = {
676 .name = "sparc", .e_machine = EM_SPARC,
677 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
680 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
682 if (test_tsk_thread_flag(task, TIF_32BIT))
683 return &user_sparc32_view;
684 return &user_sparc64_view;
688 unsigned int regs[32];
694 unsigned int insnaddr;
699 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
700 compat_ulong_t caddr, compat_ulong_t cdata)
702 const struct user_regset_view *view = task_user_regset_view(child);
703 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
704 struct pt_regs32 __user *pregs;
705 struct compat_fps __user *fps;
706 unsigned long addr2 = caddr2;
707 unsigned long addr = caddr;
708 unsigned long data = cdata;
711 pregs = (struct pt_regs32 __user *) addr;
712 fps = (struct compat_fps __user *) addr;
716 ret = (addr != 0) ? -EIO : 0;
720 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
725 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
732 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
737 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
743 case PTRACE_GETFPREGS:
744 ret = copy_regset_to_user(child, view, REGSET_FP,
749 ret = copy_regset_to_user(child, view, REGSET_FP,
754 if (__put_user(0, &fps->flags) ||
755 __put_user(0, &fps->extra) ||
756 __put_user(0, &fps->fpqd) ||
757 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
762 case PTRACE_SETFPREGS:
763 ret = copy_regset_from_user(child, view, REGSET_FP,
768 ret = copy_regset_from_user(child, view, REGSET_FP,
774 case PTRACE_READTEXT:
775 case PTRACE_READDATA:
776 ret = ptrace_readdata(child, addr,
777 (char __user *)addr2, data);
784 case PTRACE_WRITETEXT:
785 case PTRACE_WRITEDATA:
786 ret = ptrace_writedata(child, (char __user *) addr2,
795 ret = compat_ptrace_request(child, request, addr, data);
803 unsigned int regs[64];
807 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
809 const struct user_regset_view *view = task_user_regset_view(child);
810 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
811 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
812 struct fps __user *fps = (struct fps __user *) addr;
817 ret = (addr != 0) ? -EIO : 0;
820 case PTRACE_GETREGS64:
821 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
826 /* XXX doesn't handle 'y' register correctly XXX */
827 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
834 case PTRACE_SETREGS64:
835 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
840 /* XXX doesn't handle 'y' register correctly XXX */
841 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
848 case PTRACE_GETFPREGS64:
849 ret = copy_regset_to_user(child, view, REGSET_FP,
855 case PTRACE_SETFPREGS64:
856 ret = copy_regset_to_user(child, view, REGSET_FP,
862 case PTRACE_READTEXT:
863 case PTRACE_READDATA:
864 ret = ptrace_readdata(child, addr,
865 (char __user *)addr2, data);
872 case PTRACE_WRITETEXT:
873 case PTRACE_WRITEDATA:
874 ret = ptrace_writedata(child, (char __user *) addr2,
883 ret = ptrace_request(child, request, addr, data);
890 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
892 /* do the secure computing check first */
893 secure_computing(regs->u_regs[UREG_G1]);
895 if (unlikely(current->audit_context) && syscall_exit_p) {
896 unsigned long tstate = regs->tstate;
897 int result = AUDITSC_SUCCESS;
899 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
900 result = AUDITSC_FAILURE;
902 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
905 if (!(current->ptrace & PT_PTRACED))
908 if (!test_thread_flag(TIF_SYSCALL_TRACE))
911 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
915 * this isn't the same as continuing with a signal, but it will do
916 * for normal use. strace only continues with a signal if the
917 * stopping signal is not SIGTRAP. -brl
919 if (current->exit_code) {
920 send_sig(current->exit_code, current, 1);
921 current->exit_code = 0;
925 if (unlikely(current->audit_context) && !syscall_exit_p)
926 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
929 regs->u_regs[UREG_G1],
930 regs->u_regs[UREG_I0],
931 regs->u_regs[UREG_I1],
932 regs->u_regs[UREG_I2],
933 regs->u_regs[UREG_I3]);