blob: c831d426c4ac8aea807f525e1f519b72b180fb5d [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];
690 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
David S. Miller97753692008-02-07 03:00:17 -0800692#if 1
693 printk(KERN_INFO
694 "arch_ptrace: request[%ld] addr[%lx] data[%lx] addr2[%lx]\n",
695 request, addr, data, addr2);
696#endif
697 if (test_thread_flag(TIF_32BIT))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 addr2 &= 0xffffffffUL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 switch(request) {
David S. Miller1759e582006-04-01 23:28:10 -0800701 case PTRACE_PEEKUSR:
David S. Miller97753692008-02-07 03:00:17 -0800702 ret = (addr != 0) ? -EIO : 0;
703 break;
David S. Miller1759e582006-04-01 23:28:10 -0800704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 case PTRACE_PEEKTEXT: /* read word at location addr. */
706 case PTRACE_PEEKDATA: {
707 unsigned long tmp64;
708 unsigned int tmp32;
David S. Miller97753692008-02-07 03:00:17 -0800709 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
David S. Miller97753692008-02-07 03:00:17 -0800711 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (test_thread_flag(TIF_32BIT)) {
713 copied = access_process_vm(child, addr,
714 &tmp32, sizeof(tmp32), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (copied == sizeof(tmp32))
David S. Miller97753692008-02-07 03:00:17 -0800716 ret = put_user(tmp32,
717 (unsigned int __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
719 copied = access_process_vm(child, addr,
720 &tmp64, sizeof(tmp64), 0);
721 if (copied == sizeof(tmp64))
David S. Miller97753692008-02-07 03:00:17 -0800722 ret = put_user(tmp64,
723 (unsigned long __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
David S. Miller97753692008-02-07 03:00:17 -0800725 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 case PTRACE_POKETEXT: /* write the word at location addr. */
729 case PTRACE_POKEDATA: {
730 unsigned long tmp64;
731 unsigned int tmp32;
David S. Miller97753692008-02-07 03:00:17 -0800732 int copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
David S. Miller97753692008-02-07 03:00:17 -0800734 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (test_thread_flag(TIF_32BIT)) {
736 tmp32 = data;
737 copied = access_process_vm(child, addr,
738 &tmp32, sizeof(tmp32), 1);
739 if (copied == sizeof(tmp32))
David S. Miller97753692008-02-07 03:00:17 -0800740 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } else {
742 tmp64 = data;
743 copied = access_process_vm(child, addr,
744 &tmp64, sizeof(tmp64), 1);
745 if (copied == sizeof(tmp64))
David S. Miller97753692008-02-07 03:00:17 -0800746 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
David S. Miller97753692008-02-07 03:00:17 -0800748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750
751 case PTRACE_GETREGS: {
752 struct pt_regs32 __user *pregs =
753 (struct pt_regs32 __user *) addr;
Al Viro26ecbde2006-01-12 01:05:43 -0800754 struct pt_regs *cregs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
David S. Miller97753692008-02-07 03:00:17 -0800756 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
758 __put_user(cregs->tpc, (&pregs->pc)) ||
759 __put_user(cregs->tnpc, (&pregs->npc)) ||
David S. Miller97753692008-02-07 03:00:17 -0800760 __put_user(cregs->y, (&pregs->y)))
761 break;
762 for (i = 1; i < 16; i++) {
763 if (__put_user(cregs->u_regs[i],
764 (&pregs->u_regs[i - 1])))
765 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
David S. Miller97753692008-02-07 03:00:17 -0800767 if (i == 16)
768 ret = 0;
769 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
772 case PTRACE_GETREGS64: {
773 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Al Viro26ecbde2006-01-12 01:05:43 -0800774 struct pt_regs *cregs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 unsigned long tpc = cregs->tpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Al Virof3169642006-01-12 01:05:42 -0800777 if ((task_thread_info(child)->flags & _TIF_32BIT) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 tpc &= 0xffffffff;
David S. Miller97753692008-02-07 03:00:17 -0800779
780 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (__put_user(cregs->tstate, (&pregs->tstate)) ||
782 __put_user(tpc, (&pregs->tpc)) ||
783 __put_user(cregs->tnpc, (&pregs->tnpc)) ||
David S. Miller97753692008-02-07 03:00:17 -0800784 __put_user(cregs->y, (&pregs->y)))
785 break;
786 for (i = 1; i < 16; i++) {
787 if (__put_user(cregs->u_regs[i],
788 (&pregs->u_regs[i - 1])))
789 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
David S. Miller97753692008-02-07 03:00:17 -0800791 if (i == 16)
792 ret = 0;
793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 case PTRACE_SETREGS: {
797 struct pt_regs32 __user *pregs =
798 (struct pt_regs32 __user *) addr;
Al Viro26ecbde2006-01-12 01:05:43 -0800799 struct pt_regs *cregs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 unsigned int psr, pc, npc, y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Must be careful, tracing process can only set certain
803 * bits in the psr.
804 */
David S. Miller97753692008-02-07 03:00:17 -0800805 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (__get_user(psr, (&pregs->psr)) ||
807 __get_user(pc, (&pregs->pc)) ||
808 __get_user(npc, (&pregs->npc)) ||
David S. Miller97753692008-02-07 03:00:17 -0800809 __get_user(y, (&pregs->y)))
810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 cregs->tstate &= ~(TSTATE_ICC);
812 cregs->tstate |= psr_to_tstate_icc(psr);
813 if (!((pc | npc) & 3)) {
814 cregs->tpc = pc;
815 cregs->tnpc = npc;
816 }
817 cregs->y = y;
818 for (i = 1; i < 16; i++) {
David S. Miller97753692008-02-07 03:00:17 -0800819 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1])))
820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
David S. Miller97753692008-02-07 03:00:17 -0800822 if (i == 16)
823 ret = 0;
824 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
827 case PTRACE_SETREGS64: {
828 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
Al Viro26ecbde2006-01-12 01:05:43 -0800829 struct pt_regs *cregs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 unsigned long tstate, tpc, tnpc, y;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /* Must be careful, tracing process can only set certain
833 * bits in the psr.
834 */
David S. Miller97753692008-02-07 03:00:17 -0800835 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (__get_user(tstate, (&pregs->tstate)) ||
837 __get_user(tpc, (&pregs->tpc)) ||
838 __get_user(tnpc, (&pregs->tnpc)) ||
David S. Miller97753692008-02-07 03:00:17 -0800839 __get_user(y, (&pregs->y)))
840 break;
Al Virof3169642006-01-12 01:05:42 -0800841 if ((task_thread_info(child)->flags & _TIF_32BIT) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 tpc &= 0xffffffff;
843 tnpc &= 0xffffffff;
844 }
845 tstate &= (TSTATE_ICC | TSTATE_XCC);
846 cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
847 cregs->tstate |= tstate;
848 if (!((tpc | tnpc) & 3)) {
849 cregs->tpc = tpc;
850 cregs->tnpc = tnpc;
851 }
852 cregs->y = y;
853 for (i = 1; i < 16; i++) {
David S. Miller97753692008-02-07 03:00:17 -0800854 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1])))
855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
David S. Miller97753692008-02-07 03:00:17 -0800857 if (i == 16)
858 ret = 0;
859 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
862 case PTRACE_GETFPREGS: {
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;
Al Virof3169642006-01-12 01:05:42 -0800875 unsigned long *fpregs = task_thread_info(child)->fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
David S. Miller97753692008-02-07 03:00:17 -0800877 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (copy_to_user(&fps->regs[0], fpregs,
879 (32 * sizeof(unsigned int))) ||
Al Virof3169642006-01-12 01:05:42 -0800880 __put_user(task_thread_info(child)->xfsr[0], (&fps->fsr)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 __put_user(0, (&fps->fpqd)) ||
882 __put_user(0, (&fps->flags)) ||
883 __put_user(0, (&fps->extra)) ||
David S. Miller97753692008-02-07 03:00:17 -0800884 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int)))
885 break;
886
887 ret = 0;
888 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
891 case PTRACE_GETFPREGS64: {
892 struct fps {
893 unsigned int regs[64];
894 unsigned long fsr;
895 };
896 struct fps __user *fps = (struct fps __user *) addr;
Al Virof3169642006-01-12 01:05:42 -0800897 unsigned long *fpregs = task_thread_info(child)->fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
David S. Miller97753692008-02-07 03:00:17 -0800899 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (copy_to_user(&fps->regs[0], fpregs,
901 (64 * sizeof(unsigned int))) ||
David S. Miller97753692008-02-07 03:00:17 -0800902 __put_user(task_thread_info(child)->xfsr[0], (&fps->fsr)))
903 break;
904
905 ret = 0;
906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
909 case PTRACE_SETFPREGS: {
910 struct fps {
911 unsigned int regs[32];
912 unsigned int fsr;
913 unsigned int flags;
914 unsigned int extra;
915 unsigned int fpqd;
916 struct fq {
917 unsigned int insnaddr;
918 unsigned int insn;
919 } fpq[16];
920 };
921 struct fps __user *fps = (struct fps __user *) addr;
Al Virof3169642006-01-12 01:05:42 -0800922 unsigned long *fpregs = task_thread_info(child)->fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned fsr;
924
David S. Miller97753692008-02-07 03:00:17 -0800925 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (copy_from_user(fpregs, &fps->regs[0],
927 (32 * sizeof(unsigned int))) ||
David S. Miller97753692008-02-07 03:00:17 -0800928 __get_user(fsr, (&fps->fsr)))
929 break;
930
Al Virof3169642006-01-12 01:05:42 -0800931 task_thread_info(child)->xfsr[0] &= 0xffffffff00000000UL;
932 task_thread_info(child)->xfsr[0] |= fsr;
933 if (!(task_thread_info(child)->fpsaved[0] & FPRS_FEF))
934 task_thread_info(child)->gsr[0] = 0;
935 task_thread_info(child)->fpsaved[0] |= (FPRS_FEF | FPRS_DL);
David S. Miller97753692008-02-07 03:00:17 -0800936 ret = 0;
937 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 case PTRACE_SETFPREGS64: {
941 struct fps {
942 unsigned int regs[64];
943 unsigned long fsr;
944 };
945 struct fps __user *fps = (struct fps __user *) addr;
Al Virof3169642006-01-12 01:05:42 -0800946 unsigned long *fpregs = task_thread_info(child)->fpregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
David S. Miller97753692008-02-07 03:00:17 -0800948 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (copy_from_user(fpregs, &fps->regs[0],
950 (64 * sizeof(unsigned int))) ||
David S. Miller97753692008-02-07 03:00:17 -0800951 __get_user(task_thread_info(child)->xfsr[0], (&fps->fsr)))
952 break;
953
Al Virof3169642006-01-12 01:05:42 -0800954 if (!(task_thread_info(child)->fpsaved[0] & FPRS_FEF))
955 task_thread_info(child)->gsr[0] = 0;
David S. Miller97753692008-02-07 03:00:17 -0800956 task_thread_info(child)->fpsaved[0] |=
957 (FPRS_FEF | FPRS_DL | FPRS_DU);
958 ret = 0;
David S. Miller731bbe42006-04-04 16:54:40 -0700959 break;
960 }
961
David S. Miller97753692008-02-07 03:00:17 -0800962 case PTRACE_READTEXT:
963 case PTRACE_READDATA:
964 ret = ptrace_readdata(child, addr,
965 (char __user *)addr2, data);
966 if (ret == data)
967 ret = 0;
968 else if (ret >= 0)
969 ret = -EIO;
970 break;
971
972 case PTRACE_WRITETEXT:
973 case PTRACE_WRITEDATA:
974 ret = ptrace_writedata(child, (char __user *) addr2,
975 addr, data);
976 if (ret == data)
977 ret = 0;
978 else if (ret >= 0)
979 ret = -EIO;
980 break;
981
982 case PTRACE_GETEVENTMSG: {
983 if (test_thread_flag(TIF_32BIT))
984 ret = put_user(child->ptrace_message,
985 (unsigned int __user *) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 else
David S. Miller97753692008-02-07 03:00:17 -0800987 ret = put_user(child->ptrace_message,
988 (unsigned long __user *) data);
989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
David S. Miller97753692008-02-07 03:00:17 -0800991
992 default:
993 ret = ptrace_request(child, request, addr, data);
994 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
David S. Miller97753692008-02-07 03:00:17 -0800996
997 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
David S. Miller8d8a6472005-07-10 16:55:48 -07001000asmlinkage void syscall_trace(struct pt_regs *regs, int syscall_exit_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
David S. Millerbb49bcd2005-07-10 16:49:28 -07001002 /* do the secure computing check first */
David S. Miller8d8a6472005-07-10 16:55:48 -07001003 secure_computing(regs->u_regs[UREG_G1]);
David S. Millerbb49bcd2005-07-10 16:49:28 -07001004
David S. Millerf7ceba32005-07-10 19:29:45 -07001005 if (unlikely(current->audit_context) && syscall_exit_p) {
1006 unsigned long tstate = regs->tstate;
1007 int result = AUDITSC_SUCCESS;
1008
1009 if (unlikely(tstate & (TSTATE_XCARRY | TSTATE_ICARRY)))
1010 result = AUDITSC_FAILURE;
1011
Al Viro5411be52006-03-29 20:23:36 -05001012 audit_syscall_exit(result, regs->u_regs[UREG_I0]);
David S. Millerf7ceba32005-07-10 19:29:45 -07001013 }
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!(current->ptrace & PT_PTRACED))
David S. Millerf7ceba32005-07-10 19:29:45 -07001016 goto out;
1017
1018 if (!test_thread_flag(TIF_SYSCALL_TRACE))
1019 goto out;
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
1022 ? 0x80 : 0));
1023
1024 /*
1025 * this isn't the same as continuing with a signal, but it will do
1026 * for normal use. strace only continues with a signal if the
1027 * stopping signal is not SIGTRAP. -brl
1028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (current->exit_code) {
David S. Millerbb49bcd2005-07-10 16:49:28 -07001030 send_sig(current->exit_code, current, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 current->exit_code = 0;
1032 }
David S. Millerf7ceba32005-07-10 19:29:45 -07001033
1034out:
1035 if (unlikely(current->audit_context) && !syscall_exit_p)
Al Viro5411be52006-03-29 20:23:36 -05001036 audit_syscall_entry((test_thread_flag(TIF_32BIT) ?
David S. Millerf7ceba32005-07-10 19:29:45 -07001037 AUDIT_ARCH_SPARC :
1038 AUDIT_ARCH_SPARC64),
1039 regs->u_regs[UREG_G1],
1040 regs->u_regs[UREG_I0],
1041 regs->u_regs[UREG_I1],
1042 regs->u_regs[UREG_I2],
1043 regs->u_regs[UREG_I3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044}