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