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>
32 #include <asm/pgtable.h>
33 #include <asm/uaccess.h>
34 #include <asm/psrcompat.h>
35 #include <asm/visasm.h>
36 #include <asm/spitfire.h>
38 #include <asm/cpudata.h>
39 #include <asm/cacheflush.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/syscalls.h>
46 /* #define ALLOW_INIT_TRACING */
49 * Called by kernel/ptrace.c when detaching..
51 * Make sure single step bits etc are not set.
53 void ptrace_disable(struct task_struct *child)
58 /* To get the necessary page struct, access_process_vm() first calls
59 * get_user_pages(). This has done a flush_dcache_page() on the
60 * accessed page. Then our caller (copy_{to,from}_user_page()) did
61 * to memcpy to read/write the data from that page.
63 * Now, the only thing we have to do is:
64 * 1) flush the D-cache if it's possible than an illegal alias
66 * 2) flush the I-cache if this is pre-cheetah and we did a write
68 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
69 unsigned long uaddr, void *kaddr,
70 unsigned long len, int write)
72 BUG_ON(len > PAGE_SIZE);
74 if (tlb_type == hypervisor)
79 #ifdef DCACHE_ALIASING_POSSIBLE
80 /* If bit 13 of the kernel address we used to access the
81 * user page is the same as the virtual address that page
82 * is mapped to in the user's address space, we can skip the
85 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
86 unsigned long start = __pa(kaddr);
87 unsigned long end = start + len;
88 unsigned long dcache_line_size;
90 dcache_line_size = local_cpu_data().dcache_line_size;
92 if (tlb_type == spitfire) {
93 for (; start < end; start += dcache_line_size)
94 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
96 start &= ~(dcache_line_size - 1);
97 for (; start < end; start += dcache_line_size)
99 "stxa %%g0, [%0] %1\n\t"
103 "i" (ASI_DCACHE_INVALIDATE));
107 if (write && tlb_type == spitfire) {
108 unsigned long start = (unsigned long) kaddr;
109 unsigned long end = start + len;
110 unsigned long icache_line_size;
112 icache_line_size = local_cpu_data().icache_line_size;
114 for (; start < end; start += icache_line_size)
120 EXPORT_SYMBOL_GPL(flush_ptrace_access);
122 static int get_from_target(struct task_struct *target, unsigned long uaddr,
125 if (target == current) {
126 if (copy_from_user(kbuf, (void __user *) uaddr, len))
129 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
136 static int set_to_target(struct task_struct *target, unsigned long uaddr,
139 if (target == current) {
140 if (copy_to_user((void __user *) uaddr, kbuf, len))
143 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
150 static int regwindow64_get(struct task_struct *target,
151 const struct pt_regs *regs,
152 struct reg_window *wbuf)
154 unsigned long rw_addr = regs->u_regs[UREG_I6];
156 if (!test_thread_64bit_stack(rw_addr)) {
157 struct reg_window32 win32;
160 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
162 for (i = 0; i < 8; i++)
163 wbuf->locals[i] = win32.locals[i];
164 for (i = 0; i < 8; i++)
165 wbuf->ins[i] = win32.ins[i];
167 rw_addr += STACK_BIAS;
168 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
175 static int regwindow64_set(struct task_struct *target,
176 const struct pt_regs *regs,
177 struct reg_window *wbuf)
179 unsigned long rw_addr = regs->u_regs[UREG_I6];
181 if (!test_thread_64bit_stack(rw_addr)) {
182 struct reg_window32 win32;
185 for (i = 0; i < 8; i++)
186 win32.locals[i] = wbuf->locals[i];
187 for (i = 0; i < 8; i++)
188 win32.ins[i] = wbuf->ins[i];
190 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
193 rw_addr += STACK_BIAS;
194 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
206 static int genregs64_get(struct task_struct *target,
207 const struct user_regset *regset,
208 unsigned int pos, unsigned int count,
209 void *kbuf, void __user *ubuf)
211 const struct pt_regs *regs = task_pt_regs(target);
214 if (target == current)
217 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
219 0, 16 * sizeof(u64));
220 if (!ret && count && pos < (32 * sizeof(u64))) {
221 struct reg_window window;
223 if (regwindow64_get(target, regs, &window))
225 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
232 /* TSTATE, TPC, TNPC */
233 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
240 unsigned long y = regs->y;
242 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
249 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
250 36 * sizeof(u64), -1);
256 static int genregs64_set(struct task_struct *target,
257 const struct user_regset *regset,
258 unsigned int pos, unsigned int count,
259 const void *kbuf, const void __user *ubuf)
261 struct pt_regs *regs = task_pt_regs(target);
264 if (target == current)
267 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
269 0, 16 * sizeof(u64));
270 if (!ret && count && pos < (32 * sizeof(u64))) {
271 struct reg_window window;
273 if (regwindow64_get(target, regs, &window))
276 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
282 regwindow64_set(target, regs, &window))
286 if (!ret && count > 0) {
287 unsigned long tstate;
290 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
295 /* Only the condition codes and the "in syscall"
296 * state can be modified in the %tstate register.
298 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
299 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
300 regs->tstate |= tstate;
306 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
315 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
324 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
325 36 * sizeof(u64), -1);
330 static int fpregs64_get(struct task_struct *target,
331 const struct user_regset *regset,
332 unsigned int pos, unsigned int count,
333 void *kbuf, void __user *ubuf)
335 const unsigned long *fpregs = task_thread_info(target)->fpregs;
336 unsigned long fprs, fsr, gsr;
339 if (target == current)
340 save_and_clear_fpu();
342 fprs = task_thread_info(target)->fpsaved[0];
345 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
347 0, 16 * sizeof(u64));
349 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
355 ret = user_regset_copyout(&pos, &count,
361 ret = user_regset_copyout_zero(&pos, &count,
367 if (fprs & FPRS_FEF) {
368 fsr = task_thread_info(target)->xfsr[0];
369 gsr = task_thread_info(target)->gsr[0];
375 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
380 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
385 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
391 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
392 35 * sizeof(u64), -1);
397 static int fpregs64_set(struct task_struct *target,
398 const struct user_regset *regset,
399 unsigned int pos, unsigned int count,
400 const void *kbuf, const void __user *ubuf)
402 unsigned long *fpregs = task_thread_info(target)->fpregs;
406 if (target == current)
407 save_and_clear_fpu();
409 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
411 0, 32 * sizeof(u64));
413 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
414 task_thread_info(target)->xfsr,
418 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
419 task_thread_info(target)->gsr,
423 fprs = task_thread_info(target)->fpsaved[0];
424 if (!ret && count > 0) {
425 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
431 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
432 task_thread_info(target)->fpsaved[0] = fprs;
435 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
436 35 * sizeof(u64), -1);
440 static const struct user_regset sparc64_regsets[] = {
446 * TSTATE, TPC, TNPC, Y
449 .core_note_type = NT_PRSTATUS,
451 .size = sizeof(u64), .align = sizeof(u64),
452 .get = genregs64_get, .set = genregs64_set
461 .core_note_type = NT_PRFPREG,
463 .size = sizeof(u64), .align = sizeof(u64),
464 .get = fpregs64_get, .set = fpregs64_set
468 static const struct user_regset_view user_sparc64_view = {
469 .name = "sparc64", .e_machine = EM_SPARCV9,
470 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
474 static int genregs32_get(struct task_struct *target,
475 const struct user_regset *regset,
476 unsigned int pos, unsigned int count,
477 void *kbuf, void __user *ubuf)
479 const struct pt_regs *regs = task_pt_regs(target);
480 compat_ulong_t __user *reg_window;
481 compat_ulong_t *k = kbuf;
482 compat_ulong_t __user *u = ubuf;
485 if (target == current)
489 count /= sizeof(reg);
492 for (; count > 0 && pos < 16; count--)
493 *k++ = regs->u_regs[pos++];
495 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
497 if (target == current) {
498 for (; count > 0 && pos < 32; count--) {
499 if (get_user(*k++, ®_window[pos++]))
503 for (; count > 0 && pos < 32; count--) {
504 if (access_process_vm(target,
515 for (; count > 0 && pos < 16; count--) {
516 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
520 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
522 if (target == current) {
523 for (; count > 0 && pos < 32; count--) {
524 if (get_user(reg, ®_window[pos++]) ||
529 for (; count > 0 && pos < 32; count--) {
530 if (access_process_vm(target,
533 ®, sizeof(reg), 0)
536 if (access_process_vm(target,
538 ®, sizeof(reg), 1)
549 reg = tstate_to_psr(regs->tstate);
570 else if (put_user(reg, u++))
577 count *= sizeof(reg);
579 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
580 38 * sizeof(reg), -1);
583 static int genregs32_set(struct task_struct *target,
584 const struct user_regset *regset,
585 unsigned int pos, unsigned int count,
586 const void *kbuf, const void __user *ubuf)
588 struct pt_regs *regs = task_pt_regs(target);
589 compat_ulong_t __user *reg_window;
590 const compat_ulong_t *k = kbuf;
591 const compat_ulong_t __user *u = ubuf;
594 if (target == current)
598 count /= sizeof(reg);
601 for (; count > 0 && pos < 16; count--)
602 regs->u_regs[pos++] = *k++;
604 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
606 if (target == current) {
607 for (; count > 0 && pos < 32; count--) {
608 if (put_user(*k++, ®_window[pos++]))
612 for (; count > 0 && pos < 32; count--) {
613 if (access_process_vm(target,
625 for (; count > 0 && pos < 16; count--) {
626 if (get_user(reg, u++))
628 regs->u_regs[pos++] = reg;
631 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
633 if (target == current) {
634 for (; count > 0 && pos < 32; count--) {
635 if (get_user(reg, u++) ||
636 put_user(reg, ®_window[pos++]))
640 for (; count > 0 && pos < 32; count--) {
641 if (access_process_vm(target,
644 ®, sizeof(reg), 0)
647 if (access_process_vm(target,
650 ®, sizeof(reg), 1)
659 unsigned long tstate;
663 else if (get_user(reg, u++))
668 tstate = regs->tstate;
669 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
670 tstate |= psr_to_tstate_icc(reg);
671 if (reg & PSR_SYSCALL)
672 tstate |= TSTATE_SYSCALL;
673 regs->tstate = tstate;
696 count *= sizeof(reg);
698 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
699 38 * sizeof(reg), -1);
702 static int fpregs32_get(struct task_struct *target,
703 const struct user_regset *regset,
704 unsigned int pos, unsigned int count,
705 void *kbuf, void __user *ubuf)
707 const unsigned long *fpregs = task_thread_info(target)->fpregs;
708 compat_ulong_t enabled;
713 if (target == current)
714 save_and_clear_fpu();
716 fprs = task_thread_info(target)->fpsaved[0];
717 if (fprs & FPRS_FEF) {
718 fsr = task_thread_info(target)->xfsr[0];
725 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
727 0, 32 * sizeof(u32));
730 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
734 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
742 val = (enabled << 8) | (8 << 16);
743 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
750 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
751 35 * sizeof(u32), -1);
756 static int fpregs32_set(struct task_struct *target,
757 const struct user_regset *regset,
758 unsigned int pos, unsigned int count,
759 const void *kbuf, const void __user *ubuf)
761 unsigned long *fpregs = task_thread_info(target)->fpregs;
765 if (target == current)
766 save_and_clear_fpu();
768 fprs = task_thread_info(target)->fpsaved[0];
770 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
772 0, 32 * sizeof(u32));
774 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
777 if (!ret && count > 0) {
781 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
786 val = task_thread_info(target)->xfsr[0];
787 val &= 0xffffffff00000000UL;
789 task_thread_info(target)->xfsr[0] = val;
793 fprs |= (FPRS_FEF | FPRS_DL);
794 task_thread_info(target)->fpsaved[0] = fprs;
797 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
798 34 * sizeof(u32), -1);
802 static const struct user_regset sparc32_regsets[] = {
808 * PSR, PC, nPC, Y, WIM, TBR
811 .core_note_type = NT_PRSTATUS,
813 .size = sizeof(u32), .align = sizeof(u32),
814 .get = genregs32_get, .set = genregs32_set
820 * FPU QUEUE COUNT (8-bit char)
821 * FPU QUEUE ENTRYSIZE (8-bit char)
822 * FPU ENABLED (8-bit char)
824 * FPU QUEUE (64 32-bit ints)
827 .core_note_type = NT_PRFPREG,
829 .size = sizeof(u32), .align = sizeof(u32),
830 .get = fpregs32_get, .set = fpregs32_set
834 static const struct user_regset_view user_sparc32_view = {
835 .name = "sparc", .e_machine = EM_SPARC,
836 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
838 #endif /* CONFIG_COMPAT */
840 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
843 if (test_tsk_thread_flag(task, TIF_32BIT))
844 return &user_sparc32_view;
846 return &user_sparc64_view;
851 unsigned int regs[32];
857 unsigned int insnaddr;
862 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
863 compat_ulong_t caddr, compat_ulong_t cdata)
865 const struct user_regset_view *view = task_user_regset_view(current);
866 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
867 struct pt_regs32 __user *pregs;
868 struct compat_fps __user *fps;
869 unsigned long addr2 = caddr2;
870 unsigned long addr = caddr;
871 unsigned long data = cdata;
874 pregs = (struct pt_regs32 __user *) addr;
875 fps = (struct compat_fps __user *) addr;
879 ret = (addr != 0) ? -EIO : 0;
883 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
888 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
895 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
900 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
906 case PTRACE_GETFPREGS:
907 ret = copy_regset_to_user(child, view, REGSET_FP,
912 ret = copy_regset_to_user(child, view, REGSET_FP,
917 if (__put_user(0, &fps->flags) ||
918 __put_user(0, &fps->extra) ||
919 __put_user(0, &fps->fpqd) ||
920 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
925 case PTRACE_SETFPREGS:
926 ret = copy_regset_from_user(child, view, REGSET_FP,
931 ret = copy_regset_from_user(child, view, REGSET_FP,
937 case PTRACE_READTEXT:
938 case PTRACE_READDATA:
939 ret = ptrace_readdata(child, addr,
940 (char __user *)addr2, data);
947 case PTRACE_WRITETEXT:
948 case PTRACE_WRITEDATA:
949 ret = ptrace_writedata(child, (char __user *) addr2,
958 if (request == PTRACE_SPARC_DETACH)
959 request = PTRACE_DETACH;
960 ret = compat_ptrace_request(child, request, addr, data);
966 #endif /* CONFIG_COMPAT */
969 unsigned int regs[64];
973 long arch_ptrace(struct task_struct *child, long request,
974 unsigned long addr, unsigned long data)
976 const struct user_regset_view *view = task_user_regset_view(current);
977 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
978 struct pt_regs __user *pregs;
979 struct fps __user *fps;
983 pregs = (struct pt_regs __user *) addr;
984 fps = (struct fps __user *) addr;
985 addr2p = (void __user *) addr2;
989 ret = (addr != 0) ? -EIO : 0;
992 case PTRACE_GETREGS64:
993 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
998 /* XXX doesn't handle 'y' register correctly XXX */
999 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
1006 case PTRACE_SETREGS64:
1007 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1012 /* XXX doesn't handle 'y' register correctly XXX */
1013 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1020 case PTRACE_GETFPREGS64:
1021 ret = copy_regset_to_user(child, view, REGSET_FP,
1027 case PTRACE_SETFPREGS64:
1028 ret = copy_regset_from_user(child, view, REGSET_FP,
1034 case PTRACE_READTEXT:
1035 case PTRACE_READDATA:
1036 ret = ptrace_readdata(child, addr, addr2p, data);
1043 case PTRACE_WRITETEXT:
1044 case PTRACE_WRITEDATA:
1045 ret = ptrace_writedata(child, addr2p, addr, data);
1053 if (request == PTRACE_SPARC_DETACH)
1054 request = PTRACE_DETACH;
1055 ret = ptrace_request(child, request, addr, data);
1062 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1066 /* do the secure computing check first */
1067 secure_computing_strict(regs->u_regs[UREG_G1]);
1069 if (test_thread_flag(TIF_SYSCALL_TRACE))
1070 ret = tracehook_report_syscall_entry(regs);
1072 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1073 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1075 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1077 AUDIT_ARCH_SPARC64),
1078 regs->u_regs[UREG_G1],
1079 regs->u_regs[UREG_I0],
1080 regs->u_regs[UREG_I1],
1081 regs->u_regs[UREG_I2],
1082 regs->u_regs[UREG_I3]);
1087 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1089 audit_syscall_exit(regs);
1091 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1092 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1094 if (test_thread_flag(TIF_SYSCALL_TRACE))
1095 tracehook_report_syscall_exit(regs, 0);