GNU Linux-libre 4.4.288-gnu1
[releases.git] / arch / sparc / kernel / ptrace_64.c
1 /* ptrace.c: Sparc process tracing support.
2  *
3  * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
4  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  *
6  * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
7  * and David Mosberger.
8  *
9  * Added Linux support -miguel (weird, eh?, the original code was meant
10  * to emulate SunOS).
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.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>
31
32 #include <asm/asi.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>
38 #include <asm/page.h>
39 #include <asm/cpudata.h>
40 #include <asm/cacheflush.h>
41
42 #define CREATE_TRACE_POINTS
43 #include <trace/events/syscalls.h>
44
45 #include "entry.h"
46
47 /* #define ALLOW_INIT_TRACING */
48
49 /*
50  * Called by kernel/ptrace.c when detaching..
51  *
52  * Make sure single step bits etc are not set.
53  */
54 void ptrace_disable(struct task_struct *child)
55 {
56         /* nothing to do */
57 }
58
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.
63  *
64  * Now, the only thing we have to do is:
65  * 1) flush the D-cache if it's possible than an illegal alias
66  *    has been created
67  * 2) flush the I-cache if this is pre-cheetah and we did a write
68  */
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)
72 {
73         BUG_ON(len > PAGE_SIZE);
74
75         if (tlb_type == hypervisor)
76                 return;
77
78         preempt_disable();
79
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
84          * D-cache flush.
85          */
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;
90
91                 dcache_line_size = local_cpu_data().dcache_line_size;
92
93                 if (tlb_type == spitfire) {
94                         for (; start < end; start += dcache_line_size)
95                                 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
96                 } else {
97                         start &= ~(dcache_line_size - 1);
98                         for (; start < end; start += dcache_line_size)
99                                 __asm__ __volatile__(
100                                         "stxa %%g0, [%0] %1\n\t"
101                                         "membar #Sync"
102                                         : /* no outputs */
103                                         : "r" (start),
104                                         "i" (ASI_DCACHE_INVALIDATE));
105                 }
106         }
107 #endif
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;
112
113                 icache_line_size = local_cpu_data().icache_line_size;
114
115                 for (; start < end; start += icache_line_size)
116                         flushi(start);
117         }
118
119         preempt_enable();
120 }
121 EXPORT_SYMBOL_GPL(flush_ptrace_access);
122
123 static int get_from_target(struct task_struct *target, unsigned long uaddr,
124                            void *kbuf, int len)
125 {
126         if (target == current) {
127                 if (copy_from_user(kbuf, (void __user *) uaddr, len))
128                         return -EFAULT;
129         } else {
130                 int len2 = access_process_vm(target, uaddr, kbuf, len, 0);
131                 if (len2 != len)
132                         return -EFAULT;
133         }
134         return 0;
135 }
136
137 static int set_to_target(struct task_struct *target, unsigned long uaddr,
138                          void *kbuf, int len)
139 {
140         if (target == current) {
141                 if (copy_to_user((void __user *) uaddr, kbuf, len))
142                         return -EFAULT;
143         } else {
144                 int len2 = access_process_vm(target, uaddr, kbuf, len, 1);
145                 if (len2 != len)
146                         return -EFAULT;
147         }
148         return 0;
149 }
150
151 static int regwindow64_get(struct task_struct *target,
152                            const struct pt_regs *regs,
153                            struct reg_window *wbuf)
154 {
155         unsigned long rw_addr = regs->u_regs[UREG_I6];
156
157         if (!test_thread_64bit_stack(rw_addr)) {
158                 struct reg_window32 win32;
159                 int i;
160
161                 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
162                         return -EFAULT;
163                 for (i = 0; i < 8; i++)
164                         wbuf->locals[i] = win32.locals[i];
165                 for (i = 0; i < 8; i++)
166                         wbuf->ins[i] = win32.ins[i];
167         } else {
168                 rw_addr += STACK_BIAS;
169                 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
170                         return -EFAULT;
171         }
172
173         return 0;
174 }
175
176 static int regwindow64_set(struct task_struct *target,
177                            const struct pt_regs *regs,
178                            struct reg_window *wbuf)
179 {
180         unsigned long rw_addr = regs->u_regs[UREG_I6];
181
182         if (!test_thread_64bit_stack(rw_addr)) {
183                 struct reg_window32 win32;
184                 int i;
185
186                 for (i = 0; i < 8; i++)
187                         win32.locals[i] = wbuf->locals[i];
188                 for (i = 0; i < 8; i++)
189                         win32.ins[i] = wbuf->ins[i];
190
191                 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
192                         return -EFAULT;
193         } else {
194                 rw_addr += STACK_BIAS;
195                 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
196                         return -EFAULT;
197         }
198
199         return 0;
200 }
201
202 enum sparc_regset {
203         REGSET_GENERAL,
204         REGSET_FP,
205 };
206
207 static int genregs64_get(struct task_struct *target,
208                          const struct user_regset *regset,
209                          unsigned int pos, unsigned int count,
210                          void *kbuf, void __user *ubuf)
211 {
212         const struct pt_regs *regs = task_pt_regs(target);
213         int ret;
214
215         if (target == current)
216                 flushw_user();
217
218         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
219                                   regs->u_regs,
220                                   0, 16 * sizeof(u64));
221         if (!ret && count && pos < (32 * sizeof(u64))) {
222                 struct reg_window window;
223
224                 if (regwindow64_get(target, regs, &window))
225                         return -EFAULT;
226                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
227                                           &window,
228                                           16 * sizeof(u64),
229                                           32 * sizeof(u64));
230         }
231
232         if (!ret) {
233                 /* TSTATE, TPC, TNPC */
234                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
235                                           &regs->tstate,
236                                           32 * sizeof(u64),
237                                           35 * sizeof(u64));
238         }
239
240         if (!ret) {
241                 unsigned long y = regs->y;
242
243                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
244                                           &y,
245                                           35 * sizeof(u64),
246                                           36 * sizeof(u64));
247         }
248
249         if (!ret) {
250                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
251                                                36 * sizeof(u64), -1);
252
253         }
254         return ret;
255 }
256
257 static int genregs64_set(struct task_struct *target,
258                          const struct user_regset *regset,
259                          unsigned int pos, unsigned int count,
260                          const void *kbuf, const void __user *ubuf)
261 {
262         struct pt_regs *regs = task_pt_regs(target);
263         int ret;
264
265         if (target == current)
266                 flushw_user();
267
268         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
269                                  regs->u_regs,
270                                  0, 16 * sizeof(u64));
271         if (!ret && count && pos < (32 * sizeof(u64))) {
272                 struct reg_window window;
273
274                 if (regwindow64_get(target, regs, &window))
275                         return -EFAULT;
276
277                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
278                                          &window,
279                                          16 * sizeof(u64),
280                                          32 * sizeof(u64));
281
282                 if (!ret &&
283                     regwindow64_set(target, regs, &window))
284                         return -EFAULT;
285         }
286
287         if (!ret && count > 0) {
288                 unsigned long tstate;
289
290                 /* TSTATE */
291                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
292                                          &tstate,
293                                          32 * sizeof(u64),
294                                          33 * sizeof(u64));
295                 if (!ret) {
296                         /* Only the condition codes and the "in syscall"
297                          * state can be modified in the %tstate register.
298                          */
299                         tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
300                         regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
301                         regs->tstate |= tstate;
302                 }
303         }
304
305         if (!ret) {
306                 /* TPC, TNPC */
307                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
308                                          &regs->tpc,
309                                          33 * sizeof(u64),
310                                          35 * sizeof(u64));
311         }
312
313         if (!ret) {
314                 unsigned long y = regs->y;
315
316                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
317                                          &y,
318                                          35 * sizeof(u64),
319                                          36 * sizeof(u64));
320                 if (!ret)
321                         regs->y = y;
322         }
323
324         if (!ret)
325                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
326                                                 36 * sizeof(u64), -1);
327
328         return ret;
329 }
330
331 static int fpregs64_get(struct task_struct *target,
332                         const struct user_regset *regset,
333                         unsigned int pos, unsigned int count,
334                         void *kbuf, void __user *ubuf)
335 {
336         const unsigned long *fpregs = task_thread_info(target)->fpregs;
337         unsigned long fprs, fsr, gsr;
338         int ret;
339
340         if (target == current)
341                 save_and_clear_fpu();
342
343         fprs = task_thread_info(target)->fpsaved[0];
344
345         if (fprs & FPRS_DL)
346                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
347                                           fpregs,
348                                           0, 16 * sizeof(u64));
349         else
350                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
351                                                0,
352                                                16 * sizeof(u64));
353
354         if (!ret) {
355                 if (fprs & FPRS_DU)
356                         ret = user_regset_copyout(&pos, &count,
357                                                   &kbuf, &ubuf,
358                                                   fpregs + 16,
359                                                   16 * sizeof(u64),
360                                                   32 * sizeof(u64));
361                 else
362                         ret = user_regset_copyout_zero(&pos, &count,
363                                                        &kbuf, &ubuf,
364                                                        16 * sizeof(u64),
365                                                        32 * sizeof(u64));
366         }
367
368         if (fprs & FPRS_FEF) {
369                 fsr = task_thread_info(target)->xfsr[0];
370                 gsr = task_thread_info(target)->gsr[0];
371         } else {
372                 fsr = gsr = 0;
373         }
374
375         if (!ret)
376                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
377                                           &fsr,
378                                           32 * sizeof(u64),
379                                           33 * sizeof(u64));
380         if (!ret)
381                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
382                                           &gsr,
383                                           33 * sizeof(u64),
384                                           34 * sizeof(u64));
385         if (!ret)
386                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
387                                           &fprs,
388                                           34 * sizeof(u64),
389                                           35 * sizeof(u64));
390
391         if (!ret)
392                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
393                                                35 * sizeof(u64), -1);
394
395         return ret;
396 }
397
398 static int fpregs64_set(struct task_struct *target,
399                         const struct user_regset *regset,
400                         unsigned int pos, unsigned int count,
401                         const void *kbuf, const void __user *ubuf)
402 {
403         unsigned long *fpregs = task_thread_info(target)->fpregs;
404         unsigned long fprs;
405         int ret;
406
407         if (target == current)
408                 save_and_clear_fpu();
409
410         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
411                                  fpregs,
412                                  0, 32 * sizeof(u64));
413         if (!ret)
414                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
415                                          task_thread_info(target)->xfsr,
416                                          32 * sizeof(u64),
417                                          33 * sizeof(u64));
418         if (!ret)
419                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
420                                          task_thread_info(target)->gsr,
421                                          33 * sizeof(u64),
422                                          34 * sizeof(u64));
423
424         fprs = task_thread_info(target)->fpsaved[0];
425         if (!ret && count > 0) {
426                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
427                                          &fprs,
428                                          34 * sizeof(u64),
429                                          35 * sizeof(u64));
430         }
431
432         fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
433         task_thread_info(target)->fpsaved[0] = fprs;
434
435         if (!ret)
436                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
437                                                 35 * sizeof(u64), -1);
438         return ret;
439 }
440
441 static const struct user_regset sparc64_regsets[] = {
442         /* Format is:
443          *      G0 --> G7
444          *      O0 --> O7
445          *      L0 --> L7
446          *      I0 --> I7
447          *      TSTATE, TPC, TNPC, Y
448          */
449         [REGSET_GENERAL] = {
450                 .core_note_type = NT_PRSTATUS,
451                 .n = 36,
452                 .size = sizeof(u64), .align = sizeof(u64),
453                 .get = genregs64_get, .set = genregs64_set
454         },
455         /* Format is:
456          *      F0 --> F63
457          *      FSR
458          *      GSR
459          *      FPRS
460          */
461         [REGSET_FP] = {
462                 .core_note_type = NT_PRFPREG,
463                 .n = 35,
464                 .size = sizeof(u64), .align = sizeof(u64),
465                 .get = fpregs64_get, .set = fpregs64_set
466         },
467 };
468
469 static const struct user_regset_view user_sparc64_view = {
470         .name = "sparc64", .e_machine = EM_SPARCV9,
471         .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
472 };
473
474 #ifdef CONFIG_COMPAT
475 static int genregs32_get(struct task_struct *target,
476                          const struct user_regset *regset,
477                          unsigned int pos, unsigned int count,
478                          void *kbuf, void __user *ubuf)
479 {
480         const struct pt_regs *regs = task_pt_regs(target);
481         compat_ulong_t __user *reg_window;
482         compat_ulong_t *k = kbuf;
483         compat_ulong_t __user *u = ubuf;
484         compat_ulong_t reg;
485
486         if (target == current)
487                 flushw_user();
488
489         pos /= sizeof(reg);
490         count /= sizeof(reg);
491
492         if (kbuf) {
493                 for (; count > 0 && pos < 16; count--)
494                         *k++ = regs->u_regs[pos++];
495
496                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
497                 reg_window -= 16;
498                 if (target == current) {
499                         for (; count > 0 && pos < 32; count--) {
500                                 if (get_user(*k++, &reg_window[pos++]))
501                                         return -EFAULT;
502                         }
503                 } else {
504                         for (; count > 0 && pos < 32; count--) {
505                                 if (access_process_vm(target,
506                                                       (unsigned long)
507                                                       &reg_window[pos],
508                                                       k, sizeof(*k), 0)
509                                     != sizeof(*k))
510                                         return -EFAULT;
511                                 k++;
512                                 pos++;
513                         }
514                 }
515         } else {
516                 for (; count > 0 && pos < 16; count--) {
517                         if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
518                                 return -EFAULT;
519                 }
520
521                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
522                 reg_window -= 16;
523                 if (target == current) {
524                         for (; count > 0 && pos < 32; count--) {
525                                 if (get_user(reg, &reg_window[pos++]) ||
526                                     put_user(reg, u++))
527                                         return -EFAULT;
528                         }
529                 } else {
530                         for (; count > 0 && pos < 32; count--) {
531                                 if (access_process_vm(target,
532                                                       (unsigned long)
533                                                       &reg_window[pos],
534                                                       &reg, sizeof(reg), 0)
535                                     != sizeof(reg))
536                                         return -EFAULT;
537                                 if (put_user(reg, u++))
538                                         return -EFAULT;
539                         }
540                 }
541         }
542         while (count > 0) {
543                 switch (pos) {
544                 case 32: /* PSR */
545                         reg = tstate_to_psr(regs->tstate);
546                         break;
547                 case 33: /* PC */
548                         reg = regs->tpc;
549                         break;
550                 case 34: /* NPC */
551                         reg = regs->tnpc;
552                         break;
553                 case 35: /* Y */
554                         reg = regs->y;
555                         break;
556                 case 36: /* WIM */
557                 case 37: /* TBR */
558                         reg = 0;
559                         break;
560                 default:
561                         goto finish;
562                 }
563
564                 if (kbuf)
565                         *k++ = reg;
566                 else if (put_user(reg, u++))
567                         return -EFAULT;
568                 pos++;
569                 count--;
570         }
571 finish:
572         pos *= sizeof(reg);
573         count *= sizeof(reg);
574
575         return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
576                                         38 * sizeof(reg), -1);
577 }
578
579 static int genregs32_set(struct task_struct *target,
580                          const struct user_regset *regset,
581                          unsigned int pos, unsigned int count,
582                          const void *kbuf, const void __user *ubuf)
583 {
584         struct pt_regs *regs = task_pt_regs(target);
585         compat_ulong_t __user *reg_window;
586         const compat_ulong_t *k = kbuf;
587         const compat_ulong_t __user *u = ubuf;
588         compat_ulong_t reg;
589
590         if (target == current)
591                 flushw_user();
592
593         pos /= sizeof(reg);
594         count /= sizeof(reg);
595
596         if (kbuf) {
597                 for (; count > 0 && pos < 16; count--)
598                         regs->u_regs[pos++] = *k++;
599
600                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
601                 reg_window -= 16;
602                 if (target == current) {
603                         for (; count > 0 && pos < 32; count--) {
604                                 if (put_user(*k++, &reg_window[pos++]))
605                                         return -EFAULT;
606                         }
607                 } else {
608                         for (; count > 0 && pos < 32; count--) {
609                                 if (access_process_vm(target,
610                                                       (unsigned long)
611                                                       &reg_window[pos],
612                                                       (void *) k,
613                                                       sizeof(*k), 1)
614                                     != sizeof(*k))
615                                         return -EFAULT;
616                                 k++;
617                                 pos++;
618                         }
619                 }
620         } else {
621                 for (; count > 0 && pos < 16; count--) {
622                         if (get_user(reg, u++))
623                                 return -EFAULT;
624                         regs->u_regs[pos++] = reg;
625                 }
626
627                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
628                 reg_window -= 16;
629                 if (target == current) {
630                         for (; count > 0 && pos < 32; count--) {
631                                 if (get_user(reg, u++) ||
632                                     put_user(reg, &reg_window[pos++]))
633                                         return -EFAULT;
634                         }
635                 } else {
636                         for (; count > 0 && pos < 32; count--) {
637                                 if (get_user(reg, u++))
638                                         return -EFAULT;
639                                 if (access_process_vm(target,
640                                                       (unsigned long)
641                                                       &reg_window[pos],
642                                                       &reg, sizeof(reg), 1)
643                                     != sizeof(reg))
644                                         return -EFAULT;
645                                 pos++;
646                                 u++;
647                         }
648                 }
649         }
650         while (count > 0) {
651                 unsigned long tstate;
652
653                 if (kbuf)
654                         reg = *k++;
655                 else if (get_user(reg, u++))
656                         return -EFAULT;
657
658                 switch (pos) {
659                 case 32: /* PSR */
660                         tstate = regs->tstate;
661                         tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
662                         tstate |= psr_to_tstate_icc(reg);
663                         if (reg & PSR_SYSCALL)
664                                 tstate |= TSTATE_SYSCALL;
665                         regs->tstate = tstate;
666                         break;
667                 case 33: /* PC */
668                         regs->tpc = reg;
669                         break;
670                 case 34: /* NPC */
671                         regs->tnpc = reg;
672                         break;
673                 case 35: /* Y */
674                         regs->y = reg;
675                         break;
676                 case 36: /* WIM */
677                 case 37: /* TBR */
678                         break;
679                 default:
680                         goto finish;
681                 }
682
683                 pos++;
684                 count--;
685         }
686 finish:
687         pos *= sizeof(reg);
688         count *= sizeof(reg);
689
690         return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
691                                          38 * sizeof(reg), -1);
692 }
693
694 static int fpregs32_get(struct task_struct *target,
695                         const struct user_regset *regset,
696                         unsigned int pos, unsigned int count,
697                         void *kbuf, void __user *ubuf)
698 {
699         const unsigned long *fpregs = task_thread_info(target)->fpregs;
700         compat_ulong_t enabled;
701         unsigned long fprs;
702         compat_ulong_t fsr;
703         int ret = 0;
704
705         if (target == current)
706                 save_and_clear_fpu();
707
708         fprs = task_thread_info(target)->fpsaved[0];
709         if (fprs & FPRS_FEF) {
710                 fsr = task_thread_info(target)->xfsr[0];
711                 enabled = 1;
712         } else {
713                 fsr = 0;
714                 enabled = 0;
715         }
716
717         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
718                                   fpregs,
719                                   0, 32 * sizeof(u32));
720
721         if (!ret)
722                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
723                                                32 * sizeof(u32),
724                                                33 * sizeof(u32));
725         if (!ret)
726                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
727                                           &fsr,
728                                           33 * sizeof(u32),
729                                           34 * sizeof(u32));
730
731         if (!ret) {
732                 compat_ulong_t val;
733
734                 val = (enabled << 8) | (8 << 16);
735                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
736                                           &val,
737                                           34 * sizeof(u32),
738                                           35 * sizeof(u32));
739         }
740
741         if (!ret)
742                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
743                                                35 * sizeof(u32), -1);
744
745         return ret;
746 }
747
748 static int fpregs32_set(struct task_struct *target,
749                         const struct user_regset *regset,
750                         unsigned int pos, unsigned int count,
751                         const void *kbuf, const void __user *ubuf)
752 {
753         unsigned long *fpregs = task_thread_info(target)->fpregs;
754         unsigned long fprs;
755         int ret;
756
757         if (target == current)
758                 save_and_clear_fpu();
759
760         fprs = task_thread_info(target)->fpsaved[0];
761
762         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
763                                  fpregs,
764                                  0, 32 * sizeof(u32));
765         if (!ret)
766                 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
767                                           32 * sizeof(u32),
768                                           33 * sizeof(u32));
769         if (!ret && count > 0) {
770                 compat_ulong_t fsr;
771                 unsigned long val;
772
773                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
774                                          &fsr,
775                                          33 * sizeof(u32),
776                                          34 * sizeof(u32));
777                 if (!ret) {
778                         val = task_thread_info(target)->xfsr[0];
779                         val &= 0xffffffff00000000UL;
780                         val |= fsr;
781                         task_thread_info(target)->xfsr[0] = val;
782                 }
783         }
784
785         fprs |= (FPRS_FEF | FPRS_DL);
786         task_thread_info(target)->fpsaved[0] = fprs;
787
788         if (!ret)
789                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
790                                                 34 * sizeof(u32), -1);
791         return ret;
792 }
793
794 static const struct user_regset sparc32_regsets[] = {
795         /* Format is:
796          *      G0 --> G7
797          *      O0 --> O7
798          *      L0 --> L7
799          *      I0 --> I7
800          *      PSR, PC, nPC, Y, WIM, TBR
801          */
802         [REGSET_GENERAL] = {
803                 .core_note_type = NT_PRSTATUS,
804                 .n = 38,
805                 .size = sizeof(u32), .align = sizeof(u32),
806                 .get = genregs32_get, .set = genregs32_set
807         },
808         /* Format is:
809          *      F0 --> F31
810          *      empty 32-bit word
811          *      FSR (32--bit word)
812          *      FPU QUEUE COUNT (8-bit char)
813          *      FPU QUEUE ENTRYSIZE (8-bit char)
814          *      FPU ENABLED (8-bit char)
815          *      empty 8-bit char
816          *      FPU QUEUE (64 32-bit ints)
817          */
818         [REGSET_FP] = {
819                 .core_note_type = NT_PRFPREG,
820                 .n = 99,
821                 .size = sizeof(u32), .align = sizeof(u32),
822                 .get = fpregs32_get, .set = fpregs32_set
823         },
824 };
825
826 static const struct user_regset_view user_sparc32_view = {
827         .name = "sparc", .e_machine = EM_SPARC,
828         .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
829 };
830 #endif /* CONFIG_COMPAT */
831
832 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
833 {
834 #ifdef CONFIG_COMPAT
835         if (test_tsk_thread_flag(task, TIF_32BIT))
836                 return &user_sparc32_view;
837 #endif
838         return &user_sparc64_view;
839 }
840
841 #ifdef CONFIG_COMPAT
842 struct compat_fps {
843         unsigned int regs[32];
844         unsigned int fsr;
845         unsigned int flags;
846         unsigned int extra;
847         unsigned int fpqd;
848         struct compat_fq {
849                 unsigned int insnaddr;
850                 unsigned int insn;
851         } fpq[16];
852 };
853
854 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
855                         compat_ulong_t caddr, compat_ulong_t cdata)
856 {
857         const struct user_regset_view *view = task_user_regset_view(current);
858         compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
859         struct pt_regs32 __user *pregs;
860         struct compat_fps __user *fps;
861         unsigned long addr2 = caddr2;
862         unsigned long addr = caddr;
863         unsigned long data = cdata;
864         int ret;
865
866         pregs = (struct pt_regs32 __user *) addr;
867         fps = (struct compat_fps __user *) addr;
868
869         switch (request) {
870         case PTRACE_PEEKUSR:
871                 ret = (addr != 0) ? -EIO : 0;
872                 break;
873
874         case PTRACE_GETREGS:
875                 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
876                                           32 * sizeof(u32),
877                                           4 * sizeof(u32),
878                                           &pregs->psr);
879                 if (!ret)
880                         ret = copy_regset_to_user(child, view, REGSET_GENERAL,
881                                                   1 * sizeof(u32),
882                                                   15 * sizeof(u32),
883                                                   &pregs->u_regs[0]);
884                 break;
885
886         case PTRACE_SETREGS:
887                 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
888                                             32 * sizeof(u32),
889                                             4 * sizeof(u32),
890                                             &pregs->psr);
891                 if (!ret)
892                         ret = copy_regset_from_user(child, view, REGSET_GENERAL,
893                                                     1 * sizeof(u32),
894                                                     15 * sizeof(u32),
895                                                     &pregs->u_regs[0]);
896                 break;
897
898         case PTRACE_GETFPREGS:
899                 ret = copy_regset_to_user(child, view, REGSET_FP,
900                                           0 * sizeof(u32),
901                                           32 * sizeof(u32),
902                                           &fps->regs[0]);
903                 if (!ret)
904                         ret = copy_regset_to_user(child, view, REGSET_FP,
905                                                   33 * sizeof(u32),
906                                                   1 * sizeof(u32),
907                                                   &fps->fsr);
908                 if (!ret) {
909                         if (__put_user(0, &fps->flags) ||
910                             __put_user(0, &fps->extra) ||
911                             __put_user(0, &fps->fpqd) ||
912                             clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
913                                 ret = -EFAULT;
914                 }
915                 break;
916
917         case PTRACE_SETFPREGS:
918                 ret = copy_regset_from_user(child, view, REGSET_FP,
919                                             0 * sizeof(u32),
920                                             32 * sizeof(u32),
921                                             &fps->regs[0]);
922                 if (!ret)
923                         ret = copy_regset_from_user(child, view, REGSET_FP,
924                                                     33 * sizeof(u32),
925                                                     1 * sizeof(u32),
926                                                     &fps->fsr);
927                 break;
928
929         case PTRACE_READTEXT:
930         case PTRACE_READDATA:
931                 ret = ptrace_readdata(child, addr,
932                                       (char __user *)addr2, data);
933                 if (ret == data)
934                         ret = 0;
935                 else if (ret >= 0)
936                         ret = -EIO;
937                 break;
938
939         case PTRACE_WRITETEXT:
940         case PTRACE_WRITEDATA:
941                 ret = ptrace_writedata(child, (char __user *) addr2,
942                                        addr, data);
943                 if (ret == data)
944                         ret = 0;
945                 else if (ret >= 0)
946                         ret = -EIO;
947                 break;
948
949         default:
950                 if (request == PTRACE_SPARC_DETACH)
951                         request = PTRACE_DETACH;
952                 ret = compat_ptrace_request(child, request, addr, data);
953                 break;
954         }
955
956         return ret;
957 }
958 #endif /* CONFIG_COMPAT */
959
960 struct fps {
961         unsigned int regs[64];
962         unsigned long fsr;
963 };
964
965 long arch_ptrace(struct task_struct *child, long request,
966                  unsigned long addr, unsigned long data)
967 {
968         const struct user_regset_view *view = task_user_regset_view(current);
969         unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
970         struct pt_regs __user *pregs;
971         struct fps __user *fps;
972         void __user *addr2p;
973         int ret;
974
975         pregs = (struct pt_regs __user *) addr;
976         fps = (struct fps __user *) addr;
977         addr2p = (void __user *) addr2;
978
979         switch (request) {
980         case PTRACE_PEEKUSR:
981                 ret = (addr != 0) ? -EIO : 0;
982                 break;
983
984         case PTRACE_GETREGS64:
985                 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
986                                           1 * sizeof(u64),
987                                           15 * sizeof(u64),
988                                           &pregs->u_regs[0]);
989                 if (!ret) {
990                         /* XXX doesn't handle 'y' register correctly XXX */
991                         ret = copy_regset_to_user(child, view, REGSET_GENERAL,
992                                                   32 * sizeof(u64),
993                                                   4 * sizeof(u64),
994                                                   &pregs->tstate);
995                 }
996                 break;
997
998         case PTRACE_SETREGS64:
999                 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1000                                             1 * sizeof(u64),
1001                                             15 * sizeof(u64),
1002                                             &pregs->u_regs[0]);
1003                 if (!ret) {
1004                         /* XXX doesn't handle 'y' register correctly XXX */
1005                         ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1006                                                     32 * sizeof(u64),
1007                                                     4 * sizeof(u64),
1008                                                     &pregs->tstate);
1009                 }
1010                 break;
1011
1012         case PTRACE_GETFPREGS64:
1013                 ret = copy_regset_to_user(child, view, REGSET_FP,
1014                                           0 * sizeof(u64),
1015                                           33 * sizeof(u64),
1016                                           fps);
1017                 break;
1018
1019         case PTRACE_SETFPREGS64:
1020                 ret = copy_regset_from_user(child, view, REGSET_FP,
1021                                           0 * sizeof(u64),
1022                                           33 * sizeof(u64),
1023                                           fps);
1024                 break;
1025
1026         case PTRACE_READTEXT:
1027         case PTRACE_READDATA:
1028                 ret = ptrace_readdata(child, addr, addr2p, data);
1029                 if (ret == data)
1030                         ret = 0;
1031                 else if (ret >= 0)
1032                         ret = -EIO;
1033                 break;
1034
1035         case PTRACE_WRITETEXT:
1036         case PTRACE_WRITEDATA:
1037                 ret = ptrace_writedata(child, addr2p, addr, data);
1038                 if (ret == data)
1039                         ret = 0;
1040                 else if (ret >= 0)
1041                         ret = -EIO;
1042                 break;
1043
1044         default:
1045                 if (request == PTRACE_SPARC_DETACH)
1046                         request = PTRACE_DETACH;
1047                 ret = ptrace_request(child, request, addr, data);
1048                 break;
1049         }
1050
1051         return ret;
1052 }
1053
1054 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1055 {
1056         int ret = 0;
1057
1058         /* do the secure computing check first */
1059         secure_computing_strict(regs->u_regs[UREG_G1]);
1060
1061         if (test_thread_flag(TIF_NOHZ))
1062                 user_exit();
1063
1064         if (test_thread_flag(TIF_SYSCALL_TRACE))
1065                 ret = tracehook_report_syscall_entry(regs);
1066
1067         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1068                 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1069
1070         audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
1071                             regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
1072                             regs->u_regs[UREG_I3]);
1073
1074         return ret;
1075 }
1076
1077 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1078 {
1079         if (test_thread_flag(TIF_NOHZ))
1080                 user_exit();
1081
1082         audit_syscall_exit(regs);
1083
1084         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1085                 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1086
1087         if (test_thread_flag(TIF_SYSCALL_TRACE))
1088                 tracehook_report_syscall_exit(regs, 0);
1089
1090         if (test_thread_flag(TIF_NOHZ))
1091                 user_enter();
1092 }