VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / 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         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
84                 goto out_tsk;
85
86         ret = -EPERM;
87         if (pid == 1)           /* you may not mess with init */
88                 goto out_tsk;
89
90         if (request == PTRACE_ATTACH) {
91                 ret = ptrace_attach(child);
92                 goto out_tsk;
93         }
94         ret = -ESRCH;
95         if (!(child->ptrace & PT_PTRACED))
96                 goto out_tsk;
97         if (child->state != TASK_STOPPED) {
98                 if (request != PTRACE_KILL)
99                         goto out_tsk;
100         }
101         ret = ptrace_check_attach(child, request == PTRACE_KILL);
102         if (ret < 0)
103                 goto out_tsk;
104
105         switch (request) {
106                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
107                 case PTRACE_PEEKDATA: {
108                         unsigned long tmp;
109
110                         ret = read_long(child, addr, &tmp);
111                         if (ret < 0)
112                                 break ;
113                         ret = put_user(tmp, (unsigned long *) data);
114                         break ;
115                 }
116
117         /* read the word at location addr in the USER area. */
118                 case PTRACE_PEEKUSR: {
119                         unsigned long tmp;
120                         
121                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
122                                 ret = -EIO;
123                                 break ;
124                         }
125                         
126                         ret = 0;  /* Default return condition */
127                         addr = addr >> 2; /* temporary hack. */
128
129                         if (addr < H8300_REGS_NO)
130                                 tmp = h8300_get_reg(child, addr);
131                         else {
132                                 switch(addr) {
133                                 case 49:
134                                         tmp = child->mm->start_code;
135                                         break ;
136                                 case 50:
137                                         tmp = child->mm->start_data;
138                                         break ;
139                                 case 51:
140                                         tmp = child->mm->end_code;
141                                         break ;
142                                 case 52:
143                                         tmp = child->mm->end_data;
144                                         break ;
145                                 default:
146                                         ret = -EIO;
147                                 }
148                         }
149                         if (!ret)
150                                 ret = put_user(tmp,(unsigned long *) data);
151                         break ;
152                 }
153
154       /* when I and D space are separate, this will have to be fixed. */
155                 case PTRACE_POKETEXT: /* write the word at location addr. */
156                 case PTRACE_POKEDATA:
157                         ret = 0;
158                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
159                                 break;
160                         ret = -EIO;
161                         break;
162
163                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
164                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
165                                 ret = -EIO;
166                                 break ;
167                         }
168                         addr = addr >> 2; /* temporary hack. */
169                             
170                         if (addr == PT_ORIG_ER0) {
171                                 ret = -EIO;
172                                 break ;
173                         }
174                         if (addr < H8300_REGS_NO) {
175                                 ret = h8300_put_reg(child, addr, data);
176                                 break ;
177                         }
178                         ret = -EIO;
179                         break ;
180                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
181                 case PTRACE_CONT: { /* restart after signal. */
182                         ret = -EIO;
183                         if ((unsigned long) data >= _NSIG)
184                                 break ;
185                         if (request == PTRACE_SYSCALL)
186                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
187                         else
188                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
189                         child->exit_code = data;
190                         wake_up_process(child);
191                         /* make sure the single step bit is not set. */
192                         h8300_disable_trace(child);
193                         ret = 0;
194                 }
195
196 /*
197  * make the child exit.  Best I can do is send it a sigkill. 
198  * perhaps it should be put in the status that it wants to 
199  * exit.
200  */
201                 case PTRACE_KILL: {
202
203                         ret = 0;
204                         if (child->state == TASK_ZOMBIE) /* already dead */
205                                 break;
206                         child->exit_code = SIGKILL;
207                         h8300_disable_trace(child);
208                         wake_up_process(child);
209                         break;
210                 }
211
212                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
213                         ret = -EIO;
214                         if ((unsigned long) data > _NSIG)
215                                 break;
216                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
217                         child->exit_code = data;
218                         h8300_enable_trace(child);
219                         wake_up_process(child);
220                         ret = 0;
221                         break;
222                 }
223
224                 case PTRACE_DETACH:     /* detach a process that was attached. */
225                         ret = ptrace_detach(child, data);
226                         break;
227
228                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
229                         int i;
230                         unsigned long tmp;
231                         for (i = 0; i < H8300_REGS_NO; i++) {
232                             tmp = h8300_get_reg(child, i);
233                             if (put_user(tmp, (unsigned long *) data)) {
234                                 ret = -EFAULT;
235                                 break;
236                             }
237                             data += sizeof(long);
238                         }
239                         ret = 0;
240                         break;
241                 }
242
243                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
244                         int i;
245                         unsigned long tmp;
246                         for (i = 0; i < H8300_REGS_NO; i++) {
247                             if (get_user(tmp, (unsigned long *) data)) {
248                                 ret = -EFAULT;
249                                 break;
250                             }
251                             h8300_put_reg(child, i, tmp);
252                             data += sizeof(long);
253                         }
254                         ret = 0;
255                         break;
256                 }
257
258                 default:
259                         ret = -EIO;
260                         break;
261         }
262 out_tsk:
263         put_task_struct(child);
264 out:
265         unlock_kernel();
266         return ret;
267 }
268
269 asmlinkage void syscall_trace(void)
270 {
271         if (!test_thread_flag(TIF_SYSCALL_TRACE))
272                 return;
273         if (!(current->ptrace & PT_PTRACED))
274                 return;
275         current->exit_code = SIGTRAP;
276         current->state = TASK_STOPPED;
277         notify_parent(current, SIGCHLD);
278         schedule();
279         /*
280          * this isn't the same as continuing with a signal, but it will do
281          * for normal use.  strace only continues with a signal if the
282          * stopping signal is not SIGTRAP.  -brl
283          */
284         if (current->exit_code) {
285                 send_sig(current->exit_code, current, 1);
286                 current->exit_code = 0;
287         }
288 }