ptrace: use ptrace_request() in the remaining architectures
[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/errno.h>
23 #include <linux/ptrace.h>
24 #include <linux/user.h>
25 #include <linux/signal.h>
26
27 #include <asm/uaccess.h>
28 #include <asm/page.h>
29 #include <asm/pgtable.h>
30 #include <asm/system.h>
31 #include <asm/processor.h>
32 #include <asm/signal.h>
33
34 /* cpu depend functions */
35 extern long h8300_get_reg(struct task_struct *task, int regno);
36 extern int  h8300_put_reg(struct task_struct *task, int regno, unsigned long data);
37 extern void h8300_disable_trace(struct task_struct *child);
38 extern void h8300_enable_trace(struct task_struct *child);
39
40 /*
41  * does not yet catch signals sent when the child dies.
42  * in exit.c or in signal.c.
43  */
44
45 void ptrace_disable(struct task_struct *child)
46 {
47         h8300_disable_trace(child);
48 }
49
50 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
51 {
52         int ret;
53
54         switch (request) {
55         /* read the word at location addr in the USER area. */
56                 case PTRACE_PEEKUSR: {
57                         unsigned long tmp = 0;
58                         
59                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
60                                 ret = -EIO;
61                                 break ;
62                         }
63                         
64                         ret = 0;  /* Default return condition */
65                         addr = addr >> 2; /* temporary hack. */
66
67                         if (addr < H8300_REGS_NO)
68                                 tmp = h8300_get_reg(child, addr);
69                         else {
70                                 switch(addr) {
71                                 case 49:
72                                         tmp = child->mm->start_code;
73                                         break ;
74                                 case 50:
75                                         tmp = child->mm->start_data;
76                                         break ;
77                                 case 51:
78                                         tmp = child->mm->end_code;
79                                         break ;
80                                 case 52:
81                                         tmp = child->mm->end_data;
82                                         break ;
83                                 default:
84                                         ret = -EIO;
85                                 }
86                         }
87                         if (!ret)
88                                 ret = put_user(tmp,(unsigned long *) data);
89                         break ;
90                 }
91
92       /* when I and D space are separate, this will have to be fixed. */
93                 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
94                         if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
95                                 ret = -EIO;
96                                 break ;
97                         }
98                         addr = addr >> 2; /* temporary hack. */
99                             
100                         if (addr == PT_ORIG_ER0) {
101                                 ret = -EIO;
102                                 break ;
103                         }
104                         if (addr < H8300_REGS_NO) {
105                                 ret = h8300_put_reg(child, addr, data);
106                                 break ;
107                         }
108                         ret = -EIO;
109                         break ;
110                 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
111                 case PTRACE_CONT: { /* restart after signal. */
112                         ret = -EIO;
113                         if (!valid_signal(data))
114                                 break ;
115                         if (request == PTRACE_SYSCALL)
116                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
117                         else
118                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
119                         child->exit_code = data;
120                         wake_up_process(child);
121                         /* make sure the single step bit is not set. */
122                         h8300_disable_trace(child);
123                         ret = 0;
124                 }
125
126 /*
127  * make the child exit.  Best I can do is send it a sigkill. 
128  * perhaps it should be put in the status that it wants to 
129  * exit.
130  */
131                 case PTRACE_KILL: {
132
133                         ret = 0;
134                         if (child->exit_state == EXIT_ZOMBIE) /* already dead */
135                                 break;
136                         child->exit_code = SIGKILL;
137                         h8300_disable_trace(child);
138                         wake_up_process(child);
139                         break;
140                 }
141
142                 case PTRACE_SINGLESTEP: {  /* set the trap flag. */
143                         ret = -EIO;
144                         if (!valid_signal(data))
145                                 break;
146                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
147                         child->exit_code = data;
148                         h8300_enable_trace(child);
149                         wake_up_process(child);
150                         ret = 0;
151                         break;
152                 }
153
154                 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
155                         int i;
156                         unsigned long tmp;
157                         for (i = 0; i < H8300_REGS_NO; i++) {
158                             tmp = h8300_get_reg(child, i);
159                             if (put_user(tmp, (unsigned long *) data)) {
160                                 ret = -EFAULT;
161                                 break;
162                             }
163                             data += sizeof(long);
164                         }
165                         ret = 0;
166                         break;
167                 }
168
169                 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
170                         int i;
171                         unsigned long tmp;
172                         for (i = 0; i < H8300_REGS_NO; i++) {
173                             if (get_user(tmp, (unsigned long *) data)) {
174                                 ret = -EFAULT;
175                                 break;
176                             }
177                             h8300_put_reg(child, i, tmp);
178                             data += sizeof(long);
179                         }
180                         ret = 0;
181                         break;
182                 }
183
184                 default:
185                         ret = ptrace_request(child, request, addr, data);
186                         break;
187         }
188         return ret;
189 }
190
191 asmlinkage void do_syscall_trace(void)
192 {
193         if (!test_thread_flag(TIF_SYSCALL_TRACE))
194                 return;
195         if (!(current->ptrace & PT_PTRACED))
196                 return;
197         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
198                                  ? 0x80 : 0));
199         /*
200          * this isn't the same as continuing with a signal, but it will do
201          * for normal use.  strace only continues with a signal if the
202          * stopping signal is not SIGTRAP.  -brl
203          */
204         if (current->exit_code) {
205                 send_sig(current->exit_code, current, 1);
206                 current->exit_code = 0;
207         }
208 }