[POWERPC] ptrace updates & new, better requests
[safe/jmp/linux-2.6] / arch / powerpc / kernel / ptrace.c
1 /*
2  *  PowerPC version
3  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
4  *
5  *  Derived from "arch/m68k/kernel/ptrace.c"
6  *  Copyright (C) 1994 by Hamish Macdonald
7  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
8  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
9  *
10  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
11  * and Paul Mackerras (paulus@samba.org).
12  *
13  * This file is subject to the terms and conditions of the GNU General
14  * Public License.  See the file README.legal in the main directory of
15  * this archive for more details.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/security.h>
26 #include <linux/signal.h>
27 #include <linux/seccomp.h>
28 #include <linux/audit.h>
29 #ifdef CONFIG_PPC32
30 #include <linux/module.h>
31 #endif
32
33 #include <asm/uaccess.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/system.h>
37
38 #ifdef CONFIG_PPC64
39 #include "ptrace-ppc64.h"
40 #else
41 #include "ptrace-ppc32.h"
42 #endif
43
44 #include "ptrace-common.h"
45
46 /*
47  * does not yet catch signals sent when the child dies.
48  * in exit.c or in signal.c.
49  */
50
51 /*
52  * Called by kernel/ptrace.c when detaching..
53  *
54  * Make sure single step bits etc are not set.
55  */
56 void ptrace_disable(struct task_struct *child)
57 {
58         /* make sure the single step bit is not set. */
59         clear_single_step(child);
60 }
61
62 /*
63  * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
64  * we mark them as obsolete now, they will be removed in a future version
65  */
66 static long arch_ptrace_old(struct task_struct *child, long request, long addr,
67                             long data)
68 {
69         int ret = -EPERM;
70
71         switch(request) {
72         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
73                 int i;
74                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
75                 unsigned long __user *tmp = (unsigned long __user *)addr;
76
77                 for (i = 0; i < 32; i++) {
78                         ret = put_user(*reg, tmp);
79                         if (ret)
80                                 break;
81                         reg++;
82                         tmp++;
83                 }
84                 break;
85         }
86
87         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
88                 int i;
89                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
90                 unsigned long __user *tmp = (unsigned long __user *)addr;
91
92                 for (i = 0; i < 32; i++) {
93                         ret = get_user(*reg, tmp);
94                         if (ret)
95                                 break;
96                         reg++;
97                         tmp++;
98                 }
99                 break;
100         }
101
102         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
103                 flush_fp_to_thread(child);
104                 ret = get_fpregs((void __user *)addr, child, 0);
105                 break;
106         }
107
108         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
109                 flush_fp_to_thread(child);
110                 ret = set_fpregs((void __user *)addr, child, 0);
111                 break;
112         }
113
114         }
115         return ret;
116 }
117
118 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
119 {
120         int ret = -EPERM;
121
122         switch (request) {
123         /* when I and D space are separate, these will need to be fixed. */
124         case PTRACE_PEEKTEXT: /* read word at location addr. */
125         case PTRACE_PEEKDATA: {
126                 unsigned long tmp;
127                 int copied;
128
129                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
130                 ret = -EIO;
131                 if (copied != sizeof(tmp))
132                         break;
133                 ret = put_user(tmp,(unsigned long __user *) data);
134                 break;
135         }
136
137         /* read the word at location addr in the USER area. */
138         case PTRACE_PEEKUSR: {
139                 unsigned long index, tmp;
140
141                 ret = -EIO;
142                 /* convert to index and check */
143 #ifdef CONFIG_PPC32
144                 index = (unsigned long) addr >> 2;
145                 if ((addr & 3) || (index > PT_FPSCR)
146                     || (child->thread.regs == NULL))
147 #else
148                 index = (unsigned long) addr >> 3;
149                 if ((addr & 7) || (index > PT_FPSCR))
150 #endif
151                         break;
152
153 #ifdef CONFIG_PPC32
154                 CHECK_FULL_REGS(child->thread.regs);
155 #endif
156                 if (index < PT_FPR0) {
157                         tmp = get_reg(child, (int) index);
158                 } else {
159                         flush_fp_to_thread(child);
160                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
161                 }
162                 ret = put_user(tmp,(unsigned long __user *) data);
163                 break;
164         }
165
166         /* If I and D space are separate, this will have to be fixed. */
167         case PTRACE_POKETEXT: /* write the word at location addr. */
168         case PTRACE_POKEDATA:
169                 ret = 0;
170                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
171                                 == sizeof(data))
172                         break;
173                 ret = -EIO;
174                 break;
175
176         /* write the word at location addr in the USER area */
177         case PTRACE_POKEUSR: {
178                 unsigned long index;
179
180                 ret = -EIO;
181                 /* convert to index and check */
182 #ifdef CONFIG_PPC32
183                 index = (unsigned long) addr >> 2;
184                 if ((addr & 3) || (index > PT_FPSCR)
185                     || (child->thread.regs == NULL))
186 #else
187                 index = (unsigned long) addr >> 3;
188                 if ((addr & 7) || (index > PT_FPSCR))
189 #endif
190                         break;
191
192 #ifdef CONFIG_PPC32
193                 CHECK_FULL_REGS(child->thread.regs);
194 #endif
195                 if (index == PT_ORIG_R3)
196                         break;
197                 if (index < PT_FPR0) {
198                         ret = put_reg(child, index, data);
199                 } else {
200                         flush_fp_to_thread(child);
201                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
202                         ret = 0;
203                 }
204                 break;
205         }
206
207         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
208         case PTRACE_CONT: { /* restart after signal. */
209                 ret = -EIO;
210                 if (!valid_signal(data))
211                         break;
212                 if (request == PTRACE_SYSCALL)
213                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
214                 else
215                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
216                 child->exit_code = data;
217                 /* make sure the single step bit is not set. */
218                 clear_single_step(child);
219                 wake_up_process(child);
220                 ret = 0;
221                 break;
222         }
223
224 /*
225  * make the child exit.  Best I can do is send it a sigkill.
226  * perhaps it should be put in the status that it wants to
227  * exit.
228  */
229         case PTRACE_KILL: {
230                 ret = 0;
231                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
232                         break;
233                 child->exit_code = SIGKILL;
234                 /* make sure the single step bit is not set. */
235                 clear_single_step(child);
236                 wake_up_process(child);
237                 break;
238         }
239
240         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
241                 ret = -EIO;
242                 if (!valid_signal(data))
243                         break;
244                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
245                 set_single_step(child);
246                 child->exit_code = data;
247                 /* give it a chance to run. */
248                 wake_up_process(child);
249                 ret = 0;
250                 break;
251         }
252
253 #ifdef CONFIG_PPC64
254         case PTRACE_GET_DEBUGREG: {
255                 ret = -EINVAL;
256                 /* We only support one DABR and no IABRS at the moment */
257                 if (addr > 0)
258                         break;
259                 ret = put_user(child->thread.dabr,
260                                (unsigned long __user *)data);
261                 break;
262         }
263
264         case PTRACE_SET_DEBUGREG:
265                 ret = ptrace_set_debugreg(child, addr, data);
266                 break;
267 #endif
268
269         case PTRACE_DETACH:
270                 ret = ptrace_detach(child, data);
271                 break;
272
273 #ifdef CONFIG_PPC64
274         case PTRACE_GETREGS64:
275 #endif
276         case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
277                 int ui;
278                 if (!access_ok(VERIFY_WRITE, (void __user *)data,
279                                sizeof(struct pt_regs))) {
280                         ret = -EIO;
281                         break;
282                 }
283                 ret = 0;
284                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
285                         ret |= __put_user(get_reg(child, ui),
286                                           (unsigned long __user *) data);
287                         data += sizeof(long);
288                 }
289                 break;
290         }
291
292 #ifdef CONFIG_PPC64
293         case PTRACE_SETREGS64:
294 #endif
295         case PTRACE_SETREGS: { /* Set all gp regs in the child. */
296                 unsigned long tmp;
297                 int ui;
298                 if (!access_ok(VERIFY_READ, (void __user *)data,
299                                sizeof(struct pt_regs))) {
300                         ret = -EIO;
301                         break;
302                 }
303                 ret = 0;
304                 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
305                         ret = __get_user(tmp, (unsigned long __user *) data);
306                         if (ret)
307                                 break;
308                         put_reg(child, ui, tmp);
309                         data += sizeof(long);
310                 }
311                 break;
312         }
313
314         case PTRACE_GETFPREGS: { /* Get the child FPU state (FPR0...31 + FPSCR) */
315                 flush_fp_to_thread(child);
316                 ret = get_fpregs((void __user *)data, child, 1);
317                 break;
318         }
319
320         case PTRACE_SETFPREGS: { /* Set the child FPU state (FPR0...31 + FPSCR) */
321                 flush_fp_to_thread(child);
322                 ret = set_fpregs((void __user *)data, child, 1);
323                 break;
324         }
325
326 #ifdef CONFIG_ALTIVEC
327         case PTRACE_GETVRREGS:
328                 /* Get the child altivec register state. */
329                 flush_altivec_to_thread(child);
330                 ret = get_vrregs((unsigned long __user *)data, child);
331                 break;
332
333         case PTRACE_SETVRREGS:
334                 /* Set the child altivec register state. */
335                 flush_altivec_to_thread(child);
336                 ret = set_vrregs(child, (unsigned long __user *)data);
337                 break;
338 #endif
339 #ifdef CONFIG_SPE
340         case PTRACE_GETEVRREGS:
341                 /* Get the child spe register state. */
342                 if (child->thread.regs->msr & MSR_SPE)
343                         giveup_spe(child);
344                 ret = get_evrregs((unsigned long __user *)data, child);
345                 break;
346
347         case PTRACE_SETEVRREGS:
348                 /* Set the child spe register state. */
349                 /* this is to clear the MSR_SPE bit to force a reload
350                  * of register state from memory */
351                 if (child->thread.regs->msr & MSR_SPE)
352                         giveup_spe(child);
353                 ret = set_evrregs(child, (unsigned long __user *)data);
354                 break;
355 #endif
356
357         /* Old reverse args ptrace callss */
358         case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
359         case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
360         case PPC_PTRACE_GETFPREGS: /* Get FPRs 0 - 31. */
361         case PPC_PTRACE_SETFPREGS: /* Get FPRs 0 - 31. */
362                 ret = arch_ptrace_old(child, request, addr, data);
363                 break;
364
365         default:
366                 ret = ptrace_request(child, request, addr, data);
367                 break;
368         }
369         return ret;
370 }
371
372 static void do_syscall_trace(void)
373 {
374         /* the 0x80 provides a way for the tracing parent to distinguish
375            between a syscall stop and SIGTRAP delivery */
376         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
377                                  ? 0x80 : 0));
378
379         /*
380          * this isn't the same as continuing with a signal, but it will do
381          * for normal use.  strace only continues with a signal if the
382          * stopping signal is not SIGTRAP.  -brl
383          */
384         if (current->exit_code) {
385                 send_sig(current->exit_code, current, 1);
386                 current->exit_code = 0;
387         }
388 }
389
390 void do_syscall_trace_enter(struct pt_regs *regs)
391 {
392         secure_computing(regs->gpr[0]);
393
394         if (test_thread_flag(TIF_SYSCALL_TRACE)
395             && (current->ptrace & PT_PTRACED))
396                 do_syscall_trace();
397
398         if (unlikely(current->audit_context)) {
399 #ifdef CONFIG_PPC64
400                 if (!test_thread_flag(TIF_32BIT))
401                         audit_syscall_entry(AUDIT_ARCH_PPC64,
402                                             regs->gpr[0],
403                                             regs->gpr[3], regs->gpr[4],
404                                             regs->gpr[5], regs->gpr[6]);
405                 else
406 #endif
407                         audit_syscall_entry(AUDIT_ARCH_PPC,
408                                             regs->gpr[0],
409                                             regs->gpr[3] & 0xffffffff,
410                                             regs->gpr[4] & 0xffffffff,
411                                             regs->gpr[5] & 0xffffffff,
412                                             regs->gpr[6] & 0xffffffff);
413         }
414 }
415
416 void do_syscall_trace_leave(struct pt_regs *regs)
417 {
418         if (unlikely(current->audit_context))
419                 audit_syscall_exit((regs->ccr&0x10000000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
420                                    regs->result);
421
422         if ((test_thread_flag(TIF_SYSCALL_TRACE)
423              || test_thread_flag(TIF_SINGLESTEP))
424             && (current->ptrace & PT_PTRACED))
425                 do_syscall_trace();
426 }