Merge branch 'reset-seq' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik/libat...
[linux-2.6] / arch / cris / arch-v10 / kernel / ptrace.c
1 /*
2  * Copyright (C) 2000-2003, Axis Communications AB.
3  */
4
5 #include <linux/kernel.h>
6 #include <linux/sched.h>
7 #include <linux/mm.h>
8 #include <linux/smp.h>
9 #include <linux/errno.h>
10 #include <linux/ptrace.h>
11 #include <linux/user.h>
12 #include <linux/signal.h>
13 #include <linux/security.h>
14
15 #include <asm/uaccess.h>
16 #include <asm/page.h>
17 #include <asm/pgtable.h>
18 #include <asm/system.h>
19 #include <asm/processor.h>
20
21 /* 
22  * Determines which bits in DCCR the user has access to.
23  * 1 = access, 0 = no access.
24  */
25 #define DCCR_MASK 0x0000001f     /* XNZVC */
26
27 /*
28  * Get contents of register REGNO in task TASK.
29  */
30 inline long get_reg(struct task_struct *task, unsigned int regno)
31 {
32         /* USP is a special case, it's not in the pt_regs struct but
33          * in the tasks thread struct
34          */
35
36         if (regno == PT_USP)
37                 return task->thread.usp;
38         else if (regno < PT_MAX)
39                 return ((unsigned long *)task_pt_regs(task))[regno];
40         else
41                 return 0;
42 }
43
44 /*
45  * Write contents of register REGNO in task TASK.
46  */
47 inline int put_reg(struct task_struct *task, unsigned int regno,
48                           unsigned long data)
49 {
50         if (regno == PT_USP)
51                 task->thread.usp = data;
52         else if (regno < PT_MAX)
53                 ((unsigned long *)task_pt_regs(task))[regno] = data;
54         else
55                 return -1;
56         return 0;
57 }
58
59 /*
60  * Called by kernel/ptrace.c when detaching.
61  *
62  * Make sure the single step bit is not set.
63  */
64 void 
65 ptrace_disable(struct task_struct *child)
66 {
67        /* Todo - pending singlesteps? */
68 }
69
70 /* 
71  * Note that this implementation of ptrace behaves differently from vanilla
72  * ptrace.  Contrary to what the man page says, in the PTRACE_PEEKTEXT,
73  * PTRACE_PEEKDATA, and PTRACE_PEEKUSER requests the data variable is not
74  * ignored.  Instead, the data variable is expected to point at a location
75  * (in user space) where the result of the ptrace call is written (instead of
76  * being returned).
77  */
78 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
79 {
80         int ret;
81         unsigned long __user *datap = (unsigned long __user *)data;
82
83         switch (request) {
84                 /* Read word at location address. */ 
85                 case PTRACE_PEEKTEXT:
86                 case PTRACE_PEEKDATA: {
87                         unsigned long tmp;
88                         int copied;
89
90                         copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
91                         ret = -EIO;
92                         
93                         if (copied != sizeof(tmp))
94                                 break;
95                         
96                         ret = put_user(tmp,datap);
97                         break;
98                 }
99
100                 /* Read the word at location address in the USER area. */
101                 case PTRACE_PEEKUSR: {
102                         unsigned long tmp;
103
104                         ret = -EIO;
105                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
106                                 break;
107
108                         tmp = get_reg(child, addr >> 2);
109                         ret = put_user(tmp, datap);
110                         break;
111                 }
112                 
113                 /* Write the word at location address. */
114                 case PTRACE_POKETEXT:
115                 case PTRACE_POKEDATA:
116                         ret = 0;
117                         
118                         if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
119                                 break;
120                         
121                         ret = -EIO;
122                         break;
123  
124                 /* Write the word at location address in the USER area. */
125                 case PTRACE_POKEUSR:
126                         ret = -EIO;
127                         if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
128                                 break;
129
130                         addr >>= 2;
131
132                         if (addr == PT_DCCR) {
133                                 /* don't allow the tracing process to change stuff like
134                                  * interrupt enable, kernel/user bit, dma enables etc.
135                                  */
136                                 data &= DCCR_MASK;
137                                 data |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
138                         }
139                         if (put_reg(child, addr, data))
140                                 break;
141                         ret = 0;
142                         break;
143
144                 case PTRACE_SYSCALL:
145                 case PTRACE_CONT:
146                         ret = -EIO;
147                         
148                         if (!valid_signal(data))
149                                 break;
150                         
151                         if (request == PTRACE_SYSCALL) {
152                                 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
153                         }
154                         else {
155                                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
156                         }
157                         
158                         child->exit_code = data;
159                         
160                         /* TODO: make sure any pending breakpoint is killed */
161                         wake_up_process(child);
162                         ret = 0;
163                         
164                         break;
165                 
166                 /* Make the child exit by sending it a sigkill. */
167                 case PTRACE_KILL:
168                         ret = 0;
169                         
170                         if (child->exit_state == EXIT_ZOMBIE)
171                                 break;
172                         
173                         child->exit_code = SIGKILL;
174                         
175                         /* TODO: make sure any pending breakpoint is killed */
176                         wake_up_process(child);
177                         break;
178
179                 /* Set the trap flag. */
180                 case PTRACE_SINGLESTEP:
181                         ret = -EIO;
182                         
183                         if (!valid_signal(data))
184                                 break;
185                         
186                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
187
188                         /* TODO: set some clever breakpoint mechanism... */
189
190                         child->exit_code = data;
191                         wake_up_process(child);
192                         ret = 0;
193                         break;
194
195                 case PTRACE_DETACH:
196                         ret = ptrace_detach(child, data);
197                         break;
198
199                 /* Get all GP registers from the child. */
200                 case PTRACE_GETREGS: {
201                         int i;
202                         unsigned long tmp;
203                         
204                         ret = 0;
205                         for (i = 0; i <= PT_MAX; i++) {
206                                 tmp = get_reg(child, i);
207                                 
208                                 if (put_user(tmp, datap)) {
209                                         ret = -EFAULT;
210                                         break;
211                                 }
212                                 
213                                 data += sizeof(long);
214                         }
215
216                         break;
217                 }
218
219                 /* Set all GP registers in the child. */
220                 case PTRACE_SETREGS: {
221                         int i;
222                         unsigned long tmp;
223                         
224                         ret = 0;
225                         for (i = 0; i <= PT_MAX; i++) {
226                                 if (get_user(tmp, datap)) {
227                                         ret = -EFAULT;
228                                         break;
229                                 }
230                                 
231                                 if (i == PT_DCCR) {
232                                         tmp &= DCCR_MASK;
233                                         tmp |= get_reg(child, PT_DCCR) & ~DCCR_MASK;
234                                 }
235                                 
236                                 put_reg(child, i, tmp);
237                                 data += sizeof(long);
238                         }
239                         
240                         break;
241                 }
242
243                 default:
244                         ret = ptrace_request(child, request, addr, data);
245                         break;
246         }
247
248         return ret;
249 }
250
251 void do_syscall_trace(void)
252 {
253         if (!test_thread_flag(TIF_SYSCALL_TRACE))
254                 return;
255         
256         if (!(current->ptrace & PT_PTRACED))
257                 return;
258         
259         /* the 0x80 provides a way for the tracing parent to distinguish
260            between a syscall stop and SIGTRAP delivery */
261         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
262                                  ? 0x80 : 0));
263         
264         /*
265          * This isn't the same as continuing with a signal, but it will do for
266          * normal use.
267          */
268         if (current->exit_code) {
269                 send_sig(current->exit_code, current, 1);
270                 current->exit_code = 0;
271         }
272 }