Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / arm / kernel / ptrace.c
blob2bf27f364d098c210ed7cca9ece5edca613a9f2b
1 /*
2 * linux/arch/arm/kernel/ptrace.c
4 * By Ross Biro 1/23/92
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>
14 #include <linux/mm.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>
29 #define REG_PC 15
30 #define REG_PSR 16
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
36 #if 0
38 * Breakpoint SWI instruction: SWI &9F0001
40 #define BREAKINST_ARM 0xef9f0001
41 #define BREAKINST_THUMB 0xdf00 /* fill this in later */
42 #else
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
53 #endif
55 struct pt_regs_offset {
56 const char *name;
57 int 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[] = {
65 REG_OFFSET_NAME(r0),
66 REG_OFFSET_NAME(r1),
67 REG_OFFSET_NAME(r2),
68 REG_OFFSET_NAME(r3),
69 REG_OFFSET_NAME(r4),
70 REG_OFFSET_NAME(r5),
71 REG_OFFSET_NAME(r6),
72 REG_OFFSET_NAME(r7),
73 REG_OFFSET_NAME(r8),
74 REG_OFFSET_NAME(r9),
75 REG_OFFSET_NAME(r10),
76 REG_OFFSET_NAME(fp),
77 REG_OFFSET_NAME(ip),
78 REG_OFFSET_NAME(sp),
79 REG_OFFSET_NAME(lr),
80 REG_OFFSET_NAME(pc),
81 REG_OFFSET_NAME(cpsr),
82 REG_OFFSET_NAME(ORIG_r0),
83 REG_OFFSET_END,
86 /**
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))
98 return roff->offset;
99 return -EINVAL;
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)
114 return roff->name;
115 return NULL;
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,
139 * this returns 0.
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);
144 addr += n;
145 if (regs_within_kernel_stack(regs, (unsigned long)addr))
146 return *addr;
147 else
148 return 0;
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
155 * data space.
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
166 * data space.
168 static inline int
169 put_user_reg(struct task_struct *task, int offset, long data)
171 struct pt_regs newregs, *regs = task_pt_regs(task);
172 int ret = -EINVAL;
174 newregs = *regs;
175 newregs.uregs[offset] = data;
177 if (valid_user_regs(&newregs)) {
178 regs->uregs[offset] = data;
179 ret = 0;
182 return ret;
186 * Called by kernel/ptrace.c when detaching..
188 void ptrace_disable(struct task_struct *child)
190 /* Nothing to do. */
194 * Handle hitting a breakpoint.
196 void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
198 siginfo_t info;
200 info.si_signo = SIGTRAP;
201 info.si_errno = 0;
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);
211 return 0;
214 static struct undef_hook arm_break_hook = {
215 .instr_mask = 0x0fffffff,
216 .instr_val = 0x07f001f0,
217 .cpsr_mask = PSR_T_BIT,
218 .cpsr_val = 0,
219 .fn = break_trap,
222 static struct undef_hook thumb_break_hook = {
223 .instr_mask = 0xffff,
224 .instr_val = 0xde01,
225 .cpsr_mask = PSR_T_BIT,
226 .cpsr_val = PSR_T_BIT,
227 .fn = break_trap,
230 static int thumb2_break_trap(struct pt_regs *regs, unsigned int instr)
232 unsigned int instr2;
233 void __user *pc;
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;
240 } else {
241 get_user(instr2, (u16 __user *)pc);
244 if (instr2 == 0xa000) {
245 ptrace_break(current, regs);
246 return 0;
247 } else {
248 return 1;
252 static struct undef_hook thumb2_break_hook = {
253 .instr_mask = 0xffff,
254 .instr_val = 0xf7f0,
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);
265 return 0;
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)
277 unsigned long tmp;
279 if (off & 3 || off >= sizeof(struct user))
280 return -EIO;
282 tmp = 0;
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,
300 unsigned long val)
302 if (off & 3 || off >= sizeof(struct user))
303 return -EIO;
305 if (off >= sizeof(struct pt_regs))
306 return 0;
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;
327 int ret;
329 ret = -EFAULT;
330 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
331 struct pt_regs *regs = task_pt_regs(tsk);
333 ret = -EINVAL;
334 if (valid_user_regs(&newregs)) {
335 *regs = newregs;
336 ret = 0;
340 return ret;
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;
363 #ifdef CONFIG_IWMMXT
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))
373 return -ENODATA;
374 iwmmxt_task_disable(thread); /* force it to ram */
375 return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
376 ? -EFAULT : 0;
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))
387 return -EACCES;
388 iwmmxt_task_release(thread); /* force a reload */
389 return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
390 ? -EFAULT : 0;
393 #endif
395 #ifdef CONFIG_CRUNCH
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)
405 ? -EFAULT : 0;
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)
417 ? -EFAULT : 0;
419 #endif
421 #ifdef CONFIG_VFP
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)))
436 return -EFAULT;
438 /* copy the status and control register */
439 if (put_user(vfp->hard.fpscr, &ufp->fpscr))
440 return -EFAULT;
442 return 0;
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)))
459 return -EFAULT;
461 /* copy the status and control register */
462 if (get_user(vfp->hard.fpscr, &ufp->fpscr))
463 return -EFAULT;
465 vfp_flush_hwstate(thread);
467 return 0;
469 #endif
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)
481 if (num < 0)
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);
505 long num;
506 int i;
507 siginfo_t info;
509 for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
510 if (current->thread.debug.hbp[i] == bp)
511 break;
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
535 * the thread_struct.
537 void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
539 int i;
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;
553 u32 reg = 0;
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();
560 reg |= debug_arch;
561 reg <<= 8;
562 reg |= wp_len;
563 reg <<= 8;
564 reg |= num_wrps;
565 reg <<= 8;
566 reg |= num_brps;
568 return reg;
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. */
578 attr.bp_addr = 0;
579 attr.bp_len = HW_BREAKPOINT_LEN_4;
580 attr.bp_type = type;
581 attr.disabled = 1;
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)
589 u32 reg;
590 int idx, ret = 0;
591 struct perf_event *bp;
592 struct arch_hw_breakpoint_ctrl arch_ctrl;
594 if (num == 0) {
595 reg = ptrace_get_hbp_resource_info();
596 } else {
597 idx = ptrace_hbp_num_to_idx(num);
598 if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
599 ret = -EINVAL;
600 goto out;
603 bp = tsk->thread.debug.hbp[idx];
604 if (!bp) {
605 reg = 0;
606 goto put;
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))
616 arch_ctrl.len >>= 1;
618 if (num & 0x1)
619 reg = bp->attr.bp_addr;
620 else
621 reg = encode_ctrl_reg(arch_ctrl);
624 put:
625 if (put_user(reg, data))
626 ret = -EFAULT;
628 out:
629 return ret;
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;
636 u32 user_val;
637 struct perf_event *bp;
638 struct arch_hw_breakpoint_ctrl ctrl;
639 struct perf_event_attr attr;
641 if (num == 0)
642 goto out;
643 else if (num < 0)
644 implied_type = HW_BREAKPOINT_RW;
645 else
646 implied_type = HW_BREAKPOINT_X;
648 idx = ptrace_hbp_num_to_idx(num);
649 if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
650 ret = -EINVAL;
651 goto out;
654 if (get_user(user_val, data)) {
655 ret = -EFAULT;
656 goto out;
659 bp = tsk->thread.debug.hbp[idx];
660 if (!bp) {
661 bp = ptrace_hbp_create(tsk, implied_type);
662 if (IS_ERR(bp)) {
663 ret = PTR_ERR(bp);
664 goto out;
666 tsk->thread.debug.hbp[idx] = bp;
669 attr = bp->attr;
671 if (num & 0x1) {
672 /* Address */
673 attr.bp_addr = user_val;
674 } else {
675 /* Control */
676 decode_ctrl_reg(user_val, &ctrl);
677 ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
678 if (ret)
679 goto out;
681 if ((gen_type & implied_type) != gen_type) {
682 ret = -EINVAL;
683 goto out;
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);
692 out:
693 return ret;
695 #endif
697 long arch_ptrace(struct task_struct *child, long request,
698 unsigned long addr, unsigned long data)
700 int ret;
701 unsigned long __user *datap = (unsigned long __user *) data;
703 switch (request) {
704 case PTRACE_PEEKUSR:
705 ret = ptrace_read_user(child, addr, datap);
706 break;
708 case PTRACE_POKEUSR:
709 ret = ptrace_write_user(child, addr, data);
710 break;
712 case PTRACE_GETREGS:
713 ret = ptrace_getregs(child, datap);
714 break;
716 case PTRACE_SETREGS:
717 ret = ptrace_setregs(child, datap);
718 break;
720 case PTRACE_GETFPREGS:
721 ret = ptrace_getfpregs(child, datap);
722 break;
724 case PTRACE_SETFPREGS:
725 ret = ptrace_setfpregs(child, datap);
726 break;
728 #ifdef CONFIG_IWMMXT
729 case PTRACE_GETWMMXREGS:
730 ret = ptrace_getwmmxregs(child, datap);
731 break;
733 case PTRACE_SETWMMXREGS:
734 ret = ptrace_setwmmxregs(child, datap);
735 break;
736 #endif
738 case PTRACE_GET_THREAD_AREA:
739 ret = put_user(task_thread_info(child)->tp_value,
740 datap);
741 break;
743 case PTRACE_SET_SYSCALL:
744 task_thread_info(child)->syscall = data;
745 ret = 0;
746 break;
748 #ifdef CONFIG_CRUNCH
749 case PTRACE_GETCRUNCHREGS:
750 ret = ptrace_getcrunchregs(child, datap);
751 break;
753 case PTRACE_SETCRUNCHREGS:
754 ret = ptrace_setcrunchregs(child, datap);
755 break;
756 #endif
758 #ifdef CONFIG_VFP
759 case PTRACE_GETVFPREGS:
760 ret = ptrace_getvfpregs(child, datap);
761 break;
763 case PTRACE_SETVFPREGS:
764 ret = ptrace_setvfpregs(child, datap);
765 break;
766 #endif
768 #ifdef CONFIG_HAVE_HW_BREAKPOINT
769 case PTRACE_GETHBPREGS:
770 ret = ptrace_gethbpregs(child, addr,
771 (unsigned long __user *)data);
772 break;
773 case PTRACE_SETHBPREGS:
774 ret = ptrace_sethbpregs(child, addr,
775 (unsigned long __user *)data);
776 break;
777 #endif
779 default:
780 ret = ptrace_request(child, request, addr, data);
781 break;
784 return ret;
787 asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
789 unsigned long ip;
791 if (!test_thread_flag(TIF_SYSCALL_TRACE))
792 return scno;
793 if (!(current->ptrace & PT_PTRACED))
794 return scno;
797 * Save IP. IP is used to denote syscall entry/exit:
798 * IP = 0 -> entry, = 1 -> exit
800 ip = regs->ARM_ip;
801 regs->ARM_ip = why;
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)
808 ? 0x80 : 0));
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;
818 regs->ARM_ip = ip;
820 return current_thread_info()->syscall;