vserver 2.0 rc7
[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 #include <linux/signal.h>
28
29 #include <asm/uaccess.h>
30 #include <asm/page.h>
31 #include <asm/pgtable.h>
32 #include <asm/system.h>
33 #include <asm/processor.h>
34 #include <asm/signal.h>
35
36 /* cpu depend functions */
37 extern long h8300_get_reg(struct task_struct *task, int regno);
38 extern int  h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
39 extern void h8300_disable_trace(struct task_struct *child);
40 extern void h8300_enable_trace(struct task_struct *child);
41
42 /*
43  * does not yet catch signals sent when the child dies.
44  * in exit.c or in signal.c.
45  */
46
47 inline
48 static int read_long(struct task_struct * tsk, unsigned long addr,
49         unsigned long * result)
50 {
51         *result = *(unsigned long *)addr;
52         return 0;
53 }
54
55 void ptrace_disable(struct task_struct *child)
56 {
57         h8300_disable_trace(child);
58 }
59
60 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
61 {
62         struct task_struct *child;
63         int ret;
64
65         lock_kernel();
66         ret = -EPERM;
67         if (request == PTRACE_TRACEME) {
68                 /* are we already being traced? */
69                 if (current->ptrace & PT_PTRACED)
70                         goto out;
71                 /* set the ptrace bit in the process flags. */
72                 current->ptrace |= PT_PTRACED;
73                 ret = 0;
74                 goto out;
75         }
76         ret = -ESRCH;
77         read_lock(&tasklist_lock);
78         child = find_task_by_pid(pid);
79         if (child)
80                 get_task_struct(child);
81         read_unlock(&tasklist_lock);
82         if (!child)
83                 goto out;
84         if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
85                 goto out_tsk;
86
87         ret = -EPERM;
88         if (pid == 1)           /* you may not mess with init */
89                 goto out_tsk;
90
91         if (request == PTRACE_ATTACH) {
92                 ret = ptrace_attach(child);
93                 goto out_tsk;
94         }
95         ret = ptrace_check_attach(child, request == PTRACE_KILL);
96         if (ret < 0)
97                 goto out_tsk;
98
99         switch (request) {
100                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
101                 case PTRACE_PEEKDATA: {
102                         unsigned long tmp;
103
104                         ret = read_long(child, addr, &tmp);
105                         if (ret < 0)
106                                 break ;
107                         ret = put_user(tmp, (unsigned long *) data);
108                         break ;
109                 }
110
111         /* read the word at location addr in the USER area. */
112                 case PTRACE_PEEKUSR: {
113                         unsigned long tmp = 0;
114                         
115                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
116                                 ret = -EIO;
117                                 break ;
118                         }
119                         
120                         ret = 0;  /* Default return condition */
121                         addr = addr >> 2; /* temporary hack. */
122
123                         if (addr < H8300_REGS_NO)
124                                 tmp = h8300_get_reg(child, addr);
125                         else {
126                                 switch(addr) {
127                                 case 49:
128                                         tmp = child->mm->start_code;
129                                         break ;
130                                 case 50:
131                                         tmp = child->mm->start_data;
132                                         break ;
133                                 case 51:
134                                         tmp = child->mm->end_code;
135                                         break ;
136                                 case 52:
137                                         tmp = child->mm->end_data;
138                                         break ;
139                                 default:
140                                         ret = -EIO;
141                                 }
142                         }
143                         if (!ret)
144                                 ret = put_user(tmp,(unsigned long *) data);
145                         break ;
146                 }
147
148       /* when I and D space are separate, this will have to be fixed. */
149                 case PTRACE_POKETEXT: /* write the word at location addr. */
150                 case PTRACE_POKEDATA:
151                         ret = 0;
152                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
153                                 break;
154                         ret = -EIO;
155                         break;
156
157                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
158                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
159                                 ret = -EIO;
160                                 break ;
161                         }
162                         addr = addr >> 2; /* temporary hack. */
163                             
164                         if (addr == PT_ORIG_ER0) {
165                                 ret = -EIO;
166                                 break ;
167                         }
168                         if (addr < H8300_REGS_NO) {
169                                 ret = h8300_put_reg(child, addr, data);
170                                 break ;
171                         }
172                         ret = -EIO;
173                         break ;
174                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
175                 case PTRACE_CONT: { /* restart after signal. */
176                         ret = -EIO;
177                         if (!valid_signal(data))
178                                 break ;
179                         if (request == PTRACE_SYSCALL)
180                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
181                         else
182                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
183                         child->exit_code = data;
184                         wake_up_process(child);
185                         /* make sure the single step bit is not set. */
186                         h8300_disable_trace(child);
187                         ret = 0;
188                 }
189
190 /*
191  * make the child exit.  Best I can do is send it a sigkill. 
192  * perhaps it should be put in the status that it wants to 
193  * exit.
194  */
195                 case PTRACE_KILL: {
196
197                         ret = 0;
198                         if (child->exit_state == EXIT_ZOMBIE) /* already dead */
199                                 break;
200                         child->exit_code = SIGKILL;
201                         h8300_disable_trace(child);
202                         wake_up_process(child);
203                         break;
204                 }
205
206                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
207                         ret = -EIO;
208                         if (!valid_signal(data))
209                                 break;
210                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
211                         child->exit_code = data;
212                         h8300_enable_trace(child);
213                         wake_up_process(child);
214                         ret = 0;
215                         break;
216                 }
217
218                 case PTRACE_DETACH:     /* detach a process that was attached. */
219                         ret = ptrace_detach(child, data);
220                         break;
221
222                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
223                         int i;
224                         unsigned long tmp;
225                         for (i = 0; i < H8300_REGS_NO; i++) {
226                             tmp = h8300_get_reg(child, i);
227                             if (put_user(tmp, (unsigned long *) data)) {
228                                 ret = -EFAULT;
229                                 break;
230                             }
231                             data += sizeof(long);
232                         }
233                         ret = 0;
234                         break;
235                 }
236
237                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
238                         int i;
239                         unsigned long tmp;
240                         for (i = 0; i < H8300_REGS_NO; i++) {
241                             if (get_user(tmp, (unsigned long *) data)) {
242                                 ret = -EFAULT;
243                                 break;
244                             }
245                             h8300_put_reg(child, i, tmp);
246                             data += sizeof(long);
247                         }
248                         ret = 0;
249                         break;
250                 }
251
252                 default:
253                         ret = -EIO;
254                         break;
255         }
256 out_tsk:
257         put_task_struct(child);
258 out:
259         unlock_kernel();
260         return ret;
261 }
262
263 asmlinkage void syscall_trace(void)
264 {
265         if (!test_thread_flag(TIF_SYSCALL_TRACE))
266                 return;
267         if (!(current->ptrace & PT_PTRACED))
268                 return;
269         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
270                                  ? 0x80 : 0));
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 (current->exit_code) {
277                 send_sig(current->exit_code, current, 1);
278                 current->exit_code = 0;
279         }
280 }