Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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 #include <linux/signal.h>
31 #include <linux/syscalls.h>
32
33 #include <asm/io.h>
34 #include <asm/uaccess.h>
35 #include <asm/pgtable.h>
36 #include <asm/system.h>
37 #include <asm/processor.h>
38 #include <asm/mmu_context.h>
39
40 /* This mask defines the bits of the SR which the user is not allowed to
41    change, which are everything except S, Q, M, PR, SZ, FR. */
42 #define SR_MASK      (0xffff8cfd)
43
44 /*
45  * does not yet catch signals sent when the child dies.
46  * in exit.c or in signal.c.
47  */
48
49 /*
50  * This routine will get a word from the user area in the process kernel stack.
51  */
52 static inline int get_stack_long(struct task_struct *task, int offset)
53 {
54         unsigned char *stack;
55
56         stack = (unsigned char *)(task->thread.uregs);
57         stack += offset;
58         return (*((int *)stack));
59 }
60
61 static inline unsigned long
62 get_fpu_long(struct task_struct *task, unsigned long addr)
63 {
64         unsigned long tmp;
65         struct pt_regs *regs;
66         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
67
68         if (!tsk_used_math(task)) {
69                 if (addr == offsetof(struct user_fpu_struct, fpscr)) {
70                         tmp = FPSCR_INIT;
71                 } else {
72                         tmp = 0xffffffffUL; /* matches initial value in fpu.c */
73                 }
74                 return tmp;
75         }
76
77         if (last_task_used_math == task) {
78                 grab_fpu();
79                 fpsave(&task->thread.fpu.hard);
80                 release_fpu();
81                 last_task_used_math = 0;
82                 regs->sr |= SR_FD;
83         }
84
85         tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)];
86         return tmp;
87 }
88
89 /*
90  * This routine will put a word into the user area in the process kernel stack.
91  */
92 static inline int put_stack_long(struct task_struct *task, int offset,
93                                  unsigned long data)
94 {
95         unsigned char *stack;
96
97         stack = (unsigned char *)(task->thread.uregs);
98         stack += offset;
99         *(unsigned long *) stack = data;
100         return 0;
101 }
102
103 static inline int
104 put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data)
105 {
106         struct pt_regs *regs;
107
108         regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1;
109
110         if (!tsk_used_math(task)) {
111                 fpinit(&task->thread.fpu.hard);
112                 set_stopped_child_used_math(task);
113         } else if (last_task_used_math == task) {
114                 grab_fpu();
115                 fpsave(&task->thread.fpu.hard);
116                 release_fpu();
117                 last_task_used_math = 0;
118                 regs->sr |= SR_FD;
119         }
120
121         ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data;
122         return 0;
123 }
124
125
126 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
127 {
128         int ret;
129
130         switch (request) {
131         /* when I and D space are separate, these will need to be fixed. */
132         case PTRACE_PEEKTEXT: /* read word at location addr. */
133         case PTRACE_PEEKDATA: {
134                 unsigned long tmp;
135                 int copied;
136
137                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
138                 ret = -EIO;
139                 if (copied != sizeof(tmp))
140                         break;
141                 ret = put_user(tmp,(unsigned long *) data);
142                 break;
143         }
144
145         /* read the word at location addr in the USER area. */
146         case PTRACE_PEEKUSR: {
147                 unsigned long tmp;
148
149                 ret = -EIO;
150                 if ((addr & 3) || addr < 0)
151                         break;
152
153                 if (addr < sizeof(struct pt_regs))
154                         tmp = get_stack_long(child, addr);
155                 else if ((addr >= offsetof(struct user, fpu)) &&
156                          (addr <  offsetof(struct user, u_fpvalid))) {
157                         tmp = get_fpu_long(child, addr - offsetof(struct user, fpu));
158                 } else if (addr == offsetof(struct user, u_fpvalid)) {
159                         tmp = !!tsk_used_math(child);
160                 } else {
161                         break;
162                 }
163                 ret = put_user(tmp, (unsigned long *)data);
164                 break;
165         }
166
167         /* when I and D space are separate, this will have to be fixed. */
168         case PTRACE_POKETEXT: /* write the word at location addr. */
169         case PTRACE_POKEDATA:
170                 ret = 0;
171                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
172                         break;
173                 ret = -EIO;
174                 break;
175
176         case PTRACE_POKEUSR:
177                 /* write the word at location addr in the USER area. We must
178                    disallow any changes to certain SR bits or u_fpvalid, since
179                    this could crash the kernel or result in a security
180                    loophole. */
181                 ret = -EIO;
182                 if ((addr & 3) || addr < 0)
183                         break;
184
185                 if (addr < sizeof(struct pt_regs)) {
186                         /* Ignore change of top 32 bits of SR */
187                         if (addr == offsetof (struct pt_regs, sr)+4)
188                         {
189                                 ret = 0;
190                                 break;
191                         }
192                         /* If lower 32 bits of SR, ignore non-user bits */
193                         if (addr == offsetof (struct pt_regs, sr))
194                         {
195                                 long cursr = get_stack_long(child, addr);
196                                 data &= ~(SR_MASK);
197                                 data |= (cursr & SR_MASK);
198                         }
199                         ret = put_stack_long(child, addr, data);
200                 }
201                 else if ((addr >= offsetof(struct user, fpu)) &&
202                          (addr <  offsetof(struct user, u_fpvalid))) {
203                         ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data);
204                 }
205                 break;
206
207         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
208         case PTRACE_CONT: { /* restart after signal. */
209                 ret = -EIO;
210                 if (!valid_signal(data))
211                         break;
212                 if (request == PTRACE_SYSCALL)
213                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
214                 else
215                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
216                 child->exit_code = data;
217                 wake_up_process(child);
218                 ret = 0;
219                 break;
220         }
221
222 /*
223  * make the child exit.  Best I can do is send it a sigkill.
224  * perhaps it should be put in the status that it wants to
225  * exit.
226  */
227         case PTRACE_KILL: {
228                 ret = 0;
229                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
230                         break;
231                 child->exit_code = SIGKILL;
232                 wake_up_process(child);
233                 break;
234         }
235
236         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
237                 struct pt_regs *regs;
238
239                 ret = -EIO;
240                 if (!valid_signal(data))
241                         break;
242                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
243                 if ((child->ptrace & PT_DTRACE) == 0) {
244                         /* Spurious delayed TF traps may occur */
245                         child->ptrace |= PT_DTRACE;
246                 }
247
248                 regs = child->thread.uregs;
249
250                 regs->sr |= SR_SSTEP;   /* auto-resetting upon exception */
251
252                 child->exit_code = data;
253                 /* give it a chance to run. */
254                 wake_up_process(child);
255                 ret = 0;
256                 break;
257         }
258
259         case PTRACE_DETACH: /* detach a process that was attached. */
260                 ret = ptrace_detach(child, data);
261                 break;
262
263         default:
264                 ret = ptrace_request(child, request, addr, data);
265                 break;
266         }
267         return ret;
268 }
269
270 asmlinkage int sh64_ptrace(long request, long pid, long addr, long data)
271 {
272         extern void poke_real_address_q(unsigned long long addr, unsigned long long data);
273 #define WPC_DBRMODE 0x0d104008
274         static int first_call = 1;
275
276         lock_kernel();
277         if (first_call) {
278                 /* Set WPC.DBRMODE to 0.  This makes all debug events get
279                  * delivered through RESVEC, i.e. into the handlers in entry.S.
280                  * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE
281                  * would normally be left set to 1, which makes debug events get
282                  * delivered through DBRVEC, i.e. into the remote gdb's
283                  * handlers.  This prevents ptrace getting them, and confuses
284                  * the remote gdb.) */
285                 printk("DBRMODE set to 0 to permit native debugging\n");
286                 poke_real_address_q(WPC_DBRMODE, 0);
287                 first_call = 0;
288         }
289         unlock_kernel();
290
291         return sys_ptrace(request, pid, addr, data);
292 }
293
294 asmlinkage void syscall_trace(void)
295 {
296         struct task_struct *tsk = current;
297
298         if (!test_thread_flag(TIF_SYSCALL_TRACE))
299                 return;
300         if (!(tsk->ptrace & PT_PTRACED))
301                 return;
302
303         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
304                                  ? 0x80 : 0));
305         /*
306          * this isn't the same as continuing with a signal, but it will do
307          * for normal use.  strace only continues with a signal if the
308          * stopping signal is not SIGTRAP.  -brl
309          */
310         if (tsk->exit_code) {
311                 send_sig(tsk->exit_code, tsk, 1);
312                 tsk->exit_code = 0;
313         }
314 }
315
316 /* Called with interrupts disabled */
317 asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs)
318 {
319         /* This is called after a single step exception (DEBUGSS).
320            There is no need to change the PC, as it is a post-execution
321            exception, as entry.S does not do anything to the PC for DEBUGSS.
322            We need to clear the Single Step setting in SR to avoid
323            continually stepping. */
324         local_irq_enable();
325         regs->sr &= ~SR_SSTEP;
326         force_sig(SIGTRAP, current);
327 }
328
329 /* Called with interrupts disabled */
330 asmlinkage void do_software_break_point(unsigned long long vec,
331                                         struct pt_regs *regs)
332 {
333         /* We need to forward step the PC, to counteract the backstep done
334            in signal.c. */
335         local_irq_enable();
336         force_sig(SIGTRAP, current);
337         regs->pc += 4;
338 }
339
340 /*
341  * Called by kernel/ptrace.c when detaching..
342  *
343  * Make sure single step bits etc are not set.
344  */
345 void ptrace_disable(struct task_struct *child)
346 {
347         /* nothing to do.. */
348 }