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 can be modified
291 * in the %tstate register.
293 tstate &= (TSTATE_ICC | TSTATE_XCC);
294 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
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);
661 tstate |= psr_to_tstate_icc(reg);
662 regs->tstate = tstate;
685 count *= sizeof(reg);
687 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
688 38 * sizeof(reg), -1);
691 static int fpregs32_get(struct task_struct *target,
692 const struct user_regset *regset,
693 unsigned int pos, unsigned int count,
694 void *kbuf, void __user *ubuf)
696 const unsigned long *fpregs = task_thread_info(target)->fpregs;
697 compat_ulong_t enabled;
702 if (target == current)
703 save_and_clear_fpu();
705 fprs = task_thread_info(target)->fpsaved[0];
706 if (fprs & FPRS_FEF) {
707 fsr = task_thread_info(target)->xfsr[0];
714 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
716 0, 32 * sizeof(u32));
719 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
723 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
731 val = (enabled << 8) | (8 << 16);
732 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
739 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
740 35 * sizeof(u32), -1);
745 static int fpregs32_set(struct task_struct *target,
746 const struct user_regset *regset,
747 unsigned int pos, unsigned int count,
748 const void *kbuf, const void __user *ubuf)
750 unsigned long *fpregs = task_thread_info(target)->fpregs;
754 if (target == current)
755 save_and_clear_fpu();
757 fprs = task_thread_info(target)->fpsaved[0];
759 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
761 0, 32 * sizeof(u32));
763 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
766 if (!ret && count > 0) {
770 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
775 val = task_thread_info(target)->xfsr[0];
776 val &= 0xffffffff00000000UL;
778 task_thread_info(target)->xfsr[0] = val;
782 fprs |= (FPRS_FEF | FPRS_DL);
783 task_thread_info(target)->fpsaved[0] = fprs;
786 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
787 34 * sizeof(u32), -1);
791 static const struct user_regset sparc32_regsets[] = {
797 * PSR, PC, nPC, Y, WIM, TBR
800 .core_note_type = NT_PRSTATUS,
801 .n = 38 * sizeof(u32),
802 .size = sizeof(u32), .align = sizeof(u32),
803 .get = genregs32_get, .set = genregs32_set
809 * FPU QUEUE COUNT (8-bit char)
810 * FPU QUEUE ENTRYSIZE (8-bit char)
811 * FPU ENABLED (8-bit char)
813 * FPU QUEUE (64 32-bit ints)
816 .core_note_type = NT_PRFPREG,
817 .n = 99 * sizeof(u32),
818 .size = sizeof(u32), .align = sizeof(u32),
819 .get = fpregs32_get, .set = fpregs32_set
823 static const struct user_regset_view user_sparc32_view = {
824 .name = "sparc", .e_machine = EM_SPARC,
825 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
827 #endif /* CONFIG_COMPAT */
829 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
832 if (test_tsk_thread_flag(task, TIF_32BIT))
833 return &user_sparc32_view;
835 return &user_sparc64_view;
840 unsigned int regs[32];
846 unsigned int insnaddr;
851 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
852 compat_ulong_t caddr, compat_ulong_t cdata)
854 const struct user_regset_view *view = task_user_regset_view(current);
855 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
856 struct pt_regs32 __user *pregs;
857 struct compat_fps __user *fps;
858 unsigned long addr2 = caddr2;
859 unsigned long addr = caddr;
860 unsigned long data = cdata;
863 pregs = (struct pt_regs32 __user *) addr;
864 fps = (struct compat_fps __user *) addr;
868 ret = (addr != 0) ? -EIO : 0;
872 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
877 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
884 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
889 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
895 case PTRACE_GETFPREGS:
896 ret = copy_regset_to_user(child, view, REGSET_FP,
901 ret = copy_regset_to_user(child, view, REGSET_FP,
906 if (__put_user(0, &fps->flags) ||
907 __put_user(0, &fps->extra) ||
908 __put_user(0, &fps->fpqd) ||
909 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
914 case PTRACE_SETFPREGS:
915 ret = copy_regset_from_user(child, view, REGSET_FP,
920 ret = copy_regset_from_user(child, view, REGSET_FP,
926 case PTRACE_READTEXT:
927 case PTRACE_READDATA:
928 ret = ptrace_readdata(child, addr,
929 (char __user *)addr2, data);
936 case PTRACE_WRITETEXT:
937 case PTRACE_WRITEDATA:
938 ret = ptrace_writedata(child, (char __user *) addr2,
947 ret = compat_ptrace_request(child, request, addr, data);
953 #endif /* CONFIG_COMPAT */
956 unsigned int regs[64];
960 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
962 const struct user_regset_view *view = task_user_regset_view(current);
963 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
964 struct pt_regs __user *pregs;
965 struct fps __user *fps;
968 pregs = (struct pt_regs __user *) (unsigned long) addr;
969 fps = (struct fps __user *) (unsigned long) addr;
973 ret = (addr != 0) ? -EIO : 0;
976 case PTRACE_GETREGS64:
977 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
982 /* XXX doesn't handle 'y' register correctly XXX */
983 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
990 case PTRACE_SETREGS64:
991 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
996 /* XXX doesn't handle 'y' register correctly XXX */
997 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1004 case PTRACE_GETFPREGS64:
1005 ret = copy_regset_to_user(child, view, REGSET_FP,
1011 case PTRACE_SETFPREGS64:
1012 ret = copy_regset_to_user(child, view, REGSET_FP,
1018 case PTRACE_READTEXT:
1019 case PTRACE_READDATA:
1020 ret = ptrace_readdata(child, addr,
1021 (char __user *)addr2, data);
1028 case PTRACE_WRITETEXT:
1029 case PTRACE_WRITEDATA:
1030 ret = ptrace_writedata(child, (char __user *) addr2,
1039 ret = ptrace_request(child, request, addr, data);
1046 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
1048 /* do the secure computing check first */
1049 secure_computing(regs->u_regs[UREG_G1]);
1051 if (unlikely(current->audit_context) && syscall_exit_p) {
1052 unsigned long tstate = regs->tstate;
1053 int result = AUDITSC_SUCCESS;
1055 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
1056 result = AUDITSC_FAILURE;
1058 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
1061 if (!(current->ptrace & PT_PTRACED))
1064 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1067 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1071 * this isn't the same as continuing with a signal, but it will do
1072 * for normal use. strace only continues with a signal if the
1073 * stopping signal is not SIGTRAP. -brl
1075 if (current->exit_code) {
1076 send_sig(current->exit_code, current, 1);
1077 current->exit_code = 0;
1081 if (unlikely(current->audit_context) && !syscall_exit_p)
1082 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1084 AUDIT_ARCH_SPARC64),
1085 regs->u_regs[UREG_G1],
1086 regs->u_regs[UREG_I0],
1087 regs->u_regs[UREG_I1],
1088 regs->u_regs[UREG_I2],
1089 regs->u_regs[UREG_I3]);