6afe71a7d56c93f651e194bbbc52c9a6dce95bbd
[linux-2.6.git] / arch / ppc64 / kernel / ptrace.c
1 /*
2  *  linux/arch/ppc64/kernel/ptrace.c
3  *
4  *  PowerPC version
5  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6  *
7  *  Derived from "arch/m68k/kernel/ptrace.c"
8  *  Copyright (C) 1994 by Hamish Macdonald
9  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
10  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
11  *
12  * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13  * and Paul Mackerras (paulus@linuxcare.com.au).
14  *
15  * This file is subject to the terms and conditions of the GNU General
16  * Public License.  See the file README.legal in the main directory of
17  * this archive for more details.
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/audit.h>
30
31 #include <asm/uaccess.h>
32 #include <asm/page.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35 #include <asm/ptrace-common.h>
36
37 /*
38  * does not yet catch signals sent when the child dies.
39  * in exit.c or in signal.c.
40  */
41
42 /*
43  * Called by kernel/ptrace.c when detaching..
44  *
45  * Make sure single step bits etc are not set.
46  */
47 void ptrace_disable(struct task_struct *child)
48 {
49         /* make sure the single step bit is not set. */
50         clear_single_step(child);
51 }
52
53 int sys_ptrace(long request, long pid, long addr, long data)
54 {
55         struct task_struct *child;
56         int ret = -EPERM;
57
58         lock_kernel();
59         if (request == PTRACE_TRACEME) {
60                 /* are we already being traced? */
61                 if (current->ptrace & PT_PTRACED)
62                         goto out;
63                 ret = security_ptrace(current->parent, current);
64                 if (ret)
65                         goto out;
66                 /* set the ptrace bit in the process flags. */
67                 current->ptrace |= PT_PTRACED;
68                 ret = 0;
69                 goto out;
70         }
71         ret = -ESRCH;
72         read_lock(&tasklist_lock);
73         child = find_task_by_pid(pid);
74         if (child)
75                 get_task_struct(child);
76         read_unlock(&tasklist_lock);
77         if (!child)
78                 goto out;
79
80         ret = -EPERM;
81         if (pid == 1)           /* you may not mess with init */
82                 goto out_tsk;
83
84         if (request == PTRACE_ATTACH) {
85                 ret = ptrace_attach(child);
86                 goto out_tsk;
87         }
88
89         ret = ptrace_check_attach(child, request == PTRACE_KILL);
90         if (ret < 0)
91                 goto out_tsk;
92
93         switch (request) {
94         /* when I and D space are separate, these will need to be fixed. */
95         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
96         case PTRACE_PEEKDATA: {
97                 unsigned long tmp;
98                 int copied;
99
100                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
101                 ret = -EIO;
102                 if (copied != sizeof(tmp))
103                         break;
104                 ret = put_user(tmp,(unsigned long *) data);
105                 break;
106         }
107
108         /* read the word at location addr in the USER area. */
109         case PTRACE_PEEKUSR: {
110                 unsigned long index;
111                 unsigned long tmp;
112
113                 ret = -EIO;
114                 /* convert to index and check */
115                 index = (unsigned long) addr >> 3;
116                 if ((addr & 7) || (index > PT_FPSCR))
117                         break;
118
119                 if (index < PT_FPR0) {
120                         tmp = get_reg(child, (int)index);
121                 } else {
122                         if (child->thread.regs->msr & MSR_FP)
123                                 giveup_fpu(child);
124                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
125                 }
126                 ret = put_user(tmp,(unsigned long *) data);
127                 break;
128         }
129
130         /* If I and D space are separate, this will have to be fixed. */
131         case PTRACE_POKETEXT: /* write the word at location addr. */
132         case PTRACE_POKEDATA:
133                 ret = 0;
134                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
135                                 == sizeof(data))
136                         break;
137                 ret = -EIO;
138                 break;
139
140         /* write the word at location addr in the USER area */
141         case PTRACE_POKEUSR: {
142                 unsigned long index;
143
144                 ret = -EIO;
145                 /* convert to index and check */
146                 index = (unsigned long) addr >> 3;
147                 if ((addr & 7) || (index > PT_FPSCR))
148                         break;
149
150                 if (index == PT_ORIG_R3)
151                         break;
152                 if (index < PT_FPR0) {
153                         ret = put_reg(child, index, data);
154                 } else {
155                         if (child->thread.regs->msr & MSR_FP)
156                                 giveup_fpu(child);
157                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
158                         ret = 0;
159                 }
160                 break;
161         }
162
163         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
164         case PTRACE_CONT: { /* restart after signal. */
165                 ret = -EIO;
166                 if ((unsigned long) data > _NSIG)
167                         break;
168                 if (request == PTRACE_SYSCALL)
169                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
170                 else
171                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
172                 child->exit_code = data;
173                 /* make sure the single step bit is not set. */
174                 clear_single_step(child);
175                 wake_up_process(child);
176                 ret = 0;
177                 break;
178         }
179
180         /*
181          * make the child exit.  Best I can do is send it a sigkill.
182          * perhaps it should be put in the status that it wants to
183          * exit.
184          */
185         case PTRACE_KILL: {
186                 ret = 0;
187                 if (child->state == TASK_ZOMBIE)        /* already dead */
188                         break;
189                 child->exit_code = SIGKILL;
190                 /* make sure the single step bit is not set. */
191                 clear_single_step(child);
192                 wake_up_process(child);
193                 break;
194         }
195
196         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
197                 ret = -EIO;
198                 if ((unsigned long) data > _NSIG)
199                         break;
200                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
201                 set_single_step(child);
202                 child->exit_code = data;
203                 /* give it a chance to run. */
204                 wake_up_process(child);
205                 ret = 0;
206                 break;
207         }
208
209         case PTRACE_DETACH:
210                 ret = ptrace_detach(child, data);
211                 break;
212
213         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
214                 int i;
215                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
216                 unsigned long *tmp = (unsigned long *)addr;
217
218                 for (i = 0; i < 32; i++) {
219                         ret = put_user(*reg, tmp);
220                         if (ret)
221                                 break;
222                         reg++;
223                         tmp++;
224                 }
225                 break;
226         }
227
228         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
229                 int i;
230                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
231                 unsigned long *tmp = (unsigned long *)addr;
232
233                 for (i = 0; i < 32; i++) {
234                         ret = get_user(*reg, tmp);
235                         if (ret)
236                                 break;
237                         reg++;
238                         tmp++;
239                 }
240                 break;
241         }
242
243         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
244                 int i;
245                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
246                 unsigned long *tmp = (unsigned long *)addr;
247
248                 if (child->thread.regs->msr & MSR_FP)
249                         giveup_fpu(child);
250
251                 for (i = 0; i < 32; i++) {
252                         ret = put_user(*reg, tmp);
253                         if (ret)
254                                 break;
255                         reg++;
256                         tmp++;
257                 }
258                 break;
259         }
260
261         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
262                 int i;
263                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
264                 unsigned long *tmp = (unsigned long *)addr;
265
266                 if (child->thread.regs->msr & MSR_FP)
267                         giveup_fpu(child);
268
269                 for (i = 0; i < 32; i++) {
270                         ret = get_user(*reg, tmp);
271                         if (ret)
272                                 break;
273                         reg++;
274                         tmp++;
275                 }
276                 break;
277         }
278
279         default:
280                 ret = ptrace_request(child, request, addr, data);
281                 break;
282         }
283 out_tsk:
284         put_task_struct(child);
285 out:
286         unlock_kernel();
287         return ret;
288 }
289
290 static void do_syscall_trace(void)
291 {
292         /* the 0x80 provides a way for the tracing parent to distinguish
293            between a syscall stop and SIGTRAP delivery */
294         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
295                                  ? 0x80 : 0));
296
297         /*
298          * this isn't the same as continuing with a signal, but it will do
299          * for normal use.  strace only continues with a signal if the
300          * stopping signal is not SIGTRAP.  -brl
301          */
302         if (current->exit_code) {
303                 send_sig(current->exit_code, current, 1);
304                 current->exit_code = 0;
305         }
306 }
307
308 void do_syscall_trace_enter(struct pt_regs *regs)
309 {
310         if (unlikely(current->audit_context))
311                 audit_syscall_entry(current, regs->gpr[0],
312                                     regs->gpr[3], regs->gpr[4],
313                                     regs->gpr[5], regs->gpr[6]);
314
315         if (test_thread_flag(TIF_SYSCALL_TRACE)
316             && (current->ptrace & PT_PTRACED))
317                 do_syscall_trace();
318 }
319
320 void do_syscall_trace_leave(void)
321 {
322         if (unlikely(current->audit_context))
323                 audit_syscall_exit(current, 0); /* FIXME: pass pt_regs */
324
325         if (test_thread_flag(TIF_SYSCALL_TRACE)
326             && (current->ptrace & PT_PTRACED))
327                 do_syscall_trace();
328 }