Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.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 #include <linux/seccomp.h>
31
32 #include <asm/uaccess.h>
33 #include <asm/page.h>
34 #include <asm/pgtable.h>
35 #include <asm/system.h>
36 #include <asm/ptrace-common.h>
37
38 /*
39  * does not yet catch signals sent when the child dies.
40  * in exit.c or in signal.c.
41  */
42
43 /*
44  * Called by kernel/ptrace.c when detaching..
45  *
46  * Make sure single step bits etc are not set.
47  */
48 void ptrace_disable(struct task_struct *child)
49 {
50         /* make sure the single step bit is not set. */
51         clear_single_step(child);
52 }
53
54 int sys_ptrace(long request, long pid, long addr, long data)
55 {
56         struct task_struct *child;
57         int ret = -EPERM;
58
59         lock_kernel();
60         if (request == PTRACE_TRACEME) {
61                 /* are we already being traced? */
62                 if (current->ptrace & PT_PTRACED)
63                         goto out;
64                 ret = security_ptrace(current->parent, current);
65                 if (ret)
66                         goto out;
67                 /* set the ptrace bit in the process flags. */
68                 current->ptrace |= PT_PTRACED;
69                 ret = 0;
70                 goto out;
71         }
72         ret = -ESRCH;
73         read_lock(&tasklist_lock);
74         child = find_task_by_pid(pid);
75         if (child)
76                 get_task_struct(child);
77         read_unlock(&tasklist_lock);
78         if (!child)
79                 goto out;
80
81         ret = -EPERM;
82         if (pid == 1)           /* you may not mess with init */
83                 goto out_tsk;
84
85         if (request == PTRACE_ATTACH) {
86                 ret = ptrace_attach(child);
87                 goto out_tsk;
88         }
89
90         ret = ptrace_check_attach(child, request == PTRACE_KILL);
91         if (ret < 0)
92                 goto out_tsk;
93
94         switch (request) {
95         /* when I and D space are separate, these will need to be fixed. */
96         case PTRACE_PEEKTEXT: /* read word at location addr. */ 
97         case PTRACE_PEEKDATA: {
98                 unsigned long tmp;
99                 int copied;
100
101                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
102                 ret = -EIO;
103                 if (copied != sizeof(tmp))
104                         break;
105                 ret = put_user(tmp,(unsigned long __user *) data);
106                 break;
107         }
108
109         /* read the word at location addr in the USER area. */
110         case PTRACE_PEEKUSR: {
111                 unsigned long index;
112                 unsigned long tmp;
113
114                 ret = -EIO;
115                 /* convert to index and check */
116                 index = (unsigned long) addr >> 3;
117                 if ((addr & 7) || (index > PT_FPSCR))
118                         break;
119
120                 if (index < PT_FPR0) {
121                         tmp = get_reg(child, (int)index);
122                 } else {
123                         flush_fp_to_thread(child);
124                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
125                 }
126                 ret = put_user(tmp,(unsigned long __user *) 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                         flush_fp_to_thread(child);
156                         ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
157                         ret = 0;
158                 }
159                 break;
160         }
161
162         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
163         case PTRACE_CONT: { /* restart after signal. */
164                 ret = -EIO;
165                 if ((unsigned long) data > _NSIG)
166                         break;
167                 if (request == PTRACE_SYSCALL)
168                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
169                 else
170                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
171                 child->exit_code = data;
172                 /* make sure the single step bit is not set. */
173                 clear_single_step(child);
174                 wake_up_process(child);
175                 ret = 0;
176                 break;
177         }
178
179         /*
180          * make the child exit.  Best I can do is send it a sigkill.
181          * perhaps it should be put in the status that it wants to
182          * exit.
183          */
184         case PTRACE_KILL: {
185                 ret = 0;
186                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
187                         break;
188                 child->exit_code = SIGKILL;
189                 /* make sure the single step bit is not set. */
190                 clear_single_step(child);
191                 wake_up_process(child);
192                 break;
193         }
194
195         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
196                 ret = -EIO;
197                 if ((unsigned long) data > _NSIG)
198                         break;
199                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
200                 set_single_step(child);
201                 child->exit_code = data;
202                 /* give it a chance to run. */
203                 wake_up_process(child);
204                 ret = 0;
205                 break;
206         }
207
208         case PTRACE_DETACH:
209                 ret = ptrace_detach(child, data);
210                 break;
211
212         case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
213                 int i;
214                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
215                 unsigned long __user *tmp = (unsigned long __user *)addr;
216
217                 for (i = 0; i < 32; i++) {
218                         ret = put_user(*reg, tmp);
219                         if (ret)
220                                 break;
221                         reg++;
222                         tmp++;
223                 }
224                 break;
225         }
226
227         case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
228                 int i;
229                 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
230                 unsigned long __user *tmp = (unsigned long __user *)addr;
231
232                 for (i = 0; i < 32; i++) {
233                         ret = get_user(*reg, tmp);
234                         if (ret)
235                                 break;
236                         reg++;
237                         tmp++;
238                 }
239                 break;
240         }
241
242         case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
243                 int i;
244                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
245                 unsigned long __user *tmp = (unsigned long __user *)addr;
246
247                 flush_fp_to_thread(child);
248
249                 for (i = 0; i < 32; i++) {
250                         ret = put_user(*reg, tmp);
251                         if (ret)
252                                 break;
253                         reg++;
254                         tmp++;
255                 }
256                 break;
257         }
258
259         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
260                 int i;
261                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
262                 unsigned long __user *tmp = (unsigned long __user *)addr;
263
264                 flush_fp_to_thread(child);
265
266                 for (i = 0; i < 32; i++) {
267                         ret = get_user(*reg, tmp);
268                         if (ret)
269                                 break;
270                         reg++;
271                         tmp++;
272                 }
273                 break;
274         }
275
276         default:
277                 ret = ptrace_request(child, request, addr, data);
278                 break;
279         }
280 out_tsk:
281         put_task_struct(child);
282 out:
283         unlock_kernel();
284         return ret;
285 }
286
287 static void do_syscall_trace(void)
288 {
289         /* the 0x80 provides a way for the tracing parent to distinguish
290            between a syscall stop and SIGTRAP delivery */
291         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
292                                  ? 0x80 : 0));
293
294         /*
295          * this isn't the same as continuing with a signal, but it will do
296          * for normal use.  strace only continues with a signal if the
297          * stopping signal is not SIGTRAP.  -brl
298          */
299         if (current->exit_code) {
300                 send_sig(current->exit_code, current, 1);
301                 current->exit_code = 0;
302         }
303 }
304
305 void do_syscall_trace_enter(struct pt_regs *regs)
306 {
307         if (unlikely(current->audit_context))
308                 audit_syscall_entry(current, regs->gpr[0],
309                                     regs->gpr[3], regs->gpr[4],
310                                     regs->gpr[5], regs->gpr[6]);
311
312         if (test_thread_flag(TIF_SYSCALL_TRACE)
313             && (current->ptrace & PT_PTRACED))
314                 do_syscall_trace();
315 }
316
317 void do_syscall_trace_leave(struct pt_regs *regs)
318 {
319         secure_computing(regs->gpr[0]);
320
321         if (unlikely(current->audit_context))
322                 audit_syscall_exit(current, regs->result);
323
324         if ((test_thread_flag(TIF_SYSCALL_TRACE)
325              || test_thread_flag(TIF_SINGLESTEP))
326             && (current->ptrace & PT_PTRACED))
327                 do_syscall_trace();
328 }