GNU Linux-libre 4.9.309-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,
131                                 FOLL_FORCE);
132                 if (len2 != len)
133                         return -EFAULT;
134         }
135         return 0;
136 }
137
138 static int set_to_target(struct task_struct *target, unsigned long uaddr,
139                          void *kbuf, int len)
140 {
141         if (target == current) {
142                 if (copy_to_user((void __user *) uaddr, kbuf, len))
143                         return -EFAULT;
144         } else {
145                 int len2 = access_process_vm(target, uaddr, kbuf, len,
146                                 FOLL_FORCE | FOLL_WRITE);
147                 if (len2 != len)
148                         return -EFAULT;
149         }
150         return 0;
151 }
152
153 static int regwindow64_get(struct task_struct *target,
154                            const struct pt_regs *regs,
155                            struct reg_window *wbuf)
156 {
157         unsigned long rw_addr = regs->u_regs[UREG_I6];
158
159         if (!test_thread_64bit_stack(rw_addr)) {
160                 struct reg_window32 win32;
161                 int i;
162
163                 if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
164                         return -EFAULT;
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];
169         } else {
170                 rw_addr += STACK_BIAS;
171                 if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
172                         return -EFAULT;
173         }
174
175         return 0;
176 }
177
178 static int regwindow64_set(struct task_struct *target,
179                            const struct pt_regs *regs,
180                            struct reg_window *wbuf)
181 {
182         unsigned long rw_addr = regs->u_regs[UREG_I6];
183
184         if (!test_thread_64bit_stack(rw_addr)) {
185                 struct reg_window32 win32;
186                 int i;
187
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];
192
193                 if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
194                         return -EFAULT;
195         } else {
196                 rw_addr += STACK_BIAS;
197                 if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
198                         return -EFAULT;
199         }
200
201         return 0;
202 }
203
204 enum sparc_regset {
205         REGSET_GENERAL,
206         REGSET_FP,
207 };
208
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)
213 {
214         const struct pt_regs *regs = task_pt_regs(target);
215         int ret;
216
217         if (target == current)
218                 flushw_user();
219
220         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
221                                   regs->u_regs,
222                                   0, 16 * sizeof(u64));
223         if (!ret && count && pos < (32 * sizeof(u64))) {
224                 struct reg_window window;
225
226                 if (regwindow64_get(target, regs, &window))
227                         return -EFAULT;
228                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
229                                           &window,
230                                           16 * sizeof(u64),
231                                           32 * sizeof(u64));
232         }
233
234         if (!ret) {
235                 /* TSTATE, TPC, TNPC */
236                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
237                                           &regs->tstate,
238                                           32 * sizeof(u64),
239                                           35 * sizeof(u64));
240         }
241
242         if (!ret) {
243                 unsigned long y = regs->y;
244
245                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
246                                           &y,
247                                           35 * sizeof(u64),
248                                           36 * sizeof(u64));
249         }
250
251         if (!ret) {
252                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
253                                                36 * sizeof(u64), -1);
254
255         }
256         return ret;
257 }
258
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)
263 {
264         struct pt_regs *regs = task_pt_regs(target);
265         int ret;
266
267         if (target == current)
268                 flushw_user();
269
270         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
271                                  regs->u_regs,
272                                  0, 16 * sizeof(u64));
273         if (!ret && count && pos < (32 * sizeof(u64))) {
274                 struct reg_window window;
275
276                 if (regwindow64_get(target, regs, &window))
277                         return -EFAULT;
278
279                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
280                                          &window,
281                                          16 * sizeof(u64),
282                                          32 * sizeof(u64));
283
284                 if (!ret &&
285                     regwindow64_set(target, regs, &window))
286                         return -EFAULT;
287         }
288
289         if (!ret && count > 0) {
290                 unsigned long tstate;
291
292                 /* TSTATE */
293                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
294                                          &tstate,
295                                          32 * sizeof(u64),
296                                          33 * sizeof(u64));
297                 if (!ret) {
298                         /* Only the condition codes and the "in syscall"
299                          * state can be modified in the %tstate register.
300                          */
301                         tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
302                         regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
303                         regs->tstate |= tstate;
304                 }
305         }
306
307         if (!ret) {
308                 /* TPC, TNPC */
309                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
310                                          &regs->tpc,
311                                          33 * sizeof(u64),
312                                          35 * sizeof(u64));
313         }
314
315         if (!ret) {
316                 unsigned long y = regs->y;
317
318                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
319                                          &y,
320                                          35 * sizeof(u64),
321                                          36 * sizeof(u64));
322                 if (!ret)
323                         regs->y = y;
324         }
325
326         if (!ret)
327                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
328                                                 36 * sizeof(u64), -1);
329
330         return ret;
331 }
332
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)
337 {
338         const unsigned long *fpregs = task_thread_info(target)->fpregs;
339         unsigned long fprs, fsr, gsr;
340         int ret;
341
342         if (target == current)
343                 save_and_clear_fpu();
344
345         fprs = task_thread_info(target)->fpsaved[0];
346
347         if (fprs & FPRS_DL)
348                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
349                                           fpregs,
350                                           0, 16 * sizeof(u64));
351         else
352                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
353                                                0,
354                                                16 * sizeof(u64));
355
356         if (!ret) {
357                 if (fprs & FPRS_DU)
358                         ret = user_regset_copyout(&pos, &count,
359                                                   &kbuf, &ubuf,
360                                                   fpregs + 16,
361                                                   16 * sizeof(u64),
362                                                   32 * sizeof(u64));
363                 else
364                         ret = user_regset_copyout_zero(&pos, &count,
365                                                        &kbuf, &ubuf,
366                                                        16 * sizeof(u64),
367                                                        32 * sizeof(u64));
368         }
369
370         if (fprs & FPRS_FEF) {
371                 fsr = task_thread_info(target)->xfsr[0];
372                 gsr = task_thread_info(target)->gsr[0];
373         } else {
374                 fsr = gsr = 0;
375         }
376
377         if (!ret)
378                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
379                                           &fsr,
380                                           32 * sizeof(u64),
381                                           33 * sizeof(u64));
382         if (!ret)
383                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
384                                           &gsr,
385                                           33 * sizeof(u64),
386                                           34 * sizeof(u64));
387         if (!ret)
388                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
389                                           &fprs,
390                                           34 * sizeof(u64),
391                                           35 * sizeof(u64));
392
393         if (!ret)
394                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
395                                                35 * sizeof(u64), -1);
396
397         return ret;
398 }
399
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)
404 {
405         unsigned long *fpregs = task_thread_info(target)->fpregs;
406         unsigned long fprs;
407         int ret;
408
409         if (target == current)
410                 save_and_clear_fpu();
411
412         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
413                                  fpregs,
414                                  0, 32 * sizeof(u64));
415         if (!ret)
416                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
417                                          task_thread_info(target)->xfsr,
418                                          32 * sizeof(u64),
419                                          33 * sizeof(u64));
420         if (!ret)
421                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
422                                          task_thread_info(target)->gsr,
423                                          33 * sizeof(u64),
424                                          34 * sizeof(u64));
425
426         fprs = task_thread_info(target)->fpsaved[0];
427         if (!ret && count > 0) {
428                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
429                                          &fprs,
430                                          34 * sizeof(u64),
431                                          35 * sizeof(u64));
432         }
433
434         fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
435         task_thread_info(target)->fpsaved[0] = fprs;
436
437         if (!ret)
438                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
439                                                 35 * sizeof(u64), -1);
440         return ret;
441 }
442
443 static const struct user_regset sparc64_regsets[] = {
444         /* Format is:
445          *      G0 --> G7
446          *      O0 --> O7
447          *      L0 --> L7
448          *      I0 --> I7
449          *      TSTATE, TPC, TNPC, Y
450          */
451         [REGSET_GENERAL] = {
452                 .core_note_type = NT_PRSTATUS,
453                 .n = 36,
454                 .size = sizeof(u64), .align = sizeof(u64),
455                 .get = genregs64_get, .set = genregs64_set
456         },
457         /* Format is:
458          *      F0 --> F63
459          *      FSR
460          *      GSR
461          *      FPRS
462          */
463         [REGSET_FP] = {
464                 .core_note_type = NT_PRFPREG,
465                 .n = 35,
466                 .size = sizeof(u64), .align = sizeof(u64),
467                 .get = fpregs64_get, .set = fpregs64_set
468         },
469 };
470
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)
474 };
475
476 #ifdef CONFIG_COMPAT
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)
481 {
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;
486         compat_ulong_t reg;
487
488         if (target == current)
489                 flushw_user();
490
491         pos /= sizeof(reg);
492         count /= sizeof(reg);
493
494         if (kbuf) {
495                 for (; count > 0 && pos < 16; count--)
496                         *k++ = regs->u_regs[pos++];
497
498                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
499                 reg_window -= 16;
500                 if (target == current) {
501                         for (; count > 0 && pos < 32; count--) {
502                                 if (get_user(*k++, &reg_window[pos++]))
503                                         return -EFAULT;
504                         }
505                 } else {
506                         for (; count > 0 && pos < 32; count--) {
507                                 if (access_process_vm(target,
508                                                       (unsigned long)
509                                                       &reg_window[pos],
510                                                       k, sizeof(*k),
511                                                       FOLL_FORCE)
512                                     != sizeof(*k))
513                                         return -EFAULT;
514                                 k++;
515                                 pos++;
516                         }
517                 }
518         } else {
519                 for (; count > 0 && pos < 16; count--) {
520                         if (put_user((compat_ulong_t) regs->u_regs[pos++], u++))
521                                 return -EFAULT;
522                 }
523
524                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
525                 reg_window -= 16;
526                 if (target == current) {
527                         for (; count > 0 && pos < 32; count--) {
528                                 if (get_user(reg, &reg_window[pos++]) ||
529                                     put_user(reg, u++))
530                                         return -EFAULT;
531                         }
532                 } else {
533                         for (; count > 0 && pos < 32; count--) {
534                                 if (access_process_vm(target,
535                                                       (unsigned long)
536                                                       &reg_window[pos++],
537                                                       &reg, sizeof(reg),
538                                                       FOLL_FORCE)
539                                     != sizeof(reg))
540                                         return -EFAULT;
541                                 if (put_user(reg, u++))
542                                         return -EFAULT;
543                         }
544                 }
545         }
546         while (count > 0) {
547                 switch (pos) {
548                 case 32: /* PSR */
549                         reg = tstate_to_psr(regs->tstate);
550                         break;
551                 case 33: /* PC */
552                         reg = regs->tpc;
553                         break;
554                 case 34: /* NPC */
555                         reg = regs->tnpc;
556                         break;
557                 case 35: /* Y */
558                         reg = regs->y;
559                         break;
560                 case 36: /* WIM */
561                 case 37: /* TBR */
562                         reg = 0;
563                         break;
564                 default:
565                         goto finish;
566                 }
567
568                 if (kbuf)
569                         *k++ = reg;
570                 else if (put_user(reg, u++))
571                         return -EFAULT;
572                 pos++;
573                 count--;
574         }
575 finish:
576         pos *= sizeof(reg);
577         count *= sizeof(reg);
578
579         return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
580                                         38 * sizeof(reg), -1);
581 }
582
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)
587 {
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;
592         compat_ulong_t reg;
593
594         if (target == current)
595                 flushw_user();
596
597         pos /= sizeof(reg);
598         count /= sizeof(reg);
599
600         if (kbuf) {
601                 for (; count > 0 && pos < 16; count--)
602                         regs->u_regs[pos++] = *k++;
603
604                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
605                 reg_window -= 16;
606                 if (target == current) {
607                         for (; count > 0 && pos < 32; count--) {
608                                 if (put_user(*k++, &reg_window[pos++]))
609                                         return -EFAULT;
610                         }
611                 } else {
612                         for (; count > 0 && pos < 32; count--) {
613                                 if (access_process_vm(target,
614                                                       (unsigned long)
615                                                       &reg_window[pos],
616                                                       (void *) k,
617                                                       sizeof(*k),
618                                                       FOLL_FORCE | FOLL_WRITE)
619                                     != sizeof(*k))
620                                         return -EFAULT;
621                                 k++;
622                                 pos++;
623                         }
624                 }
625         } else {
626                 for (; count > 0 && pos < 16; count--) {
627                         if (get_user(reg, u++))
628                                 return -EFAULT;
629                         regs->u_regs[pos++] = reg;
630                 }
631
632                 reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
633                 reg_window -= 16;
634                 if (target == current) {
635                         for (; count > 0 && pos < 32; count--) {
636                                 if (get_user(reg, u++) ||
637                                     put_user(reg, &reg_window[pos++]))
638                                         return -EFAULT;
639                         }
640                 } else {
641                         for (; count > 0 && pos < 32; count--) {
642                                 if (get_user(reg, u++))
643                                         return -EFAULT;
644                                 if (access_process_vm(target,
645                                                       (unsigned long)
646                                                       &reg_window[pos],
647                                                       &reg, sizeof(reg),
648                                                       FOLL_FORCE | FOLL_WRITE)
649                                     != sizeof(reg))
650                                         return -EFAULT;
651                                 pos++;
652                                 u++;
653                         }
654                 }
655         }
656         while (count > 0) {
657                 unsigned long tstate;
658
659                 if (kbuf)
660                         reg = *k++;
661                 else if (get_user(reg, u++))
662                         return -EFAULT;
663
664                 switch (pos) {
665                 case 32: /* PSR */
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;
672                         break;
673                 case 33: /* PC */
674                         regs->tpc = reg;
675                         break;
676                 case 34: /* NPC */
677                         regs->tnpc = reg;
678                         break;
679                 case 35: /* Y */
680                         regs->y = reg;
681                         break;
682                 case 36: /* WIM */
683                 case 37: /* TBR */
684                         break;
685                 default:
686                         goto finish;
687                 }
688
689                 pos++;
690                 count--;
691         }
692 finish:
693         pos *= sizeof(reg);
694         count *= sizeof(reg);
695
696         return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
697                                          38 * sizeof(reg), -1);
698 }
699
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)
704 {
705         const unsigned long *fpregs = task_thread_info(target)->fpregs;
706         compat_ulong_t enabled;
707         unsigned long fprs;
708         compat_ulong_t fsr;
709         int ret = 0;
710
711         if (target == current)
712                 save_and_clear_fpu();
713
714         fprs = task_thread_info(target)->fpsaved[0];
715         if (fprs & FPRS_FEF) {
716                 fsr = task_thread_info(target)->xfsr[0];
717                 enabled = 1;
718         } else {
719                 fsr = 0;
720                 enabled = 0;
721         }
722
723         ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
724                                   fpregs,
725                                   0, 32 * sizeof(u32));
726
727         if (!ret)
728                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
729                                                32 * sizeof(u32),
730                                                33 * sizeof(u32));
731         if (!ret)
732                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
733                                           &fsr,
734                                           33 * sizeof(u32),
735                                           34 * sizeof(u32));
736
737         if (!ret) {
738                 compat_ulong_t val;
739
740                 val = (enabled << 8) | (8 << 16);
741                 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
742                                           &val,
743                                           34 * sizeof(u32),
744                                           35 * sizeof(u32));
745         }
746
747         if (!ret)
748                 ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
749                                                35 * sizeof(u32), -1);
750
751         return ret;
752 }
753
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)
758 {
759         unsigned long *fpregs = task_thread_info(target)->fpregs;
760         unsigned long fprs;
761         int ret;
762
763         if (target == current)
764                 save_and_clear_fpu();
765
766         fprs = task_thread_info(target)->fpsaved[0];
767
768         ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
769                                  fpregs,
770                                  0, 32 * sizeof(u32));
771         if (!ret)
772                 user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
773                                           32 * sizeof(u32),
774                                           33 * sizeof(u32));
775         if (!ret && count > 0) {
776                 compat_ulong_t fsr;
777                 unsigned long val;
778
779                 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
780                                          &fsr,
781                                          33 * sizeof(u32),
782                                          34 * sizeof(u32));
783                 if (!ret) {
784                         val = task_thread_info(target)->xfsr[0];
785                         val &= 0xffffffff00000000UL;
786                         val |= fsr;
787                         task_thread_info(target)->xfsr[0] = val;
788                 }
789         }
790
791         fprs |= (FPRS_FEF | FPRS_DL);
792         task_thread_info(target)->fpsaved[0] = fprs;
793
794         if (!ret)
795                 ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
796                                                 34 * sizeof(u32), -1);
797         return ret;
798 }
799
800 static const struct user_regset sparc32_regsets[] = {
801         /* Format is:
802          *      G0 --> G7
803          *      O0 --> O7
804          *      L0 --> L7
805          *      I0 --> I7
806          *      PSR, PC, nPC, Y, WIM, TBR
807          */
808         [REGSET_GENERAL] = {
809                 .core_note_type = NT_PRSTATUS,
810                 .n = 38,
811                 .size = sizeof(u32), .align = sizeof(u32),
812                 .get = genregs32_get, .set = genregs32_set
813         },
814         /* Format is:
815          *      F0 --> F31
816          *      empty 32-bit word
817          *      FSR (32--bit word)
818          *      FPU QUEUE COUNT (8-bit char)
819          *      FPU QUEUE ENTRYSIZE (8-bit char)
820          *      FPU ENABLED (8-bit char)
821          *      empty 8-bit char
822          *      FPU QUEUE (64 32-bit ints)
823          */
824         [REGSET_FP] = {
825                 .core_note_type = NT_PRFPREG,
826                 .n = 99,
827                 .size = sizeof(u32), .align = sizeof(u32),
828                 .get = fpregs32_get, .set = fpregs32_set
829         },
830 };
831
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)
835 };
836 #endif /* CONFIG_COMPAT */
837
838 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
839 {
840 #ifdef CONFIG_COMPAT
841         if (test_tsk_thread_flag(task, TIF_32BIT))
842                 return &user_sparc32_view;
843 #endif
844         return &user_sparc64_view;
845 }
846
847 #ifdef CONFIG_COMPAT
848 struct compat_fps {
849         unsigned int regs[32];
850         unsigned int fsr;
851         unsigned int flags;
852         unsigned int extra;
853         unsigned int fpqd;
854         struct compat_fq {
855                 unsigned int insnaddr;
856                 unsigned int insn;
857         } fpq[16];
858 };
859
860 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
861                         compat_ulong_t caddr, compat_ulong_t cdata)
862 {
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;
870         int ret;
871
872         pregs = (struct pt_regs32 __user *) addr;
873         fps = (struct compat_fps __user *) addr;
874
875         switch (request) {
876         case PTRACE_PEEKUSR:
877                 ret = (addr != 0) ? -EIO : 0;
878                 break;
879
880         case PTRACE_GETREGS:
881                 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
882                                           32 * sizeof(u32),
883                                           4 * sizeof(u32),
884                                           &pregs->psr);
885                 if (!ret)
886                         ret = copy_regset_to_user(child, view, REGSET_GENERAL,
887                                                   1 * sizeof(u32),
888                                                   15 * sizeof(u32),
889                                                   &pregs->u_regs[0]);
890                 break;
891
892         case PTRACE_SETREGS:
893                 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
894                                             32 * sizeof(u32),
895                                             4 * sizeof(u32),
896                                             &pregs->psr);
897                 if (!ret)
898                         ret = copy_regset_from_user(child, view, REGSET_GENERAL,
899                                                     1 * sizeof(u32),
900                                                     15 * sizeof(u32),
901                                                     &pregs->u_regs[0]);
902                 break;
903
904         case PTRACE_GETFPREGS:
905                 ret = copy_regset_to_user(child, view, REGSET_FP,
906                                           0 * sizeof(u32),
907                                           32 * sizeof(u32),
908                                           &fps->regs[0]);
909                 if (!ret)
910                         ret = copy_regset_to_user(child, view, REGSET_FP,
911                                                   33 * sizeof(u32),
912                                                   1 * sizeof(u32),
913                                                   &fps->fsr);
914                 if (!ret) {
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)))
919                                 ret = -EFAULT;
920                 }
921                 break;
922
923         case PTRACE_SETFPREGS:
924                 ret = copy_regset_from_user(child, view, REGSET_FP,
925                                             0 * sizeof(u32),
926                                             32 * sizeof(u32),
927                                             &fps->regs[0]);
928                 if (!ret)
929                         ret = copy_regset_from_user(child, view, REGSET_FP,
930                                                     33 * sizeof(u32),
931                                                     1 * sizeof(u32),
932                                                     &fps->fsr);
933                 break;
934
935         case PTRACE_READTEXT:
936         case PTRACE_READDATA:
937                 ret = ptrace_readdata(child, addr,
938                                       (char __user *)addr2, data);
939                 if (ret == data)
940                         ret = 0;
941                 else if (ret >= 0)
942                         ret = -EIO;
943                 break;
944
945         case PTRACE_WRITETEXT:
946         case PTRACE_WRITEDATA:
947                 ret = ptrace_writedata(child, (char __user *) addr2,
948                                        addr, data);
949                 if (ret == data)
950                         ret = 0;
951                 else if (ret >= 0)
952                         ret = -EIO;
953                 break;
954
955         default:
956                 if (request == PTRACE_SPARC_DETACH)
957                         request = PTRACE_DETACH;
958                 ret = compat_ptrace_request(child, request, addr, data);
959                 break;
960         }
961
962         return ret;
963 }
964 #endif /* CONFIG_COMPAT */
965
966 struct fps {
967         unsigned int regs[64];
968         unsigned long fsr;
969 };
970
971 long arch_ptrace(struct task_struct *child, long request,
972                  unsigned long addr, unsigned long data)
973 {
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;
978         void __user *addr2p;
979         int ret;
980
981         pregs = (struct pt_regs __user *) addr;
982         fps = (struct fps __user *) addr;
983         addr2p = (void __user *) addr2;
984
985         switch (request) {
986         case PTRACE_PEEKUSR:
987                 ret = (addr != 0) ? -EIO : 0;
988                 break;
989
990         case PTRACE_GETREGS64:
991                 ret = copy_regset_to_user(child, view, REGSET_GENERAL,
992                                           1 * sizeof(u64),
993                                           15 * sizeof(u64),
994                                           &pregs->u_regs[0]);
995                 if (!ret) {
996                         /* XXX doesn't handle 'y' register correctly XXX */
997                         ret = copy_regset_to_user(child, view, REGSET_GENERAL,
998                                                   32 * sizeof(u64),
999                                                   4 * sizeof(u64),
1000                                                   &pregs->tstate);
1001                 }
1002                 break;
1003
1004         case PTRACE_SETREGS64:
1005                 ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1006                                             1 * sizeof(u64),
1007                                             15 * sizeof(u64),
1008                                             &pregs->u_regs[0]);
1009                 if (!ret) {
1010                         /* XXX doesn't handle 'y' register correctly XXX */
1011                         ret = copy_regset_from_user(child, view, REGSET_GENERAL,
1012                                                     32 * sizeof(u64),
1013                                                     4 * sizeof(u64),
1014                                                     &pregs->tstate);
1015                 }
1016                 break;
1017
1018         case PTRACE_GETFPREGS64:
1019                 ret = copy_regset_to_user(child, view, REGSET_FP,
1020                                           0 * sizeof(u64),
1021                                           33 * sizeof(u64),
1022                                           fps);
1023                 break;
1024
1025         case PTRACE_SETFPREGS64:
1026                 ret = copy_regset_from_user(child, view, REGSET_FP,
1027                                           0 * sizeof(u64),
1028                                           33 * sizeof(u64),
1029                                           fps);
1030                 break;
1031
1032         case PTRACE_READTEXT:
1033         case PTRACE_READDATA:
1034                 ret = ptrace_readdata(child, addr, addr2p, data);
1035                 if (ret == data)
1036                         ret = 0;
1037                 else if (ret >= 0)
1038                         ret = -EIO;
1039                 break;
1040
1041         case PTRACE_WRITETEXT:
1042         case PTRACE_WRITEDATA:
1043                 ret = ptrace_writedata(child, addr2p, addr, data);
1044                 if (ret == data)
1045                         ret = 0;
1046                 else if (ret >= 0)
1047                         ret = -EIO;
1048                 break;
1049
1050         default:
1051                 if (request == PTRACE_SPARC_DETACH)
1052                         request = PTRACE_DETACH;
1053                 ret = ptrace_request(child, request, addr, data);
1054                 break;
1055         }
1056
1057         return ret;
1058 }
1059
1060 asmlinkage int syscall_trace_enter(struct pt_regs *regs)
1061 {
1062         int ret = 0;
1063
1064         /* do the secure computing check first */
1065         secure_computing_strict(regs->u_regs[UREG_G1]);
1066
1067         if (test_thread_flag(TIF_NOHZ))
1068                 user_exit();
1069
1070         if (test_thread_flag(TIF_SYSCALL_TRACE))
1071                 ret = tracehook_report_syscall_entry(regs);
1072
1073         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1074                 trace_sys_enter(regs, regs->u_regs[UREG_G1]);
1075
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]);
1079
1080         return ret;
1081 }
1082
1083 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
1084 {
1085         if (test_thread_flag(TIF_NOHZ))
1086                 user_exit();
1087
1088         audit_syscall_exit(regs);
1089
1090         if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
1091                 trace_sys_exit(regs, regs->u_regs[UREG_I0]);
1092
1093         if (test_thread_flag(TIF_SYSCALL_TRACE))
1094                 tracehook_report_syscall_exit(regs, 0);
1095
1096         if (test_thread_flag(TIF_NOHZ))
1097                 user_enter();
1098 }