blob: 30f4828277ad8782ab8a909c7d98e5f6770bdfb4 [file] [log] [blame]
Bryan Wu1394f032007-05-06 14:50:22 -07001/*
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
5 *
6 * Created: 1/23/92
7 * Description:
8 *
9 * Modified:
10 * Copyright 2004-2006 Analog Devices Inc.
11 *
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 *
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.
18 *
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.
23 *
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
28 */
29
30#include <linux/kernel.h>
31#include <linux/sched.h>
32#include <linux/mm.h>
33#include <linux/smp.h>
Bryan Wu1394f032007-05-06 14:50:22 -070034#include <linux/errno.h>
35#include <linux/ptrace.h>
36#include <linux/user.h>
37#include <linux/signal.h>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080038#include <linux/uaccess.h>
Bryan Wu1394f032007-05-06 14:50:22 -070039
Bryan Wu1394f032007-05-06 14:50:22 -070040#include <asm/page.h>
41#include <asm/pgtable.h>
42#include <asm/system.h>
43#include <asm/processor.h>
44#include <asm/asm-offsets.h>
45#include <asm/dma.h>
Jie Zhang26156392007-08-05 16:25:23 +080046#include <asm/fixed_code.h>
Jie Zhang7786ce82009-03-05 18:50:26 +080047#include <asm/cacheflush.h>
Graf Yangdbc895f2009-01-07 23:14:39 +080048#include <asm/mem_map.h>
Bryan Wu1394f032007-05-06 14:50:22 -070049
Bryan Wu1394f032007-05-06 14:50:22 -070050#define TEXT_OFFSET 0
51/*
52 * does not yet catch signals sent when the child dies.
53 * in exit.c or in signal.c.
54 */
55
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
61
62/* Find the stack offset for a register, relative to thread.esp0. */
63#define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
64
65/*
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.
69 *
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.
73 */
74static inline struct pt_regs *get_user_regs(struct task_struct *task)
75{
76 return (struct pt_regs *)
Roman Zippelf7e42172007-05-09 02:35:17 -070077 ((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -070078 (THREAD_SIZE - sizeof(struct pt_regs)));
79}
80
81/*
82 * Get all user integer registers.
83 */
Mike Frysinger8a861762008-11-18 17:48:22 +080084static inline int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
Bryan Wu1394f032007-05-06 14:50:22 -070085{
Mike Frysinger8a861762008-11-18 17:48:22 +080086 struct pt_regs regs;
87 memcpy(&regs, get_user_regs(tsk), sizeof(regs));
88 regs.usp = tsk->thread.usp;
89 return copy_to_user(uregs, &regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
Bryan Wu1394f032007-05-06 14:50:22 -070090}
91
92/* Mapping from PT_xxx to the stack offset at which the register is
93 * saved. Notice that usp has no stack-slot and needs to be treated
94 * specially (see get_reg/put_reg below).
95 */
96
97/*
98 * Get contents of register REGNO in task TASK.
99 */
100static inline long get_reg(struct task_struct *task, int regno)
101{
102 unsigned char *reg_ptr;
103
104 struct pt_regs *regs =
Roman Zippelf7e42172007-05-09 02:35:17 -0700105 (struct pt_regs *)((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -0700106 (THREAD_SIZE - sizeof(struct pt_regs)));
107 reg_ptr = (char *)regs;
108
109 switch (regno) {
110 case PT_USP:
111 return task->thread.usp;
112 default:
113 if (regno <= 216)
114 return *(long *)(reg_ptr + regno);
115 }
116 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
117
118 printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
119 return 0;
120}
121
122/*
123 * Write contents of register REGNO in task TASK.
124 */
125static inline int
126put_reg(struct task_struct *task, int regno, unsigned long data)
127{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800128 char *reg_ptr;
Bryan Wu1394f032007-05-06 14:50:22 -0700129
130 struct pt_regs *regs =
Roman Zippelf7e42172007-05-09 02:35:17 -0700131 (struct pt_regs *)((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -0700132 (THREAD_SIZE - sizeof(struct pt_regs)));
133 reg_ptr = (char *)regs;
134
135 switch (regno) {
136 case PT_PC:
137 /*********************************************************************/
138 /* At this point the kernel is most likely in exception. */
139 /* The RETX register will be used to populate the pc of the process. */
140 /*********************************************************************/
141 regs->retx = data;
142 regs->pc = data;
143 break;
144 case PT_RETX:
145 break; /* regs->retx = data; break; */
146 case PT_USP:
147 regs->usp = data;
148 task->thread.usp = data;
149 break;
150 default:
151 if (regno <= 216)
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800152 *(long *)(reg_ptr + regno) = data;
Bryan Wu1394f032007-05-06 14:50:22 -0700153 }
154 return 0;
155}
156
157/*
158 * check that an address falls within the bounds of the target process's memory mappings
159 */
160static inline int is_user_addr_valid(struct task_struct *child,
161 unsigned long start, unsigned long len)
162{
David Howells8feae132009-01-08 12:04:47 +0000163 struct vm_area_struct *vma;
Bryan Wu1394f032007-05-06 14:50:22 -0700164 struct sram_list_struct *sraml;
165
Mike Frysinger3c08f1d2008-10-10 17:12:51 +0800166 /* overflow */
167 if (start + len < start)
168 return -EIO;
169
David Howells8feae132009-01-08 12:04:47 +0000170 vma = find_vma(child->mm, start);
171 if (vma && start >= vma->vm_start && start + len <= vma->vm_end)
Bryan Wu1394f032007-05-06 14:50:22 -0700172 return 0;
173
174 for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
175 if (start >= (unsigned long)sraml->addr
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800176 && start + len < (unsigned long)sraml->addr + sraml->length)
Bryan Wu1394f032007-05-06 14:50:22 -0700177 return 0;
178
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800179 if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
Jie Zhang26156392007-08-05 16:25:23 +0800180 return 0;
181
Bryan Wu1394f032007-05-06 14:50:22 -0700182 return -EIO;
183}
184
Mike Frysingercb4c1732008-10-09 15:21:05 +0800185void ptrace_enable(struct task_struct *child)
186{
187 unsigned long tmp;
188 tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
189 put_reg(child, PT_SYSCFG, tmp);
190}
191
Bryan Wu1394f032007-05-06 14:50:22 -0700192/*
193 * Called by kernel/ptrace.c when detaching..
194 *
195 * Make sure the single step bit is not set.
196 */
197void ptrace_disable(struct task_struct *child)
198{
199 unsigned long tmp;
200 /* make sure the single step bit is not set. */
Bernd Schmidt7d392702008-05-07 11:41:26 +0800201 tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
202 put_reg(child, PT_SYSCFG, tmp);
Bryan Wu1394f032007-05-06 14:50:22 -0700203}
204
205long arch_ptrace(struct task_struct *child, long request, long addr, long data)
206{
207 int ret;
Mike Frysinger0ddeeca2008-03-07 02:37:41 +0800208 unsigned long __user *datap = (unsigned long __user *)data;
Mike Frysingerc014e152009-06-24 20:02:58 -0400209 void *paddr = (void *)addr;
Bryan Wu1394f032007-05-06 14:50:22 -0700210
211 switch (request) {
212 /* when I and D space are separate, these will need to be fixed. */
213 case PTRACE_PEEKDATA:
214 pr_debug("ptrace: PEEKDATA\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700215 /* fall through */
216 case PTRACE_PEEKTEXT: /* read word at location addr. */
217 {
218 unsigned long tmp = 0;
Mike Frysingerc014e152009-06-24 20:02:58 -0400219 int copied = 0, to_copy = sizeof(tmp);
Bryan Wu1394f032007-05-06 14:50:22 -0700220
221 ret = -EIO;
Mike Frysingerc014e152009-06-24 20:02:58 -0400222 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %i\n", addr, to_copy);
223 if (is_user_addr_valid(child, addr, to_copy) < 0)
Bryan Wu1394f032007-05-06 14:50:22 -0700224 break;
225 pr_debug("ptrace: user address is valid\n");
226
Mike Frysingerc014e152009-06-24 20:02:58 -0400227 switch (bfin_mem_access_type(addr, to_copy)) {
228 case BFIN_MEM_ACCESS_CORE:
229 case BFIN_MEM_ACCESS_CORE_ONLY:
Mike Frysingerdabaad52008-10-09 15:17:36 +0800230 copied = access_process_vm(child, addr, &tmp,
Mike Frysingerc014e152009-06-24 20:02:58 -0400231 to_copy, 0);
232 if (copied)
233 break;
234
235 /* hrm, why didn't that work ... maybe no mapping */
236 if (addr >= FIXED_CODE_START &&
237 addr + to_copy <= FIXED_CODE_END) {
238 copy_from_user_page(0, 0, 0, &tmp, paddr, to_copy);
239 copied = to_copy;
240 } else if (addr >= BOOT_ROM_START) {
241 memcpy(&tmp, paddr, to_copy);
242 copied = to_copy;
243 }
244
245 break;
246 case BFIN_MEM_ACCESS_DMA:
247 if (safe_dma_memcpy(&tmp, paddr, to_copy))
248 copied = to_copy;
249 break;
250 case BFIN_MEM_ACCESS_ITEST:
251 if (isram_memcpy(&tmp, paddr, to_copy))
252 copied = to_copy;
253 break;
254 default:
255 copied = 0;
256 break;
257 }
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800258
Bryan Wu1394f032007-05-06 14:50:22 -0700259 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
Mike Frysingerc014e152009-06-24 20:02:58 -0400260 if (copied == to_copy)
261 ret = put_user(tmp, datap);
Bryan Wu1394f032007-05-06 14:50:22 -0700262 break;
263 }
264
265 /* read the word at location addr in the USER area. */
266 case PTRACE_PEEKUSR:
267 {
268 unsigned long tmp;
269 ret = -EIO;
270 tmp = 0;
271 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
272 printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning "
273 "0 - %x sizeof(pt_regs) is %lx\n",
274 (int)addr, sizeof(struct pt_regs));
275 break;
276 }
277 if (addr == sizeof(struct pt_regs)) {
278 /* PT_TEXT_ADDR */
279 tmp = child->mm->start_code + TEXT_OFFSET;
280 } else if (addr == (sizeof(struct pt_regs) + 4)) {
281 /* PT_TEXT_END_ADDR */
282 tmp = child->mm->end_code;
283 } else if (addr == (sizeof(struct pt_regs) + 8)) {
284 /* PT_DATA_ADDR */
285 tmp = child->mm->start_data;
286#ifdef CONFIG_BINFMT_ELF_FDPIC
287 } else if (addr == (sizeof(struct pt_regs) + 12)) {
Mike Frysinger53e18df2009-07-03 00:17:45 +0000288 goto case_PTRACE_GETFDPIC_EXEC;
Bryan Wu1394f032007-05-06 14:50:22 -0700289 } else if (addr == (sizeof(struct pt_regs) + 16)) {
Mike Frysinger53e18df2009-07-03 00:17:45 +0000290 goto case_PTRACE_GETFDPIC_INTERP;
Bryan Wu1394f032007-05-06 14:50:22 -0700291#endif
292 } else {
293 tmp = get_reg(child, addr);
294 }
Mike Frysinger0ddeeca2008-03-07 02:37:41 +0800295 ret = put_user(tmp, datap);
Bryan Wu1394f032007-05-06 14:50:22 -0700296 break;
297 }
298
Mike Frysinger53e18df2009-07-03 00:17:45 +0000299#ifdef CONFIG_BINFMT_ELF_FDPIC
300 case PTRACE_GETFDPIC: {
301 unsigned long tmp = 0;
302
303 switch (addr) {
304 case_PTRACE_GETFDPIC_EXEC:
305 case PTRACE_GETFDPIC_EXEC:
306 tmp = child->mm->context.exec_fdpic_loadmap;
307 break;
308 case_PTRACE_GETFDPIC_INTERP:
309 case PTRACE_GETFDPIC_INTERP:
310 tmp = child->mm->context.interp_fdpic_loadmap;
311 break;
312 default:
313 break;
314 }
315
316 ret = put_user(tmp, datap);
317 break;
318 }
319#endif
320
Bryan Wu1394f032007-05-06 14:50:22 -0700321 /* when I and D space are separate, this will have to be fixed. */
322 case PTRACE_POKEDATA:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800323 pr_debug("ptrace: PTRACE_PEEKDATA\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700324 /* fall through */
325 case PTRACE_POKETEXT: /* write the word at location addr. */
326 {
Mike Frysingerc014e152009-06-24 20:02:58 -0400327 int copied = 0, to_copy = sizeof(data);
Bryan Wu1394f032007-05-06 14:50:22 -0700328
329 ret = -EIO;
Mike Frysingerc014e152009-06-24 20:02:58 -0400330 pr_debug("ptrace: POKETEXT at addr 0x%08lx + %i bytes %lx\n",
331 addr, to_copy, data);
332 if (is_user_addr_valid(child, addr, to_copy) < 0)
Bryan Wu1394f032007-05-06 14:50:22 -0700333 break;
334 pr_debug("ptrace: user address is valid\n");
335
Mike Frysingerc014e152009-06-24 20:02:58 -0400336 switch (bfin_mem_access_type(addr, to_copy)) {
337 case BFIN_MEM_ACCESS_CORE:
338 case BFIN_MEM_ACCESS_CORE_ONLY:
Mike Frysingerdabaad52008-10-09 15:17:36 +0800339 copied = access_process_vm(child, addr, &data,
Mike Frysingerc014e152009-06-24 20:02:58 -0400340 to_copy, 0);
341 if (copied)
342 break;
343
344 /* hrm, why didn't that work ... maybe no mapping */
345 if (addr >= FIXED_CODE_START &&
346 addr + to_copy <= FIXED_CODE_END) {
347 copy_to_user_page(0, 0, 0, paddr, &data, to_copy);
348 copied = to_copy;
349 } else if (addr >= BOOT_ROM_START) {
350 memcpy(paddr, &data, to_copy);
351 copied = to_copy;
352 }
353
354 break;
355 case BFIN_MEM_ACCESS_DMA:
356 if (safe_dma_memcpy(paddr, &data, to_copy))
357 copied = to_copy;
358 break;
359 case BFIN_MEM_ACCESS_ITEST:
360 if (isram_memcpy(paddr, &data, to_copy))
361 copied = to_copy;
362 break;
363 default:
364 copied = 0;
365 break;
366 }
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800367
Bryan Wu1394f032007-05-06 14:50:22 -0700368 pr_debug("ptrace: copied size %d\n", copied);
Mike Frysingerc014e152009-06-24 20:02:58 -0400369 if (copied == to_copy)
370 ret = 0;
Bryan Wu1394f032007-05-06 14:50:22 -0700371 break;
372 }
373
374 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
375 ret = -EIO;
376 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
377 printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
378 break;
379 }
380
381 if (addr >= (sizeof(struct pt_regs))) {
382 ret = 0;
383 break;
384 }
385 if (addr == PT_SYSCFG) {
386 data &= SYSCFG_MASK;
387 data |= get_reg(child, PT_SYSCFG);
388 }
389 ret = put_reg(child, addr, data);
390 break;
391
392 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
Mike Frysingercb4c1732008-10-09 15:21:05 +0800393 case PTRACE_CONT: /* restart after signal. */
394 pr_debug("ptrace: syscall/cont\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700395
Mike Frysingercb4c1732008-10-09 15:21:05 +0800396 ret = -EIO;
397 if (!valid_signal(data))
Bryan Wu1394f032007-05-06 14:50:22 -0700398 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800399 if (request == PTRACE_SYSCALL)
400 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
401 else
402 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
403 child->exit_code = data;
404 ptrace_disable(child);
405 pr_debug("ptrace: before wake_up_process\n");
406 wake_up_process(child);
407 ret = 0;
408 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700409
410 /*
411 * make the child exit. Best I can do is send it a sigkill.
412 * perhaps it should be put in the status that it wants to
413 * exit.
414 */
415 case PTRACE_KILL:
Mike Frysingercb4c1732008-10-09 15:21:05 +0800416 ret = 0;
417 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
Bryan Wu1394f032007-05-06 14:50:22 -0700418 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800419 child->exit_code = SIGKILL;
420 ptrace_disable(child);
421 wake_up_process(child);
422 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700423
Mike Frysingercb4c1732008-10-09 15:21:05 +0800424 case PTRACE_SINGLESTEP: /* set the trap flag. */
425 pr_debug("ptrace: single step\n");
426 ret = -EIO;
427 if (!valid_signal(data))
Bryan Wu1394f032007-05-06 14:50:22 -0700428 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800429 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
430 ptrace_enable(child);
431 child->exit_code = data;
432 wake_up_process(child);
433 ret = 0;
434 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700435
Bryan Wu1394f032007-05-06 14:50:22 -0700436 case PTRACE_GETREGS:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800437 /* Get all gp regs from the child. */
438 ret = ptrace_getregs(child, datap);
439 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700440
441 case PTRACE_SETREGS:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800442 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
443 /* Set all gp regs in the child. */
444 ret = 0;
445 break;
446
Bryan Wu1394f032007-05-06 14:50:22 -0700447 default:
448 ret = ptrace_request(child, request, addr, data);
449 break;
450 }
451
452 return ret;
453}
454
455asmlinkage void syscall_trace(void)
456{
Bryan Wu1394f032007-05-06 14:50:22 -0700457 if (!test_thread_flag(TIF_SYSCALL_TRACE))
458 return;
459
460 if (!(current->ptrace & PT_PTRACED))
461 return;
462
463 /* the 0x80 provides a way for the tracing parent to distinguish
464 * between a syscall stop and SIGTRAP delivery
465 */
466 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
467 ? 0x80 : 0));
468
469 /*
470 * this isn't the same as continuing with a signal, but it will do
471 * for normal use. strace only continues with a signal if the
472 * stopping signal is not SIGTRAP. -brl
473 */
474 if (current->exit_code) {
475 send_sig(current->exit_code, current, 1);
476 current->exit_code = 0;
477 }
478}