Linux 2.6.25.3
[linux/fpc-iii.git] / arch / blackfin / kernel / ptrace.c
blob85caf9b711a16ff59eb069a42ee9fb29491a589b
1 /*
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
6 * Created: 1/23/92
7 * Description:
9 * Modified:
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>
32 #include <linux/mm.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>
41 #include <asm/page.h>
42 #include <asm/pgtable.h>
43 #include <asm/system.h>
44 #include <asm/processor.h>
45 #include <asm/asm-offsets.h>
46 #include <asm/dma.h>
47 #include <asm/fixed_code.h>
49 #define MAX_SHARED_LIBS 3
50 #define TEXT_OFFSET 0
52 * does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
56 /* determines which bits in the SYSCFG reg the user has access to. */
57 /* 1 = access 0 = no access */
58 #define SYSCFG_MASK 0x0007 /* SYSCFG reg */
59 /* sets the trace bits. */
60 #define TRACE_BITS 0x0001
62 /* Find the stack offset for a register, relative to thread.esp0. */
63 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
66 * Get the address of the live pt_regs for the specified task.
67 * These are saved onto the top kernel stack when the process
68 * is not running.
70 * Note: if a user thread is execve'd from kernel space, the
71 * kernel stack will not be empty on entry to the kernel, so
72 * ptracing these tasks will fail.
74 static inline struct pt_regs *get_user_regs(struct task_struct *task)
76 return (struct pt_regs *)
77 ((unsigned long)task_stack_page(task) +
78 (THREAD_SIZE - sizeof(struct pt_regs)));
82 * Get all user integer registers.
84 static inline int ptrace_getregs(struct task_struct *tsk, void __user * uregs)
86 struct pt_regs *regs = get_user_regs(tsk);
87 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
90 /* Mapping from PT_xxx to the stack offset at which the register is
91 * saved. Notice that usp has no stack-slot and needs to be treated
92 * specially (see get_reg/put_reg below).
96 * Get contents of register REGNO in task TASK.
98 static inline long get_reg(struct task_struct *task, int regno)
100 unsigned char *reg_ptr;
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;
107 switch (regno) {
108 case PT_USP:
109 return task->thread.usp;
110 default:
111 if (regno <= 216)
112 return *(long *)(reg_ptr + regno);
114 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
116 printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
117 return 0;
121 * Write contents of register REGNO in task TASK.
123 static inline int
124 put_reg(struct task_struct *task, int regno, unsigned long data)
126 char *reg_ptr;
128 struct pt_regs *regs =
129 (struct pt_regs *)((unsigned long)task_stack_page(task) +
130 (THREAD_SIZE - sizeof(struct pt_regs)));
131 reg_ptr = (char *)regs;
133 switch (regno) {
134 case PT_PC:
135 /*********************************************************************/
136 /* At this point the kernel is most likely in exception. */
137 /* The RETX register will be used to populate the pc of the process. */
138 /*********************************************************************/
139 regs->retx = data;
140 regs->pc = data;
141 break;
142 case PT_RETX:
143 break; /* regs->retx = data; break; */
144 case PT_USP:
145 regs->usp = data;
146 task->thread.usp = data;
147 break;
148 default:
149 if (regno <= 216)
150 *(long *)(reg_ptr + regno) = data;
152 return 0;
156 * check that an address falls within the bounds of the target process's memory mappings
158 static inline int is_user_addr_valid(struct task_struct *child,
159 unsigned long start, unsigned long len)
161 struct vm_list_struct *vml;
162 struct sram_list_struct *sraml;
164 for (vml = child->mm->context.vmlist; vml; vml = vml->next)
165 if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end)
166 return 0;
168 for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
169 if (start >= (unsigned long)sraml->addr
170 && start + len <= (unsigned long)sraml->addr + sraml->length)
171 return 0;
173 if (start >= FIXED_CODE_START && start + len <= FIXED_CODE_END)
174 return 0;
176 return -EIO;
180 * Called by kernel/ptrace.c when detaching..
182 * Make sure the single step bit is not set.
184 void ptrace_disable(struct task_struct *child)
186 unsigned long tmp;
187 /* make sure the single step bit is not set. */
188 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
189 put_reg(child, PT_SR, tmp);
192 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
194 int ret;
195 int add = 0;
197 switch (request) {
198 /* when I and D space are separate, these will need to be fixed. */
199 case PTRACE_PEEKDATA:
200 pr_debug("ptrace: PEEKDATA\n");
201 add = MAX_SHARED_LIBS * 4; /* space between text and data */
202 /* fall through */
203 case PTRACE_PEEKTEXT: /* read word at location addr. */
205 unsigned long tmp = 0;
206 int copied;
208 ret = -EIO;
209 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + add %d %ld\n", addr, add,
210 sizeof(data));
211 if (is_user_addr_valid(child, addr + add, sizeof(tmp)) < 0)
212 break;
213 pr_debug("ptrace: user address is valid\n");
215 #if L1_CODE_LENGTH != 0
216 if (addr + add >= L1_CODE_START
217 && addr + add + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) {
218 safe_dma_memcpy (&tmp, (const void *)(addr + add), sizeof(tmp));
219 copied = sizeof(tmp);
220 } else
221 #endif
222 if (addr + add >= FIXED_CODE_START
223 && addr + add + sizeof(tmp) <= FIXED_CODE_END) {
224 memcpy(&tmp, (const void *)(addr + add), sizeof(tmp));
225 copied = sizeof(tmp);
226 } else
227 copied = access_process_vm(child, addr + add, &tmp,
228 sizeof(tmp), 0);
229 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
230 if (copied != sizeof(tmp))
231 break;
232 ret = put_user(tmp, (unsigned long *)data);
233 break;
236 /* read the word at location addr in the USER area. */
237 case PTRACE_PEEKUSR:
239 unsigned long tmp;
240 ret = -EIO;
241 tmp = 0;
242 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
243 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
244 "0 - %x sizeof(pt_regs) is %lx\n",
245 (int)addr, sizeof(struct pt_regs));
246 break;
248 if (addr == sizeof(struct pt_regs)) {
249 /* PT_TEXT_ADDR */
250 tmp = child->mm->start_code + TEXT_OFFSET;
251 } else if (addr == (sizeof(struct pt_regs) + 4)) {
252 /* PT_TEXT_END_ADDR */
253 tmp = child->mm->end_code;
254 } else if (addr == (sizeof(struct pt_regs) + 8)) {
255 /* PT_DATA_ADDR */
256 tmp = child->mm->start_data;
257 #ifdef CONFIG_BINFMT_ELF_FDPIC
258 } else if (addr == (sizeof(struct pt_regs) + 12)) {
259 tmp = child->mm->context.exec_fdpic_loadmap;
260 } else if (addr == (sizeof(struct pt_regs) + 16)) {
261 tmp = child->mm->context.interp_fdpic_loadmap;
262 #endif
263 } else {
264 tmp = get_reg(child, addr);
266 ret = put_user(tmp, (unsigned long *)data);
267 break;
270 /* when I and D space are separate, this will have to be fixed. */
271 case PTRACE_POKEDATA:
272 printk(KERN_NOTICE "ptrace: PTRACE_PEEKDATA\n");
273 /* fall through */
274 case PTRACE_POKETEXT: /* write the word at location addr. */
276 int copied;
278 ret = -EIO;
279 pr_debug("ptrace: POKETEXT at addr 0x%08lx + add %d %ld bytes %lx\n",
280 addr, add, sizeof(data), data);
281 if (is_user_addr_valid(child, addr + add, sizeof(data)) < 0)
282 break;
283 pr_debug("ptrace: user address is valid\n");
285 #if L1_CODE_LENGTH != 0
286 if (addr + add >= L1_CODE_START
287 && addr + add + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) {
288 safe_dma_memcpy ((void *)(addr + add), &data, sizeof(data));
289 copied = sizeof(data);
290 } else
291 #endif
292 if (addr + add >= FIXED_CODE_START
293 && addr + add + sizeof(data) <= FIXED_CODE_END) {
294 memcpy((void *)(addr + add), &data, sizeof(data));
295 copied = sizeof(data);
296 } else
297 copied = access_process_vm(child, addr + add, &data,
298 sizeof(data), 1);
299 pr_debug("ptrace: copied size %d\n", copied);
300 if (copied != sizeof(data))
301 break;
302 ret = 0;
303 break;
306 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
307 ret = -EIO;
308 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
309 printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
310 break;
313 if (addr >= (sizeof(struct pt_regs))) {
314 ret = 0;
315 break;
317 if (addr == PT_SYSCFG) {
318 data &= SYSCFG_MASK;
319 data |= get_reg(child, PT_SYSCFG);
321 ret = put_reg(child, addr, data);
322 break;
324 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
325 case PTRACE_CONT:
326 { /* restart after signal. */
327 long tmp;
329 pr_debug("ptrace_cont\n");
331 ret = -EIO;
332 if (!valid_signal(data))
333 break;
334 if (request == PTRACE_SYSCALL)
335 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
336 else
337 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
339 child->exit_code = data;
340 /* make sure the single step bit is not set. */
341 tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
342 put_reg(child, PT_SYSCFG, tmp);
343 pr_debug("before wake_up_process\n");
344 wake_up_process(child);
345 ret = 0;
346 break;
350 * make the child exit. Best I can do is send it a sigkill.
351 * perhaps it should be put in the status that it wants to
352 * exit.
354 case PTRACE_KILL:
356 long tmp;
357 ret = 0;
358 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
359 break;
360 child->exit_code = SIGKILL;
361 /* make sure the single step bit is not set. */
362 tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS);
363 put_reg(child, PT_SYSCFG, tmp);
364 wake_up_process(child);
365 break;
368 case PTRACE_SINGLESTEP:
369 { /* set the trap flag. */
370 long tmp;
372 pr_debug("single step\n");
373 ret = -EIO;
374 if (!valid_signal(data))
375 break;
376 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
378 tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
379 put_reg(child, PT_SYSCFG, tmp);
381 child->exit_code = data;
382 /* give it a chance to run. */
383 wake_up_process(child);
384 ret = 0;
385 break;
388 case PTRACE_GETREGS:
391 /* Get all gp regs from the child. */
392 ret = ptrace_getregs(child, (void __user *)data);
393 break;
396 case PTRACE_SETREGS:
398 printk(KERN_NOTICE
399 "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
400 /* Set all gp regs in the child. */
401 ret = 0;
402 break;
404 default:
405 ret = ptrace_request(child, request, addr, data);
406 break;
409 return ret;
412 asmlinkage void syscall_trace(void)
415 if (!test_thread_flag(TIF_SYSCALL_TRACE))
416 return;
418 if (!(current->ptrace & PT_PTRACED))
419 return;
421 /* the 0x80 provides a way for the tracing parent to distinguish
422 * between a syscall stop and SIGTRAP delivery
424 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
425 ? 0x80 : 0));
428 * this isn't the same as continuing with a signal, but it will do
429 * for normal use. strace only continues with a signal if the
430 * stopping signal is not SIGTRAP. -brl
432 if (current->exit_code) {
433 send_sig(current->exit_code, current, 1);
434 current->exit_code = 0;