Pull altix-mmr into release branch
[sfrench/cifs-2.6.git] / arch / parisc / kernel / ptrace.c
1 /*
2  * Kernel support for the ptrace() and syscall tracing interfaces.
3  *
4  * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
5  * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
6  * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
7  */
8
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/smp.h>
13 #include <linux/smp_lock.h>
14 #include <linux/errno.h>
15 #include <linux/ptrace.h>
16 #include <linux/user.h>
17 #include <linux/personality.h>
18 #include <linux/security.h>
19 #include <linux/compat.h>
20 #include <linux/signal.h>
21
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
26 #include <asm/asm-offsets.h>
27
28 /* PSW bits we allow the debugger to modify */
29 #define USER_PSW_BITS   (PSW_N | PSW_V | PSW_CB)
30
31 #undef DEBUG_PTRACE
32
33 #ifdef DEBUG_PTRACE
34 #define DBG(x...)       printk(x)
35 #else
36 #define DBG(x...)
37 #endif
38
39 #ifdef __LP64__
40
41 /* This function is needed to translate 32 bit pt_regs offsets in to
42  * 64 bit pt_regs offsets.  For example, a 32 bit gdb under a 64 bit kernel
43  * will request offset 12 if it wants gr3, but the lower 32 bits of
44  * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
45  * This code relies on a 32 bit pt_regs being comprised of 32 bit values
46  * except for the fp registers which (a) are 64 bits, and (b) follow
47  * the gr registers at the start of pt_regs.  The 32 bit pt_regs should
48  * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
49  * being 64 bit in both cases.
50  */
51
52 static long translate_usr_offset(long offset)
53 {
54         if (offset < 0)
55                 return -1;
56         else if (offset <= 32*4)        /* gr[0..31] */
57                 return offset * 2 + 4;
58         else if (offset <= 32*4+32*8)   /* gr[0..31] + fr[0..31] */
59                 return offset + 32*4;
60         else if (offset < sizeof(struct pt_regs)/2 + 32*4)
61                 return offset * 2 + 4 - 32*8;
62         else
63                 return -1;
64 }
65 #endif
66
67 /*
68  * Called by kernel/ptrace.c when detaching..
69  *
70  * Make sure single step bits etc are not set.
71  */
72 void ptrace_disable(struct task_struct *child)
73 {
74         /* make sure the trap bits are not set */
75         pa_psw(child)->r = 0;
76         pa_psw(child)->t = 0;
77         pa_psw(child)->h = 0;
78         pa_psw(child)->l = 0;
79 }
80
81 long sys_ptrace(long request, pid_t pid, long addr, long data)
82 {
83         struct task_struct *child;
84         long ret;
85 #ifdef DEBUG_PTRACE
86         long oaddr=addr, odata=data;
87 #endif
88
89         lock_kernel();
90         ret = -EPERM;
91         if (request == PTRACE_TRACEME) {
92                 /* are we already being traced? */
93                 if (current->ptrace & PT_PTRACED)
94                         goto out;
95
96                 ret = security_ptrace(current->parent, current);
97                 if (ret) 
98                         goto out;
99
100                 /* set the ptrace bit in the process flags. */
101                 current->ptrace |= PT_PTRACED;
102                 ret = 0;
103                 goto out;
104         }
105
106         ret = -ESRCH;
107         read_lock(&tasklist_lock);
108         child = find_task_by_pid(pid);
109         if (child)
110                 get_task_struct(child);
111         read_unlock(&tasklist_lock);
112         if (!child)
113                 goto out;
114         ret = -EPERM;
115         if (pid == 1)           /* no messing around with init! */
116                 goto out_tsk;
117
118         if (request == PTRACE_ATTACH) {
119                 ret = ptrace_attach(child);
120                 goto out_tsk;
121         }
122
123         ret = ptrace_check_attach(child, request == PTRACE_KILL);
124         if (ret < 0)
125                 goto out_tsk;
126
127         switch (request) {
128         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
129         case PTRACE_PEEKDATA: {
130                 int copied;
131
132 #ifdef __LP64__
133                 if (is_compat_task(child)) {
134                         unsigned int tmp;
135
136                         addr &= 0xffffffffL;
137                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
138                         ret = -EIO;
139                         if (copied != sizeof(tmp))
140                                 goto out_tsk;
141                         ret = put_user(tmp,(unsigned int *) data);
142                         DBG("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n",
143                                 request == PTRACE_PEEKTEXT ? "TEXT" : "DATA",
144                                 pid, oaddr, odata, ret, tmp);
145                 }
146                 else
147 #endif
148                 {
149                         unsigned long tmp;
150
151                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
152                         ret = -EIO;
153                         if (copied != sizeof(tmp))
154                                 goto out_tsk;
155                         ret = put_user(tmp,(unsigned long *) data);
156                 }
157                 goto out_tsk;
158         }
159
160         /* when I and D space are separate, this will have to be fixed. */
161         case PTRACE_POKETEXT: /* write the word at location addr. */
162         case PTRACE_POKEDATA:
163                 ret = 0;
164 #ifdef __LP64__
165                 if (is_compat_task(child)) {
166                         unsigned int tmp = (unsigned int)data;
167                         DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
168                                 request == PTRACE_POKETEXT ? "TEXT" : "DATA",
169                                 pid, oaddr, odata);
170                         addr &= 0xffffffffL;
171                         if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
172                                 goto out_tsk;
173                 }
174                 else
175 #endif
176                 {
177                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
178                                 goto out_tsk;
179                 }
180                 ret = -EIO;
181                 goto out_tsk;
182
183         /* Read the word at location addr in the USER area.  For ptraced
184            processes, the kernel saves all regs on a syscall. */
185         case PTRACE_PEEKUSR: {
186                 ret = -EIO;
187 #ifdef __LP64__
188                 if (is_compat_task(child)) {
189                         unsigned int tmp;
190
191                         if (addr & (sizeof(int)-1))
192                                 goto out_tsk;
193                         if ((addr = translate_usr_offset(addr)) < 0)
194                                 goto out_tsk;
195
196                         tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
197                         ret = put_user(tmp, (unsigned int *) data);
198                         DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
199                                 pid, oaddr, odata, ret, addr, tmp);
200                 }
201                 else
202 #endif
203                 {
204                         unsigned long tmp;
205
206                         if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
207                                 goto out_tsk;
208                         tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
209                         ret = put_user(tmp, (unsigned long *) data);
210                 }
211                 goto out_tsk;
212         }
213
214         /* Write the word at location addr in the USER area.  This will need
215            to change when the kernel no longer saves all regs on a syscall.
216            FIXME.  There is a problem at the moment in that r3-r18 are only
217            saved if the process is ptraced on syscall entry, and even then
218            those values are overwritten by actual register values on syscall
219            exit. */
220         case PTRACE_POKEUSR:
221                 ret = -EIO;
222                 /* Some register values written here may be ignored in
223                  * entry.S:syscall_restore_rfi; e.g. iaoq is written with
224                  * r31/r31+4, and not with the values in pt_regs.
225                  */
226                  /* PT_PSW=0, so this is valid for 32 bit processes under 64
227                  * bit kernels.
228                  */
229                 if (addr == PT_PSW) {
230                         /* PT_PSW=0, so this is valid for 32 bit processes
231                          * under 64 bit kernels.
232                          *
233                          * Allow writing to Nullify, Divide-step-correction,
234                          * and carry/borrow bits.
235                          * BEWARE, if you set N, and then single step, it won't
236                          * stop on the nullified instruction.
237                          */
238                         DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
239                                 pid, oaddr, odata);
240                         data &= USER_PSW_BITS;
241                         task_regs(child)->gr[0] &= ~USER_PSW_BITS;
242                         task_regs(child)->gr[0] |= data;
243                         ret = 0;
244                         goto out_tsk;
245                 }
246 #ifdef __LP64__
247                 if (is_compat_task(child)) {
248                         if (addr & (sizeof(int)-1))
249                                 goto out_tsk;
250                         if ((addr = translate_usr_offset(addr)) < 0)
251                                 goto out_tsk;
252                         DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
253                                 pid, oaddr, odata, addr);
254                         if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
255                                 /* Special case, fp regs are 64 bits anyway */
256                                 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
257                                 ret = 0;
258                         }
259                         else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
260                                         addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
261                                         addr == PT_SAR+4) {
262                                 /* Zero the top 32 bits */
263                                 *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
264                                 *(unsigned int *) ((char *) task_regs(child) + addr) = data;
265                                 ret = 0;
266                         }
267                         goto out_tsk;
268                 }
269                 else
270 #endif
271                 {
272                         if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
273                                 goto out_tsk;
274                         if ((addr >= PT_GR1 && addr <= PT_GR31) ||
275                                         addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
276                                         (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
277                                         addr == PT_SAR) {
278                                 *(unsigned long *) ((char *) task_regs(child) + addr) = data;
279                                 ret = 0;
280                         }
281                         goto out_tsk;
282                 }
283
284         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
285         case PTRACE_CONT:
286                 ret = -EIO;
287                 DBG("sys_ptrace(%s)\n",
288                         request == PTRACE_SYSCALL ? "SYSCALL" : "CONT");
289                 if (!valid_signal(data))
290                         goto out_tsk;
291                 child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
292                 if (request == PTRACE_SYSCALL) {
293                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
294                 } else {
295                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
296                 }               
297                 child->exit_code = data;
298                 goto out_wake_notrap;
299
300         case PTRACE_KILL:
301                 /*
302                  * make the child exit.  Best I can do is send it a
303                  * sigkill.  perhaps it should be put in the status
304                  * that it wants to exit.
305                  */
306                 DBG("sys_ptrace(KILL)\n");
307                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
308                         goto out_tsk;
309                 child->exit_code = SIGKILL;
310                 goto out_wake_notrap;
311
312         case PTRACE_SINGLEBLOCK:
313                 DBG("sys_ptrace(SINGLEBLOCK)\n");
314                 ret = -EIO;
315                 if (!valid_signal(data))
316                         goto out_tsk;
317                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
318                 child->ptrace &= ~PT_SINGLESTEP;
319                 child->ptrace |= PT_BLOCKSTEP;
320                 child->exit_code = data;
321
322                 /* Enable taken branch trap. */
323                 pa_psw(child)->r = 0;
324                 pa_psw(child)->t = 1;
325                 pa_psw(child)->h = 0;
326                 pa_psw(child)->l = 0;
327                 goto out_wake;
328
329         case PTRACE_SINGLESTEP:
330                 DBG("sys_ptrace(SINGLESTEP)\n");
331                 ret = -EIO;
332                 if (!valid_signal(data))
333                         goto out_tsk;
334
335                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
336                 child->ptrace &= ~PT_BLOCKSTEP;
337                 child->ptrace |= PT_SINGLESTEP;
338                 child->exit_code = data;
339
340                 if (pa_psw(child)->n) {
341                         struct siginfo si;
342
343                         /* Nullified, just crank over the queue. */
344                         task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
345                         task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
346                         task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
347                         pa_psw(child)->n = 0;
348                         pa_psw(child)->x = 0;
349                         pa_psw(child)->y = 0;
350                         pa_psw(child)->z = 0;
351                         pa_psw(child)->b = 0;
352                         ptrace_disable(child);
353                         /* Don't wake up the child, but let the
354                            parent know something happened. */
355                         si.si_code = TRAP_TRACE;
356                         si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3);
357                         si.si_signo = SIGTRAP;
358                         si.si_errno = 0;
359                         force_sig_info(SIGTRAP, &si, child);
360                         //notify_parent(child, SIGCHLD);
361                         //ret = 0;
362                         goto out_wake;
363                 }
364
365                 /* Enable recovery counter traps.  The recovery counter
366                  * itself will be set to zero on a task switch.  If the
367                  * task is suspended on a syscall then the syscall return
368                  * path will overwrite the recovery counter with a suitable
369                  * value such that it traps once back in user space.  We
370                  * disable interrupts in the childs PSW here also, to avoid
371                  * interrupts while the recovery counter is decrementing.
372                  */
373                 pa_psw(child)->r = 1;
374                 pa_psw(child)->t = 0;
375                 pa_psw(child)->h = 0;
376                 pa_psw(child)->l = 0;
377                 /* give it a chance to run. */
378                 goto out_wake;
379
380         case PTRACE_DETACH:
381                 ret = ptrace_detach(child, data);
382                 goto out_tsk;
383
384         case PTRACE_GETEVENTMSG:
385                 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
386                 goto out_tsk;
387
388         default:
389                 ret = ptrace_request(child, request, addr, data);
390                 goto out_tsk;
391         }
392
393 out_wake_notrap:
394         ptrace_disable(child);
395 out_wake:
396         wake_up_process(child);
397         ret = 0;
398 out_tsk:
399         put_task_struct(child);
400 out:
401         unlock_kernel();
402         DBG("sys_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
403                 request, pid, oaddr, odata, ret);
404         return ret;
405 }
406
407 void syscall_trace(void)
408 {
409         if (!test_thread_flag(TIF_SYSCALL_TRACE))
410                 return;
411         if (!(current->ptrace & PT_PTRACED))
412                 return;
413         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
414                                  ? 0x80 : 0));
415         /*
416          * this isn't the same as continuing with a signal, but it will do
417          * for normal use.  strace only continues with a signal if the
418          * stopping signal is not SIGTRAP.  -brl
419          */
420         if (current->exit_code) {
421                 send_sig(current->exit_code, current, 1);
422                 current->exit_code = 0;
423         }
424 }