http://downloads.netgear.com/files/GPL/DM111PSP_v3.61d_GPL.tar.gz
[bcm963xx.git] / kernel / linux / arch / h8300 / kernel / ptrace.c
1 /*
2  *  linux/arch/h8300/kernel/ptrace.c
3  *
4  *  Yoshinori Sato <ysato@users.sourceforge.jp>
5  *
6  *  Based on:
7  *  linux/arch/m68k/kernel/ptrace.c
8  *
9  *  Copyright (C) 1994 by Hamish Macdonald
10  *  Taken from linux/kernel/ptrace.c and modified for M680x0.
11  *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
12  *
13  * This file is subject to the terms and conditions of the GNU General
14  * Public License.  See the file COPYING in the main directory of
15  * this archive for more details.
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/mm.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/config.h>
27
28 #include <asm/uaccess.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/processor.h>
33 #include <asm/signal.h>
34
35 /* cpu depend functions */
36 extern long h8300_get_reg(struct task_struct *task, int regno);
37 extern int  h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
38 extern void h8300_disable_trace(struct task_struct *child);
39 extern void h8300_enable_trace(struct task_struct *child);
40
41 /*
42  * does not yet catch signals sent when the child dies.
43  * in exit.c or in signal.c.
44  */
45
46 inline
47 static int read_long(struct task_struct * tsk, unsigned long addr,
48         unsigned long * result)
49 {
50         *result = *(unsigned long *)addr;
51         return 0;
52 }
53
54 void ptrace_disable(struct task_struct *child)
55 {
56         h8300_disable_trace(child);
57 }
58
59 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
60 {
61         struct task_struct *child;
62         int ret;
63
64         lock_kernel();
65         ret = -EPERM;
66         if (request == PTRACE_TRACEME) {
67                 /* are we already being traced? */
68                 if (current->ptrace & PT_PTRACED)
69                         goto out;
70                 /* set the ptrace bit in the process flags. */
71                 current->ptrace |= PT_PTRACED;
72                 ret = 0;
73                 goto out;
74         }
75         ret = -ESRCH;
76         read_lock(&tasklist_lock);
77         child = find_task_by_pid(pid);
78         if (child)
79                 get_task_struct(child);
80         read_unlock(&tasklist_lock);
81         if (!child)
82                 goto out;
83
84         ret = -EPERM;
85         if (pid == 1)           /* you may not mess with init */
86                 goto out_tsk;
87
88         if (request == PTRACE_ATTACH) {
89                 ret = ptrace_attach(child);
90                 goto out_tsk;
91         }
92         ret = -ESRCH;
93         if (!(child->ptrace & PT_PTRACED))
94                 goto out_tsk;
95         if (child->state != TASK_STOPPED) {
96                 if (request != PTRACE_KILL)
97                         goto out_tsk;
98         }
99         ret = ptrace_check_attach(child, request == PTRACE_KILL);
100         if (ret < 0)
101                 goto out_tsk;
102
103         switch (request) {
104                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
105                 case PTRACE_PEEKDATA: {
106                         unsigned long tmp;
107
108                         ret = read_long(child, addr, &tmp);
109                         if (ret < 0)
110                                 break ;
111                         ret = put_user(tmp, (unsigned long *) data);
112                         break ;
113                 }
114
115         /* read the word at location addr in the USER area. */
116                 case PTRACE_PEEKUSR: {
117                         unsigned long tmp;
118                         
119                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
120                                 ret = -EIO;
121                                 break ;
122                         }
123                         
124                         ret = 0;  /* Default return condition */
125                         addr = addr >> 2; /* temporary hack. */
126
127                         if (addr < H8300_REGS_NO)
128                                 tmp = h8300_get_reg(child, addr);
129                         else {
130                                 switch(addr) {
131                                 case 49:
132                                         tmp = child->mm->start_code;
133                                         break ;
134                                 case 50:
135                                         tmp = child->mm->start_data;
136                                         break ;
137                                 case 51:
138                                         tmp = child->mm->end_code;
139                                         break ;
140                                 case 52:
141                                         tmp = child->mm->end_data;
142                                         break ;
143                                 default:
144                                         ret = -EIO;
145                                 }
146                         }
147                         if (!ret)
148                                 ret = put_user(tmp,(unsigned long *) data);
149                         break ;
150                 }
151
152       /* when I and D space are separate, this will have to be fixed. */
153                 case PTRACE_POKETEXT: /* write the word at location addr. */
154                 case PTRACE_POKEDATA:
155                         ret = 0;
156                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
157                                 break;
158                         ret = -EIO;
159                         break;
160
161                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
162                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
163                                 ret = -EIO;
164                                 break ;
165                         }
166                         addr = addr >> 2; /* temporary hack. */
167                             
168                         if (addr == PT_ORIG_ER0) {
169                                 ret = -EIO;
170                                 break ;
171                         }
172                         if (addr < H8300_REGS_NO) {
173                                 ret = h8300_put_reg(child, addr, data);
174                                 break ;
175                         }
176                         ret = -EIO;
177                         break ;
178                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
179                 case PTRACE_CONT: { /* restart after signal. */
180                         ret = -EIO;
181                         if ((unsigned long) data >= _NSIG)
182                                 break ;
183                         if (request == PTRACE_SYSCALL)
184                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
185                         else
186                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
187                         child->exit_code = data;
188                         wake_up_process(child);
189                         /* make sure the single step bit is not set. */
190                         h8300_disable_trace(child);
191                         ret = 0;
192                 }
193
194 /*
195  * make the child exit.  Best I can do is send it a sigkill. 
196  * perhaps it should be put in the status that it wants to 
197  * exit.
198  */
199                 case PTRACE_KILL: {
200
201                         ret = 0;
202                         if (child->state == TASK_ZOMBIE) /* already dead */
203                                 break;
204                         child->exit_code = SIGKILL;
205                         h8300_disable_trace(child);
206                         wake_up_process(child);
207                         break;
208                 }
209
210                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
211                         ret = -EIO;
212                         if ((unsigned long) data > _NSIG)
213                                 break;
214                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
215                         child->exit_code = data;
216                         h8300_enable_trace(child);
217                         wake_up_process(child);
218                         ret = 0;
219                         break;
220                 }
221
222                 case PTRACE_DETACH:     /* detach a process that was attached. */
223                         ret = ptrace_detach(child, data);
224                         break;
225
226                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
227                         int i;
228                         unsigned long tmp;
229                         for (i = 0; i < H8300_REGS_NO; i++) {
230                             tmp = h8300_get_reg(child, i);
231                             if (put_user(tmp, (unsigned long *) data)) {
232                                 ret = -EFAULT;
233                                 break;
234                             }
235                             data += sizeof(long);
236                         }
237                         ret = 0;
238                         break;
239                 }
240
241                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
242                         int i;
243                         unsigned long tmp;
244                         for (i = 0; i < H8300_REGS_NO; i++) {
245                             if (get_user(tmp, (unsigned long *) data)) {
246                                 ret = -EFAULT;
247                                 break;
248                             }
249                             h8300_put_reg(child, i, tmp);
250                             data += sizeof(long);
251                         }
252                         ret = 0;
253                         break;
254                 }
255
256                 default:
257                         ret = -EIO;
258                         break;
259         }
260 out_tsk:
261         put_task_struct(child);
262 out:
263         unlock_kernel();
264         return ret;
265 }
266
267 asmlinkage void syscall_trace(void)
268 {
269         if (!test_thread_flag(TIF_SYSCALL_TRACE))
270                 return;
271         if (!(current->ptrace & PT_PTRACED))
272                 return;
273         current->exit_code = SIGTRAP;
274         current->state = TASK_STOPPED;
275         notify_parent(current, SIGCHLD);
276         schedule();
277         /*
278          * this isn't the same as continuing with a signal, but it will do
279          * for normal use.  strace only continues with a signal if the
280          * stopping signal is not SIGTRAP.  -brl
281          */
282         if (current->exit_code) {
283                 send_sig(current->exit_code, current, 1);
284                 current->exit_code = 0;
285         }
286 }