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>
26 #include <asm/pgtable.h>
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/ptrace.h>
33 #define TEST_KERNEL // verify kernel operations FIXME: remove
37 * Called by kernel/ptrace.c when detaching..
39 * Make sure single step bits etc are not set.
42 void ptrace_disable(struct task_struct *child)
47 int sys_ptrace(long request, long pid, long addr, long data)
49 struct task_struct *child;
55 if ((int)request != 1)
56 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
57 (int) request, (int) pid, (unsigned long) addr,
58 (unsigned long) data);
61 if (request == PTRACE_TRACEME) {
63 /* Are we already being traced? */
65 if (current->ptrace & PT_PTRACED)
68 if ((ret = security_ptrace(current->parent, current)))
71 /* Set the ptrace bit in the process flags. */
73 current->ptrace |= PT_PTRACED;
79 read_lock(&tasklist_lock);
80 child = find_task_by_pid(pid);
82 get_task_struct(child);
83 read_unlock(&tasklist_lock);
88 if (pid == 1) /* you may not mess with init */
91 if (request == PTRACE_ATTACH) {
92 ret = ptrace_attach(child);
96 if ((ret = ptrace_check_attach(child, request == PTRACE_KILL)) < 0)
100 case PTRACE_PEEKTEXT: /* read word at location addr. */
101 case PTRACE_PEEKDATA:
106 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
108 if (copied != sizeof(tmp))
110 ret = put_user(tmp,(unsigned long *) data);
115 /* Read the word at location addr in the USER area. */
119 struct pt_regs *regs;
122 regs = xtensa_pt_regs(child);
123 tmp = 0; /* Default return value. */
127 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
129 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
130 ar &= (XCHAL_NUM_AREGS - 1);
131 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
132 tmp = regs->areg[ar];
137 case REG_A_BASE ... REG_A_BASE + 15:
138 tmp = regs->areg[addr - REG_A_BASE];
144 /* Note: PS.EXCM is not set while user task is running;
145 * its being set in regs is for exception handling
147 tmp = (regs->ps & ~XCHAL_PS_EXCM_MASK);
150 tmp = regs->windowbase;
153 tmp = regs->windowstart;
171 tmp = regs->exccause;
174 tmp = regs->excvaddr;
184 ret = put_user(tmp, (unsigned long *) data);
188 case PTRACE_POKETEXT: /* write the word at location addr. */
189 case PTRACE_POKEDATA:
190 if (access_process_vm(child, addr, &data, sizeof(data), 1)
198 struct pt_regs *regs;
199 regs = xtensa_pt_regs(child);
202 case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
204 int ar = addr - REG_AR_BASE - regs->windowbase * 4;
205 if (ar < 16 && ar + (regs->wmask >> 4) * 4 >= 0)
206 regs->areg[ar & (XCHAL_NUM_AREGS - 1)] = data;
211 case REG_A_BASE ... REG_A_BASE + 15:
212 regs->areg[addr - REG_A_BASE] = data;
218 regs->syscall = data;
222 regs->windowbase = data;
225 regs->windowstart = data;
230 /* The rest are not allowed. */
237 /* continue and stop at next (return from) syscall */
239 case PTRACE_CONT: /* restart after signal. */
242 if ((unsigned long) data > _NSIG)
244 if (request == PTRACE_SYSCALL)
245 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
247 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
248 child->exit_code = data;
249 /* Make sure the single step bit is not set. */
250 child->ptrace &= ~PT_SINGLESTEP;
251 wake_up_process(child);
257 * make the child exit. Best I can do is send it a sigkill.
258 * perhaps it should be put in the status that it wants to
263 if (child->state == EXIT_ZOMBIE) /* already dead */
265 child->exit_code = SIGKILL;
266 child->ptrace &= ~PT_SINGLESTEP;
267 wake_up_process(child);
270 case PTRACE_SINGLESTEP:
272 if ((unsigned long) data > _NSIG)
274 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
275 child->ptrace |= PT_SINGLESTEP;
276 child->exit_code = data;
277 wake_up_process(child);
283 /* 'data' points to user memory in which to write.
284 * Mainly due to the non-live register values, we
285 * reformat the register values into something more
286 * standard. For convenience, we use the handy
287 * elf_gregset_t format. */
289 xtensa_gregset_t format;
290 struct pt_regs *regs = xtensa_pt_regs(child);
292 do_copy_regs (&format, regs, child);
294 /* Now, copy to user space nice and easy... */
296 if (copy_to_user((void *)data, &format, sizeof(elf_gregset_t)))
303 /* 'data' points to user memory that contains the new
304 * values in the elf_gregset_t format. */
306 xtensa_gregset_t format;
307 struct pt_regs *regs = xtensa_pt_regs(child);
309 if (copy_from_user(&format,(void *)data,sizeof(elf_gregset_t))){
314 /* FIXME: Perhaps we want some sanity checks on
315 * these user-space values? See ARM version. Are
316 * debuggers a security concern? */
318 do_restore_regs (&format, regs, child);
324 case PTRACE_GETFPREGS:
326 /* 'data' points to user memory in which to write.
327 * For convenience, we use the handy
328 * elf_fpregset_t format. */
330 elf_fpregset_t fpregs;
331 struct pt_regs *regs = xtensa_pt_regs(child);
333 do_save_fpregs (&fpregs, regs, child);
335 /* Now, copy to user space nice and easy... */
337 if (copy_to_user((void *)data, &fpregs, sizeof(elf_fpregset_t)))
343 case PTRACE_SETFPREGS:
345 /* 'data' points to user memory that contains the new
346 * values in the elf_fpregset_t format.
348 elf_fpregset_t fpregs;
349 struct pt_regs *regs = xtensa_pt_regs(child);
352 if (copy_from_user(&fpregs, (void *)data, sizeof(elf_fpregset_t))) {
357 if (do_restore_fpregs (&fpregs, regs, child))
362 case PTRACE_GETFPREGSIZE:
363 /* 'data' points to 'unsigned long' set to the size
366 ret = put_user(sizeof(elf_fpregset_t), (unsigned long *) data);
369 case PTRACE_DETACH: /* detach a process that was attached. */
370 ret = ptrace_detach(child, data);
374 ret = ptrace_request(child, request, addr, data);
378 put_task_struct(child);
384 void do_syscall_trace(void)
386 if (!test_thread_flag(TIF_SYSCALL_TRACE))
389 if (!(current->ptrace & PT_PTRACED))
393 * The 0x80 provides a way for the tracing parent to distinguish
394 * between a syscall stop and SIGTRAP delivery
396 ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
399 * this isn't the same as continuing with a signal, but it will do
400 * for normal use. strace only continues with a signal if the
401 * stopping signal is not SIGTRAP. -brl
403 if (current->exit_code) {
404 send_sig(current->exit_code, current, 1);
405 current->exit_code = 0;