[PATCH] sparc: Fix PTRACE_CONT bogosity
[linux-2.6.git] / arch / sparc64 / kernel / ptrace.c
1 /* ptrace.c: Sparc process tracing support.
2  *
3  * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
4  * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  *
6  * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
7  * and David Mosberger.
8  *
9  * Added Linux support -miguel (weird, eh?, the original code was meant
10  * to emulate SunOS).
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/errno.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/security.h>
22
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>
30
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.
35  */
36 static inline void pt_error_return(struct pt_regs *regs, unsigned long error)
37 {
38         regs->u_regs[UREG_I0] = error;
39         regs->tstate |= (TSTATE_ICARRY | TSTATE_XCARRY);
40         regs->tpc = regs->tnpc;
41         regs->tnpc += 4;
42 }
43
44 static inline void pt_succ_return(struct pt_regs *regs, unsigned long value)
45 {
46         regs->u_regs[UREG_I0] = value;
47         regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
48         regs->tpc = regs->tnpc;
49         regs->tnpc += 4;
50 }
51
52 static inline void
53 pt_succ_return_linux(struct pt_regs *regs, unsigned long value, void __user *addr)
54 {
55         if (test_thread_flag(TIF_32BIT)) {
56                 if (put_user(value, (unsigned int __user *) addr)) {
57                         pt_error_return(regs, EFAULT);
58                         return;
59                 }
60         } else {
61                 if (put_user(value, (long __user *) addr)) {
62                         pt_error_return(regs, EFAULT);
63                         return;
64                 }
65         }
66         regs->u_regs[UREG_I0] = 0;
67         regs->tstate &= ~(TSTATE_ICARRY | TSTATE_XCARRY);
68         regs->tpc = regs->tnpc;
69         regs->tnpc += 4;
70 }
71
72 static void
73 pt_os_succ_return (struct pt_regs *regs, unsigned long val, void __user *addr)
74 {
75         if (current->personality == PER_SUNOS)
76                 pt_succ_return (regs, val);
77         else
78                 pt_succ_return_linux (regs, val, addr);
79 }
80
81 /* #define ALLOW_INIT_TRACING */
82 /* #define DEBUG_PTRACE */
83
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", ""
93 };
94 #endif
95
96 /*
97  * Called by kernel/ptrace.c when detaching..
98  *
99  * Make sure single step bits etc are not set.
100  */
101 void ptrace_disable(struct task_struct *child)
102 {
103         /* nothing to do */
104 }
105
106 asmlinkage void do_ptrace(struct pt_regs *regs)
107 {
108         int request = regs->u_regs[UREG_I0];
109         pid_t pid = regs->u_regs[UREG_I1];
110         unsigned long addr = regs->u_regs[UREG_I2];
111         unsigned long data = regs->u_regs[UREG_I3];
112         unsigned long addr2 = regs->u_regs[UREG_I4];
113         struct task_struct *child;
114         int ret;
115
116         if (test_thread_flag(TIF_32BIT)) {
117                 addr &= 0xffffffffUL;
118                 data &= 0xffffffffUL;
119                 addr2 &= 0xffffffffUL;
120         }
121         lock_kernel();
122 #ifdef DEBUG_PTRACE
123         {
124                 char *s;
125
126                 if ((request >= 0) && (request <= 24))
127                         s = pt_rq [request];
128                 else
129                         s = "unknown";
130
131                 if (request == PTRACE_POKEDATA && data == 0x91d02001){
132                         printk ("do_ptrace: breakpoint pid=%d, addr=%016lx addr2=%016lx\n",
133                                 pid, addr, addr2);
134                 } else 
135                         printk("do_ptrace: rq=%s(%d) pid=%d addr=%016lx data=%016lx addr2=%016lx\n",
136                                s, request, pid, addr, data, addr2);
137         }
138 #endif
139         if (request == PTRACE_TRACEME) {
140                 int ret;
141
142                 /* are we already being traced? */
143                 if (current->ptrace & PT_PTRACED) {
144                         pt_error_return(regs, EPERM);
145                         goto out;
146                 }
147                 ret = security_ptrace(current->parent, current);
148                 if (ret) {
149                         pt_error_return(regs, -ret);
150                         goto out;
151                 }
152
153                 /* set the ptrace bit in the process flags. */
154                 current->ptrace |= PT_PTRACED;
155                 pt_succ_return(regs, 0);
156                 goto out;
157         }
158 #ifndef ALLOW_INIT_TRACING
159         if (pid == 1) {
160                 /* Can't dork with init. */
161                 pt_error_return(regs, EPERM);
162                 goto out;
163         }
164 #endif
165         read_lock(&tasklist_lock);
166         child = find_task_by_pid(pid);
167         if (child)
168                 get_task_struct(child);
169         read_unlock(&tasklist_lock);
170
171         if (!child) {
172                 pt_error_return(regs, ESRCH);
173                 goto out;
174         }
175
176         if ((current->personality == PER_SUNOS && request == PTRACE_SUNATTACH)
177             || (current->personality != PER_SUNOS && request == PTRACE_ATTACH)) {
178                 if (ptrace_attach(child)) {
179                         pt_error_return(regs, EPERM);
180                         goto out_tsk;
181                 }
182                 pt_succ_return(regs, 0);
183                 goto out_tsk;
184         }
185
186         ret = ptrace_check_attach(child, request == PTRACE_KILL);
187         if (ret < 0) {
188                 pt_error_return(regs, -ret);
189                 goto out_tsk;
190         }
191
192         if (!(test_thread_flag(TIF_32BIT))      &&
193             ((request == PTRACE_READDATA64)             ||
194              (request == PTRACE_WRITEDATA64)            ||
195              (request == PTRACE_READTEXT64)             ||
196              (request == PTRACE_WRITETEXT64)            ||
197              (request == PTRACE_PEEKTEXT64)             ||
198              (request == PTRACE_POKETEXT64)             ||
199              (request == PTRACE_PEEKDATA64)             ||
200              (request == PTRACE_POKEDATA64))) {
201                 addr = regs->u_regs[UREG_G2];
202                 addr2 = regs->u_regs[UREG_G3];
203                 request -= 30; /* wheee... */
204         }
205
206         switch(request) {
207         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
208         case PTRACE_PEEKDATA: {
209                 unsigned long tmp64;
210                 unsigned int tmp32;
211                 int res, copied;
212
213                 res = -EIO;
214                 if (test_thread_flag(TIF_32BIT)) {
215                         copied = access_process_vm(child, addr,
216                                                    &tmp32, sizeof(tmp32), 0);
217                         tmp64 = (unsigned long) tmp32;
218                         if (copied == sizeof(tmp32))
219                                 res = 0;
220                 } else {
221                         copied = access_process_vm(child, addr,
222                                                    &tmp64, sizeof(tmp64), 0);
223                         if (copied == sizeof(tmp64))
224                                 res = 0;
225                 }
226                 if (res < 0)
227                         pt_error_return(regs, -res);
228                 else
229                         pt_os_succ_return(regs, tmp64, (void __user *) data);
230                 goto flush_and_out;
231         }
232
233         case PTRACE_POKETEXT: /* write the word at location addr. */
234         case PTRACE_POKEDATA: {
235                 unsigned long tmp64;
236                 unsigned int tmp32;
237                 int copied, res = -EIO;
238
239                 if (test_thread_flag(TIF_32BIT)) {
240                         tmp32 = data;
241                         copied = access_process_vm(child, addr,
242                                                    &tmp32, sizeof(tmp32), 1);
243                         if (copied == sizeof(tmp32))
244                                 res = 0;
245                 } else {
246                         tmp64 = data;
247                         copied = access_process_vm(child, addr,
248                                                    &tmp64, sizeof(tmp64), 1);
249                         if (copied == sizeof(tmp64))
250                                 res = 0;
251                 }
252                 if (res < 0)
253                         pt_error_return(regs, -res);
254                 else
255                         pt_succ_return(regs, res);
256                 goto flush_and_out;
257         }
258
259         case PTRACE_GETREGS: {
260                 struct pt_regs32 __user *pregs =
261                         (struct pt_regs32 __user *) addr;
262                 struct pt_regs *cregs = child->thread_info->kregs;
263                 int rval;
264
265                 if (__put_user(tstate_to_psr(cregs->tstate), (&pregs->psr)) ||
266                     __put_user(cregs->tpc, (&pregs->pc)) ||
267                     __put_user(cregs->tnpc, (&pregs->npc)) ||
268                     __put_user(cregs->y, (&pregs->y))) {
269                         pt_error_return(regs, EFAULT);
270                         goto out_tsk;
271                 }
272                 for (rval = 1; rval < 16; rval++)
273                         if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
274                                 pt_error_return(regs, EFAULT);
275                                 goto out_tsk;
276                         }
277                 pt_succ_return(regs, 0);
278 #ifdef DEBUG_PTRACE
279                 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
280 #endif
281                 goto out_tsk;
282         }
283
284         case PTRACE_GETREGS64: {
285                 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
286                 struct pt_regs *cregs = child->thread_info->kregs;
287                 unsigned long tpc = cregs->tpc;
288                 int rval;
289
290                 if ((child->thread_info->flags & _TIF_32BIT) != 0)
291                         tpc &= 0xffffffff;
292                 if (__put_user(cregs->tstate, (&pregs->tstate)) ||
293                     __put_user(tpc, (&pregs->tpc)) ||
294                     __put_user(cregs->tnpc, (&pregs->tnpc)) ||
295                     __put_user(cregs->y, (&pregs->y))) {
296                         pt_error_return(regs, EFAULT);
297                         goto out_tsk;
298                 }
299                 for (rval = 1; rval < 16; rval++)
300                         if (__put_user(cregs->u_regs[rval], (&pregs->u_regs[rval - 1]))) {
301                                 pt_error_return(regs, EFAULT);
302                                 goto out_tsk;
303                         }
304                 pt_succ_return(regs, 0);
305 #ifdef DEBUG_PTRACE
306                 printk ("PC=%lx nPC=%lx o7=%lx\n", cregs->tpc, cregs->tnpc, cregs->u_regs [15]);
307 #endif
308                 goto out_tsk;
309         }
310
311         case PTRACE_SETREGS: {
312                 struct pt_regs32 __user *pregs =
313                         (struct pt_regs32 __user *) addr;
314                 struct pt_regs *cregs = child->thread_info->kregs;
315                 unsigned int psr, pc, npc, y;
316                 int i;
317
318                 /* Must be careful, tracing process can only set certain
319                  * bits in the psr.
320                  */
321                 if (__get_user(psr, (&pregs->psr)) ||
322                     __get_user(pc, (&pregs->pc)) ||
323                     __get_user(npc, (&pregs->npc)) ||
324                     __get_user(y, (&pregs->y))) {
325                         pt_error_return(regs, EFAULT);
326                         goto out_tsk;
327                 }
328                 cregs->tstate &= ~(TSTATE_ICC);
329                 cregs->tstate |= psr_to_tstate_icc(psr);
330                 if (!((pc | npc) & 3)) {
331                         cregs->tpc = pc;
332                         cregs->tnpc = npc;
333                 }
334                 cregs->y = y;
335                 for (i = 1; i < 16; i++) {
336                         if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
337                                 pt_error_return(regs, EFAULT);
338                                 goto out_tsk;
339                         }
340                 }
341                 pt_succ_return(regs, 0);
342                 goto out_tsk;
343         }
344
345         case PTRACE_SETREGS64: {
346                 struct pt_regs __user *pregs = (struct pt_regs __user *) addr;
347                 struct pt_regs *cregs = child->thread_info->kregs;
348                 unsigned long tstate, tpc, tnpc, y;
349                 int i;
350
351                 /* Must be careful, tracing process can only set certain
352                  * bits in the psr.
353                  */
354                 if (__get_user(tstate, (&pregs->tstate)) ||
355                     __get_user(tpc, (&pregs->tpc)) ||
356                     __get_user(tnpc, (&pregs->tnpc)) ||
357                     __get_user(y, (&pregs->y))) {
358                         pt_error_return(regs, EFAULT);
359                         goto out_tsk;
360                 }
361                 if ((child->thread_info->flags & _TIF_32BIT) != 0) {
362                         tpc &= 0xffffffff;
363                         tnpc &= 0xffffffff;
364                 }
365                 tstate &= (TSTATE_ICC | TSTATE_XCC);
366                 cregs->tstate &= ~(TSTATE_ICC | TSTATE_XCC);
367                 cregs->tstate |= tstate;
368                 if (!((tpc | tnpc) & 3)) {
369                         cregs->tpc = tpc;
370                         cregs->tnpc = tnpc;
371                 }
372                 cregs->y = y;
373                 for (i = 1; i < 16; i++) {
374                         if (__get_user(cregs->u_regs[i], (&pregs->u_regs[i-1]))) {
375                                 pt_error_return(regs, EFAULT);
376                                 goto out_tsk;
377                         }
378                 }
379                 pt_succ_return(regs, 0);
380                 goto out_tsk;
381         }
382
383         case PTRACE_GETFPREGS: {
384                 struct fps {
385                         unsigned int regs[32];
386                         unsigned int fsr;
387                         unsigned int flags;
388                         unsigned int extra;
389                         unsigned int fpqd;
390                         struct fq {
391                                 unsigned int insnaddr;
392                                 unsigned int insn;
393                         } fpq[16];
394                 };
395                 struct fps __user *fps = (struct fps __user *) addr;
396                 unsigned long *fpregs = child->thread_info->fpregs;
397
398                 if (copy_to_user(&fps->regs[0], fpregs,
399                                  (32 * sizeof(unsigned int))) ||
400                     __put_user(child->thread_info->xfsr[0], (&fps->fsr)) ||
401                     __put_user(0, (&fps->fpqd)) ||
402                     __put_user(0, (&fps->flags)) ||
403                     __put_user(0, (&fps->extra)) ||
404                     clear_user(&fps->fpq[0], 32 * sizeof(unsigned int))) {
405                         pt_error_return(regs, EFAULT);
406                         goto out_tsk;
407                 }
408                 pt_succ_return(regs, 0);
409                 goto out_tsk;
410         }
411
412         case PTRACE_GETFPREGS64: {
413                 struct fps {
414                         unsigned int regs[64];
415                         unsigned long fsr;
416                 };
417                 struct fps __user *fps = (struct fps __user *) addr;
418                 unsigned long *fpregs = child->thread_info->fpregs;
419
420                 if (copy_to_user(&fps->regs[0], fpregs,
421                                  (64 * sizeof(unsigned int))) ||
422                     __put_user(child->thread_info->xfsr[0], (&fps->fsr))) {
423                         pt_error_return(regs, EFAULT);
424                         goto out_tsk;
425                 }
426                 pt_succ_return(regs, 0);
427                 goto out_tsk;
428         }
429
430         case PTRACE_SETFPREGS: {
431                 struct fps {
432                         unsigned int regs[32];
433                         unsigned int fsr;
434                         unsigned int flags;
435                         unsigned int extra;
436                         unsigned int fpqd;
437                         struct fq {
438                                 unsigned int insnaddr;
439                                 unsigned int insn;
440                         } fpq[16];
441                 };
442                 struct fps __user *fps = (struct fps __user *) addr;
443                 unsigned long *fpregs = child->thread_info->fpregs;
444                 unsigned fsr;
445
446                 if (copy_from_user(fpregs, &fps->regs[0],
447                                    (32 * sizeof(unsigned int))) ||
448                     __get_user(fsr, (&fps->fsr))) {
449                         pt_error_return(regs, EFAULT);
450                         goto out_tsk;
451                 }
452                 child->thread_info->xfsr[0] &= 0xffffffff00000000UL;
453                 child->thread_info->xfsr[0] |= fsr;
454                 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
455                         child->thread_info->gsr[0] = 0;
456                 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL);
457                 pt_succ_return(regs, 0);
458                 goto out_tsk;
459         }
460
461         case PTRACE_SETFPREGS64: {
462                 struct fps {
463                         unsigned int regs[64];
464                         unsigned long fsr;
465                 };
466                 struct fps __user *fps = (struct fps __user *) addr;
467                 unsigned long *fpregs = child->thread_info->fpregs;
468
469                 if (copy_from_user(fpregs, &fps->regs[0],
470                                    (64 * sizeof(unsigned int))) ||
471                     __get_user(child->thread_info->xfsr[0], (&fps->fsr))) {
472                         pt_error_return(regs, EFAULT);
473                         goto out_tsk;
474                 }
475                 if (!(child->thread_info->fpsaved[0] & FPRS_FEF))
476                         child->thread_info->gsr[0] = 0;
477                 child->thread_info->fpsaved[0] |= (FPRS_FEF | FPRS_DL | FPRS_DU);
478                 pt_succ_return(regs, 0);
479                 goto out_tsk;
480         }
481
482         case PTRACE_READTEXT:
483         case PTRACE_READDATA: {
484                 int res = ptrace_readdata(child, addr,
485                                           (char __user *)addr2, data);
486                 if (res == data) {
487                         pt_succ_return(regs, 0);
488                         goto flush_and_out;
489                 }
490                 if (res >= 0)
491                         res = -EIO;
492                 pt_error_return(regs, -res);
493                 goto flush_and_out;
494         }
495
496         case PTRACE_WRITETEXT:
497         case PTRACE_WRITEDATA: {
498                 int res = ptrace_writedata(child, (char __user *) addr2,
499                                            addr, data);
500                 if (res == data) {
501                         pt_succ_return(regs, 0);
502                         goto flush_and_out;
503                 }
504                 if (res >= 0)
505                         res = -EIO;
506                 pt_error_return(regs, -res);
507                 goto flush_and_out;
508         }
509         case PTRACE_SYSCALL: /* continue and stop at (return from) syscall */
510                 addr = 1;
511
512         case PTRACE_CONT: { /* restart after signal. */
513                 if (data > _NSIG) {
514                         pt_error_return(regs, EIO);
515                         goto out_tsk;
516                 }
517
518                 if (request == PTRACE_SYSCALL) {
519                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
520                 } else {
521                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
522                 }
523
524                 child->exit_code = data;
525 #ifdef DEBUG_PTRACE
526                 printk("CONT: %s [%d]: set exit_code = %x %lx %lx\n", child->comm,
527                         child->pid, child->exit_code,
528                         child->thread_info->kregs->tpc,
529                         child->thread_info->kregs->tnpc);
530                        
531 #endif
532                 wake_up_process(child);
533                 pt_succ_return(regs, 0);
534                 goto out_tsk;
535         }
536
537 /*
538  * make the child exit.  Best I can do is send it a sigkill. 
539  * perhaps it should be put in the status that it wants to 
540  * exit.
541  */
542         case PTRACE_KILL: {
543                 if (child->exit_state == EXIT_ZOMBIE) { /* already dead */
544                         pt_succ_return(regs, 0);
545                         goto out_tsk;
546                 }
547                 child->exit_code = SIGKILL;
548                 wake_up_process(child);
549                 pt_succ_return(regs, 0);
550                 goto out_tsk;
551         }
552
553         case PTRACE_SUNDETACH: { /* detach a process that was attached. */
554                 int error = ptrace_detach(child, data);
555                 if (error) {
556                         pt_error_return(regs, EIO);
557                         goto out_tsk;
558                 }
559                 pt_succ_return(regs, 0);
560                 goto out_tsk;
561         }
562
563         /* PTRACE_DUMPCORE unsupported... */
564
565         default: {
566                 int err = ptrace_request(child, request, addr, data);
567                 if (err)
568                         pt_error_return(regs, -err);
569                 else
570                         pt_succ_return(regs, 0);
571                 goto out_tsk;
572         }
573         }
574 flush_and_out:
575         {
576                 unsigned long va;
577
578                 if (tlb_type == cheetah || tlb_type == cheetah_plus) {
579                         for (va = 0; va < (1 << 16); va += (1 << 5))
580                                 spitfire_put_dcache_tag(va, 0x0);
581                         /* No need to mess with I-cache on Cheetah. */
582                 } else {
583                         for (va =  0; va < L1DCACHE_SIZE; va += 32)
584                                 spitfire_put_dcache_tag(va, 0x0);
585                         if (request == PTRACE_PEEKTEXT ||
586                             request == PTRACE_POKETEXT ||
587                             request == PTRACE_READTEXT ||
588                             request == PTRACE_WRITETEXT) {
589                                 for (va =  0; va < (PAGE_SIZE << 1); va += 32)
590                                         spitfire_put_icache_tag(va, 0x0);
591                                 __asm__ __volatile__("flush %g6");
592                         }
593                 }
594         }
595 out_tsk:
596         if (child)
597                 put_task_struct(child);
598 out:
599         unlock_kernel();
600 }
601
602 asmlinkage void syscall_trace(void)
603 {
604 #ifdef DEBUG_PTRACE
605         printk("%s [%d]: syscall_trace\n", current->comm, current->pid);
606 #endif
607         if (!test_thread_flag(TIF_SYSCALL_TRACE))
608                 return;
609         if (!(current->ptrace & PT_PTRACED))
610                 return;
611         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
612                                  ? 0x80 : 0));
613
614         /*
615          * this isn't the same as continuing with a signal, but it will do
616          * for normal use.  strace only continues with a signal if the
617          * stopping signal is not SIGTRAP.  -brl
618          */
619 #ifdef DEBUG_PTRACE
620         printk("%s [%d]: syscall_trace exit= %x\n", current->comm,
621                 current->pid, current->exit_code);
622 #endif
623         if (current->exit_code) {
624                 send_sig (current->exit_code, current, 1);
625                 current->exit_code = 0;
626         }
627 }