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