Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / arch / mips / kernel / ptrace.c
blob4a55e23251bb30b43867137c3859a86b68314647
1 /* $Id: ptrace.c,v 1.17 1999/09/28 22:25:47 ralf Exp $
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) 1992 Ross Biro
8 * Copyright (C) Linus Torvalds
9 * Copyright (C) 1994, 1995, 1996, 1997, 1998 Ralf Baechle
10 * Copyright (C) 1996 David S. Miller
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15 #include <linux/errno.h>
16 #include <linux/ptrace.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/user.h>
21 #include <asm/fp.h>
22 #include <asm/mipsregs.h>
23 #include <asm/pgtable.h>
24 #include <asm/page.h>
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
28 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
30 struct task_struct *child;
31 int res;
32 extern void save_fp(void*);
34 lock_kernel();
35 #if 0
36 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
37 (int) request, (int) pid, (unsigned long) addr,
38 (unsigned long) data);
39 #endif
40 if (request == PTRACE_TRACEME) {
41 /* are we already being traced? */
42 if (current->ptrace & PT_PTRACED) {
43 res = -EPERM;
44 goto out;
46 /* set the ptrace bit in the process flags. */
47 current->ptrace |= PT_PTRACED;
48 res = 0;
49 goto out;
51 res = -ESRCH;
52 read_lock(&tasklist_lock);
53 child = find_task_by_pid(pid);
54 if (child)
55 get_task_struct(child);
56 read_unlock(&tasklist_lock);
57 if (!child)
58 goto out;
60 res = -EPERM;
61 if (pid == 1) /* you may not mess with init */
62 goto out;
64 if (request == PTRACE_ATTACH) {
65 if (child == current)
66 goto out_tsk;
67 if ((!child->dumpable ||
68 (current->uid != child->euid) ||
69 (current->uid != child->suid) ||
70 (current->uid != child->uid) ||
71 (current->gid != child->egid) ||
72 (current->gid != child->sgid) ||
73 (current->gid != child->gid) ||
74 (!cap_issubset(child->cap_permitted,
75 current->cap_permitted)) ||
76 (current->gid != child->gid)) && !capable(CAP_SYS_PTRACE))
77 goto out_tsk;
78 /* the same process cannot be attached many times */
79 if (child->ptrace & PT_PTRACED)
80 goto out_tsk;
81 child->ptrace |= PT_PTRACED;
83 write_lock_irq(&tasklist_lock);
84 if (child->p_pptr != current) {
85 REMOVE_LINKS(child);
86 child->p_pptr = current;
87 SET_LINKS(child);
89 write_unlock_irq(&tasklist_lock);
91 send_sig(SIGSTOP, child, 1);
92 res = 0;
93 goto out_tsk;
95 res = -ESRCH;
96 if (!(child->ptrace & PT_PTRACED))
97 goto out_tsk;
98 if (child->state != TASK_STOPPED) {
99 if (request != PTRACE_KILL)
100 goto out_tsk;
102 if (child->p_pptr != current)
103 goto out_tsk;
104 switch (request) {
105 case PTRACE_PEEKTEXT: /* read word at location addr. */
106 case PTRACE_PEEKDATA: {
107 unsigned long tmp;
108 int copied;
110 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
111 res = -EIO;
112 if (copied != sizeof(tmp))
113 break;
114 res = put_user(tmp,(unsigned long *) data);
116 goto out;
119 /* Read the word at location addr in the USER area. */
120 case PTRACE_PEEKUSR: {
121 struct pt_regs *regs;
122 unsigned long tmp;
124 regs = (struct pt_regs *) ((unsigned long) child +
125 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
126 tmp = 0; /* Default return value. */
128 switch(addr) {
129 case 0 ... 31:
130 tmp = regs->regs[addr];
131 break;
132 case FPR_BASE ... FPR_BASE + 31:
133 if (child->used_math) {
134 if (last_task_used_math == child) {
135 enable_cp1();
136 save_fp(child);
137 disable_cp1();
138 last_task_used_math = NULL;
140 tmp = child->thread.fpu.hard.fp_regs[addr - 32];
141 } else {
142 tmp = -1; /* FP not yet used */
144 break;
145 case PC:
146 tmp = regs->cp0_epc;
147 break;
148 case CAUSE:
149 tmp = regs->cp0_cause;
150 break;
151 case BADVADDR:
152 tmp = regs->cp0_badvaddr;
153 break;
154 case MMHI:
155 tmp = regs->hi;
156 break;
157 case MMLO:
158 tmp = regs->lo;
159 break;
160 case FPC_CSR:
161 tmp = child->thread.fpu.hard.control;
162 break;
163 case FPC_EIR: { /* implementation / version register */
164 unsigned int flags;
166 __save_flags(flags);
167 enable_cp1();
168 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
169 __restore_flags(flags);
170 break;
172 default:
173 tmp = 0;
174 res = -EIO;
175 goto out;
177 res = put_user(tmp, (unsigned long *) data);
178 goto out;
181 case PTRACE_POKETEXT: /* write the word at location addr. */
182 case PTRACE_POKEDATA:
183 res = 0;
184 if (access_process_vm(child, addr, &data, sizeof(data), 1)
185 == sizeof(data))
186 break;
187 res = -EIO;
188 goto out;
190 case PTRACE_POKEUSR: {
191 struct pt_regs *regs;
192 int res = 0;
193 regs = (struct pt_regs *) ((unsigned long) child +
194 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
196 switch (addr) {
197 case 0 ... 31:
198 regs->regs[addr] = data;
199 break;
200 case FPR_BASE ... FPR_BASE + 31: {
201 unsigned int *fregs;
202 if (child->used_math) {
203 if (last_task_used_math == child) {
204 enable_cp1();
205 save_fp(child);
206 disable_cp1();
207 last_task_used_math = NULL;
208 regs->cp0_status &= ~ST0_CU1;
210 } else {
211 /* FP not yet used */
212 memset(&child->thread.fpu.hard, ~0,
213 sizeof(child->thread.fpu.hard));
214 child->thread.fpu.hard.control = 0;
216 fregs = child->thread.fpu.hard.fp_regs;
217 fregs[addr - FPR_BASE] = data;
218 break;
220 case PC:
221 regs->cp0_epc = data;
222 break;
223 case MMHI:
224 regs->hi = data;
225 break;
226 case MMLO:
227 regs->lo = data;
228 break;
229 case FPC_CSR:
230 child->thread.fpu.hard.control = data;
231 break;
232 default:
233 /* The rest are not allowed. */
234 res = -EIO;
235 break;
237 break;
240 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
241 case PTRACE_CONT: { /* restart after signal. */
242 res = -EIO;
243 if ((unsigned long) data > _NSIG)
244 break;
245 if (request == PTRACE_SYSCALL)
246 child->ptrace |= PT_TRACESYS;
247 else
248 child->ptrace &= ~PT_TRACESYS;
249 child->exit_code = data;
250 wake_up_process(child);
251 res = 0;
252 break;
256 * make the child exit. Best I can do is send it a sigkill.
257 * perhaps it should be put in the status that it wants to
258 * exit.
260 case PTRACE_KILL:
261 res = 0;
262 if (child->state != TASK_ZOMBIE) /* already dead */
263 break;
264 child->exit_code = SIGKILL;
265 wake_up_process(child);
266 break;
268 case PTRACE_DETACH: /* detach a process that was attached. */
269 res = -EIO;
270 if ((unsigned long) data > _NSIG)
271 break;
272 child->ptrace &= ~(PT_PTRACED|PT_TRACESYS);
273 child->exit_code = data;
274 write_lock_irq(&tasklist_lock);
275 REMOVE_LINKS(child);
276 child->p_pptr = child->p_opptr;
277 SET_LINKS(child);
278 write_unlock_irq(&tasklist_lock);
279 wake_up_process(child);
280 res = 0;
281 break;
283 default:
284 res = -EIO;
285 goto out;
287 out_tsk:
288 free_task_struct(child);
289 out:
290 unlock_kernel();
291 return res;
294 asmlinkage void syscall_trace(void)
296 if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
297 != (PT_PTRACED|PT_TRACESYS))
298 return;
299 current->exit_code = SIGTRAP;
300 current->state = TASK_STOPPED;
301 notify_parent(current, SIGCHLD);
302 schedule();
304 * this isn't the same as continuing with a signal, but it will do
305 * for normal use. strace only continues with a signal if the
306 * stopping signal is not SIGTRAP. -brl
308 if (current->exit_code) {
309 send_sig(current->exit_code, current, 1);
310 current->exit_code = 0;