blob: 7e55457a491f3c71b730dd5c3286030db571f02f [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 Baechlee50c0a8f2005-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>
Ralf Baechle101b3532005-10-06 17:39:32 +010032#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/pgtable.h>
34#include <asm/page.h>
35#include <asm/system.h>
36#include <asm/uaccess.h>
37#include <asm/bootinfo.h>
38
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -040039int ptrace_getregs (struct task_struct *child, __s64 __user *data);
40int ptrace_setregs (struct task_struct *child, __s64 __user *data);
41
42int ptrace_getfpregs (struct task_struct *child, __u32 __user *data);
43int ptrace_setfpregs (struct task_struct *child, __u32 __user *data);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * Tracing a 32-bit process with a 64-bit strace and vice versa will not
47 * work. I don't know how to fix this.
48 */
49asmlinkage int sys32_ptrace(int request, int pid, int addr, int data)
50{
51 struct task_struct *child;
52 int ret;
53
54#if 0
55 printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
56 (int) request, (int) pid, (unsigned long) addr,
57 (unsigned long) data);
58#endif
59 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 if (request == PTRACE_TRACEME) {
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -080061 ret = ptrace_traceme();
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 goto out;
63 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -080065 child = ptrace_get_task_struct(pid);
66 if (IS_ERR(child)) {
67 ret = PTR_ERR(child);
68 goto out;
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 if (request == PTRACE_ATTACH) {
72 ret = ptrace_attach(child);
73 goto out_tsk;
74 }
75
76 ret = ptrace_check_attach(child, request == PTRACE_KILL);
77 if (ret < 0)
78 goto out_tsk;
79
80 switch (request) {
81 /* when I and D space are separate, these will need to be fixed. */
82 case PTRACE_PEEKTEXT: /* read word at location addr. */
83 case PTRACE_PEEKDATA: {
84 unsigned int tmp;
85 int copied;
86
87 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
88 ret = -EIO;
89 if (copied != sizeof(tmp))
90 break;
91 ret = put_user(tmp, (unsigned int *) (unsigned long) data);
92 break;
93 }
94
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -040095 /*
96 * Read 4 bytes of the other process' storage
97 * data is a pointer specifying where the user wants the
98 * 4 bytes copied into
99 * addr is a pointer in the user's storage that contains an 8 byte
100 * address in the other process of the 4 bytes that is to be read
101 * (this is run in a 32-bit process looking at a 64-bit process)
102 * when I and D space are separate, these will need to be fixed.
103 */
104 case PTRACE_PEEKTEXT_3264:
105 case PTRACE_PEEKDATA_3264: {
106 u32 tmp;
107 int copied;
108 u32 __user * addrOthers;
109
110 ret = -EIO;
111
112 /* Get the addr in the other process that we want to read */
113 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
114 break;
115
116 copied = access_process_vm(child, (u64)addrOthers, &tmp,
117 sizeof(tmp), 0);
118 if (copied != sizeof(tmp))
119 break;
120 ret = put_user(tmp, (u32 __user *) (unsigned long) data);
121 break;
122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* Read the word at location addr in the USER area. */
125 case PTRACE_PEEKUSR: {
126 struct pt_regs *regs;
127 unsigned int tmp;
128
129 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
130 THREAD_SIZE - 32 - sizeof(struct pt_regs));
131 ret = 0; /* Default return value. */
132
133 switch (addr) {
134 case 0 ... 31:
135 tmp = regs->regs[addr];
136 break;
137 case FPR_BASE ... FPR_BASE + 31:
138 if (tsk_used_math(child)) {
139 fpureg_t *fregs = get_fpu_regs(child);
140
141 /*
142 * The odd registers are actually the high
143 * order bits of the values stored in the even
144 * registers - unless we're using r2k_switch.S.
145 */
146 if (addr & 1)
147 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
148 else
149 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
150 } else {
151 tmp = -1; /* FP not yet used */
152 }
153 break;
154 case PC:
155 tmp = regs->cp0_epc;
156 break;
157 case CAUSE:
158 tmp = regs->cp0_cause;
159 break;
160 case BADVADDR:
161 tmp = regs->cp0_badvaddr;
162 break;
163 case MMHI:
164 tmp = regs->hi;
165 break;
166 case MMLO:
167 tmp = regs->lo;
168 break;
169 case FPC_CSR:
170 if (cpu_has_fpu)
171 tmp = child->thread.fpu.hard.fcr31;
172 else
173 tmp = child->thread.fpu.soft.fcr31;
174 break;
175 case FPC_EIR: { /* implementation / version register */
176 unsigned int flags;
177
178 if (!cpu_has_fpu)
179 break;
180
Ralf Baechle101b3532005-10-06 17:39:32 +0100181 preempt_disable();
182 if (cpu_has_mipsmt) {
183 unsigned int vpflags = dvpe();
184 flags = read_c0_status();
185 __enable_fpu();
186 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
187 write_c0_status(flags);
188 evpe(vpflags);
189 } else {
190 flags = read_c0_status();
191 __enable_fpu();
192 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
193 write_c0_status(flags);
194 }
195 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
197 }
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000198 case DSP_BASE ... DSP_BASE + 5:
199 if (!cpu_has_dsp) {
200 tmp = 0;
201 ret = -EIO;
202 goto out_tsk;
203 }
204 if (child->thread.dsp.used_dsp) {
205 dspreg_t *dregs = __get_dsp_regs(child);
206 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
207 } else {
208 tmp = -1; /* DSP registers yet used */
209 }
210 break;
211 case DSP_CONTROL:
212 if (!cpu_has_dsp) {
213 tmp = 0;
214 ret = -EIO;
215 goto out_tsk;
216 }
217 tmp = child->thread.dsp.dspcontrol;
218 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 default:
220 tmp = 0;
221 ret = -EIO;
222 goto out_tsk;
223 }
224 ret = put_user(tmp, (unsigned *) (unsigned long) data);
225 break;
226 }
227
228 /* when I and D space are separate, this will have to be fixed. */
229 case PTRACE_POKETEXT: /* write the word at location addr. */
230 case PTRACE_POKEDATA:
231 ret = 0;
232 if (access_process_vm(child, addr, &data, sizeof(data), 1)
233 == sizeof(data))
234 break;
235 ret = -EIO;
236 break;
237
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400238 /*
239 * Write 4 bytes into the other process' storage
240 * data is the 4 bytes that the user wants written
241 * addr is a pointer in the user's storage that contains an
242 * 8 byte address in the other process where the 4 bytes
243 * that is to be written
244 * (this is run in a 32-bit process looking at a 64-bit process)
245 * when I and D space are separate, these will need to be fixed.
246 */
247 case PTRACE_POKETEXT_3264:
248 case PTRACE_POKEDATA_3264: {
249 u32 __user * addrOthers;
250
251 /* Get the addr in the other process that we want to write into */
252 ret = -EIO;
253 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
254 break;
255 ret = 0;
256 if (access_process_vm(child, (u64)addrOthers, &data,
257 sizeof(data), 1) == sizeof(data))
258 break;
259 ret = -EIO;
260 break;
261 }
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 case PTRACE_POKEUSR: {
264 struct pt_regs *regs;
265 ret = 0;
266 regs = (struct pt_regs *) ((unsigned long) child->thread_info +
267 THREAD_SIZE - 32 - sizeof(struct pt_regs));
268
269 switch (addr) {
270 case 0 ... 31:
271 regs->regs[addr] = data;
272 break;
273 case FPR_BASE ... FPR_BASE + 31: {
274 fpureg_t *fregs = get_fpu_regs(child);
275
276 if (!tsk_used_math(child)) {
277 /* FP not yet used */
278 memset(&child->thread.fpu.hard, ~0,
279 sizeof(child->thread.fpu.hard));
280 child->thread.fpu.hard.fcr31 = 0;
281 }
282 /*
283 * The odd registers are actually the high order bits
284 * of the values stored in the even registers - unless
285 * we're using r2k_switch.S.
286 */
287 if (addr & 1) {
288 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
289 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
290 } else {
291 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
292 /* Must cast, lest sign extension fill upper
293 bits! */
294 fregs[addr - FPR_BASE] |= (unsigned int)data;
295 }
296 break;
297 }
298 case PC:
299 regs->cp0_epc = data;
300 break;
301 case MMHI:
302 regs->hi = data;
303 break;
304 case MMLO:
305 regs->lo = data;
306 break;
307 case FPC_CSR:
308 if (cpu_has_fpu)
309 child->thread.fpu.hard.fcr31 = data;
310 else
311 child->thread.fpu.soft.fcr31 = data;
312 break;
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000313 case DSP_BASE ... DSP_BASE + 5:
314 if (!cpu_has_dsp) {
315 ret = -EIO;
316 break;
317 }
318
319 dspreg_t *dregs = __get_dsp_regs(child);
320 dregs[addr - DSP_BASE] = data;
321 break;
322 case DSP_CONTROL:
323 if (!cpu_has_dsp) {
324 ret = -EIO;
325 break;
326 }
327 child->thread.dsp.dspcontrol = data;
328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 default:
330 /* The rest are not allowed. */
331 ret = -EIO;
332 break;
333 }
334 break;
335 }
336
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400337 case PTRACE_GETREGS:
338 ret = ptrace_getregs (child, (__u64 __user *) (__u64) data);
339 break;
340
341 case PTRACE_SETREGS:
342 ret = ptrace_setregs (child, (__u64 __user *) (__u64) data);
343 break;
344
345 case PTRACE_GETFPREGS:
346 ret = ptrace_getfpregs (child, (__u32 __user *) (__u64) data);
347 break;
348
349 case PTRACE_SETFPREGS:
350 ret = ptrace_setfpregs (child, (__u32 __user *) (__u64) data);
351 break;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
354 case PTRACE_CONT: { /* restart after signal. */
355 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700356 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 if (request == PTRACE_SYSCALL) {
359 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
360 }
361 else {
362 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
363 }
364 child->exit_code = data;
365 wake_up_process(child);
366 ret = 0;
367 break;
368 }
369
370 /*
371 * make the child exit. Best I can do is send it a sigkill.
372 * perhaps it should be put in the status that it wants to
373 * exit.
374 */
375 case PTRACE_KILL:
376 ret = 0;
377 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
378 break;
379 child->exit_code = SIGKILL;
380 wake_up_process(child);
381 break;
382
Ralf Baechle3c370262005-04-13 17:43:59 +0000383 case PTRACE_GET_THREAD_AREA:
384 ret = put_user(child->thread_info->tp_value,
385 (unsigned int __user *) (unsigned long) data);
386 break;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 case PTRACE_DETACH: /* detach a process that was attached. */
389 ret = ptrace_detach(child, data);
390 break;
391
Ralf Baechle09276d92005-02-16 21:22:40 +0000392 case PTRACE_GETEVENTMSG:
393 ret = put_user(child->ptrace_message,
394 (unsigned int __user *) (unsigned long) data);
395 break;
396
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400397 case PTRACE_GET_THREAD_AREA_3264:
398 ret = put_user(child->thread_info->tp_value,
399 (unsigned long __user *) (unsigned long) data);
400 break;
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 default:
403 ret = ptrace_request(child, request, addr, data);
404 break;
405 }
406
407out_tsk:
408 put_task_struct(child);
409out:
410 unlock_kernel();
411 return ret;
412}