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