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