[PATCH] consolidate sys_ptrace()
[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 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
61 {
62         int ret;
63
64         switch (request) {
65                 case PTRACE_PEEKTEXT: /* read word at location addr. */ 
66                 case PTRACE_PEEKDATA: {
67                         unsigned long tmp;
68
69                         ret = read_long(child, addr, &tmp);
70                         if (ret < 0)
71                                 break ;
72                         ret = put_user(tmp, (unsigned long *) data);
73                         break ;
74                 }
75
76         /* read the word at location addr in the USER area. */
77                 case PTRACE_PEEKUSR: {
78                         unsigned long tmp = 0;
79                         
80                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
81                                 ret = -EIO;
82                                 break ;
83                         }
84                         
85                         ret = 0;  /* Default return condition */
86                         addr = addr >> 2; /* temporary hack. */
87
88                         if (addr < H8300_REGS_NO)
89                                 tmp = h8300_get_reg(child, addr);
90                         else {
91                                 switch(addr) {
92                                 case 49:
93                                         tmp = child->mm->start_code;
94                                         break ;
95                                 case 50:
96                                         tmp = child->mm->start_data;
97                                         break ;
98                                 case 51:
99                                         tmp = child->mm->end_code;
100                                         break ;
101                                 case 52:
102                                         tmp = child->mm->end_data;
103                                         break ;
104                                 default:
105                                         ret = -EIO;
106                                 }
107                         }
108                         if (!ret)
109                                 ret = put_user(tmp,(unsigned long *) data);
110                         break ;
111                 }
112
113       /* when I and D space are separate, this will have to be fixed. */
114                 case PTRACE_POKETEXT: /* write the word at location addr. */
115                 case PTRACE_POKEDATA:
116                         ret = 0;
117                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
118                                 break;
119                         ret = -EIO;
120                         break;
121
122                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
123                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
124                                 ret = -EIO;
125                                 break ;
126                         }
127                         addr = addr >> 2; /* temporary hack. */
128                             
129                         if (addr == PT_ORIG_ER0) {
130                                 ret = -EIO;
131                                 break ;
132                         }
133                         if (addr < H8300_REGS_NO) {
134                                 ret = h8300_put_reg(child, addr, data);
135                                 break ;
136                         }
137                         ret = -EIO;
138                         break ;
139                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
140                 case PTRACE_CONT: { /* restart after signal. */
141                         ret = -EIO;
142                         if (!valid_signal(data))
143                                 break ;
144                         if (request == PTRACE_SYSCALL)
145                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
146                         else
147                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
148                         child->exit_code = data;
149                         wake_up_process(child);
150                         /* make sure the single step bit is not set. */
151                         h8300_disable_trace(child);
152                         ret = 0;
153                 }
154
155 /*
156  * make the child exit.  Best I can do is send it a sigkill. 
157  * perhaps it should be put in the status that it wants to 
158  * exit.
159  */
160                 case PTRACE_KILL: {
161
162                         ret = 0;
163                         if (child->exit_state == EXIT_ZOMBIE) /* already dead */
164                                 break;
165                         child->exit_code = SIGKILL;
166                         h8300_disable_trace(child);
167                         wake_up_process(child);
168                         break;
169                 }
170
171                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
172                         ret = -EIO;
173                         if (!valid_signal(data))
174                                 break;
175                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
176                         child->exit_code = data;
177                         h8300_enable_trace(child);
178                         wake_up_process(child);
179                         ret = 0;
180                         break;
181                 }
182
183                 case PTRACE_DETACH:     /* detach a process that was attached. */
184                         ret = ptrace_detach(child, data);
185                         break;
186
187                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
188                         int i;
189                         unsigned long tmp;
190                         for (i = 0; i < H8300_REGS_NO; i++) {
191                             tmp = h8300_get_reg(child, i);
192                             if (put_user(tmp, (unsigned long *) data)) {
193                                 ret = -EFAULT;
194                                 break;
195                             }
196                             data += sizeof(long);
197                         }
198                         ret = 0;
199                         break;
200                 }
201
202                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
203                         int i;
204                         unsigned long tmp;
205                         for (i = 0; i < H8300_REGS_NO; i++) {
206                             if (get_user(tmp, (unsigned long *) data)) {
207                                 ret = -EFAULT;
208                                 break;
209                             }
210                             h8300_put_reg(child, i, tmp);
211                             data += sizeof(long);
212                         }
213                         ret = 0;
214                         break;
215                 }
216
217                 default:
218                         ret = -EIO;
219                         break;
220         }
221         return ret;
222 }
223
224 asmlinkage void syscall_trace(void)
225 {
226         if (!test_thread_flag(TIF_SYSCALL_TRACE))
227                 return;
228         if (!(current->ptrace & PT_PTRACED))
229                 return;
230         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
231                                  ? 0x80 : 0));
232         /*
233          * this isn't the same as continuing with a signal, but it will do
234          * for normal use.  strace only continues with a signal if the
235          * stopping signal is not SIGTRAP.  -brl
236          */
237         if (current->exit_code) {
238                 send_sig(current->exit_code, current, 1);
239                 current->exit_code = 0;
240         }
241 }