1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * ptrace for 32-bit processes running on a 64-bit kernel.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * PowerPC version
5*4882a593Smuzhiyun * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Derived from "arch/m68k/kernel/ptrace.c"
8*4882a593Smuzhiyun * Copyright (C) 1994 by Hamish Macdonald
9*4882a593Smuzhiyun * Taken from linux/kernel/ptrace.c and modified for M680x0.
10*4882a593Smuzhiyun * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13*4882a593Smuzhiyun * and Paul Mackerras (paulus@samba.org).
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * This file is subject to the terms and conditions of the GNU General
16*4882a593Smuzhiyun * Public License. See the file COPYING in the main directory of
17*4882a593Smuzhiyun * this archive for more details.
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include <linux/ptrace.h>
21*4882a593Smuzhiyun #include <linux/regset.h>
22*4882a593Smuzhiyun #include <linux/compat.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <asm/switch_to.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /*
27*4882a593Smuzhiyun * does not yet catch signals sent when the child dies.
28*4882a593Smuzhiyun * in exit.c or in signal.c.
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Macros to workout the correct index for the FPR in the thread struct */
32*4882a593Smuzhiyun #define FPRNUMBER(i) (((i) - PT_FPR0) >> 1)
33*4882a593Smuzhiyun #define FPRHALF(i) (((i) - PT_FPR0) & 1)
34*4882a593Smuzhiyun #define FPRINDEX(i) TS_FPRWIDTH * FPRNUMBER(i) * 2 + FPRHALF(i)
35*4882a593Smuzhiyun
compat_arch_ptrace(struct task_struct * child,compat_long_t request,compat_ulong_t caddr,compat_ulong_t cdata)36*4882a593Smuzhiyun long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
37*4882a593Smuzhiyun compat_ulong_t caddr, compat_ulong_t cdata)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun unsigned long addr = caddr;
40*4882a593Smuzhiyun unsigned long data = cdata;
41*4882a593Smuzhiyun int ret;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun switch (request) {
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun * Read 4 bytes of the other process' storage
46*4882a593Smuzhiyun * data is a pointer specifying where the user wants the
47*4882a593Smuzhiyun * 4 bytes copied into
48*4882a593Smuzhiyun * addr is a pointer in the user's storage that contains an 8 byte
49*4882a593Smuzhiyun * address in the other process of the 4 bytes that is to be read
50*4882a593Smuzhiyun * (this is run in a 32-bit process looking at a 64-bit process)
51*4882a593Smuzhiyun * when I and D space are separate, these will need to be fixed.
52*4882a593Smuzhiyun */
53*4882a593Smuzhiyun case PPC_PTRACE_PEEKTEXT_3264:
54*4882a593Smuzhiyun case PPC_PTRACE_PEEKDATA_3264: {
55*4882a593Smuzhiyun u32 tmp;
56*4882a593Smuzhiyun int copied;
57*4882a593Smuzhiyun u32 __user * addrOthers;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun ret = -EIO;
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /* Get the addr in the other process that we want to read */
62*4882a593Smuzhiyun if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
63*4882a593Smuzhiyun break;
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun copied = ptrace_access_vm(child, (u64)addrOthers, &tmp,
66*4882a593Smuzhiyun sizeof(tmp), FOLL_FORCE);
67*4882a593Smuzhiyun if (copied != sizeof(tmp))
68*4882a593Smuzhiyun break;
69*4882a593Smuzhiyun ret = put_user(tmp, (u32 __user *)data);
70*4882a593Smuzhiyun break;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Read a register (specified by ADDR) out of the "user area" */
74*4882a593Smuzhiyun case PTRACE_PEEKUSR: {
75*4882a593Smuzhiyun int index;
76*4882a593Smuzhiyun unsigned long tmp;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun ret = -EIO;
79*4882a593Smuzhiyun /* convert to index and check */
80*4882a593Smuzhiyun index = (unsigned long) addr >> 2;
81*4882a593Smuzhiyun if ((addr & 3) || (index > PT_FPSCR32))
82*4882a593Smuzhiyun break;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun CHECK_FULL_REGS(child->thread.regs);
85*4882a593Smuzhiyun if (index < PT_FPR0) {
86*4882a593Smuzhiyun ret = ptrace_get_reg(child, index, &tmp);
87*4882a593Smuzhiyun if (ret)
88*4882a593Smuzhiyun break;
89*4882a593Smuzhiyun } else {
90*4882a593Smuzhiyun flush_fp_to_thread(child);
91*4882a593Smuzhiyun /*
92*4882a593Smuzhiyun * the user space code considers the floating point
93*4882a593Smuzhiyun * to be an array of unsigned int (32 bits) - the
94*4882a593Smuzhiyun * index passed in is based on this assumption.
95*4882a593Smuzhiyun */
96*4882a593Smuzhiyun tmp = ((unsigned int *)child->thread.fp_state.fpr)
97*4882a593Smuzhiyun [FPRINDEX(index)];
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun ret = put_user((unsigned int)tmp, (u32 __user *)data);
100*4882a593Smuzhiyun break;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * Read 4 bytes out of the other process' pt_regs area
105*4882a593Smuzhiyun * data is a pointer specifying where the user wants the
106*4882a593Smuzhiyun * 4 bytes copied into
107*4882a593Smuzhiyun * addr is the offset into the other process' pt_regs structure
108*4882a593Smuzhiyun * that is to be read
109*4882a593Smuzhiyun * (this is run in a 32-bit process looking at a 64-bit process)
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun case PPC_PTRACE_PEEKUSR_3264: {
112*4882a593Smuzhiyun u32 index;
113*4882a593Smuzhiyun u32 reg32bits;
114*4882a593Smuzhiyun u64 tmp;
115*4882a593Smuzhiyun u32 numReg;
116*4882a593Smuzhiyun u32 part;
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun ret = -EIO;
119*4882a593Smuzhiyun /* Determine which register the user wants */
120*4882a593Smuzhiyun index = (u64)addr >> 2;
121*4882a593Smuzhiyun numReg = index / 2;
122*4882a593Smuzhiyun /* Determine which part of the register the user wants */
123*4882a593Smuzhiyun if (index % 2)
124*4882a593Smuzhiyun part = 1; /* want the 2nd half of the register (right-most). */
125*4882a593Smuzhiyun else
126*4882a593Smuzhiyun part = 0; /* want the 1st half of the register (left-most). */
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /* Validate the input - check to see if address is on the wrong boundary
129*4882a593Smuzhiyun * or beyond the end of the user area
130*4882a593Smuzhiyun */
131*4882a593Smuzhiyun if ((addr & 3) || numReg > PT_FPSCR)
132*4882a593Smuzhiyun break;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun CHECK_FULL_REGS(child->thread.regs);
135*4882a593Smuzhiyun if (numReg >= PT_FPR0) {
136*4882a593Smuzhiyun flush_fp_to_thread(child);
137*4882a593Smuzhiyun /* get 64 bit FPR */
138*4882a593Smuzhiyun tmp = child->thread.fp_state.fpr[numReg - PT_FPR0][0];
139*4882a593Smuzhiyun } else { /* register within PT_REGS struct */
140*4882a593Smuzhiyun unsigned long tmp2;
141*4882a593Smuzhiyun ret = ptrace_get_reg(child, numReg, &tmp2);
142*4882a593Smuzhiyun if (ret)
143*4882a593Smuzhiyun break;
144*4882a593Smuzhiyun tmp = tmp2;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun reg32bits = ((u32*)&tmp)[part];
147*4882a593Smuzhiyun ret = put_user(reg32bits, (u32 __user *)data);
148*4882a593Smuzhiyun break;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Write 4 bytes into the other process' storage
153*4882a593Smuzhiyun * data is the 4 bytes that the user wants written
154*4882a593Smuzhiyun * addr is a pointer in the user's storage that contains an
155*4882a593Smuzhiyun * 8 byte address in the other process where the 4 bytes
156*4882a593Smuzhiyun * that is to be written
157*4882a593Smuzhiyun * (this is run in a 32-bit process looking at a 64-bit process)
158*4882a593Smuzhiyun * when I and D space are separate, these will need to be fixed.
159*4882a593Smuzhiyun */
160*4882a593Smuzhiyun case PPC_PTRACE_POKETEXT_3264:
161*4882a593Smuzhiyun case PPC_PTRACE_POKEDATA_3264: {
162*4882a593Smuzhiyun u32 tmp = data;
163*4882a593Smuzhiyun u32 __user * addrOthers;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /* Get the addr in the other process that we want to write into */
166*4882a593Smuzhiyun ret = -EIO;
167*4882a593Smuzhiyun if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
168*4882a593Smuzhiyun break;
169*4882a593Smuzhiyun ret = 0;
170*4882a593Smuzhiyun if (ptrace_access_vm(child, (u64)addrOthers, &tmp,
171*4882a593Smuzhiyun sizeof(tmp),
172*4882a593Smuzhiyun FOLL_FORCE | FOLL_WRITE) == sizeof(tmp))
173*4882a593Smuzhiyun break;
174*4882a593Smuzhiyun ret = -EIO;
175*4882a593Smuzhiyun break;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun /* write the word at location addr in the USER area */
179*4882a593Smuzhiyun case PTRACE_POKEUSR: {
180*4882a593Smuzhiyun unsigned long index;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun ret = -EIO;
183*4882a593Smuzhiyun /* convert to index and check */
184*4882a593Smuzhiyun index = (unsigned long) addr >> 2;
185*4882a593Smuzhiyun if ((addr & 3) || (index > PT_FPSCR32))
186*4882a593Smuzhiyun break;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun CHECK_FULL_REGS(child->thread.regs);
189*4882a593Smuzhiyun if (index < PT_FPR0) {
190*4882a593Smuzhiyun ret = ptrace_put_reg(child, index, data);
191*4882a593Smuzhiyun } else {
192*4882a593Smuzhiyun flush_fp_to_thread(child);
193*4882a593Smuzhiyun /*
194*4882a593Smuzhiyun * the user space code considers the floating point
195*4882a593Smuzhiyun * to be an array of unsigned int (32 bits) - the
196*4882a593Smuzhiyun * index passed in is based on this assumption.
197*4882a593Smuzhiyun */
198*4882a593Smuzhiyun ((unsigned int *)child->thread.fp_state.fpr)
199*4882a593Smuzhiyun [FPRINDEX(index)] = data;
200*4882a593Smuzhiyun ret = 0;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun break;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun * Write 4 bytes into the other process' pt_regs area
207*4882a593Smuzhiyun * data is the 4 bytes that the user wants written
208*4882a593Smuzhiyun * addr is the offset into the other process' pt_regs structure
209*4882a593Smuzhiyun * that is to be written into
210*4882a593Smuzhiyun * (this is run in a 32-bit process looking at a 64-bit process)
211*4882a593Smuzhiyun */
212*4882a593Smuzhiyun case PPC_PTRACE_POKEUSR_3264: {
213*4882a593Smuzhiyun u32 index;
214*4882a593Smuzhiyun u32 numReg;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun ret = -EIO;
217*4882a593Smuzhiyun /* Determine which register the user wants */
218*4882a593Smuzhiyun index = (u64)addr >> 2;
219*4882a593Smuzhiyun numReg = index / 2;
220*4882a593Smuzhiyun
221*4882a593Smuzhiyun /*
222*4882a593Smuzhiyun * Validate the input - check to see if address is on the
223*4882a593Smuzhiyun * wrong boundary or beyond the end of the user area
224*4882a593Smuzhiyun */
225*4882a593Smuzhiyun if ((addr & 3) || (numReg > PT_FPSCR))
226*4882a593Smuzhiyun break;
227*4882a593Smuzhiyun CHECK_FULL_REGS(child->thread.regs);
228*4882a593Smuzhiyun if (numReg < PT_FPR0) {
229*4882a593Smuzhiyun unsigned long freg;
230*4882a593Smuzhiyun ret = ptrace_get_reg(child, numReg, &freg);
231*4882a593Smuzhiyun if (ret)
232*4882a593Smuzhiyun break;
233*4882a593Smuzhiyun if (index % 2)
234*4882a593Smuzhiyun freg = (freg & ~0xfffffffful) | (data & 0xfffffffful);
235*4882a593Smuzhiyun else
236*4882a593Smuzhiyun freg = (freg & 0xfffffffful) | (data << 32);
237*4882a593Smuzhiyun ret = ptrace_put_reg(child, numReg, freg);
238*4882a593Smuzhiyun } else {
239*4882a593Smuzhiyun u64 *tmp;
240*4882a593Smuzhiyun flush_fp_to_thread(child);
241*4882a593Smuzhiyun /* get 64 bit FPR ... */
242*4882a593Smuzhiyun tmp = &child->thread.fp_state.fpr[numReg - PT_FPR0][0];
243*4882a593Smuzhiyun /* ... write the 32 bit part we want */
244*4882a593Smuzhiyun ((u32 *)tmp)[index % 2] = data;
245*4882a593Smuzhiyun ret = 0;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun break;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun case PTRACE_GET_DEBUGREG: {
251*4882a593Smuzhiyun #ifndef CONFIG_PPC_ADV_DEBUG_REGS
252*4882a593Smuzhiyun unsigned long dabr_fake;
253*4882a593Smuzhiyun #endif
254*4882a593Smuzhiyun ret = -EINVAL;
255*4882a593Smuzhiyun /* We only support one DABR and no IABRS at the moment */
256*4882a593Smuzhiyun if (addr > 0)
257*4882a593Smuzhiyun break;
258*4882a593Smuzhiyun #ifdef CONFIG_PPC_ADV_DEBUG_REGS
259*4882a593Smuzhiyun ret = put_user(child->thread.debug.dac1, (u32 __user *)data);
260*4882a593Smuzhiyun #else
261*4882a593Smuzhiyun dabr_fake = (
262*4882a593Smuzhiyun (child->thread.hw_brk[0].address & (~HW_BRK_TYPE_DABR)) |
263*4882a593Smuzhiyun (child->thread.hw_brk[0].type & HW_BRK_TYPE_DABR));
264*4882a593Smuzhiyun ret = put_user(dabr_fake, (u32 __user *)data);
265*4882a593Smuzhiyun #endif
266*4882a593Smuzhiyun break;
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun case PTRACE_GETREGS: /* Get all pt_regs from the child. */
270*4882a593Smuzhiyun return copy_regset_to_user(
271*4882a593Smuzhiyun child, task_user_regset_view(current), 0,
272*4882a593Smuzhiyun 0, PT_REGS_COUNT * sizeof(compat_long_t),
273*4882a593Smuzhiyun compat_ptr(data));
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun case PTRACE_SETREGS: /* Set all gp regs in the child. */
276*4882a593Smuzhiyun return copy_regset_from_user(
277*4882a593Smuzhiyun child, task_user_regset_view(current), 0,
278*4882a593Smuzhiyun 0, PT_REGS_COUNT * sizeof(compat_long_t),
279*4882a593Smuzhiyun compat_ptr(data));
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun case PTRACE_GETFPREGS:
282*4882a593Smuzhiyun case PTRACE_SETFPREGS:
283*4882a593Smuzhiyun case PTRACE_GETVRREGS:
284*4882a593Smuzhiyun case PTRACE_SETVRREGS:
285*4882a593Smuzhiyun case PTRACE_GETVSRREGS:
286*4882a593Smuzhiyun case PTRACE_SETVSRREGS:
287*4882a593Smuzhiyun case PTRACE_GETREGS64:
288*4882a593Smuzhiyun case PTRACE_SETREGS64:
289*4882a593Smuzhiyun case PTRACE_KILL:
290*4882a593Smuzhiyun case PTRACE_SINGLESTEP:
291*4882a593Smuzhiyun case PTRACE_DETACH:
292*4882a593Smuzhiyun case PTRACE_SET_DEBUGREG:
293*4882a593Smuzhiyun case PTRACE_SYSCALL:
294*4882a593Smuzhiyun case PTRACE_CONT:
295*4882a593Smuzhiyun case PPC_PTRACE_GETHWDBGINFO:
296*4882a593Smuzhiyun case PPC_PTRACE_SETHWDEBUG:
297*4882a593Smuzhiyun case PPC_PTRACE_DELHWDEBUG:
298*4882a593Smuzhiyun ret = arch_ptrace(child, request, addr, data);
299*4882a593Smuzhiyun break;
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun default:
302*4882a593Smuzhiyun ret = compat_ptrace_request(child, request, addr, data);
303*4882a593Smuzhiyun break;
304*4882a593Smuzhiyun }
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun return ret;
307*4882a593Smuzhiyun }
308