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>
22 #include <linux/perf_event.h>
23 #include <linux/hw_breakpoint.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #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
55 struct pt_regs_offset
{
60 #define REG_OFFSET_NAME(r) \
61 {.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
62 #define REG_OFFSET_END {.name = NULL, .offset = 0}
64 static const struct pt_regs_offset regoffset_table
[] = {
81 REG_OFFSET_NAME(cpsr
),
82 REG_OFFSET_NAME(ORIG_r0
),
87 * regs_query_register_offset() - query register offset from its name
88 * @name: the name of a register
90 * regs_query_register_offset() returns the offset of a register in struct
91 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
93 int regs_query_register_offset(const char *name
)
95 const struct pt_regs_offset
*roff
;
96 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
97 if (!strcmp(roff
->name
, name
))
103 * regs_query_register_name() - query register name from its offset
104 * @offset: the offset of a register in struct pt_regs.
106 * regs_query_register_name() returns the name of a register from its
107 * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
109 const char *regs_query_register_name(unsigned int offset
)
111 const struct pt_regs_offset
*roff
;
112 for (roff
= regoffset_table
; roff
->name
!= NULL
; roff
++)
113 if (roff
->offset
== offset
)
119 * regs_within_kernel_stack() - check the address in the stack
120 * @regs: pt_regs which contains kernel stack pointer.
121 * @addr: address which is checked.
123 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
124 * If @addr is within the kernel stack, it returns true. If not, returns false.
126 bool regs_within_kernel_stack(struct pt_regs
*regs
, unsigned long addr
)
128 return ((addr
& ~(THREAD_SIZE
- 1)) ==
129 (kernel_stack_pointer(regs
) & ~(THREAD_SIZE
- 1)));
133 * regs_get_kernel_stack_nth() - get Nth entry of the stack
134 * @regs: pt_regs which contains kernel stack pointer.
135 * @n: stack entry number.
137 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
138 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
141 unsigned long regs_get_kernel_stack_nth(struct pt_regs
*regs
, unsigned int n
)
143 unsigned long *addr
= (unsigned long *)kernel_stack_pointer(regs
);
145 if (regs_within_kernel_stack(regs
, (unsigned long)addr
))
152 * this routine will get a word off of the processes privileged stack.
153 * the offset is how far from the base addr as stored in the THREAD.
154 * this routine assumes that all the privileged stacks are in our
157 static inline long get_user_reg(struct task_struct
*task
, int offset
)
159 return task_pt_regs(task
)->uregs
[offset
];
163 * this routine will put a word on the processes privileged stack.
164 * the offset is how far from the base addr as stored in the THREAD.
165 * this routine assumes that all the privileged stacks are in our
169 put_user_reg(struct task_struct
*task
, int offset
, long data
)
171 struct pt_regs newregs
, *regs
= task_pt_regs(task
);
175 newregs
.uregs
[offset
] = data
;
177 if (valid_user_regs(&newregs
)) {
178 regs
->uregs
[offset
] = data
;
186 * Called by kernel/ptrace.c when detaching..
188 void ptrace_disable(struct task_struct
*child
)
194 * Handle hitting a breakpoint.
196 void ptrace_break(struct task_struct
*tsk
, struct pt_regs
*regs
)
200 info
.si_signo
= SIGTRAP
;
202 info
.si_code
= TRAP_BRKPT
;
203 info
.si_addr
= (void __user
*)instruction_pointer(regs
);
205 force_sig_info(SIGTRAP
, &info
, tsk
);
208 static int break_trap(struct pt_regs
*regs
, unsigned int instr
)
210 ptrace_break(current
, regs
);
214 static struct undef_hook arm_break_hook
= {
215 .instr_mask
= 0x0fffffff,
216 .instr_val
= 0x07f001f0,
217 .cpsr_mask
= PSR_T_BIT
,
222 static struct undef_hook thumb_break_hook
= {
223 .instr_mask
= 0xffff,
225 .cpsr_mask
= PSR_T_BIT
,
226 .cpsr_val
= PSR_T_BIT
,
230 static int thumb2_break_trap(struct pt_regs
*regs
, unsigned int instr
)
235 /* Check the second half of the instruction. */
236 pc
= (void __user
*)(instruction_pointer(regs
) + 2);
238 if (processor_mode(regs
) == SVC_MODE
) {
239 instr2
= *(u16
*) pc
;
241 get_user(instr2
, (u16 __user
*)pc
);
244 if (instr2
== 0xa000) {
245 ptrace_break(current
, regs
);
252 static struct undef_hook thumb2_break_hook
= {
253 .instr_mask
= 0xffff,
255 .cpsr_mask
= PSR_T_BIT
,
256 .cpsr_val
= PSR_T_BIT
,
257 .fn
= thumb2_break_trap
,
260 static int __init
ptrace_break_init(void)
262 register_undef_hook(&arm_break_hook
);
263 register_undef_hook(&thumb_break_hook
);
264 register_undef_hook(&thumb2_break_hook
);
268 core_initcall(ptrace_break_init
);
271 * Read the word at offset "off" into the "struct user". We
272 * actually access the pt_regs stored on the kernel stack.
274 static int ptrace_read_user(struct task_struct
*tsk
, unsigned long off
,
275 unsigned long __user
*ret
)
279 if (off
& 3 || off
>= sizeof(struct user
))
283 if (off
== PT_TEXT_ADDR
)
284 tmp
= tsk
->mm
->start_code
;
285 else if (off
== PT_DATA_ADDR
)
286 tmp
= tsk
->mm
->start_data
;
287 else if (off
== PT_TEXT_END_ADDR
)
288 tmp
= tsk
->mm
->end_code
;
289 else if (off
< sizeof(struct pt_regs
))
290 tmp
= get_user_reg(tsk
, off
>> 2);
292 return put_user(tmp
, ret
);
296 * Write the word at offset "off" into "struct user". We
297 * actually access the pt_regs stored on the kernel stack.
299 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long off
,
302 if (off
& 3 || off
>= sizeof(struct user
))
305 if (off
>= sizeof(struct pt_regs
))
308 return put_user_reg(tsk
, off
>> 2, val
);
312 * Get all user integer registers.
314 static int ptrace_getregs(struct task_struct
*tsk
, void __user
*uregs
)
316 struct pt_regs
*regs
= task_pt_regs(tsk
);
318 return copy_to_user(uregs
, regs
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
322 * Set all user integer registers.
324 static int ptrace_setregs(struct task_struct
*tsk
, void __user
*uregs
)
326 struct pt_regs newregs
;
330 if (copy_from_user(&newregs
, uregs
, sizeof(struct pt_regs
)) == 0) {
331 struct pt_regs
*regs
= task_pt_regs(tsk
);
334 if (valid_user_regs(&newregs
)) {
344 * Get the child FPU state.
346 static int ptrace_getfpregs(struct task_struct
*tsk
, void __user
*ufp
)
348 return copy_to_user(ufp
, &task_thread_info(tsk
)->fpstate
,
349 sizeof(struct user_fp
)) ? -EFAULT
: 0;
353 * Set the child FPU state.
355 static int ptrace_setfpregs(struct task_struct
*tsk
, void __user
*ufp
)
357 struct thread_info
*thread
= task_thread_info(tsk
);
358 thread
->used_cp
[1] = thread
->used_cp
[2] = 1;
359 return copy_from_user(&thread
->fpstate
, ufp
,
360 sizeof(struct user_fp
)) ? -EFAULT
: 0;
366 * Get the child iWMMXt state.
368 static int ptrace_getwmmxregs(struct task_struct
*tsk
, void __user
*ufp
)
370 struct thread_info
*thread
= task_thread_info(tsk
);
372 if (!test_ti_thread_flag(thread
, TIF_USING_IWMMXT
))
374 iwmmxt_task_disable(thread
); /* force it to ram */
375 return copy_to_user(ufp
, &thread
->fpstate
.iwmmxt
, IWMMXT_SIZE
)
380 * Set the child iWMMXt state.
382 static int ptrace_setwmmxregs(struct task_struct
*tsk
, void __user
*ufp
)
384 struct thread_info
*thread
= task_thread_info(tsk
);
386 if (!test_ti_thread_flag(thread
, TIF_USING_IWMMXT
))
388 iwmmxt_task_release(thread
); /* force a reload */
389 return copy_from_user(&thread
->fpstate
.iwmmxt
, ufp
, IWMMXT_SIZE
)
397 * Get the child Crunch state.
399 static int ptrace_getcrunchregs(struct task_struct
*tsk
, void __user
*ufp
)
401 struct thread_info
*thread
= task_thread_info(tsk
);
403 crunch_task_disable(thread
); /* force it to ram */
404 return copy_to_user(ufp
, &thread
->crunchstate
, CRUNCH_SIZE
)
409 * Set the child Crunch state.
411 static int ptrace_setcrunchregs(struct task_struct
*tsk
, void __user
*ufp
)
413 struct thread_info
*thread
= task_thread_info(tsk
);
415 crunch_task_release(thread
); /* force a reload */
416 return copy_from_user(&thread
->crunchstate
, ufp
, CRUNCH_SIZE
)
423 * Get the child VFP state.
425 static int ptrace_getvfpregs(struct task_struct
*tsk
, void __user
*data
)
427 struct thread_info
*thread
= task_thread_info(tsk
);
428 union vfp_state
*vfp
= &thread
->vfpstate
;
429 struct user_vfp __user
*ufp
= data
;
431 vfp_sync_hwstate(thread
);
433 /* copy the floating point registers */
434 if (copy_to_user(&ufp
->fpregs
, &vfp
->hard
.fpregs
,
435 sizeof(vfp
->hard
.fpregs
)))
438 /* copy the status and control register */
439 if (put_user(vfp
->hard
.fpscr
, &ufp
->fpscr
))
446 * Set the child VFP state.
448 static int ptrace_setvfpregs(struct task_struct
*tsk
, void __user
*data
)
450 struct thread_info
*thread
= task_thread_info(tsk
);
451 union vfp_state
*vfp
= &thread
->vfpstate
;
452 struct user_vfp __user
*ufp
= data
;
454 vfp_sync_hwstate(thread
);
456 /* copy the floating point registers */
457 if (copy_from_user(&vfp
->hard
.fpregs
, &ufp
->fpregs
,
458 sizeof(vfp
->hard
.fpregs
)))
461 /* copy the status and control register */
462 if (get_user(vfp
->hard
.fpscr
, &ufp
->fpscr
))
465 vfp_flush_hwstate(thread
);
471 #ifdef CONFIG_HAVE_HW_BREAKPOINT
473 * Convert a virtual register number into an index for a thread_info
474 * breakpoint array. Breakpoints are identified using positive numbers
475 * whilst watchpoints are negative. The registers are laid out as pairs
476 * of (address, control), each pair mapping to a unique hw_breakpoint struct.
477 * Register 0 is reserved for describing resource information.
479 static int ptrace_hbp_num_to_idx(long num
)
482 num
= (ARM_MAX_BRP
<< 1) - num
;
483 return (num
- 1) >> 1;
487 * Returns the virtual register number for the address of the
488 * breakpoint at index idx.
490 static long ptrace_hbp_idx_to_num(int idx
)
492 long mid
= ARM_MAX_BRP
<< 1;
493 long num
= (idx
<< 1) + 1;
494 return num
> mid
? mid
- num
: num
;
498 * Handle hitting a HW-breakpoint.
500 static void ptrace_hbptriggered(struct perf_event
*bp
, int unused
,
501 struct perf_sample_data
*data
,
502 struct pt_regs
*regs
)
504 struct arch_hw_breakpoint
*bkpt
= counter_arch_bp(bp
);
509 for (i
= 0; i
< ARM_MAX_HBP_SLOTS
; ++i
)
510 if (current
->thread
.debug
.hbp
[i
] == bp
)
513 num
= (i
== ARM_MAX_HBP_SLOTS
) ? 0 : ptrace_hbp_idx_to_num(i
);
515 info
.si_signo
= SIGTRAP
;
516 info
.si_errno
= (int)num
;
517 info
.si_code
= TRAP_HWBKPT
;
518 info
.si_addr
= (void __user
*)(bkpt
->trigger
);
520 force_sig_info(SIGTRAP
, &info
, current
);
524 * Set ptrace breakpoint pointers to zero for this task.
525 * This is required in order to prevent child processes from unregistering
526 * breakpoints held by their parent.
528 void clear_ptrace_hw_breakpoint(struct task_struct
*tsk
)
530 memset(tsk
->thread
.debug
.hbp
, 0, sizeof(tsk
->thread
.debug
.hbp
));
534 * Unregister breakpoints from this task and reset the pointers in
537 void flush_ptrace_hw_breakpoint(struct task_struct
*tsk
)
540 struct thread_struct
*t
= &tsk
->thread
;
542 for (i
= 0; i
< ARM_MAX_HBP_SLOTS
; i
++) {
543 if (t
->debug
.hbp
[i
]) {
544 unregister_hw_breakpoint(t
->debug
.hbp
[i
]);
545 t
->debug
.hbp
[i
] = NULL
;
550 static u32
ptrace_get_hbp_resource_info(void)
552 u8 num_brps
, num_wrps
, debug_arch
, wp_len
;
555 num_brps
= hw_breakpoint_slots(TYPE_INST
);
556 num_wrps
= hw_breakpoint_slots(TYPE_DATA
);
557 debug_arch
= arch_get_debug_arch();
558 wp_len
= arch_get_max_wp_len();
571 static struct perf_event
*ptrace_hbp_create(struct task_struct
*tsk
, int type
)
573 struct perf_event_attr attr
;
575 ptrace_breakpoint_init(&attr
);
577 /* Initialise fields to sane defaults. */
579 attr
.bp_len
= HW_BREAKPOINT_LEN_4
;
583 return register_user_hw_breakpoint(&attr
, ptrace_hbptriggered
, tsk
);
586 static int ptrace_gethbpregs(struct task_struct
*tsk
, long num
,
587 unsigned long __user
*data
)
591 struct perf_event
*bp
;
592 struct arch_hw_breakpoint_ctrl arch_ctrl
;
595 reg
= ptrace_get_hbp_resource_info();
597 idx
= ptrace_hbp_num_to_idx(num
);
598 if (idx
< 0 || idx
>= ARM_MAX_HBP_SLOTS
) {
603 bp
= tsk
->thread
.debug
.hbp
[idx
];
609 arch_ctrl
= counter_arch_bp(bp
)->ctrl
;
612 * Fix up the len because we may have adjusted it
613 * to compensate for an unaligned address.
615 while (!(arch_ctrl
.len
& 0x1))
619 reg
= bp
->attr
.bp_addr
;
621 reg
= encode_ctrl_reg(arch_ctrl
);
625 if (put_user(reg
, data
))
632 static int ptrace_sethbpregs(struct task_struct
*tsk
, long num
,
633 unsigned long __user
*data
)
635 int idx
, gen_len
, gen_type
, implied_type
, ret
= 0;
637 struct perf_event
*bp
;
638 struct arch_hw_breakpoint_ctrl ctrl
;
639 struct perf_event_attr attr
;
644 implied_type
= HW_BREAKPOINT_RW
;
646 implied_type
= HW_BREAKPOINT_X
;
648 idx
= ptrace_hbp_num_to_idx(num
);
649 if (idx
< 0 || idx
>= ARM_MAX_HBP_SLOTS
) {
654 if (get_user(user_val
, data
)) {
659 bp
= tsk
->thread
.debug
.hbp
[idx
];
661 bp
= ptrace_hbp_create(tsk
, implied_type
);
666 tsk
->thread
.debug
.hbp
[idx
] = bp
;
673 attr
.bp_addr
= user_val
;
676 decode_ctrl_reg(user_val
, &ctrl
);
677 ret
= arch_bp_generic_fields(ctrl
, &gen_len
, &gen_type
);
681 if ((gen_type
& implied_type
) != gen_type
) {
686 attr
.bp_len
= gen_len
;
687 attr
.bp_type
= gen_type
;
688 attr
.disabled
= !ctrl
.enabled
;
691 ret
= modify_user_hw_breakpoint(bp
, &attr
);
697 long arch_ptrace(struct task_struct
*child
, long request
,
698 unsigned long addr
, unsigned long data
)
701 unsigned long __user
*datap
= (unsigned long __user
*) data
;
705 ret
= ptrace_read_user(child
, addr
, datap
);
709 ret
= ptrace_write_user(child
, addr
, data
);
713 ret
= ptrace_getregs(child
, datap
);
717 ret
= ptrace_setregs(child
, datap
);
720 case PTRACE_GETFPREGS
:
721 ret
= ptrace_getfpregs(child
, datap
);
724 case PTRACE_SETFPREGS
:
725 ret
= ptrace_setfpregs(child
, datap
);
729 case PTRACE_GETWMMXREGS
:
730 ret
= ptrace_getwmmxregs(child
, datap
);
733 case PTRACE_SETWMMXREGS
:
734 ret
= ptrace_setwmmxregs(child
, datap
);
738 case PTRACE_GET_THREAD_AREA
:
739 ret
= put_user(task_thread_info(child
)->tp_value
,
743 case PTRACE_SET_SYSCALL
:
744 task_thread_info(child
)->syscall
= data
;
749 case PTRACE_GETCRUNCHREGS
:
750 ret
= ptrace_getcrunchregs(child
, datap
);
753 case PTRACE_SETCRUNCHREGS
:
754 ret
= ptrace_setcrunchregs(child
, datap
);
759 case PTRACE_GETVFPREGS
:
760 ret
= ptrace_getvfpregs(child
, datap
);
763 case PTRACE_SETVFPREGS
:
764 ret
= ptrace_setvfpregs(child
, datap
);
768 #ifdef CONFIG_HAVE_HW_BREAKPOINT
769 case PTRACE_GETHBPREGS
:
770 ret
= ptrace_gethbpregs(child
, addr
,
771 (unsigned long __user
*)data
);
773 case PTRACE_SETHBPREGS
:
774 ret
= ptrace_sethbpregs(child
, addr
,
775 (unsigned long __user
*)data
);
780 ret
= ptrace_request(child
, request
, addr
, data
);
787 asmlinkage
int syscall_trace(int why
, struct pt_regs
*regs
, int scno
)
791 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
793 if (!(current
->ptrace
& PT_PTRACED
))
797 * Save IP. IP is used to denote syscall entry/exit:
798 * IP = 0 -> entry, = 1 -> exit
803 current_thread_info()->syscall
= scno
;
805 /* the 0x80 provides a way for the tracing parent to distinguish
806 between a syscall stop and SIGTRAP delivery */
807 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
810 * this isn't the same as continuing with a signal, but it will do
811 * for normal use. strace only continues with a signal if the
812 * stopping signal is not SIGTRAP. -brl
814 if (current
->exit_code
) {
815 send_sig(current
->exit_code
, current
, 1);
816 current
->exit_code
= 0;
820 return current_thread_info()->syscall
;