Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-drm-fsl-dcu.git] / arch / sh / kernel / ptrace.c
1 /*
2  * linux/arch/sh/kernel/ptrace.c
3  *
4  * Original x86 implementation:
5  *      By Ross Biro 1/23/92
6  *      edited by Linus Torvalds
7  *
8  * SuperH version:   Copyright (C) 1999, 2000  Kaz Kojima & Niibe Yutaka
9  *
10  */
11 #include <linux/kernel.h>
12 #include <linux/sched.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/slab.h>
19 #include <linux/security.h>
20 #include <linux/signal.h>
21 #include <linux/io.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/processor.h>
26 #include <asm/mmu_context.h>
27
28 /*
29  * does not yet catch signals sent when the child dies.
30  * in exit.c or in signal.c.
31  */
32
33 /*
34  * This routine will get a word off of the process kernel stack.
35  */
36 static inline int get_stack_long(struct task_struct *task, int offset)
37 {
38         unsigned char *stack;
39
40         stack = (unsigned char *)task_pt_regs(task);
41         stack += offset;
42         return (*((int *)stack));
43 }
44
45 /*
46  * This routine will put a word on the process kernel stack.
47  */
48 static inline int put_stack_long(struct task_struct *task, int offset,
49                                  unsigned long data)
50 {
51         unsigned char *stack;
52
53         stack = (unsigned char *)task_pt_regs(task);
54         stack += offset;
55         *(unsigned long *) stack = data;
56         return 0;
57 }
58
59 static void ptrace_disable_singlestep(struct task_struct *child)
60 {
61         clear_tsk_thread_flag(child, TIF_SINGLESTEP);
62
63         /*
64          * Ensure the UBC is not programmed at the next context switch.
65          *
66          * Normally this is not needed but there are sequences such as
67          * singlestep, signal delivery, and continue that leave the
68          * ubc_pc non-zero leading to spurious SIGTRAPs.
69          */
70         if (child->thread.ubc_pc != 0) {
71                 ubc_usercnt -= 1;
72                 child->thread.ubc_pc = 0;
73         }
74 }
75
76 /*
77  * Called by kernel/ptrace.c when detaching..
78  *
79  * Make sure single step bits etc are not set.
80  */
81 void ptrace_disable(struct task_struct *child)
82 {
83         ptrace_disable_singlestep(child);
84 }
85
86 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
87 {
88         struct user * dummy = NULL;
89         int ret;
90
91         switch (request) {
92         /* when I and D space are separate, these will need to be fixed. */
93         case PTRACE_PEEKTEXT: /* read word at location addr. */
94         case PTRACE_PEEKDATA: {
95                 unsigned long tmp;
96                 int copied;
97
98                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
99                 ret = -EIO;
100                 if (copied != sizeof(tmp))
101                         break;
102                 ret = put_user(tmp,(unsigned long *) data);
103                 break;
104         }
105
106         /* read the word at location addr in the USER area. */
107         case PTRACE_PEEKUSR: {
108                 unsigned long tmp;
109
110                 ret = -EIO;
111                 if ((addr & 3) || addr < 0 ||
112                     addr > sizeof(struct user) - 3)
113                         break;
114
115                 if (addr < sizeof(struct pt_regs))
116                         tmp = get_stack_long(child, addr);
117                 else if (addr >= (long) &dummy->fpu &&
118                          addr < (long) &dummy->u_fpvalid) {
119                         if (!tsk_used_math(child)) {
120                                 if (addr == (long)&dummy->fpu.fpscr)
121                                         tmp = FPSCR_INIT;
122                                 else
123                                         tmp = 0;
124                         } else
125                                 tmp = ((long *)&child->thread.fpu)
126                                         [(addr - (long)&dummy->fpu) >> 2];
127                 } else if (addr == (long) &dummy->u_fpvalid)
128                         tmp = !!tsk_used_math(child);
129                 else
130                         tmp = 0;
131                 ret = put_user(tmp, (unsigned long *)data);
132                 break;
133         }
134
135         /* when I and D space are separate, this will have to be fixed. */
136         case PTRACE_POKETEXT: /* write the word at location addr. */
137         case PTRACE_POKEDATA:
138                 ret = 0;
139                 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
140                         break;
141                 ret = -EIO;
142                 break;
143
144         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
145                 ret = -EIO;
146                 if ((addr & 3) || addr < 0 ||
147                     addr > sizeof(struct user) - 3)
148                         break;
149
150                 if (addr < sizeof(struct pt_regs))
151                         ret = put_stack_long(child, addr, data);
152                 else if (addr >= (long) &dummy->fpu &&
153                          addr < (long) &dummy->u_fpvalid) {
154                         set_stopped_child_used_math(child);
155                         ((long *)&child->thread.fpu)
156                                 [(addr - (long)&dummy->fpu) >> 2] = data;
157                         ret = 0;
158                 } else if (addr == (long) &dummy->u_fpvalid) {
159                         conditional_stopped_child_used_math(data, child);
160                         ret = 0;
161                 }
162                 break;
163
164         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
165         case PTRACE_CONT: { /* restart after signal. */
166                 ret = -EIO;
167                 if (!valid_signal(data))
168                         break;
169                 if (request == PTRACE_SYSCALL)
170                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
171                 else
172                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
173
174                 ptrace_disable_singlestep(child);
175
176                 child->exit_code = data;
177                 wake_up_process(child);
178                 ret = 0;
179                 break;
180         }
181
182 /*
183  * make the child exit.  Best I can do is send it a sigkill.
184  * perhaps it should be put in the status that it wants to
185  * exit.
186  */
187         case PTRACE_KILL: {
188                 ret = 0;
189                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
190                         break;
191                 ptrace_disable_singlestep(child);
192                 child->exit_code = SIGKILL;
193                 wake_up_process(child);
194                 break;
195         }
196
197         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
198                 long pc;
199                 struct pt_regs *dummy = NULL;
200
201                 ret = -EIO;
202                 if (!valid_signal(data))
203                         break;
204                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
205                 if ((child->ptrace & PT_DTRACE) == 0) {
206                         /* Spurious delayed TF traps may occur */
207                         child->ptrace |= PT_DTRACE;
208                 }
209
210                 pc = get_stack_long(child, (long)&dummy->pc);
211
212                 /* Next scheduling will set up UBC */
213                 if (child->thread.ubc_pc == 0)
214                         ubc_usercnt += 1;
215                 child->thread.ubc_pc = pc;
216
217                 set_tsk_thread_flag(child, TIF_SINGLESTEP);
218                 child->exit_code = data;
219                 /* give it a chance to run. */
220                 wake_up_process(child);
221                 ret = 0;
222                 break;
223         }
224
225         case PTRACE_DETACH: /* detach a process that was attached. */
226                 ret = ptrace_detach(child, data);
227                 break;
228
229 #ifdef CONFIG_SH_DSP
230         case PTRACE_GETDSPREGS: {
231                 unsigned long dp;
232
233                 ret = -EIO;
234                 dp = ((unsigned long) child) + THREAD_SIZE -
235                          sizeof(struct pt_dspregs);
236                 if (*((int *) (dp - 4)) == SR_FD) {
237                         copy_to_user(addr, (void *) dp,
238                                 sizeof(struct pt_dspregs));
239                         ret = 0;
240                 }
241                 break;
242         }
243
244         case PTRACE_SETDSPREGS: {
245                 unsigned long dp;
246
247                 ret = -EIO;
248                 dp = ((unsigned long) child) + THREAD_SIZE -
249                          sizeof(struct pt_dspregs);
250                 if (*((int *) (dp - 4)) == SR_FD) {
251                         copy_from_user((void *) dp, addr,
252                                 sizeof(struct pt_dspregs));
253                         ret = 0;
254                 }
255                 break;
256         }
257 #endif
258         default:
259                 ret = ptrace_request(child, request, addr, data);
260                 break;
261         }
262
263         return ret;
264 }
265
266 asmlinkage void do_syscall_trace(void)
267 {
268         struct task_struct *tsk = current;
269
270         if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
271             !test_thread_flag(TIF_SINGLESTEP))
272                 return;
273         if (!(tsk->ptrace & PT_PTRACED))
274                 return;
275         /* the 0x80 provides a way for the tracing parent to distinguish
276            between a syscall stop and SIGTRAP delivery */
277         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
278                                  !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
279
280         /*
281          * this isn't the same as continuing with a signal, but it will do
282          * for normal use.  strace only continues with a signal if the
283          * stopping signal is not SIGTRAP.  -brl
284          */
285         if (tsk->exit_code) {
286                 send_sig(tsk->exit_code, tsk, 1);
287                 tsk->exit_code = 0;
288         }
289 }