1 // TODO some minor issues
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
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>,
13 * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
16 #include <linux/kernel.h>
17 #include <linux/sched.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>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/ptrace.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)
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;
50 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
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));
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;
79 if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
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)
95 regs->windowbase = wb;
97 if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
101 if (__copy_from_user(regs->areg, gregset->a + wb*4, (WSBITS-wb) * 16))
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;
115 if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
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));
124 ret |= __copy_to_user(&xtregs->opt, ®s->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;
139 #if XTENSA_HAVE_COPROCESSORS
140 /* Flush all coprocessors before we overwrite them. */
141 coprocessor_flush_all(ti);
142 coprocessor_release_all(ti);
144 ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
145 sizeof(xtregs_coprocessor_t));
147 ret |= __copy_from_user(®s->xtregs_opt, &xtregs->opt,
148 sizeof(xtregs->opt));
149 ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
150 sizeof(xtregs->user));
152 return ret ? -EFAULT : 0;
155 int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
157 struct pt_regs *regs;
160 regs = task_pt_regs(child);
161 tmp = 0; /* Default return value. */
165 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
166 tmp = regs->areg[regno - REG_AR_BASE];
169 case REG_A_BASE ... REG_A_BASE + 15:
170 tmp = regs->areg[regno - REG_A_BASE];
178 /* Note: PS.EXCM is not set while user task is running;
179 * its being set in regs is for exception handling
181 tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
189 unsigned long wb = regs->windowbase;
190 unsigned long ws = regs->windowstart;
191 tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
217 return put_user(tmp, ret);
220 int ptrace_pokeusr(struct task_struct *child, long regno, long val)
222 struct pt_regs *regs;
223 regs = task_pt_regs(child);
226 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
227 regs->areg[regno - REG_AR_BASE] = val;
230 case REG_A_BASE ... REG_A_BASE + 15:
231 regs->areg[regno - REG_A_BASE] = val;
248 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
253 case PTRACE_PEEKTEXT: /* read word at location addr. */
254 case PTRACE_PEEKDATA:
255 ret = generic_ptrace_peekdata(child, addr, data);
258 case PTRACE_PEEKUSR: /* read register specified by addr. */
259 ret = ptrace_peekusr(child, addr, (void __user *) data);
262 case PTRACE_POKETEXT: /* write the word at location addr. */
263 case PTRACE_POKEDATA:
264 ret = generic_ptrace_pokedata(child, addr, data);
267 case PTRACE_POKEUSR: /* write register specified by addr. */
268 ret = ptrace_pokeusr(child, addr, data);
271 /* continue and stop at next (return from) syscall */
274 case PTRACE_CONT: /* restart after signal. */
277 if (!valid_signal(data))
279 if (request == PTRACE_SYSCALL)
280 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
282 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
283 child->exit_code = data;
284 /* Make sure the single step bit is not set. */
285 child->ptrace &= ~PT_SINGLESTEP;
286 wake_up_process(child);
292 * make the child exit. Best I can do is send it a sigkill.
293 * perhaps it should be put in the status that it wants to
298 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
300 child->exit_code = SIGKILL;
301 child->ptrace &= ~PT_SINGLESTEP;
302 wake_up_process(child);
305 case PTRACE_SINGLESTEP:
307 if (!valid_signal(data))
309 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
310 child->ptrace |= PT_SINGLESTEP;
311 child->exit_code = data;
312 wake_up_process(child);
317 ret = ptrace_getregs(child, (void __user *) data);
321 ret = ptrace_setregs(child, (void __user *) data);
324 case PTRACE_GETXTREGS:
325 ret = ptrace_getxregs(child, (void __user *) data);
328 case PTRACE_SETXTREGS:
329 ret = ptrace_setxregs(child, (void __user *) data);
333 ret = ptrace_request(child, request, addr, data);
340 void do_syscall_trace(void)
343 * The 0x80 provides a way for the tracing parent to distinguish
344 * between a syscall stop and SIGTRAP delivery
346 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
349 * this isn't the same as continuing with a signal, but it will do
350 * for normal use. strace only continues with a signal if the
351 * stopping signal is not SIGTRAP. -brl
353 if (current->exit_code) {
354 send_sig(current->exit_code, current, 1);
355 current->exit_code = 0;
359 void do_syscall_trace_enter(struct pt_regs *regs)
361 if (test_thread_flag(TIF_SYSCALL_TRACE)
362 && (current->ptrace & PT_PTRACED))
366 if (unlikely(current->audit_context))
367 audit_syscall_entry(current, AUDIT_ARCH_XTENSA..);
371 void do_syscall_trace_leave(struct pt_regs *regs)
373 if ((test_thread_flag(TIF_SYSCALL_TRACE))
374 && (current->ptrace & PT_PTRACED))