2 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
3 * these modifications are Copyright 2004-2009 Analog Devices Inc.
5 * Licensed under the GPL-2
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
11 #include <linux/smp.h>
12 #include <linux/errno.h>
13 #include <linux/ptrace.h>
14 #include <linux/user.h>
15 #include <linux/signal.h>
16 #include <linux/uaccess.h>
19 #include <asm/pgtable.h>
20 #include <asm/system.h>
21 #include <asm/processor.h>
22 #include <asm/asm-offsets.h>
24 #include <asm/fixed_code.h>
25 #include <asm/cacheflush.h>
26 #include <asm/mem_map.h>
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
34 /* determines which bits in the SYSCFG reg the user has access to. */
35 /* 1 = access 0 = no access */
36 #define SYSCFG_MASK 0x0007 /* SYSCFG reg */
37 /* sets the trace bits. */
38 #define TRACE_BITS 0x0001
40 /* Find the stack offset for a register, relative to thread.esp0. */
41 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
44 * Get the address of the live pt_regs for the specified task.
45 * These are saved onto the top kernel stack when the process
48 * Note: if a user thread is execve'd from kernel space, the
49 * kernel stack will not be empty on entry to the kernel, so
50 * ptracing these tasks will fail.
52 static inline struct pt_regs
*get_user_regs(struct task_struct
*task
)
54 return (struct pt_regs
*)
55 ((unsigned long)task_stack_page(task
) +
56 (THREAD_SIZE
- sizeof(struct pt_regs
)));
60 * Get all user integer registers.
62 static inline int ptrace_getregs(struct task_struct
*tsk
, void __user
*uregs
)
65 memcpy(®s
, get_user_regs(tsk
), sizeof(regs
));
66 regs
.usp
= tsk
->thread
.usp
;
67 return copy_to_user(uregs
, ®s
, sizeof(struct pt_regs
)) ? -EFAULT
: 0;
70 /* Mapping from PT_xxx to the stack offset at which the register is
71 * saved. Notice that usp has no stack-slot and needs to be treated
72 * specially (see get_reg/put_reg below).
76 * Get contents of register REGNO in task TASK.
78 static inline long get_reg(struct task_struct
*task
, int regno
)
80 unsigned char *reg_ptr
;
82 struct pt_regs
*regs
=
83 (struct pt_regs
*)((unsigned long)task_stack_page(task
) +
84 (THREAD_SIZE
- sizeof(struct pt_regs
)));
85 reg_ptr
= (char *)regs
;
89 return task
->thread
.usp
;
92 return *(long *)(reg_ptr
+ regno
);
94 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
96 printk(KERN_WARNING
"Request to get for unknown register %d\n", regno
);
101 * Write contents of register REGNO in task TASK.
104 put_reg(struct task_struct
*task
, int regno
, unsigned long data
)
108 struct pt_regs
*regs
=
109 (struct pt_regs
*)((unsigned long)task_stack_page(task
) +
110 (THREAD_SIZE
- sizeof(struct pt_regs
)));
111 reg_ptr
= (char *)regs
;
115 /*********************************************************************/
116 /* At this point the kernel is most likely in exception. */
117 /* The RETX register will be used to populate the pc of the process. */
118 /*********************************************************************/
123 break; /* regs->retx = data; break; */
126 task
->thread
.usp
= data
;
130 *(long *)(reg_ptr
+ regno
) = data
;
136 * check that an address falls within the bounds of the target process's memory mappings
138 static inline int is_user_addr_valid(struct task_struct
*child
,
139 unsigned long start
, unsigned long len
)
141 struct vm_area_struct
*vma
;
142 struct sram_list_struct
*sraml
;
145 if (start
+ len
< start
)
148 vma
= find_vma(child
->mm
, start
);
149 if (vma
&& start
>= vma
->vm_start
&& start
+ len
<= vma
->vm_end
)
152 for (sraml
= child
->mm
->context
.sram_list
; sraml
; sraml
= sraml
->next
)
153 if (start
>= (unsigned long)sraml
->addr
154 && start
+ len
< (unsigned long)sraml
->addr
+ sraml
->length
)
157 if (start
>= FIXED_CODE_START
&& start
+ len
< FIXED_CODE_END
)
163 void ptrace_enable(struct task_struct
*child
)
166 tmp
= get_reg(child
, PT_SYSCFG
) | (TRACE_BITS
);
167 put_reg(child
, PT_SYSCFG
, tmp
);
171 * Called by kernel/ptrace.c when detaching..
173 * Make sure the single step bit is not set.
175 void ptrace_disable(struct task_struct
*child
)
178 /* make sure the single step bit is not set. */
179 tmp
= get_reg(child
, PT_SYSCFG
) & ~TRACE_BITS
;
180 put_reg(child
, PT_SYSCFG
, tmp
);
183 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
186 unsigned long __user
*datap
= (unsigned long __user
*)data
;
187 void *paddr
= (void *)addr
;
190 /* when I and D space are separate, these will need to be fixed. */
191 case PTRACE_PEEKDATA
:
192 pr_debug("ptrace: PEEKDATA\n");
194 case PTRACE_PEEKTEXT
: /* read word at location addr. */
196 unsigned long tmp
= 0;
197 int copied
= 0, to_copy
= sizeof(tmp
);
200 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr
, to_copy
);
201 if (is_user_addr_valid(child
, addr
, to_copy
) < 0)
203 pr_debug("ptrace: user address is valid\n");
205 switch (bfin_mem_access_type(addr
, to_copy
)) {
206 case BFIN_MEM_ACCESS_CORE
:
207 case BFIN_MEM_ACCESS_CORE_ONLY
:
208 copied
= access_process_vm(child
, addr
, &tmp
,
213 /* hrm, why didn't that work ... maybe no mapping */
214 if (addr
>= FIXED_CODE_START
&&
215 addr
+ to_copy
<= FIXED_CODE_END
) {
216 copy_from_user_page(0, 0, 0, &tmp
, paddr
, to_copy
);
218 } else if (addr
>= BOOT_ROM_START
) {
219 memcpy(&tmp
, paddr
, to_copy
);
224 case BFIN_MEM_ACCESS_DMA
:
225 if (safe_dma_memcpy(&tmp
, paddr
, to_copy
))
228 case BFIN_MEM_ACCESS_ITEST
:
229 if (isram_memcpy(&tmp
, paddr
, to_copy
))
237 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied
, tmp
);
238 if (copied
== to_copy
)
239 ret
= put_user(tmp
, datap
);
243 /* read the word at location addr in the USER area. */
249 if ((addr
& 3) || (addr
> (sizeof(struct pt_regs
) + 16))) {
250 printk(KERN_WARNING
"ptrace error : PEEKUSR : temporarily returning "
251 "0 - %x sizeof(pt_regs) is %lx\n",
252 (int)addr
, sizeof(struct pt_regs
));
255 if (addr
== sizeof(struct pt_regs
)) {
257 tmp
= child
->mm
->start_code
+ TEXT_OFFSET
;
258 } else if (addr
== (sizeof(struct pt_regs
) + 4)) {
259 /* PT_TEXT_END_ADDR */
260 tmp
= child
->mm
->end_code
;
261 } else if (addr
== (sizeof(struct pt_regs
) + 8)) {
263 tmp
= child
->mm
->start_data
;
264 #ifdef CONFIG_BINFMT_ELF_FDPIC
265 } else if (addr
== (sizeof(struct pt_regs
) + 12)) {
266 goto case_PTRACE_GETFDPIC_EXEC
;
267 } else if (addr
== (sizeof(struct pt_regs
) + 16)) {
268 goto case_PTRACE_GETFDPIC_INTERP
;
271 tmp
= get_reg(child
, addr
);
273 ret
= put_user(tmp
, datap
);
277 #ifdef CONFIG_BINFMT_ELF_FDPIC
278 case PTRACE_GETFDPIC
: {
279 unsigned long tmp
= 0;
282 case_PTRACE_GETFDPIC_EXEC
:
283 case PTRACE_GETFDPIC_EXEC
:
284 tmp
= child
->mm
->context
.exec_fdpic_loadmap
;
286 case_PTRACE_GETFDPIC_INTERP
:
287 case PTRACE_GETFDPIC_INTERP
:
288 tmp
= child
->mm
->context
.interp_fdpic_loadmap
;
294 ret
= put_user(tmp
, datap
);
299 /* when I and D space are separate, this will have to be fixed. */
300 case PTRACE_POKEDATA
:
301 pr_debug("ptrace: PTRACE_PEEKDATA\n");
303 case PTRACE_POKETEXT
: /* write the word at location addr. */
305 int copied
= 0, to_copy
= sizeof(data
);
308 pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
309 addr
, to_copy
, data
);
310 if (is_user_addr_valid(child
, addr
, to_copy
) < 0)
312 pr_debug("ptrace: user address is valid\n");
314 switch (bfin_mem_access_type(addr
, to_copy
)) {
315 case BFIN_MEM_ACCESS_CORE
:
316 case BFIN_MEM_ACCESS_CORE_ONLY
:
317 copied
= access_process_vm(child
, addr
, &data
,
322 /* hrm, why didn't that work ... maybe no mapping */
323 if (addr
>= FIXED_CODE_START
&&
324 addr
+ to_copy
<= FIXED_CODE_END
) {
325 copy_to_user_page(0, 0, 0, paddr
, &data
, to_copy
);
327 } else if (addr
>= BOOT_ROM_START
) {
328 memcpy(paddr
, &data
, to_copy
);
333 case BFIN_MEM_ACCESS_DMA
:
334 if (safe_dma_memcpy(paddr
, &data
, to_copy
))
337 case BFIN_MEM_ACCESS_ITEST
:
338 if (isram_memcpy(paddr
, &data
, to_copy
))
346 pr_debug("ptrace: copied size %d\n", copied
);
347 if (copied
== to_copy
)
352 case PTRACE_POKEUSR
: /* write the word at location addr in the USER area */
354 if ((addr
& 3) || (addr
> (sizeof(struct pt_regs
) + 16))) {
355 printk(KERN_WARNING
"ptrace error : POKEUSR: temporarily returning 0\n");
359 if (addr
>= (sizeof(struct pt_regs
))) {
363 if (addr
== PT_SYSCFG
) {
365 data
|= get_reg(child
, PT_SYSCFG
);
367 ret
= put_reg(child
, addr
, data
);
370 case PTRACE_SYSCALL
: /* continue and stop at next (return from) syscall */
371 case PTRACE_CONT
: /* restart after signal. */
372 pr_debug("ptrace: syscall/cont\n");
375 if (!valid_signal(data
))
377 if (request
== PTRACE_SYSCALL
)
378 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
380 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
381 child
->exit_code
= data
;
382 ptrace_disable(child
);
383 pr_debug("ptrace: before wake_up_process\n");
384 wake_up_process(child
);
389 * make the child exit. Best I can do is send it a sigkill.
390 * perhaps it should be put in the status that it wants to
395 if (child
->exit_state
== EXIT_ZOMBIE
) /* already dead */
397 child
->exit_code
= SIGKILL
;
398 ptrace_disable(child
);
399 wake_up_process(child
);
402 case PTRACE_SINGLESTEP
: /* set the trap flag. */
403 pr_debug("ptrace: single step\n");
405 if (!valid_signal(data
))
407 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
408 ptrace_enable(child
);
409 child
->exit_code
= data
;
410 wake_up_process(child
);
415 /* Get all gp regs from the child. */
416 ret
= ptrace_getregs(child
, datap
);
420 printk(KERN_WARNING
"ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
421 /* Set all gp regs in the child. */
426 ret
= ptrace_request(child
, request
, addr
, data
);
433 asmlinkage
void syscall_trace(void)
435 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
438 if (!(current
->ptrace
& PT_PTRACED
))
441 /* the 0x80 provides a way for the tracing parent to distinguish
442 * between a syscall stop and SIGTRAP delivery
444 ptrace_notify(SIGTRAP
| ((current
->ptrace
& PT_TRACESYSGOOD
)
448 * this isn't the same as continuing with a signal, but it will do
449 * for normal use. strace only continues with a signal if the
450 * stopping signal is not SIGTRAP. -brl
452 if (current
->exit_code
) {
453 send_sig(current
->exit_code
, current
, 1);
454 current
->exit_code
= 0;