2 * linux/arch/arm26/kernel/ptrace.c
5 * edited by Linus Torvalds
6 * ARM modifications Copyright (C) 2000 Russell King
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/smp.h>
16 #include <linux/smp_lock.h>
17 #include <linux/ptrace.h>
18 #include <linux/user.h>
19 #include <linux/security.h>
20 #include <linux/signal.h>
22 #include <asm/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 //#include <asm/processor.h>
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
37 * Breakpoint SWI instruction: SWI &9F0001
39 #define BREAKINST_ARM 0xef9f0001
42 * this routine will get a word off of the processes privileged stack.
43 * the offset is how far from the base addr as stored in the THREAD.
44 * this routine assumes that all the privileged stacks are in our
47 static inline long get_user_reg(struct task_struct
*task
, int offset
)
49 return task_pt_regs(task
)->uregs
[offset
];
53 * this routine will put a word on the processes privileged stack.
54 * the offset is how far from the base addr as stored in the THREAD.
55 * this routine assumes that all the privileged stacks are in our
59 put_user_reg(struct task_struct
*task
, int offset
, long data
)
61 struct pt_regs newregs
, *regs
= task_pt_regs(task
);
65 newregs
.uregs
[offset
] = data
;
67 if (valid_user_regs(&newregs
)) {
68 regs
->uregs
[offset
] = data
;
76 read_u32(struct task_struct
*task
, unsigned long addr
, u32
*res
)
80 ret
= access_process_vm(task
, addr
, res
, sizeof(*res
), 0);
82 return ret
== sizeof(*res
) ? 0 : -EIO
;
86 read_instr(struct task_struct
*task
, unsigned long addr
, u32
*res
)
90 ret
= access_process_vm(task
, addr
& ~3, &val
, sizeof(val
), 0);
91 ret
= ret
== sizeof(val
) ? 0 : -EIO
;
97 * Get value of register `rn' (in the instruction)
100 ptrace_getrn(struct task_struct
*child
, unsigned long insn
)
102 unsigned int reg
= (insn
>> 16) & 15;
105 val
= get_user_reg(child
, reg
);
107 val
= pc_pointer(val
+ 8); //FIXME - correct for arm26?
113 * Get value of operand 2 (in an ALU instruction)
116 ptrace_getaluop2(struct task_struct
*child
, unsigned long insn
)
122 if (insn
& 1 << 25) {
124 shift
= (insn
>> 8) & 15;
127 val
= get_user_reg (child
, insn
& 15);
130 shift
= (int)get_user_reg (child
, (insn
>> 8) & 15);
132 shift
= (insn
>> 7) & 31;
134 type
= (insn
>> 5) & 3;
138 case 0: val
<<= shift
; break;
139 case 1: val
>>= shift
; break;
141 val
= (((signed long)val
) >> shift
);
144 val
= (val
>> shift
) | (val
<< (32 - shift
));
151 * Get value of operand 2 (in a LDR instruction)
154 ptrace_getldrop2(struct task_struct
*child
, unsigned long insn
)
160 val
= get_user_reg(child
, insn
& 15);
161 shift
= (insn
>> 7) & 31;
162 type
= (insn
>> 5) & 3;
165 case 0: val
<<= shift
; break;
166 case 1: val
>>= shift
; break;
168 val
= (((signed long)val
) >> shift
);
171 val
= (val
>> shift
) | (val
<< (32 - shift
));
177 #define OP_MASK 0x01e00000
178 #define OP_AND 0x00000000
179 #define OP_EOR 0x00200000
180 #define OP_SUB 0x00400000
181 #define OP_RSB 0x00600000
182 #define OP_ADD 0x00800000
183 #define OP_ADC 0x00a00000
184 #define OP_SBC 0x00c00000
185 #define OP_RSC 0x00e00000
186 #define OP_ORR 0x01800000
187 #define OP_MOV 0x01a00000
188 #define OP_BIC 0x01c00000
189 #define OP_MVN 0x01e00000
192 get_branch_address(struct task_struct
*child
, unsigned long pc
, unsigned long insn
)
196 switch (insn
& 0x0e000000) {
202 long aluop1
, aluop2
, ccbit
;
204 if ((insn
& 0xf000) != 0xf000)
207 aluop1
= ptrace_getrn(child
, insn
);
208 aluop2
= ptrace_getaluop2(child
, insn
);
209 ccbit
= get_user_reg(child
, REG_PSR
) & PSR_C_BIT
? 1 : 0;
211 switch (insn
& OP_MASK
) {
212 case OP_AND
: alt
= aluop1
& aluop2
; break;
213 case OP_EOR
: alt
= aluop1
^ aluop2
; break;
214 case OP_SUB
: alt
= aluop1
- aluop2
; break;
215 case OP_RSB
: alt
= aluop2
- aluop1
; break;
216 case OP_ADD
: alt
= aluop1
+ aluop2
; break;
217 case OP_ADC
: alt
= aluop1
+ aluop2
+ ccbit
; break;
218 case OP_SBC
: alt
= aluop1
- aluop2
+ ccbit
; break;
219 case OP_RSC
: alt
= aluop2
- aluop1
+ ccbit
; break;
220 case OP_ORR
: alt
= aluop1
| aluop2
; break;
221 case OP_MOV
: alt
= aluop2
; break;
222 case OP_BIC
: alt
= aluop1
& ~aluop2
; break;
223 case OP_MVN
: alt
= ~aluop2
; break;
233 if ((insn
& 0x0010f000) == 0x0010f000) {
236 base
= ptrace_getrn(child
, insn
);
237 if (insn
& 1 << 24) {
240 if (insn
& 0x02000000)
241 aluop2
= ptrace_getldrop2(child
, insn
);
243 aluop2
= insn
& 0xfff;
250 if (read_u32(child
, base
, &alt
) == 0)
251 alt
= pc_pointer(alt
);
259 if ((insn
& 0x00108000) == 0x00108000) {
261 unsigned int nr_regs
;
263 if (insn
& (1 << 23)) {
264 nr_regs
= hweight16(insn
& 65535) << 2;
266 if (!(insn
& (1 << 24)))
269 if (insn
& (1 << 24))
275 base
= ptrace_getrn(child
, insn
);
277 if (read_u32(child
, base
+ nr_regs
, &alt
) == 0)
278 alt
= pc_pointer(alt
);
288 /* It's a branch/branch link: instead of trying to
289 * figure out whether the branch will be taken or not,
290 * we'll put a breakpoint at both locations. This is
291 * simpler, more reliable, and probably not a whole lot
292 * slower than the alternative approach of emulating the
295 displ
= (insn
& 0x00ffffff) << 8;
296 displ
= (displ
>> 6) + 8;
297 if (displ
!= 0 && displ
!= 4)
307 swap_insn(struct task_struct
*task
, unsigned long addr
,
308 void *old_insn
, void *new_insn
, int size
)
312 ret
= access_process_vm(task
, addr
, old_insn
, size
, 0);
314 ret
= access_process_vm(task
, addr
, new_insn
, size
, 1);
319 add_breakpoint(struct task_struct
*task
, struct debug_info
*dbg
, unsigned long addr
)
321 int nr
= dbg
->nsaved
;
324 u32 new_insn
= BREAKINST_ARM
;
327 res
= swap_insn(task
, addr
, &dbg
->bp
[nr
].insn
, &new_insn
, 4);
330 dbg
->bp
[nr
].address
= addr
;
334 printk(KERN_ERR
"ptrace: too many breakpoints\n");
338 * Clear one breakpoint in the user program. We copy what the hardware
339 * does and use bit 0 of the address to indicate whether this is a Thumb
340 * breakpoint or an ARM breakpoint.
342 static void clear_breakpoint(struct task_struct
*task
, struct debug_entry
*bp
)
344 unsigned long addr
= bp
->address
;
348 ret
= swap_insn(task
, addr
& ~3, &old_insn
,
351 if (ret
!= 4 || old_insn
!= BREAKINST_ARM
)
352 printk(KERN_ERR
"%s:%d: corrupted ARM breakpoint at "
353 "0x%08lx (0x%08x)\n", task
->comm
, task
->pid
,
357 void ptrace_set_bpt(struct task_struct
*child
)
359 struct pt_regs
*regs
;
364 regs
= task_pt_regs(child
);
365 pc
= instruction_pointer(regs
);
367 res
= read_instr(child
, pc
, &insn
);
369 struct debug_info
*dbg
= &child
->thread
.debug
;
374 alt
= get_branch_address(child
, pc
, insn
);
376 add_breakpoint(child
, dbg
, alt
);
379 * Note that we ignore the result of setting the above
380 * breakpoint since it may fail. When it does, this is
381 * not so much an error, but a forewarning that we may
382 * be receiving a prefetch abort shortly.
384 * If we don't set this breakpoint here, then we can
385 * lose control of the thread during single stepping.
387 if (!alt
|| predicate(insn
) != PREDICATE_ALWAYS
)
388 add_breakpoint(child
, dbg
, pc
+ 4);
393 * Ensure no single-step breakpoint is pending. Returns non-zero
394 * value if child was being single-stepped.
396 void ptrace_cancel_bpt(struct task_struct
*child
)
398 int i
, nsaved
= child
->thread
.debug
.nsaved
;
400 child
->thread
.debug
.nsaved
= 0;
403 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved
);
407 for (i
= 0; i
< nsaved
; i
++)
408 clear_breakpoint(child
, &child
->thread
.debug
.bp
[i
]);
412 * Called by kernel/ptrace.c when detaching..
414 * Make sure the single step bit is not set.
416 void ptrace_disable(struct task_struct
*child
)
418 child
->ptrace
&= ~PT_SINGLESTEP
;
419 ptrace_cancel_bpt(child
);
423 * Handle hitting a breakpoint.
425 void ptrace_break(struct task_struct
*tsk
, struct pt_regs
*regs
)
430 * The PC is always left pointing at the next instruction. Fix this.
434 if (tsk
->thread
.debug
.nsaved
== 0)
435 printk(KERN_ERR
"ptrace: bogus breakpoint trap\n");
437 ptrace_cancel_bpt(tsk
);
439 info
.si_signo
= SIGTRAP
;
441 info
.si_code
= TRAP_BRKPT
;
442 info
.si_addr
= (void *)instruction_pointer(regs
) - 4;
444 force_sig_info(SIGTRAP
, &info
, tsk
);
448 * Read the word at offset "off" into the "struct user". We
449 * actually access the pt_regs stored on the kernel stack.
451 static int ptrace_read_user(struct task_struct
*tsk
, unsigned long off
,
456 if (off
& 3 || off
>= sizeof(struct user
))
460 if (off
< sizeof(struct pt_regs
))
461 tmp
= get_user_reg(tsk
, off
>> 2);
463 return put_user(tmp
, ret
);
467 * Write the word at offset "off" into "struct user". We
468 * actually access the pt_regs stored on the kernel stack.
470 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long off
,
473 if (off
& 3 || off
>= sizeof(struct user
))
476 if (off
>= sizeof(struct pt_regs
))
479 return put_user_reg(tsk
, off
>> 2, val
);
483 * Get all user integer registers.
485 static int ptrace_getregs(struct task_struct
*tsk
, void *uregs
)
487 struct pt_regs
*regs
= task_pt_regs(tsk
);
489 return copy_to_user(uregs
, regs
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
493 * Set all user integer registers.
495 static int ptrace_setregs(struct task_struct
*tsk
, void *uregs
)
497 struct pt_regs newregs
;
501 if (copy_from_user(&newregs
, uregs
, sizeof(struct pt_regs
)) == 0) {
502 struct pt_regs
*regs
= task_pt_regs(tsk
);
505 if (valid_user_regs(&newregs
)) {
515 * Get the child FPU state.
517 static int ptrace_getfpregs(struct task_struct
*tsk
, void *ufp
)
519 return copy_to_user(ufp
, &task_thread_info(tsk
)->fpstate
,
520 sizeof(struct user_fp
)) ? -EFAULT
: 0;
524 * Set the child FPU state.
526 static int ptrace_setfpregs(struct task_struct
*tsk
, void *ufp
)
528 set_stopped_child_used_math(tsk
);
529 return copy_from_user(&task_thread_info(tsk
)->fpstate
, ufp
,
530 sizeof(struct user_fp
)) ? -EFAULT
: 0;
533 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
540 * read word at location "addr" in the child process.
542 case PTRACE_PEEKTEXT
:
543 case PTRACE_PEEKDATA
:
544 ret
= access_process_vm(child
, addr
, &tmp
,
545 sizeof(unsigned long), 0);
546 if (ret
== sizeof(unsigned long))
547 ret
= put_user(tmp
, (unsigned long *) data
);
553 ret
= ptrace_read_user(child
, addr
, (unsigned long *)data
);
557 * write the word at location addr.
559 case PTRACE_POKETEXT
:
560 case PTRACE_POKEDATA
:
561 ret
= access_process_vm(child
, addr
, &data
,
562 sizeof(unsigned long), 1);
563 if (ret
== sizeof(unsigned long))
570 ret
= ptrace_write_user(child
, addr
, data
);
574 * continue/restart and stop at next (return from) syscall
579 if (!valid_signal(data
))
581 if (request
== PTRACE_SYSCALL
)
582 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
584 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
585 child
->exit_code
= data
;
586 /* make sure single-step breakpoint is gone. */
587 child
->ptrace
&= ~PT_SINGLESTEP
;
588 ptrace_cancel_bpt(child
);
589 wake_up_process(child
);
594 * make the child exit. Best I can do is send it a sigkill.
595 * perhaps it should be put in the status that it wants to
599 /* make sure single-step breakpoint is gone. */
600 child
->ptrace
&= ~PT_SINGLESTEP
;
601 ptrace_cancel_bpt(child
);
602 if (child
->exit_state
!= EXIT_ZOMBIE
) {
603 child
->exit_code
= SIGKILL
;
604 wake_up_process(child
);
610 * execute single instruction.
612 case PTRACE_SINGLESTEP
:
614 if (!valid_signal(data
))
616 child
->ptrace
|= PT_SINGLESTEP
;
617 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
618 child
->exit_code
= data
;
619 /* give it a chance to run. */
620 wake_up_process(child
);
625 ret
= ptrace_detach(child
, data
);
629 ret
= ptrace_getregs(child
, (void *)data
);
633 ret
= ptrace_setregs(child
, (void *)data
);
636 case PTRACE_GETFPREGS
:
637 ret
= ptrace_getfpregs(child
, (void *)data
);
640 case PTRACE_SETFPREGS
:
641 ret
= ptrace_setfpregs(child
, (void *)data
);
645 ret
= ptrace_request(child
, request
, addr
, data
);
652 asmlinkage
void syscall_trace(int why
, struct pt_regs
*regs
)
656 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
658 if (!(current
->ptrace
& PT_PTRACED
))
662 * Save IP. IP is used to denote syscall entry/exit:
663 * IP = 0 -> entry, = 1 -> exit
668 /* the 0x80 provides a way for the tracing parent to distinguish
669 between a syscall stop and SIGTRAP delivery */
670 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
673 * this isn't the same as continuing with a signal, but it will do
674 * for normal use. strace only continues with a signal if the
675 * stopping signal is not SIGTRAP. -brl
677 if (current
->exit_code
) {
678 send_sig(current
->exit_code
, current
, 1);
679 current
->exit_code
= 0;