]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/arm26/kernel/ptrace.c
2a137146a77cb04adff251efa2b639ed59ca2df3
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/config.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/ptrace.h>
19 #include <linux/user.h>
20 #include <linux/security.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 * Get the address of the live pt_regs for the specified task.
43 * These are saved onto the top kernel stack when the process
46 * Note: if a user thread is execve'd from kernel space, the
47 * kernel stack will not be empty on entry to the kernel, so
48 * ptracing these tasks will fail.
50 static inline struct pt_regs
*
51 get_user_regs(struct task_struct
*task
)
53 return __get_user_regs(task
->thread_info
);
57 * this routine will get a word off of the processes privileged stack.
58 * the offset is how far from the base addr as stored in the THREAD.
59 * this routine assumes that all the privileged stacks are in our
62 static inline long get_user_reg(struct task_struct
*task
, int offset
)
64 return get_user_regs(task
)->uregs
[offset
];
68 * this routine will put a word on the processes privileged stack.
69 * the offset is how far from the base addr as stored in the THREAD.
70 * this routine assumes that all the privileged stacks are in our
74 put_user_reg(struct task_struct
*task
, int offset
, long data
)
76 struct pt_regs newregs
, *regs
= get_user_regs(task
);
80 newregs
.uregs
[offset
] = data
;
82 if (valid_user_regs(&newregs
)) {
83 regs
->uregs
[offset
] = data
;
91 read_u32(struct task_struct
*task
, unsigned long addr
, u32
*res
)
95 ret
= access_process_vm(task
, addr
, res
, sizeof(*res
), 0);
97 return ret
== sizeof(*res
) ? 0 : -EIO
;
101 read_instr(struct task_struct
*task
, unsigned long addr
, u32
*res
)
105 ret
= access_process_vm(task
, addr
& ~3, &val
, sizeof(val
), 0);
106 ret
= ret
== sizeof(val
) ? 0 : -EIO
;
112 * Get value of register `rn' (in the instruction)
115 ptrace_getrn(struct task_struct
*child
, unsigned long insn
)
117 unsigned int reg
= (insn
>> 16) & 15;
120 val
= get_user_reg(child
, reg
);
122 val
= pc_pointer(val
+ 8); //FIXME - correct for arm26?
128 * Get value of operand 2 (in an ALU instruction)
131 ptrace_getaluop2(struct task_struct
*child
, unsigned long insn
)
137 if (insn
& 1 << 25) {
139 shift
= (insn
>> 8) & 15;
142 val
= get_user_reg (child
, insn
& 15);
145 shift
= (int)get_user_reg (child
, (insn
>> 8) & 15);
147 shift
= (insn
>> 7) & 31;
149 type
= (insn
>> 5) & 3;
153 case 0: val
<<= shift
; break;
154 case 1: val
>>= shift
; break;
156 val
= (((signed long)val
) >> shift
);
159 val
= (val
>> shift
) | (val
<< (32 - shift
));
166 * Get value of operand 2 (in a LDR instruction)
169 ptrace_getldrop2(struct task_struct
*child
, unsigned long insn
)
175 val
= get_user_reg(child
, insn
& 15);
176 shift
= (insn
>> 7) & 31;
177 type
= (insn
>> 5) & 3;
180 case 0: val
<<= shift
; break;
181 case 1: val
>>= shift
; break;
183 val
= (((signed long)val
) >> shift
);
186 val
= (val
>> shift
) | (val
<< (32 - shift
));
192 #define OP_MASK 0x01e00000
193 #define OP_AND 0x00000000
194 #define OP_EOR 0x00200000
195 #define OP_SUB 0x00400000
196 #define OP_RSB 0x00600000
197 #define OP_ADD 0x00800000
198 #define OP_ADC 0x00a00000
199 #define OP_SBC 0x00c00000
200 #define OP_RSC 0x00e00000
201 #define OP_ORR 0x01800000
202 #define OP_MOV 0x01a00000
203 #define OP_BIC 0x01c00000
204 #define OP_MVN 0x01e00000
207 get_branch_address(struct task_struct
*child
, unsigned long pc
, unsigned long insn
)
211 switch (insn
& 0x0e000000) {
217 long aluop1
, aluop2
, ccbit
;
219 if ((insn
& 0xf000) != 0xf000)
222 aluop1
= ptrace_getrn(child
, insn
);
223 aluop2
= ptrace_getaluop2(child
, insn
);
224 ccbit
= get_user_reg(child
, REG_PSR
) & PSR_C_BIT
? 1 : 0;
226 switch (insn
& OP_MASK
) {
227 case OP_AND
: alt
= aluop1
& aluop2
; break;
228 case OP_EOR
: alt
= aluop1
^ aluop2
; break;
229 case OP_SUB
: alt
= aluop1
- aluop2
; break;
230 case OP_RSB
: alt
= aluop2
- aluop1
; break;
231 case OP_ADD
: alt
= aluop1
+ aluop2
; break;
232 case OP_ADC
: alt
= aluop1
+ aluop2
+ ccbit
; break;
233 case OP_SBC
: alt
= aluop1
- aluop2
+ ccbit
; break;
234 case OP_RSC
: alt
= aluop2
- aluop1
+ ccbit
; break;
235 case OP_ORR
: alt
= aluop1
| aluop2
; break;
236 case OP_MOV
: alt
= aluop2
; break;
237 case OP_BIC
: alt
= aluop1
& ~aluop2
; break;
238 case OP_MVN
: alt
= ~aluop2
; break;
248 if ((insn
& 0x0010f000) == 0x0010f000) {
251 base
= ptrace_getrn(child
, insn
);
252 if (insn
& 1 << 24) {
255 if (insn
& 0x02000000)
256 aluop2
= ptrace_getldrop2(child
, insn
);
258 aluop2
= insn
& 0xfff;
265 if (read_u32(child
, base
, &alt
) == 0)
266 alt
= pc_pointer(alt
);
274 if ((insn
& 0x00108000) == 0x00108000) {
276 unsigned int nr_regs
;
278 if (insn
& (1 << 23)) {
279 nr_regs
= hweight16(insn
& 65535) << 2;
281 if (!(insn
& (1 << 24)))
284 if (insn
& (1 << 24))
290 base
= ptrace_getrn(child
, insn
);
292 if (read_u32(child
, base
+ nr_regs
, &alt
) == 0)
293 alt
= pc_pointer(alt
);
303 /* It's a branch/branch link: instead of trying to
304 * figure out whether the branch will be taken or not,
305 * we'll put a breakpoint at both locations. This is
306 * simpler, more reliable, and probably not a whole lot
307 * slower than the alternative approach of emulating the
310 displ
= (insn
& 0x00ffffff) << 8;
311 displ
= (displ
>> 6) + 8;
312 if (displ
!= 0 && displ
!= 4)
322 swap_insn(struct task_struct
*task
, unsigned long addr
,
323 void *old_insn
, void *new_insn
, int size
)
327 ret
= access_process_vm(task
, addr
, old_insn
, size
, 0);
329 ret
= access_process_vm(task
, addr
, new_insn
, size
, 1);
334 add_breakpoint(struct task_struct
*task
, struct debug_info
*dbg
, unsigned long addr
)
336 int nr
= dbg
->nsaved
;
339 u32 new_insn
= BREAKINST_ARM
;
342 res
= swap_insn(task
, addr
, &dbg
->bp
[nr
].insn
, &new_insn
, 4);
345 dbg
->bp
[nr
].address
= addr
;
349 printk(KERN_ERR
"ptrace: too many breakpoints\n");
353 * Clear one breakpoint in the user program. We copy what the hardware
354 * does and use bit 0 of the address to indicate whether this is a Thumb
355 * breakpoint or an ARM breakpoint.
357 static void clear_breakpoint(struct task_struct
*task
, struct debug_entry
*bp
)
359 unsigned long addr
= bp
->address
;
363 ret
= swap_insn(task
, addr
& ~3, &old_insn
,
366 if (ret
!= 4 || old_insn
!= BREAKINST_ARM
)
367 printk(KERN_ERR
"%s:%d: corrupted ARM breakpoint at "
368 "0x%08lx (0x%08x)\n", task
->comm
, task
->pid
,
372 void ptrace_set_bpt(struct task_struct
*child
)
374 struct pt_regs
*regs
;
379 regs
= get_user_regs(child
);
380 pc
= instruction_pointer(regs
);
382 res
= read_instr(child
, pc
, &insn
);
384 struct debug_info
*dbg
= &child
->thread
.debug
;
389 alt
= get_branch_address(child
, pc
, insn
);
391 add_breakpoint(child
, dbg
, alt
);
394 * Note that we ignore the result of setting the above
395 * breakpoint since it may fail. When it does, this is
396 * not so much an error, but a forewarning that we may
397 * be receiving a prefetch abort shortly.
399 * If we don't set this breakpoint here, then we can
400 * lose control of the thread during single stepping.
402 if (!alt
|| predicate(insn
) != PREDICATE_ALWAYS
)
403 add_breakpoint(child
, dbg
, pc
+ 4);
408 * Ensure no single-step breakpoint is pending. Returns non-zero
409 * value if child was being single-stepped.
411 void ptrace_cancel_bpt(struct task_struct
*child
)
413 int i
, nsaved
= child
->thread
.debug
.nsaved
;
415 child
->thread
.debug
.nsaved
= 0;
418 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved
);
422 for (i
= 0; i
< nsaved
; i
++)
423 clear_breakpoint(child
, &child
->thread
.debug
.bp
[i
]);
427 * Called by kernel/ptrace.c when detaching..
429 * Make sure the single step bit is not set.
431 void ptrace_disable(struct task_struct
*child
)
433 child
->ptrace
&= ~PT_SINGLESTEP
;
434 ptrace_cancel_bpt(child
);
438 * Handle hitting a breakpoint.
440 void ptrace_break(struct task_struct
*tsk
, struct pt_regs
*regs
)
445 * The PC is always left pointing at the next instruction. Fix this.
449 if (tsk
->thread
.debug
.nsaved
== 0)
450 printk(KERN_ERR
"ptrace: bogus breakpoint trap\n");
452 ptrace_cancel_bpt(tsk
);
454 info
.si_signo
= SIGTRAP
;
456 info
.si_code
= TRAP_BRKPT
;
457 info
.si_addr
= (void *)instruction_pointer(regs
) - 4;
459 force_sig_info(SIGTRAP
, &info
, tsk
);
463 * Read the word at offset "off" into the "struct user". We
464 * actually access the pt_regs stored on the kernel stack.
466 static int ptrace_read_user(struct task_struct
*tsk
, unsigned long off
,
471 if (off
& 3 || off
>= sizeof(struct user
))
475 if (off
< sizeof(struct pt_regs
))
476 tmp
= get_user_reg(tsk
, off
>> 2);
478 return put_user(tmp
, ret
);
482 * Write the word at offset "off" into "struct user". We
483 * actually access the pt_regs stored on the kernel stack.
485 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long off
,
488 if (off
& 3 || off
>= sizeof(struct user
))
491 if (off
>= sizeof(struct pt_regs
))
494 return put_user_reg(tsk
, off
>> 2, val
);
498 * Get all user integer registers.
500 static int ptrace_getregs(struct task_struct
*tsk
, void *uregs
)
502 struct pt_regs
*regs
= get_user_regs(tsk
);
504 return copy_to_user(uregs
, regs
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
508 * Set all user integer registers.
510 static int ptrace_setregs(struct task_struct
*tsk
, void *uregs
)
512 struct pt_regs newregs
;
516 if (copy_from_user(&newregs
, uregs
, sizeof(struct pt_regs
)) == 0) {
517 struct pt_regs
*regs
= get_user_regs(tsk
);
520 if (valid_user_regs(&newregs
)) {
530 * Get the child FPU state.
532 static int ptrace_getfpregs(struct task_struct
*tsk
, void *ufp
)
534 return copy_to_user(ufp
, &tsk
->thread_info
->fpstate
,
535 sizeof(struct user_fp
)) ? -EFAULT
: 0;
539 * Set the child FPU state.
541 static int ptrace_setfpregs(struct task_struct
*tsk
, void *ufp
)
543 set_stopped_child_used_math(tsk
);
544 return copy_from_user(&tsk
->thread_info
->fpstate
, ufp
,
545 sizeof(struct user_fp
)) ? -EFAULT
: 0;
548 static int do_ptrace(int request
, struct task_struct
*child
, long addr
, long data
)
555 * read word at location "addr" in the child process.
557 case PTRACE_PEEKTEXT
:
558 case PTRACE_PEEKDATA
:
559 ret
= access_process_vm(child
, addr
, &tmp
,
560 sizeof(unsigned long), 0);
561 if (ret
== sizeof(unsigned long))
562 ret
= put_user(tmp
, (unsigned long *) data
);
568 ret
= ptrace_read_user(child
, addr
, (unsigned long *)data
);
572 * write the word at location addr.
574 case PTRACE_POKETEXT
:
575 case PTRACE_POKEDATA
:
576 ret
= access_process_vm(child
, addr
, &data
,
577 sizeof(unsigned long), 1);
578 if (ret
== sizeof(unsigned long))
585 ret
= ptrace_write_user(child
, addr
, data
);
589 * continue/restart and stop at next (return from) syscall
594 if ((unsigned long) data
> _NSIG
)
596 if (request
== PTRACE_SYSCALL
)
597 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
599 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
600 child
->exit_code
= data
;
601 /* make sure single-step breakpoint is gone. */
602 child
->ptrace
&= ~PT_SINGLESTEP
;
603 ptrace_cancel_bpt(child
);
604 wake_up_process(child
);
609 * make the child exit. Best I can do is send it a sigkill.
610 * perhaps it should be put in the status that it wants to
614 /* make sure single-step breakpoint is gone. */
615 child
->ptrace
&= ~PT_SINGLESTEP
;
616 ptrace_cancel_bpt(child
);
617 if (child
->exit_state
!= EXIT_ZOMBIE
) {
618 child
->exit_code
= SIGKILL
;
619 wake_up_process(child
);
625 * execute single instruction.
627 case PTRACE_SINGLESTEP
:
629 if ((unsigned long) data
> _NSIG
)
631 child
->ptrace
|= PT_SINGLESTEP
;
632 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
633 child
->exit_code
= data
;
634 /* give it a chance to run. */
635 wake_up_process(child
);
640 ret
= ptrace_detach(child
, data
);
644 ret
= ptrace_getregs(child
, (void *)data
);
648 ret
= ptrace_setregs(child
, (void *)data
);
651 case PTRACE_GETFPREGS
:
652 ret
= ptrace_getfpregs(child
, (void *)data
);
655 case PTRACE_SETFPREGS
:
656 ret
= ptrace_setfpregs(child
, (void *)data
);
660 ret
= ptrace_request(child
, request
, addr
, data
);
667 asmlinkage
int sys_ptrace(long request
, long pid
, long addr
, long data
)
669 struct task_struct
*child
;
674 if (request
== PTRACE_TRACEME
) {
675 /* are we already being traced? */
676 if (current
->ptrace
& PT_PTRACED
)
678 ret
= security_ptrace(current
->parent
, current
);
681 /* set the ptrace bit in the process flags. */
682 current
->ptrace
|= PT_PTRACED
;
687 read_lock(&tasklist_lock
);
688 child
= find_task_by_pid(pid
);
690 get_task_struct(child
);
691 read_unlock(&tasklist_lock
);
696 if (pid
== 1) /* you may not mess with init */
699 if (request
== PTRACE_ATTACH
) {
700 ret
= ptrace_attach(child
);
703 ret
= ptrace_check_attach(child
, request
== PTRACE_KILL
);
705 ret
= do_ptrace(request
, child
, addr
, data
);
708 put_task_struct(child
);
714 asmlinkage
void syscall_trace(int why
, struct pt_regs
*regs
)
718 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
720 if (!(current
->ptrace
& PT_PTRACED
))
724 * Save IP. IP is used to denote syscall entry/exit:
725 * IP = 0 -> entry, = 1 -> exit
730 /* the 0x80 provides a way for the tracing parent to distinguish
731 between a syscall stop and SIGTRAP delivery */
732 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
735 * this isn't the same as continuing with a signal, but it will do
736 * for normal use. strace only continues with a signal if the
737 * stopping signal is not SIGTRAP. -brl
739 if (current
->exit_code
) {
740 send_sig(current
->exit_code
, current
, 1);
741 current
->exit_code
= 0;