blob: 197d49c790ad55b2e67b6b5a989bb9b958dfd14c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mackerrasb1239232005-10-20 09:11:29 +10002 * ptrace for 32-bit processes running on a 64-bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * PowerPC version
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 *
7 * Derived from "arch/m68k/kernel/ptrace.c"
8 * Copyright (C) 1994 by Hamish Macdonald
9 * Taken from linux/kernel/ptrace.c and modified for M680x0.
10 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
11 *
12 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
Paul Mackerrasb1239232005-10-20 09:11:29 +100013 * and Paul Mackerras (paulus@samba.org).
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This file is subject to the terms and conditions of the GNU General
Paul Mackerrasb1239232005-10-20 09:11:29 +100016 * Public License. See the file COPYING in the main directory of
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * this archive for more details.
18 */
19
20#include <linux/kernel.h>
21#include <linux/sched.h>
22#include <linux/mm.h>
23#include <linux/smp.h>
24#include <linux/smp_lock.h>
25#include <linux/errno.h>
26#include <linux/ptrace.h>
Roland McGrath0deef2c2007-12-20 03:59:04 -080027#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/user.h>
29#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Roland McGrath1d48d712007-12-20 03:58:49 -080031#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/uaccess.h>
34#include <asm/page.h>
35#include <asm/pgtable.h>
36#include <asm/system.h>
Paul Mackerras21a62902005-11-19 20:47:22 +110037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/*
39 * does not yet catch signals sent when the child dies.
40 * in exit.c or in signal.c.
41 */
42
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100043/*
44 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
45 * we mark them as obsolete now, they will be removed in a future version
46 */
47static long compat_ptrace_old(struct task_struct *child, long request,
48 long addr, long data)
49{
Roland McGrath0deef2c2007-12-20 03:59:04 -080050 switch (request) {
51 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
52 return copy_regset_to_user(child,
53 task_user_regset_view(current), 0,
54 0, 32 * sizeof(compat_long_t),
55 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100056
Roland McGrath0deef2c2007-12-20 03:59:04 -080057 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
58 return copy_regset_from_user(child,
59 task_user_regset_view(current), 0,
60 0, 32 * sizeof(compat_long_t),
61 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100062 }
63
Roland McGrath0deef2c2007-12-20 03:59:04 -080064 return -EPERM;
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100065}
66
Michael Neuling9c75a312008-06-26 17:07:48 +100067/* Macros to workout the correct index for the FPR in the thread struct */
68#define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
69#define FPRHALF(i) (((i) - PT_FPR0) & 1)
70#define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) + FPRHALF(i)
71
Roland McGrath81e695c2007-12-20 03:58:55 -080072long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
73 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Roland McGrath81e695c2007-12-20 03:58:55 -080075 unsigned long addr = caddr;
76 unsigned long data = cdata;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -080077 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 /*
81 * Read 4 bytes of the other process' storage
82 * data is a pointer specifying where the user wants the
83 * 4 bytes copied into
84 * addr is a pointer in the user's storage that contains an 8 byte
85 * address in the other process of the 4 bytes that is to be read
86 * (this is run in a 32-bit process looking at a 64-bit process)
87 * when I and D space are separate, these will need to be fixed.
88 */
89 case PPC_PTRACE_PEEKTEXT_3264:
90 case PPC_PTRACE_PEEKDATA_3264: {
91 u32 tmp;
92 int copied;
93 u32 __user * addrOthers;
94
95 ret = -EIO;
96
97 /* Get the addr in the other process that we want to read */
98 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
99 break;
100
101 copied = access_process_vm(child, (u64)addrOthers, &tmp,
102 sizeof(tmp), 0);
103 if (copied != sizeof(tmp))
104 break;
105 ret = put_user(tmp, (u32 __user *)data);
106 break;
107 }
108
109 /* Read a register (specified by ADDR) out of the "user area" */
110 case PTRACE_PEEKUSR: {
111 int index;
112 unsigned long tmp;
113
114 ret = -EIO;
115 /* convert to index and check */
116 index = (unsigned long) addr >> 2;
117 if ((addr & 3) || (index > PT_FPSCR32))
118 break;
119
Roland McGrathfabca2c2007-09-25 09:50:52 +1000120 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000122 tmp = ptrace_get_reg(child, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else {
124 flush_fp_to_thread(child);
125 /*
126 * the user space code considers the floating point
127 * to be an array of unsigned int (32 bits) - the
128 * index passed in is based on this assumption.
129 */
Michael Neuling9c75a312008-06-26 17:07:48 +1000130 tmp = ((unsigned int *)child->thread.fpr)
131 [FPRINDEX(index)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133 ret = put_user((unsigned int)tmp, (u32 __user *)data);
134 break;
135 }
136
137 /*
138 * Read 4 bytes out of the other process' pt_regs area
139 * data is a pointer specifying where the user wants the
140 * 4 bytes copied into
141 * addr is the offset into the other process' pt_regs structure
142 * that is to be read
143 * (this is run in a 32-bit process looking at a 64-bit process)
144 */
145 case PPC_PTRACE_PEEKUSR_3264: {
146 u32 index;
147 u32 reg32bits;
148 u64 tmp;
149 u32 numReg;
150 u32 part;
151
152 ret = -EIO;
153 /* Determine which register the user wants */
154 index = (u64)addr >> 2;
155 numReg = index / 2;
156 /* Determine which part of the register the user wants */
157 if (index % 2)
158 part = 1; /* want the 2nd half of the register (right-most). */
159 else
160 part = 0; /* want the 1st half of the register (left-most). */
161
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000162 /* Validate the input - check to see if address is on the wrong boundary
163 * or beyond the end of the user area
164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if ((addr & 3) || numReg > PT_FPSCR)
166 break;
167
Roland McGrathfabca2c2007-09-25 09:50:52 +1000168 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (numReg >= PT_FPR0) {
170 flush_fp_to_thread(child);
Michael Neuling9c75a312008-06-26 17:07:48 +1000171 tmp = ((unsigned long int *)child->thread.fpr)
172 [FPRINDEX(numReg)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 } else { /* register within PT_REGS struct */
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000174 tmp = ptrace_get_reg(child, numReg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176 reg32bits = ((u32*)&tmp)[part];
177 ret = put_user(reg32bits, (u32 __user *)data);
178 break;
179 }
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /*
182 * Write 4 bytes into the other process' storage
183 * data is the 4 bytes that the user wants written
184 * addr is a pointer in the user's storage that contains an
185 * 8 byte address in the other process where the 4 bytes
186 * that is to be written
187 * (this is run in a 32-bit process looking at a 64-bit process)
188 * when I and D space are separate, these will need to be fixed.
189 */
190 case PPC_PTRACE_POKETEXT_3264:
191 case PPC_PTRACE_POKEDATA_3264: {
192 u32 tmp = data;
193 u32 __user * addrOthers;
194
195 /* Get the addr in the other process that we want to write into */
196 ret = -EIO;
197 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
198 break;
199 ret = 0;
200 if (access_process_vm(child, (u64)addrOthers, &tmp,
201 sizeof(tmp), 1) == sizeof(tmp))
202 break;
203 ret = -EIO;
204 break;
205 }
206
207 /* write the word at location addr in the USER area */
208 case PTRACE_POKEUSR: {
209 unsigned long index;
210
211 ret = -EIO;
212 /* convert to index and check */
213 index = (unsigned long) addr >> 2;
214 if ((addr & 3) || (index > PT_FPSCR32))
215 break;
216
Roland McGrathfabca2c2007-09-25 09:50:52 +1000217 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000219 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 } else {
221 flush_fp_to_thread(child);
222 /*
223 * the user space code considers the floating point
224 * to be an array of unsigned int (32 bits) - the
225 * index passed in is based on this assumption.
226 */
Michael Neuling9c75a312008-06-26 17:07:48 +1000227 ((unsigned int *)child->thread.fpr)
228 [FPRINDEX(index)] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 ret = 0;
230 }
231 break;
232 }
233
234 /*
235 * Write 4 bytes into the other process' pt_regs area
236 * data is the 4 bytes that the user wants written
237 * addr is the offset into the other process' pt_regs structure
238 * that is to be written into
239 * (this is run in a 32-bit process looking at a 64-bit process)
240 */
241 case PPC_PTRACE_POKEUSR_3264: {
242 u32 index;
243 u32 numReg;
244
245 ret = -EIO;
246 /* Determine which register the user wants */
247 index = (u64)addr >> 2;
248 numReg = index / 2;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /*
251 * Validate the input - check to see if address is on the
252 * wrong boundary or beyond the end of the user area
253 */
254 if ((addr & 3) || (numReg > PT_FPSCR))
255 break;
Roland McGrathfabca2c2007-09-25 09:50:52 +1000256 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000257 if (numReg < PT_FPR0) {
258 unsigned long freg = ptrace_get_reg(child, numReg);
259 if (index % 2)
260 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
261 else
262 freg = (freg & 0xfffffffful) | (data << 32);
263 ret = ptrace_put_reg(child, numReg, freg);
264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 flush_fp_to_thread(child);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000266 ((unsigned int *)child->thread.regs)[index] = data;
267 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 break;
270 }
271
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000272 case PTRACE_GET_DEBUGREG: {
273 ret = -EINVAL;
274 /* We only support one DABR and no IABRS at the moment */
275 if (addr > 0)
276 break;
277 ret = put_user(child->thread.dabr, (u32 __user *)data);
278 break;
279 }
280
Roland McGrath0deef2c2007-12-20 03:59:04 -0800281 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
282 return copy_regset_to_user(
283 child, task_user_regset_view(current), 0,
284 0, PT_REGS_COUNT * sizeof(compat_long_t),
285 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000286
Roland McGrath0deef2c2007-12-20 03:59:04 -0800287 case PTRACE_SETREGS: /* Set all gp regs in the child. */
288 return copy_regset_from_user(
289 child, task_user_regset_view(current), 0,
290 0, PT_REGS_COUNT * sizeof(compat_long_t),
291 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000292
293 case PTRACE_GETFPREGS:
294 case PTRACE_SETFPREGS:
Robert Jennings962bca72005-09-10 16:01:07 +1000295 case PTRACE_GETVRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000296 case PTRACE_SETVRREGS:
Michael Neuling33b3f032008-07-29 01:13:14 +1000297 case PTRACE_GETVSRREGS:
298 case PTRACE_SETVSRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000299 case PTRACE_GETREGS64:
300 case PTRACE_SETREGS64:
301 case PPC_PTRACE_GETFPREGS:
302 case PPC_PTRACE_SETFPREGS:
303 case PTRACE_KILL:
304 case PTRACE_SINGLESTEP:
305 case PTRACE_DETACH:
306 case PTRACE_SET_DEBUGREG:
307 case PTRACE_SYSCALL:
308 case PTRACE_CONT:
309 ret = arch_ptrace(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000310 break;
311
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000312 /* Old reverse args ptrace callss */
313 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
314 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
315 ret = compat_ptrace_old(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000316 break;
Robert Jennings962bca72005-09-10 16:01:07 +1000317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 default:
Roland McGrath1d48d712007-12-20 03:58:49 -0800319 ret = compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 break;
321 }
Roland McGrath81e695c2007-12-20 03:58:55 -0800322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return ret;
324}