[PATCH] ppc32: Added support for the Book-E style Watchdog Timer
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / ppc / kernel / ptrace.c
blobe7aee4108dea69985ba02507a78a46f227ba47da
1 /*
2 * arch/ppc/kernel/ptrace.c
4 * PowerPC version
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
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
12 * Modified by Cort Dougan (cort@hq.fsmlabs.com)
13 * and Paul Mackerras (paulus@linuxcare.com.au).
15 * This file is subject to the terms and conditions of the GNU General
16 * Public License. See the file README.legal in the main directory of
17 * this archive for more details.
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/mm.h>
23 #include <linux/smp.h>
24 #include <linux/smp_lock.h>
25 #include <linux/errno.h>
26 #include <linux/ptrace.h>
27 #include <linux/user.h>
28 #include <linux/security.h>
29 #include <linux/signal.h>
30 #include <linux/seccomp.h>
31 #include <linux/audit.h>
32 #include <linux/module.h>
34 #include <asm/uaccess.h>
35 #include <asm/page.h>
36 #include <asm/pgtable.h>
37 #include <asm/system.h>
40 * Set of msr bits that gdb can change on behalf of a process.
42 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
43 #define MSR_DEBUGCHANGE 0
44 #else
45 #define MSR_DEBUGCHANGE (MSR_SE | MSR_BE)
46 #endif
49 * does not yet catch signals sent when the child dies.
50 * in exit.c or in signal.c.
54 * Get contents of register REGNO in task TASK.
56 static inline unsigned long get_reg(struct task_struct *task, int regno)
58 if (regno < sizeof(struct pt_regs) / sizeof(unsigned long)
59 && task->thread.regs != NULL)
60 return ((unsigned long *)task->thread.regs)[regno];
61 return (0);
65 * Write contents of register REGNO in task TASK.
67 static inline int put_reg(struct task_struct *task, int regno,
68 unsigned long data)
70 if (regno <= PT_MQ && task->thread.regs != NULL) {
71 if (regno == PT_MSR)
72 data = (data & MSR_DEBUGCHANGE)
73 | (task->thread.regs->msr & ~MSR_DEBUGCHANGE);
74 ((unsigned long *)task->thread.regs)[regno] = data;
75 return 0;
77 return -EIO;
80 #ifdef CONFIG_ALTIVEC
82 * Get contents of AltiVec register state in task TASK
84 static inline int get_vrregs(unsigned long __user *data, struct task_struct *task)
86 int i, j;
88 if (!access_ok(VERIFY_WRITE, data, 133 * sizeof(unsigned long)))
89 return -EFAULT;
91 /* copy AltiVec registers VR[0] .. VR[31] */
92 for (i = 0; i < 32; i++)
93 for (j = 0; j < 4; j++, data++)
94 if (__put_user(task->thread.vr[i].u[j], data))
95 return -EFAULT;
97 /* copy VSCR */
98 for (i = 0; i < 4; i++, data++)
99 if (__put_user(task->thread.vscr.u[i], data))
100 return -EFAULT;
102 /* copy VRSAVE */
103 if (__put_user(task->thread.vrsave, data))
104 return -EFAULT;
106 return 0;
110 * Write contents of AltiVec register state into task TASK.
112 static inline int set_vrregs(struct task_struct *task, unsigned long __user *data)
114 int i, j;
116 if (!access_ok(VERIFY_READ, data, 133 * sizeof(unsigned long)))
117 return -EFAULT;
119 /* copy AltiVec registers VR[0] .. VR[31] */
120 for (i = 0; i < 32; i++)
121 for (j = 0; j < 4; j++, data++)
122 if (__get_user(task->thread.vr[i].u[j], data))
123 return -EFAULT;
125 /* copy VSCR */
126 for (i = 0; i < 4; i++, data++)
127 if (__get_user(task->thread.vscr.u[i], data))
128 return -EFAULT;
130 /* copy VRSAVE */
131 if (__get_user(task->thread.vrsave, data))
132 return -EFAULT;
134 return 0;
136 #endif
138 #ifdef CONFIG_SPE
141 * For get_evrregs/set_evrregs functions 'data' has the following layout:
143 * struct {
144 * u32 evr[32];
145 * u64 acc;
146 * u32 spefscr;
151 * Get contents of SPE register state in task TASK.
153 static inline int get_evrregs(unsigned long *data, struct task_struct *task)
155 int i;
157 if (!access_ok(VERIFY_WRITE, data, 35 * sizeof(unsigned long)))
158 return -EFAULT;
160 /* copy SPEFSCR */
161 if (__put_user(task->thread.spefscr, &data[34]))
162 return -EFAULT;
164 /* copy SPE registers EVR[0] .. EVR[31] */
165 for (i = 0; i < 32; i++, data++)
166 if (__put_user(task->thread.evr[i], data))
167 return -EFAULT;
169 /* copy ACC */
170 if (__put_user64(task->thread.acc, (unsigned long long *)data))
171 return -EFAULT;
173 return 0;
177 * Write contents of SPE register state into task TASK.
179 static inline int set_evrregs(struct task_struct *task, unsigned long *data)
181 int i;
183 if (!access_ok(VERIFY_READ, data, 35 * sizeof(unsigned long)))
184 return -EFAULT;
186 /* copy SPEFSCR */
187 if (__get_user(task->thread.spefscr, &data[34]))
188 return -EFAULT;
190 /* copy SPE registers EVR[0] .. EVR[31] */
191 for (i = 0; i < 32; i++, data++)
192 if (__get_user(task->thread.evr[i], data))
193 return -EFAULT;
194 /* copy ACC */
195 if (__get_user64(task->thread.acc, (unsigned long long*)data))
196 return -EFAULT;
198 return 0;
200 #endif /* CONFIG_SPE */
202 static inline void
203 set_single_step(struct task_struct *task)
205 struct pt_regs *regs = task->thread.regs;
207 if (regs != NULL) {
208 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
209 task->thread.dbcr0 = DBCR0_IDM | DBCR0_IC;
210 regs->msr |= MSR_DE;
211 #else
212 regs->msr |= MSR_SE;
213 #endif
217 static inline void
218 clear_single_step(struct task_struct *task)
220 struct pt_regs *regs = task->thread.regs;
222 if (regs != NULL) {
223 #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
224 task->thread.dbcr0 = 0;
225 regs->msr &= ~MSR_DE;
226 #else
227 regs->msr &= ~MSR_SE;
228 #endif
233 * Called by kernel/ptrace.c when detaching..
235 * Make sure single step bits etc are not set.
237 void ptrace_disable(struct task_struct *child)
239 /* make sure the single step bit is not set. */
240 clear_single_step(child);
243 int sys_ptrace(long request, long pid, long addr, long data)
245 struct task_struct *child;
246 int ret = -EPERM;
248 lock_kernel();
249 if (request == PTRACE_TRACEME) {
250 /* are we already being traced? */
251 if (current->ptrace & PT_PTRACED)
252 goto out;
253 ret = security_ptrace(current->parent, current);
254 if (ret)
255 goto out;
256 /* set the ptrace bit in the process flags. */
257 current->ptrace |= PT_PTRACED;
258 ret = 0;
259 goto out;
261 ret = -ESRCH;
262 read_lock(&tasklist_lock);
263 child = find_task_by_pid(pid);
264 if (child)
265 get_task_struct(child);
266 read_unlock(&tasklist_lock);
267 if (!child)
268 goto out;
270 ret = -EPERM;
271 if (pid == 1) /* you may not mess with init */
272 goto out_tsk;
274 if (request == PTRACE_ATTACH) {
275 ret = ptrace_attach(child);
276 goto out_tsk;
279 ret = ptrace_check_attach(child, request == PTRACE_KILL);
280 if (ret < 0)
281 goto out_tsk;
283 switch (request) {
284 /* when I and D space are separate, these will need to be fixed. */
285 case PTRACE_PEEKTEXT: /* read word at location addr. */
286 case PTRACE_PEEKDATA: {
287 unsigned long tmp;
288 int copied;
290 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
291 ret = -EIO;
292 if (copied != sizeof(tmp))
293 break;
294 ret = put_user(tmp,(unsigned long __user *) data);
295 break;
298 /* read the word at location addr in the USER area. */
299 /* XXX this will need fixing for 64-bit */
300 case PTRACE_PEEKUSR: {
301 unsigned long index, tmp;
303 ret = -EIO;
304 /* convert to index and check */
305 index = (unsigned long) addr >> 2;
306 if ((addr & 3) || index > PT_FPSCR
307 || child->thread.regs == NULL)
308 break;
310 CHECK_FULL_REGS(child->thread.regs);
311 if (index < PT_FPR0) {
312 tmp = get_reg(child, (int) index);
313 } else {
314 preempt_disable();
315 if (child->thread.regs->msr & MSR_FP)
316 giveup_fpu(child);
317 preempt_enable();
318 tmp = ((unsigned long *)child->thread.fpr)[index - PT_FPR0];
320 ret = put_user(tmp,(unsigned long __user *) data);
321 break;
324 /* If I and D space are separate, this will have to be fixed. */
325 case PTRACE_POKETEXT: /* write the word at location addr. */
326 case PTRACE_POKEDATA:
327 ret = 0;
328 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
329 break;
330 ret = -EIO;
331 break;
333 /* write the word at location addr in the USER area */
334 case PTRACE_POKEUSR: {
335 unsigned long index;
337 ret = -EIO;
338 /* convert to index and check */
339 index = (unsigned long) addr >> 2;
340 if ((addr & 3) || index > PT_FPSCR
341 || child->thread.regs == NULL)
342 break;
344 CHECK_FULL_REGS(child->thread.regs);
345 if (index == PT_ORIG_R3)
346 break;
347 if (index < PT_FPR0) {
348 ret = put_reg(child, index, data);
349 } else {
350 preempt_disable();
351 if (child->thread.regs->msr & MSR_FP)
352 giveup_fpu(child);
353 preempt_enable();
354 ((unsigned long *)child->thread.fpr)[index - PT_FPR0] = data;
355 ret = 0;
357 break;
360 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
361 case PTRACE_CONT: { /* restart after signal. */
362 ret = -EIO;
363 if (!valid_signal(data))
364 break;
365 if (request == PTRACE_SYSCALL) {
366 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
367 } else {
368 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
370 child->exit_code = data;
371 /* make sure the single step bit is not set. */
372 clear_single_step(child);
373 wake_up_process(child);
374 ret = 0;
375 break;
379 * make the child exit. Best I can do is send it a sigkill.
380 * perhaps it should be put in the status that it wants to
381 * exit.
383 case PTRACE_KILL: {
384 ret = 0;
385 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
386 break;
387 child->exit_code = SIGKILL;
388 /* make sure the single step bit is not set. */
389 clear_single_step(child);
390 wake_up_process(child);
391 break;
394 case PTRACE_SINGLESTEP: { /* set the trap flag. */
395 ret = -EIO;
396 if (!valid_signal(data))
397 break;
398 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
399 set_single_step(child);
400 child->exit_code = data;
401 /* give it a chance to run. */
402 wake_up_process(child);
403 ret = 0;
404 break;
407 case PTRACE_DETACH:
408 ret = ptrace_detach(child, data);
409 break;
411 #ifdef CONFIG_ALTIVEC
412 case PTRACE_GETVRREGS:
413 /* Get the child altivec register state. */
414 preempt_disable();
415 if (child->thread.regs->msr & MSR_VEC)
416 giveup_altivec(child);
417 preempt_enable();
418 ret = get_vrregs((unsigned long __user *)data, child);
419 break;
421 case PTRACE_SETVRREGS:
422 /* Set the child altivec register state. */
423 /* this is to clear the MSR_VEC bit to force a reload
424 * of register state from memory */
425 preempt_disable();
426 if (child->thread.regs->msr & MSR_VEC)
427 giveup_altivec(child);
428 preempt_enable();
429 ret = set_vrregs(child, (unsigned long __user *)data);
430 break;
431 #endif
432 #ifdef CONFIG_SPE
433 case PTRACE_GETEVRREGS:
434 /* Get the child spe register state. */
435 if (child->thread.regs->msr & MSR_SPE)
436 giveup_spe(child);
437 ret = get_evrregs((unsigned long __user *)data, child);
438 break;
440 case PTRACE_SETEVRREGS:
441 /* Set the child spe register state. */
442 /* this is to clear the MSR_SPE bit to force a reload
443 * of register state from memory */
444 if (child->thread.regs->msr & MSR_SPE)
445 giveup_spe(child);
446 ret = set_evrregs(child, (unsigned long __user *)data);
447 break;
448 #endif
450 default:
451 ret = ptrace_request(child, request, addr, data);
452 break;
454 out_tsk:
455 put_task_struct(child);
456 out:
457 unlock_kernel();
458 return ret;
461 static void do_syscall_trace(void)
463 /* the 0x80 provides a way for the tracing parent to distinguish
464 between a syscall stop and SIGTRAP delivery */
465 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
466 ? 0x80 : 0));
469 * this isn't the same as continuing with a signal, but it will do
470 * for normal use. strace only continues with a signal if the
471 * stopping signal is not SIGTRAP. -brl
473 if (current->exit_code) {
474 send_sig(current->exit_code, current, 1);
475 current->exit_code = 0;
479 void do_syscall_trace_enter(struct pt_regs *regs)
481 if (test_thread_flag(TIF_SYSCALL_TRACE)
482 && (current->ptrace & PT_PTRACED))
483 do_syscall_trace();
485 if (unlikely(current->audit_context))
486 audit_syscall_entry(current, AUDIT_ARCH_PPC,
487 regs->gpr[0],
488 regs->gpr[3], regs->gpr[4],
489 regs->gpr[5], regs->gpr[6]);
492 void do_syscall_trace_leave(struct pt_regs *regs)
494 secure_computing(regs->gpr[0]);
496 if (unlikely(current->audit_context))
497 audit_syscall_exit(current,
498 (regs->ccr&0x1000)?AUDITSC_FAILURE:AUDITSC_SUCCESS,
499 regs->result);
501 if ((test_thread_flag(TIF_SYSCALL_TRACE))
502 && (current->ptrace & PT_PTRACED))
503 do_syscall_trace();
506 EXPORT_SYMBOL(do_syscall_trace_enter);
507 EXPORT_SYMBOL(do_syscall_trace_leave);