This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / arch / sh64 / kernel / ptrace.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * arch/sh64/kernel/ptrace.c
7  *
8  * Copyright (C) 2000, 2001  Paolo Alberelli
9  * Copyright (C) 2003  Paul Mundt
10  *
11  * Started from SH3/4 version:
12  *   SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
13  *
14  *   Original x86 implementation:
15  *      By Ross Biro 1/23/92
16  *      edited by Linus Torvalds
17  *
18  */
19
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/rwsem.h>
23 #include <linux/sched.h>
24 #include <linux/mm.h>
25 #include <linux/smp.h>
26 #include <linux/smp_lock.h>
27 #include <linux/errno.h>
28 #include <linux/ptrace.h>
29 #include <linux/user.h>
30
31 #include <asm/io.h>
32 #include <asm/uaccess.h>
33 #include <asm/pgtable.h>
34 #include <asm/system.h>
35 #include <asm/processor.h>
36 #include <asm/mmu_context.h>
37
38 /* This mask defines the bits of the SR which the user is not allowed to
39    change, which are everything except S, Q, M, PR, SZ, FR. */
40 #define SR_MASK      (0xffff8cfd)
41
42 /*
43  * does not yet catch signals sent when the child dies.
44  * in exit.c or in signal.c.
45  */
46
47 /*
48  * This routine will get a word from the user area in the process kernel stack.
49  */
50 static inline int get_stack_long(struct task_struct *task, int offset)
51 {
52         unsigned char *stack;
53
54         stack = (unsigned char *)(task->thread.uregs);
55         stack += offset;
56         return (*((int *)stack));
57 }
58
59 static inline unsigned long
60 get_fpu_long(struct task_struct *task, unsigned long addr)
61 {
62         unsigned long tmp;
63         struct pt_regs *regs;
64         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
65
66         if (!task->used_math) {
67                 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
68                         tmp = FPSCR_INIT;
69                 } else {
70                         tmp = 0xffffffffUL; /* matches initial value in fpu.c */
71                 }
72                 return tmp;
73         }
74
75         if (last_task_used_math == task) {
76                 grab_fpu();
77                 fpsave(&task->thread.fpu.hard);
78                 release_fpu();
79                 last_task_used_math = 0;
80                 regs->sr |= SR_FD;
81         }
82
83         tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
84         return tmp;
85 }
86
87 /*
88  * This routine will put a word into the user area in the process kernel stack.
89  */
90 static inline int put_stack_long(struct task_struct *task, int offset,
91                                  unsigned long data)
92 {
93         unsigned char *stack;
94
95         stack = (unsigned char *)(task->thread.uregs);
96         stack += offset;
97         *(unsigned long *) stack = data;
98         return 0;
99 }
100
101 static inline int
102 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
103 {
104         struct pt_regs *regs;
105
106         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
107
108         if (!task->used_math) {
109                 fpinit(&task->thread.fpu.hard);
110                 task->used_math = 1;
111         } else if (last_task_used_math == task) {
112                 grab_fpu();
113                 fpsave(&task->thread.fpu.hard);
114                 release_fpu();
115                 last_task_used_math = 0;
116                 regs->sr |= SR_FD;
117         }
118
119         ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
120         return 0;
121 }
122
123 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
124 {
125         struct task_struct *child;
126         int ret;
127
128         lock_kernel();
129         ret = -EPERM;
130         if (request == PTRACE_TRACEME) {
131                 /* are we already being traced? */
132                 if (current->ptrace & PT_PTRACED)
133                         goto out;
134                 /* set the ptrace bit in the process flags. */
135                 current->ptrace |= PT_PTRACED;
136                 ret = 0;
137                 goto out;
138         }
139         ret = -ESRCH;
140         read_lock(&tasklist_lock);
141         child = find_task_by_pid(pid);
142         if (child)
143                 get_task_struct(child);
144         read_unlock(&tasklist_lock);
145         if (!child)
146                 goto out;
147
148         ret = -EPERM;
149         if (pid == 1)           /* you may not mess with init */
150                 goto out_tsk;
151
152         if (request == PTRACE_ATTACH) {
153                 ret = ptrace_attach(child);
154                         goto out_tsk;
155                 }
156
157         ret = ptrace_check_attach(child, request == PTRACE_KILL);
158         if (ret < 0)
159                 goto out_tsk;
160
161         switch (request) {
162         /* when I and D space are separate, these will need to be fixed. */
163         case PTRACE_PEEKTEXT: /* read word at location addr. */
164         case PTRACE_PEEKDATA: {
165                 unsigned long tmp;
166                 int copied;
167
168                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
169                 ret = -EIO;
170                 if (copied != sizeof(tmp))
171                         break;
172                 ret = put_user(tmp,(unsigned long *) data);
173                 break;
174         }
175
176         /* read the word at location addr in the USER area. */
177         case PTRACE_PEEKUSR: {
178                 unsigned long tmp;
179
180                 ret = -EIO;
181                 if ((addr & 3) || addr < 0)
182                         break;
183
184                 if (addr < sizeof(struct pt_regs))
185                         tmp = get_stack_long(child, addr);
186                 else if ((addr >= offsetof(struct user, fpu)) &&
187                          (addr <  offsetof(struct user, u_fpvalid))) {
188                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
189                 } else if (addr == offsetof(struct user, u_fpvalid)) {
190                         tmp = child->used_math;
191                 } else {
192                         break;
193                 }
194                 ret = put_user(tmp, (unsigned long *)data);
195                 break;
196         }
197
198         /* when I and D space are separate, this will have to be fixed. */
199         case PTRACE_POKETEXT: /* write the word at location addr. */
200         case PTRACE_POKEDATA:
201                 ret = 0;
202                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
203                         break;
204                 ret = -EIO;
205                 break;
206
207         case PTRACE_POKEUSR:
208                 /* write the word at location addr in the USER area. We must
209                    disallow any changes to certain SR bits or u_fpvalid, since
210                    this could crash the kernel or result in a security
211                    loophole. */
212                 ret = -EIO;
213                 if ((addr & 3) || addr < 0)
214                         break;
215
216                 if (addr < sizeof(struct pt_regs)) {
217                         /* Ignore change of top 32 bits of SR */
218                         if (addr == offsetof (struct pt_regs, sr)+4)
219                         {
220                                 ret = 0;
221                                 break;
222                         }
223                         /* If lower 32 bits of SR, ignore non-user bits */
224                         if (addr == offsetof (struct pt_regs, sr))
225                         {
226                                 long cursr = get_stack_long(child, addr);
227                                 data &= ~(SR_MASK);
228                                 data |= (cursr & SR_MASK);
229                         }
230                         ret = put_stack_long(child, addr, data);
231                 }
232                 else if ((addr >= offsetof(struct user, fpu)) &&
233                          (addr <  offsetof(struct user, u_fpvalid))) {
234                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
235                 }
236                 break;
237
238         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
239         case PTRACE_CONT: { /* restart after signal. */
240                 ret = -EIO;
241                 if ((unsigned long) data > _NSIG)
242                         break;
243                 if (request == PTRACE_SYSCALL)
244                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
245                 else
246                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247                 child->exit_code = data;
248                 wake_up_process(child);
249                 ret = 0;
250                 break;
251         }
252
253 /*
254  * make the child exit.  Best I can do is send it a sigkill.
255  * perhaps it should be put in the status that it wants to
256  * exit.
257  */
258         case PTRACE_KILL: {
259                 ret = 0;
260                 if (child->state == TASK_ZOMBIE)        /* already dead */
261                         break;
262                 child->exit_code = SIGKILL;
263                 wake_up_process(child);
264                 break;
265         }
266
267         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
268                 struct pt_regs *regs;
269
270                 ret = -EIO;
271                 if ((unsigned long) data > _NSIG)
272                         break;
273                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
274                 if ((child->ptrace & PT_DTRACE) == 0) {
275                         /* Spurious delayed TF traps may occur */
276                         child->ptrace |= PT_DTRACE;
277                 }
278
279                 regs = child->thread.uregs;
280
281                 regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
282
283                 child->exit_code = data;
284                 /* give it a chance to run. */
285                 wake_up_process(child);
286                 ret = 0;
287                 break;
288         }
289
290         case PTRACE_DETACH: /* detach a process that was attached. */
291                 ret = ptrace_detach(child, data);
292                 break;
293
294         default:
295                 ret = ptrace_request(child, request, addr, data);
296                 break;
297         }
298 out_tsk:
299         put_task_struct(child);
300 out:
301         unlock_kernel();
302         return ret;
303 }
304
305 asmlinkage void syscall_trace(void)
306 {
307         struct task_struct *tsk = current;
308
309         if (!test_thread_flag(TIF_SYSCALL_TRACE))
310                 return;
311         if (!(tsk->ptrace & PT_PTRACED))
312                 return;
313
314         tsk->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
315                                     ? 0x80 : 0);
316         tsk->state = TASK_STOPPED;
317         notify_parent(tsk, SIGCHLD);
318         schedule();
319         /*
320          * this isn't the same as continuing with a signal, but it will do
321          * for normal use.  strace only continues with a signal if the
322          * stopping signal is not SIGTRAP.  -brl
323          */
324         if (tsk->exit_code) {
325                 send_sig(tsk->exit_code, tsk, 1);
326                 tsk->exit_code = 0;
327         }
328 }
329
330 /* Called with interrupts disabled */
331 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
332 {
333         /* This is called after a single step exception (DEBUGSS).
334            There is no need to change the PC, as it is a post-execution
335            exception, as entry.S does not do anything to the PC for DEBUGSS.
336            We need to clear the Single Step setting in SR to avoid
337            continually stepping. */
338         local_irq_enable();
339         regs->sr &= ~SR_SSTEP;
340         force_sig(SIGTRAP, current);
341 }
342
343 /* Called with interrupts disabled */
344 asmlinkage void do_software_break_point(unsigned long long vec,
345                                         struct pt_regs *regs)
346 {
347         /* We need to forward step the PC, to counteract the backstep done
348            in signal.c. */
349         local_irq_enable();
350         force_sig(SIGTRAP, current);
351         regs->pc += 4;
352 }
353
354 /*
355  * Called by kernel/ptrace.c when detaching..
356  *
357  * Make sure single step bits etc are not set.
358  */
359 void ptrace_disable(struct task_struct *child)
360 {
361         /* nothing to do.. */
362 }