blob: 51f012410f9d1118197ba4480da764eac6b3622b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ptrace.c: Sparc process tracing support.
2 *
David S. Millerd09c2a22008-02-06 23:02:08 -08003 * Copyright (C) 1996, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
David S. Millerf7ceba32005-07-10 19:29:45 -070022#include <linux/seccomp.h>
23#include <linux/audit.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070024#include <linux/signal.h>
David S. Millerd09c2a22008-02-06 23:02:08 -080025#include <linux/regset.h>
26#include <linux/compat.h>
27#include <linux/elf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
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>
David S. Miller6a9b4902005-09-19 20:11:57 -070036#include <asm/page.h>
David S. Miller717463d2005-09-29 18:50:34 -070037#include <asm/cpudata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039/* #define ALLOW_INIT_TRACING */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/*
42 * Called by kernel/ptrace.c when detaching..
43 *
44 * Make sure single step bits etc are not set.
45 */
46void ptrace_disable(struct task_struct *child)
47{
48 /* nothing to do */
49}
50
David S. Millerdadeafd2005-04-17 18:03:11 -070051/* 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 */
61void 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
David S. Miller7adb37f2006-02-17 15:07:43 -080067 if (tlb_type == hypervisor)
68 return;
69
David S. Millerdadeafd2005-04-17 18:03:11 -070070#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 */
David S. Miller6a9b4902005-09-19 20:11:57 -070076 if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
David S. Millerdadeafd2005-04-17 18:03:11 -070077 unsigned long start = __pa(kaddr);
78 unsigned long end = start + len;
David S. Miller717463d2005-09-29 18:50:34 -070079 unsigned long dcache_line_size;
80
81 dcache_line_size = local_cpu_data().dcache_line_size;
David S. Millerdadeafd2005-04-17 18:03:11 -070082
83 if (tlb_type == spitfire) {
David S. Miller717463d2005-09-29 18:50:34 -070084 for (; start < end; start += dcache_line_size)
David S. Miller6a9b4902005-09-19 20:11:57 -070085 spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
David S. Millerdadeafd2005-04-17 18:03:11 -070086 } else {
David S. Miller717463d2005-09-29 18:50:34 -070087 start &= ~(dcache_line_size - 1);
88 for (; start < end; start += dcache_line_size)
David S. Millerdadeafd2005-04-17 18:03:11 -070089 __asm__ __volatile__(
90 "stxa %%g0, [%0] %1\n\t"
91 "membar #Sync"
92 : /* no outputs */
David S. Miller6a9b4902005-09-19 20:11:57 -070093 : "r" (start),
David S. Millerdadeafd2005-04-17 18:03:11 -070094 "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;
David S. Miller717463d2005-09-29 18:50:34 -0700101 unsigned long icache_line_size;
David S. Millerdadeafd2005-04-17 18:03:11 -0700102
David S. Miller717463d2005-09-29 18:50:34 -0700103 icache_line_size = local_cpu_data().icache_line_size;
104
105 for (; start < end; start += icache_line_size)
David S. Millerdadeafd2005-04-17 18:03:11 -0700106 flushi(start);
107 }
108}
109
David S. Millerd09c2a22008-02-06 23:02:08 -0800110enum sparc_regset {
111 REGSET_GENERAL,
112 REGSET_FP,
113};
114
115static 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
167static 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
242static 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
309static 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
352static 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
380static 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
385static 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 }
453finish:
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
461static 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 }
535finish:
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
543static 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
597static 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
643static 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
675static 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
680const 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
David S. Miller97753692008-02-07 03:00:17 -0800687long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
David S. Miller97753692008-02-07 03:00:17 -0800689 long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
David S. Miller94732722008-02-07 05:06:12 -0800690 const struct user_regset_view *view;
691 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
David S. Miller97753692008-02-07 03:00:17 -0800693 if (test_thread_flag(TIF_32BIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 addr2 &= 0xffffffffUL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
David S. Miller94732722008-02-07 05:06:12 -0800696 view = task_user_regset_view(child);
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 switch(request) {
David S. Miller1759e582006-04-01 23:28:10 -0800699 case PTRACE_PEEKUSR:
David S. Miller97753692008-02-07 03:00:17 -0800700 ret = (addr != 0) ? -EIO : 0;
701 break;
David S. Miller1759e582006-04-01 23:28:10 -0800702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 case PTRACE_PEEKTEXT: /* read word at location addr. */
704 case PTRACE_PEEKDATA: {
705 unsigned long tmp64;
706 unsigned int tmp32;
David S. Miller97753692008-02-07 03:00:17 -0800707 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
David S. Miller97753692008-02-07 03:00:17 -0800709 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if (test_thread_flag(TIF_32BIT)) {
711 copied = access_process_vm(child, addr,
712 &tmp32, sizeof(tmp32), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (copied == sizeof(tmp32))
David S. Miller97753692008-02-07 03:00:17 -0800714 ret = put_user(tmp32,
715 (unsigned int __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
717 copied = access_process_vm(child, addr,
718 &tmp64, sizeof(tmp64), 0);
719 if (copied == sizeof(tmp64))
David S. Miller97753692008-02-07 03:00:17 -0800720 ret = put_user(tmp64,
721 (unsigned long __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
David S. Miller97753692008-02-07 03:00:17 -0800723 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 case PTRACE_POKETEXT: /* write the word at location addr. */
727 case PTRACE_POKEDATA: {
728 unsigned long tmp64;
729 unsigned int tmp32;
David S. Miller97753692008-02-07 03:00:17 -0800730 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
David S. Miller97753692008-02-07 03:00:17 -0800732 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 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))
David S. Miller97753692008-02-07 03:00:17 -0800738 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 } else {
740 tmp64 = data;
741 copied = access_process_vm(child, addr,
742 &tmp64, sizeof(tmp64), 1);
743 if (copied == sizeof(tmp64))
David S. Miller97753692008-02-07 03:00:17 -0800744 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
David S. Miller97753692008-02-07 03:00:17 -0800746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 case PTRACE_GETREGS: {
750 struct pt_regs32 __user *pregs =
751 (struct pt_regs32 __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
David S. Miller94732722008-02-07 05:06:12 -0800753 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]);
David S. Miller97753692008-02-07 03:00:17 -0800762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 case PTRACE_GETREGS64: {
766 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
David S. Miller94732722008-02-07 05:06:12 -0800768 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 }
David S. Miller97753692008-02-07 03:00:17 -0800779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 }
781
782 case PTRACE_SETREGS: {
783 struct pt_regs32 __user *pregs =
784 (struct pt_regs32 __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
David S. Miller94732722008-02-07 05:06:12 -0800786 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]);
David S. Miller97753692008-02-07 03:00:17 -0800795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797
798 case PTRACE_SETREGS64: {
799 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
David S. Miller94732722008-02-07 05:06:12 -0800801 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
David S. Miller97753692008-02-07 03:00:17 -0800812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
David S. Miller94732722008-02-07 05:06:12 -0800829 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 }
David S. Miller97753692008-02-07 03:00:17 -0800845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
David S. Miller94732722008-02-07 05:06:12 -0800855 ret = copy_regset_to_user(child, view, REGSET_FP,
856 0 * sizeof(u64),
857 33 * sizeof(u64),
858 fps);
David S. Miller97753692008-02-07 03:00:17 -0800859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
David S. Miller94732722008-02-07 05:06:12 -0800876 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);
David S. Miller97753692008-02-07 03:00:17 -0800885 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
David S. Miller94732722008-02-07 05:06:12 -0800895 ret = copy_regset_to_user(child, view, REGSET_FP,
896 0 * sizeof(u64),
897 33 * sizeof(u64),
898 fps);
David S. Miller731bbe42006-04-04 16:54:40 -0700899 break;
900 }
901
David S. Miller97753692008-02-07 03:00:17 -0800902 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 else
David S. Miller97753692008-02-07 03:00:17 -0800927 ret = put_user(child->ptrace_message,
928 (unsigned long __user *) data);
929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
David S. Miller97753692008-02-07 03:00:17 -0800931
932 default:
933 ret = ptrace_request(child, request, addr, data);
934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
David S. Miller97753692008-02-07 03:00:17 -0800936
937 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
David S. Miller8d8a6472005-07-10 16:55:48 -0700940asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
David S. Millerbb49bcd2005-07-10 16:49:28 -0700942 /* do the secure computing check first */
David S. Miller8d8a6472005-07-10 16:55:48 -0700943 secure_computing(regs->u_regs[UREG_G1]);
David S. Millerbb49bcd2005-07-10 16:49:28 -0700944
David S. Millerf7ceba32005-07-10 19:29:45 -0700945 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
Al Viro5411be52006-03-29 20:23:36 -0500952 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
David S. Millerf7ceba32005-07-10 19:29:45 -0700953 }
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (!(current->ptrace & PT_PTRACED))
David S. Millerf7ceba32005-07-10 19:29:45 -0700956 goto out;
957
958 if (!test_thread_flag(TIF_SYSCALL_TRACE))
959 goto out;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (current->exit_code) {
David S. Millerbb49bcd2005-07-10 16:49:28 -0700970 send_sig(current->exit_code, current, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 current->exit_code = 0;
972 }
David S. Millerf7ceba32005-07-10 19:29:45 -0700973
974out:
975 if (unlikely(current->audit_context) && !syscall_exit_p)
Al Viro5411be52006-03-29 20:23:36 -0500976 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
David S. Millerf7ceba32005-07-10 19:29:45 -0700977 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}