patch-2_6_7-vs1_9_1_12
[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         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
80                 goto out_tsk;
81
82         ret = -EPERM;
83         if (pid == 1)           /* you may not mess with init */
84                 goto out_tsk;
85
86         if (request == PTRACE_ATTACH) {
87                 ret = ptrace_attach(child);
88                 goto out_tsk;
89         }
90
91         ret = ptrace_check_attach(child, request == PTRACE_KILL);
92         if (ret < 0)
93                 goto out_tsk;
94
95         switch (request) {
96         /* when I and D space are separate, these will need to be fixed. */
97         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
98         case PTRACE_PEEKDATA: {
99                 unsigned long tmp;
100                 int copied;
101
102                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
103                 ret = -EIO;
104                 if (copied != sizeof(tmp))
105                         break;
106                 ret = put_user(tmp,(unsigned long __user *) data);
107                 break;
108         }
109
110         /* read the word at location addr in the USER area. */
111         case PTRACE_PEEKUSR: {
112                 unsigned long index;
113                 unsigned long tmp;
114
115                 ret = -EIO;
116                 /* convert to index and check */
117                 index = (unsigned long) addr >> 3;
118                 if ((addr & 7) || (index > PT_FPSCR))
119                         break;
120
121                 if (index < PT_FPR0) {
122                         tmp = get_reg(child, (int)index);
123                 } else {
124                         if (child->thread.regs->msr & MSR_FP)
125                                 giveup_fpu(child);
126                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
127                 }
128                 ret = put_user(tmp,(unsigned long __user *) data);
129                 break;
130         }
131
132         /* If I and D space are separate, this will have to be fixed. */
133         case PTRACE_POKETEXT: /* write the word at location addr. */
134         case PTRACE_POKEDATA:
135                 ret = 0;
136                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
137                                 == sizeof(data))
138                         break;
139                 ret = -EIO;
140                 break;
141
142         /* write the word at location addr in the USER area */
143         case PTRACE_POKEUSR: {
144                 unsigned long index;
145
146                 ret = -EIO;
147                 /* convert to index and check */
148                 index = (unsigned long) addr >> 3;
149                 if ((addr & 7) || (index > PT_FPSCR))
150                         break;
151
152                 if (index == PT_ORIG_R3)
153                         break;
154                 if (index < PT_FPR0) {
155                         ret = put_reg(child, index, data);
156                 } else {
157                         if (child->thread.regs->msr & MSR_FP)
158                                 giveup_fpu(child);
159                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
160                         ret = 0;
161                 }
162                 break;
163         }
164
165         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
166         case PTRACE_CONT: { /* restart after signal. */
167                 ret = -EIO;
168                 if ((unsigned long) data > _NSIG)
169                         break;
170                 if (request == PTRACE_SYSCALL)
171                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
172                 else
173                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
174                 child->exit_code = data;
175                 /* make sure the single step bit is not set. */
176                 clear_single_step(child);
177                 wake_up_process(child);
178                 ret = 0;
179                 break;
180         }
181
182         /*
183          * make the child exit.  Best I can do is send it a sigkill.
184          * perhaps it should be put in the status that it wants to
185          * exit.
186          */
187         case PTRACE_KILL: {
188                 ret = 0;
189                 if (child->state == TASK_ZOMBIE)        /* already dead */
190                         break;
191                 child->exit_code = SIGKILL;
192                 /* make sure the single step bit is not set. */
193                 clear_single_step(child);
194                 wake_up_process(child);
195                 break;
196         }
197
198         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
199                 ret = -EIO;
200                 if ((unsigned long) data > _NSIG)
201                         break;
202                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
203                 set_single_step(child);
204                 child->exit_code = data;
205                 /* give it a chance to run. */
206                 wake_up_process(child);
207                 ret = 0;
208                 break;
209         }
210
211         case PTRACE_DETACH:
212                 ret = ptrace_detach(child, data);
213                 break;
214
215         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
216                 int i;
217                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
218                 unsigned long __user *tmp = (unsigned long __user *)addr;
219
220                 for (i = 0; i < 32; i++) {
221                         ret = put_user(*reg, tmp);
222                         if (ret)
223                                 break;
224                         reg++;
225                         tmp++;
226                 }
227                 break;
228         }
229
230         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
231                 int i;
232                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
233                 unsigned long __user *tmp = (unsigned long __user *)addr;
234
235                 for (i = 0; i < 32; i++) {
236                         ret = get_user(*reg, tmp);
237                         if (ret)
238                                 break;
239                         reg++;
240                         tmp++;
241                 }
242                 break;
243         }
244
245         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
246                 int i;
247                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
248                 unsigned long __user *tmp = (unsigned long __user *)addr;
249
250                 if (child->thread.regs->msr & MSR_FP)
251                         giveup_fpu(child);
252
253                 for (i = 0; i < 32; i++) {
254                         ret = put_user(*reg, tmp);
255                         if (ret)
256                                 break;
257                         reg++;
258                         tmp++;
259                 }
260                 break;
261         }
262
263         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
264                 int i;
265                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
266                 unsigned long __user *tmp = (unsigned long __user *)addr;
267
268                 if (child->thread.regs->msr & MSR_FP)
269                         giveup_fpu(child);
270
271                 for (i = 0; i < 32; i++) {
272                         ret = get_user(*reg, tmp);
273                         if (ret)
274                                 break;
275                         reg++;
276                         tmp++;
277                 }
278                 break;
279         }
280
281         default:
282                 ret = ptrace_request(child, request, addr, data);
283                 break;
284         }
285 out_tsk:
286         put_task_struct(child);
287 out:
288         unlock_kernel();
289         return ret;
290 }
291
292 static void do_syscall_trace(void)
293 {
294         /* the 0x80 provides a way for the tracing parent to distinguish
295            between a syscall stop and SIGTRAP delivery */
296         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
297                                  ? 0x80 : 0));
298
299         /*
300          * this isn't the same as continuing with a signal, but it will do
301          * for normal use.  strace only continues with a signal if the
302          * stopping signal is not SIGTRAP.  -brl
303          */
304         if (current->exit_code) {
305                 send_sig(current->exit_code, current, 1);
306                 current->exit_code = 0;
307         }
308 }
309
310 void do_syscall_trace_enter(struct pt_regs *regs)
311 {
312         if (unlikely(current->audit_context))
313                 audit_syscall_entry(current, regs->gpr[0],
314                                     regs->gpr[3], regs->gpr[4],
315                                     regs->gpr[5], regs->gpr[6]);
316
317         if (test_thread_flag(TIF_SYSCALL_TRACE)
318             && (current->ptrace & PT_PTRACED))
319                 do_syscall_trace();
320 }
321
322 void do_syscall_trace_leave(void)
323 {
324         if (unlikely(current->audit_context))
325                 audit_syscall_exit(current, 0); /* FIXME: pass pt_regs */
326
327         if (test_thread_flag(TIF_SYSCALL_TRACE)
328             && (current->ptrace & PT_PTRACED))
329                 do_syscall_trace();
330 }