initial commit with v2.6.32.60
[linux-2.6.32.60-moxart.git] / arch / xtensa / kernel / ptrace.c
blob885ebe76e4439e64cc28f5c64ffc78ff30da12bc
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 - 2007 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>
31 #include <asm/coprocessor.h>
34 * Called by kernel/ptrace.c when detaching to disable single stepping.
37 void ptrace_disable(struct task_struct *child)
39 /* Nothing to do.. */
42 int ptrace_getregs(struct task_struct *child, void __user *uregs)
44 struct pt_regs *regs = task_pt_regs(child);
45 xtensa_gregset_t __user *gregset = uregs;
46 unsigned long wm = regs->wmask;
47 unsigned long wb = regs->windowbase;
48 int live, i;
50 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
51 return -EIO;
53 __put_user(regs->pc, &gregset->pc);
54 __put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
55 __put_user(regs->lbeg, &gregset->lbeg);
56 __put_user(regs->lend, &gregset->lend);
57 __put_user(regs->lcount, &gregset->lcount);
58 __put_user(regs->windowstart, &gregset->windowstart);
59 __put_user(regs->windowbase, &gregset->windowbase);
61 live = (wm & 2) ? 4 : (wm & 4) ? 8 : (wm & 8) ? 12 : 16;
63 for (i = 0; i < live; i++)
64 __put_user(regs->areg[i],gregset->a+((wb*4+i)%XCHAL_NUM_AREGS));
65 for (i = XCHAL_NUM_AREGS - (wm >> 4) * 4; i < XCHAL_NUM_AREGS; i++)
66 __put_user(regs->areg[i],gregset->a+((wb*4+i)%XCHAL_NUM_AREGS));
68 return 0;
71 int ptrace_setregs(struct task_struct *child, void __user *uregs)
73 struct pt_regs *regs = task_pt_regs(child);
74 xtensa_gregset_t *gregset = uregs;
75 const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
76 unsigned long ps;
77 unsigned long wb;
79 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
80 return -EIO;
82 __get_user(regs->pc, &gregset->pc);
83 __get_user(ps, &gregset->ps);
84 __get_user(regs->lbeg, &gregset->lbeg);
85 __get_user(regs->lend, &gregset->lend);
86 __get_user(regs->lcount, &gregset->lcount);
87 __get_user(regs->windowstart, &gregset->windowstart);
88 __get_user(wb, &gregset->windowbase);
90 regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);
92 if (wb >= XCHAL_NUM_AREGS / 4)
93 return -EFAULT;
95 regs->windowbase = wb;
97 if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
98 gregset->a, wb * 16))
99 return -EFAULT;
101 if (__copy_from_user(regs->areg, gregset->a + wb*4, (WSBITS-wb) * 16))
102 return -EFAULT;
104 return 0;
108 int ptrace_getxregs(struct task_struct *child, void __user *uregs)
110 struct pt_regs *regs = task_pt_regs(child);
111 struct thread_info *ti = task_thread_info(child);
112 elf_xtregs_t __user *xtregs = uregs;
113 int ret = 0;
115 if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
116 return -EIO;
118 #if XTENSA_HAVE_COPROCESSORS
119 /* Flush all coprocessor registers to memory. */
120 coprocessor_flush_all(ti);
121 ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
122 sizeof(xtregs_coprocessor_t));
123 #endif
124 ret |= __copy_to_user(&xtregs->opt, &regs->xtregs_opt,
125 sizeof(xtregs->opt));
126 ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
127 sizeof(xtregs->user));
129 return ret ? -EFAULT : 0;
132 int ptrace_setxregs(struct task_struct *child, void __user *uregs)
134 struct thread_info *ti = task_thread_info(child);
135 struct pt_regs *regs = task_pt_regs(child);
136 elf_xtregs_t *xtregs = uregs;
137 int ret = 0;
139 if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
140 return -EFAULT;
142 #if XTENSA_HAVE_COPROCESSORS
143 /* Flush all coprocessors before we overwrite them. */
144 coprocessor_flush_all(ti);
145 coprocessor_release_all(ti);
147 ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
148 sizeof(xtregs_coprocessor_t));
149 #endif
150 ret |= __copy_from_user(&regs->xtregs_opt, &xtregs->opt,
151 sizeof(xtregs->opt));
152 ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
153 sizeof(xtregs->user));
155 return ret ? -EFAULT : 0;
158 int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
160 struct pt_regs *regs;
161 unsigned long tmp;
163 regs = task_pt_regs(child);
164 tmp = 0; /* Default return value. */
166 switch(regno) {
168 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
169 tmp = regs->areg[regno - REG_AR_BASE];
170 break;
172 case REG_A_BASE ... REG_A_BASE + 15:
173 tmp = regs->areg[regno - REG_A_BASE];
174 break;
176 case REG_PC:
177 tmp = regs->pc;
178 break;
180 case REG_PS:
181 /* Note: PS.EXCM is not set while user task is running;
182 * its being set in regs is for exception handling
183 * convenience. */
184 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
185 break;
187 case REG_WB:
188 break; /* tmp = 0 */
190 case REG_WS:
192 unsigned long wb = regs->windowbase;
193 unsigned long ws = regs->windowstart;
194 tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
195 break;
197 case REG_LBEG:
198 tmp = regs->lbeg;
199 break;
201 case REG_LEND:
202 tmp = regs->lend;
203 break;
205 case REG_LCOUNT:
206 tmp = regs->lcount;
207 break;
209 case REG_SAR:
210 tmp = regs->sar;
211 break;
213 case SYSCALL_NR:
214 tmp = regs->syscall;
215 break;
217 default:
218 return -EIO;
220 return put_user(tmp, ret);
223 int ptrace_pokeusr(struct task_struct *child, long regno, long val)
225 struct pt_regs *regs;
226 regs = task_pt_regs(child);
228 switch (regno) {
229 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
230 regs->areg[regno - REG_AR_BASE] = val;
231 break;
233 case REG_A_BASE ... REG_A_BASE + 15:
234 regs->areg[regno - REG_A_BASE] = val;
235 break;
237 case REG_PC:
238 regs->pc = val;
239 break;
241 case SYSCALL_NR:
242 regs->syscall = val;
243 break;
245 default:
246 return -EIO;
248 return 0;
251 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
253 int ret = -EPERM;
255 switch (request) {
256 case PTRACE_PEEKTEXT: /* read word at location addr. */
257 case PTRACE_PEEKDATA:
258 ret = generic_ptrace_peekdata(child, addr, data);
259 break;
261 case PTRACE_PEEKUSR: /* read register specified by addr. */
262 ret = ptrace_peekusr(child, addr, (void __user *) data);
263 break;
265 case PTRACE_POKETEXT: /* write the word at location addr. */
266 case PTRACE_POKEDATA:
267 ret = generic_ptrace_pokedata(child, addr, data);
268 break;
270 case PTRACE_POKEUSR: /* write register specified by addr. */
271 ret = ptrace_pokeusr(child, addr, data);
272 break;
274 /* continue and stop at next (return from) syscall */
276 case PTRACE_SYSCALL:
277 case PTRACE_CONT: /* restart after signal. */
279 ret = -EIO;
280 if (!valid_signal(data))
281 break;
282 if (request == PTRACE_SYSCALL)
283 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
284 else
285 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
286 child->exit_code = data;
287 /* Make sure the single step bit is not set. */
288 child->ptrace &= ~PT_SINGLESTEP;
289 wake_up_process(child);
290 ret = 0;
291 break;
295 * make the child exit. Best I can do is send it a sigkill.
296 * perhaps it should be put in the status that it wants to
297 * exit.
299 case PTRACE_KILL:
300 ret = 0;
301 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
302 break;
303 child->exit_code = SIGKILL;
304 child->ptrace &= ~PT_SINGLESTEP;
305 wake_up_process(child);
306 break;
308 case PTRACE_SINGLESTEP:
309 ret = -EIO;
310 if (!valid_signal(data))
311 break;
312 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
313 child->ptrace |= PT_SINGLESTEP;
314 child->exit_code = data;
315 wake_up_process(child);
316 ret = 0;
317 break;
319 case PTRACE_GETREGS:
320 ret = ptrace_getregs(child, (void __user *) data);
321 break;
323 case PTRACE_SETREGS:
324 ret = ptrace_setregs(child, (void __user *) data);
325 break;
327 case PTRACE_GETXTREGS:
328 ret = ptrace_getxregs(child, (void __user *) data);
329 break;
331 case PTRACE_SETXTREGS:
332 ret = ptrace_setxregs(child, (void __user *) data);
333 break;
335 default:
336 ret = ptrace_request(child, request, addr, data);
337 break;
340 return ret;
343 void do_syscall_trace(void)
346 * The 0x80 provides a way for the tracing parent to distinguish
347 * between a syscall stop and SIGTRAP delivery
349 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
352 * this isn't the same as continuing with a signal, but it will do
353 * for normal use. strace only continues with a signal if the
354 * stopping signal is not SIGTRAP. -brl
356 if (current->exit_code) {
357 send_sig(current->exit_code, current, 1);
358 current->exit_code = 0;
362 void do_syscall_trace_enter(struct pt_regs *regs)
364 if (test_thread_flag(TIF_SYSCALL_TRACE)
365 && (current->ptrace & PT_PTRACED))
366 do_syscall_trace();
368 #if 0
369 if (unlikely(current->audit_context))
370 audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
371 #endif
374 void do_syscall_trace_leave(struct pt_regs *regs)
376 if ((test_thread_flag(TIF_SYSCALL_TRACE))
377 && (current->ptrace & PT_PTRACED))
378 do_syscall_trace();