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>
38 #include <asm/cacheflush.h>
42 /* #define ALLOW_INIT_TRACING */
45 * Called by kernel/ptrace.c when detaching..
47 * Make sure single step bits etc are not set.
49 void ptrace_disable(struct task_struct *child)
54 /* To get the necessary page struct, access_process_vm() first calls
55 * get_user_pages(). This has done a flush_dcache_page() on the
56 * accessed page. Then our caller (copy_{to,from}_user_page()) did
57 * to memcpy to read/write the data from that page.
59 * Now, the only thing we have to do is:
60 * 1) flush the D-cache if it's possible than an illegal alias
62 * 2) flush the I-cache if this is pre-cheetah and we did a write
64 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
65 unsigned long uaddr, void *kaddr,
66 unsigned long len, int write)
68 BUG_ON(len > PAGE_SIZE);
70 if (tlb_type == hypervisor)
75 #ifdef DCACHE_ALIASING_POSSIBLE
76 /* If bit 13 of the kernel address we used to access the
77 * user page is the same as the virtual address that page
78 * is mapped to in the user's address space, we can skip the
81 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
82 unsigned long start = __pa(kaddr);
83 unsigned long end = start + len;
84 unsigned long dcache_line_size;
86 dcache_line_size = local_cpu_data().dcache_line_size;
88 if (tlb_type == spitfire) {
89 for (; start < end; start += dcache_line_size)
90 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
92 start &= ~(dcache_line_size - 1);
93 for (; start < end; start += dcache_line_size)
95 "stxa %%g0, [%0] %1\n\t"
99 "i" (ASI_DCACHE_INVALIDATE));
103 if (write && tlb_type == spitfire) {
104 unsigned long start = (unsigned long) kaddr;
105 unsigned long end = start + len;
106 unsigned long icache_line_size;
108 icache_line_size = local_cpu_data().icache_line_size;
110 for (; start < end; start += icache_line_size)
117 static int get_from_target(struct task_struct *target, unsigned long uaddr,
120 if (target == current) {
121 if (copy_from_user(kbuf, (void __user *) uaddr, len))
124 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
131 static int set_to_target(struct task_struct *target, unsigned long uaddr,
134 if (target == current) {
135 if (copy_to_user((void __user *) uaddr, kbuf, len))
138 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
145 static int regwindow64_get(struct task_struct *target,
146 const struct pt_regs *regs,
147 struct reg_window *wbuf)
149 unsigned long rw_addr = regs->u_regs[UREG_I6];
151 if (test_tsk_thread_flag(current, TIF_32BIT)) {
152 struct reg_window32 win32;
155 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
157 for (i = 0; i < 8; i++)
158 wbuf->locals[i] = win32.locals[i];
159 for (i = 0; i < 8; i++)
160 wbuf->ins[i] = win32.ins[i];
162 rw_addr += STACK_BIAS;
163 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
170 static int regwindow64_set(struct task_struct *target,
171 const struct pt_regs *regs,
172 struct reg_window *wbuf)
174 unsigned long rw_addr = regs->u_regs[UREG_I6];
176 if (test_tsk_thread_flag(current, TIF_32BIT)) {
177 struct reg_window32 win32;
180 for (i = 0; i < 8; i++)
181 win32.locals[i] = wbuf->locals[i];
182 for (i = 0; i < 8; i++)
183 win32.ins[i] = wbuf->ins[i];
185 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
188 rw_addr += STACK_BIAS;
189 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
201 static int genregs64_get(struct task_struct *target,
202 const struct user_regset *regset,
203 unsigned int pos, unsigned int count,
204 void *kbuf, void __user *ubuf)
206 const struct pt_regs *regs = task_pt_regs(target);
209 if (target == current)
212 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
214 0, 16 * sizeof(u64));
215 if (!ret && count && pos < (32 * sizeof(u64))) {
216 struct reg_window window;
218 if (regwindow64_get(target, regs, &window))
220 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
227 /* TSTATE, TPC, TNPC */
228 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
235 unsigned long y = regs->y;
237 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
244 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
245 36 * sizeof(u64), -1);
251 static int genregs64_set(struct task_struct *target,
252 const struct user_regset *regset,
253 unsigned int pos, unsigned int count,
254 const void *kbuf, const void __user *ubuf)
256 struct pt_regs *regs = task_pt_regs(target);
259 if (target == current)
262 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
264 0, 16 * sizeof(u64));
265 if (!ret && count && pos < (32 * sizeof(u64))) {
266 struct reg_window window;
268 if (regwindow64_get(target, regs, &window))
271 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
277 regwindow64_set(target, regs, &window))
281 if (!ret && count > 0) {
282 unsigned long tstate;
285 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
290 /* Only the condition codes and the "in syscall"
291 * state can be modified in the %tstate register.
293 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
294 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
295 regs->tstate |= tstate;
301 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
310 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
319 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
320 36 * sizeof(u64), -1);
325 static int fpregs64_get(struct task_struct *target,
326 const struct user_regset *regset,
327 unsigned int pos, unsigned int count,
328 void *kbuf, void __user *ubuf)
330 const unsigned long *fpregs = task_thread_info(target)->fpregs;
331 unsigned long fprs, fsr, gsr;
334 if (target == current)
335 save_and_clear_fpu();
337 fprs = task_thread_info(target)->fpsaved[0];
340 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
342 0, 16 * sizeof(u64));
344 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
350 ret = user_regset_copyout(&pos, &count,
356 ret = user_regset_copyout_zero(&pos, &count,
362 if (fprs & FPRS_FEF) {
363 fsr = task_thread_info(target)->xfsr[0];
364 gsr = task_thread_info(target)->gsr[0];
370 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
375 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
380 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
386 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
387 35 * sizeof(u64), -1);
392 static int fpregs64_set(struct task_struct *target,
393 const struct user_regset *regset,
394 unsigned int pos, unsigned int count,
395 const void *kbuf, const void __user *ubuf)
397 unsigned long *fpregs = task_thread_info(target)->fpregs;
401 if (target == current)
402 save_and_clear_fpu();
404 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
406 0, 32 * sizeof(u64));
408 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
409 task_thread_info(target)->xfsr,
413 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
414 task_thread_info(target)->gsr,
418 fprs = task_thread_info(target)->fpsaved[0];
419 if (!ret && count > 0) {
420 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
426 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
427 task_thread_info(target)->fpsaved[0] = fprs;
430 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
431 35 * sizeof(u64), -1);
435 static const struct user_regset sparc64_regsets[] = {
441 * TSTATE, TPC, TNPC, Y
444 .core_note_type = NT_PRSTATUS,
445 .n = 36 * sizeof(u64),
446 .size = sizeof(u64), .align = sizeof(u64),
447 .get = genregs64_get, .set = genregs64_set
456 .core_note_type = NT_PRFPREG,
457 .n = 35 * sizeof(u64),
458 .size = sizeof(u64), .align = sizeof(u64),
459 .get = fpregs64_get, .set = fpregs64_set
463 static const struct user_regset_view user_sparc64_view = {
464 .name = "sparc64", .e_machine = EM_SPARCV9,
465 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
469 static int genregs32_get(struct task_struct *target,
470 const struct user_regset *regset,
471 unsigned int pos, unsigned int count,
472 void *kbuf, void __user *ubuf)
474 const struct pt_regs *regs = task_pt_regs(target);
475 compat_ulong_t __user *reg_window;
476 compat_ulong_t *k = kbuf;
477 compat_ulong_t __user *u = ubuf;
480 if (target == current)
484 count /= sizeof(reg);
487 for (; count > 0 && pos < 16; count--)
488 *k++ = regs->u_regs[pos++];
490 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
491 if (target == current) {
492 for (; count > 0 && pos < 32; count--) {
493 if (get_user(*k++, ®_window[pos++]))
497 for (; count > 0 && pos < 32; count--) {
498 if (access_process_vm(target,
509 for (; count > 0 && pos < 16; count--) {
510 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
514 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
515 if (target == current) {
516 for (; count > 0 && pos < 32; count--) {
517 if (get_user(reg, ®_window[pos++]) ||
522 for (; count > 0 && pos < 32; count--) {
523 if (access_process_vm(target,
526 ®, sizeof(reg), 0)
529 if (access_process_vm(target,
531 ®, sizeof(reg), 1)
542 reg = tstate_to_psr(regs->tstate);
563 else if (put_user(reg, u++))
570 count *= sizeof(reg);
572 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
573 38 * sizeof(reg), -1);
576 static int genregs32_set(struct task_struct *target,
577 const struct user_regset *regset,
578 unsigned int pos, unsigned int count,
579 const void *kbuf, const void __user *ubuf)
581 struct pt_regs *regs = task_pt_regs(target);
582 compat_ulong_t __user *reg_window;
583 const compat_ulong_t *k = kbuf;
584 const compat_ulong_t __user *u = ubuf;
587 if (target == current)
591 count /= sizeof(reg);
594 for (; count > 0 && pos < 16; count--)
595 regs->u_regs[pos++] = *k++;
597 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
598 if (target == current) {
599 for (; count > 0 && pos < 32; count--) {
600 if (put_user(*k++, ®_window[pos++]))
604 for (; count > 0 && pos < 32; count--) {
605 if (access_process_vm(target,
617 for (; count > 0 && pos < 16; count--) {
618 if (get_user(reg, u++))
620 regs->u_regs[pos++] = reg;
623 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
624 if (target == current) {
625 for (; count > 0 && pos < 32; count--) {
626 if (get_user(reg, u++) ||
627 put_user(reg, ®_window[pos++]))
631 for (; count > 0 && pos < 32; count--) {
632 if (access_process_vm(target,
635 ®, sizeof(reg), 0)
638 if (access_process_vm(target,
641 ®, sizeof(reg), 1)
650 unsigned long tstate;
654 else if (get_user(reg, u++))
659 tstate = regs->tstate;
660 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
661 tstate |= psr_to_tstate_icc(reg);
662 if (reg & PSR_SYSCALL)
663 tstate |= TSTATE_SYSCALL;
664 regs->tstate = tstate;
687 count *= sizeof(reg);
689 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
690 38 * sizeof(reg), -1);
693 static int fpregs32_get(struct task_struct *target,
694 const struct user_regset *regset,
695 unsigned int pos, unsigned int count,
696 void *kbuf, void __user *ubuf)
698 const unsigned long *fpregs = task_thread_info(target)->fpregs;
699 compat_ulong_t enabled;
704 if (target == current)
705 save_and_clear_fpu();
707 fprs = task_thread_info(target)->fpsaved[0];
708 if (fprs & FPRS_FEF) {
709 fsr = task_thread_info(target)->xfsr[0];
716 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
718 0, 32 * sizeof(u32));
721 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
725 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
733 val = (enabled << 8) | (8 << 16);
734 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
741 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
742 35 * sizeof(u32), -1);
747 static int fpregs32_set(struct task_struct *target,
748 const struct user_regset *regset,
749 unsigned int pos, unsigned int count,
750 const void *kbuf, const void __user *ubuf)
752 unsigned long *fpregs = task_thread_info(target)->fpregs;
756 if (target == current)
757 save_and_clear_fpu();
759 fprs = task_thread_info(target)->fpsaved[0];
761 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
763 0, 32 * sizeof(u32));
765 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
768 if (!ret && count > 0) {
772 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
777 val = task_thread_info(target)->xfsr[0];
778 val &= 0xffffffff00000000UL;
780 task_thread_info(target)->xfsr[0] = val;
784 fprs |= (FPRS_FEF | FPRS_DL);
785 task_thread_info(target)->fpsaved[0] = fprs;
788 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
789 34 * sizeof(u32), -1);
793 static const struct user_regset sparc32_regsets[] = {
799 * PSR, PC, nPC, Y, WIM, TBR
802 .core_note_type = NT_PRSTATUS,
803 .n = 38 * sizeof(u32),
804 .size = sizeof(u32), .align = sizeof(u32),
805 .get = genregs32_get, .set = genregs32_set
811 * FPU QUEUE COUNT (8-bit char)
812 * FPU QUEUE ENTRYSIZE (8-bit char)
813 * FPU ENABLED (8-bit char)
815 * FPU QUEUE (64 32-bit ints)
818 .core_note_type = NT_PRFPREG,
819 .n = 99 * sizeof(u32),
820 .size = sizeof(u32), .align = sizeof(u32),
821 .get = fpregs32_get, .set = fpregs32_set
825 static const struct user_regset_view user_sparc32_view = {
826 .name = "sparc", .e_machine = EM_SPARC,
827 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
829 #endif /* CONFIG_COMPAT */
831 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
834 if (test_tsk_thread_flag(task, TIF_32BIT))
835 return &user_sparc32_view;
837 return &user_sparc64_view;
842 unsigned int regs[32];
848 unsigned int insnaddr;
853 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
854 compat_ulong_t caddr, compat_ulong_t cdata)
856 const struct user_regset_view *view = task_user_regset_view(current);
857 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
858 struct pt_regs32 __user *pregs;
859 struct compat_fps __user *fps;
860 unsigned long addr2 = caddr2;
861 unsigned long addr = caddr;
862 unsigned long data = cdata;
865 pregs = (struct pt_regs32 __user *) addr;
866 fps = (struct compat_fps __user *) addr;
870 ret = (addr != 0) ? -EIO : 0;
874 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
879 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
886 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
891 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
897 case PTRACE_GETFPREGS:
898 ret = copy_regset_to_user(child, view, REGSET_FP,
903 ret = copy_regset_to_user(child, view, REGSET_FP,
908 if (__put_user(0, &fps->flags) ||
909 __put_user(0, &fps->extra) ||
910 __put_user(0, &fps->fpqd) ||
911 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
916 case PTRACE_SETFPREGS:
917 ret = copy_regset_from_user(child, view, REGSET_FP,
922 ret = copy_regset_from_user(child, view, REGSET_FP,
928 case PTRACE_READTEXT:
929 case PTRACE_READDATA:
930 ret = ptrace_readdata(child, addr,
931 (char __user *)addr2, data);
938 case PTRACE_WRITETEXT:
939 case PTRACE_WRITEDATA:
940 ret = ptrace_writedata(child, (char __user *) addr2,
949 if (request == PTRACE_SPARC_DETACH)
950 request = PTRACE_DETACH;
951 ret = compat_ptrace_request(child, request, addr, data);
957 #endif /* CONFIG_COMPAT */
960 unsigned int regs[64];
964 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
966 const struct user_regset_view *view = task_user_regset_view(current);
967 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
968 struct pt_regs __user *pregs;
969 struct fps __user *fps;
972 pregs = (struct pt_regs __user *) (unsigned long) addr;
973 fps = (struct fps __user *) (unsigned long) addr;
977 ret = (addr != 0) ? -EIO : 0;
980 case PTRACE_GETREGS64:
981 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
986 /* XXX doesn't handle 'y' register correctly XXX */
987 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
994 case PTRACE_SETREGS64:
995 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1000 /* XXX doesn't handle 'y' register correctly XXX */
1001 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1008 case PTRACE_GETFPREGS64:
1009 ret = copy_regset_to_user(child, view, REGSET_FP,
1015 case PTRACE_SETFPREGS64:
1016 ret = copy_regset_to_user(child, view, REGSET_FP,
1022 case PTRACE_READTEXT:
1023 case PTRACE_READDATA:
1024 ret = ptrace_readdata(child, addr,
1025 (char __user *)addr2, data);
1032 case PTRACE_WRITETEXT:
1033 case PTRACE_WRITEDATA:
1034 ret = ptrace_writedata(child, (char __user *) addr2,
1043 if (request == PTRACE_SPARC_DETACH)
1044 request = PTRACE_DETACH;
1045 ret = ptrace_request(child, request, addr, data);
1052 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
1054 /* do the secure computing check first */
1055 secure_computing(regs->u_regs[UREG_G1]);
1057 if (unlikely(current->audit_context) && syscall_exit_p) {
1058 unsigned long tstate = regs->tstate;
1059 int result = AUDITSC_SUCCESS;
1061 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
1062 result = AUDITSC_FAILURE;
1064 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
1067 if (!(current->ptrace & PT_PTRACED))
1070 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1073 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1077 * this isn't the same as continuing with a signal, but it will do
1078 * for normal use. strace only continues with a signal if the
1079 * stopping signal is not SIGTRAP. -brl
1081 if (current->exit_code) {
1082 send_sig(current->exit_code, current, 1);
1083 current->exit_code = 0;
1087 if (unlikely(current->audit_context) && !syscall_exit_p)
1088 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1090 AUDIT_ARCH_SPARC64),
1091 regs->u_regs[UREG_G1],
1092 regs->u_regs[UREG_I0],
1093 regs->u_regs[UREG_I1],
1094 regs->u_regs[UREG_I2],
1095 regs->u_regs[UREG_I3]);