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/tracehook.h>
27 #include <linux/compat.h>
28 #include <linux/elf.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/uaccess.h>
34 #include <asm/psrcompat.h>
35 #include <asm/visasm.h>
36 #include <asm/spitfire.h>
38 #include <asm/cpudata.h>
39 #include <asm/cacheflush.h>
43 /* #define ALLOW_INIT_TRACING */
46 * Called by kernel/ptrace.c when detaching..
48 * Make sure single step bits etc are not set.
50 void ptrace_disable(struct task_struct *child)
55 /* To get the necessary page struct, access_process_vm() first calls
56 * get_user_pages(). This has done a flush_dcache_page() on the
57 * accessed page. Then our caller (copy_{to,from}_user_page()) did
58 * to memcpy to read/write the data from that page.
60 * Now, the only thing we have to do is:
61 * 1) flush the D-cache if it's possible than an illegal alias
63 * 2) flush the I-cache if this is pre-cheetah and we did a write
65 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
66 unsigned long uaddr, void *kaddr,
67 unsigned long len, int write)
69 BUG_ON(len > PAGE_SIZE);
71 if (tlb_type == hypervisor)
76 #ifdef DCACHE_ALIASING_POSSIBLE
77 /* If bit 13 of the kernel address we used to access the
78 * user page is the same as the virtual address that page
79 * is mapped to in the user's address space, we can skip the
82 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
83 unsigned long start = __pa(kaddr);
84 unsigned long end = start + len;
85 unsigned long dcache_line_size;
87 dcache_line_size = local_cpu_data().dcache_line_size;
89 if (tlb_type == spitfire) {
90 for (; start < end; start += dcache_line_size)
91 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
93 start &= ~(dcache_line_size - 1);
94 for (; start < end; start += dcache_line_size)
96 "stxa %%g0, [%0] %1\n\t"
100 "i" (ASI_DCACHE_INVALIDATE));
104 if (write && tlb_type == spitfire) {
105 unsigned long start = (unsigned long) kaddr;
106 unsigned long end = start + len;
107 unsigned long icache_line_size;
109 icache_line_size = local_cpu_data().icache_line_size;
111 for (; start < end; start += icache_line_size)
118 static int get_from_target(struct task_struct *target, unsigned long uaddr,
121 if (target == current) {
122 if (copy_from_user(kbuf, (void __user *) uaddr, len))
125 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
132 static int set_to_target(struct task_struct *target, unsigned long uaddr,
135 if (target == current) {
136 if (copy_to_user((void __user *) uaddr, kbuf, len))
139 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
146 static int regwindow64_get(struct task_struct *target,
147 const struct pt_regs *regs,
148 struct reg_window *wbuf)
150 unsigned long rw_addr = regs->u_regs[UREG_I6];
152 if (test_tsk_thread_flag(current, TIF_32BIT)) {
153 struct reg_window32 win32;
156 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
158 for (i = 0; i < 8; i++)
159 wbuf->locals[i] = win32.locals[i];
160 for (i = 0; i < 8; i++)
161 wbuf->ins[i] = win32.ins[i];
163 rw_addr += STACK_BIAS;
164 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
171 static int regwindow64_set(struct task_struct *target,
172 const struct pt_regs *regs,
173 struct reg_window *wbuf)
175 unsigned long rw_addr = regs->u_regs[UREG_I6];
177 if (test_tsk_thread_flag(current, TIF_32BIT)) {
178 struct reg_window32 win32;
181 for (i = 0; i < 8; i++)
182 win32.locals[i] = wbuf->locals[i];
183 for (i = 0; i < 8; i++)
184 win32.ins[i] = wbuf->ins[i];
186 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
189 rw_addr += STACK_BIAS;
190 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
202 static int genregs64_get(struct task_struct *target,
203 const struct user_regset *regset,
204 unsigned int pos, unsigned int count,
205 void *kbuf, void __user *ubuf)
207 const struct pt_regs *regs = task_pt_regs(target);
210 if (target == current)
213 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
215 0, 16 * sizeof(u64));
216 if (!ret && count && pos < (32 * sizeof(u64))) {
217 struct reg_window window;
219 if (regwindow64_get(target, regs, &window))
221 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
228 /* TSTATE, TPC, TNPC */
229 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
236 unsigned long y = regs->y;
238 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
245 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
246 36 * sizeof(u64), -1);
252 static int genregs64_set(struct task_struct *target,
253 const struct user_regset *regset,
254 unsigned int pos, unsigned int count,
255 const void *kbuf, const void __user *ubuf)
257 struct pt_regs *regs = task_pt_regs(target);
260 if (target == current)
263 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
265 0, 16 * sizeof(u64));
266 if (!ret && count && pos < (32 * sizeof(u64))) {
267 struct reg_window window;
269 if (regwindow64_get(target, regs, &window))
272 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
278 regwindow64_set(target, regs, &window))
282 if (!ret && count > 0) {
283 unsigned long tstate;
286 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
291 /* Only the condition codes and the "in syscall"
292 * state can be modified in the %tstate register.
294 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
295 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
296 regs->tstate |= tstate;
302 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
311 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
320 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
321 36 * sizeof(u64), -1);
326 static int fpregs64_get(struct task_struct *target,
327 const struct user_regset *regset,
328 unsigned int pos, unsigned int count,
329 void *kbuf, void __user *ubuf)
331 const unsigned long *fpregs = task_thread_info(target)->fpregs;
332 unsigned long fprs, fsr, gsr;
335 if (target == current)
336 save_and_clear_fpu();
338 fprs = task_thread_info(target)->fpsaved[0];
341 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
343 0, 16 * sizeof(u64));
345 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
351 ret = user_regset_copyout(&pos, &count,
357 ret = user_regset_copyout_zero(&pos, &count,
363 if (fprs & FPRS_FEF) {
364 fsr = task_thread_info(target)->xfsr[0];
365 gsr = task_thread_info(target)->gsr[0];
371 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
376 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
381 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
387 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
388 35 * sizeof(u64), -1);
393 static int fpregs64_set(struct task_struct *target,
394 const struct user_regset *regset,
395 unsigned int pos, unsigned int count,
396 const void *kbuf, const void __user *ubuf)
398 unsigned long *fpregs = task_thread_info(target)->fpregs;
402 if (target == current)
403 save_and_clear_fpu();
405 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
407 0, 32 * sizeof(u64));
409 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
410 task_thread_info(target)->xfsr,
414 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
415 task_thread_info(target)->gsr,
419 fprs = task_thread_info(target)->fpsaved[0];
420 if (!ret && count > 0) {
421 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
427 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
428 task_thread_info(target)->fpsaved[0] = fprs;
431 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
432 35 * sizeof(u64), -1);
436 static const struct user_regset sparc64_regsets[] = {
442 * TSTATE, TPC, TNPC, Y
445 .core_note_type = NT_PRSTATUS,
447 .size = sizeof(u64), .align = sizeof(u64),
448 .get = genregs64_get, .set = genregs64_set
457 .core_note_type = NT_PRFPREG,
459 .size = sizeof(u64), .align = sizeof(u64),
460 .get = fpregs64_get, .set = fpregs64_set
464 static const struct user_regset_view user_sparc64_view = {
465 .name = "sparc64", .e_machine = EM_SPARCV9,
466 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
470 static int genregs32_get(struct task_struct *target,
471 const struct user_regset *regset,
472 unsigned int pos, unsigned int count,
473 void *kbuf, void __user *ubuf)
475 const struct pt_regs *regs = task_pt_regs(target);
476 compat_ulong_t __user *reg_window;
477 compat_ulong_t *k = kbuf;
478 compat_ulong_t __user *u = ubuf;
481 if (target == current)
485 count /= sizeof(reg);
488 for (; count > 0 && pos < 16; count--)
489 *k++ = regs->u_regs[pos++];
491 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
492 if (target == current) {
493 for (; count > 0 && pos < 32; count--) {
494 if (get_user(*k++, ®_window[pos++]))
498 for (; count > 0 && pos < 32; count--) {
499 if (access_process_vm(target,
510 for (; count > 0 && pos < 16; count--) {
511 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
515 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
516 if (target == current) {
517 for (; count > 0 && pos < 32; count--) {
518 if (get_user(reg, ®_window[pos++]) ||
523 for (; count > 0 && pos < 32; count--) {
524 if (access_process_vm(target,
527 ®, sizeof(reg), 0)
530 if (access_process_vm(target,
532 ®, sizeof(reg), 1)
543 reg = tstate_to_psr(regs->tstate);
564 else if (put_user(reg, u++))
571 count *= sizeof(reg);
573 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
574 38 * sizeof(reg), -1);
577 static int genregs32_set(struct task_struct *target,
578 const struct user_regset *regset,
579 unsigned int pos, unsigned int count,
580 const void *kbuf, const void __user *ubuf)
582 struct pt_regs *regs = task_pt_regs(target);
583 compat_ulong_t __user *reg_window;
584 const compat_ulong_t *k = kbuf;
585 const compat_ulong_t __user *u = ubuf;
588 if (target == current)
592 count /= sizeof(reg);
595 for (; count > 0 && pos < 16; count--)
596 regs->u_regs[pos++] = *k++;
598 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
599 if (target == current) {
600 for (; count > 0 && pos < 32; count--) {
601 if (put_user(*k++, ®_window[pos++]))
605 for (; count > 0 && pos < 32; count--) {
606 if (access_process_vm(target,
618 for (; count > 0 && pos < 16; count--) {
619 if (get_user(reg, u++))
621 regs->u_regs[pos++] = reg;
624 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
625 if (target == current) {
626 for (; count > 0 && pos < 32; count--) {
627 if (get_user(reg, u++) ||
628 put_user(reg, ®_window[pos++]))
632 for (; count > 0 && pos < 32; count--) {
633 if (access_process_vm(target,
636 ®, sizeof(reg), 0)
639 if (access_process_vm(target,
642 ®, sizeof(reg), 1)
651 unsigned long tstate;
655 else if (get_user(reg, u++))
660 tstate = regs->tstate;
661 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
662 tstate |= psr_to_tstate_icc(reg);
663 if (reg & PSR_SYSCALL)
664 tstate |= TSTATE_SYSCALL;
665 regs->tstate = tstate;
688 count *= sizeof(reg);
690 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
691 38 * sizeof(reg), -1);
694 static int fpregs32_get(struct task_struct *target,
695 const struct user_regset *regset,
696 unsigned int pos, unsigned int count,
697 void *kbuf, void __user *ubuf)
699 const unsigned long *fpregs = task_thread_info(target)->fpregs;
700 compat_ulong_t enabled;
705 if (target == current)
706 save_and_clear_fpu();
708 fprs = task_thread_info(target)->fpsaved[0];
709 if (fprs & FPRS_FEF) {
710 fsr = task_thread_info(target)->xfsr[0];
717 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
719 0, 32 * sizeof(u32));
722 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
726 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
734 val = (enabled << 8) | (8 << 16);
735 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
742 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
743 35 * sizeof(u32), -1);
748 static int fpregs32_set(struct task_struct *target,
749 const struct user_regset *regset,
750 unsigned int pos, unsigned int count,
751 const void *kbuf, const void __user *ubuf)
753 unsigned long *fpregs = task_thread_info(target)->fpregs;
757 if (target == current)
758 save_and_clear_fpu();
760 fprs = task_thread_info(target)->fpsaved[0];
762 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
764 0, 32 * sizeof(u32));
766 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
769 if (!ret && count > 0) {
773 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
778 val = task_thread_info(target)->xfsr[0];
779 val &= 0xffffffff00000000UL;
781 task_thread_info(target)->xfsr[0] = val;
785 fprs |= (FPRS_FEF | FPRS_DL);
786 task_thread_info(target)->fpsaved[0] = fprs;
789 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
790 34 * sizeof(u32), -1);
794 static const struct user_regset sparc32_regsets[] = {
800 * PSR, PC, nPC, Y, WIM, TBR
803 .core_note_type = NT_PRSTATUS,
805 .size = sizeof(u32), .align = sizeof(u32),
806 .get = genregs32_get, .set = genregs32_set
812 * FPU QUEUE COUNT (8-bit char)
813 * FPU QUEUE ENTRYSIZE (8-bit char)
814 * FPU ENABLED (8-bit char)
816 * FPU QUEUE (64 32-bit ints)
819 .core_note_type = NT_PRFPREG,
821 .size = sizeof(u32), .align = sizeof(u32),
822 .get = fpregs32_get, .set = fpregs32_set
826 static const struct user_regset_view user_sparc32_view = {
827 .name = "sparc", .e_machine = EM_SPARC,
828 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
830 #endif /* CONFIG_COMPAT */
832 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
835 if (test_tsk_thread_flag(task, TIF_32BIT))
836 return &user_sparc32_view;
838 return &user_sparc64_view;
843 unsigned int regs[32];
849 unsigned int insnaddr;
854 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
855 compat_ulong_t caddr, compat_ulong_t cdata)
857 const struct user_regset_view *view = task_user_regset_view(current);
858 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
859 struct pt_regs32 __user *pregs;
860 struct compat_fps __user *fps;
861 unsigned long addr2 = caddr2;
862 unsigned long addr = caddr;
863 unsigned long data = cdata;
866 pregs = (struct pt_regs32 __user *) addr;
867 fps = (struct compat_fps __user *) addr;
871 ret = (addr != 0) ? -EIO : 0;
875 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
880 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
887 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
892 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
898 case PTRACE_GETFPREGS:
899 ret = copy_regset_to_user(child, view, REGSET_FP,
904 ret = copy_regset_to_user(child, view, REGSET_FP,
909 if (__put_user(0, &fps->flags) ||
910 __put_user(0, &fps->extra) ||
911 __put_user(0, &fps->fpqd) ||
912 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
917 case PTRACE_SETFPREGS:
918 ret = copy_regset_from_user(child, view, REGSET_FP,
923 ret = copy_regset_from_user(child, view, REGSET_FP,
929 case PTRACE_READTEXT:
930 case PTRACE_READDATA:
931 ret = ptrace_readdata(child, addr,
932 (char __user *)addr2, data);
939 case PTRACE_WRITETEXT:
940 case PTRACE_WRITEDATA:
941 ret = ptrace_writedata(child, (char __user *) addr2,
950 if (request == PTRACE_SPARC_DETACH)
951 request = PTRACE_DETACH;
952 ret = compat_ptrace_request(child, request, addr, data);
958 #endif /* CONFIG_COMPAT */
961 unsigned int regs[64];
965 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
967 const struct user_regset_view *view = task_user_regset_view(current);
968 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
969 struct pt_regs __user *pregs;
970 struct fps __user *fps;
973 pregs = (struct pt_regs __user *) (unsigned long) addr;
974 fps = (struct fps __user *) (unsigned long) addr;
978 ret = (addr != 0) ? -EIO : 0;
981 case PTRACE_GETREGS64:
982 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
987 /* XXX doesn't handle 'y' register correctly XXX */
988 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
995 case PTRACE_SETREGS64:
996 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1001 /* XXX doesn't handle 'y' register correctly XXX */
1002 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1009 case PTRACE_GETFPREGS64:
1010 ret = copy_regset_to_user(child, view, REGSET_FP,
1016 case PTRACE_SETFPREGS64:
1017 ret = copy_regset_to_user(child, view, REGSET_FP,
1023 case PTRACE_READTEXT:
1024 case PTRACE_READDATA:
1025 ret = ptrace_readdata(child, addr,
1026 (char __user *)addr2, data);
1033 case PTRACE_WRITETEXT:
1034 case PTRACE_WRITEDATA:
1035 ret = ptrace_writedata(child, (char __user *) addr2,
1044 if (request == PTRACE_SPARC_DETACH)
1045 request = PTRACE_DETACH;
1046 ret = ptrace_request(child, request, addr, data);
1053 asmlinkage int syscall_trace(struct pt_regs *regs, int syscall_exit_p)
1057 /* do the secure computing check first */
1058 secure_computing(regs->u_regs[UREG_G1]);
1060 if (unlikely(current->audit_context) && syscall_exit_p) {
1061 unsigned long tstate = regs->tstate;
1062 int result = AUDITSC_SUCCESS;
1064 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
1065 result = AUDITSC_FAILURE;
1067 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
1070 if (test_thread_flag(TIF_SYSCALL_TRACE)) {
1072 tracehook_report_syscall_exit(regs, 0);
1074 ret = tracehook_report_syscall_entry(regs);
1077 if (unlikely(current->audit_context) && !syscall_exit_p && !ret)
1078 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1080 AUDIT_ARCH_SPARC64),
1081 regs->u_regs[UREG_G1],
1082 regs->u_regs[UREG_I0],
1083 regs->u_regs[UREG_I1],
1084 regs->u_regs[UREG_I2],
1085 regs->u_regs[UREG_I3]);