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/elf.h>
16
#include <linux/smp.h>
17
#include <linux/ptrace.h>
18
#include <linux/user.h>
19
#include <linux/security.h>
20
#include <linux/init.h>
21
#include <linux/signal.h>
22
#include <linux/uaccess.h>
23
#include <linux/perf_event.h>
24
#include <linux/hw_breakpoint.h>
25
#include <linux/regset.h>
27
#include <asm/pgtable.h>
28
#include <asm/system.h>
29
#include <asm/traps.h>
34
* does not yet catch signals sent when the child dies.
35
* in exit.c or in signal.c.
40
* Breakpoint SWI instruction: SWI &9F0001
42
#define BREAKINST_ARM 0xef9f0001
43
#define BREAKINST_THUMB 0xdf00 /* fill this in later */
46
* New breakpoints - use an undefined instruction. The ARM architecture
47
* reference manual guarantees that the following instruction space
48
* will produce an undefined instruction exception on all CPUs:
50
* ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
51
* Thumb: 1101 1110 xxxx xxxx
53
#define BREAKINST_ARM 0xe7f001f0
54
#define BREAKINST_THUMB 0xde01
57
struct pt_regs_offset {
62
#define REG_OFFSET_NAME(r) \
63
{.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
64
#define REG_OFFSET_END {.name = NULL, .offset = 0}
66
static const struct pt_regs_offset regoffset_table[] = {
83
REG_OFFSET_NAME(cpsr),
84
REG_OFFSET_NAME(ORIG_r0),
89
* regs_query_register_offset() - query register offset from its name
90
* @name: the name of a register
92
* regs_query_register_offset() returns the offset of a register in struct
93
* pt_regs from its name. If the name is invalid, this returns -EINVAL;
95
int regs_query_register_offset(const char *name)
97
const struct pt_regs_offset *roff;
98
for (roff = regoffset_table; roff->name != NULL; roff++)
99
if (!strcmp(roff->name, name))
105
* regs_query_register_name() - query register name from its offset
106
* @offset: the offset of a register in struct pt_regs.
108
* regs_query_register_name() returns the name of a register from its
109
* offset in struct pt_regs. If the @offset is invalid, this returns NULL;
111
const char *regs_query_register_name(unsigned int offset)
113
const struct pt_regs_offset *roff;
114
for (roff = regoffset_table; roff->name != NULL; roff++)
115
if (roff->offset == offset)
121
* regs_within_kernel_stack() - check the address in the stack
122
* @regs: pt_regs which contains kernel stack pointer.
123
* @addr: address which is checked.
125
* regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
126
* If @addr is within the kernel stack, it returns true. If not, returns false.
128
bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
130
return ((addr & ~(THREAD_SIZE - 1)) ==
131
(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
135
* regs_get_kernel_stack_nth() - get Nth entry of the stack
136
* @regs: pt_regs which contains kernel stack pointer.
137
* @n: stack entry number.
139
* regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
140
* is specified by @regs. If the @n th entry is NOT in the kernel stack,
143
unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
145
unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
147
if (regs_within_kernel_stack(regs, (unsigned long)addr))
154
* this routine will get a word off of the processes privileged stack.
155
* the offset is how far from the base addr as stored in the THREAD.
156
* this routine assumes that all the privileged stacks are in our
159
static inline long get_user_reg(struct task_struct *task, int offset)
161
return task_pt_regs(task)->uregs[offset];
165
* this routine will put a word on the processes privileged stack.
166
* the offset is how far from the base addr as stored in the THREAD.
167
* this routine assumes that all the privileged stacks are in our
171
put_user_reg(struct task_struct *task, int offset, long data)
173
struct pt_regs newregs, *regs = task_pt_regs(task);
177
newregs.uregs[offset] = data;
179
if (valid_user_regs(&newregs)) {
180
regs->uregs[offset] = data;
188
* Called by kernel/ptrace.c when detaching..
190
void ptrace_disable(struct task_struct *child)
196
* Handle hitting a breakpoint.
198
void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
202
info.si_signo = SIGTRAP;
204
info.si_code = TRAP_BRKPT;
205
info.si_addr = (void __user *)instruction_pointer(regs);
207
force_sig_info(SIGTRAP, &info, tsk);
210
static int break_trap(struct pt_regs *regs, unsigned int instr)
212
ptrace_break(current, regs);
216
static struct undef_hook arm_break_hook = {
217
.instr_mask = 0x0fffffff,
218
.instr_val = 0x07f001f0,
219
.cpsr_mask = PSR_T_BIT,
224
static struct undef_hook thumb_break_hook = {
225
.instr_mask = 0xffff,
227
.cpsr_mask = PSR_T_BIT,
228
.cpsr_val = PSR_T_BIT,
232
static struct undef_hook thumb2_break_hook = {
233
.instr_mask = 0xffffffff,
234
.instr_val = 0xf7f0a000,
235
.cpsr_mask = PSR_T_BIT,
236
.cpsr_val = PSR_T_BIT,
240
static int __init ptrace_break_init(void)
242
register_undef_hook(&arm_break_hook);
243
register_undef_hook(&thumb_break_hook);
244
register_undef_hook(&thumb2_break_hook);
248
core_initcall(ptrace_break_init);
251
* Read the word at offset "off" into the "struct user". We
252
* actually access the pt_regs stored on the kernel stack.
254
static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
255
unsigned long __user *ret)
259
if (off & 3 || off >= sizeof(struct user))
263
if (off == PT_TEXT_ADDR)
264
tmp = tsk->mm->start_code;
265
else if (off == PT_DATA_ADDR)
266
tmp = tsk->mm->start_data;
267
else if (off == PT_TEXT_END_ADDR)
268
tmp = tsk->mm->end_code;
269
else if (off < sizeof(struct pt_regs))
270
tmp = get_user_reg(tsk, off >> 2);
272
return put_user(tmp, ret);
276
* Write the word at offset "off" into "struct user". We
277
* actually access the pt_regs stored on the kernel stack.
279
static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
282
if (off & 3 || off >= sizeof(struct user))
285
if (off >= sizeof(struct pt_regs))
288
return put_user_reg(tsk, off >> 2, val);
294
* Get the child iWMMXt state.
296
static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
298
struct thread_info *thread = task_thread_info(tsk);
300
if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
302
iwmmxt_task_disable(thread); /* force it to ram */
303
return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
308
* Set the child iWMMXt state.
310
static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
312
struct thread_info *thread = task_thread_info(tsk);
314
if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
316
iwmmxt_task_release(thread); /* force a reload */
317
return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
325
* Get the child Crunch state.
327
static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
329
struct thread_info *thread = task_thread_info(tsk);
331
crunch_task_disable(thread); /* force it to ram */
332
return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
337
* Set the child Crunch state.
339
static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
341
struct thread_info *thread = task_thread_info(tsk);
343
crunch_task_release(thread); /* force a reload */
344
return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
349
#ifdef CONFIG_HAVE_HW_BREAKPOINT
351
* Convert a virtual register number into an index for a thread_info
352
* breakpoint array. Breakpoints are identified using positive numbers
353
* whilst watchpoints are negative. The registers are laid out as pairs
354
* of (address, control), each pair mapping to a unique hw_breakpoint struct.
355
* Register 0 is reserved for describing resource information.
357
static int ptrace_hbp_num_to_idx(long num)
360
num = (ARM_MAX_BRP << 1) - num;
361
return (num - 1) >> 1;
365
* Returns the virtual register number for the address of the
366
* breakpoint at index idx.
368
static long ptrace_hbp_idx_to_num(int idx)
370
long mid = ARM_MAX_BRP << 1;
371
long num = (idx << 1) + 1;
372
return num > mid ? mid - num : num;
376
* Handle hitting a HW-breakpoint.
378
static void ptrace_hbptriggered(struct perf_event *bp,
379
struct perf_sample_data *data,
380
struct pt_regs *regs)
382
struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
387
for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
388
if (current->thread.debug.hbp[i] == bp)
391
num = (i == ARM_MAX_HBP_SLOTS) ? 0 : ptrace_hbp_idx_to_num(i);
393
info.si_signo = SIGTRAP;
394
info.si_errno = (int)num;
395
info.si_code = TRAP_HWBKPT;
396
info.si_addr = (void __user *)(bkpt->trigger);
398
force_sig_info(SIGTRAP, &info, current);
402
* Set ptrace breakpoint pointers to zero for this task.
403
* This is required in order to prevent child processes from unregistering
404
* breakpoints held by their parent.
406
void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
408
memset(tsk->thread.debug.hbp, 0, sizeof(tsk->thread.debug.hbp));
412
* Unregister breakpoints from this task and reset the pointers in
415
void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
418
struct thread_struct *t = &tsk->thread;
420
for (i = 0; i < ARM_MAX_HBP_SLOTS; i++) {
421
if (t->debug.hbp[i]) {
422
unregister_hw_breakpoint(t->debug.hbp[i]);
423
t->debug.hbp[i] = NULL;
428
static u32 ptrace_get_hbp_resource_info(void)
430
u8 num_brps, num_wrps, debug_arch, wp_len;
433
num_brps = hw_breakpoint_slots(TYPE_INST);
434
num_wrps = hw_breakpoint_slots(TYPE_DATA);
435
debug_arch = arch_get_debug_arch();
436
wp_len = arch_get_max_wp_len();
449
static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
451
struct perf_event_attr attr;
453
ptrace_breakpoint_init(&attr);
455
/* Initialise fields to sane defaults. */
457
attr.bp_len = HW_BREAKPOINT_LEN_4;
461
return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
465
static int ptrace_gethbpregs(struct task_struct *tsk, long num,
466
unsigned long __user *data)
470
struct perf_event *bp;
471
struct arch_hw_breakpoint_ctrl arch_ctrl;
474
reg = ptrace_get_hbp_resource_info();
476
idx = ptrace_hbp_num_to_idx(num);
477
if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
482
bp = tsk->thread.debug.hbp[idx];
488
arch_ctrl = counter_arch_bp(bp)->ctrl;
491
* Fix up the len because we may have adjusted it
492
* to compensate for an unaligned address.
494
while (!(arch_ctrl.len & 0x1))
498
reg = bp->attr.bp_addr;
500
reg = encode_ctrl_reg(arch_ctrl);
504
if (put_user(reg, data))
511
static int ptrace_sethbpregs(struct task_struct *tsk, long num,
512
unsigned long __user *data)
514
int idx, gen_len, gen_type, implied_type, ret = 0;
516
struct perf_event *bp;
517
struct arch_hw_breakpoint_ctrl ctrl;
518
struct perf_event_attr attr;
523
implied_type = HW_BREAKPOINT_RW;
525
implied_type = HW_BREAKPOINT_X;
527
idx = ptrace_hbp_num_to_idx(num);
528
if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
533
if (get_user(user_val, data)) {
538
bp = tsk->thread.debug.hbp[idx];
540
bp = ptrace_hbp_create(tsk, implied_type);
545
tsk->thread.debug.hbp[idx] = bp;
552
attr.bp_addr = user_val;
555
decode_ctrl_reg(user_val, &ctrl);
556
ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
560
if ((gen_type & implied_type) != gen_type) {
565
attr.bp_len = gen_len;
566
attr.bp_type = gen_type;
567
attr.disabled = !ctrl.enabled;
570
ret = modify_user_hw_breakpoint(bp, &attr);
576
/* regset get/set implementations */
578
static int gpr_get(struct task_struct *target,
579
const struct user_regset *regset,
580
unsigned int pos, unsigned int count,
581
void *kbuf, void __user *ubuf)
583
struct pt_regs *regs = task_pt_regs(target);
585
return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
590
static int gpr_set(struct task_struct *target,
591
const struct user_regset *regset,
592
unsigned int pos, unsigned int count,
593
const void *kbuf, const void __user *ubuf)
596
struct pt_regs newregs;
598
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
604
if (!valid_user_regs(&newregs))
607
*task_pt_regs(target) = newregs;
611
static int fpa_get(struct task_struct *target,
612
const struct user_regset *regset,
613
unsigned int pos, unsigned int count,
614
void *kbuf, void __user *ubuf)
616
return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
617
&task_thread_info(target)->fpstate,
618
0, sizeof(struct user_fp));
621
static int fpa_set(struct task_struct *target,
622
const struct user_regset *regset,
623
unsigned int pos, unsigned int count,
624
const void *kbuf, const void __user *ubuf)
626
struct thread_info *thread = task_thread_info(target);
628
thread->used_cp[1] = thread->used_cp[2] = 1;
630
return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
632
0, sizeof(struct user_fp));
637
* VFP register get/set implementations.
639
* With respect to the kernel, struct user_fp is divided into three chunks:
640
* 16 or 32 real VFP registers (d0-d15 or d0-31)
641
* These are transferred to/from the real registers in the task's
642
* vfp_hard_struct. The number of registers depends on the kernel
645
* 16 or 0 fake VFP registers (d16-d31 or empty)
646
* i.e., the user_vfp structure has space for 32 registers even if
647
* the kernel doesn't have them all.
649
* vfp_get() reads this chunk as zero where applicable
650
* vfp_set() ignores this chunk
652
* 1 word for the FPSCR
654
* The bounds-checking logic built into user_regset_copyout and friends
655
* means that we can make a simple sequence of calls to map the relevant data
656
* to/from the specified slice of the user regset structure.
658
static int vfp_get(struct task_struct *target,
659
const struct user_regset *regset,
660
unsigned int pos, unsigned int count,
661
void *kbuf, void __user *ubuf)
664
struct thread_info *thread = task_thread_info(target);
665
struct vfp_hard_struct const *vfp = &thread->vfpstate.hard;
666
const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
667
const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
669
vfp_sync_hwstate(thread);
671
ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
674
user_fpregs_offset + sizeof(vfp->fpregs));
678
ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
679
user_fpregs_offset + sizeof(vfp->fpregs),
684
return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
687
user_fpscr_offset + sizeof(vfp->fpscr));
691
* For vfp_set() a read-modify-write is done on the VFP registers,
692
* in order to avoid writing back a half-modified set of registers on
695
static int vfp_set(struct task_struct *target,
696
const struct user_regset *regset,
697
unsigned int pos, unsigned int count,
698
const void *kbuf, const void __user *ubuf)
701
struct thread_info *thread = task_thread_info(target);
702
struct vfp_hard_struct new_vfp = thread->vfpstate.hard;
703
const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
704
const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
706
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
709
user_fpregs_offset + sizeof(new_vfp.fpregs));
713
ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
714
user_fpregs_offset + sizeof(new_vfp.fpregs),
719
ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
722
user_fpscr_offset + sizeof(new_vfp.fpscr));
726
vfp_sync_hwstate(thread);
727
thread->vfpstate.hard = new_vfp;
728
vfp_flush_hwstate(thread);
732
#endif /* CONFIG_VFP */
742
static const struct user_regset arm_regsets[] = {
744
.core_note_type = NT_PRSTATUS,
747
.align = sizeof(u32),
753
* For the FPA regs in fpstate, the real fields are a mixture
754
* of sizes, so pretend that the registers are word-sized:
756
.core_note_type = NT_PRFPREG,
757
.n = sizeof(struct user_fp) / sizeof(u32),
759
.align = sizeof(u32),
766
* Pretend that the VFP regs are word-sized, since the FPSCR is
767
* a single word dangling at the end of struct user_vfp:
769
.core_note_type = NT_ARM_VFP,
770
.n = ARM_VFPREGS_SIZE / sizeof(u32),
772
.align = sizeof(u32),
776
#endif /* CONFIG_VFP */
779
static const struct user_regset_view user_arm_view = {
780
.name = "arm", .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
781
.regsets = arm_regsets, .n = ARRAY_SIZE(arm_regsets)
784
const struct user_regset_view *task_user_regset_view(struct task_struct *task)
786
return &user_arm_view;
789
long arch_ptrace(struct task_struct *child, long request,
790
unsigned long addr, unsigned long data)
793
unsigned long __user *datap = (unsigned long __user *) data;
797
ret = ptrace_read_user(child, addr, datap);
801
ret = ptrace_write_user(child, addr, data);
805
ret = copy_regset_to_user(child,
806
&user_arm_view, REGSET_GPR,
807
0, sizeof(struct pt_regs),
812
ret = copy_regset_from_user(child,
813
&user_arm_view, REGSET_GPR,
814
0, sizeof(struct pt_regs),
818
case PTRACE_GETFPREGS:
819
ret = copy_regset_to_user(child,
820
&user_arm_view, REGSET_FPR,
821
0, sizeof(union fp_state),
825
case PTRACE_SETFPREGS:
826
ret = copy_regset_from_user(child,
827
&user_arm_view, REGSET_FPR,
828
0, sizeof(union fp_state),
833
case PTRACE_GETWMMXREGS:
834
ret = ptrace_getwmmxregs(child, datap);
837
case PTRACE_SETWMMXREGS:
838
ret = ptrace_setwmmxregs(child, datap);
842
case PTRACE_GET_THREAD_AREA:
843
ret = put_user(task_thread_info(child)->tp_value,
847
case PTRACE_SET_SYSCALL:
848
task_thread_info(child)->syscall = data;
853
case PTRACE_GETCRUNCHREGS:
854
ret = ptrace_getcrunchregs(child, datap);
857
case PTRACE_SETCRUNCHREGS:
858
ret = ptrace_setcrunchregs(child, datap);
863
case PTRACE_GETVFPREGS:
864
ret = copy_regset_to_user(child,
865
&user_arm_view, REGSET_VFP,
870
case PTRACE_SETVFPREGS:
871
ret = copy_regset_from_user(child,
872
&user_arm_view, REGSET_VFP,
878
#ifdef CONFIG_HAVE_HW_BREAKPOINT
879
case PTRACE_GETHBPREGS:
880
if (ptrace_get_breakpoints(child) < 0)
883
ret = ptrace_gethbpregs(child, addr,
884
(unsigned long __user *)data);
885
ptrace_put_breakpoints(child);
887
case PTRACE_SETHBPREGS:
888
if (ptrace_get_breakpoints(child) < 0)
891
ret = ptrace_sethbpregs(child, addr,
892
(unsigned long __user *)data);
893
ptrace_put_breakpoints(child);
898
ret = ptrace_request(child, request, addr, data);
905
asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
909
if (!test_thread_flag(TIF_SYSCALL_TRACE))
911
if (!(current->ptrace & PT_PTRACED))
915
* Save IP. IP is used to denote syscall entry/exit:
916
* IP = 0 -> entry, = 1 -> exit
921
current_thread_info()->syscall = scno;
923
/* the 0x80 provides a way for the tracing parent to distinguish
924
between a syscall stop and SIGTRAP delivery */
925
ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
928
* this isn't the same as continuing with a signal, but it will do
929
* for normal use. strace only continues with a signal if the
930
* stopping signal is not SIGTRAP. -brl
932
if (current->exit_code) {
933
send_sig(current->exit_code, current, 1);
934
current->exit_code = 0;
938
return current_thread_info()->syscall;