Pull ec into test branch
[linux-2.6] / arch / xtensa / kernel / ptrace.c
1 // TODO some minor issues
2 /*
3  * This file is subject to the terms and conditions of the GNU General Public
4  * License.  See the file "COPYING" in the main directory of this archive
5  * for more details.
6  *
7  * Copyright (C) 2001 - 2005  Tensilica Inc.
8  *
9  * Joe Taylor   <joe@tensilica.com, joetylr@yahoo.com>
10  * Chris Zankel <chris@zankel.net>
11  * Scott Foehner<sfoehner@yahoo.com>,
12  * Kevin Chea
13  * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/mm.h>
19 #include <linux/errno.h>
20 #include <linux/ptrace.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/security.h>
24 #include <linux/signal.h>
25
26 #include <asm/pgtable.h>
27 #include <asm/page.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/ptrace.h>
31 #include <asm/elf.h>
32
33 #define TEST_KERNEL     // verify kernel operations FIXME: remove
34
35
36 /*
37  * Called by kernel/ptrace.c when detaching..
38  *
39  * Make sure single step bits etc are not set.
40  */
41
42 void ptrace_disable(struct task_struct *child)
43 {
44         /* Nothing to do.. */
45 }
46
47 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
48 {
49         int ret = -EPERM;
50
51         switch (request) {
52         case PTRACE_PEEKTEXT: /* read word at location addr. */
53         case PTRACE_PEEKDATA:
54         {
55                 unsigned long tmp;
56                 int copied;
57
58                 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
59                 ret = -EIO;
60                 if (copied != sizeof(tmp))
61                         break;
62                 ret = put_user(tmp,(unsigned long *) data);
63
64                 goto out;
65         }
66
67         /* Read the word at location addr in the USER area.  */
68
69         case PTRACE_PEEKUSR:
70                 {
71                 struct pt_regs *regs;
72                 unsigned long tmp;
73
74                 regs = task_pt_regs(child);
75                 tmp = 0;  /* Default return value. */
76
77                 switch(addr) {
78
79                 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
80                         {
81                         int ar = addr - REG_AR_BASE - regs->windowbase * 4;
82                         ar &= (XCHAL_NUM_AREGS - 1);
83                         if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
84                                 tmp = regs->areg[ar];
85                         else
86                                 ret = -EIO;
87                         break;
88                         }
89                 case REG_A_BASE ... REG_A_BASE + 15:
90                         tmp = regs->areg[addr - REG_A_BASE];
91                         break;
92                 case REG_PC:
93                         tmp = regs->pc;
94                         break;
95                 case REG_PS:
96                         /* Note:  PS.EXCM is not set while user task is running;
97                          * its being set in regs is for exception handling
98                          * convenience.  */
99                         tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
100                         break;
101                 case REG_WB:
102                         tmp = regs->windowbase;
103                         break;
104                 case REG_WS:
105                         tmp = regs->windowstart;
106                         break;
107                 case REG_LBEG:
108                         tmp = regs->lbeg;
109                         break;
110                 case REG_LEND:
111                         tmp = regs->lend;
112                         break;
113                 case REG_LCOUNT:
114                         tmp = regs->lcount;
115                         break;
116                 case REG_SAR:
117                         tmp = regs->sar;
118                         break;
119                 case REG_DEPC:
120                         tmp = regs->depc;
121                         break;
122                 case REG_EXCCAUSE:
123                         tmp = regs->exccause;
124                         break;
125                 case REG_EXCVADDR:
126                         tmp = regs->excvaddr;
127                         break;
128                 case SYSCALL_NR:
129                         tmp = regs->syscall;
130                         break;
131                 default:
132                         tmp = 0;
133                         ret = -EIO;
134                         goto out;
135                 }
136                 ret = put_user(tmp, (unsigned long *) data);
137                 goto out;
138                 }
139
140         case PTRACE_POKETEXT: /* write the word at location addr. */
141         case PTRACE_POKEDATA:
142                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
143                     == sizeof(data))
144                         break;
145                 ret = -EIO;
146                 goto out;
147
148         case PTRACE_POKEUSR:
149                 {
150                 struct pt_regs *regs;
151                 regs = task_pt_regs(child);
152
153                 switch (addr) {
154                 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
155                         {
156                         int ar = addr - REG_AR_BASE - regs->windowbase * 4;
157                         if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
158                                 regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data;
159                         else
160                                 ret = -EIO;
161                         break;
162                         }
163                 case REG_A_BASE ... REG_A_BASE + 15:
164                         regs->areg[addr - REG_A_BASE] = data;
165                         break;
166                 case REG_PC:
167                         regs->pc = data;
168                         break;
169                 case SYSCALL_NR:
170                         regs->syscall = data;
171                         break;
172 #ifdef TEST_KERNEL
173                 case REG_WB:
174                         regs->windowbase = data;
175                         break;
176                 case REG_WS:
177                         regs->windowstart = data;
178                         break;
179 #endif
180
181                 default:
182                         /* The rest are not allowed. */
183                         ret = -EIO;
184                         break;
185                 }
186                 break;
187                 }
188
189         /* continue and stop at next (return from) syscall */
190         case PTRACE_SYSCALL:
191         case PTRACE_CONT: /* restart after signal. */
192         {
193                 ret = -EIO;
194                 if (!valid_signal(data))
195                         break;
196                 if (request == PTRACE_SYSCALL)
197                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
198                 else
199                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
200                 child->exit_code = data;
201                 /* Make sure the single step bit is not set. */
202                 child->ptrace &= ~PT_SINGLESTEP;
203                 wake_up_process(child);
204                 ret = 0;
205                 break;
206         }
207
208         /*
209          * make the child exit.  Best I can do is send it a sigkill.
210          * perhaps it should be put in the status that it wants to
211          * exit.
212          */
213         case PTRACE_KILL:
214                 ret = 0;
215                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
216                         break;
217                 child->exit_code = SIGKILL;
218                 child->ptrace &= ~PT_SINGLESTEP;
219                 wake_up_process(child);
220                 break;
221
222         case PTRACE_SINGLESTEP:
223                 ret = -EIO;
224                 if (!valid_signal(data))
225                         break;
226                 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
227                 child->ptrace |= PT_SINGLESTEP;
228                 child->exit_code = data;
229                 wake_up_process(child);
230                 ret = 0;
231                 break;
232
233         case PTRACE_GETREGS:
234         {
235                 /* 'data' points to user memory in which to write.
236                  * Mainly due to the non-live register values, we
237                  * reformat the register values into something more
238                  * standard.  For convenience, we use the handy
239                  * elf_gregset_t format. */
240
241                 xtensa_gregset_t format;
242                 struct pt_regs *regs = task_pt_regs(child);
243
244                 do_copy_regs (&format, regs, child);
245
246                 /* Now, copy to user space nice and easy... */
247                 ret = 0;
248                 if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t)))
249                         ret = -EFAULT;
250                 break;
251         }
252
253         case PTRACE_SETREGS:
254         {
255                 /* 'data' points to user memory that contains the new
256                  * values in the elf_gregset_t format. */
257
258                 xtensa_gregset_t format;
259                 struct pt_regs *regs = task_pt_regs(child);
260
261                 if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){
262                         ret = -EFAULT;
263                         break;
264                 }
265
266                 /* FIXME: Perhaps we want some sanity checks on
267                  * these user-space values?  See ARM version.  Are
268                  * debuggers a security concern? */
269
270                 do_restore_regs (&format, regs, child);
271
272                 ret = 0;
273                 break;
274         }
275
276         case PTRACE_GETFPREGS:
277         {
278                 /* 'data' points to user memory in which to write.
279                  * For convenience, we use the handy
280                  * elf_fpregset_t format. */
281
282                 elf_fpregset_t fpregs;
283                 struct pt_regs *regs = task_pt_regs(child);
284
285                 do_save_fpregs (&fpregs, regs, child);
286
287                 /* Now, copy to user space nice and easy... */
288                 ret = 0;
289                 if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t)))
290                         ret = -EFAULT;
291
292                 break;
293         }
294
295         case PTRACE_SETFPREGS:
296         {
297                 /* 'data' points to user memory that contains the new
298                  * values in the elf_fpregset_t format.
299                  */
300                 elf_fpregset_t fpregs;
301                 struct pt_regs *regs = task_pt_regs(child);
302
303                 ret = 0;
304                 if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) {
305                         ret = -EFAULT;
306                         break;
307                 }
308
309                 if (do_restore_fpregs (&fpregs, regs, child))
310                         ret = -EIO;
311                 break;
312         }
313
314         case PTRACE_GETFPREGSIZE:
315                 /* 'data' points to 'unsigned long' set to the size
316                  * of elf_fpregset_t
317                  */
318                 ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data);
319                 break;
320
321         case PTRACE_DETACH: /* detach a process that was attached. */
322                 ret = ptrace_detach(child, data);
323                 break;
324
325         default:
326                 ret = ptrace_request(child, request, addr, data);
327                 goto out;
328         }
329  out:
330         return ret;
331 }
332
333 void do_syscall_trace(void)
334 {
335         /*
336          * The 0x80 provides a way for the tracing parent to distinguish
337          * between a syscall stop and SIGTRAP delivery
338          */
339         ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
340
341         /*
342          * this isn't the same as continuing with a signal, but it will do
343          * for normal use.  strace only continues with a signal if the
344          * stopping signal is not SIGTRAP.  -brl
345          */
346         if (current->exit_code) {
347                 send_sig(current->exit_code, current, 1);
348                 current->exit_code = 0;
349         }
350 }
351
352 void do_syscall_trace_enter(struct pt_regs *regs)
353 {
354         if (test_thread_flag(TIF_SYSCALL_TRACE)
355                         && (current->ptrace & PT_PTRACED))
356                 do_syscall_trace();
357
358 #if 0
359         if (unlikely(current->audit_context))
360                 audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
361 #endif
362 }
363
364 void do_syscall_trace_leave(struct pt_regs *regs)
365 {
366         if ((test_thread_flag(TIF_SYSCALL_TRACE))
367                         && (current->ptrace & PT_PTRACED))
368                 do_syscall_trace();
369 }
370