signal: Use task_is_*
[linux-2.6/mini2440.git] / arch / xtensa / kernel / ptrace.c
blob5533c7850d53b33c11aee1063ef1536bd91a46fd
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.
7 * Copyright (C) 2001 - 2005 Tensilica Inc.
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>
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/security.h>
23 #include <linux/signal.h>
25 #include <asm/pgtable.h>
26 #include <asm/page.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/ptrace.h>
30 #include <asm/elf.h>
32 #define TEST_KERNEL // verify kernel operations FIXME: remove
36 * Called by kernel/ptrace.c when detaching..
38 * Make sure single step bits etc are not set.
41 void ptrace_disable(struct task_struct *child)
43 /* Nothing to do.. */
46 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
48 int ret = -EPERM;
50 switch (request) {
51 case PTRACE_PEEKTEXT: /* read word at location addr. */
52 case PTRACE_PEEKDATA:
53 ret = generic_ptrace_peekdata(child, addr, data);
54 goto out;
56 /* Read the word at location addr in the USER area. */
58 case PTRACE_PEEKUSR:
60 struct pt_regs *regs;
61 unsigned long tmp;
63 regs = task_pt_regs(child);
64 tmp = 0; /* Default return value. */
66 switch(addr) {
68 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
70 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
71 ar &= (XCHAL_NUM_AREGS - 1);
72 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
73 tmp = regs->areg[ar];
74 else
75 ret = -EIO;
76 break;
78 case REG_A_BASE ... REG_A_BASE + 15:
79 tmp = regs->areg[addr - REG_A_BASE];
80 break;
81 case REG_PC:
82 tmp = regs->pc;
83 break;
84 case REG_PS:
85 /* Note: PS.EXCM is not set while user task is running;
86 * its being set in regs is for exception handling
87 * convenience. */
88 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
89 break;
90 case REG_WB:
91 tmp = regs->windowbase;
92 break;
93 case REG_WS:
94 tmp = regs->windowstart;
95 break;
96 case REG_LBEG:
97 tmp = regs->lbeg;
98 break;
99 case REG_LEND:
100 tmp = regs->lend;
101 break;
102 case REG_LCOUNT:
103 tmp = regs->lcount;
104 break;
105 case REG_SAR:
106 tmp = regs->sar;
107 break;
108 case REG_DEPC:
109 tmp = regs->depc;
110 break;
111 case REG_EXCCAUSE:
112 tmp = regs->exccause;
113 break;
114 case REG_EXCVADDR:
115 tmp = regs->excvaddr;
116 break;
117 case SYSCALL_NR:
118 tmp = regs->syscall;
119 break;
120 default:
121 tmp = 0;
122 ret = -EIO;
123 goto out;
125 ret = put_user(tmp, (unsigned long *) data);
126 goto out;
129 case PTRACE_POKETEXT: /* write the word at location addr. */
130 case PTRACE_POKEDATA:
131 ret = generic_ptrace_pokedata(child, addr, data);
132 goto out;
134 case PTRACE_POKEUSR:
136 struct pt_regs *regs;
137 regs = task_pt_regs(child);
139 switch (addr) {
140 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
142 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
143 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
144 regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data;
145 else
146 ret = -EIO;
147 break;
149 case REG_A_BASE ... REG_A_BASE + 15:
150 regs->areg[addr - REG_A_BASE] = data;
151 break;
152 case REG_PC:
153 regs->pc = data;
154 break;
155 case SYSCALL_NR:
156 regs->syscall = data;
157 break;
158 #ifdef TEST_KERNEL
159 case REG_WB:
160 regs->windowbase = data;
161 break;
162 case REG_WS:
163 regs->windowstart = data;
164 break;
165 #endif
167 default:
168 /* The rest are not allowed. */
169 ret = -EIO;
170 break;
172 break;
175 /* continue and stop at next (return from) syscall */
176 case PTRACE_SYSCALL:
177 case PTRACE_CONT: /* restart after signal. */
179 ret = -EIO;
180 if (!valid_signal(data))
181 break;
182 if (request == PTRACE_SYSCALL)
183 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
184 else
185 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
186 child->exit_code = data;
187 /* Make sure the single step bit is not set. */
188 child->ptrace &= ~PT_SINGLESTEP;
189 wake_up_process(child);
190 ret = 0;
191 break;
195 * make the child exit. Best I can do is send it a sigkill.
196 * perhaps it should be put in the status that it wants to
197 * exit.
199 case PTRACE_KILL:
200 ret = 0;
201 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
202 break;
203 child->exit_code = SIGKILL;
204 child->ptrace &= ~PT_SINGLESTEP;
205 wake_up_process(child);
206 break;
208 case PTRACE_SINGLESTEP:
209 ret = -EIO;
210 if (!valid_signal(data))
211 break;
212 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
213 child->ptrace |= PT_SINGLESTEP;
214 child->exit_code = data;
215 wake_up_process(child);
216 ret = 0;
217 break;
219 case PTRACE_GETREGS:
221 /* 'data' points to user memory in which to write.
222 * Mainly due to the non-live register values, we
223 * reformat the register values into something more
224 * standard. For convenience, we use the handy
225 * elf_gregset_t format. */
227 xtensa_gregset_t format;
228 struct pt_regs *regs = task_pt_regs(child);
230 do_copy_regs (&format, regs, child);
232 /* Now, copy to user space nice and easy... */
233 ret = 0;
234 if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t)))
235 ret = -EFAULT;
236 break;
239 case PTRACE_SETREGS:
241 /* 'data' points to user memory that contains the new
242 * values in the elf_gregset_t format. */
244 xtensa_gregset_t format;
245 struct pt_regs *regs = task_pt_regs(child);
247 if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){
248 ret = -EFAULT;
249 break;
252 /* FIXME: Perhaps we want some sanity checks on
253 * these user-space values? See ARM version. Are
254 * debuggers a security concern? */
256 do_restore_regs (&format, regs, child);
258 ret = 0;
259 break;
262 case PTRACE_GETFPREGS:
264 /* 'data' points to user memory in which to write.
265 * For convenience, we use the handy
266 * elf_fpregset_t format. */
268 elf_fpregset_t fpregs;
269 struct pt_regs *regs = task_pt_regs(child);
271 do_save_fpregs (&fpregs, regs, child);
273 /* Now, copy to user space nice and easy... */
274 ret = 0;
275 if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t)))
276 ret = -EFAULT;
278 break;
281 case PTRACE_SETFPREGS:
283 /* 'data' points to user memory that contains the new
284 * values in the elf_fpregset_t format.
286 elf_fpregset_t fpregs;
287 struct pt_regs *regs = task_pt_regs(child);
289 ret = 0;
290 if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) {
291 ret = -EFAULT;
292 break;
295 if (do_restore_fpregs (&fpregs, regs, child))
296 ret = -EIO;
297 break;
300 case PTRACE_GETFPREGSIZE:
301 /* 'data' points to 'unsigned long' set to the size
302 * of elf_fpregset_t
304 ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data);
305 break;
307 default:
308 ret = ptrace_request(child, request, addr, data);
309 goto out;
311 out:
312 return ret;
315 void do_syscall_trace(void)
318 * The 0x80 provides a way for the tracing parent to distinguish
319 * between a syscall stop and SIGTRAP delivery
321 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
324 * this isn't the same as continuing with a signal, but it will do
325 * for normal use. strace only continues with a signal if the
326 * stopping signal is not SIGTRAP. -brl
328 if (current->exit_code) {
329 send_sig(current->exit_code, current, 1);
330 current->exit_code = 0;
334 void do_syscall_trace_enter(struct pt_regs *regs)
336 if (test_thread_flag(TIF_SYSCALL_TRACE)
337 && (current->ptrace & PT_PTRACED))
338 do_syscall_trace();
340 #if 0
341 if (unlikely(current->audit_context))
342 audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
343 #endif
346 void do_syscall_trace_leave(struct pt_regs *regs)
348 if ((test_thread_flag(TIF_SYSCALL_TRACE))
349 && (current->ptrace & PT_PTRACED))
350 do_syscall_trace();