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