blob: c4f9ac17474a2157d7a6ab12e0c485cd61b11c48 [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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -040053 /*
54 * Read 4 bytes of the other process' storage
55 * data is a pointer specifying where the user wants the
56 * 4 bytes copied into
57 * addr is a pointer in the user's storage that contains an 8 byte
58 * address in the other process of the 4 bytes that is to be read
59 * (this is run in a 32-bit process looking at a 64-bit process)
60 * when I and D space are separate, these will need to be fixed.
61 */
62 case PTRACE_PEEKTEXT_3264:
63 case PTRACE_PEEKDATA_3264: {
64 u32 tmp;
65 int copied;
66 u32 __user * addrOthers;
67
68 ret = -EIO;
69
70 /* Get the addr in the other process that we want to read */
71 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
72 break;
73
74 copied = access_process_vm(child, (u64)addrOthers, &tmp,
75 sizeof(tmp), 0);
76 if (copied != sizeof(tmp))
77 break;
78 ret = put_user(tmp, (u32 __user *) (unsigned long) data);
79 break;
80 }
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 /* Read the word at location addr in the USER area. */
83 case PTRACE_PEEKUSR: {
84 struct pt_regs *regs;
85 unsigned int tmp;
86
Al Viro40bc9c62006-01-12 01:06:07 -080087 regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 ret = 0; /* Default return value. */
89
90 switch (addr) {
91 case 0 ... 31:
92 tmp = regs->regs[addr];
93 break;
94 case FPR_BASE ... FPR_BASE + 31:
95 if (tsk_used_math(child)) {
96 fpureg_t *fregs = get_fpu_regs(child);
97
98 /*
99 * The odd registers are actually the high
100 * order bits of the values stored in the even
101 * registers - unless we're using r2k_switch.S.
102 */
103 if (addr & 1)
104 tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
105 else
106 tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
107 } else {
108 tmp = -1; /* FP not yet used */
109 }
110 break;
111 case PC:
112 tmp = regs->cp0_epc;
113 break;
114 case CAUSE:
115 tmp = regs->cp0_cause;
116 break;
117 case BADVADDR:
118 tmp = regs->cp0_badvaddr;
119 break;
120 case MMHI:
121 tmp = regs->hi;
122 break;
123 case MMLO:
124 tmp = regs->lo;
125 break;
126 case FPC_CSR:
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900127 tmp = child->thread.fpu.fcr31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 break;
129 case FPC_EIR: { /* implementation / version register */
130 unsigned int flags;
Ralf Baechle41c594a2006-04-05 09:45:45 +0100131#ifdef CONFIG_MIPS_MT_SMTC
132 unsigned int irqflags;
133 unsigned int mtflags;
134#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900136 preempt_disable();
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900137 if (!cpu_has_fpu) {
Atsushi Nemotoe04582b2006-10-09 00:10:01 +0900138 preempt_enable();
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900139 tmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Ralf Baechle41c594a2006-04-05 09:45:45 +0100143#ifdef CONFIG_MIPS_MT_SMTC
144 /* Read-modify-write of Status must be atomic */
145 local_irq_save(irqflags);
146 mtflags = dmt();
147#endif /* CONFIG_MIPS_MT_SMTC */
148
Ralf Baechle101b3532005-10-06 17:39:32 +0100149 if (cpu_has_mipsmt) {
150 unsigned int vpflags = dvpe();
151 flags = read_c0_status();
152 __enable_fpu();
153 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
154 write_c0_status(flags);
155 evpe(vpflags);
156 } else {
157 flags = read_c0_status();
158 __enable_fpu();
159 __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
160 write_c0_status(flags);
161 }
Ralf Baechle41c594a2006-04-05 09:45:45 +0100162#ifdef CONFIG_MIPS_MT_SMTC
163 emt(mtflags);
164 local_irq_restore(irqflags);
165#endif /* CONFIG_MIPS_MT_SMTC */
Ralf Baechle101b3532005-10-06 17:39:32 +0100166 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900169 case DSP_BASE ... DSP_BASE + 5: {
170 dspreg_t *dregs;
171
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000172 if (!cpu_has_dsp) {
173 tmp = 0;
174 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200175 goto out;
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000176 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900177 dregs = __get_dsp_regs(child);
Ralf Baechle6c355852005-12-05 13:47:25 +0000178 tmp = (unsigned long) (dregs[addr - DSP_BASE]);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000179 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900180 }
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000181 case DSP_CONTROL:
182 if (!cpu_has_dsp) {
183 tmp = 0;
184 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200185 goto out;
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000186 }
187 tmp = child->thread.dsp.dspcontrol;
188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 default:
190 tmp = 0;
191 ret = -EIO;
Thomas Bogendoerfer5d9a76c2008-08-17 16:49:25 +0200192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900194 ret = put_user(tmp, (unsigned __user *) (unsigned long) data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 break;
196 }
197
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400198 /*
199 * Write 4 bytes into the other process' storage
200 * data is the 4 bytes that the user wants written
201 * addr is a pointer in the user's storage that contains an
202 * 8 byte address in the other process where the 4 bytes
203 * that is to be written
204 * (this is run in a 32-bit process looking at a 64-bit process)
205 * when I and D space are separate, these will need to be fixed.
206 */
207 case PTRACE_POKETEXT_3264:
208 case PTRACE_POKEDATA_3264: {
209 u32 __user * addrOthers;
210
211 /* Get the addr in the other process that we want to write into */
212 ret = -EIO;
213 if (get_user(addrOthers, (u32 __user * __user *) (unsigned long) addr) != 0)
214 break;
215 ret = 0;
216 if (access_process_vm(child, (u64)addrOthers, &data,
217 sizeof(data), 1) == sizeof(data))
218 break;
219 ret = -EIO;
220 break;
221 }
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 case PTRACE_POKEUSR: {
224 struct pt_regs *regs;
225 ret = 0;
Al Viro40bc9c62006-01-12 01:06:07 -0800226 regs = task_pt_regs(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 switch (addr) {
229 case 0 ... 31:
230 regs->regs[addr] = data;
231 break;
232 case FPR_BASE ... FPR_BASE + 31: {
233 fpureg_t *fregs = get_fpu_regs(child);
234
235 if (!tsk_used_math(child)) {
236 /* FP not yet used */
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900237 memset(&child->thread.fpu, ~0,
238 sizeof(child->thread.fpu));
239 child->thread.fpu.fcr31 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241 /*
242 * The odd registers are actually the high order bits
243 * of the values stored in the even registers - unless
244 * we're using r2k_switch.S.
245 */
246 if (addr & 1) {
247 fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
248 fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
249 } else {
250 fregs[addr - FPR_BASE] &= ~0xffffffffLL;
251 /* Must cast, lest sign extension fill upper
252 bits! */
253 fregs[addr - FPR_BASE] |= (unsigned int)data;
254 }
255 break;
256 }
257 case PC:
258 regs->cp0_epc = data;
259 break;
260 case MMHI:
261 regs->hi = data;
262 break;
263 case MMLO:
264 regs->lo = data;
265 break;
266 case FPC_CSR:
Atsushi Nemotoeae89072006-05-16 01:26:03 +0900267 child->thread.fpu.fcr31 = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900269 case DSP_BASE ... DSP_BASE + 5: {
270 dspreg_t *dregs;
271
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000272 if (!cpu_has_dsp) {
273 ret = -EIO;
274 break;
275 }
276
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900277 dregs = __get_dsp_regs(child);
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000278 dregs[addr - DSP_BASE] = data;
279 break;
Atsushi Nemoto3055acb2006-01-29 22:34:32 +0900280 }
Ralf Baechlee50c0a8f2005-05-31 11:49:19 +0000281 case DSP_CONTROL:
282 if (!cpu_has_dsp) {
283 ret = -EIO;
284 break;
285 }
286 child->thread.dsp.dspcontrol = data;
287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 default:
289 /* The rest are not allowed. */
290 ret = -EIO;
291 break;
292 }
293 break;
294 }
295
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400296 case PTRACE_GETREGS:
Atsushi Nemoto62b14c22007-10-26 00:53:02 +0900297 ret = ptrace_getregs(child, (__s64 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400298 break;
299
300 case PTRACE_SETREGS:
Atsushi Nemoto62b14c22007-10-26 00:53:02 +0900301 ret = ptrace_setregs(child, (__s64 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400302 break;
303
304 case PTRACE_GETFPREGS:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100305 ret = ptrace_getfpregs(child, (__u32 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400306 break;
307
308 case PTRACE_SETFPREGS:
Ralf Baechle49a89ef2007-10-11 23:46:15 +0100309 ret = ptrace_setfpregs(child, (__u32 __user *) (__u64) data);
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400310 break;
311
Ralf Baechle3c370262005-04-13 17:43:59 +0000312 case PTRACE_GET_THREAD_AREA:
Al Virodc8f6022006-01-12 01:06:07 -0800313 ret = put_user(task_thread_info(child)->tp_value,
Ralf Baechle3c370262005-04-13 17:43:59 +0000314 (unsigned int __user *) (unsigned long) data);
315 break;
316
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400317 case PTRACE_GET_THREAD_AREA_3264:
Al Virodc8f6022006-01-12 01:06:07 -0800318 ret = put_user(task_thread_info(child)->tp_value,
Daniel Jacobowitzea3d7102005-09-28 18:11:15 -0400319 (unsigned long __user *) (unsigned long) data);
320 break;
321
David Daney0926bf92008-09-23 00:11:26 -0700322 case PTRACE_GET_WATCH_REGS:
323 ret = ptrace_get_watch_regs(child,
324 (struct pt_watch_regs __user *) (unsigned long) addr);
325 break;
326
327 case PTRACE_SET_WATCH_REGS:
328 ret = ptrace_set_watch_regs(child,
329 (struct pt_watch_regs __user *) (unsigned long) addr);
330 break;
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 default:
Anirban Sinha797c3f32008-11-13 11:50:12 -0800333 ret = compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return ret;
338}