blob: 1ca34104e59342147713e3147b29a5ac9b0421df [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>
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +020018#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
20#include <linux/sched.h>
21#include <linux/mm.h>
22#include <linux/errno.h>
23#include <linux/ptrace.h>
24#include <linux/smp.h>
25#include <linux/smp_lock.h>
26#include <linux/user.h>
27#include <linux/security.h>
28
29#include <asm/cpu.h>
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +000030#include <asm/dsp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/fpu.h>
32#include <asm/mipsregs.h>
Ralf Baechle101b3532005-10-06 17:39:32 +010033#include <asm/mipsmtregs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/pgtable.h>
35#include <asm/page.h>
36#include <asm/system.h>
37#include <asm/uaccess.h>
38#include <asm/bootinfo.h>
39
40/*
41 * Tracing a 32-bit process with a 64-bit strace and vice versa will not
42 * work. I don't know how to fix this.
43 */
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +020044long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
45 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +020047 int addr = caddr;
48 int data = cdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 int ret;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 switch (request) {
52 /* when I and D space are separate, these will need to be fixed. */
53 case PTRACE_PEEKTEXT: /* read word at location addr. */
54 case PTRACE_PEEKDATA: {
55 unsigned int tmp;
56 int copied;
57
58 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
59 ret = -EIO;
60 if (copied != sizeof(tmp))
61 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +090062 ret = put_user(tmp, (unsigned int __user *) (unsigned long) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64 }
65
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -040066 /*
67 * Read 4 bytes of the other process' storage
68 * data is a pointer specifying where the user wants the
69 * 4 bytes copied into
70 * addr is a pointer in the user's storage that contains an 8 byte
71 * address in the other process of the 4 bytes that is to be read
72 * (this is run in a 32-bit process looking at a 64-bit process)
73 * when I and D space are separate, these will need to be fixed.
74 */
75 case PTRACE_PEEKTEXT_3264:
76 case PTRACE_PEEKDATA_3264: {
77 u32 tmp;
78 int copied;
79 u32 __user * addrOthers;
80
81 ret = -EIO;
82
83 /* Get the addr in the other process that we want to read */
84 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
85 break;
86
87 copied = access_process_vm(child, (u64)addrOthers, &tmp,
88 sizeof(tmp), 0);
89 if (copied != sizeof(tmp))
90 break;
91 ret = put_user(tmp, (u32 __user *) (unsigned long) data);
92 break;
93 }
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 /* Read the word at location addr in the USER area. */
96 case PTRACE_PEEKUSR: {
97 struct pt_regs *regs;
98 unsigned int tmp;
99
Al Viro40bc9c62006-01-12 01:06:07 -0800100 regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 ret = 0; /* Default return value. */
102
103 switch (addr) {
104 case 0 ... 31:
105 tmp = regs->regs[addr];
106 break;
107 case FPR_BASE ... FPR_BASE + 31:
108 if (tsk_used_math(child)) {
109 fpureg_t *fregs = get_fpu_regs(child);
110
111 /*
112 * The odd registers are actually the high
113 * order bits of the values stored in the even
114 * registers - unless we're using r2k_switch.S.
115 */
116 if (addr & 1)
117 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
118 else
119 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
120 } else {
121 tmp = -1; /* FP not yet used */
122 }
123 break;
124 case PC:
125 tmp = regs->cp0_epc;
126 break;
127 case CAUSE:
128 tmp = regs->cp0_cause;
129 break;
130 case BADVADDR:
131 tmp = regs->cp0_badvaddr;
132 break;
133 case MMHI:
134 tmp = regs->hi;
135 break;
136 case MMLO:
137 tmp = regs->lo;
138 break;
139 case FPC_CSR:
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900140 tmp = child->thread.fpu.fcr31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 case FPC_EIR: { /* implementation / version register */
143 unsigned int flags;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100144#ifdef CONFIG_MIPS_MT_SMTC
145 unsigned int irqflags;
146 unsigned int mtflags;
147#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900149 preempt_disable();
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900150 if (!cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900151 preempt_enable();
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900152 tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Ralf Baechle41c594a2006-04-05 09:45:45 +0100156#ifdef CONFIG_MIPS_MT_SMTC
157 /* Read-modify-write of Status must be atomic */
158 local_irq_save(irqflags);
159 mtflags = dmt();
160#endif /* CONFIG_MIPS_MT_SMTC */
161
Ralf Baechle101b3532005-10-06 17:39:32 +0100162 if (cpu_has_mipsmt) {
163 unsigned int vpflags = dvpe();
164 flags = read_c0_status();
165 __enable_fpu();
166 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
167 write_c0_status(flags);
168 evpe(vpflags);
169 } else {
170 flags = read_c0_status();
171 __enable_fpu();
172 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
173 write_c0_status(flags);
174 }
Ralf Baechle41c594a2006-04-05 09:45:45 +0100175#ifdef CONFIG_MIPS_MT_SMTC
176 emt(mtflags);
177 local_irq_restore(irqflags);
178#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle101b3532005-10-06 17:39:32 +0100179 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 break;
181 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900182 case DSP_BASE ... DSP_BASE + 5: {
183 dspreg_t *dregs;
184
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000185 if (!cpu_has_dsp) {
186 tmp = 0;
187 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200188 goto out;
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000189 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900190 dregs = __get_dsp_regs(child);
Ralf Baechle6c355852005-12-05 13:47:25 +0000191 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000192 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900193 }
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000194 case DSP_CONTROL:
195 if (!cpu_has_dsp) {
196 tmp = 0;
197 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200198 goto out;
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000199 }
200 tmp = child->thread.dsp.dspcontrol;
201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 default:
203 tmp = 0;
204 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900207 ret = put_user(tmp, (unsigned __user *) (unsigned long) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 break;
209 }
210
211 /* when I and D space are separate, this will have to be fixed. */
212 case PTRACE_POKETEXT: /* write the word at location addr. */
213 case PTRACE_POKEDATA:
214 ret = 0;
215 if (access_process_vm(child, addr, &data, sizeof(data), 1)
216 == sizeof(data))
217 break;
218 ret = -EIO;
219 break;
220
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400221 /*
222 * Write 4 bytes into the other process' storage
223 * data is the 4 bytes that the user wants written
224 * addr is a pointer in the user's storage that contains an
225 * 8 byte address in the other process where the 4 bytes
226 * that is to be written
227 * (this is run in a 32-bit process looking at a 64-bit process)
228 * when I and D space are separate, these will need to be fixed.
229 */
230 case PTRACE_POKETEXT_3264:
231 case PTRACE_POKEDATA_3264: {
232 u32 __user * addrOthers;
233
234 /* Get the addr in the other process that we want to write into */
235 ret = -EIO;
236 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
237 break;
238 ret = 0;
239 if (access_process_vm(child, (u64)addrOthers, &data,
240 sizeof(data), 1) == sizeof(data))
241 break;
242 ret = -EIO;
243 break;
244 }
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 case PTRACE_POKEUSR: {
247 struct pt_regs *regs;
248 ret = 0;
Al Viro40bc9c62006-01-12 01:06:07 -0800249 regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 switch (addr) {
252 case 0 ... 31:
253 regs->regs[addr] = data;
254 break;
255 case FPR_BASE ... FPR_BASE + 31: {
256 fpureg_t *fregs = get_fpu_regs(child);
257
258 if (!tsk_used_math(child)) {
259 /* FP not yet used */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900260 memset(&child->thread.fpu, ~0,
261 sizeof(child->thread.fpu));
262 child->thread.fpu.fcr31 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 /*
265 * The odd registers are actually the high order bits
266 * of the values stored in the even registers - unless
267 * we're using r2k_switch.S.
268 */
269 if (addr & 1) {
270 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
271 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
272 } else {
273 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
274 /* Must cast, lest sign extension fill upper
275 bits! */
276 fregs[addr - FPR_BASE] |= (unsigned int)data;
277 }
278 break;
279 }
280 case PC:
281 regs->cp0_epc = data;
282 break;
283 case MMHI:
284 regs->hi = data;
285 break;
286 case MMLO:
287 regs->lo = data;
288 break;
289 case FPC_CSR:
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900290 child->thread.fpu.fcr31 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900292 case DSP_BASE ... DSP_BASE + 5: {
293 dspreg_t *dregs;
294
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000295 if (!cpu_has_dsp) {
296 ret = -EIO;
297 break;
298 }
299
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900300 dregs = __get_dsp_regs(child);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000301 dregs[addr - DSP_BASE] = data;
302 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900303 }
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000304 case DSP_CONTROL:
305 if (!cpu_has_dsp) {
306 ret = -EIO;
307 break;
308 }
309 child->thread.dsp.dspcontrol = data;
310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 default:
312 /* The rest are not allowed. */
313 ret = -EIO;
314 break;
315 }
316 break;
317 }
318
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400319 case PTRACE_GETREGS:
Atsushi Nemoto62b14c22007-10-26 00:53:02 +0900320 ret = ptrace_getregs(child, (__s64 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400321 break;
322
323 case PTRACE_SETREGS:
Atsushi Nemoto62b14c22007-10-26 00:53:02 +0900324 ret = ptrace_setregs(child, (__s64 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400325 break;
326
327 case PTRACE_GETFPREGS:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100328 ret = ptrace_getfpregs(child, (__u32 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400329 break;
330
331 case PTRACE_SETFPREGS:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100332 ret = ptrace_setfpregs(child, (__u32 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400333 break;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
336 case PTRACE_CONT: { /* restart after signal. */
337 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700338 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 break;
340 if (request == PTRACE_SYSCALL) {
341 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
342 }
343 else {
344 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
345 }
346 child->exit_code = data;
347 wake_up_process(child);
348 ret = 0;
349 break;
350 }
351
352 /*
353 * make the child exit. Best I can do is send it a sigkill.
354 * perhaps it should be put in the status that it wants to
355 * exit.
356 */
357 case PTRACE_KILL:
358 ret = 0;
359 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
360 break;
361 child->exit_code = SIGKILL;
362 wake_up_process(child);
363 break;
364
Ralf Baechle3c370262005-04-13 17:43:59 +0000365 case PTRACE_GET_THREAD_AREA:
Al Virodc8f6022006-01-12 01:06:07 -0800366 ret = put_user(task_thread_info(child)->tp_value,
Ralf Baechle3c370262005-04-13 17:43:59 +0000367 (unsigned int __user *) (unsigned long) data);
368 break;
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case PTRACE_DETACH: /* detach a process that was attached. */
371 ret = ptrace_detach(child, data);
372 break;
373
Ralf Baechle09276d92005-02-16 21:22:40 +0000374 case PTRACE_GETEVENTMSG:
375 ret = put_user(child->ptrace_message,
376 (unsigned int __user *) (unsigned long) data);
377 break;
378
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400379 case PTRACE_GET_THREAD_AREA_3264:
Al Virodc8f6022006-01-12 01:06:07 -0800380 ret = put_user(task_thread_info(child)->tp_value,
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400381 (unsigned long __user *) (unsigned long) data);
382 break;
383
David Daney0926bf92008-09-23 00:11:26 -0700384 case PTRACE_GET_WATCH_REGS:
385 ret = ptrace_get_watch_regs(child,
386 (struct pt_watch_regs __user *) (unsigned long) addr);
387 break;
388
389 case PTRACE_SET_WATCH_REGS:
390 ret = ptrace_set_watch_regs(child,
391 (struct pt_watch_regs __user *) (unsigned long) addr);
392 break;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 default:
395 ret = ptrace_request(child, request, addr, data);
396 break;
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return ret;
400}