2 * ptrace for 32-bit processes running on a 64-bit kernel.
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@samba.org).
15 * This file is subject to the terms and conditions of the GNU General
16 * Public License. See the file COPYING in the main directory of
17 * this archive for more details.
20 #include <linux/config.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
24 #include <linux/smp.h>
25 #include <linux/smp_lock.h>
26 #include <linux/errno.h>
27 #include <linux/ptrace.h>
28 #include <linux/user.h>
29 #include <linux/security.h>
30 #include <linux/signal.h>
32 #include <asm/uaccess.h>
34 #include <asm/pgtable.h>
35 #include <asm/system.h>
37 #include "ptrace-common.h"
40 * does not yet catch signals sent when the child dies.
41 * in exit.c or in signal.c.
44 long compat_sys_ptrace(int request, int pid, unsigned long addr,
47 struct task_struct *child;
51 if (request == PTRACE_TRACEME) {
52 /* are we already being traced? */
53 if (current->ptrace & PT_PTRACED)
55 ret = security_ptrace(current->parent, current);
58 /* set the ptrace bit in the process flags. */
59 current->ptrace |= PT_PTRACED;
64 read_lock(&tasklist_lock);
65 child = find_task_by_pid(pid);
67 get_task_struct(child);
68 read_unlock(&tasklist_lock);
73 if (pid == 1) /* you may not mess with init */
76 if (request == PTRACE_ATTACH) {
77 ret = ptrace_attach(child);
81 ret = ptrace_check_attach(child, request == PTRACE_KILL);
86 /* when I and D space are separate, these will need to be fixed. */
87 case PTRACE_PEEKTEXT: /* read word at location addr. */
88 case PTRACE_PEEKDATA: {
92 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
94 if (copied != sizeof(tmp))
96 ret = put_user(tmp, (u32 __user *)data);
101 * Read 4 bytes of the other process' storage
102 * data is a pointer specifying where the user wants the
103 * 4 bytes copied into
104 * addr is a pointer in the user's storage that contains an 8 byte
105 * address in the other process of the 4 bytes that is to be read
106 * (this is run in a 32-bit process looking at a 64-bit process)
107 * when I and D space are separate, these will need to be fixed.
109 case PPC_PTRACE_PEEKTEXT_3264:
110 case PPC_PTRACE_PEEKDATA_3264: {
113 u32 __user * addrOthers;
117 /* Get the addr in the other process that we want to read */
118 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
121 copied = access_process_vm(child, (u64)addrOthers, &tmp,
123 if (copied != sizeof(tmp))
125 ret = put_user(tmp, (u32 __user *)data);
129 /* Read a register (specified by ADDR) out of the "user area" */
130 case PTRACE_PEEKUSR: {
135 /* convert to index and check */
136 index = (unsigned long) addr >> 2;
137 if ((addr & 3) || (index > PT_FPSCR32))
140 if (index < PT_FPR0) {
141 tmp = get_reg(child, index);
143 flush_fp_to_thread(child);
145 * the user space code considers the floating point
146 * to be an array of unsigned int (32 bits) - the
147 * index passed in is based on this assumption.
149 tmp = ((unsigned int *)child->thread.fpr)[index - PT_FPR0];
151 ret = put_user((unsigned int)tmp, (u32 __user *)data);
156 * Read 4 bytes out of the other process' pt_regs area
157 * data is a pointer specifying where the user wants the
158 * 4 bytes copied into
159 * addr is the offset into the other process' pt_regs structure
161 * (this is run in a 32-bit process looking at a 64-bit process)
163 case PPC_PTRACE_PEEKUSR_3264: {
171 /* Determine which register the user wants */
172 index = (u64)addr >> 2;
174 /* Determine which part of the register the user wants */
176 part = 1; /* want the 2nd half of the register (right-most). */
178 part = 0; /* want the 1st half of the register (left-most). */
180 /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
181 if ((addr & 3) || numReg > PT_FPSCR)
184 if (numReg >= PT_FPR0) {
185 flush_fp_to_thread(child);
186 tmp = ((unsigned long int *)child->thread.fpr)[numReg - PT_FPR0];
187 } else { /* register within PT_REGS struct */
188 tmp = get_reg(child, numReg);
190 reg32bits = ((u32*)&tmp)[part];
191 ret = put_user(reg32bits, (u32 __user *)data);
195 /* If I and D space are separate, this will have to be fixed. */
196 case PTRACE_POKETEXT: /* write the word at location addr. */
197 case PTRACE_POKEDATA: {
201 if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1)
209 * Write 4 bytes into the other process' storage
210 * data is the 4 bytes that the user wants written
211 * addr is a pointer in the user's storage that contains an
212 * 8 byte address in the other process where the 4 bytes
213 * that is to be written
214 * (this is run in a 32-bit process looking at a 64-bit process)
215 * when I and D space are separate, these will need to be fixed.
217 case PPC_PTRACE_POKETEXT_3264:
218 case PPC_PTRACE_POKEDATA_3264: {
220 u32 __user * addrOthers;
222 /* Get the addr in the other process that we want to write into */
224 if (get_user(addrOthers, (u32 __user * __user *)addr) != 0)
227 if (access_process_vm(child, (u64)addrOthers, &tmp,
228 sizeof(tmp), 1) == sizeof(tmp))
234 /* write the word at location addr in the USER area */
235 case PTRACE_POKEUSR: {
239 /* convert to index and check */
240 index = (unsigned long) addr >> 2;
241 if ((addr & 3) || (index > PT_FPSCR32))
244 if (index == PT_ORIG_R3)
246 if (index < PT_FPR0) {
247 ret = put_reg(child, index, data);
249 flush_fp_to_thread(child);
251 * the user space code considers the floating point
252 * to be an array of unsigned int (32 bits) - the
253 * index passed in is based on this assumption.
255 ((unsigned int *)child->thread.fpr)[index - PT_FPR0] = data;
262 * Write 4 bytes into the other process' pt_regs area
263 * data is the 4 bytes that the user wants written
264 * addr is the offset into the other process' pt_regs structure
265 * that is to be written into
266 * (this is run in a 32-bit process looking at a 64-bit process)
268 case PPC_PTRACE_POKEUSR_3264: {
273 /* Determine which register the user wants */
274 index = (u64)addr >> 2;
277 * Validate the input - check to see if address is on the
278 * wrong boundary or beyond the end of the user area
280 if ((addr & 3) || (numReg > PT_FPSCR))
282 /* Insure it is a register we let them change */
283 if ((numReg == PT_ORIG_R3)
284 || ((numReg > PT_CCR) && (numReg < PT_FPR0)))
286 if (numReg >= PT_FPR0) {
287 flush_fp_to_thread(child);
289 if (numReg == PT_MSR)
290 data = (data & MSR_DEBUGCHANGE)
291 | (child->thread.regs->msr & ~MSR_DEBUGCHANGE);
292 ((u32*)child->thread.regs)[index] = data;
297 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
298 case PTRACE_CONT: { /* restart after signal. */
300 if (!valid_signal(data))
302 if (request == PTRACE_SYSCALL)
303 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
305 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
306 child->exit_code = data;
307 /* make sure the single step bit is not set. */
308 clear_single_step(child);
309 wake_up_process(child);
315 * make the child exit. Best I can do is send it a sigkill.
316 * perhaps it should be put in the status that it wants to
321 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
323 child->exit_code = SIGKILL;
324 /* make sure the single step bit is not set. */
325 clear_single_step(child);
326 wake_up_process(child);
330 case PTRACE_SINGLESTEP: { /* set the trap flag. */
332 if (!valid_signal(data))
334 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
335 set_single_step(child);
336 child->exit_code = data;
337 /* give it a chance to run. */
338 wake_up_process(child);
343 case PTRACE_GET_DEBUGREG: {
345 /* We only support one DABR and no IABRS at the moment */
348 ret = put_user(child->thread.dabr, (u32 __user *)data);
352 case PTRACE_SET_DEBUGREG:
353 ret = ptrace_set_debugreg(child, addr, data);
357 ret = ptrace_detach(child, data);
360 case PPC_PTRACE_GETREGS: { /* Get GPRs 0 - 31. */
362 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
363 unsigned int __user *tmp = (unsigned int __user *)addr;
365 for (i = 0; i < 32; i++) {
366 ret = put_user(*reg, tmp);
375 case PPC_PTRACE_SETREGS: { /* Set GPRs 0 - 31. */
377 unsigned long *reg = &((unsigned long *)child->thread.regs)[0];
378 unsigned int __user *tmp = (unsigned int __user *)addr;
380 for (i = 0; i < 32; i++) {
381 ret = get_user(*reg, tmp);
390 case PPC_PTRACE_GETFPREGS: { /* Get FPRs 0 - 31. */
392 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
393 unsigned int __user *tmp = (unsigned int __user *)addr;
395 flush_fp_to_thread(child);
397 for (i = 0; i < 32; i++) {
398 ret = put_user(*reg, tmp);
407 case PPC_PTRACE_SETFPREGS: { /* Get FPRs 0 - 31. */
409 unsigned long *reg = &((unsigned long *)child->thread.fpr)[0];
410 unsigned int __user *tmp = (unsigned int __user *)addr;
412 flush_fp_to_thread(child);
414 for (i = 0; i < 32; i++) {
415 ret = get_user(*reg, tmp);
424 case PTRACE_GETEVENTMSG:
425 ret = put_user(child->ptrace_message, (unsigned int __user *) data);
428 #ifdef CONFIG_ALTIVEC
429 case PTRACE_GETVRREGS:
430 /* Get the child altivec register state. */
431 flush_altivec_to_thread(child);
432 ret = get_vrregs((unsigned long __user *)data, child);
435 case PTRACE_SETVRREGS:
436 /* Set the child altivec register state. */
437 flush_altivec_to_thread(child);
438 ret = set_vrregs(child, (unsigned long __user *)data);
443 ret = ptrace_request(child, request, addr, data);
447 put_task_struct(child);