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