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