2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1992 Ross Biro
7 * Copyright (C) Linus Torvalds
8 * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
9 * Copyright (C) 1996 David S. Miller
10 * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
11 * Copyright (C) 1999 MIPS Technologies, Inc.
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
17 #include <linux/errno.h>
18 #include <linux/ptrace.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/user.h>
23 #include <asm/mipsregs.h>
24 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/bootinfo.h>
33 * Called by kernel/ptrace.c when detaching..
35 * Make sure single step bits etc are not set.
37 void ptrace_disable(struct task_struct *child)
42 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
44 struct task_struct *child;
49 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
50 (int) request, (int) pid, (unsigned long) addr,
51 (unsigned long) data);
53 if (request == PTRACE_TRACEME) {
54 /* are we already being traced? */
55 if (current->ptrace & PT_PTRACED) {
59 /* set the ptrace bit in the process flags. */
60 current->ptrace |= PT_PTRACED;
65 read_lock(&tasklist_lock);
66 child = find_task_by_pid(pid);
68 get_task_struct(child);
69 read_unlock(&tasklist_lock);
74 if (pid == 1) /* you may not mess with init */
77 if (request == PTRACE_ATTACH) {
78 ret = ptrace_attach(child);
82 ret = ptrace_check_attach(child, request == PTRACE_KILL);
87 case PTRACE_PEEKTEXT: /* read word at location addr. */
88 case PTRACE_PEEKDATA: {
92 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
94 if (copied != sizeof(tmp))
96 ret = put_user(tmp,(unsigned long *) data);
100 /* Read the word at location addr in the USER area. */
101 case PTRACE_PEEKUSR: {
102 struct pt_regs *regs;
105 regs = (struct pt_regs *) ((unsigned long) child +
106 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
107 tmp = 0; /* Default return value. */
111 tmp = regs->regs[addr];
113 case FPR_BASE ... FPR_BASE + 31:
114 if (child->used_math) {
115 unsigned long long *fregs = get_fpu_regs(child);
117 * The odd registers are actually the high
118 * order bits of the values stored in the even
119 * registers - unless we're using r2k_switch.S.
122 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
124 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
126 tmp = -1; /* FP not yet used */
133 tmp = regs->cp0_cause;
136 tmp = regs->cp0_badvaddr;
146 tmp = child->thread.fpu.soft.sr;
148 tmp = child->thread.fpu.hard.control;
150 case FPC_EIR: { /* implementation / version register */
158 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
159 __restore_flags(flags);
167 ret = put_user(tmp, (unsigned long *) data);
171 case PTRACE_POKETEXT: /* write the word at location addr. */
172 case PTRACE_POKEDATA:
174 if (access_process_vm(child, addr, &data, sizeof(data), 1)
180 case PTRACE_POKEUSR: {
181 struct pt_regs *regs;
183 regs = (struct pt_regs *) ((unsigned long) child +
184 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
188 regs->regs[addr] = data;
190 case FPR_BASE ... FPR_BASE + 31: {
191 unsigned long long *fregs;
192 fregs = (unsigned long long *)get_fpu_regs(child);
193 if (!child->used_math) {
194 /* FP not yet used */
195 memset(&child->thread.fpu.hard, ~0,
196 sizeof(child->thread.fpu.hard));
197 child->thread.fpu.hard.control = 0;
200 * The odd registers are actually the high order bits
201 * of the values stored in the even registers - unless
202 * we're using r2k_switch.S.
205 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
206 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
208 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
209 fregs[addr - FPR_BASE] |= data;
214 regs->cp0_epc = data;
224 child->thread.fpu.soft.sr = data;
226 child->thread.fpu.hard.control = data;
229 /* The rest are not allowed. */
236 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
237 case PTRACE_CONT: { /* restart after signal. */
239 if ((unsigned long) data > _NSIG)
241 if (request == PTRACE_SYSCALL)
242 child->ptrace |= PT_TRACESYS;
244 child->ptrace &= ~PT_TRACESYS;
245 child->exit_code = data;
246 wake_up_process(child);
252 * make the child exit. Best I can do is send it a sigkill.
253 * perhaps it should be put in the status that it wants to
258 if (child->state == TASK_ZOMBIE) /* already dead */
260 child->exit_code = SIGKILL;
261 wake_up_process(child);
264 case PTRACE_DETACH: /* detach a process that was attached. */
265 ret = ptrace_detach(child, data);
268 case PTRACE_SETOPTIONS:
269 if (data & PTRACE_O_TRACESYSGOOD)
270 child->ptrace |= PT_TRACESYSGOOD;
272 child->ptrace &= ~PT_TRACESYSGOOD;
281 free_task_struct(child);
287 asmlinkage void syscall_trace(void)
289 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
290 != (PT_PTRACED|PT_TRACESYS))
292 /* The 0x80 provides a way for the tracing parent to distinguish
293 between a syscall stop and SIGTRAP delivery */
294 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
296 current->state = TASK_STOPPED;
297 notify_parent(current, SIGCHLD);
300 * this isn't the same as continuing with a signal, but it will do
301 * for normal use. strace only continues with a signal if the
302 * stopping signal is not SIGTRAP. -brl
304 if (current->exit_code) {
305 send_sig(current->exit_code, current, 1);
306 current->exit_code = 0;