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