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,
138 static int set_to_target(struct task_struct *target, unsigned long uaddr,
141 if (target == current) {
142 if (copy_to_user((void __user *) uaddr, kbuf, len))
145 int len2 = access_process_vm(target, uaddr, kbuf, len,
146 FOLL_FORCE | FOLL_WRITE);
153 static int regwindow64_get(struct task_struct *target,
154 const struct pt_regs *regs,
155 struct reg_window *wbuf)
157 unsigned long rw_addr = regs->u_regs[UREG_I6];
159 if (!test_thread_64bit_stack(rw_addr)) {
160 struct reg_window32 win32;
163 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
165 for (i = 0; i < 8; i++)
166 wbuf->locals[i] = win32.locals[i];
167 for (i = 0; i < 8; i++)
168 wbuf->ins[i] = win32.ins[i];
170 rw_addr += STACK_BIAS;
171 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
178 static int regwindow64_set(struct task_struct *target,
179 const struct pt_regs *regs,
180 struct reg_window *wbuf)
182 unsigned long rw_addr = regs->u_regs[UREG_I6];
184 if (!test_thread_64bit_stack(rw_addr)) {
185 struct reg_window32 win32;
188 for (i = 0; i < 8; i++)
189 win32.locals[i] = wbuf->locals[i];
190 for (i = 0; i < 8; i++)
191 win32.ins[i] = wbuf->ins[i];
193 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
196 rw_addr += STACK_BIAS;
197 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
209 static int genregs64_get(struct task_struct *target,
210 const struct user_regset *regset,
211 unsigned int pos, unsigned int count,
212 void *kbuf, void __user *ubuf)
214 const struct pt_regs *regs = task_pt_regs(target);
217 if (target == current)
220 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
222 0, 16 * sizeof(u64));
223 if (!ret && count && pos < (32 * sizeof(u64))) {
224 struct reg_window window;
226 if (regwindow64_get(target, regs, &window))
228 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
235 /* TSTATE, TPC, TNPC */
236 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
243 unsigned long y = regs->y;
245 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
252 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
253 36 * sizeof(u64), -1);
259 static int genregs64_set(struct task_struct *target,
260 const struct user_regset *regset,
261 unsigned int pos, unsigned int count,
262 const void *kbuf, const void __user *ubuf)
264 struct pt_regs *regs = task_pt_regs(target);
267 if (target == current)
270 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
272 0, 16 * sizeof(u64));
273 if (!ret && count && pos < (32 * sizeof(u64))) {
274 struct reg_window window;
276 if (regwindow64_get(target, regs, &window))
279 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
285 regwindow64_set(target, regs, &window))
289 if (!ret && count > 0) {
290 unsigned long tstate;
293 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
298 /* Only the condition codes and the "in syscall"
299 * state can be modified in the %tstate register.
301 tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
302 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
303 regs->tstate |= tstate;
309 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
318 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
327 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
328 36 * sizeof(u64), -1);
333 static int fpregs64_get(struct task_struct *target,
334 const struct user_regset *regset,
335 unsigned int pos, unsigned int count,
336 void *kbuf, void __user *ubuf)
338 const unsigned long *fpregs = task_thread_info(target)->fpregs;
339 unsigned long fprs, fsr, gsr;
342 if (target == current)
343 save_and_clear_fpu();
345 fprs = task_thread_info(target)->fpsaved[0];
348 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
350 0, 16 * sizeof(u64));
352 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
358 ret = user_regset_copyout(&pos, &count,
364 ret = user_regset_copyout_zero(&pos, &count,
370 if (fprs & FPRS_FEF) {
371 fsr = task_thread_info(target)->xfsr[0];
372 gsr = task_thread_info(target)->gsr[0];
378 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
383 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
388 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
394 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
395 35 * sizeof(u64), -1);
400 static int fpregs64_set(struct task_struct *target,
401 const struct user_regset *regset,
402 unsigned int pos, unsigned int count,
403 const void *kbuf, const void __user *ubuf)
405 unsigned long *fpregs = task_thread_info(target)->fpregs;
409 if (target == current)
410 save_and_clear_fpu();
412 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
414 0, 32 * sizeof(u64));
416 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
417 task_thread_info(target)->xfsr,
421 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
422 task_thread_info(target)->gsr,
426 fprs = task_thread_info(target)->fpsaved[0];
427 if (!ret && count > 0) {
428 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
434 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
435 task_thread_info(target)->fpsaved[0] = fprs;
438 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
439 35 * sizeof(u64), -1);
443 static const struct user_regset sparc64_regsets[] = {
449 * TSTATE, TPC, TNPC, Y
452 .core_note_type = NT_PRSTATUS,
454 .size = sizeof(u64), .align = sizeof(u64),
455 .get = genregs64_get, .set = genregs64_set
464 .core_note_type = NT_PRFPREG,
466 .size = sizeof(u64), .align = sizeof(u64),
467 .get = fpregs64_get, .set = fpregs64_set
471 static const struct user_regset_view user_sparc64_view = {
472 .name = "sparc64", .e_machine = EM_SPARCV9,
473 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
477 static int genregs32_get(struct task_struct *target,
478 const struct user_regset *regset,
479 unsigned int pos, unsigned int count,
480 void *kbuf, void __user *ubuf)
482 const struct pt_regs *regs = task_pt_regs(target);
483 compat_ulong_t __user *reg_window;
484 compat_ulong_t *k = kbuf;
485 compat_ulong_t __user *u = ubuf;
488 if (target == current)
492 count /= sizeof(reg);
495 for (; count > 0 && pos < 16; count--)
496 *k++ = regs->u_regs[pos++];
498 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
500 if (target == current) {
501 for (; count > 0 && pos < 32; count--) {
502 if (get_user(*k++, ®_window[pos++]))
506 for (; count > 0 && pos < 32; count--) {
507 if (access_process_vm(target,
519 for (; count > 0 && pos < 16; count--) {
520 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
524 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
526 if (target == current) {
527 for (; count > 0 && pos < 32; count--) {
528 if (get_user(reg, ®_window[pos++]) ||
533 for (; count > 0 && pos < 32; count--) {
534 if (access_process_vm(target,
541 if (access_process_vm(target,
544 FOLL_FORCE | FOLL_WRITE)
555 reg = tstate_to_psr(regs->tstate);
576 else if (put_user(reg, u++))
583 count *= sizeof(reg);
585 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
586 38 * sizeof(reg), -1);
589 static int genregs32_set(struct task_struct *target,
590 const struct user_regset *regset,
591 unsigned int pos, unsigned int count,
592 const void *kbuf, const void __user *ubuf)
594 struct pt_regs *regs = task_pt_regs(target);
595 compat_ulong_t __user *reg_window;
596 const compat_ulong_t *k = kbuf;
597 const compat_ulong_t __user *u = ubuf;
600 if (target == current)
604 count /= sizeof(reg);
607 for (; count > 0 && pos < 16; count--)
608 regs->u_regs[pos++] = *k++;
610 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
612 if (target == current) {
613 for (; count > 0 && pos < 32; count--) {
614 if (put_user(*k++, ®_window[pos++]))
618 for (; count > 0 && pos < 32; count--) {
619 if (access_process_vm(target,
624 FOLL_FORCE | FOLL_WRITE)
632 for (; count > 0 && pos < 16; count--) {
633 if (get_user(reg, u++))
635 regs->u_regs[pos++] = reg;
638 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
640 if (target == current) {
641 for (; count > 0 && pos < 32; count--) {
642 if (get_user(reg, u++) ||
643 put_user(reg, ®_window[pos++]))
647 for (; count > 0 && pos < 32; count--) {
648 if (access_process_vm(target,
655 if (access_process_vm(target,
659 FOLL_FORCE | FOLL_WRITE)
668 unsigned long tstate;
672 else if (get_user(reg, u++))
677 tstate = regs->tstate;
678 tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
679 tstate |= psr_to_tstate_icc(reg);
680 if (reg & PSR_SYSCALL)
681 tstate |= TSTATE_SYSCALL;
682 regs->tstate = tstate;
705 count *= sizeof(reg);
707 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
708 38 * sizeof(reg), -1);
711 static int fpregs32_get(struct task_struct *target,
712 const struct user_regset *regset,
713 unsigned int pos, unsigned int count,
714 void *kbuf, void __user *ubuf)
716 const unsigned long *fpregs = task_thread_info(target)->fpregs;
717 compat_ulong_t enabled;
722 if (target == current)
723 save_and_clear_fpu();
725 fprs = task_thread_info(target)->fpsaved[0];
726 if (fprs & FPRS_FEF) {
727 fsr = task_thread_info(target)->xfsr[0];
734 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
736 0, 32 * sizeof(u32));
739 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
743 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
751 val = (enabled << 8) | (8 << 16);
752 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
759 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
760 35 * sizeof(u32), -1);
765 static int fpregs32_set(struct task_struct *target,
766 const struct user_regset *regset,
767 unsigned int pos, unsigned int count,
768 const void *kbuf, const void __user *ubuf)
770 unsigned long *fpregs = task_thread_info(target)->fpregs;
774 if (target == current)
775 save_and_clear_fpu();
777 fprs = task_thread_info(target)->fpsaved[0];
779 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
781 0, 32 * sizeof(u32));
783 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
786 if (!ret && count > 0) {
790 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
795 val = task_thread_info(target)->xfsr[0];
796 val &= 0xffffffff00000000UL;
798 task_thread_info(target)->xfsr[0] = val;
802 fprs |= (FPRS_FEF | FPRS_DL);
803 task_thread_info(target)->fpsaved[0] = fprs;
806 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
807 34 * sizeof(u32), -1);
811 static const struct user_regset sparc32_regsets[] = {
817 * PSR, PC, nPC, Y, WIM, TBR
820 .core_note_type = NT_PRSTATUS,
822 .size = sizeof(u32), .align = sizeof(u32),
823 .get = genregs32_get, .set = genregs32_set
829 * FPU QUEUE COUNT (8-bit char)
830 * FPU QUEUE ENTRYSIZE (8-bit char)
831 * FPU ENABLED (8-bit char)
833 * FPU QUEUE (64 32-bit ints)
836 .core_note_type = NT_PRFPREG,
838 .size = sizeof(u32), .align = sizeof(u32),
839 .get = fpregs32_get, .set = fpregs32_set
843 static const struct user_regset_view user_sparc32_view = {
844 .name = "sparc", .e_machine = EM_SPARC,
845 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
847 #endif /* CONFIG_COMPAT */
849 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
852 if (test_tsk_thread_flag(task, TIF_32BIT))
853 return &user_sparc32_view;
855 return &user_sparc64_view;
860 unsigned int regs[32];
866 unsigned int insnaddr;
871 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
872 compat_ulong_t caddr, compat_ulong_t cdata)
874 const struct user_regset_view *view = task_user_regset_view(current);
875 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
876 struct pt_regs32 __user *pregs;
877 struct compat_fps __user *fps;
878 unsigned long addr2 = caddr2;
879 unsigned long addr = caddr;
880 unsigned long data = cdata;
883 pregs = (struct pt_regs32 __user *) addr;
884 fps = (struct compat_fps __user *) addr;
888 ret = (addr != 0) ? -EIO : 0;
892 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
897 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
904 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
909 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
915 case PTRACE_GETFPREGS:
916 ret = copy_regset_to_user(child, view, REGSET_FP,
921 ret = copy_regset_to_user(child, view, REGSET_FP,
926 if (__put_user(0, &fps->flags) ||
927 __put_user(0, &fps->extra) ||
928 __put_user(0, &fps->fpqd) ||
929 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
934 case PTRACE_SETFPREGS:
935 ret = copy_regset_from_user(child, view, REGSET_FP,
940 ret = copy_regset_from_user(child, view, REGSET_FP,
946 case PTRACE_READTEXT:
947 case PTRACE_READDATA:
948 ret = ptrace_readdata(child, addr,
949 (char __user *)addr2, data);
956 case PTRACE_WRITETEXT:
957 case PTRACE_WRITEDATA:
958 ret = ptrace_writedata(child, (char __user *) addr2,
967 if (request == PTRACE_SPARC_DETACH)
968 request = PTRACE_DETACH;
969 ret = compat_ptrace_request(child, request, addr, data);
975 #endif /* CONFIG_COMPAT */
978 unsigned int regs[64];
982 long arch_ptrace(struct task_struct *child, long request,
983 unsigned long addr, unsigned long data)
985 const struct user_regset_view *view = task_user_regset_view(current);
986 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
987 struct pt_regs __user *pregs;
988 struct fps __user *fps;
992 pregs = (struct pt_regs __user *) addr;
993 fps = (struct fps __user *) addr;
994 addr2p = (void __user *) addr2;
998 ret = (addr != 0) ? -EIO : 0;
1001 case PTRACE_GETREGS64:
1002 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
1007 /* XXX doesn't handle 'y' register correctly XXX */
1008 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
1015 case PTRACE_SETREGS64:
1016 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1021 /* XXX doesn't handle 'y' register correctly XXX */
1022 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1029 case PTRACE_GETFPREGS64:
1030 ret = copy_regset_to_user(child, view, REGSET_FP,
1036 case PTRACE_SETFPREGS64:
1037 ret = copy_regset_from_user(child, view, REGSET_FP,
1043 case PTRACE_READTEXT:
1044 case PTRACE_READDATA:
1045 ret = ptrace_readdata(child, addr, addr2p, data);
1052 case PTRACE_WRITETEXT:
1053 case PTRACE_WRITEDATA:
1054 ret = ptrace_writedata(child, addr2p, addr, data);
1062 if (request == PTRACE_SPARC_DETACH)
1063 request = PTRACE_DETACH;
1064 ret = ptrace_request(child, request, addr, data);
1071 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1075 /* do the secure computing check first */
1076 secure_computing_strict(regs->u_regs[UREG_G1]);
1078 if (test_thread_flag(TIF_NOHZ))
1081 if (test_thread_flag(TIF_SYSCALL_TRACE))
1082 ret = tracehook_report_syscall_entry(regs);
1084 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1085 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1087 audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
1088 regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
1089 regs->u_regs[UREG_I3]);
1094 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1096 if (test_thread_flag(TIF_NOHZ))
1099 audit_syscall_exit(regs);
1101 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1102 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1104 if (test_thread_flag(TIF_SYSCALL_TRACE))
1105 tracehook_report_syscall_exit(regs, 0);
1107 if (test_thread_flag(TIF_NOHZ))