1 /* ptrace.c: Sparc process tracing support.
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/export.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/smp.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 <trace/syscall.h>
28 #include <linux/compat.h>
29 #include <linux/elf.h>
30 #include <linux/context_tracking.h>
33 #include <asm/pgtable.h>
34 #include <asm/uaccess.h>
35 #include <asm/psrcompat.h>
36 #include <asm/visasm.h>
37 #include <asm/spitfire.h>
39 #include <asm/cpudata.h>
40 #include <asm/cacheflush.h>
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/syscalls.h>
47 /* #define ALLOW_INIT_TRACING */
50 * Called by kernel/ptrace.c when detaching..
52 * Make sure single step bits etc are not set.
54 void ptrace_disable(struct task_struct *child)
59 /* To get the necessary page struct, access_process_vm() first calls
60 * get_user_pages(). This has done a flush_dcache_page() on the
61 * accessed page. Then our caller (copy_{to,from}_user_page()) did
62 * to memcpy to read/write the data from that page.
64 * Now, the only thing we have to do is:
65 * 1) flush the D-cache if it's possible than an illegal alias
67 * 2) flush the I-cache if this is pre-cheetah and we did a write
69 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
70 unsigned long uaddr, void *kaddr,
71 unsigned long len, int write)
73 BUG_ON(len > PAGE_SIZE);
75 if (tlb_type == hypervisor)
80 #ifdef DCACHE_ALIASING_POSSIBLE
81 /* If bit 13 of the kernel address we used to access the
82 * user page is the same as the virtual address that page
83 * is mapped to in the user's address space, we can skip the
86 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
87 unsigned long start = __pa(kaddr);
88 unsigned long end = start + len;
89 unsigned long dcache_line_size;
91 dcache_line_size = local_cpu_data().dcache_line_size;
93 if (tlb_type == spitfire) {
94 for (; start < end; start += dcache_line_size)
95 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
97 start &= ~(dcache_line_size - 1);
98 for (; start < end; start += dcache_line_size)
100 "stxa %%g0, [%0] %1\n\t"
104 "i" (ASI_DCACHE_INVALIDATE));
108 if (write && tlb_type == spitfire) {
109 unsigned long start = (unsigned long) kaddr;
110 unsigned long end = start + len;
111 unsigned long icache_line_size;
113 icache_line_size = local_cpu_data().icache_line_size;
115 for (; start < end; start += icache_line_size)
121 EXPORT_SYMBOL_GPL(flush_ptrace_access);
123 static int get_from_target(struct task_struct *target, unsigned long uaddr,
126 if (target == current) {
127 if (copy_from_user(kbuf, (void __user *) uaddr, len))
130 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
137 static int set_to_target(struct task_struct *target, unsigned long uaddr,
140 if (target == current) {
141 if (copy_to_user((void __user *) uaddr, kbuf, len))
144 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
151 static int regwindow64_get(struct task_struct *target,
152 const struct pt_regs *regs,
153 struct reg_window *wbuf)
155 unsigned long rw_addr = regs->u_regs[UREG_I6];
157 if (!test_thread_64bit_stack(rw_addr)) {
158 struct reg_window32 win32;
161 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
163 for (i = 0; i < 8; i++)
164 wbuf->locals[i] = win32.locals[i];
165 for (i = 0; i < 8; i++)
166 wbuf->ins[i] = win32.ins[i];
168 rw_addr += STACK_BIAS;
169 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
176 static int regwindow64_set(struct task_struct *target,
177 const struct pt_regs *regs,
178 struct reg_window *wbuf)
180 unsigned long rw_addr = regs->u_regs[UREG_I6];
182 if (!test_thread_64bit_stack(rw_addr)) {
183 struct reg_window32 win32;
186 for (i = 0; i < 8; i++)
187 win32.locals[i] = wbuf->locals[i];
188 for (i = 0; i < 8; i++)
189 win32.ins[i] = wbuf->ins[i];
191 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
194 rw_addr += STACK_BIAS;
195 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
207 static int genregs64_get(struct task_struct *target,
208 const struct user_regset *regset,
209 unsigned int pos, unsigned int count,
210 void *kbuf, void __user *ubuf)
212 const struct pt_regs *regs = task_pt_regs(target);
215 if (target == current)
218 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
220 0, 16 * sizeof(u64));
221 if (!ret && count && pos < (32 * sizeof(u64))) {
222 struct reg_window window;
224 if (regwindow64_get(target, regs, &window))
226 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
233 /* TSTATE, TPC, TNPC */
234 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
241 unsigned long y = regs->y;
243 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
250 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
251 36 * sizeof(u64), -1);
257 static int genregs64_set(struct task_struct *target,
258 const struct user_regset *regset,
259 unsigned int pos, unsigned int count,
260 const void *kbuf, const void __user *ubuf)
262 struct pt_regs *regs = task_pt_regs(target);
265 if (target == current)
268 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
270 0, 16 * sizeof(u64));
271 if (!ret && count && pos < (32 * sizeof(u64))) {
272 struct reg_window window;
274 if (regwindow64_get(target, regs, &window))
277 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
283 regwindow64_set(target, regs, &window))
287 if (!ret && count > 0) {
288 unsigned long tstate;
291 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
296 /* Only the condition codes and the "in syscall"
297 * state can be modified in the %tstate register.
299 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
300 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
301 regs->tstate |= tstate;
307 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
316 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
325 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
326 36 * sizeof(u64), -1);
331 static int fpregs64_get(struct task_struct *target,
332 const struct user_regset *regset,
333 unsigned int pos, unsigned int count,
334 void *kbuf, void __user *ubuf)
336 const unsigned long *fpregs = task_thread_info(target)->fpregs;
337 unsigned long fprs, fsr, gsr;
340 if (target == current)
341 save_and_clear_fpu();
343 fprs = task_thread_info(target)->fpsaved[0];
346 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
348 0, 16 * sizeof(u64));
350 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
356 ret = user_regset_copyout(&pos, &count,
362 ret = user_regset_copyout_zero(&pos, &count,
368 if (fprs & FPRS_FEF) {
369 fsr = task_thread_info(target)->xfsr[0];
370 gsr = task_thread_info(target)->gsr[0];
376 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
381 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
386 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
392 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
393 35 * sizeof(u64), -1);
398 static int fpregs64_set(struct task_struct *target,
399 const struct user_regset *regset,
400 unsigned int pos, unsigned int count,
401 const void *kbuf, const void __user *ubuf)
403 unsigned long *fpregs = task_thread_info(target)->fpregs;
407 if (target == current)
408 save_and_clear_fpu();
410 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
412 0, 32 * sizeof(u64));
414 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
415 task_thread_info(target)->xfsr,
419 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
420 task_thread_info(target)->gsr,
424 fprs = task_thread_info(target)->fpsaved[0];
425 if (!ret && count > 0) {
426 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
432 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
433 task_thread_info(target)->fpsaved[0] = fprs;
436 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
437 35 * sizeof(u64), -1);
441 static const struct user_regset sparc64_regsets[] = {
447 * TSTATE, TPC, TNPC, Y
450 .core_note_type = NT_PRSTATUS,
452 .size = sizeof(u64), .align = sizeof(u64),
453 .get = genregs64_get, .set = genregs64_set
462 .core_note_type = NT_PRFPREG,
464 .size = sizeof(u64), .align = sizeof(u64),
465 .get = fpregs64_get, .set = fpregs64_set
469 static const struct user_regset_view user_sparc64_view = {
470 .name = "sparc64", .e_machine = EM_SPARCV9,
471 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
475 static int genregs32_get(struct task_struct *target,
476 const struct user_regset *regset,
477 unsigned int pos, unsigned int count,
478 void *kbuf, void __user *ubuf)
480 const struct pt_regs *regs = task_pt_regs(target);
481 compat_ulong_t __user *reg_window;
482 compat_ulong_t *k = kbuf;
483 compat_ulong_t __user *u = ubuf;
486 if (target == current)
490 count /= sizeof(reg);
493 for (; count > 0 && pos < 16; count--)
494 *k++ = regs->u_regs[pos++];
496 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
498 if (target == current) {
499 for (; count > 0 && pos < 32; count--) {
500 if (get_user(*k++, ®_window[pos++]))
504 for (; count > 0 && pos < 32; count--) {
505 if (access_process_vm(target,
516 for (; count > 0 && pos < 16; count--) {
517 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
521 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
523 if (target == current) {
524 for (; count > 0 && pos < 32; count--) {
525 if (get_user(reg, ®_window[pos++]) ||
530 for (; count > 0 && pos < 32; count--) {
531 if (access_process_vm(target,
534 ®, sizeof(reg), 0)
537 if (access_process_vm(target,
539 ®, sizeof(reg), 1)
550 reg = tstate_to_psr(regs->tstate);
571 else if (put_user(reg, u++))
578 count *= sizeof(reg);
580 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
581 38 * sizeof(reg), -1);
584 static int genregs32_set(struct task_struct *target,
585 const struct user_regset *regset,
586 unsigned int pos, unsigned int count,
587 const void *kbuf, const void __user *ubuf)
589 struct pt_regs *regs = task_pt_regs(target);
590 compat_ulong_t __user *reg_window;
591 const compat_ulong_t *k = kbuf;
592 const compat_ulong_t __user *u = ubuf;
595 if (target == current)
599 count /= sizeof(reg);
602 for (; count > 0 && pos < 16; count--)
603 regs->u_regs[pos++] = *k++;
605 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
607 if (target == current) {
608 for (; count > 0 && pos < 32; count--) {
609 if (put_user(*k++, ®_window[pos++]))
613 for (; count > 0 && pos < 32; count--) {
614 if (access_process_vm(target,
626 for (; count > 0 && pos < 16; count--) {
627 if (get_user(reg, u++))
629 regs->u_regs[pos++] = reg;
632 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
634 if (target == current) {
635 for (; count > 0 && pos < 32; count--) {
636 if (get_user(reg, u++) ||
637 put_user(reg, ®_window[pos++]))
641 for (; count > 0 && pos < 32; count--) {
642 if (access_process_vm(target,
645 ®, sizeof(reg), 0)
648 if (access_process_vm(target,
651 ®, sizeof(reg), 1)
660 unsigned long tstate;
664 else if (get_user(reg, u++))
669 tstate = regs->tstate;
670 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
671 tstate |= psr_to_tstate_icc(reg);
672 if (reg & PSR_SYSCALL)
673 tstate |= TSTATE_SYSCALL;
674 regs->tstate = tstate;
697 count *= sizeof(reg);
699 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
700 38 * sizeof(reg), -1);
703 static int fpregs32_get(struct task_struct *target,
704 const struct user_regset *regset,
705 unsigned int pos, unsigned int count,
706 void *kbuf, void __user *ubuf)
708 const unsigned long *fpregs = task_thread_info(target)->fpregs;
709 compat_ulong_t enabled;
714 if (target == current)
715 save_and_clear_fpu();
717 fprs = task_thread_info(target)->fpsaved[0];
718 if (fprs & FPRS_FEF) {
719 fsr = task_thread_info(target)->xfsr[0];
726 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
728 0, 32 * sizeof(u32));
731 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
735 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
743 val = (enabled << 8) | (8 << 16);
744 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
751 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
752 35 * sizeof(u32), -1);
757 static int fpregs32_set(struct task_struct *target,
758 const struct user_regset *regset,
759 unsigned int pos, unsigned int count,
760 const void *kbuf, const void __user *ubuf)
762 unsigned long *fpregs = task_thread_info(target)->fpregs;
766 if (target == current)
767 save_and_clear_fpu();
769 fprs = task_thread_info(target)->fpsaved[0];
771 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
773 0, 32 * sizeof(u32));
775 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
778 if (!ret && count > 0) {
782 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
787 val = task_thread_info(target)->xfsr[0];
788 val &= 0xffffffff00000000UL;
790 task_thread_info(target)->xfsr[0] = val;
794 fprs |= (FPRS_FEF | FPRS_DL);
795 task_thread_info(target)->fpsaved[0] = fprs;
798 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
799 34 * sizeof(u32), -1);
803 static const struct user_regset sparc32_regsets[] = {
809 * PSR, PC, nPC, Y, WIM, TBR
812 .core_note_type = NT_PRSTATUS,
814 .size = sizeof(u32), .align = sizeof(u32),
815 .get = genregs32_get, .set = genregs32_set
821 * FPU QUEUE COUNT (8-bit char)
822 * FPU QUEUE ENTRYSIZE (8-bit char)
823 * FPU ENABLED (8-bit char)
825 * FPU QUEUE (64 32-bit ints)
828 .core_note_type = NT_PRFPREG,
830 .size = sizeof(u32), .align = sizeof(u32),
831 .get = fpregs32_get, .set = fpregs32_set
835 static const struct user_regset_view user_sparc32_view = {
836 .name = "sparc", .e_machine = EM_SPARC,
837 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
839 #endif /* CONFIG_COMPAT */
841 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
844 if (test_tsk_thread_flag(task, TIF_32BIT))
845 return &user_sparc32_view;
847 return &user_sparc64_view;
852 unsigned int regs[32];
858 unsigned int insnaddr;
863 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
864 compat_ulong_t caddr, compat_ulong_t cdata)
866 const struct user_regset_view *view = task_user_regset_view(current);
867 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
868 struct pt_regs32 __user *pregs;
869 struct compat_fps __user *fps;
870 unsigned long addr2 = caddr2;
871 unsigned long addr = caddr;
872 unsigned long data = cdata;
875 pregs = (struct pt_regs32 __user *) addr;
876 fps = (struct compat_fps __user *) addr;
880 ret = (addr != 0) ? -EIO : 0;
884 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
889 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
896 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
901 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
907 case PTRACE_GETFPREGS:
908 ret = copy_regset_to_user(child, view, REGSET_FP,
913 ret = copy_regset_to_user(child, view, REGSET_FP,
918 if (__put_user(0, &fps->flags) ||
919 __put_user(0, &fps->extra) ||
920 __put_user(0, &fps->fpqd) ||
921 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
926 case PTRACE_SETFPREGS:
927 ret = copy_regset_from_user(child, view, REGSET_FP,
932 ret = copy_regset_from_user(child, view, REGSET_FP,
938 case PTRACE_READTEXT:
939 case PTRACE_READDATA:
940 ret = ptrace_readdata(child, addr,
941 (char __user *)addr2, data);
948 case PTRACE_WRITETEXT:
949 case PTRACE_WRITEDATA:
950 ret = ptrace_writedata(child, (char __user *) addr2,
959 if (request == PTRACE_SPARC_DETACH)
960 request = PTRACE_DETACH;
961 ret = compat_ptrace_request(child, request, addr, data);
967 #endif /* CONFIG_COMPAT */
970 unsigned int regs[64];
974 long arch_ptrace(struct task_struct *child, long request,
975 unsigned long addr, unsigned long data)
977 const struct user_regset_view *view = task_user_regset_view(current);
978 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
979 struct pt_regs __user *pregs;
980 struct fps __user *fps;
984 pregs = (struct pt_regs __user *) addr;
985 fps = (struct fps __user *) addr;
986 addr2p = (void __user *) addr2;
990 ret = (addr != 0) ? -EIO : 0;
993 case PTRACE_GETREGS64:
994 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
999 /* XXX doesn't handle 'y' register correctly XXX */
1000 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
1007 case PTRACE_SETREGS64:
1008 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1013 /* XXX doesn't handle 'y' register correctly XXX */
1014 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1021 case PTRACE_GETFPREGS64:
1022 ret = copy_regset_to_user(child, view, REGSET_FP,
1028 case PTRACE_SETFPREGS64:
1029 ret = copy_regset_from_user(child, view, REGSET_FP,
1035 case PTRACE_READTEXT:
1036 case PTRACE_READDATA:
1037 ret = ptrace_readdata(child, addr, addr2p, data);
1044 case PTRACE_WRITETEXT:
1045 case PTRACE_WRITEDATA:
1046 ret = ptrace_writedata(child, addr2p, addr, data);
1054 if (request == PTRACE_SPARC_DETACH)
1055 request = PTRACE_DETACH;
1056 ret = ptrace_request(child, request, addr, data);
1063 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1067 /* do the secure computing check first */
1068 secure_computing_strict(regs->u_regs[UREG_G1]);
1070 if (test_thread_flag(TIF_NOHZ))
1073 if (test_thread_flag(TIF_SYSCALL_TRACE))
1074 ret = tracehook_report_syscall_entry(regs);
1076 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1077 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1079 audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
1080 regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
1081 regs->u_regs[UREG_I3]);
1086 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1088 if (test_thread_flag(TIF_NOHZ))
1091 audit_syscall_exit(regs);
1093 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1094 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1096 if (test_thread_flag(TIF_SYSCALL_TRACE))
1097 tracehook_report_syscall_exit(regs, 0);
1099 if (test_thread_flag(TIF_NOHZ))