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