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