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 /* Find the stack offset for a register, relative to thread.esp0. */
35 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
38 * Get the address of the live pt_regs for the specified task.
39 * These are saved onto the top kernel stack when the process
42 * Note: if a user thread is execve'd from kernel space, the
43 * kernel stack will not be empty on entry to the kernel, so
44 * ptracing these tasks will fail.
46 static inline struct pt_regs *get_user_regs(struct task_struct *task)
48 return (struct pt_regs *)
49 ((unsigned long)task_stack_page(task) +
50 (THREAD_SIZE - sizeof(struct pt_regs)));
54 * Get all user integer registers.
56 static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
59 memcpy(®s, get_user_regs(tsk), sizeof(regs));
60 regs.usp = tsk->thread.usp;
61 return copy_to_user(uregs, ®s, sizeof(struct pt_regs)) ? -EFAULT : 0;
64 /* Mapping from PT_xxx to the stack offset at which the register is
65 * saved. Notice that usp has no stack-slot and needs to be treated
66 * specially (see get_reg/put_reg below).
70 * Get contents of register REGNO in task TASK.
72 static inline long get_reg(struct task_struct *task, int regno)
74 unsigned char *reg_ptr;
76 struct pt_regs *regs =
77 (struct pt_regs *)((unsigned long)task_stack_page(task) +
78 (THREAD_SIZE - sizeof(struct pt_regs)));
79 reg_ptr = (char *)regs;
83 return task->thread.usp;
86 return *(long *)(reg_ptr + regno);
88 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
90 printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
95 * Write contents of register REGNO in task TASK.
98 put_reg(struct task_struct *task, int regno, unsigned long data)
102 struct pt_regs *regs =
103 (struct pt_regs *)((unsigned long)task_stack_page(task) +
104 (THREAD_SIZE - sizeof(struct pt_regs)));
105 reg_ptr = (char *)regs;
109 /*********************************************************************/
110 /* At this point the kernel is most likely in exception. */
111 /* The RETX register will be used to populate the pc of the process. */
112 /*********************************************************************/
117 break; /* regs->retx = data; break; */
120 task->thread.usp = data;
124 *(long *)(reg_ptr + regno) = data;
130 * check that an address falls within the bounds of the target process's memory mappings
132 static inline int is_user_addr_valid(struct task_struct *child,
133 unsigned long start, unsigned long len)
135 struct vm_area_struct *vma;
136 struct sram_list_struct *sraml;
139 if (start + len < start)
142 vma = find_vma(child->mm, start);
143 if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
146 for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
147 if (start >= (unsigned long)sraml->addr
148 && start + len < (unsigned long)sraml->addr + sraml->length)
151 if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
157 void ptrace_enable(struct task_struct *child)
159 struct pt_regs *regs = task_pt_regs(child);
160 regs->syscfg |= SYSCFG_SSSTEP;
164 * Called by kernel/ptrace.c when detaching..
166 * Make sure the single step bit is not set.
168 void ptrace_disable(struct task_struct *child)
170 struct pt_regs *regs = task_pt_regs(child);
171 regs->syscfg &= ~SYSCFG_SSSTEP;
174 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
177 unsigned long __user *datap = (unsigned long __user *)data;
178 void *paddr = (void *)addr;
181 /* when I and D space are separate, these will need to be fixed. */
182 case PTRACE_PEEKDATA:
183 pr_debug("ptrace: PEEKDATA\n");
185 case PTRACE_PEEKTEXT: /* read word at location addr. */
187 unsigned long tmp = 0;
188 int copied = 0, to_copy = sizeof(tmp);
191 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
192 if (is_user_addr_valid(child, addr, to_copy) < 0)
194 pr_debug("ptrace: user address is valid\n");
196 switch (bfin_mem_access_type(addr, to_copy)) {
197 case BFIN_MEM_ACCESS_CORE:
198 case BFIN_MEM_ACCESS_CORE_ONLY:
199 copied = access_process_vm(child, addr, &tmp,
204 /* hrm, why didn't that work ... maybe no mapping */
205 if (addr >= FIXED_CODE_START &&
206 addr + to_copy <= FIXED_CODE_END) {
207 copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
209 } else if (addr >= BOOT_ROM_START) {
210 memcpy(&tmp, paddr, to_copy);
215 case BFIN_MEM_ACCESS_DMA:
216 if (safe_dma_memcpy(&tmp, paddr, to_copy))
219 case BFIN_MEM_ACCESS_ITEST:
220 if (isram_memcpy(&tmp, paddr, to_copy))
228 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
229 if (copied == to_copy)
230 ret = put_user(tmp, datap);
234 /* read the word at location addr in the USER area. */
240 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
241 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
242 "0 - %x sizeof(pt_regs) is %lx\n",
243 (int)addr, sizeof(struct pt_regs));
246 if (addr == sizeof(struct pt_regs)) {
248 tmp = child->mm->start_code + TEXT_OFFSET;
249 } else if (addr == (sizeof(struct pt_regs) + 4)) {
250 /* PT_TEXT_END_ADDR */
251 tmp = child->mm->end_code;
252 } else if (addr == (sizeof(struct pt_regs) + 8)) {
254 tmp = child->mm->start_data;
255 #ifdef CONFIG_BINFMT_ELF_FDPIC
256 } else if (addr == (sizeof(struct pt_regs) + 12)) {
257 goto case_PTRACE_GETFDPIC_EXEC;
258 } else if (addr == (sizeof(struct pt_regs) + 16)) {
259 goto case_PTRACE_GETFDPIC_INTERP;
262 tmp = get_reg(child, addr);
264 ret = put_user(tmp, datap);
268 #ifdef CONFIG_BINFMT_ELF_FDPIC
269 case PTRACE_GETFDPIC: {
270 unsigned long tmp = 0;
273 case_PTRACE_GETFDPIC_EXEC:
274 case PTRACE_GETFDPIC_EXEC:
275 tmp = child->mm->context.exec_fdpic_loadmap;
277 case_PTRACE_GETFDPIC_INTERP:
278 case PTRACE_GETFDPIC_INTERP:
279 tmp = child->mm->context.interp_fdpic_loadmap;
285 ret = put_user(tmp, datap);
290 /* when I and D space are separate, this will have to be fixed. */
291 case PTRACE_POKEDATA:
292 pr_debug("ptrace: PTRACE_PEEKDATA\n");
294 case PTRACE_POKETEXT: /* write the word at location addr. */
296 int copied = 0, to_copy = sizeof(data);
299 pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
300 addr, to_copy, data);
301 if (is_user_addr_valid(child, addr, to_copy) < 0)
303 pr_debug("ptrace: user address is valid\n");
305 switch (bfin_mem_access_type(addr, to_copy)) {
306 case BFIN_MEM_ACCESS_CORE:
307 case BFIN_MEM_ACCESS_CORE_ONLY:
308 copied = access_process_vm(child, addr, &data,
311 case BFIN_MEM_ACCESS_DMA:
312 if (safe_dma_memcpy(paddr, &data, to_copy))
315 case BFIN_MEM_ACCESS_ITEST:
316 if (isram_memcpy(paddr, &data, to_copy))
324 pr_debug("ptrace: copied size %d\n", copied);
325 if (copied == to_copy)
330 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
332 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
333 printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
337 /* Ignore writes to SYSCFG and other pseudo regs */
338 if (addr >= PT_SYSCFG) {
342 ret = put_reg(child, addr, data);
346 /* Get all gp regs from the child. */
347 ret = ptrace_getregs(child, datap);
351 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
352 /* Set all gp regs in the child. */
357 ret = ptrace_request(child, request, addr, data);
364 asmlinkage void syscall_trace(void)
366 if (!test_thread_flag(TIF_SYSCALL_TRACE))
369 if (!(current->ptrace & PT_PTRACED))
372 /* the 0x80 provides a way for the tracing parent to distinguish
373 * between a syscall stop and SIGTRAP delivery
375 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
379 * this isn't the same as continuing with a signal, but it will do
380 * for normal use. strace only continues with a signal if the
381 * stopping signal is not SIGTRAP. -brl
383 if (current->exit_code) {
384 send_sig(current->exit_code, current, 1);
385 current->exit_code = 0;