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