blob: 6612304e11e3a43da1aa2aff5f266d8a01dbcdcb [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>
27#include <linux/user.h>
28#include <linux/security.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Roland McGrath1d48d712007-12-20 03:58:49 -080030#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/uaccess.h>
33#include <asm/page.h>
34#include <asm/pgtable.h>
35#include <asm/system.h>
Paul Mackerras21a62902005-11-19 20:47:22 +110036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/*
38 * does not yet catch signals sent when the child dies.
39 * in exit.c or in signal.c.
40 */
41
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100042/*
43 * Here are the old "legacy" powerpc specific getregs/setregs ptrace calls,
44 * we mark them as obsolete now, they will be removed in a future version
45 */
46static long compat_ptrace_old(struct task_struct *child, long request,
47 long addr, long data)
48{
49 int ret = -EPERM;
50
51 switch(request) {
52 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
53 int i;
54 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
55 unsigned int __user *tmp = (unsigned int __user *)addr;
56
Roland McGrathfabca2c2007-09-25 09:50:52 +100057 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100058 for (i = 0; i < 32; i++) {
59 ret = put_user(*reg, tmp);
60 if (ret)
61 break;
62 reg++;
63 tmp++;
64 }
65 break;
66 }
67
68 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
69 int i;
70 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
71 unsigned int __user *tmp = (unsigned int __user *)addr;
72
Roland McGrathfabca2c2007-09-25 09:50:52 +100073 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +100074 for (i = 0; i < 32; i++) {
75 ret = get_user(*reg, tmp);
76 if (ret)
77 break;
78 reg++;
79 tmp++;
80 }
81 break;
82 }
83
84 }
85 return ret;
86}
87
Paul Mackerras734d6522005-10-31 13:57:01 +110088long compat_sys_ptrace(int request, int pid, unsigned long addr,
89 unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
91 struct task_struct *child;
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -080092 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 lock_kernel();
95 if (request == PTRACE_TRACEME) {
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -080096 ret = ptrace_traceme();
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 goto out;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Christoph Hellwig6b9c7ed2006-01-08 01:02:33 -0800100 child = ptrace_get_task_struct(pid);
101 if (IS_ERR(child)) {
102 ret = PTR_ERR(child);
103 goto out;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 if (request == PTRACE_ATTACH) {
107 ret = ptrace_attach(child);
108 goto out_tsk;
109 }
110
111 ret = ptrace_check_attach(child, request == PTRACE_KILL);
112 if (ret < 0)
113 goto out_tsk;
114
115 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 /*
117 * Read 4 bytes of the other process' storage
118 * data is a pointer specifying where the user wants the
119 * 4 bytes copied into
120 * addr is a pointer in the user's storage that contains an 8 byte
121 * address in the other process of the 4 bytes that is to be read
122 * (this is run in a 32-bit process looking at a 64-bit process)
123 * when I and D space are separate, these will need to be fixed.
124 */
125 case PPC_PTRACE_PEEKTEXT_3264:
126 case PPC_PTRACE_PEEKDATA_3264: {
127 u32 tmp;
128 int copied;
129 u32 __user * addrOthers;
130
131 ret = -EIO;
132
133 /* Get the addr in the other process that we want to read */
134 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
135 break;
136
137 copied = access_process_vm(child, (u64)addrOthers, &tmp,
138 sizeof(tmp), 0);
139 if (copied != sizeof(tmp))
140 break;
141 ret = put_user(tmp, (u32 __user *)data);
142 break;
143 }
144
145 /* Read a register (specified by ADDR) out of the "user area" */
146 case PTRACE_PEEKUSR: {
147 int index;
148 unsigned long tmp;
149
150 ret = -EIO;
151 /* convert to index and check */
152 index = (unsigned long) addr >> 2;
153 if ((addr & 3) || (index > PT_FPSCR32))
154 break;
155
Roland McGrathfabca2c2007-09-25 09:50:52 +1000156 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000158 tmp = ptrace_get_reg(child, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 } else {
160 flush_fp_to_thread(child);
161 /*
162 * the user space code considers the floating point
163 * to be an array of unsigned int (32 bits) - the
164 * index passed in is based on this assumption.
165 */
166 tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
167 }
168 ret = put_user((unsigned int)tmp, (u32 __user *)data);
169 break;
170 }
171
172 /*
173 * Read 4 bytes out of the other process' pt_regs area
174 * data is a pointer specifying where the user wants the
175 * 4 bytes copied into
176 * addr is the offset into the other process' pt_regs structure
177 * that is to be read
178 * (this is run in a 32-bit process looking at a 64-bit process)
179 */
180 case PPC_PTRACE_PEEKUSR_3264: {
181 u32 index;
182 u32 reg32bits;
183 u64 tmp;
184 u32 numReg;
185 u32 part;
186
187 ret = -EIO;
188 /* Determine which register the user wants */
189 index = (u64)addr >> 2;
190 numReg = index / 2;
191 /* Determine which part of the register the user wants */
192 if (index % 2)
193 part = 1; /* want the 2nd half of the register (right-most). */
194 else
195 part = 0; /* want the 1st half of the register (left-most). */
196
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000197 /* Validate the input - check to see if address is on the wrong boundary
198 * or beyond the end of the user area
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if ((addr & 3) || numReg > PT_FPSCR)
201 break;
202
Roland McGrathfabca2c2007-09-25 09:50:52 +1000203 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (numReg >= PT_FPR0) {
205 flush_fp_to_thread(child);
206 tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
207 } else { /* register within PT_REGS struct */
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000208 tmp = ptrace_get_reg(child, numReg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210 reg32bits = ((u32*)&tmp)[part];
211 ret = put_user(reg32bits, (u32 __user *)data);
212 break;
213 }
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /*
216 * Write 4 bytes into the other process' storage
217 * data is the 4 bytes that the user wants written
218 * addr is a pointer in the user's storage that contains an
219 * 8 byte address in the other process where the 4 bytes
220 * that is to be written
221 * (this is run in a 32-bit process looking at a 64-bit process)
222 * when I and D space are separate, these will need to be fixed.
223 */
224 case PPC_PTRACE_POKETEXT_3264:
225 case PPC_PTRACE_POKEDATA_3264: {
226 u32 tmp = data;
227 u32 __user * addrOthers;
228
229 /* Get the addr in the other process that we want to write into */
230 ret = -EIO;
231 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
232 break;
233 ret = 0;
234 if (access_process_vm(child, (u64)addrOthers, &tmp,
235 sizeof(tmp), 1) == sizeof(tmp))
236 break;
237 ret = -EIO;
238 break;
239 }
240
241 /* write the word at location addr in the USER area */
242 case PTRACE_POKEUSR: {
243 unsigned long index;
244
245 ret = -EIO;
246 /* convert to index and check */
247 index = (unsigned long) addr >> 2;
248 if ((addr & 3) || (index > PT_FPSCR32))
249 break;
250
Roland McGrathfabca2c2007-09-25 09:50:52 +1000251 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000253 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else {
255 flush_fp_to_thread(child);
256 /*
257 * the user space code considers the floating point
258 * to be an array of unsigned int (32 bits) - the
259 * index passed in is based on this assumption.
260 */
261 ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
262 ret = 0;
263 }
264 break;
265 }
266
267 /*
268 * Write 4 bytes into the other process' pt_regs area
269 * data is the 4 bytes that the user wants written
270 * addr is the offset into the other process' pt_regs structure
271 * that is to be written into
272 * (this is run in a 32-bit process looking at a 64-bit process)
273 */
274 case PPC_PTRACE_POKEUSR_3264: {
275 u32 index;
276 u32 numReg;
277
278 ret = -EIO;
279 /* Determine which register the user wants */
280 index = (u64)addr >> 2;
281 numReg = index / 2;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 /*
284 * Validate the input - check to see if address is on the
285 * wrong boundary or beyond the end of the user area
286 */
287 if ((addr & 3) || (numReg > PT_FPSCR))
288 break;
Roland McGrathfabca2c2007-09-25 09:50:52 +1000289 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000290 if (numReg < PT_FPR0) {
291 unsigned long freg = ptrace_get_reg(child, numReg);
292 if (index % 2)
293 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
294 else
295 freg = (freg & 0xfffffffful) | (data << 32);
296 ret = ptrace_put_reg(child, numReg, freg);
297 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 flush_fp_to_thread(child);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000299 ((unsigned int *)child->thread.regs)[index] = data;
300 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 }
304
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000305 case PTRACE_GET_DEBUGREG: {
306 ret = -EINVAL;
307 /* We only support one DABR and no IABRS at the moment */
308 if (addr > 0)
309 break;
310 ret = put_user(child->thread.dabr, (u32 __user *)data);
311 break;
312 }
313
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000314 case PTRACE_GETREGS: { /* Get all pt_regs from the child. */
315 int ui;
316 if (!access_ok(VERIFY_WRITE, (void __user *)data,
317 PT_REGS_COUNT * sizeof(int))) {
318 ret = -EIO;
319 break;
320 }
Roland McGrathfabca2c2007-09-25 09:50:52 +1000321 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000322 ret = 0;
323 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000324 ret |= __put_user(ptrace_get_reg(child, ui),
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000325 (unsigned int __user *) data);
326 data += sizeof(int);
327 }
328 break;
329 }
330
331 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
332 unsigned long tmp;
333 int ui;
334 if (!access_ok(VERIFY_READ, (void __user *)data,
335 PT_REGS_COUNT * sizeof(int))) {
336 ret = -EIO;
337 break;
338 }
Roland McGrathfabca2c2007-09-25 09:50:52 +1000339 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000340 ret = 0;
341 for (ui = 0; ui < PT_REGS_COUNT; ui ++) {
342 ret = __get_user(tmp, (unsigned int __user *) data);
343 if (ret)
344 break;
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000345 ptrace_put_reg(child, ui, tmp);
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000346 data += sizeof(int);
347 }
348 break;
349 }
350
351 case PTRACE_GETFPREGS:
352 case PTRACE_SETFPREGS:
Robert Jennings962bca72005-09-10 16:01:07 +1000353 case PTRACE_GETVRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000354 case PTRACE_SETVRREGS:
355 case PTRACE_GETREGS64:
356 case PTRACE_SETREGS64:
357 case PPC_PTRACE_GETFPREGS:
358 case PPC_PTRACE_SETFPREGS:
359 case PTRACE_KILL:
360 case PTRACE_SINGLESTEP:
361 case PTRACE_DETACH:
362 case PTRACE_SET_DEBUGREG:
363 case PTRACE_SYSCALL:
364 case PTRACE_CONT:
365 ret = arch_ptrace(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000366 break;
367
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000368 /* Old reverse args ptrace callss */
369 case PPC_PTRACE_GETREGS: /* Get GPRs 0 - 31. */
370 case PPC_PTRACE_SETREGS: /* Set GPRs 0 - 31. */
371 ret = compat_ptrace_old(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000372 break;
Robert Jennings962bca72005-09-10 16:01:07 +1000373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 default:
Roland McGrath1d48d712007-12-20 03:58:49 -0800375 ret = compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 }
378out_tsk:
379 put_task_struct(child);
380out:
381 unlock_kernel();
382 return ret;
383}