blob: c0244e7668343d65f1f75d527348e44f772901df [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/ptrace.h>
Roland McGrath0deef2c2007-12-20 03:59:04 -080026#include <linux/regset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#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>
David Howellsae3a1972012-03-28 18:30:02 +010035#include <asm/switch_to.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
Michael Neuling9c75a312008-06-26 17:07:48 +100042/* Macros to workout the correct index for the FPR in the thread struct */
43#define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
44#define FPRHALF(i) (((i) - PT_FPR0) & 1)
Michael Neulingbc826662009-04-05 20:59:39 +000045#define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) * 2 + FPRHALF(i)
46#define FPRINDEX_3264(i) (TS_FPRWIDTH * ((i) - PT_FPR0))
Michael Neuling9c75a312008-06-26 17:07:48 +100047
Roland McGrath81e695c2007-12-20 03:58:55 -080048long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
49 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Roland McGrath81e695c2007-12-20 03:58:55 -080051 unsigned long addr = caddr;
52 unsigned long data = cdata;
Christoph Hellwig6b9c7ed82006-01-08 01:02:33 -080053 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /*
57 * Read 4 bytes of the other process' storage
58 * data is a pointer specifying where the user wants the
59 * 4 bytes copied into
60 * addr is a pointer in the user's storage that contains an 8 byte
61 * address in the other process of the 4 bytes that is to be read
62 * (this is run in a 32-bit process looking at a 64-bit process)
63 * when I and D space are separate, these will need to be fixed.
64 */
65 case PPC_PTRACE_PEEKTEXT_3264:
66 case PPC_PTRACE_PEEKDATA_3264: {
67 u32 tmp;
68 int copied;
69 u32 __user * addrOthers;
70
71 ret = -EIO;
72
73 /* Get the addr in the other process that we want to read */
74 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
75 break;
76
77 copied = access_process_vm(child, (u64)addrOthers, &tmp,
78 sizeof(tmp), 0);
79 if (copied != sizeof(tmp))
80 break;
81 ret = put_user(tmp, (u32 __user *)data);
82 break;
83 }
84
85 /* Read a register (specified by ADDR) out of the "user area" */
86 case PTRACE_PEEKUSR: {
87 int index;
88 unsigned long tmp;
89
90 ret = -EIO;
91 /* convert to index and check */
92 index = (unsigned long) addr >> 2;
93 if ((addr & 3) || (index > PT_FPSCR32))
94 break;
95
Roland McGrathfabca2c2007-09-25 09:50:52 +100096 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +100098 tmp = ptrace_get_reg(child, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } else {
100 flush_fp_to_thread(child);
101 /*
102 * the user space code considers the floating point
103 * to be an array of unsigned int (32 bits) - the
104 * index passed in is based on this assumption.
105 */
Michael Neuling9c75a312008-06-26 17:07:48 +1000106 tmp = ((unsigned int *)child->thread.fpr)
107 [FPRINDEX(index)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109 ret = put_user((unsigned int)tmp, (u32 __user *)data);
110 break;
111 }
112
113 /*
114 * Read 4 bytes out of the other process' pt_regs area
115 * data is a pointer specifying where the user wants the
116 * 4 bytes copied into
117 * addr is the offset into the other process' pt_regs structure
118 * that is to be read
119 * (this is run in a 32-bit process looking at a 64-bit process)
120 */
121 case PPC_PTRACE_PEEKUSR_3264: {
122 u32 index;
123 u32 reg32bits;
124 u64 tmp;
125 u32 numReg;
126 u32 part;
127
128 ret = -EIO;
129 /* Determine which register the user wants */
130 index = (u64)addr >> 2;
131 numReg = index / 2;
132 /* Determine which part of the register the user wants */
133 if (index % 2)
134 part = 1; /* want the 2nd half of the register (right-most). */
135 else
136 part = 0; /* want the 1st half of the register (left-most). */
137
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000138 /* Validate the input - check to see if address is on the wrong boundary
139 * or beyond the end of the user area
140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if ((addr & 3) || numReg > PT_FPSCR)
142 break;
143
Roland McGrathfabca2c2007-09-25 09:50:52 +1000144 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (numReg >= PT_FPR0) {
146 flush_fp_to_thread(child);
Michael Neulingbc826662009-04-05 20:59:39 +0000147 /* get 64 bit FPR */
148 tmp = ((u64 *)child->thread.fpr)
149 [FPRINDEX_3264(numReg)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } else { /* register within PT_REGS struct */
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000151 tmp = ptrace_get_reg(child, numReg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 reg32bits = ((u32*)&tmp)[part];
154 ret = put_user(reg32bits, (u32 __user *)data);
155 break;
156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /*
159 * Write 4 bytes into the other process' storage
160 * data is the 4 bytes that the user wants written
161 * addr is a pointer in the user's storage that contains an
162 * 8 byte address in the other process where the 4 bytes
163 * that is to be written
164 * (this is run in a 32-bit process looking at a 64-bit process)
165 * when I and D space are separate, these will need to be fixed.
166 */
167 case PPC_PTRACE_POKETEXT_3264:
168 case PPC_PTRACE_POKEDATA_3264: {
169 u32 tmp = data;
170 u32 __user * addrOthers;
171
172 /* Get the addr in the other process that we want to write into */
173 ret = -EIO;
174 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
175 break;
176 ret = 0;
177 if (access_process_vm(child, (u64)addrOthers, &tmp,
178 sizeof(tmp), 1) == sizeof(tmp))
179 break;
180 ret = -EIO;
181 break;
182 }
183
184 /* write the word at location addr in the USER area */
185 case PTRACE_POKEUSR: {
186 unsigned long index;
187
188 ret = -EIO;
189 /* convert to index and check */
190 index = (unsigned long) addr >> 2;
191 if ((addr & 3) || (index > PT_FPSCR32))
192 break;
193
Roland McGrathfabca2c2007-09-25 09:50:52 +1000194 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000196 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 } else {
198 flush_fp_to_thread(child);
199 /*
200 * the user space code considers the floating point
201 * to be an array of unsigned int (32 bits) - the
202 * index passed in is based on this assumption.
203 */
Michael Neuling9c75a312008-06-26 17:07:48 +1000204 ((unsigned int *)child->thread.fpr)
205 [FPRINDEX(index)] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 ret = 0;
207 }
208 break;
209 }
210
211 /*
212 * Write 4 bytes into the other process' pt_regs area
213 * data is the 4 bytes that the user wants written
214 * addr is the offset into the other process' pt_regs structure
215 * that is to be written into
216 * (this is run in a 32-bit process looking at a 64-bit process)
217 */
218 case PPC_PTRACE_POKEUSR_3264: {
219 u32 index;
220 u32 numReg;
221
222 ret = -EIO;
223 /* Determine which register the user wants */
224 index = (u64)addr >> 2;
225 numReg = index / 2;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /*
228 * Validate the input - check to see if address is on the
229 * wrong boundary or beyond the end of the user area
230 */
231 if ((addr & 3) || (numReg > PT_FPSCR))
232 break;
Roland McGrathfabca2c2007-09-25 09:50:52 +1000233 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000234 if (numReg < PT_FPR0) {
235 unsigned long freg = ptrace_get_reg(child, numReg);
236 if (index % 2)
237 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
238 else
239 freg = (freg & 0xfffffffful) | (data << 32);
240 ret = ptrace_put_reg(child, numReg, freg);
241 } else {
Michael Neulingbc826662009-04-05 20:59:39 +0000242 u64 *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 flush_fp_to_thread(child);
Michael Neulingbc826662009-04-05 20:59:39 +0000244 /* get 64 bit FPR ... */
245 tmp = &(((u64 *)child->thread.fpr)
246 [FPRINDEX_3264(numReg)]);
247 /* ... write the 32 bit part we want */
248 ((u32 *)tmp)[index % 2] = data;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000249 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 }
253
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000254 case PTRACE_GET_DEBUGREG: {
Michael Neuling9422de32012-12-20 14:06:44 +0000255#ifndef CONFIG_PPC_ADV_DEBUG_REGS
256 unsigned long dabr_fake;
257#endif
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000258 ret = -EINVAL;
259 /* We only support one DABR and no IABRS at the moment */
260 if (addr > 0)
261 break;
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000262#ifdef CONFIG_PPC_ADV_DEBUG_REGS
263 ret = put_user(child->thread.dac1, (u32 __user *)data);
264#else
Michael Neuling9422de32012-12-20 14:06:44 +0000265 dabr_fake = (
266 (child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
267 (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
268 ret = put_user(dabr_fake, (u32 __user *)data);
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000269#endif
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000270 break;
271 }
272
Roland McGrath0deef2c2007-12-20 03:59:04 -0800273 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
274 return copy_regset_to_user(
275 child, task_user_regset_view(current), 0,
276 0, PT_REGS_COUNT * sizeof(compat_long_t),
277 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000278
Roland McGrath0deef2c2007-12-20 03:59:04 -0800279 case PTRACE_SETREGS: /* Set all gp regs in the child. */
280 return copy_regset_from_user(
281 child, task_user_regset_view(current), 0,
282 0, PT_REGS_COUNT * sizeof(compat_long_t),
283 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000284
285 case PTRACE_GETFPREGS:
286 case PTRACE_SETFPREGS:
Robert Jennings962bca72005-09-10 16:01:07 +1000287 case PTRACE_GETVRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000288 case PTRACE_SETVRREGS:
Michael Neuling33b3f032008-07-29 01:13:14 +1000289 case PTRACE_GETVSRREGS:
290 case PTRACE_SETVSRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000291 case PTRACE_GETREGS64:
292 case PTRACE_SETREGS64:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000293 case PTRACE_KILL:
294 case PTRACE_SINGLESTEP:
295 case PTRACE_DETACH:
296 case PTRACE_SET_DEBUGREG:
297 case PTRACE_SYSCALL:
298 case PTRACE_CONT:
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000299 case PPC_PTRACE_GETHWDBGINFO:
300 case PPC_PTRACE_SETHWDEBUG:
301 case PPC_PTRACE_DELHWDEBUG:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000302 ret = arch_ptrace(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000303 break;
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 default:
Roland McGrath1d48d712007-12-20 03:58:49 -0800306 ret = compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
Roland McGrath81e695c2007-12-20 03:58:55 -0800309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return ret;
311}