fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / arch / v850 / kernel / ptrace.c
1 /*
2  * arch/v850/kernel/ptrace.c -- `ptrace' system call
3  *
4  *  Copyright (C) 2002,03,04  NEC Electronics Corporation
5  *  Copyright (C) 2002,03,04  Miles Bader <miles@gnu.org>
6  *
7  * Derived from arch/mips/kernel/ptrace.c:
8  *
9  *  Copyright (C) 1992 Ross Biro
10  *  Copyright (C) Linus Torvalds
11  *  Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
12  *  Copyright (C) 1996 David S. Miller
13  *  Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
14  *  Copyright (C) 1999 MIPS Technologies, Inc.
15  *
16  * This file is subject to the terms and conditions of the GNU General
17  * Public License.  See the file COPYING in the main directory of this
18  * archive for more details.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/mm.h>
23 #include <linux/sched.h>
24 #include <linux/smp_lock.h>
25 #include <linux/ptrace.h>
26 #include <linux/signal.h>
27 #include <linux/vs_base.h>
28
29 #include <asm/errno.h>
30 #include <asm/ptrace.h>
31 #include <asm/processor.h>
32 #include <asm/uaccess.h>
33
34 /* Returns the address where the register at REG_OFFS in P is stashed away.  */
35 static v850_reg_t *reg_save_addr (unsigned reg_offs, struct task_struct *t)
36 {
37         struct pt_regs *regs;
38
39         /* Three basic cases:
40
41            (1) A register normally saved before calling the scheduler, is
42                available in the kernel entry pt_regs structure at the top
43                of the kernel stack.  The kernel trap/irq exit path takes
44                care to save/restore almost all registers for ptrace'd
45                processes.
46
47            (2) A call-clobbered register, where the process P entered the
48                kernel via [syscall] trap, is not stored anywhere; that's
49                OK, because such registers are not expected to be preserved
50                when the trap returns anyway (so we don't actually bother to
51                test for this case).
52
53            (3) A few registers not used at all by the kernel, and so
54                normally never saved except by context-switches, are in the
55                context switch state.  */
56
57         if (reg_offs == PT_CTPC || reg_offs == PT_CTPSW || reg_offs == PT_CTBP)
58                 /* Register saved during context switch.  */
59                 regs = thread_saved_regs (t);
60         else
61                 /* Register saved during kernel entry (or not available).  */
62                 regs = task_pt_regs (t);
63
64         return (v850_reg_t *)((char *)regs + reg_offs);
65 }
66
67 /* Set the bits SET and clear the bits CLEAR in the v850e DIR
68    (`debug information register').  Returns the new value of DIR.  */
69 static inline v850_reg_t set_dir (v850_reg_t set, v850_reg_t clear)
70 {
71         register v850_reg_t rval asm ("r10");
72         register v850_reg_t arg0 asm ("r6") = set;
73         register v850_reg_t arg1 asm ("r7") = clear;
74
75         /* The dbtrap handler has exactly this functionality when called
76            from kernel mode.  0xf840 is a `dbtrap' insn.  */
77         asm (".short 0xf840" : "=r" (rval) : "r" (arg0), "r" (arg1));
78
79         return rval;
80 }
81
82 /* Makes sure hardware single-stepping is (globally) enabled.
83    Returns true if successful.  */
84 static inline int enable_single_stepping (void)
85 {
86         static int enabled = 0; /* Remember whether we already did it.  */
87         if (! enabled) {
88                 /* Turn on the SE (`single-step enable') bit, 0x100, in the
89                    DIR (`debug information register').  This may fail if a
90                    processor doesn't support it or something.  We also try
91                    to clear bit 0x40 (`INI'), which is necessary to use the
92                    debug stuff on the v850e2; on the v850e, clearing 0x40
93                    shouldn't cause any problem.  */
94                 v850_reg_t dir = set_dir (0x100, 0x40);
95                 /* Make sure it really got set.  */
96                 if (dir & 0x100)
97                         enabled = 1;
98         }
99         return enabled;
100 }
101
102 /* Try to set CHILD's single-step flag to VAL.  Returns true if successful.  */
103 static int set_single_step (struct task_struct *t, int val)
104 {
105         v850_reg_t *psw_addr = reg_save_addr(PT_PSW, t);
106         if (val) {
107                 /* Make sure single-stepping is enabled.  */
108                 if (! enable_single_stepping ())
109                         return 0;
110                 /* Set T's single-step flag.  */
111                 *psw_addr |= 0x800;
112         } else
113                 *psw_addr &= ~0x800;
114         return 1;
115 }
116
117 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
118 {
119         int rval;
120
121         if (!vx_check(vx_task_xid(child), VS_WATCH_P|VS_IDENT))
122                 goto out;
123
124         switch (request) {
125                 unsigned long val, copied;
126
127         case PTRACE_PEEKTEXT: /* read word at location addr. */
128         case PTRACE_PEEKDATA:
129                 copied = access_process_vm(child, addr, &val, sizeof(val), 0);
130                 rval = -EIO;
131                 if (copied != sizeof(val))
132                         break;
133                 rval = put_user(val, (unsigned long *)data);
134                 goto out;
135
136         case PTRACE_POKETEXT: /* write the word at location addr. */
137         case PTRACE_POKEDATA:
138                 rval = 0;
139                 if (access_process_vm(child, addr, &data, sizeof(data), 1)
140                     == sizeof(data))
141                         break;
142                 rval = -EIO;
143                 goto out;
144
145         /* Read/write the word at location ADDR in the registers.  */
146         case PTRACE_PEEKUSR:
147         case PTRACE_POKEUSR:
148                 rval = 0;
149                 if (addr >= PT_SIZE && request == PTRACE_PEEKUSR) {
150                         /* Special requests that don't actually correspond
151                            to offsets in struct pt_regs.  */
152                         if (addr == PT_TEXT_ADDR)
153                                 val = child->mm->start_code;
154                         else if (addr == PT_DATA_ADDR)
155                                 val = child->mm->start_data;
156                         else if (addr == PT_TEXT_LEN)
157                                 val = child->mm->end_code
158                                         - child->mm->start_code;
159                         else
160                                 rval = -EIO;
161                 } else if (addr >= 0 && addr < PT_SIZE && (addr & 0x3) == 0) {
162                         v850_reg_t *reg_addr = reg_save_addr(addr, child);
163                         if (request == PTRACE_PEEKUSR)
164                                 val = *reg_addr;
165                         else
166                                 *reg_addr = data;
167                 } else
168                         rval = -EIO;
169
170                 if (rval == 0 && request == PTRACE_PEEKUSR)
171                         rval = put_user (val, (unsigned long *)data);
172                 goto out;
173
174         /* Continue and stop at next (return from) syscall */
175         case PTRACE_SYSCALL:
176         /* Restart after a signal.  */
177         case PTRACE_CONT:
178         /* Execute a single instruction. */
179         case PTRACE_SINGLESTEP:
180                 rval = -EIO;
181                 if (!valid_signal(data))
182                         break;
183
184                 /* Turn CHILD's single-step flag on or off.  */
185                 if (! set_single_step (child, request == PTRACE_SINGLESTEP))
186                         break;
187
188                 if (request == PTRACE_SYSCALL)
189                         set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
190                 else
191                         clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
192
193                 child->exit_code = data;
194                 wake_up_process(child);
195                 rval = 0;
196                 break;
197
198         /*
199          * make the child exit.  Best I can do is send it a sigkill.
200          * perhaps it should be put in the status that it wants to
201          * exit.
202          */
203         case PTRACE_KILL:
204                 rval = 0;
205                 if (child->exit_state == EXIT_ZOMBIE)   /* already dead */
206                         break;
207                 child->exit_code = SIGKILL;
208                 wake_up_process(child);
209                 break;
210
211         case PTRACE_DETACH: /* detach a process that was attached. */
212                 set_single_step (child, 0);  /* Clear single-step flag */
213                 rval = ptrace_detach(child, data);
214                 break;
215
216         default:
217                 rval = -EIO;
218                 goto out;
219         }
220  out:
221         return rval;
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         /* The 0x80 provides a way for the tracing parent to distinguish
231            between a syscall stop and SIGTRAP delivery */
232         ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
233                                  ? 0x80 : 0));
234         /*
235          * this isn't the same as continuing with a signal, but it will do
236          * for normal use.  strace only continues with a signal if the
237          * stopping signal is not SIGTRAP.  -brl
238          */
239         if (current->exit_code) {
240                 send_sig(current->exit_code, current, 1);
241                 current->exit_code = 0;
242         }
243 }
244
245 void ptrace_disable (struct task_struct *child)
246 {
247         /* nothing to do */
248 }