blob: 5c45a5880226055aafc54f86a71a2acbc3b4c3f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
5 *
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.
12 * Copyright (C) 2000 Ulf Carlsson
13 *
14 * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
15 * binaries.
16 */
17#include <linux/compiler.h>
18#include <linux/kernel.h>
19#include <linux/sched.h>
20#include <linux/mm.h>
21#include <linux/errno.h>
22#include <linux/ptrace.h>
23#include <linux/smp.h>
24#include <linux/smp_lock.h>
25#include <linux/user.h>
26#include <linux/security.h>
27
28#include <asm/cpu.h>
Ralf Baechlee50c0a82005-05-31 11:49:19 +000029#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/fpu.h>
31#include <asm/mipsregs.h>
32#include <asm/pgtable.h>
33#include <asm/page.h>
34#include <asm/system.h>
35#include <asm/uaccess.h>
36#include <asm/bootinfo.h>
37
38/*
39 * Tracing a 32-bit process with a 64-bit strace and vice versa will not
40 * work. I don't know how to fix this.
41 */
42asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
43{
44 struct task_struct *child;
45 int ret;
46
47#if 0
48 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
49 (int) request, (int) pid, (unsigned long) addr,
50 (unsigned long) data);
51#endif
52 lock_kernel();
53 ret = -EPERM;
54 if (request == PTRACE_TRACEME) {
55 /* are we already being traced? */
56 if (current->ptrace & PT_PTRACED)
57 goto out;
58 if ((ret = security_ptrace(current->parent, current)))
59 goto out;
60 /* set the ptrace bit in the process flags. */
61 current->ptrace |= PT_PTRACED;
62 ret = 0;
63 goto out;
64 }
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;
73
74 ret = -EPERM;
75 if (pid == 1) /* you may not mess with init */
76 goto out_tsk;
77
78 if (request == PTRACE_ATTACH) {
79 ret = ptrace_attach(child);
80 goto out_tsk;
81 }
82
83 ret = ptrace_check_attach(child, request == PTRACE_KILL);
84 if (ret < 0)
85 goto out_tsk;
86
87 switch (request) {
88 /* when I and D space are separate, these will need to be fixed. */
89 case PTRACE_PEEKTEXT: /* read word at location addr. */
90 case PTRACE_PEEKDATA: {
91 unsigned int tmp;
92 int copied;
93
94 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
95 ret = -EIO;
96 if (copied != sizeof(tmp))
97 break;
98 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
99 break;
100 }
101
102 /* Read the word at location addr in the USER area. */
103 case PTRACE_PEEKUSR: {
104 struct pt_regs *regs;
105 unsigned int tmp;
106
107 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
108 THREAD_SIZE - 32 - sizeof(struct pt_regs));
109 ret = 0; /* Default return value. */
110
111 switch (addr) {
112 case 0 ... 31:
113 tmp = regs->regs[addr];
114 break;
115 case FPR_BASE ... FPR_BASE + 31:
116 if (tsk_used_math(child)) {
117 fpureg_t *fregs = get_fpu_regs(child);
118
119 /*
120 * The odd registers are actually the high
121 * order bits of the values stored in the even
122 * registers - unless we're using r2k_switch.S.
123 */
124 if (addr & 1)
125 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
126 else
127 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
128 } else {
129 tmp = -1; /* FP not yet used */
130 }
131 break;
132 case PC:
133 tmp = regs->cp0_epc;
134 break;
135 case CAUSE:
136 tmp = regs->cp0_cause;
137 break;
138 case BADVADDR:
139 tmp = regs->cp0_badvaddr;
140 break;
141 case MMHI:
142 tmp = regs->hi;
143 break;
144 case MMLO:
145 tmp = regs->lo;
146 break;
147 case FPC_CSR:
148 if (cpu_has_fpu)
149 tmp = child->thread.fpu.hard.fcr31;
150 else
151 tmp = child->thread.fpu.soft.fcr31;
152 break;
153 case FPC_EIR: { /* implementation / version register */
154 unsigned int flags;
155
156 if (!cpu_has_fpu)
157 break;
158
159 flags = read_c0_status();
160 __enable_fpu();
161 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
162 write_c0_status(flags);
163 break;
164 }
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000165 case DSP_BASE ... DSP_BASE + 5:
166 if (!cpu_has_dsp) {
167 tmp = 0;
168 ret = -EIO;
169 goto out_tsk;
170 }
171 if (child->thread.dsp.used_dsp) {
172 dspreg_t *dregs = __get_dsp_regs(child);
173 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
174 } else {
175 tmp = -1; /* DSP registers yet used */
176 }
177 break;
178 case DSP_CONTROL:
179 if (!cpu_has_dsp) {
180 tmp = 0;
181 ret = -EIO;
182 goto out_tsk;
183 }
184 tmp = child->thread.dsp.dspcontrol;
185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 default:
187 tmp = 0;
188 ret = -EIO;
189 goto out_tsk;
190 }
191 ret = put_user(tmp, (unsigned *) (unsigned long) data);
192 break;
193 }
194
195 /* when I and D space are separate, this will have to be fixed. */
196 case PTRACE_POKETEXT: /* write the word at location addr. */
197 case PTRACE_POKEDATA:
198 ret = 0;
199 if (access_process_vm(child, addr, &data, sizeof(data), 1)
200 == sizeof(data))
201 break;
202 ret = -EIO;
203 break;
204
205 case PTRACE_POKEUSR: {
206 struct pt_regs *regs;
207 ret = 0;
208 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
209 THREAD_SIZE - 32 - sizeof(struct pt_regs));
210
211 switch (addr) {
212 case 0 ... 31:
213 regs->regs[addr] = data;
214 break;
215 case FPR_BASE ... FPR_BASE + 31: {
216 fpureg_t *fregs = get_fpu_regs(child);
217
218 if (!tsk_used_math(child)) {
219 /* FP not yet used */
220 memset(&child->thread.fpu.hard, ~0,
221 sizeof(child->thread.fpu.hard));
222 child->thread.fpu.hard.fcr31 = 0;
223 }
224 /*
225 * The odd registers are actually the high order bits
226 * of the values stored in the even registers - unless
227 * we're using r2k_switch.S.
228 */
229 if (addr & 1) {
230 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
231 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
232 } else {
233 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
234 /* Must cast, lest sign extension fill upper
235 bits! */
236 fregs[addr - FPR_BASE] |= (unsigned int)data;
237 }
238 break;
239 }
240 case PC:
241 regs->cp0_epc = data;
242 break;
243 case MMHI:
244 regs->hi = data;
245 break;
246 case MMLO:
247 regs->lo = data;
248 break;
249 case FPC_CSR:
250 if (cpu_has_fpu)
251 child->thread.fpu.hard.fcr31 = data;
252 else
253 child->thread.fpu.soft.fcr31 = data;
254 break;
Ralf Baechlee50c0a82005-05-31 11:49:19 +0000255 case DSP_BASE ... DSP_BASE + 5:
256 if (!cpu_has_dsp) {
257 ret = -EIO;
258 break;
259 }
260
261 dspreg_t *dregs = __get_dsp_regs(child);
262 dregs[addr - DSP_BASE] = data;
263 break;
264 case DSP_CONTROL:
265 if (!cpu_has_dsp) {
266 ret = -EIO;
267 break;
268 }
269 child->thread.dsp.dspcontrol = data;
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 default:
272 /* The rest are not allowed. */
273 ret = -EIO;
274 break;
275 }
276 break;
277 }
278
279 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
280 case PTRACE_CONT: { /* restart after signal. */
281 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700282 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 if (request == PTRACE_SYSCALL) {
285 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
286 }
287 else {
288 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
289 }
290 child->exit_code = data;
291 wake_up_process(child);
292 ret = 0;
293 break;
294 }
295
296 /*
297 * make the child exit. Best I can do is send it a sigkill.
298 * perhaps it should be put in the status that it wants to
299 * exit.
300 */
301 case PTRACE_KILL:
302 ret = 0;
303 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
304 break;
305 child->exit_code = SIGKILL;
306 wake_up_process(child);
307 break;
308
Ralf Baechle3c370262005-04-13 17:43:59 +0000309 case PTRACE_GET_THREAD_AREA:
310 ret = put_user(child->thread_info->tp_value,
311 (unsigned int __user *) (unsigned long) data);
312 break;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case PTRACE_DETACH: /* detach a process that was attached. */
315 ret = ptrace_detach(child, data);
316 break;
317
Ralf Baechle09276d92005-02-16 21:22:40 +0000318 case PTRACE_GETEVENTMSG:
319 ret = put_user(child->ptrace_message,
320 (unsigned int __user *) (unsigned long) data);
321 break;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 default:
324 ret = ptrace_request(child, request, addr, data);
325 break;
326 }
327
328out_tsk:
329 put_task_struct(child);
330out:
331 unlock_kernel();
332 return ret;
333}