blob: 140bf00e99749f6fee7e9aec85149f9872c1d03f [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>
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>
Mike Frysinger1f83b8f2007-07-12 22:58:21 +080039#include <linux/uaccess.h>
Bryan Wu1394f032007-05-06 14:50:22 -070040
Bryan Wu1394f032007-05-06 14:50:22 -070041#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>
Jie Zhang26156392007-08-05 16:25:23 +080047#include <asm/fixed_code.h>
Bryan Wu1394f032007-05-06 14:50:22 -070048
Bryan Wu1394f032007-05-06 14:50:22 -070049#define TEXT_OFFSET 0
50/*
51 * does not yet catch signals sent when the child dies.
52 * in exit.c or in signal.c.
53 */
54
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
60
61/* Find the stack offset for a register, relative to thread.esp0. */
62#define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
63
64/*
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
67 * is not running.
68 *
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.
72 */
73static inline struct pt_regs *get_user_regs(struct task_struct *task)
74{
75 return (struct pt_regs *)
Roman Zippelf7e42172007-05-09 02:35:17 -070076 ((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -070077 (THREAD_SIZE - sizeof(struct pt_regs)));
78}
79
80/*
81 * Get all user integer registers.
82 */
83static inline int ptrace_getregs(struct task_struct *tsk, void __user * uregs)
84{
85 struct pt_regs *regs = get_user_regs(tsk);
86 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
87}
88
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).
92 */
93
94/*
95 * Get contents of register REGNO in task TASK.
96 */
97static inline long get_reg(struct task_struct *task, int regno)
98{
99 unsigned char *reg_ptr;
100
101 struct pt_regs *regs =
Roman Zippelf7e42172007-05-09 02:35:17 -0700102 (struct pt_regs *)((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -0700103 (THREAD_SIZE - sizeof(struct pt_regs)));
104 reg_ptr = (char *)regs;
105
106 switch (regno) {
107 case PT_USP:
108 return task->thread.usp;
109 default:
110 if (regno <= 216)
111 return *(long *)(reg_ptr + regno);
112 }
113 /* slight mystery ... never seems to come here but kernel misbehaves without this code! */
114
115 printk(KERN_WARNING "Request to get for unknown register %d\n", regno);
116 return 0;
117}
118
119/*
120 * Write contents of register REGNO in task TASK.
121 */
122static inline int
123put_reg(struct task_struct *task, int regno, unsigned long data)
124{
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800125 char *reg_ptr;
Bryan Wu1394f032007-05-06 14:50:22 -0700126
127 struct pt_regs *regs =
Roman Zippelf7e42172007-05-09 02:35:17 -0700128 (struct pt_regs *)((unsigned long)task_stack_page(task) +
Bryan Wu1394f032007-05-06 14:50:22 -0700129 (THREAD_SIZE - sizeof(struct pt_regs)));
130 reg_ptr = (char *)regs;
131
132 switch (regno) {
133 case PT_PC:
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 /*********************************************************************/
138 regs->retx = data;
139 regs->pc = data;
140 break;
141 case PT_RETX:
142 break; /* regs->retx = data; break; */
143 case PT_USP:
144 regs->usp = data;
145 task->thread.usp = data;
146 break;
147 default:
148 if (regno <= 216)
Mike Frysinger1f83b8f2007-07-12 22:58:21 +0800149 *(long *)(reg_ptr + regno) = data;
Bryan Wu1394f032007-05-06 14:50:22 -0700150 }
151 return 0;
152}
153
154/*
155 * check that an address falls within the bounds of the target process's memory mappings
156 */
157static inline int is_user_addr_valid(struct task_struct *child,
158 unsigned long start, unsigned long len)
159{
160 struct vm_list_struct *vml;
161 struct sram_list_struct *sraml;
162
Mike Frysinger3c08f1d2008-10-10 17:12:51 +0800163 /* overflow */
164 if (start + len < start)
165 return -EIO;
166
Bryan Wu1394f032007-05-06 14:50:22 -0700167 for (vml = child->mm->context.vmlist; vml; vml = vml->next)
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800168 if (start >= vml->vma->vm_start && start + len < vml->vma->vm_end)
Bryan Wu1394f032007-05-06 14:50:22 -0700169 return 0;
170
171 for (sraml = child->mm->context.sram_list; sraml; sraml = sraml->next)
172 if (start >= (unsigned long)sraml->addr
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800173 && start + len < (unsigned long)sraml->addr + sraml->length)
Bryan Wu1394f032007-05-06 14:50:22 -0700174 return 0;
175
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800176 if (start >= FIXED_CODE_START && start + len < FIXED_CODE_END)
Jie Zhang26156392007-08-05 16:25:23 +0800177 return 0;
178
Bryan Wu1394f032007-05-06 14:50:22 -0700179 return -EIO;
180}
181
Mike Frysingercb4c1732008-10-09 15:21:05 +0800182void ptrace_enable(struct task_struct *child)
183{
184 unsigned long tmp;
185 tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS);
186 put_reg(child, PT_SYSCFG, tmp);
187}
188
Bryan Wu1394f032007-05-06 14:50:22 -0700189/*
190 * Called by kernel/ptrace.c when detaching..
191 *
192 * Make sure the single step bit is not set.
193 */
194void ptrace_disable(struct task_struct *child)
195{
196 unsigned long tmp;
197 /* make sure the single step bit is not set. */
Bernd Schmidt7d392702008-05-07 11:41:26 +0800198 tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS;
199 put_reg(child, PT_SYSCFG, tmp);
Bryan Wu1394f032007-05-06 14:50:22 -0700200}
201
202long arch_ptrace(struct task_struct *child, long request, long addr, long data)
203{
204 int ret;
Mike Frysinger0ddeeca2008-03-07 02:37:41 +0800205 unsigned long __user *datap = (unsigned long __user *)data;
Bryan Wu1394f032007-05-06 14:50:22 -0700206
207 switch (request) {
208 /* when I and D space are separate, these will need to be fixed. */
209 case PTRACE_PEEKDATA:
210 pr_debug("ptrace: PEEKDATA\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700211 /* fall through */
212 case PTRACE_PEEKTEXT: /* read word at location addr. */
213 {
214 unsigned long tmp = 0;
215 int copied;
216
217 ret = -EIO;
Mike Frysingerdabaad52008-10-09 15:17:36 +0800218 pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %ld\n", addr, sizeof(data));
219 if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0)
Bryan Wu1394f032007-05-06 14:50:22 -0700220 break;
221 pr_debug("ptrace: user address is valid\n");
222
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800223 if (L1_CODE_LENGTH != 0 && addr >= L1_CODE_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800224 && addr + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) {
225 safe_dma_memcpy (&tmp, (const void *)(addr), sizeof(tmp));
Bryan Wu1394f032007-05-06 14:50:22 -0700226 copied = sizeof(tmp);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800227
228 } else if (L1_DATA_A_LENGTH != 0 && addr >= L1_DATA_A_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800229 && addr + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
230 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
Jie Zhang6546eae2008-07-15 16:15:40 +0800231 copied = sizeof(tmp);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800232
233 } else if (L1_DATA_B_LENGTH != 0 && addr >= L1_DATA_B_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800234 && addr + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
235 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
Jie Zhang6546eae2008-07-15 16:15:40 +0800236 copied = sizeof(tmp);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800237
238 } else if (addr >= FIXED_CODE_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800239 && addr + sizeof(tmp) <= FIXED_CODE_END) {
240 memcpy(&tmp, (const void *)(addr), sizeof(tmp));
Jie Zhang26156392007-08-05 16:25:23 +0800241 copied = sizeof(tmp);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800242
Jie Zhang26156392007-08-05 16:25:23 +0800243 } else
Mike Frysingerdabaad52008-10-09 15:17:36 +0800244 copied = access_process_vm(child, addr, &tmp,
Jie Zhang26156392007-08-05 16:25:23 +0800245 sizeof(tmp), 0);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800246
Bryan Wu1394f032007-05-06 14:50:22 -0700247 pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp);
248 if (copied != sizeof(tmp))
249 break;
Mike Frysinger0ddeeca2008-03-07 02:37:41 +0800250 ret = put_user(tmp, datap);
Bryan Wu1394f032007-05-06 14:50:22 -0700251 break;
252 }
253
254 /* read the word at location addr in the USER area. */
255 case PTRACE_PEEKUSR:
256 {
257 unsigned long tmp;
258 ret = -EIO;
259 tmp = 0;
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));
264 break;
265 }
266 if (addr == sizeof(struct pt_regs)) {
267 /* PT_TEXT_ADDR */
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)) {
273 /* PT_DATA_ADDR */
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;
280#endif
281 } else {
282 tmp = get_reg(child, addr);
283 }
Mike Frysinger0ddeeca2008-03-07 02:37:41 +0800284 ret = put_user(tmp, datap);
Bryan Wu1394f032007-05-06 14:50:22 -0700285 break;
286 }
287
288 /* when I and D space are separate, this will have to be fixed. */
289 case PTRACE_POKEDATA:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800290 pr_debug("ptrace: PTRACE_PEEKDATA\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700291 /* fall through */
292 case PTRACE_POKETEXT: /* write the word at location addr. */
293 {
294 int copied;
295
296 ret = -EIO;
Mike Frysingerdabaad52008-10-09 15:17:36 +0800297 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)
Bryan Wu1394f032007-05-06 14:50:22 -0700300 break;
301 pr_debug("ptrace: user address is valid\n");
302
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800303 if (L1_CODE_LENGTH != 0 && addr >= L1_CODE_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800304 && addr + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) {
305 safe_dma_memcpy ((void *)(addr), &data, sizeof(data));
Bryan Wu1394f032007-05-06 14:50:22 -0700306 copied = sizeof(data);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800307
308 } else if (L1_DATA_A_LENGTH != 0 && addr >= L1_DATA_A_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800309 && addr + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) {
310 memcpy((void *)(addr), &data, sizeof(data));
Jie Zhang6546eae2008-07-15 16:15:40 +0800311 copied = sizeof(data);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800312
313 } else if (L1_DATA_B_LENGTH != 0 && addr >= L1_DATA_B_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800314 && addr + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) {
315 memcpy((void *)(addr), &data, sizeof(data));
Jie Zhang6546eae2008-07-15 16:15:40 +0800316 copied = sizeof(data);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800317
318 } else if (addr >= FIXED_CODE_START
Mike Frysingerdabaad52008-10-09 15:17:36 +0800319 && addr + sizeof(data) <= FIXED_CODE_END) {
320 memcpy((void *)(addr), &data, sizeof(data));
Jie Zhang26156392007-08-05 16:25:23 +0800321 copied = sizeof(data);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800322
Jie Zhang26156392007-08-05 16:25:23 +0800323 } else
Mike Frysingerdabaad52008-10-09 15:17:36 +0800324 copied = access_process_vm(child, addr, &data,
Jie Zhang26156392007-08-05 16:25:23 +0800325 sizeof(data), 1);
Mike Frysingerd207a8c2008-10-10 17:26:57 +0800326
Bryan Wu1394f032007-05-06 14:50:22 -0700327 pr_debug("ptrace: copied size %d\n", copied);
328 if (copied != sizeof(data))
329 break;
330 ret = 0;
331 break;
332 }
333
334 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
335 ret = -EIO;
336 if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) {
337 printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n");
338 break;
339 }
340
341 if (addr >= (sizeof(struct pt_regs))) {
342 ret = 0;
343 break;
344 }
345 if (addr == PT_SYSCFG) {
346 data &= SYSCFG_MASK;
347 data |= get_reg(child, PT_SYSCFG);
348 }
349 ret = put_reg(child, addr, data);
350 break;
351
352 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
Mike Frysingercb4c1732008-10-09 15:21:05 +0800353 case PTRACE_CONT: /* restart after signal. */
354 pr_debug("ptrace: syscall/cont\n");
Bryan Wu1394f032007-05-06 14:50:22 -0700355
Mike Frysingercb4c1732008-10-09 15:21:05 +0800356 ret = -EIO;
357 if (!valid_signal(data))
Bryan Wu1394f032007-05-06 14:50:22 -0700358 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800359 if (request == PTRACE_SYSCALL)
360 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
361 else
362 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
363 child->exit_code = data;
364 ptrace_disable(child);
365 pr_debug("ptrace: before wake_up_process\n");
366 wake_up_process(child);
367 ret = 0;
368 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700369
370 /*
371 * make the child exit. Best I can do is send it a sigkill.
372 * perhaps it should be put in the status that it wants to
373 * exit.
374 */
375 case PTRACE_KILL:
Mike Frysingercb4c1732008-10-09 15:21:05 +0800376 ret = 0;
377 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
Bryan Wu1394f032007-05-06 14:50:22 -0700378 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800379 child->exit_code = SIGKILL;
380 ptrace_disable(child);
381 wake_up_process(child);
382 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700383
Mike Frysingercb4c1732008-10-09 15:21:05 +0800384 case PTRACE_SINGLESTEP: /* set the trap flag. */
385 pr_debug("ptrace: single step\n");
386 ret = -EIO;
387 if (!valid_signal(data))
Bryan Wu1394f032007-05-06 14:50:22 -0700388 break;
Mike Frysingercb4c1732008-10-09 15:21:05 +0800389 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
390 ptrace_enable(child);
391 child->exit_code = data;
392 wake_up_process(child);
393 ret = 0;
394 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700395
Bryan Wu1394f032007-05-06 14:50:22 -0700396 case PTRACE_GETREGS:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800397 /* Get all gp regs from the child. */
398 ret = ptrace_getregs(child, datap);
399 break;
Bryan Wu1394f032007-05-06 14:50:22 -0700400
401 case PTRACE_SETREGS:
Mike Frysingerd3ab3a62008-10-09 15:19:50 +0800402 printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n");
403 /* Set all gp regs in the child. */
404 ret = 0;
405 break;
406
Bryan Wu1394f032007-05-06 14:50:22 -0700407 default:
408 ret = ptrace_request(child, request, addr, data);
409 break;
410 }
411
412 return ret;
413}
414
415asmlinkage void syscall_trace(void)
416{
Bryan Wu1394f032007-05-06 14:50:22 -0700417 if (!test_thread_flag(TIF_SYSCALL_TRACE))
418 return;
419
420 if (!(current->ptrace & PT_PTRACED))
421 return;
422
423 /* the 0x80 provides a way for the tracing parent to distinguish
424 * between a syscall stop and SIGTRAP delivery
425 */
426 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
427 ? 0x80 : 0));
428
429 /*
430 * this isn't the same as continuing with a signal, but it will do
431 * for normal use. strace only continues with a signal if the
432 * stopping signal is not SIGTRAP. -brl
433 */
434 if (current->exit_code) {
435 send_sig(current->exit_code, current, 1);
436 current->exit_code = 0;
437 }
438}