aboutsummaryrefslogtreecommitdiffstats
path: root/arch/arm26/kernel/ptrace.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /arch/arm26/kernel/ptrace.c
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'arch/arm26/kernel/ptrace.c')
-rw-r--r--arch/arm26/kernel/ptrace.c744
1 files changed, 744 insertions, 0 deletions
diff --git a/arch/arm26/kernel/ptrace.c b/arch/arm26/kernel/ptrace.c
new file mode 100644
index 000000000000..2a137146a77c
--- /dev/null
+++ b/arch/arm26/kernel/ptrace.c
@@ -0,0 +1,744 @@
1/*
2 * linux/arch/arm26/kernel/ptrace.c
3 *
4 * By Ross Biro 1/23/92
5 * edited by Linus Torvalds
6 * ARM modifications Copyright (C) 2000 Russell King
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/smp.h>
17#include <linux/smp_lock.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
20#include <linux/security.h>
21
22#include <asm/uaccess.h>
23#include <asm/pgtable.h>
24#include <asm/system.h>
25//#include <asm/processor.h>
26
27#include "ptrace.h"
28
29#define REG_PC 15
30#define REG_PSR 15
31/*
32 * does not yet catch signals sent when the child dies.
33 * in exit.c or in signal.c.
34 */
35
36/*
37 * Breakpoint SWI instruction: SWI &9F0001
38 */
39#define BREAKINST_ARM 0xef9f0001
40
41/*
42 * Get the address of the live pt_regs for the specified task.
43 * These are saved onto the top kernel stack when the process
44 * is not running.
45 *
46 * Note: if a user thread is execve'd from kernel space, the
47 * kernel stack will not be empty on entry to the kernel, so
48 * ptracing these tasks will fail.
49 */
50static inline struct pt_regs *
51get_user_regs(struct task_struct *task)
52{
53 return __get_user_regs(task->thread_info);
54}
55
56/*
57 * this routine will get a word off of the processes privileged stack.
58 * the offset is how far from the base addr as stored in the THREAD.
59 * this routine assumes that all the privileged stacks are in our
60 * data space.
61 */
62static inline long get_user_reg(struct task_struct *task, int offset)
63{
64 return get_user_regs(task)->uregs[offset];
65}
66
67/*
68 * this routine will put a word on the processes privileged stack.
69 * the offset is how far from the base addr as stored in the THREAD.
70 * this routine assumes that all the privileged stacks are in our
71 * data space.
72 */
73static inline int
74put_user_reg(struct task_struct *task, int offset, long data)
75{
76 struct pt_regs newregs, *regs = get_user_regs(task);
77 int ret = -EINVAL;
78
79 newregs = *regs;
80 newregs.uregs[offset] = data;
81
82 if (valid_user_regs(&newregs)) {
83 regs->uregs[offset] = data;
84 ret = 0;
85 }
86
87 return ret;
88}
89
90static inline int
91read_u32(struct task_struct *task, unsigned long addr, u32 *res)
92{
93 int ret;
94
95 ret = access_process_vm(task, addr, res, sizeof(*res), 0);
96
97 return ret == sizeof(*res) ? 0 : -EIO;
98}
99
100static inline int
101read_instr(struct task_struct *task, unsigned long addr, u32 *res)
102{
103 int ret;
104 u32 val;
105 ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
106 ret = ret == sizeof(val) ? 0 : -EIO;
107 *res = val;
108 return ret;
109}
110
111/*
112 * Get value of register `rn' (in the instruction)
113 */
114static unsigned long
115ptrace_getrn(struct task_struct *child, unsigned long insn)
116{
117 unsigned int reg = (insn >> 16) & 15;
118 unsigned long val;
119
120 val = get_user_reg(child, reg);
121 if (reg == 15)
122 val = pc_pointer(val + 8); //FIXME - correct for arm26?
123
124 return val;
125}
126
127/*
128 * Get value of operand 2 (in an ALU instruction)
129 */
130static unsigned long
131ptrace_getaluop2(struct task_struct *child, unsigned long insn)
132{
133 unsigned long val;
134 int shift;
135 int type;
136
137 if (insn & 1 << 25) {
138 val = insn & 255;
139 shift = (insn >> 8) & 15;
140 type = 3;
141 } else {
142 val = get_user_reg (child, insn & 15);
143
144 if (insn & (1 << 4))
145 shift = (int)get_user_reg (child, (insn >> 8) & 15);
146 else
147 shift = (insn >> 7) & 31;
148
149 type = (insn >> 5) & 3;
150 }
151
152 switch (type) {
153 case 0: val <<= shift; break;
154 case 1: val >>= shift; break;
155 case 2:
156 val = (((signed long)val) >> shift);
157 break;
158 case 3:
159 val = (val >> shift) | (val << (32 - shift));
160 break;
161 }
162 return val;
163}
164
165/*
166 * Get value of operand 2 (in a LDR instruction)
167 */
168static unsigned long
169ptrace_getldrop2(struct task_struct *child, unsigned long insn)
170{
171 unsigned long val;
172 int shift;
173 int type;
174
175 val = get_user_reg(child, insn & 15);
176 shift = (insn >> 7) & 31;
177 type = (insn >> 5) & 3;
178
179 switch (type) {
180 case 0: val <<= shift; break;
181 case 1: val >>= shift; break;
182 case 2:
183 val = (((signed long)val) >> shift);
184 break;
185 case 3:
186 val = (val >> shift) | (val << (32 - shift));
187 break;
188 }
189 return val;
190}
191
192#define OP_MASK 0x01e00000
193#define OP_AND 0x00000000
194#define OP_EOR 0x00200000
195#define OP_SUB 0x00400000
196#define OP_RSB 0x00600000
197#define OP_ADD 0x00800000
198#define OP_ADC 0x00a00000
199#define OP_SBC 0x00c00000
200#define OP_RSC 0x00e00000
201#define OP_ORR 0x01800000
202#define OP_MOV 0x01a00000
203#define OP_BIC 0x01c00000
204#define OP_MVN 0x01e00000
205
206static unsigned long
207get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
208{
209 u32 alt = 0;
210
211 switch (insn & 0x0e000000) {
212 case 0x00000000:
213 case 0x02000000: {
214 /*
215 * data processing
216 */
217 long aluop1, aluop2, ccbit;
218
219 if ((insn & 0xf000) != 0xf000)
220 break;
221
222 aluop1 = ptrace_getrn(child, insn);
223 aluop2 = ptrace_getaluop2(child, insn);
224 ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
225
226 switch (insn & OP_MASK) {
227 case OP_AND: alt = aluop1 & aluop2; break;
228 case OP_EOR: alt = aluop1 ^ aluop2; break;
229 case OP_SUB: alt = aluop1 - aluop2; break;
230 case OP_RSB: alt = aluop2 - aluop1; break;
231 case OP_ADD: alt = aluop1 + aluop2; break;
232 case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
233 case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
234 case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
235 case OP_ORR: alt = aluop1 | aluop2; break;
236 case OP_MOV: alt = aluop2; break;
237 case OP_BIC: alt = aluop1 & ~aluop2; break;
238 case OP_MVN: alt = ~aluop2; break;
239 }
240 break;
241 }
242
243 case 0x04000000:
244 case 0x06000000:
245 /*
246 * ldr
247 */
248 if ((insn & 0x0010f000) == 0x0010f000) {
249 unsigned long base;
250
251 base = ptrace_getrn(child, insn);
252 if (insn & 1 << 24) {
253 long aluop2;
254
255 if (insn & 0x02000000)
256 aluop2 = ptrace_getldrop2(child, insn);
257 else
258 aluop2 = insn & 0xfff;
259
260 if (insn & 1 << 23)
261 base += aluop2;
262 else
263 base -= aluop2;
264 }
265 if (read_u32(child, base, &alt) == 0)
266 alt = pc_pointer(alt);
267 }
268 break;
269
270 case 0x08000000:
271 /*
272 * ldm
273 */
274 if ((insn & 0x00108000) == 0x00108000) {
275 unsigned long base;
276 unsigned int nr_regs;
277
278 if (insn & (1 << 23)) {
279 nr_regs = hweight16(insn & 65535) << 2;
280
281 if (!(insn & (1 << 24)))
282 nr_regs -= 4;
283 } else {
284 if (insn & (1 << 24))
285 nr_regs = -4;
286 else
287 nr_regs = 0;
288 }
289
290 base = ptrace_getrn(child, insn);
291
292 if (read_u32(child, base + nr_regs, &alt) == 0)
293 alt = pc_pointer(alt);
294 break;
295 }
296 break;
297
298 case 0x0a000000: {
299 /*
300 * bl or b
301 */
302 signed long displ;
303 /* It's a branch/branch link: instead of trying to
304 * figure out whether the branch will be taken or not,
305 * we'll put a breakpoint at both locations. This is
306 * simpler, more reliable, and probably not a whole lot
307 * slower than the alternative approach of emulating the
308 * branch.
309 */
310 displ = (insn & 0x00ffffff) << 8;
311 displ = (displ >> 6) + 8;
312 if (displ != 0 && displ != 4)
313 alt = pc + displ;
314 }
315 break;
316 }
317
318 return alt;
319}
320
321static int
322swap_insn(struct task_struct *task, unsigned long addr,
323 void *old_insn, void *new_insn, int size)
324{
325 int ret;
326
327 ret = access_process_vm(task, addr, old_insn, size, 0);
328 if (ret == size)
329 ret = access_process_vm(task, addr, new_insn, size, 1);
330 return ret;
331}
332
333static void
334add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
335{
336 int nr = dbg->nsaved;
337
338 if (nr < 2) {
339 u32 new_insn = BREAKINST_ARM;
340 int res;
341
342 res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
343
344 if (res == 4) {
345 dbg->bp[nr].address = addr;
346 dbg->nsaved += 1;
347 }
348 } else
349 printk(KERN_ERR "ptrace: too many breakpoints\n");
350}
351
352/*
353 * Clear one breakpoint in the user program. We copy what the hardware
354 * does and use bit 0 of the address to indicate whether this is a Thumb
355 * breakpoint or an ARM breakpoint.
356 */
357static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
358{
359 unsigned long addr = bp->address;
360 u32 old_insn;
361 int ret;
362
363 ret = swap_insn(task, addr & ~3, &old_insn,
364 &bp->insn, 4);
365
366 if (ret != 4 || old_insn != BREAKINST_ARM)
367 printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
368 "0x%08lx (0x%08x)\n", task->comm, task->pid,
369 addr, old_insn);
370}
371
372void ptrace_set_bpt(struct task_struct *child)
373{
374 struct pt_regs *regs;
375 unsigned long pc;
376 u32 insn;
377 int res;
378
379 regs = get_user_regs(child);
380 pc = instruction_pointer(regs);
381
382 res = read_instr(child, pc, &insn);
383 if (!res) {
384 struct debug_info *dbg = &child->thread.debug;
385 unsigned long alt;
386
387 dbg->nsaved = 0;
388
389 alt = get_branch_address(child, pc, insn);
390 if (alt)
391 add_breakpoint(child, dbg, alt);
392
393 /*
394 * Note that we ignore the result of setting the above
395 * breakpoint since it may fail. When it does, this is
396 * not so much an error, but a forewarning that we may
397 * be receiving a prefetch abort shortly.
398 *
399 * If we don't set this breakpoint here, then we can
400 * lose control of the thread during single stepping.
401 */
402 if (!alt || predicate(insn) != PREDICATE_ALWAYS)
403 add_breakpoint(child, dbg, pc + 4);
404 }
405}
406
407/*
408 * Ensure no single-step breakpoint is pending. Returns non-zero
409 * value if child was being single-stepped.
410 */
411void ptrace_cancel_bpt(struct task_struct *child)
412{
413 int i, nsaved = child->thread.debug.nsaved;
414
415 child->thread.debug.nsaved = 0;
416
417 if (nsaved > 2) {
418 printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
419 nsaved = 2;
420 }
421
422 for (i = 0; i < nsaved; i++)
423 clear_breakpoint(child, &child->thread.debug.bp[i]);
424}
425
426/*
427 * Called by kernel/ptrace.c when detaching..
428 *
429 * Make sure the single step bit is not set.
430 */
431void ptrace_disable(struct task_struct *child)
432{
433 child->ptrace &= ~PT_SINGLESTEP;
434 ptrace_cancel_bpt(child);
435}
436
437/*
438 * Handle hitting a breakpoint.
439 */
440void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
441{
442 siginfo_t info;
443
444 /*
445 * The PC is always left pointing at the next instruction. Fix this.
446 */
447 regs->ARM_pc -= 4;
448
449 if (tsk->thread.debug.nsaved == 0)
450 printk(KERN_ERR "ptrace: bogus breakpoint trap\n");
451
452 ptrace_cancel_bpt(tsk);
453
454 info.si_signo = SIGTRAP;
455 info.si_errno = 0;
456 info.si_code = TRAP_BRKPT;
457 info.si_addr = (void *)instruction_pointer(regs) - 4;
458
459 force_sig_info(SIGTRAP, &info, tsk);
460}
461
462/*
463 * Read the word at offset "off" into the "struct user". We
464 * actually access the pt_regs stored on the kernel stack.
465 */
466static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
467 unsigned long *ret)
468{
469 unsigned long tmp;
470
471 if (off & 3 || off >= sizeof(struct user))
472 return -EIO;
473
474 tmp = 0;
475 if (off < sizeof(struct pt_regs))
476 tmp = get_user_reg(tsk, off >> 2);
477
478 return put_user(tmp, ret);
479}
480
481/*
482 * Write the word at offset "off" into "struct user". We
483 * actually access the pt_regs stored on the kernel stack.
484 */
485static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
486 unsigned long val)
487{
488 if (off & 3 || off >= sizeof(struct user))
489 return -EIO;
490
491 if (off >= sizeof(struct pt_regs))
492 return 0;
493
494 return put_user_reg(tsk, off >> 2, val);
495}
496
497/*
498 * Get all user integer registers.
499 */
500static int ptrace_getregs(struct task_struct *tsk, void *uregs)
501{
502 struct pt_regs *regs = get_user_regs(tsk);
503
504 return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
505}
506
507/*
508 * Set all user integer registers.
509 */
510static int ptrace_setregs(struct task_struct *tsk, void *uregs)
511{
512 struct pt_regs newregs;
513 int ret;
514
515 ret = -EFAULT;
516 if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
517 struct pt_regs *regs = get_user_regs(tsk);
518
519 ret = -EINVAL;
520 if (valid_user_regs(&newregs)) {
521 *regs = newregs;
522 ret = 0;
523 }
524 }
525
526 return ret;
527}
528
529/*
530 * Get the child FPU state.
531 */
532static int ptrace_getfpregs(struct task_struct *tsk, void *ufp)
533{
534 return copy_to_user(ufp, &tsk->thread_info->fpstate,
535 sizeof(struct user_fp)) ? -EFAULT : 0;
536}
537
538/*
539 * Set the child FPU state.
540 */
541static int ptrace_setfpregs(struct task_struct *tsk, void *ufp)
542{
543 set_stopped_child_used_math(tsk);
544 return copy_from_user(&tsk->thread_info->fpstate, ufp,
545 sizeof(struct user_fp)) ? -EFAULT : 0;
546}
547
548static int do_ptrace(int request, struct task_struct *child, long addr, long data)
549{
550 unsigned long tmp;
551 int ret;
552
553 switch (request) {
554 /*
555 * read word at location "addr" in the child process.
556 */
557 case PTRACE_PEEKTEXT:
558 case PTRACE_PEEKDATA:
559 ret = access_process_vm(child, addr, &tmp,
560 sizeof(unsigned long), 0);
561 if (ret == sizeof(unsigned long))
562 ret = put_user(tmp, (unsigned long *) data);
563 else
564 ret = -EIO;
565 break;
566
567 case PTRACE_PEEKUSR:
568 ret = ptrace_read_user(child, addr, (unsigned long *)data);
569 break;
570
571 /*
572 * write the word at location addr.
573 */
574 case PTRACE_POKETEXT:
575 case PTRACE_POKEDATA:
576 ret = access_process_vm(child, addr, &data,
577 sizeof(unsigned long), 1);
578 if (ret == sizeof(unsigned long))
579 ret = 0;
580 else
581 ret = -EIO;
582 break;
583
584 case PTRACE_POKEUSR:
585 ret = ptrace_write_user(child, addr, data);
586 break;
587
588 /*
589 * continue/restart and stop at next (return from) syscall
590 */
591 case PTRACE_SYSCALL:
592 case PTRACE_CONT:
593 ret = -EIO;
594 if ((unsigned long) data > _NSIG)
595 break;
596 if (request == PTRACE_SYSCALL)
597 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
598 else
599 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
600 child->exit_code = data;
601 /* make sure single-step breakpoint is gone. */
602 child->ptrace &= ~PT_SINGLESTEP;
603 ptrace_cancel_bpt(child);
604 wake_up_process(child);
605 ret = 0;
606 break;
607
608 /*
609 * make the child exit. Best I can do is send it a sigkill.
610 * perhaps it should be put in the status that it wants to
611 * exit.
612 */
613 case PTRACE_KILL:
614 /* make sure single-step breakpoint is gone. */
615 child->ptrace &= ~PT_SINGLESTEP;
616 ptrace_cancel_bpt(child);
617 if (child->exit_state != EXIT_ZOMBIE) {
618 child->exit_code = SIGKILL;
619 wake_up_process(child);
620 }
621 ret = 0;
622 break;
623
624 /*
625 * execute single instruction.
626 */
627 case PTRACE_SINGLESTEP:
628 ret = -EIO;
629 if ((unsigned long) data > _NSIG)
630 break;
631 child->ptrace |= PT_SINGLESTEP;
632 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
633 child->exit_code = data;
634 /* give it a chance to run. */
635 wake_up_process(child);
636 ret = 0;
637 break;
638
639 case PTRACE_DETACH:
640 ret = ptrace_detach(child, data);
641 break;
642
643 case PTRACE_GETREGS:
644 ret = ptrace_getregs(child, (void *)data);
645 break;
646
647 case PTRACE_SETREGS:
648 ret = ptrace_setregs(child, (void *)data);
649 break;
650
651 case PTRACE_GETFPREGS:
652 ret = ptrace_getfpregs(child, (void *)data);
653 break;
654
655 case PTRACE_SETFPREGS:
656 ret = ptrace_setfpregs(child, (void *)data);
657 break;
658
659 default:
660 ret = ptrace_request(child, request, addr, data);
661 break;
662 }
663
664 return ret;
665}
666
667asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
668{
669 struct task_struct *child;
670 int ret;
671
672 lock_kernel();
673 ret = -EPERM;
674 if (request == PTRACE_TRACEME) {
675 /* are we already being traced? */
676 if (current->ptrace & PT_PTRACED)
677 goto out;
678 ret = security_ptrace(current->parent, current);
679 if (ret)
680 goto out;
681 /* set the ptrace bit in the process flags. */
682 current->ptrace |= PT_PTRACED;
683 ret = 0;
684 goto out;
685 }
686 ret = -ESRCH;
687 read_lock(&tasklist_lock);
688 child = find_task_by_pid(pid);
689 if (child)
690 get_task_struct(child);
691 read_unlock(&tasklist_lock);
692 if (!child)
693 goto out;
694
695 ret = -EPERM;
696 if (pid == 1) /* you may not mess with init */
697 goto out_tsk;
698
699 if (request == PTRACE_ATTACH) {
700 ret = ptrace_attach(child);
701 goto out_tsk;
702 }
703 ret = ptrace_check_attach(child, request == PTRACE_KILL);
704 if (ret == 0)
705 ret = do_ptrace(request, child, addr, data);
706
707out_tsk:
708 put_task_struct(child);
709out:
710 unlock_kernel();
711 return ret;
712}
713
714asmlinkage void syscall_trace(int why, struct pt_regs *regs)
715{
716 unsigned long ip;
717
718 if (!test_thread_flag(TIF_SYSCALL_TRACE))
719 return;
720 if (!(current->ptrace & PT_PTRACED))
721 return;
722
723 /*
724 * Save IP. IP is used to denote syscall entry/exit:
725 * IP = 0 -> entry, = 1 -> exit
726 */
727 ip = regs->ARM_ip;
728 regs->ARM_ip = why;
729
730 /* the 0x80 provides a way for the tracing parent to distinguish
731 between a syscall stop and SIGTRAP delivery */
732 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
733 ? 0x80 : 0));
734 /*
735 * this isn't the same as continuing with a signal, but it will do
736 * for normal use. strace only continues with a signal if the
737 * stopping signal is not SIGTRAP. -brl
738 */
739 if (current->exit_code) {
740 send_sig(current->exit_code, current, 1);
741 current->exit_code = 0;
742 }
743 regs->ARM_ip = ip;
744}