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/security.h>
21 #include <linux/seccomp.h>
22 #include <linux/audit.h>
23 #include <linux/signal.h>
24 #include <linux/regset.h>
25 #include <linux/tracehook.h>
26 #include <trace/syscall.h>
27 #include <linux/compat.h>
28 #include <linux/elf.h>
31 #include <asm/pgtable.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>
40 #define CREATE_TRACE_POINTS
41 #include <trace/events/syscalls.h>
45 /* #define ALLOW_INIT_TRACING */
48 * Called by kernel/ptrace.c when detaching..
50 * Make sure single step bits etc are not set.
52 void ptrace_disable(struct task_struct *child)
57 /* To get the necessary page struct, access_process_vm() first calls
58 * get_user_pages(). This has done a flush_dcache_page() on the
59 * accessed page. Then our caller (copy_{to,from}_user_page()) did
60 * to memcpy to read/write the data from that page.
62 * Now, the only thing we have to do is:
63 * 1) flush the D-cache if it's possible than an illegal alias
65 * 2) flush the I-cache if this is pre-cheetah and we did a write
67 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
68 unsigned long uaddr, void *kaddr,
69 unsigned long len, int write)
71 BUG_ON(len > PAGE_SIZE);
73 if (tlb_type == hypervisor)
78 #ifdef DCACHE_ALIASING_POSSIBLE
79 /* If bit 13 of the kernel address we used to access the
80 * user page is the same as the virtual address that page
81 * is mapped to in the user's address space, we can skip the
84 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
85 unsigned long start = __pa(kaddr);
86 unsigned long end = start + len;
87 unsigned long dcache_line_size;
89 dcache_line_size = local_cpu_data().dcache_line_size;
91 if (tlb_type == spitfire) {
92 for (; start < end; start += dcache_line_size)
93 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
95 start &= ~(dcache_line_size - 1);
96 for (; start < end; start += dcache_line_size)
98 "stxa %%g0, [%0] %1\n\t"
102 "i" (ASI_DCACHE_INVALIDATE));
106 if (write && tlb_type == spitfire) {
107 unsigned long start = (unsigned long) kaddr;
108 unsigned long end = start + len;
109 unsigned long icache_line_size;
111 icache_line_size = local_cpu_data().icache_line_size;
113 for (; start < end; start += icache_line_size)
120 static int get_from_target(struct task_struct *target, unsigned long uaddr,
123 if (target == current) {
124 if (copy_from_user(kbuf, (void __user *) uaddr, len))
127 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
134 static int set_to_target(struct task_struct *target, unsigned long uaddr,
137 if (target == current) {
138 if (copy_to_user((void __user *) uaddr, kbuf, len))
141 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
148 static int regwindow64_get(struct task_struct *target,
149 const struct pt_regs *regs,
150 struct reg_window *wbuf)
152 unsigned long rw_addr = regs->u_regs[UREG_I6];
154 if (!test_thread_64bit_stack(rw_addr)) {
155 struct reg_window32 win32;
158 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
160 for (i = 0; i < 8; i++)
161 wbuf->locals[i] = win32.locals[i];
162 for (i = 0; i < 8; i++)
163 wbuf->ins[i] = win32.ins[i];
165 rw_addr += STACK_BIAS;
166 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
173 static int regwindow64_set(struct task_struct *target,
174 const struct pt_regs *regs,
175 struct reg_window *wbuf)
177 unsigned long rw_addr = regs->u_regs[UREG_I6];
179 if (!test_thread_64bit_stack(rw_addr)) {
180 struct reg_window32 win32;
183 for (i = 0; i < 8; i++)
184 win32.locals[i] = wbuf->locals[i];
185 for (i = 0; i < 8; i++)
186 win32.ins[i] = wbuf->ins[i];
188 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
191 rw_addr += STACK_BIAS;
192 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
204 static int genregs64_get(struct task_struct *target,
205 const struct user_regset *regset,
206 unsigned int pos, unsigned int count,
207 void *kbuf, void __user *ubuf)
209 const struct pt_regs *regs = task_pt_regs(target);
212 if (target == current)
215 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
217 0, 16 * sizeof(u64));
218 if (!ret && count && pos < (32 * sizeof(u64))) {
219 struct reg_window window;
221 if (regwindow64_get(target, regs, &window))
223 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
230 /* TSTATE, TPC, TNPC */
231 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
238 unsigned long y = regs->y;
240 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
247 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
248 36 * sizeof(u64), -1);
254 static int genregs64_set(struct task_struct *target,
255 const struct user_regset *regset,
256 unsigned int pos, unsigned int count,
257 const void *kbuf, const void __user *ubuf)
259 struct pt_regs *regs = task_pt_regs(target);
262 if (target == current)
265 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
267 0, 16 * sizeof(u64));
268 if (!ret && count && pos < (32 * sizeof(u64))) {
269 struct reg_window window;
271 if (regwindow64_get(target, regs, &window))
274 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
280 regwindow64_set(target, regs, &window))
284 if (!ret && count > 0) {
285 unsigned long tstate;
288 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
293 /* Only the condition codes and the "in syscall"
294 * state can be modified in the %tstate register.
296 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
297 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
298 regs->tstate |= tstate;
304 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
313 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
322 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
323 36 * sizeof(u64), -1);
328 static int fpregs64_get(struct task_struct *target,
329 const struct user_regset *regset,
330 unsigned int pos, unsigned int count,
331 void *kbuf, void __user *ubuf)
333 const unsigned long *fpregs = task_thread_info(target)->fpregs;
334 unsigned long fprs, fsr, gsr;
337 if (target == current)
338 save_and_clear_fpu();
340 fprs = task_thread_info(target)->fpsaved[0];
343 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
345 0, 16 * sizeof(u64));
347 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
353 ret = user_regset_copyout(&pos, &count,
359 ret = user_regset_copyout_zero(&pos, &count,
365 if (fprs & FPRS_FEF) {
366 fsr = task_thread_info(target)->xfsr[0];
367 gsr = task_thread_info(target)->gsr[0];
373 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
378 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
383 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
389 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
390 35 * sizeof(u64), -1);
395 static int fpregs64_set(struct task_struct *target,
396 const struct user_regset *regset,
397 unsigned int pos, unsigned int count,
398 const void *kbuf, const void __user *ubuf)
400 unsigned long *fpregs = task_thread_info(target)->fpregs;
404 if (target == current)
405 save_and_clear_fpu();
407 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
409 0, 32 * sizeof(u64));
411 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
412 task_thread_info(target)->xfsr,
416 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
417 task_thread_info(target)->gsr,
421 fprs = task_thread_info(target)->fpsaved[0];
422 if (!ret && count > 0) {
423 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
429 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
430 task_thread_info(target)->fpsaved[0] = fprs;
433 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
434 35 * sizeof(u64), -1);
438 static const struct user_regset sparc64_regsets[] = {
444 * TSTATE, TPC, TNPC, Y
447 .core_note_type = NT_PRSTATUS,
449 .size = sizeof(u64), .align = sizeof(u64),
450 .get = genregs64_get, .set = genregs64_set
459 .core_note_type = NT_PRFPREG,
461 .size = sizeof(u64), .align = sizeof(u64),
462 .get = fpregs64_get, .set = fpregs64_set
466 static const struct user_regset_view user_sparc64_view = {
467 .name = "sparc64", .e_machine = EM_SPARCV9,
468 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
472 static int genregs32_get(struct task_struct *target,
473 const struct user_regset *regset,
474 unsigned int pos, unsigned int count,
475 void *kbuf, void __user *ubuf)
477 const struct pt_regs *regs = task_pt_regs(target);
478 compat_ulong_t __user *reg_window;
479 compat_ulong_t *k = kbuf;
480 compat_ulong_t __user *u = ubuf;
483 if (target == current)
487 count /= sizeof(reg);
490 for (; count > 0 && pos < 16; count--)
491 *k++ = regs->u_regs[pos++];
493 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
495 if (target == current) {
496 for (; count > 0 && pos < 32; count--) {
497 if (get_user(*k++, ®_window[pos++]))
501 for (; count > 0 && pos < 32; count--) {
502 if (access_process_vm(target,
513 for (; count > 0 && pos < 16; count--) {
514 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
518 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
520 if (target == current) {
521 for (; count > 0 && pos < 32; count--) {
522 if (get_user(reg, ®_window[pos++]) ||
527 for (; count > 0 && pos < 32; count--) {
528 if (access_process_vm(target,
531 ®, sizeof(reg), 0)
534 if (access_process_vm(target,
536 ®, sizeof(reg), 1)
547 reg = tstate_to_psr(regs->tstate);
568 else if (put_user(reg, u++))
575 count *= sizeof(reg);
577 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
578 38 * sizeof(reg), -1);
581 static int genregs32_set(struct task_struct *target,
582 const struct user_regset *regset,
583 unsigned int pos, unsigned int count,
584 const void *kbuf, const void __user *ubuf)
586 struct pt_regs *regs = task_pt_regs(target);
587 compat_ulong_t __user *reg_window;
588 const compat_ulong_t *k = kbuf;
589 const compat_ulong_t __user *u = ubuf;
592 if (target == current)
596 count /= sizeof(reg);
599 for (; count > 0 && pos < 16; count--)
600 regs->u_regs[pos++] = *k++;
602 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
604 if (target == current) {
605 for (; count > 0 && pos < 32; count--) {
606 if (put_user(*k++, ®_window[pos++]))
610 for (; count > 0 && pos < 32; count--) {
611 if (access_process_vm(target,
623 for (; count > 0 && pos < 16; count--) {
624 if (get_user(reg, u++))
626 regs->u_regs[pos++] = reg;
629 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
631 if (target == current) {
632 for (; count > 0 && pos < 32; count--) {
633 if (get_user(reg, u++) ||
634 put_user(reg, ®_window[pos++]))
638 for (; count > 0 && pos < 32; count--) {
639 if (access_process_vm(target,
642 ®, sizeof(reg), 0)
645 if (access_process_vm(target,
648 ®, sizeof(reg), 1)
657 unsigned long tstate;
661 else if (get_user(reg, u++))
666 tstate = regs->tstate;
667 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
668 tstate |= psr_to_tstate_icc(reg);
669 if (reg & PSR_SYSCALL)
670 tstate |= TSTATE_SYSCALL;
671 regs->tstate = tstate;
694 count *= sizeof(reg);
696 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
697 38 * sizeof(reg), -1);
700 static int fpregs32_get(struct task_struct *target,
701 const struct user_regset *regset,
702 unsigned int pos, unsigned int count,
703 void *kbuf, void __user *ubuf)
705 const unsigned long *fpregs = task_thread_info(target)->fpregs;
706 compat_ulong_t enabled;
711 if (target == current)
712 save_and_clear_fpu();
714 fprs = task_thread_info(target)->fpsaved[0];
715 if (fprs & FPRS_FEF) {
716 fsr = task_thread_info(target)->xfsr[0];
723 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
725 0, 32 * sizeof(u32));
728 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
732 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
740 val = (enabled << 8) | (8 << 16);
741 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
748 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
749 35 * sizeof(u32), -1);
754 static int fpregs32_set(struct task_struct *target,
755 const struct user_regset *regset,
756 unsigned int pos, unsigned int count,
757 const void *kbuf, const void __user *ubuf)
759 unsigned long *fpregs = task_thread_info(target)->fpregs;
763 if (target == current)
764 save_and_clear_fpu();
766 fprs = task_thread_info(target)->fpsaved[0];
768 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
770 0, 32 * sizeof(u32));
772 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
775 if (!ret && count > 0) {
779 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
784 val = task_thread_info(target)->xfsr[0];
785 val &= 0xffffffff00000000UL;
787 task_thread_info(target)->xfsr[0] = val;
791 fprs |= (FPRS_FEF | FPRS_DL);
792 task_thread_info(target)->fpsaved[0] = fprs;
795 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
796 34 * sizeof(u32), -1);
800 static const struct user_regset sparc32_regsets[] = {
806 * PSR, PC, nPC, Y, WIM, TBR
809 .core_note_type = NT_PRSTATUS,
811 .size = sizeof(u32), .align = sizeof(u32),
812 .get = genregs32_get, .set = genregs32_set
818 * FPU QUEUE COUNT (8-bit char)
819 * FPU QUEUE ENTRYSIZE (8-bit char)
820 * FPU ENABLED (8-bit char)
822 * FPU QUEUE (64 32-bit ints)
825 .core_note_type = NT_PRFPREG,
827 .size = sizeof(u32), .align = sizeof(u32),
828 .get = fpregs32_get, .set = fpregs32_set
832 static const struct user_regset_view user_sparc32_view = {
833 .name = "sparc", .e_machine = EM_SPARC,
834 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
836 #endif /* CONFIG_COMPAT */
838 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
841 if (test_tsk_thread_flag(task, TIF_32BIT))
842 return &user_sparc32_view;
844 return &user_sparc64_view;
849 unsigned int regs[32];
855 unsigned int insnaddr;
860 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
861 compat_ulong_t caddr, compat_ulong_t cdata)
863 const struct user_regset_view *view = task_user_regset_view(current);
864 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
865 struct pt_regs32 __user *pregs;
866 struct compat_fps __user *fps;
867 unsigned long addr2 = caddr2;
868 unsigned long addr = caddr;
869 unsigned long data = cdata;
872 pregs = (struct pt_regs32 __user *) addr;
873 fps = (struct compat_fps __user *) addr;
877 ret = (addr != 0) ? -EIO : 0;
881 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
886 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
893 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
898 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
904 case PTRACE_GETFPREGS:
905 ret = copy_regset_to_user(child, view, REGSET_FP,
910 ret = copy_regset_to_user(child, view, REGSET_FP,
915 if (__put_user(0, &fps->flags) ||
916 __put_user(0, &fps->extra) ||
917 __put_user(0, &fps->fpqd) ||
918 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
923 case PTRACE_SETFPREGS:
924 ret = copy_regset_from_user(child, view, REGSET_FP,
929 ret = copy_regset_from_user(child, view, REGSET_FP,
935 case PTRACE_READTEXT:
936 case PTRACE_READDATA:
937 ret = ptrace_readdata(child, addr,
938 (char __user *)addr2, data);
945 case PTRACE_WRITETEXT:
946 case PTRACE_WRITEDATA:
947 ret = ptrace_writedata(child, (char __user *) addr2,
956 if (request == PTRACE_SPARC_DETACH)
957 request = PTRACE_DETACH;
958 ret = compat_ptrace_request(child, request, addr, data);
964 #endif /* CONFIG_COMPAT */
967 unsigned int regs[64];
971 long arch_ptrace(struct task_struct *child, long request,
972 unsigned long addr, unsigned long data)
974 const struct user_regset_view *view = task_user_regset_view(current);
975 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
976 struct pt_regs __user *pregs;
977 struct fps __user *fps;
981 pregs = (struct pt_regs __user *) addr;
982 fps = (struct fps __user *) addr;
983 addr2p = (void __user *) addr2;
987 ret = (addr != 0) ? -EIO : 0;
990 case PTRACE_GETREGS64:
991 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
996 /* XXX doesn't handle 'y' register correctly XXX */
997 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
1004 case PTRACE_SETREGS64:
1005 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1010 /* XXX doesn't handle 'y' register correctly XXX */
1011 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1018 case PTRACE_GETFPREGS64:
1019 ret = copy_regset_to_user(child, view, REGSET_FP,
1025 case PTRACE_SETFPREGS64:
1026 ret = copy_regset_from_user(child, view, REGSET_FP,
1032 case PTRACE_READTEXT:
1033 case PTRACE_READDATA:
1034 ret = ptrace_readdata(child, addr, addr2p, data);
1041 case PTRACE_WRITETEXT:
1042 case PTRACE_WRITEDATA:
1043 ret = ptrace_writedata(child, addr2p, addr, data);
1051 if (request == PTRACE_SPARC_DETACH)
1052 request = PTRACE_DETACH;
1053 ret = ptrace_request(child, request, addr, data);
1060 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1064 /* do the secure computing check first */
1065 secure_computing_strict(regs->u_regs[UREG_G1]);
1067 if (test_thread_flag(TIF_SYSCALL_TRACE))
1068 ret = tracehook_report_syscall_entry(regs);
1070 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1071 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1073 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1075 AUDIT_ARCH_SPARC64),
1076 regs->u_regs[UREG_G1],
1077 regs->u_regs[UREG_I0],
1078 regs->u_regs[UREG_I1],
1079 regs->u_regs[UREG_I2],
1080 regs->u_regs[UREG_I3]);
1085 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1087 audit_syscall_exit(regs);
1089 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1090 trace_sys_exit(regs, regs->u_regs[UREG_G1]);
1092 if (test_thread_flag(TIF_SYSCALL_TRACE))
1093 tracehook_report_syscall_exit(regs, 0);