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/smp_lock.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/compat.h>
27 #include <linux/elf.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.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>
42 /* #define ALLOW_INIT_TRACING */
45 * Called by kernel/ptrace.c when detaching..
47 * Make sure single step bits etc are not set.
49 void ptrace_disable(struct task_struct *child)
54 /* To get the necessary page struct, access_process_vm() first calls
55 * get_user_pages(). This has done a flush_dcache_page() on the
56 * accessed page. Then our caller (copy_{to,from}_user_page()) did
57 * to memcpy to read/write the data from that page.
59 * Now, the only thing we have to do is:
60 * 1) flush the D-cache if it's possible than an illegal alias
62 * 2) flush the I-cache if this is pre-cheetah and we did a write
64 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
65 unsigned long uaddr, void *kaddr,
66 unsigned long len, int write)
68 BUG_ON(len > PAGE_SIZE);
70 if (tlb_type == hypervisor)
75 #ifdef DCACHE_ALIASING_POSSIBLE
76 /* If bit 13 of the kernel address we used to access the
77 * user page is the same as the virtual address that page
78 * is mapped to in the user's address space, we can skip the
81 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
82 unsigned long start = __pa(kaddr);
83 unsigned long end = start + len;
84 unsigned long dcache_line_size;
86 dcache_line_size = local_cpu_data().dcache_line_size;
88 if (tlb_type == spitfire) {
89 for (; start < end; start += dcache_line_size)
90 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
92 start &= ~(dcache_line_size - 1);
93 for (; start < end; start += dcache_line_size)
95 "stxa %%g0, [%0] %1\n\t"
99 "i" (ASI_DCACHE_INVALIDATE));
103 if (write && tlb_type == spitfire) {
104 unsigned long start = (unsigned long) kaddr;
105 unsigned long end = start + len;
106 unsigned long icache_line_size;
108 icache_line_size = local_cpu_data().icache_line_size;
110 for (; start < end; start += icache_line_size)
122 static int genregs64_get(struct task_struct *target,
123 const struct user_regset *regset,
124 unsigned int pos, unsigned int count,
125 void *kbuf, void __user *ubuf)
127 const struct pt_regs *regs = task_pt_regs(target);
130 if (target == current)
133 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
135 0, 16 * sizeof(u64));
137 unsigned long __user *reg_window = (unsigned long __user *)
138 (regs->u_regs[UREG_I6] + STACK_BIAS);
139 unsigned long window[16];
141 if (target == current) {
142 if (copy_from_user(window, reg_window, sizeof(window)))
145 if (access_process_vm(target,
146 (unsigned long) reg_window,
148 sizeof(window), 0) !=
153 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
160 /* TSTATE, TPC, TNPC */
161 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
168 unsigned long y = regs->y;
170 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
177 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
178 36 * sizeof(u64), -1);
183 static int genregs64_set(struct task_struct *target,
184 const struct user_regset *regset,
185 unsigned int pos, unsigned int count,
186 const void *kbuf, const void __user *ubuf)
188 struct pt_regs *regs = task_pt_regs(target);
191 if (target == current)
194 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
196 0, 16 * sizeof(u64));
197 if (!ret && count > 0) {
198 unsigned long __user *reg_window = (unsigned long __user *)
199 (regs->u_regs[UREG_I6] + STACK_BIAS);
200 unsigned long window[16];
202 if (target == current) {
203 if (copy_from_user(window, reg_window, sizeof(window)))
206 if (access_process_vm(target,
207 (unsigned long) reg_window,
209 sizeof(window), 0) !=
214 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
219 if (target == current) {
220 if (copy_to_user(reg_window, window,
224 if (access_process_vm(target,
228 sizeof(window), 1) !=
235 if (!ret && count > 0) {
236 unsigned long tstate;
239 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
244 /* Only the condition codes can be modified
245 * in the %tstate register.
247 tstate &= (TSTATE_ICC | TSTATE_XCC);
248 regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
249 regs->tstate |= tstate;
255 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
264 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
273 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
274 36 * sizeof(u64), -1);
279 static int fpregs64_get(struct task_struct *target,
280 const struct user_regset *regset,
281 unsigned int pos, unsigned int count,
282 void *kbuf, void __user *ubuf)
284 const unsigned long *fpregs = task_thread_info(target)->fpregs;
285 unsigned long fprs, fsr, gsr;
288 if (target == current)
289 save_and_clear_fpu();
291 fprs = task_thread_info(target)->fpsaved[0];
294 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
296 0, 16 * sizeof(u64));
298 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
304 ret = user_regset_copyout(&pos, &count,
310 ret = user_regset_copyout_zero(&pos, &count,
316 if (fprs & FPRS_FEF) {
317 fsr = task_thread_info(target)->xfsr[0];
318 gsr = task_thread_info(target)->gsr[0];
324 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
329 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
334 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
340 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
341 35 * sizeof(u64), -1);
346 static int fpregs64_set(struct task_struct *target,
347 const struct user_regset *regset,
348 unsigned int pos, unsigned int count,
349 const void *kbuf, const void __user *ubuf)
351 unsigned long *fpregs = task_thread_info(target)->fpregs;
355 if (target == current)
356 save_and_clear_fpu();
358 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
360 0, 32 * sizeof(u64));
362 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
363 task_thread_info(target)->xfsr,
367 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
368 task_thread_info(target)->gsr,
372 fprs = task_thread_info(target)->fpsaved[0];
373 if (!ret && count > 0) {
374 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
380 fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
381 task_thread_info(target)->fpsaved[0] = fprs;
384 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
385 35 * sizeof(u64), -1);
389 static const struct user_regset sparc64_regsets[] = {
395 * TSTATE, TPC, TNPC, Y
398 .core_note_type = NT_PRSTATUS,
399 .n = 36 * sizeof(u64),
400 .size = sizeof(u64), .align = sizeof(u64),
401 .get = genregs64_get, .set = genregs64_set
410 .core_note_type = NT_PRFPREG,
411 .n = 35 * sizeof(u64),
412 .size = sizeof(u64), .align = sizeof(u64),
413 .get = fpregs64_get, .set = fpregs64_set
417 static const struct user_regset_view user_sparc64_view = {
418 .name = "sparc64", .e_machine = EM_SPARCV9,
419 .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
423 static int genregs32_get(struct task_struct *target,
424 const struct user_regset *regset,
425 unsigned int pos, unsigned int count,
426 void *kbuf, void __user *ubuf)
428 const struct pt_regs *regs = task_pt_regs(target);
429 compat_ulong_t __user *reg_window;
430 compat_ulong_t *k = kbuf;
431 compat_ulong_t __user *u = ubuf;
434 if (target == current)
438 count /= sizeof(reg);
441 for (; count > 0 && pos < 16; count--)
442 *k++ = regs->u_regs[pos++];
444 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
445 if (target == current) {
446 for (; count > 0 && pos < 32; count--) {
447 if (get_user(*k++, ®_window[pos++]))
451 for (; count > 0 && pos < 32; count--) {
452 if (access_process_vm(target,
463 for (; count > 0 && pos < 16; count--) {
464 if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
468 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
469 if (target == current) {
470 for (; count > 0 && pos < 32; count--) {
471 if (get_user(reg, ®_window[pos++]) ||
476 for (; count > 0 && pos < 32; count--) {
477 if (access_process_vm(target,
480 ®, sizeof(reg), 0)
483 if (access_process_vm(target,
485 ®, sizeof(reg), 1)
496 reg = tstate_to_psr(regs->tstate);
517 else if (put_user(reg, u++))
524 count *= sizeof(reg);
526 return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
527 38 * sizeof(reg), -1);
530 static int genregs32_set(struct task_struct *target,
531 const struct user_regset *regset,
532 unsigned int pos, unsigned int count,
533 const void *kbuf, const void __user *ubuf)
535 struct pt_regs *regs = task_pt_regs(target);
536 compat_ulong_t __user *reg_window;
537 const compat_ulong_t *k = kbuf;
538 const compat_ulong_t __user *u = ubuf;
541 if (target == current)
545 count /= sizeof(reg);
548 for (; count > 0 && pos < 16; count--)
549 regs->u_regs[pos++] = *k++;
551 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
552 if (target == current) {
553 for (; count > 0 && pos < 32; count--) {
554 if (put_user(*k++, ®_window[pos++]))
558 for (; count > 0 && pos < 32; count--) {
559 if (access_process_vm(target,
571 for (; count > 0 && pos < 16; count--) {
572 if (get_user(reg, u++))
574 regs->u_regs[pos++] = reg;
577 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
578 if (target == current) {
579 for (; count > 0 && pos < 32; count--) {
580 if (get_user(reg, u++) ||
581 put_user(reg, ®_window[pos++]))
585 for (; count > 0 && pos < 32; count--) {
586 if (access_process_vm(target,
589 ®, sizeof(reg), 0)
592 if (access_process_vm(target,
595 ®, sizeof(reg), 1)
604 unsigned long tstate;
608 else if (get_user(reg, u++))
613 tstate = regs->tstate;
614 tstate &= ~(TSTATE_ICC | TSTATE_XCC);
615 tstate |= psr_to_tstate_icc(reg);
616 regs->tstate = tstate;
639 count *= sizeof(reg);
641 return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
642 38 * sizeof(reg), -1);
645 static int fpregs32_get(struct task_struct *target,
646 const struct user_regset *regset,
647 unsigned int pos, unsigned int count,
648 void *kbuf, void __user *ubuf)
650 const unsigned long *fpregs = task_thread_info(target)->fpregs;
651 compat_ulong_t enabled;
656 if (target == current)
657 save_and_clear_fpu();
659 fprs = task_thread_info(target)->fpsaved[0];
660 if (fprs & FPRS_FEF) {
661 fsr = task_thread_info(target)->xfsr[0];
668 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
670 0, 32 * sizeof(u32));
673 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
677 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
685 val = (enabled << 8) | (8 << 16);
686 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
693 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
694 35 * sizeof(u32), -1);
699 static int fpregs32_set(struct task_struct *target,
700 const struct user_regset *regset,
701 unsigned int pos, unsigned int count,
702 const void *kbuf, const void __user *ubuf)
704 unsigned long *fpregs = task_thread_info(target)->fpregs;
708 if (target == current)
709 save_and_clear_fpu();
711 fprs = task_thread_info(target)->fpsaved[0];
713 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
715 0, 32 * sizeof(u32));
717 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
720 if (!ret && count > 0) {
724 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
729 val = task_thread_info(target)->xfsr[0];
730 val &= 0xffffffff00000000UL;
732 task_thread_info(target)->xfsr[0] = val;
736 fprs |= (FPRS_FEF | FPRS_DL);
737 task_thread_info(target)->fpsaved[0] = fprs;
740 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
741 34 * sizeof(u32), -1);
745 static const struct user_regset sparc32_regsets[] = {
751 * PSR, PC, nPC, Y, WIM, TBR
754 .core_note_type = NT_PRSTATUS,
755 .n = 38 * sizeof(u32),
756 .size = sizeof(u32), .align = sizeof(u32),
757 .get = genregs32_get, .set = genregs32_set
763 * FPU QUEUE COUNT (8-bit char)
764 * FPU QUEUE ENTRYSIZE (8-bit char)
765 * FPU ENABLED (8-bit char)
767 * FPU QUEUE (64 32-bit ints)
770 .core_note_type = NT_PRFPREG,
771 .n = 99 * sizeof(u32),
772 .size = sizeof(u32), .align = sizeof(u32),
773 .get = fpregs32_get, .set = fpregs32_set
777 static const struct user_regset_view user_sparc32_view = {
778 .name = "sparc", .e_machine = EM_SPARC,
779 .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
781 #endif /* CONFIG_COMPAT */
783 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
786 if (test_tsk_thread_flag(task, TIF_32BIT))
787 return &user_sparc32_view;
789 return &user_sparc64_view;
794 unsigned int regs[32];
800 unsigned int insnaddr;
805 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
806 compat_ulong_t caddr, compat_ulong_t cdata)
808 const struct user_regset_view *view = task_user_regset_view(child);
809 compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
810 struct pt_regs32 __user *pregs;
811 struct compat_fps __user *fps;
812 unsigned long addr2 = caddr2;
813 unsigned long addr = caddr;
814 unsigned long data = cdata;
817 pregs = (struct pt_regs32 __user *) addr;
818 fps = (struct compat_fps __user *) addr;
822 ret = (addr != 0) ? -EIO : 0;
826 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
831 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
838 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
843 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
849 case PTRACE_GETFPREGS:
850 ret = copy_regset_to_user(child, view, REGSET_FP,
855 ret = copy_regset_to_user(child, view, REGSET_FP,
860 if (__put_user(0, &fps->flags) ||
861 __put_user(0, &fps->extra) ||
862 __put_user(0, &fps->fpqd) ||
863 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
868 case PTRACE_SETFPREGS:
869 ret = copy_regset_from_user(child, view, REGSET_FP,
874 ret = copy_regset_from_user(child, view, REGSET_FP,
880 case PTRACE_READTEXT:
881 case PTRACE_READDATA:
882 ret = ptrace_readdata(child, addr,
883 (char __user *)addr2, data);
890 case PTRACE_WRITETEXT:
891 case PTRACE_WRITEDATA:
892 ret = ptrace_writedata(child, (char __user *) addr2,
901 ret = compat_ptrace_request(child, request, addr, data);
907 #endif /* CONFIG_COMPAT */
910 unsigned int regs[64];
914 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
916 const struct user_regset_view *view = task_user_regset_view(child);
917 unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
918 struct pt_regs __user *pregs;
919 struct fps __user *fps;
922 pregs = (struct pt_regs __user *) (unsigned long) addr;
923 fps = (struct fps __user *) (unsigned long) addr;
927 ret = (addr != 0) ? -EIO : 0;
930 case PTRACE_GETREGS64:
931 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
936 /* XXX doesn't handle 'y' register correctly XXX */
937 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
944 case PTRACE_SETREGS64:
945 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
950 /* XXX doesn't handle 'y' register correctly XXX */
951 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
958 case PTRACE_GETFPREGS64:
959 ret = copy_regset_to_user(child, view, REGSET_FP,
965 case PTRACE_SETFPREGS64:
966 ret = copy_regset_to_user(child, view, REGSET_FP,
972 case PTRACE_READTEXT:
973 case PTRACE_READDATA:
974 ret = ptrace_readdata(child, addr,
975 (char __user *)addr2, data);
982 case PTRACE_WRITETEXT:
983 case PTRACE_WRITEDATA:
984 ret = ptrace_writedata(child, (char __user *) addr2,
993 ret = ptrace_request(child, request, addr, data);
1000 asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
1002 /* do the secure computing check first */
1003 secure_computing(regs->u_regs[UREG_G1]);
1005 if (unlikely(current->audit_context) && syscall_exit_p) {
1006 unsigned long tstate = regs->tstate;
1007 int result = AUDITSC_SUCCESS;
1009 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
1010 result = AUDITSC_FAILURE;
1012 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
1015 if (!(current->ptrace & PT_PTRACED))
1018 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1021 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1025 * this isn't the same as continuing with a signal, but it will do
1026 * for normal use. strace only continues with a signal if the
1027 * stopping signal is not SIGTRAP. -brl
1029 if (current->exit_code) {
1030 send_sig(current->exit_code, current, 1);
1031 current->exit_code = 0;
1035 if (unlikely(current->audit_context) && !syscall_exit_p)
1036 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
1038 AUDIT_ARCH_SPARC64),
1039 regs->u_regs[UREG_G1],
1040 regs->u_regs[UREG_I0],
1041 regs->u_regs[UREG_I1],
1042 regs->u_regs[UREG_I2],
1043 regs->u_regs[UREG_I3]);