blob: f37eb53de1a1f1aa152b70611445cd05cd18278b [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
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080032#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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)
Michael Neuling9c75a312008-06-26 17:07:48 +100046
Roland McGrath81e695c2007-12-20 03:58:55 -080047long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
48 compat_ulong_t caddr, compat_ulong_t cdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Roland McGrath81e695c2007-12-20 03:58:55 -080050 unsigned long addr = caddr;
51 unsigned long data = cdata;
Christoph Hellwig6b9c7ed82006-01-08 01:02:33 -080052 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 /*
56 * Read 4 bytes of the other process' storage
57 * data is a pointer specifying where the user wants the
58 * 4 bytes copied into
59 * addr is a pointer in the user's storage that contains an 8 byte
60 * address in the other process of the 4 bytes that is to be read
61 * (this is run in a 32-bit process looking at a 64-bit process)
62 * when I and D space are separate, these will need to be fixed.
63 */
64 case PPC_PTRACE_PEEKTEXT_3264:
65 case PPC_PTRACE_PEEKDATA_3264: {
66 u32 tmp;
67 int copied;
68 u32 __user * addrOthers;
69
70 ret = -EIO;
71
72 /* Get the addr in the other process that we want to read */
73 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
74 break;
75
Eric W. Biederman84d77d32016-11-22 12:06:50 -060076 copied = ptrace_access_vm(child, (u64)addrOthers, &tmp,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +010077 sizeof(tmp), FOLL_FORCE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (copied != sizeof(tmp))
79 break;
80 ret = put_user(tmp, (u32 __user *)data);
81 break;
82 }
83
84 /* Read a register (specified by ADDR) out of the "user area" */
85 case PTRACE_PEEKUSR: {
86 int index;
87 unsigned long tmp;
88
89 ret = -EIO;
90 /* convert to index and check */
91 index = (unsigned long) addr >> 2;
92 if ((addr & 3) || (index > PT_FPSCR32))
93 break;
94
Roland McGrathfabca2c2007-09-25 09:50:52 +100095 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (index < PT_FPR0) {
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +000097 ret = ptrace_get_reg(child, index, &tmp);
98 if (ret)
99 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 } else {
101 flush_fp_to_thread(child);
102 /*
103 * the user space code considers the floating point
104 * to be an array of unsigned int (32 bits) - the
105 * index passed in is based on this assumption.
106 */
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000107 tmp = ((unsigned int *)child->thread.fp_state.fpr)
Michael Neuling9c75a312008-06-26 17:07:48 +1000108 [FPRINDEX(index)];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
110 ret = put_user((unsigned int)tmp, (u32 __user *)data);
111 break;
112 }
113
114 /*
115 * Read 4 bytes out of the other process' pt_regs area
116 * data is a pointer specifying where the user wants the
117 * 4 bytes copied into
118 * addr is the offset into the other process' pt_regs structure
119 * that is to be read
120 * (this is run in a 32-bit process looking at a 64-bit process)
121 */
122 case PPC_PTRACE_PEEKUSR_3264: {
123 u32 index;
124 u32 reg32bits;
125 u64 tmp;
126 u32 numReg;
127 u32 part;
128
129 ret = -EIO;
130 /* Determine which register the user wants */
131 index = (u64)addr >> 2;
132 numReg = index / 2;
133 /* Determine which part of the register the user wants */
134 if (index % 2)
135 part = 1; /* want the 2nd half of the register (right-most). */
136 else
137 part = 0; /* want the 1st half of the register (left-most). */
138
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000139 /* Validate the input - check to see if address is on the wrong boundary
140 * or beyond the end of the user area
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if ((addr & 3) || numReg > PT_FPSCR)
143 break;
144
Roland McGrathfabca2c2007-09-25 09:50:52 +1000145 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (numReg >= PT_FPR0) {
147 flush_fp_to_thread(child);
Michael Neulingbc826662009-04-05 20:59:39 +0000148 /* get 64 bit FPR */
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000149 tmp = child->thread.fp_state.fpr[numReg - PT_FPR0][0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 } else { /* register within PT_REGS struct */
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000151 unsigned long tmp2;
152 ret = ptrace_get_reg(child, numReg, &tmp2);
153 if (ret)
154 break;
155 tmp = tmp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 reg32bits = ((u32*)&tmp)[part];
158 ret = put_user(reg32bits, (u32 __user *)data);
159 break;
160 }
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 /*
163 * Write 4 bytes into the other process' storage
164 * data is the 4 bytes that the user wants written
165 * addr is a pointer in the user's storage that contains an
166 * 8 byte address in the other process where the 4 bytes
167 * that is to be written
168 * (this is run in a 32-bit process looking at a 64-bit process)
169 * when I and D space are separate, these will need to be fixed.
170 */
171 case PPC_PTRACE_POKETEXT_3264:
172 case PPC_PTRACE_POKEDATA_3264: {
173 u32 tmp = data;
174 u32 __user * addrOthers;
175
176 /* Get the addr in the other process that we want to write into */
177 ret = -EIO;
178 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
179 break;
180 ret = 0;
Eric W. Biederman84d77d32016-11-22 12:06:50 -0600181 if (ptrace_access_vm(child, (u64)addrOthers, &tmp,
Lorenzo Stoakesf307ab62016-10-13 01:20:20 +0100182 sizeof(tmp),
183 FOLL_FORCE | FOLL_WRITE) == sizeof(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 ret = -EIO;
186 break;
187 }
188
189 /* write the word at location addr in the USER area */
190 case PTRACE_POKEUSR: {
191 unsigned long index;
192
193 ret = -EIO;
194 /* convert to index and check */
195 index = (unsigned long) addr >> 2;
196 if ((addr & 3) || (index > PT_FPSCR32))
197 break;
198
Roland McGrathfabca2c2007-09-25 09:50:52 +1000199 CHECK_FULL_REGS(child->thread.regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (index < PT_FPR0) {
Benjamin Herrenschmidt865418d2007-06-04 15:15:44 +1000201 ret = ptrace_put_reg(child, index, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 } else {
203 flush_fp_to_thread(child);
204 /*
205 * the user space code considers the floating point
206 * to be an array of unsigned int (32 bits) - the
207 * index passed in is based on this assumption.
208 */
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000209 ((unsigned int *)child->thread.fp_state.fpr)
Michael Neuling9c75a312008-06-26 17:07:48 +1000210 [FPRINDEX(index)] = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 ret = 0;
212 }
213 break;
214 }
215
216 /*
217 * Write 4 bytes into the other process' pt_regs area
218 * data is the 4 bytes that the user wants written
219 * addr is the offset into the other process' pt_regs structure
220 * that is to be written into
221 * (this is run in a 32-bit process looking at a 64-bit process)
222 */
223 case PPC_PTRACE_POKEUSR_3264: {
224 u32 index;
225 u32 numReg;
226
227 ret = -EIO;
228 /* Determine which register the user wants */
229 index = (u64)addr >> 2;
230 numReg = index / 2;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /*
233 * Validate the input - check to see if address is on the
234 * wrong boundary or beyond the end of the user area
235 */
236 if ((addr & 3) || (numReg > PT_FPSCR))
237 break;
Roland McGrathfabca2c2007-09-25 09:50:52 +1000238 CHECK_FULL_REGS(child->thread.regs);
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000239 if (numReg < PT_FPR0) {
Alexey Kardashevskiyee4a3912013-02-14 17:44:23 +0000240 unsigned long freg;
241 ret = ptrace_get_reg(child, numReg, &freg);
242 if (ret)
243 break;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000244 if (index % 2)
245 freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
246 else
247 freg = (freg & 0xfffffffful) | (data << 32);
248 ret = ptrace_put_reg(child, numReg, freg);
249 } else {
Michael Neulingbc826662009-04-05 20:59:39 +0000250 u64 *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 flush_fp_to_thread(child);
Michael Neulingbc826662009-04-05 20:59:39 +0000252 /* get 64 bit FPR ... */
Paul Mackerrasde79f7b2013-09-10 20:20:42 +1000253 tmp = &child->thread.fp_state.fpr[numReg - PT_FPR0][0];
Michael Neulingbc826662009-04-05 20:59:39 +0000254 /* ... write the 32 bit part we want */
255 ((u32 *)tmp)[index % 2] = data;
Benjamin Herrenschmidt912000e2007-06-04 15:15:46 +1000256 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 break;
259 }
260
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000261 case PTRACE_GET_DEBUGREG: {
Michael Neuling9422de32012-12-20 14:06:44 +0000262#ifndef CONFIG_PPC_ADV_DEBUG_REGS
263 unsigned long dabr_fake;
264#endif
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000265 ret = -EINVAL;
266 /* We only support one DABR and no IABRS at the moment */
267 if (addr > 0)
268 break;
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000269#ifdef CONFIG_PPC_ADV_DEBUG_REGS
Bharat Bhushan51ae8d42013-07-04 11:45:46 +0530270 ret = put_user(child->thread.debug.dac1, (u32 __user *)data);
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000271#else
Michael Neuling9422de32012-12-20 14:06:44 +0000272 dabr_fake = (
273 (child->thread.hw_brk.address & (~HW_BRK_TYPE_DABR)) |
274 (child->thread.hw_brk.type & HW_BRK_TYPE_DABR));
275 ret = put_user(dabr_fake, (u32 __user *)data);
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000276#endif
Anton Blanchardfd9648d2005-09-10 16:01:11 +1000277 break;
278 }
279
Roland McGrath0deef2c2007-12-20 03:59:04 -0800280 case PTRACE_GETREGS: /* Get all pt_regs from the child. */
281 return copy_regset_to_user(
282 child, task_user_regset_view(current), 0,
283 0, PT_REGS_COUNT * sizeof(compat_long_t),
284 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000285
Roland McGrath0deef2c2007-12-20 03:59:04 -0800286 case PTRACE_SETREGS: /* Set all gp regs in the child. */
287 return copy_regset_from_user(
288 child, task_user_regset_view(current), 0,
289 0, PT_REGS_COUNT * sizeof(compat_long_t),
290 compat_ptr(data));
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000291
292 case PTRACE_GETFPREGS:
293 case PTRACE_SETFPREGS:
Robert Jennings962bca72005-09-10 16:01:07 +1000294 case PTRACE_GETVRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000295 case PTRACE_SETVRREGS:
Michael Neuling33b3f032008-07-29 01:13:14 +1000296 case PTRACE_GETVSRREGS:
297 case PTRACE_SETVSRREGS:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000298 case PTRACE_GETREGS64:
299 case PTRACE_SETREGS64:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000300 case PTRACE_KILL:
301 case PTRACE_SINGLESTEP:
302 case PTRACE_DETACH:
303 case PTRACE_SET_DEBUGREG:
304 case PTRACE_SYSCALL:
305 case PTRACE_CONT:
Andreas Schwabbb2c458b2010-11-28 06:33:14 +0000306 case PPC_PTRACE_GETHWDBGINFO:
307 case PPC_PTRACE_SETHWDEBUG:
308 case PPC_PTRACE_DELHWDEBUG:
Benjamin Herrenschmidte17666b2007-06-04 15:15:43 +1000309 ret = arch_ptrace(child, request, addr, data);
Robert Jennings962bca72005-09-10 16:01:07 +1000310 break;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 default:
Roland McGrath1d48d712007-12-20 03:58:49 -0800313 ret = compat_ptrace_request(child, request, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315 }
Roland McGrath81e695c2007-12-20 03:58:55 -0800316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return ret;
318}