[PATCH] sparc64: Reduce ptrace cache flushing
[linux-2.6.git] / arch / sparc64 / kernel / ptrace.c
blob5f080cf04b33343886b4de0e33525ced699f5783
1 /* ptrace.c: Sparc process tracing support.
3 * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
6 * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
7 * and David Mosberger.
9 * Added Linux support -miguel (weird, eh?, the original code was meant
10 * to emulate SunOS).
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>
23 #include <asm/asi.h>
24 #include <asm/pgtable.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/psrcompat.h>
28 #include <asm/visasm.h>
29 #include <asm/spitfire.h>
31 /* Returning from ptrace is a bit tricky because the syscall return
32 * low level code assumes any value returned which is negative and
33 * is a valid errno will mean setting the condition codes to indicate
34 * an error return. This doesn't work, so we have this hook.
36 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
38 regs->u_regs[UREG_I0] = error;
39 regs->tstate |= (TSTATE_ICARRY | TSTATE_XCARRY);
40 regs->tpc = regs->tnpc;
41 regs->tnpc += 4;
44 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
46 regs->u_regs[UREG_I0] = value;
47 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
48 regs->tpc = regs->tnpc;
49 regs->tnpc += 4;
52 static inline void
53 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, void __user *addr)
55 if (test_thread_flag(TIF_32BIT)) {
56 if (put_user(value, (unsigned int __user *) addr)) {
57 pt_error_return(regs, EFAULT);
58 return;
60 } else {
61 if (put_user(value, (long __user *) addr)) {
62 pt_error_return(regs, EFAULT);
63 return;
66 regs->u_regs[UREG_I0] = 0;
67 regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
68 regs->tpc = regs->tnpc;
69 regs->tnpc += 4;
72 static void
73 pt_os_succ_return (struct pt_regs *regs, unsigned long val, void __user *addr)
75 if (current->personality == PER_SUNOS)
76 pt_succ_return (regs, val);
77 else
78 pt_succ_return_linux (regs, val, addr);
81 /* #define ALLOW_INIT_TRACING */
82 /* #define DEBUG_PTRACE */
84 #ifdef DEBUG_PTRACE
85 char *pt_rq [] = {
86 /* 0 */ "TRACEME", "PEEKTEXT", "PEEKDATA", "PEEKUSR",
87 /* 4 */ "POKETEXT", "POKEDATA", "POKEUSR", "CONT",
88 /* 8 */ "KILL", "SINGLESTEP", "SUNATTACH", "SUNDETACH",
89 /* 12 */ "GETREGS", "SETREGS", "GETFPREGS", "SETFPREGS",
90 /* 16 */ "READDATA", "WRITEDATA", "READTEXT", "WRITETEXT",
91 /* 20 */ "GETFPAREGS", "SETFPAREGS", "unknown", "unknown",
92 /* 24 */ "SYSCALL", ""
94 #endif
97 * Called by kernel/ptrace.c when detaching..
99 * Make sure single step bits etc are not set.
101 void ptrace_disable(struct task_struct *child)
103 /* nothing to do */
106 /* To get the necessary page struct, access_process_vm() first calls
107 * get_user_pages(). This has done a flush_dcache_page() on the
108 * accessed page. Then our caller (copy_{to,from}_user_page()) did
109 * to memcpy to read/write the data from that page.
111 * Now, the only thing we have to do is:
112 * 1) flush the D-cache if it's possible than an illegal alias
113 * has been created
114 * 2) flush the I-cache if this is pre-cheetah and we did a write
116 void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
117 unsigned long uaddr, void *kaddr,
118 unsigned long len, int write)
120 BUG_ON(len > PAGE_SIZE);
122 #ifdef DCACHE_ALIASING_POSSIBLE
123 /* If bit 13 of the kernel address we used to access the
124 * user page is the same as the virtual address that page
125 * is mapped to in the user's address space, we can skip the
126 * D-cache flush.
128 if ((uaddr ^ kaddr) & (1UL << 13)) {
129 unsigned long start = __pa(kaddr);
130 unsigned long end = start + len;
132 if (tlb_type == spitfire) {
133 for (; start < end; start += 32)
134 spitfire_put_dcache_tag(va & 0x3fe0, 0x0);
135 } else {
136 for (; start < end; start += 32)
137 __asm__ __volatile__(
138 "stxa %%g0, [%0] %1\n\t"
139 "membar #Sync"
140 : /* no outputs */
141 : "r" (va),
142 "i" (ASI_DCACHE_INVALIDATE));
145 #endif
146 if (write && tlb_type == spitfire) {
147 unsigned long start = (unsigned long) kaddr;
148 unsigned long end = start + len;
150 for (; start < end; start += 32)
151 flushi(start);
155 asmlinkage void do_ptrace(struct pt_regs *regs)
157 int request = regs->u_regs[UREG_I0];
158 pid_t pid = regs->u_regs[UREG_I1];
159 unsigned long addr = regs->u_regs[UREG_I2];
160 unsigned long data = regs->u_regs[UREG_I3];
161 unsigned long addr2 = regs->u_regs[UREG_I4];
162 struct task_struct *child;
163 int ret;
165 if (test_thread_flag(TIF_32BIT)) {
166 addr &= 0xffffffffUL;
167 data &= 0xffffffffUL;
168 addr2 &= 0xffffffffUL;
170 lock_kernel();
171 #ifdef DEBUG_PTRACE
173 char *s;
175 if ((request >= 0) && (request <= 24))
176 s = pt_rq [request];
177 else
178 s = "unknown";
180 if (request == PTRACE_POKEDATA && data == 0x91d02001){
181 printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
182 pid, addr, addr2);
183 } else
184 printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
185 s, request, pid, addr, data, addr2);
187 #endif
188 if (request == PTRACE_TRACEME) {
189 int ret;
191 /* are we already being traced? */
192 if (current->ptrace & PT_PTRACED) {
193 pt_error_return(regs, EPERM);
194 goto out;
196 ret = security_ptrace(current->parent, current);
197 if (ret) {
198 pt_error_return(regs, -ret);
199 goto out;
202 /* set the ptrace bit in the process flags. */
203 current->ptrace |= PT_PTRACED;
204 pt_succ_return(regs, 0);
205 goto out;
207 #ifndef ALLOW_INIT_TRACING
208 if (pid == 1) {
209 /* Can't dork with init. */
210 pt_error_return(regs, EPERM);
211 goto out;
213 #endif
214 read_lock(&tasklist_lock);
215 child = find_task_by_pid(pid);
216 if (child)
217 get_task_struct(child);
218 read_unlock(&tasklist_lock);
220 if (!child) {
221 pt_error_return(regs, ESRCH);
222 goto out;
225 if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
226 || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
227 if (ptrace_attach(child)) {
228 pt_error_return(regs, EPERM);
229 goto out_tsk;
231 pt_succ_return(regs, 0);
232 goto out_tsk;
235 ret = ptrace_check_attach(child, request == PTRACE_KILL);
236 if (ret < 0) {
237 pt_error_return(regs, -ret);
238 goto out_tsk;
241 if (!(test_thread_flag(TIF_32BIT)) &&
242 ((request == PTRACE_READDATA64) ||
243 (request == PTRACE_WRITEDATA64) ||
244 (request == PTRACE_READTEXT64) ||
245 (request == PTRACE_WRITETEXT64) ||
246 (request == PTRACE_PEEKTEXT64) ||
247 (request == PTRACE_POKETEXT64) ||
248 (request == PTRACE_PEEKDATA64) ||
249 (request == PTRACE_POKEDATA64))) {
250 addr = regs->u_regs[UREG_G2];
251 addr2 = regs->u_regs[UREG_G3];
252 request -= 30; /* wheee... */
255 switch(request) {
256 case PTRACE_PEEKTEXT: /* read word at location addr. */
257 case PTRACE_PEEKDATA: {
258 unsigned long tmp64;
259 unsigned int tmp32;
260 int res, copied;
262 res = -EIO;
263 if (test_thread_flag(TIF_32BIT)) {
264 copied = access_process_vm(child, addr,
265 &tmp32, sizeof(tmp32), 0);
266 tmp64 = (unsigned long) tmp32;
267 if (copied == sizeof(tmp32))
268 res = 0;
269 } else {
270 copied = access_process_vm(child, addr,
271 &tmp64, sizeof(tmp64), 0);
272 if (copied == sizeof(tmp64))
273 res = 0;
275 if (res < 0)
276 pt_error_return(regs, -res);
277 else
278 pt_os_succ_return(regs, tmp64, (void __user *) data);
279 goto out_tsk;
282 case PTRACE_POKETEXT: /* write the word at location addr. */
283 case PTRACE_POKEDATA: {
284 unsigned long tmp64;
285 unsigned int tmp32;
286 int copied, res = -EIO;
288 if (test_thread_flag(TIF_32BIT)) {
289 tmp32 = data;
290 copied = access_process_vm(child, addr,
291 &tmp32, sizeof(tmp32), 1);
292 if (copied == sizeof(tmp32))
293 res = 0;
294 } else {
295 tmp64 = data;
296 copied = access_process_vm(child, addr,
297 &tmp64, sizeof(tmp64), 1);
298 if (copied == sizeof(tmp64))
299 res = 0;
301 if (res < 0)
302 pt_error_return(regs, -res);
303 else
304 pt_succ_return(regs, res);
305 goto out_tsk;
308 case PTRACE_GETREGS: {
309 struct pt_regs32 __user *pregs =
310 (struct pt_regs32 __user *) addr;
311 struct pt_regs *cregs = child->thread_info->kregs;
312 int rval;
314 if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
315 __put_user(cregs->tpc, (&pregs->pc)) ||
316 __put_user(cregs->tnpc, (&pregs->npc)) ||
317 __put_user(cregs->y, (&pregs->y))) {
318 pt_error_return(regs, EFAULT);
319 goto out_tsk;
321 for (rval = 1; rval < 16; rval++)
322 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
323 pt_error_return(regs, EFAULT);
324 goto out_tsk;
326 pt_succ_return(regs, 0);
327 #ifdef DEBUG_PTRACE
328 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
329 #endif
330 goto out_tsk;
333 case PTRACE_GETREGS64: {
334 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
335 struct pt_regs *cregs = child->thread_info->kregs;
336 unsigned long tpc = cregs->tpc;
337 int rval;
339 if ((child->thread_info->flags & _TIF_32BIT) != 0)
340 tpc &= 0xffffffff;
341 if (__put_user(cregs->tstate, (&pregs->tstate)) ||
342 __put_user(tpc, (&pregs->tpc)) ||
343 __put_user(cregs->tnpc, (&pregs->tnpc)) ||
344 __put_user(cregs->y, (&pregs->y))) {
345 pt_error_return(regs, EFAULT);
346 goto out_tsk;
348 for (rval = 1; rval < 16; rval++)
349 if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
350 pt_error_return(regs, EFAULT);
351 goto out_tsk;
353 pt_succ_return(regs, 0);
354 #ifdef DEBUG_PTRACE
355 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
356 #endif
357 goto out_tsk;
360 case PTRACE_SETREGS: {
361 struct pt_regs32 __user *pregs =
362 (struct pt_regs32 __user *) addr;
363 struct pt_regs *cregs = child->thread_info->kregs;
364 unsigned int psr, pc, npc, y;
365 int i;
367 /* Must be careful, tracing process can only set certain
368 * bits in the psr.
370 if (__get_user(psr, (&pregs->psr)) ||
371 __get_user(pc, (&pregs->pc)) ||
372 __get_user(npc, (&pregs->npc)) ||
373 __get_user(y, (&pregs->y))) {
374 pt_error_return(regs, EFAULT);
375 goto out_tsk;
377 cregs->tstate &= ~(TSTATE_ICC);
378 cregs->tstate |= psr_to_tstate_icc(psr);
379 if (!((pc | npc) & 3)) {
380 cregs->tpc = pc;
381 cregs->tnpc = npc;
383 cregs->y = y;
384 for (i = 1; i < 16; i++) {
385 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
386 pt_error_return(regs, EFAULT);
387 goto out_tsk;
390 pt_succ_return(regs, 0);
391 goto out_tsk;
394 case PTRACE_SETREGS64: {
395 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
396 struct pt_regs *cregs = child->thread_info->kregs;
397 unsigned long tstate, tpc, tnpc, y;
398 int i;
400 /* Must be careful, tracing process can only set certain
401 * bits in the psr.
403 if (__get_user(tstate, (&pregs->tstate)) ||
404 __get_user(tpc, (&pregs->tpc)) ||
405 __get_user(tnpc, (&pregs->tnpc)) ||
406 __get_user(y, (&pregs->y))) {
407 pt_error_return(regs, EFAULT);
408 goto out_tsk;
410 if ((child->thread_info->flags & _TIF_32BIT) != 0) {
411 tpc &= 0xffffffff;
412 tnpc &= 0xffffffff;
414 tstate &= (TSTATE_ICC | TSTATE_XCC);
415 cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
416 cregs->tstate |= tstate;
417 if (!((tpc | tnpc) & 3)) {
418 cregs->tpc = tpc;
419 cregs->tnpc = tnpc;
421 cregs->y = y;
422 for (i = 1; i < 16; i++) {
423 if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
424 pt_error_return(regs, EFAULT);
425 goto out_tsk;
428 pt_succ_return(regs, 0);
429 goto out_tsk;
432 case PTRACE_GETFPREGS: {
433 struct fps {
434 unsigned int regs[32];
435 unsigned int fsr;
436 unsigned int flags;
437 unsigned int extra;
438 unsigned int fpqd;
439 struct fq {
440 unsigned int insnaddr;
441 unsigned int insn;
442 } fpq[16];
444 struct fps __user *fps = (struct fps __user *) addr;
445 unsigned long *fpregs = child->thread_info->fpregs;
447 if (copy_to_user(&fps->regs[0], fpregs,
448 (32 * sizeof(unsigned int))) ||
449 __put_user(child->thread_info->xfsr[0], (&fps->fsr)) ||
450 __put_user(0, (&fps->fpqd)) ||
451 __put_user(0, (&fps->flags)) ||
452 __put_user(0, (&fps->extra)) ||
453 clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) {
454 pt_error_return(regs, EFAULT);
455 goto out_tsk;
457 pt_succ_return(regs, 0);
458 goto out_tsk;
461 case PTRACE_GETFPREGS64: {
462 struct fps {
463 unsigned int regs[64];
464 unsigned long fsr;
466 struct fps __user *fps = (struct fps __user *) addr;
467 unsigned long *fpregs = child->thread_info->fpregs;
469 if (copy_to_user(&fps->regs[0], fpregs,
470 (64 * sizeof(unsigned int))) ||
471 __put_user(child->thread_info->xfsr[0], (&fps->fsr))) {
472 pt_error_return(regs, EFAULT);
473 goto out_tsk;
475 pt_succ_return(regs, 0);
476 goto out_tsk;
479 case PTRACE_SETFPREGS: {
480 struct fps {
481 unsigned int regs[32];
482 unsigned int fsr;
483 unsigned int flags;
484 unsigned int extra;
485 unsigned int fpqd;
486 struct fq {
487 unsigned int insnaddr;
488 unsigned int insn;
489 } fpq[16];
491 struct fps __user *fps = (struct fps __user *) addr;
492 unsigned long *fpregs = child->thread_info->fpregs;
493 unsigned fsr;
495 if (copy_from_user(fpregs, &fps->regs[0],
496 (32 * sizeof(unsigned int))) ||
497 __get_user(fsr, (&fps->fsr))) {
498 pt_error_return(regs, EFAULT);
499 goto out_tsk;
501 child->thread_info->xfsr[0] &= 0xffffffff00000000UL;
502 child->thread_info->xfsr[0] |= fsr;
503 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
504 child->thread_info->gsr[0] = 0;
505 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL);
506 pt_succ_return(regs, 0);
507 goto out_tsk;
510 case PTRACE_SETFPREGS64: {
511 struct fps {
512 unsigned int regs[64];
513 unsigned long fsr;
515 struct fps __user *fps = (struct fps __user *) addr;
516 unsigned long *fpregs = child->thread_info->fpregs;
518 if (copy_from_user(fpregs, &fps->regs[0],
519 (64 * sizeof(unsigned int))) ||
520 __get_user(child->thread_info->xfsr[0], (&fps->fsr))) {
521 pt_error_return(regs, EFAULT);
522 goto out_tsk;
524 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
525 child->thread_info->gsr[0] = 0;
526 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU);
527 pt_succ_return(regs, 0);
528 goto out_tsk;
531 case PTRACE_READTEXT:
532 case PTRACE_READDATA: {
533 int res = ptrace_readdata(child, addr,
534 (char __user *)addr2, data);
535 if (res == data) {
536 pt_succ_return(regs, 0);
537 goto out_tsk;
539 if (res >= 0)
540 res = -EIO;
541 pt_error_return(regs, -res);
542 goto out_tsk;
545 case PTRACE_WRITETEXT:
546 case PTRACE_WRITEDATA: {
547 int res = ptrace_writedata(child, (char __user *) addr2,
548 addr, data);
549 if (res == data) {
550 pt_succ_return(regs, 0);
551 goto out_tsk;
553 if (res >= 0)
554 res = -EIO;
555 pt_error_return(regs, -res);
556 goto out_tsk;
558 case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
559 addr = 1;
561 case PTRACE_CONT: { /* restart after signal. */
562 if (data > _NSIG) {
563 pt_error_return(regs, EIO);
564 goto out_tsk;
567 if (request == PTRACE_SYSCALL) {
568 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
569 } else {
570 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
573 child->exit_code = data;
574 #ifdef DEBUG_PTRACE
575 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm,
576 child->pid, child->exit_code,
577 child->thread_info->kregs->tpc,
578 child->thread_info->kregs->tnpc);
580 #endif
581 wake_up_process(child);
582 pt_succ_return(regs, 0);
583 goto out_tsk;
587 * make the child exit. Best I can do is send it a sigkill.
588 * perhaps it should be put in the status that it wants to
589 * exit.
591 case PTRACE_KILL: {
592 if (child->exit_state == EXIT_ZOMBIE) { /* already dead */
593 pt_succ_return(regs, 0);
594 goto out_tsk;
596 child->exit_code = SIGKILL;
597 wake_up_process(child);
598 pt_succ_return(regs, 0);
599 goto out_tsk;
602 case PTRACE_SUNDETACH: { /* detach a process that was attached. */
603 int error = ptrace_detach(child, data);
604 if (error) {
605 pt_error_return(regs, EIO);
606 goto out_tsk;
608 pt_succ_return(regs, 0);
609 goto out_tsk;
612 /* PTRACE_DUMPCORE unsupported... */
614 default: {
615 int err = ptrace_request(child, request, addr, data);
616 if (err)
617 pt_error_return(regs, -err);
618 else
619 pt_succ_return(regs, 0);
620 goto out_tsk;
623 out_tsk:
624 if (child)
625 put_task_struct(child);
626 out:
627 unlock_kernel();
630 asmlinkage void syscall_trace(void)
632 #ifdef DEBUG_PTRACE
633 printk("%s [%d]: syscall_trace\n", current->comm, current->pid);
634 #endif
635 if (!test_thread_flag(TIF_SYSCALL_TRACE))
636 return;
637 if (!(current->ptrace & PT_PTRACED))
638 return;
639 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
640 ? 0x80 : 0));
643 * this isn't the same as continuing with a signal, but it will do
644 * for normal use. strace only continues with a signal if the
645 * stopping signal is not SIGTRAP. -brl
647 #ifdef DEBUG_PTRACE
648 printk("%s [%d]: syscall_trace exit= %x\n", current->comm,
649 current->pid, current->exit_code);
650 #endif
651 if (current->exit_code) {
652 send_sig (current->exit_code, current, 1);
653 current->exit_code = 0;