2 * linux/arch/arm/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/ptrace.h>
17 #include <linux/user.h>
18 #include <linux/security.h>
19 #include <linux/init.h>
20 #include <linux/signal.h>
21 #include <linux/uaccess.h>
23 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/traps.h>
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
38 * Breakpoint SWI instruction: SWI &9F0001
40 #define BREAKINST_ARM 0xef9f0001
41 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
44 * New breakpoints - use an undefined instruction. The ARM architecture
45 * reference manual guarantees that the following instruction space
46 * will produce an undefined instruction exception on all CPUs:
48 * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
49 * Thumb: 1101 1110 xxxx xxxx
51 #define BREAKINST_ARM 0xe7f001f0
52 #define BREAKINST_THUMB 0xde01
56 * this routine will get a word off of the processes privileged stack.
57 * the offset is how far from the base addr as stored in the THREAD.
58 * this routine assumes that all the privileged stacks are in our
61 static inline long get_user_reg(struct task_struct
*task
, int offset
)
63 return task_pt_regs(task
)->uregs
[offset
];
67 * this routine will put a word on the processes privileged stack.
68 * the offset is how far from the base addr as stored in the THREAD.
69 * this routine assumes that all the privileged stacks are in our
73 put_user_reg(struct task_struct
*task
, int offset
, long data
)
75 struct pt_regs newregs
, *regs
= task_pt_regs(task
);
79 newregs
.uregs
[offset
] = data
;
81 if (valid_user_regs(&newregs
)) {
82 regs
->uregs
[offset
] = data
;
90 read_u32(struct task_struct
*task
, unsigned long addr
, u32
*res
)
94 ret
= access_process_vm(task
, addr
, res
, sizeof(*res
), 0);
96 return ret
== sizeof(*res
) ? 0 : -EIO
;
100 read_instr(struct task_struct
*task
, unsigned long addr
, u32
*res
)
106 ret
= access_process_vm(task
, addr
& ~1, &val
, sizeof(val
), 0);
107 ret
= ret
== sizeof(val
) ? 0 : -EIO
;
111 ret
= access_process_vm(task
, addr
& ~3, &val
, sizeof(val
), 0);
112 ret
= ret
== sizeof(val
) ? 0 : -EIO
;
119 * Get value of register `rn' (in the instruction)
122 ptrace_getrn(struct task_struct
*child
, unsigned long insn
)
124 unsigned int reg
= (insn
>> 16) & 15;
127 val
= get_user_reg(child
, reg
);
135 * Get value of operand 2 (in an ALU instruction)
138 ptrace_getaluop2(struct task_struct
*child
, unsigned long insn
)
144 if (insn
& 1 << 25) {
146 shift
= (insn
>> 8) & 15;
149 val
= get_user_reg (child
, insn
& 15);
152 shift
= (int)get_user_reg (child
, (insn
>> 8) & 15);
154 shift
= (insn
>> 7) & 31;
156 type
= (insn
>> 5) & 3;
160 case 0: val
<<= shift
; break;
161 case 1: val
>>= shift
; break;
163 val
= (((signed long)val
) >> shift
);
166 val
= (val
>> shift
) | (val
<< (32 - shift
));
173 * Get value of operand 2 (in a LDR instruction)
176 ptrace_getldrop2(struct task_struct
*child
, unsigned long insn
)
182 val
= get_user_reg(child
, insn
& 15);
183 shift
= (insn
>> 7) & 31;
184 type
= (insn
>> 5) & 3;
187 case 0: val
<<= shift
; break;
188 case 1: val
>>= shift
; break;
190 val
= (((signed long)val
) >> shift
);
193 val
= (val
>> shift
) | (val
<< (32 - shift
));
199 #define OP_MASK 0x01e00000
200 #define OP_AND 0x00000000
201 #define OP_EOR 0x00200000
202 #define OP_SUB 0x00400000
203 #define OP_RSB 0x00600000
204 #define OP_ADD 0x00800000
205 #define OP_ADC 0x00a00000
206 #define OP_SBC 0x00c00000
207 #define OP_RSC 0x00e00000
208 #define OP_ORR 0x01800000
209 #define OP_MOV 0x01a00000
210 #define OP_BIC 0x01c00000
211 #define OP_MVN 0x01e00000
214 get_branch_address(struct task_struct
*child
, unsigned long pc
, unsigned long insn
)
218 switch (insn
& 0x0e000000) {
224 long aluop1
, aluop2
, ccbit
;
226 if ((insn
& 0x0fffffd0) == 0x012fff10) {
230 alt
= get_user_reg(child
, insn
& 15);
235 if ((insn
& 0xf000) != 0xf000)
238 aluop1
= ptrace_getrn(child
, insn
);
239 aluop2
= ptrace_getaluop2(child
, insn
);
240 ccbit
= get_user_reg(child
, REG_PSR
) & PSR_C_BIT
? 1 : 0;
242 switch (insn
& OP_MASK
) {
243 case OP_AND
: alt
= aluop1
& aluop2
; break;
244 case OP_EOR
: alt
= aluop1
^ aluop2
; break;
245 case OP_SUB
: alt
= aluop1
- aluop2
; break;
246 case OP_RSB
: alt
= aluop2
- aluop1
; break;
247 case OP_ADD
: alt
= aluop1
+ aluop2
; break;
248 case OP_ADC
: alt
= aluop1
+ aluop2
+ ccbit
; break;
249 case OP_SBC
: alt
= aluop1
- aluop2
+ ccbit
; break;
250 case OP_RSC
: alt
= aluop2
- aluop1
+ ccbit
; break;
251 case OP_ORR
: alt
= aluop1
| aluop2
; break;
252 case OP_MOV
: alt
= aluop2
; break;
253 case OP_BIC
: alt
= aluop1
& ~aluop2
; break;
254 case OP_MVN
: alt
= ~aluop2
; break;
264 if ((insn
& 0x0010f000) == 0x0010f000) {
267 base
= ptrace_getrn(child
, insn
);
268 if (insn
& 1 << 24) {
271 if (insn
& 0x02000000)
272 aluop2
= ptrace_getldrop2(child
, insn
);
274 aluop2
= insn
& 0xfff;
281 read_u32(child
, base
, &alt
);
289 if ((insn
& 0x00108000) == 0x00108000) {
291 unsigned int nr_regs
;
293 if (insn
& (1 << 23)) {
294 nr_regs
= hweight16(insn
& 65535) << 2;
296 if (!(insn
& (1 << 24)))
299 if (insn
& (1 << 24))
305 base
= ptrace_getrn(child
, insn
);
307 read_u32(child
, base
+ nr_regs
, &alt
);
317 /* It's a branch/branch link: instead of trying to
318 * figure out whether the branch will be taken or not,
319 * we'll put a breakpoint at both locations. This is
320 * simpler, more reliable, and probably not a whole lot
321 * slower than the alternative approach of emulating the
324 displ
= (insn
& 0x00ffffff) << 8;
325 displ
= (displ
>> 6) + 8;
326 if (displ
!= 0 && displ
!= 4)
336 swap_insn(struct task_struct
*task
, unsigned long addr
,
337 void *old_insn
, void *new_insn
, int size
)
341 ret
= access_process_vm(task
, addr
, old_insn
, size
, 0);
343 ret
= access_process_vm(task
, addr
, new_insn
, size
, 1);
348 add_breakpoint(struct task_struct
*task
, struct debug_info
*dbg
, unsigned long addr
)
350 int nr
= dbg
->nsaved
;
353 u32 new_insn
= BREAKINST_ARM
;
356 res
= swap_insn(task
, addr
, &dbg
->bp
[nr
].insn
, &new_insn
, 4);
359 dbg
->bp
[nr
].address
= addr
;
363 printk(KERN_ERR
"ptrace: too many breakpoints\n");
367 * Clear one breakpoint in the user program. We copy what the hardware
368 * does and use bit 0 of the address to indicate whether this is a Thumb
369 * breakpoint or an ARM breakpoint.
371 static void clear_breakpoint(struct task_struct
*task
, struct debug_entry
*bp
)
373 unsigned long addr
= bp
->address
;
374 union debug_insn old_insn
;
378 ret
= swap_insn(task
, addr
& ~1, &old_insn
.thumb
,
381 if (ret
!= 2 || old_insn
.thumb
!= BREAKINST_THUMB
)
382 printk(KERN_ERR
"%s:%d: corrupted Thumb breakpoint at "
383 "0x%08lx (0x%04x)\n", task
->comm
,
384 task_pid_nr(task
), addr
, old_insn
.thumb
);
386 ret
= swap_insn(task
, addr
& ~3, &old_insn
.arm
,
389 if (ret
!= 4 || old_insn
.arm
!= BREAKINST_ARM
)
390 printk(KERN_ERR
"%s:%d: corrupted ARM breakpoint at "
391 "0x%08lx (0x%08x)\n", task
->comm
,
392 task_pid_nr(task
), addr
, old_insn
.arm
);
396 void ptrace_set_bpt(struct task_struct
*child
)
398 struct pt_regs
*regs
;
403 regs
= task_pt_regs(child
);
404 pc
= instruction_pointer(regs
);
406 if (thumb_mode(regs
)) {
407 printk(KERN_WARNING
"ptrace: can't handle thumb mode\n");
411 res
= read_instr(child
, pc
, &insn
);
413 struct debug_info
*dbg
= &child
->thread
.debug
;
418 alt
= get_branch_address(child
, pc
, insn
);
420 add_breakpoint(child
, dbg
, alt
);
423 * Note that we ignore the result of setting the above
424 * breakpoint since it may fail. When it does, this is
425 * not so much an error, but a forewarning that we may
426 * be receiving a prefetch abort shortly.
428 * If we don't set this breakpoint here, then we can
429 * lose control of the thread during single stepping.
431 if (!alt
|| predicate(insn
) != PREDICATE_ALWAYS
)
432 add_breakpoint(child
, dbg
, pc
+ 4);
437 * Ensure no single-step breakpoint is pending. Returns non-zero
438 * value if child was being single-stepped.
440 void ptrace_cancel_bpt(struct task_struct
*child
)
442 int i
, nsaved
= child
->thread
.debug
.nsaved
;
444 child
->thread
.debug
.nsaved
= 0;
447 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved
);
451 for (i
= 0; i
< nsaved
; i
++)
452 clear_breakpoint(child
, &child
->thread
.debug
.bp
[i
]);
455 void user_disable_single_step(struct task_struct
*task
)
457 task
->ptrace
&= ~PT_SINGLESTEP
;
458 ptrace_cancel_bpt(task
);
461 void user_enable_single_step(struct task_struct
*task
)
463 task
->ptrace
|= PT_SINGLESTEP
;
467 * Called by kernel/ptrace.c when detaching..
469 void ptrace_disable(struct task_struct
*child
)
471 user_disable_single_step(child
);
475 * Handle hitting a breakpoint.
477 void ptrace_break(struct task_struct
*tsk
, struct pt_regs
*regs
)
481 ptrace_cancel_bpt(tsk
);
483 info
.si_signo
= SIGTRAP
;
485 info
.si_code
= TRAP_BRKPT
;
486 info
.si_addr
= (void __user
*)instruction_pointer(regs
);
488 force_sig_info(SIGTRAP
, &info
, tsk
);
491 static int break_trap(struct pt_regs
*regs
, unsigned int instr
)
493 ptrace_break(current
, regs
);
497 static struct undef_hook arm_break_hook
= {
498 .instr_mask
= 0x0fffffff,
499 .instr_val
= 0x07f001f0,
500 .cpsr_mask
= PSR_T_BIT
,
505 static struct undef_hook thumb_break_hook
= {
506 .instr_mask
= 0xffff,
508 .cpsr_mask
= PSR_T_BIT
,
509 .cpsr_val
= PSR_T_BIT
,
513 static int thumb2_break_trap(struct pt_regs
*regs
, unsigned int instr
)
518 /* Check the second half of the instruction. */
519 pc
= (void __user
*)(instruction_pointer(regs
) + 2);
521 if (processor_mode(regs
) == SVC_MODE
) {
522 instr2
= *(u16
*) pc
;
524 get_user(instr2
, (u16 __user
*)pc
);
527 if (instr2
== 0xa000) {
528 ptrace_break(current
, regs
);
535 static struct undef_hook thumb2_break_hook
= {
536 .instr_mask
= 0xffff,
538 .cpsr_mask
= PSR_T_BIT
,
539 .cpsr_val
= PSR_T_BIT
,
540 .fn
= thumb2_break_trap
,
543 static int __init
ptrace_break_init(void)
545 register_undef_hook(&arm_break_hook
);
546 register_undef_hook(&thumb_break_hook
);
547 register_undef_hook(&thumb2_break_hook
);
551 core_initcall(ptrace_break_init
);
554 * Read the word at offset "off" into the "struct user". We
555 * actually access the pt_regs stored on the kernel stack.
557 static int ptrace_read_user(struct task_struct
*tsk
, unsigned long off
,
558 unsigned long __user
*ret
)
562 if (off
& 3 || off
>= sizeof(struct user
))
566 if (off
== PT_TEXT_ADDR
)
567 tmp
= tsk
->mm
->start_code
;
568 else if (off
== PT_DATA_ADDR
)
569 tmp
= tsk
->mm
->start_data
;
570 else if (off
== PT_TEXT_END_ADDR
)
571 tmp
= tsk
->mm
->end_code
;
572 else if (off
< sizeof(struct pt_regs
))
573 tmp
= get_user_reg(tsk
, off
>> 2);
575 return put_user(tmp
, ret
);
579 * Write the word at offset "off" into "struct user". We
580 * actually access the pt_regs stored on the kernel stack.
582 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long off
,
585 if (off
& 3 || off
>= sizeof(struct user
))
588 if (off
>= sizeof(struct pt_regs
))
591 return put_user_reg(tsk
, off
>> 2, val
);
595 * Get all user integer registers.
597 static int ptrace_getregs(struct task_struct
*tsk
, void __user
*uregs
)
599 struct pt_regs
*regs
= task_pt_regs(tsk
);
601 return copy_to_user(uregs
, regs
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
605 * Set all user integer registers.
607 static int ptrace_setregs(struct task_struct
*tsk
, void __user
*uregs
)
609 struct pt_regs newregs
;
613 if (copy_from_user(&newregs
, uregs
, sizeof(struct pt_regs
)) == 0) {
614 struct pt_regs
*regs
= task_pt_regs(tsk
);
617 if (valid_user_regs(&newregs
)) {
627 * Get the child FPU state.
629 static int ptrace_getfpregs(struct task_struct
*tsk
, void __user
*ufp
)
631 return copy_to_user(ufp
, &task_thread_info(tsk
)->fpstate
,
632 sizeof(struct user_fp
)) ? -EFAULT
: 0;
636 * Set the child FPU state.
638 static int ptrace_setfpregs(struct task_struct
*tsk
, void __user
*ufp
)
640 struct thread_info
*thread
= task_thread_info(tsk
);
641 thread
->used_cp
[1] = thread
->used_cp
[2] = 1;
642 return copy_from_user(&thread
->fpstate
, ufp
,
643 sizeof(struct user_fp
)) ? -EFAULT
: 0;
649 * Get the child iWMMXt state.
651 static int ptrace_getwmmxregs(struct task_struct
*tsk
, void __user
*ufp
)
653 struct thread_info
*thread
= task_thread_info(tsk
);
655 if (!test_ti_thread_flag(thread
, TIF_USING_IWMMXT
))
657 iwmmxt_task_disable(thread
); /* force it to ram */
658 return copy_to_user(ufp
, &thread
->fpstate
.iwmmxt
, IWMMXT_SIZE
)
663 * Set the child iWMMXt state.
665 static int ptrace_setwmmxregs(struct task_struct
*tsk
, void __user
*ufp
)
667 struct thread_info
*thread
= task_thread_info(tsk
);
669 if (!test_ti_thread_flag(thread
, TIF_USING_IWMMXT
))
671 iwmmxt_task_release(thread
); /* force a reload */
672 return copy_from_user(&thread
->fpstate
.iwmmxt
, ufp
, IWMMXT_SIZE
)
680 * Get the child Crunch state.
682 static int ptrace_getcrunchregs(struct task_struct
*tsk
, void __user
*ufp
)
684 struct thread_info
*thread
= task_thread_info(tsk
);
686 crunch_task_disable(thread
); /* force it to ram */
687 return copy_to_user(ufp
, &thread
->crunchstate
, CRUNCH_SIZE
)
692 * Set the child Crunch state.
694 static int ptrace_setcrunchregs(struct task_struct
*tsk
, void __user
*ufp
)
696 struct thread_info
*thread
= task_thread_info(tsk
);
698 crunch_task_release(thread
); /* force a reload */
699 return copy_from_user(&thread
->crunchstate
, ufp
, CRUNCH_SIZE
)
706 * Get the child VFP state.
708 static int ptrace_getvfpregs(struct task_struct
*tsk
, void __user
*data
)
710 struct thread_info
*thread
= task_thread_info(tsk
);
711 union vfp_state
*vfp
= &thread
->vfpstate
;
712 struct user_vfp __user
*ufp
= data
;
714 vfp_sync_hwstate(thread
);
716 /* copy the floating point registers */
717 if (copy_to_user(&ufp
->fpregs
, &vfp
->hard
.fpregs
,
718 sizeof(vfp
->hard
.fpregs
)))
721 /* copy the status and control register */
722 if (put_user(vfp
->hard
.fpscr
, &ufp
->fpscr
))
729 * Set the child VFP state.
731 static int ptrace_setvfpregs(struct task_struct
*tsk
, void __user
*data
)
733 struct thread_info
*thread
= task_thread_info(tsk
);
734 union vfp_state
*vfp
= &thread
->vfpstate
;
735 struct user_vfp __user
*ufp
= data
;
737 vfp_sync_hwstate(thread
);
739 /* copy the floating point registers */
740 if (copy_from_user(&vfp
->hard
.fpregs
, &ufp
->fpregs
,
741 sizeof(vfp
->hard
.fpregs
)))
744 /* copy the status and control register */
745 if (get_user(vfp
->hard
.fpscr
, &ufp
->fpscr
))
748 vfp_flush_hwstate(thread
);
754 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
760 ret
= ptrace_read_user(child
, addr
, (unsigned long __user
*)data
);
764 ret
= ptrace_write_user(child
, addr
, data
);
768 ret
= ptrace_getregs(child
, (void __user
*)data
);
772 ret
= ptrace_setregs(child
, (void __user
*)data
);
775 case PTRACE_GETFPREGS
:
776 ret
= ptrace_getfpregs(child
, (void __user
*)data
);
779 case PTRACE_SETFPREGS
:
780 ret
= ptrace_setfpregs(child
, (void __user
*)data
);
784 case PTRACE_GETWMMXREGS
:
785 ret
= ptrace_getwmmxregs(child
, (void __user
*)data
);
788 case PTRACE_SETWMMXREGS
:
789 ret
= ptrace_setwmmxregs(child
, (void __user
*)data
);
793 case PTRACE_GET_THREAD_AREA
:
794 ret
= put_user(task_thread_info(child
)->tp_value
,
795 (unsigned long __user
*) data
);
798 case PTRACE_SET_SYSCALL
:
799 task_thread_info(child
)->syscall
= data
;
804 case PTRACE_GETCRUNCHREGS
:
805 ret
= ptrace_getcrunchregs(child
, (void __user
*)data
);
808 case PTRACE_SETCRUNCHREGS
:
809 ret
= ptrace_setcrunchregs(child
, (void __user
*)data
);
814 case PTRACE_GETVFPREGS
:
815 ret
= ptrace_getvfpregs(child
, (void __user
*)data
);
818 case PTRACE_SETVFPREGS
:
819 ret
= ptrace_setvfpregs(child
, (void __user
*)data
);
824 ret
= ptrace_request(child
, request
, addr
, data
);
831 asmlinkage
int syscall_trace(int why
, struct pt_regs
*regs
, int scno
)
835 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
837 if (!(current
->ptrace
& PT_PTRACED
))
841 * Save IP. IP is used to denote syscall entry/exit:
842 * IP = 0 -> entry, = 1 -> exit
847 current_thread_info()->syscall
= scno
;
849 /* the 0x80 provides a way for the tracing parent to distinguish
850 between a syscall stop and SIGTRAP delivery */
851 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
854 * this isn't the same as continuing with a signal, but it will do
855 * for normal use. strace only continues with a signal if the
856 * stopping signal is not SIGTRAP. -brl
858 if (current
->exit_code
) {
859 send_sig(current
->exit_code
, current
, 1);
860 current
->exit_code
= 0;
864 return current_thread_info()->syscall
;