2 * File: arch/blackfin/kernel/ptrace.c
3 * Based on: Taken from linux/kernel/ptrace.c
4 * Author: linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
10 * Copyright 2004-2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
33 #include <linux/smp.h>
34 #include <linux/smp_lock.h>
35 #include <linux/errno.h>
36 #include <linux/ptrace.h>
37 #include <linux/user.h>
38 #include <linux/signal.h>
39 #include <linux/uaccess.h>
42 #include <asm/pgtable.h>
43 #include <asm/system.h>
44 #include <asm/processor.h>
45 #include <asm/asm-offsets.h>
47 #include <asm/fixed_code.h>
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
55 /* determines which bits in the SYSCFG reg the user has access to. */
56 /* 1 = access 0 = no access */
57 #define SYSCFG_MASK 0x0007 /* SYSCFG reg */
58 /* sets the trace bits. */
59 #define TRACE_BITS 0x0001
61 /* Find the stack offset for a register, relative to thread.esp0. */
62 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
65 * Get the address of the live pt_regs for the specified task.
66 * These are saved onto the top kernel stack when the process
69 * Note: if a user thread is execve'd from kernel space, the
70 * kernel stack will not be empty on entry to the kernel, so
71 * ptracing these tasks will fail.
73 static inline struct pt_regs *get_user_regs(struct task_struct *task)
75 return (struct pt_regs *)
76 ((unsigned long)task_stack_page(task) +
77 (THREAD_SIZE - sizeof(struct pt_regs)));
81 * Get all user integer registers.
83 static inline int ptrace_getregs(struct task_struct *tsk, void __user * uregs)
85 struct pt_regs *regs = get_user_regs(tsk);
86 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
89 /* Mapping from PT_xxx to the stack offset at which the register is
90 * saved. Notice that usp has no stack-slot and needs to be treated
91 * specially (see get_reg/put_reg below).
95 * Get contents of register REGNO in task TASK.
97 static inline long get_reg(struct task_struct *task, int regno)
99 unsigned char *reg_ptr;
101 struct pt_regs *regs =
102 (struct pt_regs *)((unsigned long)task_stack_page(task) +
103 (THREAD_SIZE - sizeof(struct pt_regs)));
104 reg_ptr = (char *)regs;
108 return task->thread.usp;
111 return *(long *)(reg_ptr + regno);
113 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
115 printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
120 * Write contents of register REGNO in task TASK.
123 put_reg(struct task_struct *task, int regno, unsigned long data)
127 struct pt_regs *regs =
128 (struct pt_regs *)((unsigned long)task_stack_page(task) +
129 (THREAD_SIZE - sizeof(struct pt_regs)));
130 reg_ptr = (char *)regs;
134 /*********************************************************************/
135 /* At this point the kernel is most likely in exception. */
136 /* The RETX register will be used to populate the pc of the process. */
137 /*********************************************************************/
142 break; /* regs->retx = data; break; */
145 task->thread.usp = data;
149 *(long *)(reg_ptr + regno) = data;
155 * check that an address falls within the bounds of the target process's memory mappings
157 static inline int is_user_addr_valid(struct task_struct *child,
158 unsigned long start, unsigned long len)
160 struct vm_list_struct *vml;
161 struct sram_list_struct *sraml;
163 for (vml = child->mm->context.vmlist; vml; vml = vml->next)
164 if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end)
167 for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
168 if (start >= (unsigned long)sraml->addr
169 && start + len <= (unsigned long)sraml->addr + sraml->length)
172 if (start >= FIXED_CODE_START && start + len <= FIXED_CODE_END)
178 void ptrace_enable(struct task_struct *child)
181 tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
182 put_reg(child, PT_SYSCFG, tmp);
186 * Called by kernel/ptrace.c when detaching..
188 * Make sure the single step bit is not set.
190 void ptrace_disable(struct task_struct *child)
193 /* make sure the single step bit is not set. */
194 tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
195 put_reg(child, PT_SYSCFG, tmp);
198 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
201 unsigned long __user *datap = (unsigned long __user *)data;
204 /* when I and D space are separate, these will need to be fixed. */
205 case PTRACE_PEEKDATA:
206 pr_debug("ptrace: PEEKDATA\n");
208 case PTRACE_PEEKTEXT: /* read word at location addr. */
210 unsigned long tmp = 0;
214 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %ld\n", addr, sizeof(data));
215 if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0)
217 pr_debug("ptrace: user address is valid\n");
219 #if L1_CODE_LENGTH != 0
220 if (addr >= L1_CODE_START
221 && addr + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) {
222 safe_dma_memcpy (&tmp, (const void *)(addr), sizeof(tmp));
223 copied = sizeof(tmp);
226 #if L1_DATA_A_LENGTH != 0
227 if (addr >= L1_DATA_A_START
228 && addr + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
229 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
230 copied = sizeof(tmp);
233 #if L1_DATA_B_LENGTH != 0
234 if (addr >= L1_DATA_B_START
235 && addr + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
236 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
237 copied = sizeof(tmp);
240 if (addr >= FIXED_CODE_START
241 && addr + sizeof(tmp) <= FIXED_CODE_END) {
242 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
243 copied = sizeof(tmp);
245 copied = access_process_vm(child, addr, &tmp,
247 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
248 if (copied != sizeof(tmp))
250 ret = put_user(tmp, datap);
254 /* read the word at location addr in the USER area. */
260 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
261 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
262 "0 - %x sizeof(pt_regs) is %lx\n",
263 (int)addr, sizeof(struct pt_regs));
266 if (addr == sizeof(struct pt_regs)) {
268 tmp = child->mm->start_code + TEXT_OFFSET;
269 } else if (addr == (sizeof(struct pt_regs) + 4)) {
270 /* PT_TEXT_END_ADDR */
271 tmp = child->mm->end_code;
272 } else if (addr == (sizeof(struct pt_regs) + 8)) {
274 tmp = child->mm->start_data;
275 #ifdef CONFIG_BINFMT_ELF_FDPIC
276 } else if (addr == (sizeof(struct pt_regs) + 12)) {
277 tmp = child->mm->context.exec_fdpic_loadmap;
278 } else if (addr == (sizeof(struct pt_regs) + 16)) {
279 tmp = child->mm->context.interp_fdpic_loadmap;
282 tmp = get_reg(child, addr);
284 ret = put_user(tmp, datap);
288 /* when I and D space are separate, this will have to be fixed. */
289 case PTRACE_POKEDATA:
290 pr_debug("ptrace: PTRACE_PEEKDATA\n");
292 case PTRACE_POKETEXT: /* write the word at location addr. */
297 pr_debug("ptrace: POKETEXT at addr 0x%08lx + %ld bytes %lx\n",
298 addr, sizeof(data), data);
299 if (is_user_addr_valid(child, addr, sizeof(data)) < 0)
301 pr_debug("ptrace: user address is valid\n");
303 #if L1_CODE_LENGTH != 0
304 if (addr >= L1_CODE_START
305 && addr + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) {
306 safe_dma_memcpy ((void *)(addr), &data, sizeof(data));
307 copied = sizeof(data);
310 #if L1_DATA_A_LENGTH != 0
311 if (addr >= L1_DATA_A_START
312 && addr + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
313 memcpy((void *)(addr), &data, sizeof(data));
314 copied = sizeof(data);
317 #if L1_DATA_B_LENGTH != 0
318 if (addr >= L1_DATA_B_START
319 && addr + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
320 memcpy((void *)(addr), &data, sizeof(data));
321 copied = sizeof(data);
324 if (addr >= FIXED_CODE_START
325 && addr + sizeof(data) <= FIXED_CODE_END) {
326 memcpy((void *)(addr), &data, sizeof(data));
327 copied = sizeof(data);
329 copied = access_process_vm(child, addr, &data,
331 pr_debug("ptrace: copied size %d\n", copied);
332 if (copied != sizeof(data))
338 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
340 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
341 printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
345 if (addr >= (sizeof(struct pt_regs))) {
349 if (addr == PT_SYSCFG) {
351 data |= get_reg(child, PT_SYSCFG);
353 ret = put_reg(child, addr, data);
356 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
357 case PTRACE_CONT: /* restart after signal. */
358 pr_debug("ptrace: syscall/cont\n");
361 if (!valid_signal(data))
363 if (request == PTRACE_SYSCALL)
364 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
366 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
367 child->exit_code = data;
368 ptrace_disable(child);
369 pr_debug("ptrace: before wake_up_process\n");
370 wake_up_process(child);
375 * make the child exit. Best I can do is send it a sigkill.
376 * perhaps it should be put in the status that it wants to
381 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
383 child->exit_code = SIGKILL;
384 ptrace_disable(child);
385 wake_up_process(child);
388 case PTRACE_SINGLESTEP: /* set the trap flag. */
389 pr_debug("ptrace: single step\n");
391 if (!valid_signal(data))
393 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
394 ptrace_enable(child);
395 child->exit_code = data;
396 wake_up_process(child);
401 /* Get all gp regs from the child. */
402 ret = ptrace_getregs(child, datap);
406 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
407 /* Set all gp regs in the child. */
412 ret = ptrace_request(child, request, addr, data);
419 asmlinkage void syscall_trace(void)
421 if (!test_thread_flag(TIF_SYSCALL_TRACE))
424 if (!(current->ptrace & PT_PTRACED))
427 /* the 0x80 provides a way for the tracing parent to distinguish
428 * between a syscall stop and SIGTRAP delivery
430 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
434 * this isn't the same as continuing with a signal, but it will do
435 * for normal use. strace only continues with a signal if the
436 * stopping signal is not SIGTRAP. -brl
438 if (current->exit_code) {
439 send_sig(current->exit_code, current, 1);
440 current->exit_code = 0;