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,
316 unsigned long y = regs->y;
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 (put_user(reg, u++))
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,
618 FOLL_FORCE | FOLL_WRITE)
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 (get_user(reg, u++))
644 if (access_process_vm(target,
648 FOLL_FORCE | FOLL_WRITE)
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_NOHZ))
1070 if (test_thread_flag(TIF_SYSCALL_TRACE))
1071 ret = tracehook_report_syscall_entry(regs);
1073 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1074 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1076 audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
1077 regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
1078 regs->u_regs[UREG_I3]);
1083 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1085 if (test_thread_flag(TIF_NOHZ))
1088 audit_syscall_exit(regs);
1090 if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1091 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1093 if (test_thread_flag(TIF_SYSCALL_TRACE))
1094 tracehook_report_syscall_exit(regs, 0);
1096 if (test_thread_flag(TIF_NOHZ))