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/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,
314 unsigned long y = regs->y;
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 (put_user(reg, u++))
545 reg = tstate_to_psr(regs->tstate);
566 else if (put_user(reg, u++))
573 count *= sizeof(reg);
575 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
576 38 * sizeof(reg), -1);
579 static int genregs32_set(struct task_struct *target,
580 const struct user_regset *regset,
581 unsigned int pos, unsigned int count,
582 const void *kbuf, const void __user *ubuf)
584 struct pt_regs *regs = task_pt_regs(target);
585 compat_ulong_t __user *reg_window;
586 const compat_ulong_t *k = kbuf;
587 const compat_ulong_t __user *u = ubuf;
590 if (target == current)
594 count /= sizeof(reg);
597 for (; count > 0 && pos < 16; count--)
598 regs->u_regs[pos++] = *k++;
600 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
602 if (target == current) {
603 for (; count > 0 && pos < 32; count--) {
604 if (put_user(*k++, ®_window[pos++]))
608 for (; count > 0 && pos < 32; count--) {
609 if (access_process_vm(target,
621 for (; count > 0 && pos < 16; count--) {
622 if (get_user(reg, u++))
624 regs->u_regs[pos++] = reg;
627 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
629 if (target == current) {
630 for (; count > 0 && pos < 32; count--) {
631 if (get_user(reg, u++) ||
632 put_user(reg, ®_window[pos++]))
636 for (; count > 0 && pos < 32; count--) {
637 if (get_user(reg, u++))
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,
966 unsigned long addr, unsigned long data)
968 const struct user_regset_view *view = task_user_regset_view(current);
969 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
970 struct pt_regs __user *pregs;
971 struct fps __user *fps;
975 pregs = (struct pt_regs __user *) addr;
976 fps = (struct fps __user *) addr;
977 addr2p = (void __user *) addr2;
981 ret = (addr != 0) ? -EIO : 0;
984 case PTRACE_GETREGS64:
985 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
990 /* XXX doesn't handle 'y' register correctly XXX */
991 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
998 case PTRACE_SETREGS64:
999 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1004 /* XXX doesn't handle 'y' register correctly XXX */
1005 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1012 case PTRACE_GETFPREGS64:
1013 ret = copy_regset_to_user(child, view, REGSET_FP,
1019 case PTRACE_SETFPREGS64:
1020 ret = copy_regset_from_user(child, view, REGSET_FP,
1026 case PTRACE_READTEXT:
1027 case PTRACE_READDATA:
1028 ret = ptrace_readdata(child, addr, addr2p, data);
1035 case PTRACE_WRITETEXT:
1036 case PTRACE_WRITEDATA:
1037 ret = ptrace_writedata(child, addr2p, addr, data);
1045 if (request == PTRACE_SPARC_DETACH)
1046 request = PTRACE_DETACH;
1047 ret = ptrace_request(child, request, addr, data);
1054 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1058 /* do the secure computing check first */
1059 secure_computing_strict(regs->u_regs[UREG_G1]);
1061 if (test_thread_flag(TIF_NOHZ))
1064 if (test_thread_flag(TIF_SYSCALL_TRACE))
1065 ret = tracehook_report_syscall_entry(regs);
1067 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1068 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1070 audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
1071 regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
1072 regs->u_regs[UREG_I3]);
1077 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1079 if (test_thread_flag(TIF_NOHZ))
1082 audit_syscall_exit(regs);
1084 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1085 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1087 if (test_thread_flag(TIF_SYSCALL_TRACE))
1088 tracehook_report_syscall_exit(regs, 0);
1090 if (test_thread_flag(TIF_NOHZ))