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 - 2005 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/config.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
20 #include <linux/errno.h>
21 #include <linux/ptrace.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/security.h>
25 #include <linux/signal.h>
27 #include <asm/pgtable.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <asm/ptrace.h>
34 #define TEST_KERNEL // verify kernel operations FIXME: remove
38 * Called by kernel/ptrace.c when detaching..
40 * Make sure single step bits etc are not set.
43 void ptrace_disable(struct task_struct *child)
48 long sys_ptrace(long request, long pid, long addr, long data)
50 struct task_struct *child;
56 if ((int)request != 1)
57 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
58 (int) request, (int) pid, (unsigned long) addr,
59 (unsigned long) data);
62 if (request == PTRACE_TRACEME) {
64 /* Are we already being traced? */
66 if (current->ptrace & PT_PTRACED)
69 if ((ret = security_ptrace(current->parent, current)))
72 /* Set the ptrace bit in the process flags. */
74 current->ptrace |= PT_PTRACED;
80 read_lock(&tasklist_lock);
81 child = find_task_by_pid(pid);
83 get_task_struct(child);
84 read_unlock(&tasklist_lock);
89 if (pid == 1) /* you may not mess with init */
92 if (request == PTRACE_ATTACH) {
93 ret = ptrace_attach(child);
97 if ((ret = ptrace_check_attach(child, request == PTRACE_KILL)) < 0)
101 case PTRACE_PEEKTEXT: /* read word at location addr. */
102 case PTRACE_PEEKDATA:
107 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
109 if (copied != sizeof(tmp))
111 ret = put_user(tmp,(unsigned long *) data);
116 /* Read the word at location addr in the USER area. */
120 struct pt_regs *regs;
123 regs = xtensa_pt_regs(child);
124 tmp = 0; /* Default return value. */
128 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
130 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
131 ar &= (XCHAL_NUM_AREGS - 1);
132 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
133 tmp = regs->areg[ar];
138 case REG_A_BASE ... REG_A_BASE + 15:
139 tmp = regs->areg[addr - REG_A_BASE];
145 /* Note: PS.EXCM is not set while user task is running;
146 * its being set in regs is for exception handling
148 tmp = (regs->ps & ~XCHAL_PS_EXCM_MASK);
151 tmp = regs->windowbase;
154 tmp = regs->windowstart;
172 tmp = regs->exccause;
175 tmp = regs->excvaddr;
185 ret = put_user(tmp, (unsigned long *) data);
189 case PTRACE_POKETEXT: /* write the word at location addr. */
190 case PTRACE_POKEDATA:
191 if (access_process_vm(child, addr, &data, sizeof(data), 1)
199 struct pt_regs *regs;
200 regs = xtensa_pt_regs(child);
203 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
205 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
206 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
207 regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data;
212 case REG_A_BASE ... REG_A_BASE + 15:
213 regs->areg[addr - REG_A_BASE] = data;
219 regs->syscall = data;
223 regs->windowbase = data;
226 regs->windowstart = data;
231 /* The rest are not allowed. */
238 /* continue and stop at next (return from) syscall */
240 case PTRACE_CONT: /* restart after signal. */
243 if (!valid_signal(data))
245 if (request == PTRACE_SYSCALL)
246 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
248 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
249 child->exit_code = data;
250 /* Make sure the single step bit is not set. */
251 child->ptrace &= ~PT_SINGLESTEP;
252 wake_up_process(child);
258 * make the child exit. Best I can do is send it a sigkill.
259 * perhaps it should be put in the status that it wants to
264 if (child->state == EXIT_ZOMBIE) /* already dead */
266 child->exit_code = SIGKILL;
267 child->ptrace &= ~PT_SINGLESTEP;
268 wake_up_process(child);
271 case PTRACE_SINGLESTEP:
273 if (!valid_signal(data))
275 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
276 child->ptrace |= PT_SINGLESTEP;
277 child->exit_code = data;
278 wake_up_process(child);
284 /* 'data' points to user memory in which to write.
285 * Mainly due to the non-live register values, we
286 * reformat the register values into something more
287 * standard. For convenience, we use the handy
288 * elf_gregset_t format. */
290 xtensa_gregset_t format;
291 struct pt_regs *regs = xtensa_pt_regs(child);
293 do_copy_regs (&format, regs, child);
295 /* Now, copy to user space nice and easy... */
297 if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t)))
304 /* 'data' points to user memory that contains the new
305 * values in the elf_gregset_t format. */
307 xtensa_gregset_t format;
308 struct pt_regs *regs = xtensa_pt_regs(child);
310 if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){
315 /* FIXME: Perhaps we want some sanity checks on
316 * these user-space values? See ARM version. Are
317 * debuggers a security concern? */
319 do_restore_regs (&format, regs, child);
325 case PTRACE_GETFPREGS:
327 /* 'data' points to user memory in which to write.
328 * For convenience, we use the handy
329 * elf_fpregset_t format. */
331 elf_fpregset_t fpregs;
332 struct pt_regs *regs = xtensa_pt_regs(child);
334 do_save_fpregs (&fpregs, regs, child);
336 /* Now, copy to user space nice and easy... */
338 if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t)))
344 case PTRACE_SETFPREGS:
346 /* 'data' points to user memory that contains the new
347 * values in the elf_fpregset_t format.
349 elf_fpregset_t fpregs;
350 struct pt_regs *regs = xtensa_pt_regs(child);
353 if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) {
358 if (do_restore_fpregs (&fpregs, regs, child))
363 case PTRACE_GETFPREGSIZE:
364 /* 'data' points to 'unsigned long' set to the size
367 ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data);
370 case PTRACE_DETACH: /* detach a process that was attached. */
371 ret = ptrace_detach(child, data);
375 ret = ptrace_request(child, request, addr, data);
379 put_task_struct(child);
385 void do_syscall_trace(void)
387 if (!test_thread_flag(TIF_SYSCALL_TRACE))
390 if (!(current->ptrace & PT_PTRACED))
394 * The 0x80 provides a way for the tracing parent to distinguish
395 * between a syscall stop and SIGTRAP delivery
397 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
400 * this isn't the same as continuing with a signal, but it will do
401 * for normal use. strace only continues with a signal if the
402 * stopping signal is not SIGTRAP. -brl
404 if (current->exit_code) {
405 send_sig(current->exit_code, current, 1);
406 current->exit_code = 0;