Eleminate mips_cpu and move it into cpu_data.
[linux-2.6/linux-mips.git] / arch / mips / kernel / ptrace.c
blob52ea0c13d26e602f6cf04e49e688323612a5a4b3
1 /*
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
4 * for more details.
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/compiler.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/mm.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/smp.h>
21 #include <linux/smp_lock.h>
22 #include <linux/user.h>
24 #include <asm/mipsregs.h>
25 #include <asm/pgtable.h>
26 #include <asm/page.h>
27 #include <asm/system.h>
28 #include <asm/uaccess.h>
29 #include <asm/bootinfo.h>
30 #include <asm/cpu.h>
31 #include <asm/fpu.h>
34 * Called by kernel/ptrace.c when detaching..
36 * Make sure single step bits etc are not set.
38 void ptrace_disable(struct task_struct *child)
40 /* Nothing to do.. */
43 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
45 struct task_struct *child;
46 int ret;
48 lock_kernel();
49 #if 0
50 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
51 (int) request, (int) pid, (unsigned long) addr,
52 (unsigned long) data);
53 #endif
54 if (request == PTRACE_TRACEME) {
55 /* are we already being traced? */
56 if (current->ptrace & PT_PTRACED) {
57 ret = -EPERM;
58 goto out;
60 /* set the ptrace bit in the process flags. */
61 current->ptrace |= PT_PTRACED;
62 ret = 0;
63 goto out;
65 ret = -ESRCH;
66 read_lock(&tasklist_lock);
67 child = find_task_by_pid(pid);
68 if (child)
69 get_task_struct(child);
70 read_unlock(&tasklist_lock);
71 if (!child)
72 goto out;
74 ret = -EPERM;
75 if (pid == 1) /* you may not mess with init */
76 goto out_tsk;
78 if (request == PTRACE_ATTACH) {
79 ret = ptrace_attach(child);
80 goto out_tsk;
83 ret = ptrace_check_attach(child, request == PTRACE_KILL);
84 if (ret < 0)
85 goto out_tsk;
87 switch (request) {
88 case PTRACE_PEEKTEXT: /* read word at location addr. */
89 case PTRACE_PEEKDATA: {
90 unsigned long tmp;
91 int copied;
93 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
94 ret = -EIO;
95 if (copied != sizeof(tmp))
96 break;
97 ret = put_user(tmp,(unsigned long *) data);
98 break;
101 /* Read the word at location addr in the USER area. */
102 case PTRACE_PEEKUSR: {
103 struct pt_regs *regs;
104 unsigned long tmp;
106 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
107 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
108 tmp = 0; /* Default return value. */
110 switch(addr) {
111 case 0 ... 31:
112 tmp = regs->regs[addr];
113 break;
114 case FPR_BASE ... FPR_BASE + 31:
115 if (child->used_math) {
116 unsigned long long *fregs = get_fpu_regs(child);
118 * The odd registers are actually the high
119 * order bits of the values stored in the even
120 * registers - unless we're using r2k_switch.S.
122 if (addr & 1)
123 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
124 else
125 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
126 } else {
127 tmp = -1; /* FP not yet used */
129 break;
130 case PC:
131 tmp = regs->cp0_epc;
132 break;
133 case CAUSE:
134 tmp = regs->cp0_cause;
135 break;
136 case BADVADDR:
137 tmp = regs->cp0_badvaddr;
138 break;
139 case MMHI:
140 tmp = regs->hi;
141 break;
142 case MMLO:
143 tmp = regs->lo;
144 break;
145 case FPC_CSR:
146 if (!(current_cpu_data.options & MIPS_CPU_FPU))
147 tmp = child->thread.fpu.soft.sr;
148 else
149 tmp = child->thread.fpu.hard.control;
150 break;
151 case FPC_EIR: { /* implementation / version register */
152 unsigned int flags;
154 if (!(current_cpu_data.options & MIPS_CPU_FPU)) {
155 break;
158 flags = read_c0_status();
159 __enable_fpu();
160 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
161 write_c0_status(flags);
162 break;
164 default:
165 tmp = 0;
166 ret = -EIO;
167 goto out_tsk;
169 ret = put_user(tmp, (unsigned long *) data);
170 break;
173 case PTRACE_POKETEXT: /* write the word at location addr. */
174 case PTRACE_POKEDATA:
175 ret = 0;
176 if (access_process_vm(child, addr, &data, sizeof(data), 1)
177 == sizeof(data))
178 break;
179 ret = -EIO;
180 break;
182 case PTRACE_POKEUSR: {
183 struct pt_regs *regs;
184 ret = 0;
185 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
186 KERNEL_STACK_SIZE - 32 - sizeof(struct pt_regs));
188 switch (addr) {
189 case 0 ... 31:
190 regs->regs[addr] = data;
191 break;
192 case FPR_BASE ... FPR_BASE + 31: {
193 unsigned long long *fregs;
194 fregs = (unsigned long long *)get_fpu_regs(child);
195 if (!child->used_math) {
196 /* FP not yet used */
197 memset(&child->thread.fpu.hard, ~0,
198 sizeof(child->thread.fpu.hard));
199 child->thread.fpu.hard.control = 0;
202 * The odd registers are actually the high order bits
203 * of the values stored in the even registers - unless
204 * we're using r2k_switch.S.
206 if (addr & 1) {
207 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
208 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
209 } else {
210 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
211 fregs[addr - FPR_BASE] |= data;
213 break;
215 case PC:
216 regs->cp0_epc = data;
217 break;
218 case MMHI:
219 regs->hi = data;
220 break;
221 case MMLO:
222 regs->lo = data;
223 break;
224 case FPC_CSR:
225 if (!(current_cpu_data.options & MIPS_CPU_FPU))
226 child->thread.fpu.soft.sr = data;
227 else
228 child->thread.fpu.hard.control = data;
229 break;
230 default:
231 /* The rest are not allowed. */
232 ret = -EIO;
233 break;
235 break;
238 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
239 case PTRACE_CONT: { /* restart after signal. */
240 ret = -EIO;
241 if ((unsigned long) data > _NSIG)
242 break;
243 if (request == PTRACE_SYSCALL) {
244 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
246 else {
247 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
249 child->exit_code = data;
250 wake_up_process(child);
251 ret = 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 ret = 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 ret = ptrace_detach(child, data);
270 break;
272 default:
273 ret = ptrace_request(child, request, addr, data);
274 break;
276 out_tsk:
277 put_task_struct(child);
278 out:
279 unlock_kernel();
280 return ret;
284 * Notification of system call entry/exit
285 * - triggered by current->work.syscall_trace
287 asmlinkage void do_syscall_trace(void)
289 if (!test_thread_flag(TIF_SYSCALL_TRACE))
290 return;
291 if (!(current->ptrace & PT_PTRACED))
292 return;
294 /* The 0x80 provides a way for the tracing parent to distinguish
295 between a syscall stop and SIGTRAP delivery */
296 current->exit_code = SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
297 ? 0x80 : 0);
298 preempt_disable();
299 current->state = TASK_STOPPED;
300 notify_parent(current, SIGCHLD);
301 schedule();
302 preempt_enable();
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;