2 * linux/arch/h8300/kernel/ptrace.c
4 * Yoshinori Sato <qzb04471@nifty.ne.jp>
7 * linux/arch/m68k/kernel/ptrace.c
9 * Copyright (C) 1994 by Hamish Macdonald
10 * Taken from linux/kernel/ptrace.c and modified for M680x0.
11 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
13 * This file is subject to the terms and conditions of the GNU General
14 * Public License. See the file COPYING in the main directory of
15 * this archive for more details.
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
21 #include <linux/smp.h>
22 #include <linux/smp_lock.h>
23 #include <linux/errno.h>
24 #include <linux/ptrace.h>
25 #include <linux/user.h>
26 #include <linux/config.h>
28 #include <asm/uaccess.h>
30 #include <asm/pgtable.h>
31 #include <asm/system.h>
32 #include <asm/processor.h>
33 #include <asm/signal.h>
36 * does not yet catch signals sent when the child dies.
37 * in exit.c or in signal.c.
40 /* determines which bits in the SR the user has access to. */
41 /* 1 = access 0 = no access */
42 #define SR_MASK 0x001f
44 /* sets the trace bits. */
45 #define TRACE_BITS 0x8000
47 /* Find the stack offset for a register, relative to thread.esp0. */
48 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
49 /* Mapping from PT_xxx to the stack offset at which the register is
50 saved. Notice that usp has no stack-slot and needs to be treated
51 specially (see get_reg/put_reg below). */
52 static const int regoff[] = {
53 PT_REG(er1), PT_REG(er2), PT_REG(er3), PT_REG(er4),
54 PT_REG(er5), PT_REG(er6), PT_REG(er0), PT_REG(orig_er0),
55 PT_REG(ccr), PT_REG(pc)
59 * Get contents of register REGNO in task TASK.
61 static inline long get_reg(struct task_struct *task, int regno)
66 addr = &task->thread.usp;
67 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
68 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
75 * Write contents of register REGNO in task TASK.
77 static inline int put_reg(struct task_struct *task, int regno,
83 addr = &task->thread.usp;
84 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
85 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
93 * Called by kernel/ptrace.c when detaching..
95 * Make sure the single step bit is not set.
97 int ptrace_cancel_bpt(struct task_struct *child)
102 if (child->thread.debugreg[i]) {
103 if (child->thread.debugreg[i] != ~0)
104 put_user(child->thread.debugreg[i+4],
105 (unsigned short *)child->thread.debugreg[i]);
107 child->thread.debugreg[i] = 0;
113 const static unsigned char opcode0[]={
114 0x04,0x02,0x04,0x02,0x04,0x02,0x04,0x02, /* 0x58 */
115 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, /* 0x60 */
116 0x02,0x02,0x11,0x11,0x02,0x02,0x04,0x04, /* 0x68 */
117 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, /* 0x70 */
118 0x08,0x04,0x06,0x04,0x04,0x04,0x04,0x04}; /* 0x78 */
120 const static int table_parser01(unsigned char *pc);
121 const static int table_parser02(unsigned char *pc);
122 const static int table_parser100(unsigned char *pc);
123 const static int table_parser101(unsigned char *pc);
125 const static int (*parsers[])(unsigned char *pc)={table_parser01,table_parser02};
127 static int insn_length(unsigned char *pc)
130 return table_parser01(pc+1);
131 if (*pc < 0x58 || *pc>=0x80)
134 if (opcode0[*pc-0x58]<0x10)
135 return opcode0[*pc-0x58];
137 return (*parsers[opcode0[*pc-0x58]-0x10])(pc+1);
140 const static int table_parser01(unsigned char *pc)
142 const unsigned char codelen[]={0x10,0x00,0x00,0x00,0x11,0x00,0x00,0x00,
143 0x02,0x00,0x00,0x00,0x04,0x04,0x00,0x04};
144 const static int (*parsers[])(unsigned char *)={table_parser100,table_parser101};
145 unsigned char second_index;
146 second_index = (*pc) >> 4;
147 if (codelen[second_index]<0x10)
148 return codelen[second_index];
150 return parsers[codelen[second_index]-0x10](pc);
153 const static int table_parser02(unsigned char *pc)
155 return (*pc & 0x20)?0x06:0x04;
158 const static int table_parser100(unsigned char *pc)
160 return (*(pc+2) & 0x02)?0x08:0x06;
163 const static int table_parser101(unsigned char *pc)
165 return (*(pc+2) & 0x02)?0x08:0x06;
168 #define BREAK_INST 0x5730 /* TRAPA #3 */
170 int ptrace_set_bpt(struct task_struct *child)
172 unsigned long pc,next;
174 pc = get_reg(child,PT_PC);
175 next = insn_length((unsigned char *)pc) + pc;
176 get_user(insn,(unsigned short *)pc);
177 if (insn == 0x5470) {
180 sp = get_reg(child,PT_USP);
181 get_user(next,(unsigned long *)sp);
182 } else if ((insn & 0xfb00) != 0x5800) {
185 const short reg_tbl[]={PT_ER0,PT_ER1,PT_ER2,PT_ER3,
186 PT_ER4,PT_ER5,PT_ER6,PT_USP};
187 switch(insn & 0xfb00) {
189 regs = (insn & 0x0070) >> 8;
190 next = get_reg(child,reg_tbl[regs]);
193 get_user(next,(unsigned long *)(pc+2));
198 next = *(unsigned long *)(insn & 0xff);
201 } else if (((insn & 0xf000) == 0x4000) || ((insn &0xff00) == 0x5500)) {
204 dsp = (long)(insn && 0xff)+pc+2;
205 child->thread.debugreg[1] = dsp;
206 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
207 put_user(BREAK_INST,(unsigned short *)dsp);
208 } else if (((insn & 0xff00) == 0x5800) || ((insn &0xff00) == 0x5c00)) {
211 get_user(dsp,(unsigned short *)(pc+2));
212 dsp = (long)dsp+pc+4;
213 child->thread.debugreg[1] = dsp;
214 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
215 put_user(BREAK_INST,(unsigned short *)dsp);
217 child->thread.debugreg[0] = next;
218 get_user(child->thread.debugreg[4],(unsigned short *)next);
219 put_user(BREAK_INST,(unsigned short *)next);
224 static int read_long(struct task_struct * tsk, unsigned long addr,
225 unsigned long * result)
227 *result = *(unsigned long *)addr;
231 void ptrace_disable(struct task_struct *child)
233 ptrace_cancel_bpt(child);
236 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
238 struct task_struct *child;
243 if (request == PTRACE_TRACEME) {
244 /* are we already being traced? */
245 if (current->ptrace & PT_PTRACED)
247 /* set the ptrace bit in the process flags. */
248 current->ptrace |= PT_PTRACED;
253 read_lock(&tasklist_lock);
254 child = find_task_by_pid(pid);
256 get_task_struct(child);
257 read_unlock(&tasklist_lock);
262 if (pid == 1) /* you may not mess with init */
265 if (request == PTRACE_ATTACH) {
266 ret = ptrace_attach(child);
270 if (!(child->ptrace & PT_PTRACED))
272 if (child->state != TASK_STOPPED) {
273 if (request != PTRACE_KILL)
276 ret = ptrace_check_attach(child, request == PTRACE_KILL);
281 case PTRACE_PEEKTEXT: /* read word at location addr. */
282 case PTRACE_PEEKDATA: {
285 ret = read_long(child, addr, &tmp);
288 ret = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
290 put_user(tmp, (unsigned long *) data);
294 /* read the word at location addr in the USER area. */
295 case PTRACE_PEEKUSR: {
298 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
301 ret = verify_area(VERIFY_WRITE, (void *) data,
305 tmp = 0; /* Default return condition */
306 addr = addr >> 2; /* temporary hack. */
308 tmp = get_reg(child, addr);
313 put_user(tmp,(unsigned long *) data);
318 /* when I and D space are separate, this will have to be fixed. */
319 case PTRACE_POKETEXT: /* write the word at location addr. */
320 case PTRACE_POKEDATA:
322 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
327 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
328 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
332 addr = addr >> 2; /* temporary hack. */
334 if (addr == PT_ORIG_ER0) {
338 if (addr == PT_CCR) {
342 if (put_reg(child, addr, data))
350 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
351 case PTRACE_CONT: { /* restart after signal. */
353 if ((unsigned long) data >= _NSIG)
355 if (request == PTRACE_SYSCALL)
356 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
358 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
359 child->exit_code = data;
360 wake_up_process(child);
361 /* make sure the single step bit is not set. */
362 ptrace_cancel_bpt(child);
367 * make the child exit. Best I can do is send it a sigkill.
368 * perhaps it should be put in the status that it wants to
374 if (child->state == TASK_ZOMBIE) /* already dead */
376 child->exit_code = SIGKILL;
377 ptrace_cancel_bpt(child);
378 wake_up_process(child);
382 case PTRACE_SINGLESTEP: { /* set the trap flag. */
384 if ((unsigned long) data > _NSIG)
386 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
387 child->thread.debugreg[0]=-1;
388 child->exit_code = data;
389 wake_up_process(child);
394 case PTRACE_DETACH: /* detach a process that was attached. */
395 ret = ptrace_detach(child, data);
398 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
401 for (i = 0; i < 19; i++) {
402 tmp = get_reg(child, i);
403 if (put_user(tmp, (unsigned long *) data)) {
407 data += sizeof(long);
413 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
416 for (i = 0; i < 10; i++) {
417 if (get_user(tmp, (unsigned long *) data)) {
421 put_reg(child, i, tmp);
422 data += sizeof(long);
433 put_task_struct(child);
439 asmlinkage void syscall_trace(void)
441 if (!test_thread_flag(TIF_SYSCALL_TRACE))
443 if (!(current->ptrace & PT_PTRACED))
445 current->exit_code = SIGTRAP;
446 current->state = TASK_STOPPED;
447 notify_parent(current, SIGCHLD);
450 * this isn't the same as continuing with a signal, but it will do
451 * for normal use. strace only continues with a signal if the
452 * stopping signal is not SIGTRAP. -brl
454 if (current->exit_code) {
455 send_sig(current->exit_code, current, 1);
456 current->exit_code = 0;
460 asmlinkage void trace_trap(unsigned long bp)
462 if (current->thread.debugreg[0] == bp ||
463 current->thread.debugreg[1] == bp) {
464 ptrace_cancel_bpt(current);
465 force_sig(SIGTRAP,current);
467 force_sig(SIGILL,current);