1 /*
2  *  linux/arch/ppc/kernel/ptrace.c
3  *
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)
13  * and Paul Mackerras (paulus@linuxcare.com.au).
14  *
15  * This file is subject to the terms and conditions of the GNU General
16  * Public License.  Please read the COPYING file for all license details.
17  */
18 
19 #include <linux/kernel.h>
20 #include <linux/sched.h>
21 #include <linux/mm.h>
22 #include <linux/smp.h>
23 #include <linux/smp_lock.h>
24 #include <linux/errno.h>
25 #include <linux/ptrace.h>
26 #include <linux/user.h>
27 
28 #include <asm/uaccess.h>
29 #include <asm/page.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 
33 /*
34  * Set of msr bits that gdb can change on behalf of a process.
35  */
36 #if defined(CONFIG_4xx)
37 #define MSR_DEBUGCHANGE	(0)
38 #else
39 #define MSR_DEBUGCHANGE	(MSR_FE0 | MSR_SE | MSR_BE | MSR_FE1)
40 #endif
41 
42 /*
43  * does not yet catch signals sent when the child dies.
44  * in exit.c or in signal.c.
45  */
46 
47 /*
48  * Get contents of register REGNO in task TASK.
49  */
get_reg(struct task_struct * task,int regno)50 static inline unsigned long get_reg(struct task_struct *task, int regno)
51 {
52 	if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
53 	    && task->thread.regs != NULL)
54 		return ((unsigned long *)task->thread.regs)[regno];
55 	return (0);
56 }
57 
58 /*
59  * Write contents of register REGNO in task TASK.
60  */
put_reg(struct task_struct * task,int regno,unsigned long data)61 static inline int put_reg(struct task_struct *task, int regno,
62 			  unsigned long data)
63 {
64 	if (regno <= PT_MQ && task->thread.regs != NULL) {
65 		if (regno == PT_MSR)
66 			data = (data & MSR_DEBUGCHANGE)
67 				| (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
68 		((unsigned long *)task->thread.regs)[regno] = data;
69 		return 0;
70 	}
71 	return -EIO;
72 }
73 
74 #ifdef CONFIG_ALTIVEC
75 /*
76  * Get contents of AltiVec register state in task TASK
77  */
get_vrregs(unsigned long * data,struct task_struct * task)78 static inline int get_vrregs(unsigned long *data, struct task_struct *task)
79 {
80 	int i, j;
81 
82 	if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
83 		return -EFAULT;
84 
85 	/* copy AltiVec registers VR[0] .. VR[31] */
86 	for (i = 0; i < 32; i++)
87 		for (j = 0; j < 4; j++, data++)
88 			if (__put_user(task->thread.vr[i].u[j], data))
89 				return -EFAULT;
90 
91 	/* copy VSCR */
92 	for (i = 0; i < 4; i++, data++)
93 		if (__put_user(task->thread.vscr.u[i], data))
94 			return -EFAULT;
95 
96         /* copy VRSAVE */
97 	if (__put_user(task->thread.vrsave, data))
98 		return -EFAULT;
99 
100 	return 0;
101 }
102 
103 /*
104  * Write contents of AltiVec register state into task TASK.
105  */
set_vrregs(struct task_struct * task,unsigned long * data)106 static inline int set_vrregs(struct task_struct *task, unsigned long *data)
107 {
108 	int i, j;
109 
110 	if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
111 		return -EFAULT;
112 
113 	/* copy AltiVec registers VR[0] .. VR[31] */
114 	for (i = 0; i < 32; i++)
115 		for (j = 0; j < 4; j++, data++)
116 			if (__get_user(task->thread.vr[i].u[j], data))
117 				return -EFAULT;
118 
119 	/* copy VSCR */
120 	for (i = 0; i < 4; i++, data++)
121 		if (__get_user(task->thread.vscr.u[i], data))
122 			return -EFAULT;
123 
124 	/* copy VRSAVE */
125 	if (__get_user(task->thread.vrsave, data))
126 		return -EFAULT;
127 
128 	return 0;
129 }
130 #endif
131 
132 static inline void
set_single_step(struct task_struct * task)133 set_single_step(struct task_struct *task)
134 {
135 	struct pt_regs *regs = task->thread.regs;
136 #if defined(CONFIG_4xx)
137 	regs->msr |= MSR_DE;
138 	task->thread.dbcr0 |=  (DBCR0_IDM | DBCR0_IC);
139 #else
140 	if (regs != NULL)
141 		regs->msr |= MSR_SE;
142 #endif
143 
144 }
145 
146 static inline void
clear_single_step(struct task_struct * task)147 clear_single_step(struct task_struct *task)
148 {
149 	struct pt_regs *regs = task->thread.regs;
150 #if defined(CONFIG_4xx)
151 	regs->msr &= ~MSR_DE;
152 	task->thread.dbcr0 &=  ~DBCR0_IC;
153 #else
154 	if (regs != NULL)
155 		regs->msr &= ~MSR_SE;
156 #endif
157 }
158 
159 /*
160  * Called by kernel/ptrace.c when detaching..
161  *
162  * Make sure single step bits etc are not set.
163  */
ptrace_disable(struct task_struct * child)164 void ptrace_disable(struct task_struct *child)
165 {
166 	/* make sure the single step bit is not set. */
167 	clear_single_step(child);
168 }
169 
sys_ptrace(long request,long pid,long addr,long data)170 int sys_ptrace(long request, long pid, long addr, long data)
171 {
172 	struct task_struct *child;
173 	int ret = -EPERM;
174 
175 	lock_kernel();
176 	if (request == PTRACE_TRACEME) {
177 		/* are we already being traced? */
178 		if (current->ptrace & PT_PTRACED)
179 			goto out;
180 		/* set the ptrace bit in the process flags. */
181 		current->ptrace |= PT_PTRACED;
182 		ret = 0;
183 		goto out;
184 	}
185 	ret = -ESRCH;
186 	read_lock(&tasklist_lock);
187 	child = find_task_by_pid(pid);
188 	if (child)
189 		get_task_struct(child);
190 	read_unlock(&tasklist_lock);
191 	if (!child)
192 		goto out;
193 
194 	ret = -EPERM;
195 	if (pid == 1)		/* you may not mess with init */
196 		goto out_tsk;
197 
198 	if (request == PTRACE_ATTACH) {
199 		ret = ptrace_attach(child);
200 		goto out_tsk;
201 	}
202 
203 	ret = ptrace_check_attach(child, request == PTRACE_KILL);
204 	if (ret < 0)
205 		goto out_tsk;
206 
207 	switch (request) {
208 	/* when I and D space are separate, these will need to be fixed. */
209 	case PTRACE_PEEKTEXT: /* read word at location addr. */
210 	case PTRACE_PEEKDATA: {
211 		unsigned long tmp;
212 		int copied;
213 
214 		copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
215 		ret = -EIO;
216 		if (copied != sizeof(tmp))
217 			break;
218 		ret = put_user(tmp,(unsigned long *) data);
219 		break;
220 	}
221 
222 	/* read the word at location addr in the USER area. */
223 	/* XXX this will need fixing for 64-bit */
224 	case PTRACE_PEEKUSR: {
225 		unsigned long index, tmp;
226 
227 		ret = -EIO;
228 		/* convert to index and check */
229 		index = (unsigned long) addr >> 2;
230 		if ((addr & 3) || index > PT_FPSCR)
231 			break;
232 
233 		if (index < PT_FPR0) {
234 			tmp = get_reg(child, (int) index);
235 		} else {
236 			if (child->thread.regs != NULL
237 			    && child->thread.regs->msr & MSR_FP)
238 				giveup_fpu(child);
239 			tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
240 		}
241 		ret = put_user(tmp,(unsigned long *) data);
242 		break;
243 	}
244 
245 	/* If I and D space are separate, this will have to be fixed. */
246 	case PTRACE_POKETEXT: /* write the word at location addr. */
247 	case PTRACE_POKEDATA:
248 		ret = 0;
249 		if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
250 			break;
251 		ret = -EIO;
252 		break;
253 
254 	/* write the word at location addr in the USER area */
255 	/* XXX this will need fixing for 64-bit */
256 	case PTRACE_POKEUSR: {
257 		unsigned long index;
258 
259 		ret = -EIO;
260 		/* convert to index and check */
261 		index = (unsigned long) addr >> 2;
262 		if ((addr & 3) || index > PT_FPSCR)
263 			break;
264 
265 		if (index == PT_ORIG_R3)
266 			break;
267 		if (index < PT_FPR0) {
268 			ret = put_reg(child, index, data);
269 		} else {
270 			if (child->thread.regs != NULL
271 			    && child->thread.regs->msr & MSR_FP)
272 				giveup_fpu(child);
273 			((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
274 			ret = 0;
275 		}
276 		break;
277 	}
278 
279 	case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
280 	case PTRACE_CONT: { /* restart after signal. */
281 		ret = -EIO;
282 		if ((unsigned long) data > _NSIG)
283 			break;
284 		if (request == PTRACE_SYSCALL)
285 			child->ptrace |= PT_TRACESYS;
286 		else
287 			child->ptrace &= ~PT_TRACESYS;
288 		child->exit_code = data;
289 		/* make sure the single step bit is not set. */
290 		clear_single_step(child);
291 #ifdef CONFIG_4xx
292 		/* ...but traps may be set, so catch those....
293 		*/
294 		child->thread.regs->msr   |= MSR_DE;
295 		child->thread.dbcr0 |= (DBCR0_IDM | DBCR0_TDE);
296 #endif
297 		wake_up_process(child);
298 		ret = 0;
299 		break;
300 	}
301 
302 /*
303  * make the child exit.  Best I can do is send it a sigkill.
304  * perhaps it should be put in the status that it wants to
305  * exit.
306  */
307 	case PTRACE_KILL: {
308 		ret = 0;
309 		if (child->state == TASK_ZOMBIE)	/* already dead */
310 			break;
311 		child->exit_code = SIGKILL;
312 		/* make sure the single step bit is not set. */
313 		clear_single_step(child);
314 		wake_up_process(child);
315 		break;
316 	}
317 
318 	case PTRACE_SINGLESTEP: {  /* set the trap flag. */
319 		ret = -EIO;
320 		if ((unsigned long) data > _NSIG)
321 			break;
322 		child->ptrace &= ~PT_TRACESYS;
323 		set_single_step(child);
324 		child->exit_code = data;
325 		/* give it a chance to run. */
326 		wake_up_process(child);
327 		ret = 0;
328 		break;
329 	}
330 
331 	case PTRACE_DETACH:
332 		ret = ptrace_detach(child, data);
333 		break;
334 
335 #ifdef CONFIG_ALTIVEC
336 	case PTRACE_GETVRREGS:
337 		/* Get the child altivec register state. */
338 		if (child->thread.regs->msr & MSR_VEC)
339 			giveup_altivec(child);
340 		ret = get_vrregs((unsigned long *)data, child);
341 		break;
342 
343 	case PTRACE_SETVRREGS:
344 		/* Set the child altivec register state. */
345 		/* this is to clear the MSR_VEC bit to force a reload
346 		 * of register state from memory */
347 		if (child->thread.regs->msr & MSR_VEC)
348 			giveup_altivec(child);
349 		ret = set_vrregs(child, (unsigned long *)data);
350 		break;
351 #endif
352 
353 	default:
354 		ret = -EIO;
355 		break;
356 	}
357 out_tsk:
358 	free_task_struct(child);
359 out:
360 	unlock_kernel();
361 	return ret;
362 }
363 
syscall_trace(void)364 void syscall_trace(void)
365 {
366 	if ((current->ptrace & (PT_PTRACED|PT_TRACESYS))
367 			!= (PT_PTRACED|PT_TRACESYS))
368 		return;
369 	current->exit_code = SIGTRAP;
370 	current->state = TASK_STOPPED;
371 	notify_parent(current, SIGCHLD);
372 	schedule();
373 	/*
374 	 * this isn't the same as continuing with a signal, but it will do
375 	 * for normal use.  strace only continues with a signal if the
376 	 * stopping signal is not SIGTRAP.  -brl
377 	 */
378 	if (current->exit_code) {
379 		send_sig(current->exit_code, current, 1);
380 		current->exit_code = 0;
381 	}
382 }
383