sh: Move in the SH-5 ptrace impl.
[safe/jmp/linux-2.6] / arch / sh / kernel / ptrace_32.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                 ret = generic_ptrace_peekdata(child, addr, data);
96                 break;
97
98         /* read the word at location addr in the USER area. */
99         case PTRACE_PEEKUSR: {
100                 unsigned long tmp;
101
102                 ret = -EIO;
103                 if ((addr & 3) || addr < 0 ||
104                     addr > sizeof(struct user) - 3)
105                         break;
106
107                 if (addr < sizeof(struct pt_regs))
108                         tmp = get_stack_long(child, addr);
109                 else if (addr >= (long) &dummy->fpu &&
110                          addr < (long) &dummy->u_fpvalid) {
111                         if (!tsk_used_math(child)) {
112                                 if (addr == (long)&dummy->fpu.fpscr)
113                                         tmp = FPSCR_INIT;
114                                 else
115                                         tmp = 0;
116                         } else
117                                 tmp = ((long *)&child->thread.fpu)
118                                         [(addr - (long)&dummy->fpu) >> 2];
119                 } else if (addr == (long) &dummy->u_fpvalid)
120                         tmp = !!tsk_used_math(child);
121                 else
122                         tmp = 0;
123                 ret = put_user(tmp, (unsigned long __user *)data);
124                 break;
125         }
126
127         /* when I and D space are separate, this will have to be fixed. */
128         case PTRACE_POKETEXT: /* write the word at location addr. */
129         case PTRACE_POKEDATA:
130                 ret = generic_ptrace_pokedata(child, addr, data);
131                 break;
132
133         case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
134                 ret = -EIO;
135                 if ((addr & 3) || addr < 0 ||
136                     addr > sizeof(struct user) - 3)
137                         break;
138
139                 if (addr < sizeof(struct pt_regs))
140                         ret = put_stack_long(child, addr, data);
141                 else if (addr >= (long) &dummy->fpu &&
142                          addr < (long) &dummy->u_fpvalid) {
143                         set_stopped_child_used_math(child);
144                         ((long *)&child->thread.fpu)
145                                 [(addr - (long)&dummy->fpu) >> 2] = data;
146                         ret = 0;
147                 } else if (addr == (long) &dummy->u_fpvalid) {
148                         conditional_stopped_child_used_math(data, child);
149                         ret = 0;
150                 }
151                 break;
152
153         case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
154         case PTRACE_CONT: { /* restart after signal. */
155                 ret = -EIO;
156                 if (!valid_signal(data))
157                         break;
158                 if (request == PTRACE_SYSCALL)
159                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
160                 else
161                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
162
163                 ptrace_disable_singlestep(child);
164
165                 child->exit_code = data;
166                 wake_up_process(child);
167                 ret = 0;
168                 break;
169         }
170
171 /*
172  * make the child exit.  Best I can do is send it a sigkill.
173  * perhaps it should be put in the status that it wants to
174  * exit.
175  */
176         case PTRACE_KILL: {
177                 ret = 0;
178                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
179                         break;
180                 ptrace_disable_singlestep(child);
181                 child->exit_code = SIGKILL;
182                 wake_up_process(child);
183                 break;
184         }
185
186         case PTRACE_SINGLESTEP: {  /* set the trap flag. */
187                 long pc;
188                 struct pt_regs *regs = NULL;
189
190                 ret = -EIO;
191                 if (!valid_signal(data))
192                         break;
193                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
194                 if ((child->ptrace & PT_DTRACE) == 0) {
195                         /* Spurious delayed TF traps may occur */
196                         child->ptrace |= PT_DTRACE;
197                 }
198
199                 pc = get_stack_long(child, (long)&regs->pc);
200
201                 /* Next scheduling will set up UBC */
202                 if (child->thread.ubc_pc == 0)
203                         ubc_usercnt += 1;
204                 child->thread.ubc_pc = pc;
205
206                 set_tsk_thread_flag(child, TIF_SINGLESTEP);
207                 child->exit_code = data;
208                 /* give it a chance to run. */
209                 wake_up_process(child);
210                 ret = 0;
211                 break;
212         }
213
214 #ifdef CONFIG_SH_DSP
215         case PTRACE_GETDSPREGS: {
216                 unsigned long dp;
217
218                 ret = -EIO;
219                 dp = ((unsigned long) child) + THREAD_SIZE -
220                          sizeof(struct pt_dspregs);
221                 if (*((int *) (dp - 4)) == SR_FD) {
222                         copy_to_user(addr, (void *) dp,
223                                 sizeof(struct pt_dspregs));
224                         ret = 0;
225                 }
226                 break;
227         }
228
229         case PTRACE_SETDSPREGS: {
230                 unsigned long dp;
231
232                 ret = -EIO;
233                 dp = ((unsigned long) child) + THREAD_SIZE -
234                          sizeof(struct pt_dspregs);
235                 if (*((int *) (dp - 4)) == SR_FD) {
236                         copy_from_user((void *) dp, addr,
237                                 sizeof(struct pt_dspregs));
238                         ret = 0;
239                 }
240                 break;
241         }
242 #endif
243         default:
244                 ret = ptrace_request(child, request, addr, data);
245                 break;
246         }
247
248         return ret;
249 }
250
251 asmlinkage void do_syscall_trace(void)
252 {
253         struct task_struct *tsk = current;
254
255         if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
256             !test_thread_flag(TIF_SINGLESTEP))
257                 return;
258         if (!(tsk->ptrace & PT_PTRACED))
259                 return;
260         /* the 0x80 provides a way for the tracing parent to distinguish
261            between a syscall stop and SIGTRAP delivery */
262         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
263                                  !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
264
265         /*
266          * this isn't the same as continuing with a signal, but it will do
267          * for normal use.  strace only continues with a signal if the
268          * stopping signal is not SIGTRAP.  -brl
269          */
270         if (tsk->exit_code) {
271                 send_sig(tsk->exit_code, tsk, 1);
272                 tsk->exit_code = 0;
273         }
274 }