Commit | Line | Data |
---|---|---|
1394f032 BW |
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 | |
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> | |
1f83b8f1 | 39 | #include <linux/uaccess.h> |
1394f032 | 40 | |
1394f032 BW |
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> | |
26156397 | 47 | #include <asm/fixed_code.h> |
1394f032 | 48 | |
1394f032 BW |
49 | #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 | */ | |
73 | static inline struct pt_regs *get_user_regs(struct task_struct *task) | |
74 | { | |
75 | return (struct pt_regs *) | |
f7e4217b | 76 | ((unsigned long)task_stack_page(task) + |
1394f032 BW |
77 | (THREAD_SIZE - sizeof(struct pt_regs))); |
78 | } | |
79 | ||
80 | /* | |
81 | * Get all user integer registers. | |
82 | */ | |
83 | static 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 | */ | |
97 | static inline long get_reg(struct task_struct *task, int regno) | |
98 | { | |
99 | unsigned char *reg_ptr; | |
100 | ||
101 | struct pt_regs *regs = | |
f7e4217b | 102 | (struct pt_regs *)((unsigned long)task_stack_page(task) + |
1394f032 BW |
103 | (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 | */ | |
122 | static inline int | |
123 | put_reg(struct task_struct *task, int regno, unsigned long data) | |
124 | { | |
1f83b8f1 | 125 | char *reg_ptr; |
1394f032 BW |
126 | |
127 | struct pt_regs *regs = | |
f7e4217b | 128 | (struct pt_regs *)((unsigned long)task_stack_page(task) + |
1394f032 BW |
129 | (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) | |
1f83b8f1 | 149 | *(long *)(reg_ptr + regno) = data; |
1394f032 BW |
150 | } |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* | |
155 | * check that an address falls within the bounds of the target process's memory mappings | |
156 | */ | |
157 | static 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 | ||
163 | for (vml = child->mm->context.vmlist; vml; vml = vml->next) | |
164 | if (start >= vml->vma->vm_start && start + len <= vml->vma->vm_end) | |
165 | return 0; | |
166 | ||
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) | |
170 | return 0; | |
171 | ||
26156397 JZ |
172 | if (start >= FIXED_CODE_START && start + len <= FIXED_CODE_END) |
173 | return 0; | |
174 | ||
1394f032 BW |
175 | return -EIO; |
176 | } | |
177 | ||
178 | /* | |
179 | * Called by kernel/ptrace.c when detaching.. | |
180 | * | |
181 | * Make sure the single step bit is not set. | |
182 | */ | |
183 | void ptrace_disable(struct task_struct *child) | |
184 | { | |
185 | unsigned long tmp; | |
186 | /* make sure the single step bit is not set. */ | |
7d39270d BS |
187 | tmp = get_reg(child, PT_SYSCFG) & ~TRACE_BITS; |
188 | put_reg(child, PT_SYSCFG, tmp); | |
1394f032 BW |
189 | } |
190 | ||
191 | long arch_ptrace(struct task_struct *child, long request, long addr, long data) | |
192 | { | |
193 | int ret; | |
0ddeeca2 | 194 | unsigned long __user *datap = (unsigned long __user *)data; |
1394f032 BW |
195 | |
196 | switch (request) { | |
197 | /* when I and D space are separate, these will need to be fixed. */ | |
198 | case PTRACE_PEEKDATA: | |
199 | pr_debug("ptrace: PEEKDATA\n"); | |
1394f032 BW |
200 | /* fall through */ |
201 | case PTRACE_PEEKTEXT: /* read word at location addr. */ | |
202 | { | |
203 | unsigned long tmp = 0; | |
204 | int copied; | |
205 | ||
206 | ret = -EIO; | |
dabaad5b MF |
207 | pr_debug("ptrace: PEEKTEXT at addr 0x%08lx + %ld\n", addr, sizeof(data)); |
208 | if (is_user_addr_valid(child, addr, sizeof(tmp)) < 0) | |
1394f032 BW |
209 | break; |
210 | pr_debug("ptrace: user address is valid\n"); | |
211 | ||
212 | #if L1_CODE_LENGTH != 0 | |
dabaad5b MF |
213 | if (addr >= L1_CODE_START |
214 | && addr + sizeof(tmp) <= L1_CODE_START + L1_CODE_LENGTH) { | |
215 | safe_dma_memcpy (&tmp, (const void *)(addr), sizeof(tmp)); | |
1394f032 BW |
216 | copied = sizeof(tmp); |
217 | } else | |
6546eae4 JZ |
218 | #endif |
219 | #if L1_DATA_A_LENGTH != 0 | |
dabaad5b MF |
220 | if (addr >= L1_DATA_A_START |
221 | && addr + sizeof(tmp) <= L1_DATA_A_START + L1_DATA_A_LENGTH) { | |
222 | memcpy(&tmp, (const void *)(addr), sizeof(tmp)); | |
6546eae4 JZ |
223 | copied = sizeof(tmp); |
224 | } else | |
225 | #endif | |
226 | #if L1_DATA_B_LENGTH != 0 | |
dabaad5b MF |
227 | if (addr >= L1_DATA_B_START |
228 | && addr + sizeof(tmp) <= L1_DATA_B_START + L1_DATA_B_LENGTH) { | |
229 | memcpy(&tmp, (const void *)(addr), sizeof(tmp)); | |
6546eae4 JZ |
230 | copied = sizeof(tmp); |
231 | } else | |
1394f032 | 232 | #endif |
dabaad5b MF |
233 | if (addr >= FIXED_CODE_START |
234 | && addr + sizeof(tmp) <= FIXED_CODE_END) { | |
235 | memcpy(&tmp, (const void *)(addr), sizeof(tmp)); | |
26156397 JZ |
236 | copied = sizeof(tmp); |
237 | } else | |
dabaad5b | 238 | copied = access_process_vm(child, addr, &tmp, |
26156397 | 239 | sizeof(tmp), 0); |
1394f032 BW |
240 | pr_debug("ptrace: copied size %d [0x%08lx]\n", copied, tmp); |
241 | if (copied != sizeof(tmp)) | |
242 | break; | |
0ddeeca2 | 243 | ret = put_user(tmp, datap); |
1394f032 BW |
244 | break; |
245 | } | |
246 | ||
247 | /* read the word at location addr in the USER area. */ | |
248 | case PTRACE_PEEKUSR: | |
249 | { | |
250 | unsigned long tmp; | |
251 | ret = -EIO; | |
252 | tmp = 0; | |
253 | if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) { | |
254 | printk(KERN_WARNING "ptrace error : PEEKUSR : temporarily returning " | |
255 | "0 - %x sizeof(pt_regs) is %lx\n", | |
256 | (int)addr, sizeof(struct pt_regs)); | |
257 | break; | |
258 | } | |
259 | if (addr == sizeof(struct pt_regs)) { | |
260 | /* PT_TEXT_ADDR */ | |
261 | tmp = child->mm->start_code + TEXT_OFFSET; | |
262 | } else if (addr == (sizeof(struct pt_regs) + 4)) { | |
263 | /* PT_TEXT_END_ADDR */ | |
264 | tmp = child->mm->end_code; | |
265 | } else if (addr == (sizeof(struct pt_regs) + 8)) { | |
266 | /* PT_DATA_ADDR */ | |
267 | tmp = child->mm->start_data; | |
268 | #ifdef CONFIG_BINFMT_ELF_FDPIC | |
269 | } else if (addr == (sizeof(struct pt_regs) + 12)) { | |
270 | tmp = child->mm->context.exec_fdpic_loadmap; | |
271 | } else if (addr == (sizeof(struct pt_regs) + 16)) { | |
272 | tmp = child->mm->context.interp_fdpic_loadmap; | |
273 | #endif | |
274 | } else { | |
275 | tmp = get_reg(child, addr); | |
276 | } | |
0ddeeca2 | 277 | ret = put_user(tmp, datap); |
1394f032 BW |
278 | break; |
279 | } | |
280 | ||
281 | /* when I and D space are separate, this will have to be fixed. */ | |
282 | case PTRACE_POKEDATA: | |
d3ab3a62 | 283 | pr_debug("ptrace: PTRACE_PEEKDATA\n"); |
1394f032 BW |
284 | /* fall through */ |
285 | case PTRACE_POKETEXT: /* write the word at location addr. */ | |
286 | { | |
287 | int copied; | |
288 | ||
289 | ret = -EIO; | |
dabaad5b MF |
290 | pr_debug("ptrace: POKETEXT at addr 0x%08lx + %ld bytes %lx\n", |
291 | addr, sizeof(data), data); | |
292 | if (is_user_addr_valid(child, addr, sizeof(data)) < 0) | |
1394f032 BW |
293 | break; |
294 | pr_debug("ptrace: user address is valid\n"); | |
295 | ||
296 | #if L1_CODE_LENGTH != 0 | |
dabaad5b MF |
297 | if (addr >= L1_CODE_START |
298 | && addr + sizeof(data) <= L1_CODE_START + L1_CODE_LENGTH) { | |
299 | safe_dma_memcpy ((void *)(addr), &data, sizeof(data)); | |
1394f032 BW |
300 | copied = sizeof(data); |
301 | } else | |
6546eae4 JZ |
302 | #endif |
303 | #if L1_DATA_A_LENGTH != 0 | |
dabaad5b MF |
304 | if (addr >= L1_DATA_A_START |
305 | && addr + sizeof(data) <= L1_DATA_A_START + L1_DATA_A_LENGTH) { | |
306 | memcpy((void *)(addr), &data, sizeof(data)); | |
6546eae4 JZ |
307 | copied = sizeof(data); |
308 | } else | |
309 | #endif | |
310 | #if L1_DATA_B_LENGTH != 0 | |
dabaad5b MF |
311 | if (addr >= L1_DATA_B_START |
312 | && addr + sizeof(data) <= L1_DATA_B_START + L1_DATA_B_LENGTH) { | |
313 | memcpy((void *)(addr), &data, sizeof(data)); | |
6546eae4 JZ |
314 | copied = sizeof(data); |
315 | } else | |
1394f032 | 316 | #endif |
dabaad5b MF |
317 | if (addr >= FIXED_CODE_START |
318 | && addr + sizeof(data) <= FIXED_CODE_END) { | |
319 | memcpy((void *)(addr), &data, sizeof(data)); | |
26156397 JZ |
320 | copied = sizeof(data); |
321 | } else | |
dabaad5b | 322 | copied = access_process_vm(child, addr, &data, |
26156397 | 323 | sizeof(data), 1); |
1394f032 BW |
324 | pr_debug("ptrace: copied size %d\n", copied); |
325 | if (copied != sizeof(data)) | |
326 | break; | |
327 | ret = 0; | |
328 | break; | |
329 | } | |
330 | ||
331 | case PTRACE_POKEUSR: /* write the word at location addr in the USER area */ | |
332 | ret = -EIO; | |
333 | if ((addr & 3) || (addr > (sizeof(struct pt_regs) + 16))) { | |
334 | printk(KERN_WARNING "ptrace error : POKEUSR: temporarily returning 0\n"); | |
335 | break; | |
336 | } | |
337 | ||
338 | if (addr >= (sizeof(struct pt_regs))) { | |
339 | ret = 0; | |
340 | break; | |
341 | } | |
342 | if (addr == PT_SYSCFG) { | |
343 | data &= SYSCFG_MASK; | |
344 | data |= get_reg(child, PT_SYSCFG); | |
345 | } | |
346 | ret = put_reg(child, addr, data); | |
347 | break; | |
348 | ||
349 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ | |
350 | case PTRACE_CONT: | |
351 | { /* restart after signal. */ | |
352 | long tmp; | |
353 | ||
d3ab3a62 | 354 | pr_debug("ptrace: syscall/cont\n"); |
1394f032 BW |
355 | |
356 | ret = -EIO; | |
357 | if (!valid_signal(data)) | |
358 | break; | |
359 | 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 | ||
364 | child->exit_code = data; | |
365 | /* make sure the single step bit is not set. */ | |
366 | tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS); | |
367 | put_reg(child, PT_SYSCFG, tmp); | |
d3ab3a62 | 368 | pr_debug("ptrace: before wake_up_process\n"); |
1394f032 BW |
369 | wake_up_process(child); |
370 | ret = 0; | |
371 | break; | |
372 | } | |
373 | ||
374 | /* | |
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 | |
377 | * exit. | |
378 | */ | |
379 | case PTRACE_KILL: | |
380 | { | |
381 | long tmp; | |
382 | ret = 0; | |
383 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ | |
384 | break; | |
385 | child->exit_code = SIGKILL; | |
386 | /* make sure the single step bit is not set. */ | |
387 | tmp = get_reg(child, PT_SYSCFG) & ~(TRACE_BITS); | |
388 | put_reg(child, PT_SYSCFG, tmp); | |
389 | wake_up_process(child); | |
390 | break; | |
391 | } | |
392 | ||
393 | case PTRACE_SINGLESTEP: | |
394 | { /* set the trap flag. */ | |
395 | long tmp; | |
396 | ||
d3ab3a62 | 397 | pr_debug("ptrace: single step\n"); |
1394f032 BW |
398 | ret = -EIO; |
399 | if (!valid_signal(data)) | |
400 | break; | |
401 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); | |
402 | ||
403 | tmp = get_reg(child, PT_SYSCFG) | (TRACE_BITS); | |
404 | put_reg(child, PT_SYSCFG, tmp); | |
405 | ||
406 | child->exit_code = data; | |
407 | /* give it a chance to run. */ | |
408 | wake_up_process(child); | |
409 | ret = 0; | |
410 | break; | |
411 | } | |
412 | ||
1394f032 | 413 | case PTRACE_GETREGS: |
d3ab3a62 MF |
414 | /* Get all gp regs from the child. */ |
415 | ret = ptrace_getregs(child, datap); | |
416 | break; | |
1394f032 BW |
417 | |
418 | case PTRACE_SETREGS: | |
d3ab3a62 MF |
419 | printk(KERN_WARNING "ptrace: SETREGS: **** NOT IMPLEMENTED ***\n"); |
420 | /* Set all gp regs in the child. */ | |
421 | ret = 0; | |
422 | break; | |
423 | ||
1394f032 BW |
424 | default: |
425 | ret = ptrace_request(child, request, addr, data); | |
426 | break; | |
427 | } | |
428 | ||
429 | return ret; | |
430 | } | |
431 | ||
432 | asmlinkage void syscall_trace(void) | |
433 | { | |
1394f032 BW |
434 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
435 | return; | |
436 | ||
437 | if (!(current->ptrace & PT_PTRACED)) | |
438 | return; | |
439 | ||
440 | /* the 0x80 provides a way for the tracing parent to distinguish | |
441 | * between a syscall stop and SIGTRAP delivery | |
442 | */ | |
443 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) | |
444 | ? 0x80 : 0)); | |
445 | ||
446 | /* | |
447 | * this isn't the same as continuing with a signal, but it will do | |
448 | * for normal use. strace only continues with a signal if the | |
449 | * stopping signal is not SIGTRAP. -brl | |
450 | */ | |
451 | if (current->exit_code) { | |
452 | send_sig(current->exit_code, current, 1); | |
453 | current->exit_code = 0; | |
454 | } | |
455 | } |