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>
36 #include <asm/ptrace-common.h>
39 * does not yet catch signals sent when the child dies.
40 * in exit.c or in signal.c.
43 long compat_sys_ptrace(int request
, int pid
, unsigned long addr
,
46 struct task_struct
*child
;
50 if (request
== PTRACE_TRACEME
) {
51 /* are we already being traced? */
52 if (current
->ptrace
& PT_PTRACED
)
54 ret
= security_ptrace(current
->parent
, current
);
57 /* set the ptrace bit in the process flags. */
58 current
->ptrace
|= PT_PTRACED
;
63 read_lock(&tasklist_lock
);
64 child
= find_task_by_pid(pid
);
66 get_task_struct(child
);
67 read_unlock(&tasklist_lock
);
72 if (pid
== 1) /* you may not mess with init */
75 if (request
== PTRACE_ATTACH
) {
76 ret
= ptrace_attach(child
);
80 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
85 /* when I and D space are separate, these will need to be fixed. */
86 case PTRACE_PEEKTEXT
: /* read word at location addr. */
87 case PTRACE_PEEKDATA
: {
91 copied
= access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 0);
93 if (copied
!= sizeof(tmp
))
95 ret
= put_user(tmp
, (u32 __user
*)data
);
100 * Read 4 bytes of the other process' storage
101 * data is a pointer specifying where the user wants the
102 * 4 bytes copied into
103 * addr is a pointer in the user's storage that contains an 8 byte
104 * address in the other process of the 4 bytes that is to be read
105 * (this is run in a 32-bit process looking at a 64-bit process)
106 * when I and D space are separate, these will need to be fixed.
108 case PPC_PTRACE_PEEKTEXT_3264
:
109 case PPC_PTRACE_PEEKDATA_3264
: {
112 u32 __user
* addrOthers
;
116 /* Get the addr in the other process that we want to read */
117 if (get_user(addrOthers
, (u32 __user
* __user
*)addr
) != 0)
120 copied
= access_process_vm(child
, (u64
)addrOthers
, &tmp
,
122 if (copied
!= sizeof(tmp
))
124 ret
= put_user(tmp
, (u32 __user
*)data
);
128 /* Read a register (specified by ADDR) out of the "user area" */
129 case PTRACE_PEEKUSR
: {
134 /* convert to index and check */
135 index
= (unsigned long) addr
>> 2;
136 if ((addr
& 3) || (index
> PT_FPSCR32
))
139 if (index
< PT_FPR0
) {
140 tmp
= get_reg(child
, index
);
142 flush_fp_to_thread(child
);
144 * the user space code considers the floating point
145 * to be an array of unsigned int (32 bits) - the
146 * index passed in is based on this assumption.
148 tmp
= ((unsigned int *)child
->thread
.fpr
)[index
- PT_FPR0
];
150 ret
= put_user((unsigned int)tmp
, (u32 __user
*)data
);
155 * Read 4 bytes out of the other process' pt_regs area
156 * data is a pointer specifying where the user wants the
157 * 4 bytes copied into
158 * addr is the offset into the other process' pt_regs structure
160 * (this is run in a 32-bit process looking at a 64-bit process)
162 case PPC_PTRACE_PEEKUSR_3264
: {
170 /* Determine which register the user wants */
171 index
= (u64
)addr
>> 2;
173 /* Determine which part of the register the user wants */
175 part
= 1; /* want the 2nd half of the register (right-most). */
177 part
= 0; /* want the 1st half of the register (left-most). */
179 /* Validate the input - check to see if address is on the wrong boundary or beyond the end of the user area */
180 if ((addr
& 3) || numReg
> PT_FPSCR
)
183 if (numReg
>= PT_FPR0
) {
184 flush_fp_to_thread(child
);
185 tmp
= ((unsigned long int *)child
->thread
.fpr
)[numReg
- PT_FPR0
];
186 } else { /* register within PT_REGS struct */
187 tmp
= get_reg(child
, numReg
);
189 reg32bits
= ((u32
*)&tmp
)[part
];
190 ret
= put_user(reg32bits
, (u32 __user
*)data
);
194 /* If I and D space are separate, this will have to be fixed. */
195 case PTRACE_POKETEXT
: /* write the word at location addr. */
196 case PTRACE_POKEDATA
: {
200 if (access_process_vm(child
, addr
, &tmp
, sizeof(tmp
), 1)
208 * Write 4 bytes into the other process' storage
209 * data is the 4 bytes that the user wants written
210 * addr is a pointer in the user's storage that contains an
211 * 8 byte address in the other process where the 4 bytes
212 * that is to be written
213 * (this is run in a 32-bit process looking at a 64-bit process)
214 * when I and D space are separate, these will need to be fixed.
216 case PPC_PTRACE_POKETEXT_3264
:
217 case PPC_PTRACE_POKEDATA_3264
: {
219 u32 __user
* addrOthers
;
221 /* Get the addr in the other process that we want to write into */
223 if (get_user(addrOthers
, (u32 __user
* __user
*)addr
) != 0)
226 if (access_process_vm(child
, (u64
)addrOthers
, &tmp
,
227 sizeof(tmp
), 1) == sizeof(tmp
))
233 /* write the word at location addr in the USER area */
234 case PTRACE_POKEUSR
: {
238 /* convert to index and check */
239 index
= (unsigned long) addr
>> 2;
240 if ((addr
& 3) || (index
> PT_FPSCR32
))
243 if (index
== PT_ORIG_R3
)
245 if (index
< PT_FPR0
) {
246 ret
= put_reg(child
, index
, data
);
248 flush_fp_to_thread(child
);
250 * the user space code considers the floating point
251 * to be an array of unsigned int (32 bits) - the
252 * index passed in is based on this assumption.
254 ((unsigned int *)child
->thread
.fpr
)[index
- PT_FPR0
] = data
;
261 * Write 4 bytes into the other process' pt_regs area
262 * data is the 4 bytes that the user wants written
263 * addr is the offset into the other process' pt_regs structure
264 * that is to be written into
265 * (this is run in a 32-bit process looking at a 64-bit process)
267 case PPC_PTRACE_POKEUSR_3264
: {
272 /* Determine which register the user wants */
273 index
= (u64
)addr
>> 2;
276 * Validate the input - check to see if address is on the
277 * wrong boundary or beyond the end of the user area
279 if ((addr
& 3) || (numReg
> PT_FPSCR
))
281 /* Insure it is a register we let them change */
282 if ((numReg
== PT_ORIG_R3
)
283 || ((numReg
> PT_CCR
) && (numReg
< PT_FPR0
)))
285 if (numReg
>= PT_FPR0
) {
286 flush_fp_to_thread(child
);
288 if (numReg
== PT_MSR
)
289 data
= (data
& MSR_DEBUGCHANGE
)
290 | (child
->thread
.regs
->msr
& ~MSR_DEBUGCHANGE
);
291 ((u32
*)child
->thread
.regs
)[index
] = data
;
296 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
297 case PTRACE_CONT
: { /* restart after signal. */
299 if (!valid_signal(data
))
301 if (request
== PTRACE_SYSCALL
)
302 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
304 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
305 child
->exit_code
= data
;
306 /* make sure the single step bit is not set. */
307 clear_single_step(child
);
308 wake_up_process(child
);
314 * make the child exit. Best I can do is send it a sigkill.
315 * perhaps it should be put in the status that it wants to
320 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
322 child
->exit_code
= SIGKILL
;
323 /* make sure the single step bit is not set. */
324 clear_single_step(child
);
325 wake_up_process(child
);
329 case PTRACE_SINGLESTEP
: { /* set the trap flag. */
331 if (!valid_signal(data
))
333 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
334 set_single_step(child
);
335 child
->exit_code
= data
;
336 /* give it a chance to run. */
337 wake_up_process(child
);
342 case PTRACE_GET_DEBUGREG
: {
344 /* We only support one DABR and no IABRS at the moment */
347 ret
= put_user(child
->thread
.dabr
, (u32 __user
*)data
);
351 case PTRACE_SET_DEBUGREG
:
352 ret
= ptrace_set_debugreg(child
, addr
, data
);
356 ret
= ptrace_detach(child
, data
);
359 case PPC_PTRACE_GETREGS
: { /* Get GPRs 0 - 31. */
361 unsigned long *reg
= &((unsigned long *)child
->thread
.regs
)[0];
362 unsigned int __user
*tmp
= (unsigned int __user
*)addr
;
364 for (i
= 0; i
< 32; i
++) {
365 ret
= put_user(*reg
, tmp
);
374 case PPC_PTRACE_SETREGS
: { /* Set GPRs 0 - 31. */
376 unsigned long *reg
= &((unsigned long *)child
->thread
.regs
)[0];
377 unsigned int __user
*tmp
= (unsigned int __user
*)addr
;
379 for (i
= 0; i
< 32; i
++) {
380 ret
= get_user(*reg
, tmp
);
389 case PPC_PTRACE_GETFPREGS
: { /* Get FPRs 0 - 31. */
391 unsigned long *reg
= &((unsigned long *)child
->thread
.fpr
)[0];
392 unsigned int __user
*tmp
= (unsigned int __user
*)addr
;
394 flush_fp_to_thread(child
);
396 for (i
= 0; i
< 32; i
++) {
397 ret
= put_user(*reg
, tmp
);
406 case PPC_PTRACE_SETFPREGS
: { /* Get FPRs 0 - 31. */
408 unsigned long *reg
= &((unsigned long *)child
->thread
.fpr
)[0];
409 unsigned int __user
*tmp
= (unsigned int __user
*)addr
;
411 flush_fp_to_thread(child
);
413 for (i
= 0; i
< 32; i
++) {
414 ret
= get_user(*reg
, tmp
);
423 case PTRACE_GETEVENTMSG
:
424 ret
= put_user(child
->ptrace_message
, (unsigned int __user
*) data
);
427 #ifdef CONFIG_ALTIVEC
428 case PTRACE_GETVRREGS
:
429 /* Get the child altivec register state. */
430 flush_altivec_to_thread(child
);
431 ret
= get_vrregs((unsigned long __user
*)data
, child
);
434 case PTRACE_SETVRREGS
:
435 /* Set the child altivec register state. */
436 flush_altivec_to_thread(child
);
437 ret
= set_vrregs(child
, (unsigned long __user
*)data
);
442 ret
= ptrace_request(child
, request
, addr
, data
);
446 put_task_struct(child
);