Merge master.kernel.org:/home/rmk/linux-2.6-i2c manually
[sfrench/cifs-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 #include <linux/seccomp.h>
31 #include <linux/signal.h>
32
33 #include <asm/uaccess.h>
34 #include <asm/page.h>
35 #include <asm/pgtable.h>
36 #include <asm/system.h>
37 #include <asm/ptrace-common.h>
38
39 /*
40  * does not yet catch signals sent when the child dies.
41  * in exit.c or in signal.c.
42  */
43
44 /*
45  * Called by kernel/ptrace.c when detaching..
46  *
47  * Make sure single step bits etc are not set.
48  */
49 void ptrace_disable(struct task_struct *child)
50 {
51         /* make sure the single step bit is not set. */
52         clear_single_step(child);
53 }
54
55 int sys_ptrace(long request, long pid, long addr, long data)
56 {
57         struct task_struct *child;
58         int ret = -EPERM;
59
60         lock_kernel();
61         if (request == PTRACE_TRACEME) {
62                 /* are we already being traced? */
63                 if (current->ptrace & PT_PTRACED)
64                         goto out;
65                 ret = security_ptrace(current->parent, current);
66                 if (ret)
67                         goto out;
68                 /* set the ptrace bit in the process flags. */
69                 current->ptrace |= PT_PTRACED;
70                 ret = 0;
71                 goto out;
72         }
73         ret = -ESRCH;
74         read_lock(&tasklist_lock);
75         child = find_task_by_pid(pid);
76         if (child)
77                 get_task_struct(child);
78         read_unlock(&tasklist_lock);
79         if (!child)
80                 goto out;
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                         flush_fp_to_thread(child);
125                         tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
126                 }
127                 ret = put_user(tmp,(unsigned long __user *) data);
128                 break;
129         }
130
131         /* If I and D space are separate, this will have to be fixed. */
132         case PTRACE_POKETEXT: /* write the word at location addr. */
133         case PTRACE_POKEDATA:
134                 ret = 0;
135                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
136                                 == sizeof(data))
137                         break;
138                 ret = -EIO;
139                 break;
140
141         /* write the word at location addr in the USER area */
142         case PTRACE_POKEUSR: {
143                 unsigned long index;
144
145                 ret = -EIO;
146                 /* convert to index and check */
147                 index = (unsigned long) addr >> 3;
148                 if ((addr & 7) || (index > PT_FPSCR))
149                         break;
150
151                 if (index == PT_ORIG_R3)
152                         break;
153                 if (index < PT_FPR0) {
154                         ret = put_reg(child, index, data);
155                 } else {
156                         flush_fp_to_thread(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 (!valid_signal(data))
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->exit_state == EXIT_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 (!valid_signal(data))
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 __user *tmp = (unsigned long __user *)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 __user *tmp = (unsigned long __user *)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 __user *tmp = (unsigned long __user *)addr;
247
248                 flush_fp_to_thread(child);
249
250                 for (i = 0; i < 32; i++) {
251                         ret = put_user(*reg, tmp);
252                         if (ret)
253                                 break;
254                         reg++;
255                         tmp++;
256                 }
257                 break;
258         }
259
260         case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
261                 int i;
262                 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
263                 unsigned long __user *tmp = (unsigned long __user *)addr;
264
265                 flush_fp_to_thread(child);
266
267                 for (i = 0; i < 32; i++) {
268                         ret = get_user(*reg, tmp);
269                         if (ret)
270                                 break;
271                         reg++;
272                         tmp++;
273                 }
274                 break;
275         }
276
277         default:
278                 ret = ptrace_request(child, request, addr, data);
279                 break;
280         }
281 out_tsk:
282         put_task_struct(child);
283 out:
284         unlock_kernel();
285         return ret;
286 }
287
288 static void do_syscall_trace(void)
289 {
290         /* the 0x80 provides a way for the tracing parent to distinguish
291            between a syscall stop and SIGTRAP delivery */
292         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
293                                  ? 0x80 : 0));
294
295         /*
296          * this isn't the same as continuing with a signal, but it will do
297          * for normal use.  strace only continues with a signal if the
298          * stopping signal is not SIGTRAP.  -brl
299          */
300         if (current->exit_code) {
301                 send_sig(current->exit_code, current, 1);
302                 current->exit_code = 0;
303         }
304 }
305
306 void do_syscall_trace_enter(struct pt_regs *regs)
307 {
308         secure_computing(regs->gpr[0]);
309
310         if (test_thread_flag(TIF_SYSCALL_TRACE)
311             && (current->ptrace & PT_PTRACED))
312                 do_syscall_trace();
313
314         if (unlikely(current->audit_context))
315                 audit_syscall_entry(current,
316                                     test_thread_flag(TIF_32BIT)?AUDIT_ARCH_PPC:AUDIT_ARCH_PPC64,
317                                     regs->gpr[0],
318                                     regs->gpr[3], regs->gpr[4],
319                                     regs->gpr[5], regs->gpr[6]);
320
321 }
322
323 void do_syscall_trace_leave(struct pt_regs *regs)
324 {
325         if (unlikely(current->audit_context))
326                 audit_syscall_exit(current, 
327                                    (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
328                                    regs->result);
329
330         if ((test_thread_flag(TIF_SYSCALL_TRACE)
331              || test_thread_flag(TIF_SINGLESTEP))
332             && (current->ptrace & PT_PTRACED))
333                 do_syscall_trace();
334 }