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 #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
50 - sizeof(struct switch_stack))
51 /* Mapping from PT_xxx to the stack offset at which the register is
52 saved. Notice that usp has no stack-slot and needs to be treated
53 specially (see get_reg/put_reg below). */
54 static const int regoff[] = {
55 PT_REG(er1), PT_REG(er2), PT_REG(er3), SW_REG(er4),
56 SW_REG(er5), SW_REG(er6), PT_REG(er0), PT_REG(orig_er0),
57 PT_REG(ccr), PT_REG(pc)
61 * Get contents of register REGNO in task TASK.
63 static inline long get_reg(struct task_struct *task, int regno)
68 addr = &task->thread.usp;
69 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
70 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
77 * Write contents of register REGNO in task TASK.
79 static inline int put_reg(struct task_struct *task, int regno,
85 addr = &task->thread.usp;
86 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
87 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
95 * Called by kernel/ptrace.c when detaching..
97 * Make sure the single step bit is not set.
99 int ptrace_cancel_bpt(struct task_struct *child)
104 if (child->thread.debugreg[i]) {
105 if (child->thread.debugreg[i] != ~0)
106 put_user(child->thread.debugreg[i+4],
107 (unsigned short *)child->thread.debugreg[i]);
109 child->thread.debugreg[i] = 0;
115 const static unsigned char opcode0[]={
116 0x04,0x02,0x04,0x02,0x04,0x02,0x04,0x02, /* 0x58 */
117 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, /* 0x60 */
118 0x02,0x02,0x11,0x11,0x02,0x02,0x04,0x04, /* 0x68 */
119 0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02, /* 0x70 */
120 0x08,0x04,0x06,0x04,0x04,0x04,0x04,0x04}; /* 0x78 */
122 const static int table_parser01(unsigned char *pc);
123 const static int table_parser02(unsigned char *pc);
124 const static int table_parser100(unsigned char *pc);
125 const static int table_parser101(unsigned char *pc);
127 const static int (*parsers[])(unsigned char *pc)={table_parser01,table_parser02};
129 static int insn_length(unsigned char *pc)
132 return table_parser01(pc+1);
133 if (*pc < 0x58 || *pc>=0x80)
136 if (opcode0[*pc-0x58]<0x10)
137 return opcode0[*pc-0x58];
139 return (*parsers[opcode0[*pc-0x58]-0x10])(pc+1);
142 const static int table_parser01(unsigned char *pc)
144 const unsigned char codelen[]={0x10,0x00,0x00,0x00,0x11,0x00,0x00,0x00,
145 0x02,0x00,0x00,0x00,0x04,0x04,0x00,0x04};
146 const static int (*parsers[])(unsigned char *)={table_parser100,table_parser101};
147 unsigned char second_index;
148 second_index = (*pc) >> 4;
149 if (codelen[second_index]<0x10)
150 return codelen[second_index];
152 return parsers[codelen[second_index]-0x10](pc);
155 const static int table_parser02(unsigned char *pc)
157 return (*pc & 0x20)?0x06:0x04;
160 const static int table_parser100(unsigned char *pc)
162 return (*(pc+2) & 0x02)?0x08:0x06;
165 const static int table_parser101(unsigned char *pc)
167 return (*(pc+2) & 0x02)?0x08:0x06;
170 #define BREAK_INST 0x5730 /* TRAPA #3 */
172 int ptrace_set_bpt(struct task_struct *child)
174 unsigned long pc,next;
176 pc = get_reg(child,PT_PC);
177 next = insn_length((unsigned char *)pc) + pc;
178 get_user(insn,(unsigned short *)pc);
179 if (insn == 0x5470) {
182 sp = get_reg(child,PT_USP);
183 get_user(next,(unsigned long *)sp);
184 } else if ((insn & 0xfb00) != 0x5800) {
187 const short reg_tbl[]={PT_ER0,PT_ER1,PT_ER2,PT_ER3,
188 PT_ER4,PT_ER5,PT_ER6,PT_USP};
189 switch(insn & 0xfb00) {
191 regs = (insn & 0x0070) >> 8;
192 next = get_reg(child,reg_tbl[regs]);
195 get_user(next,(unsigned long *)(pc+2));
200 next = *(unsigned long *)(insn & 0xff);
203 } else if (((insn & 0xf000) == 0x4000) || ((insn &0xff00) == 0x5500)) {
206 dsp = (long)(insn && 0xff)+pc+2;
207 child->thread.debugreg[1] = dsp;
208 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
209 put_user(BREAK_INST,(unsigned short *)dsp);
210 } else if (((insn & 0xff00) == 0x5800) || ((insn &0xff00) == 0x5c00)) {
213 get_user(dsp,(unsigned short *)(pc+2));
214 dsp = (long)dsp+pc+4;
215 child->thread.debugreg[1] = dsp;
216 get_user(child->thread.debugreg[5],(unsigned short *)dsp);
217 put_user(BREAK_INST,(unsigned short *)dsp);
219 child->thread.debugreg[0] = next;
220 get_user(child->thread.debugreg[4],(unsigned short *)next);
221 put_user(BREAK_INST,(unsigned short *)next);
226 static int read_long(struct task_struct * tsk, unsigned long addr,
227 unsigned long * result)
229 *result = *(unsigned long *)addr;
233 void ptrace_disable(struct task_struct *child)
235 ptrace_cancel_bpt(child);
238 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
240 struct task_struct *child;
245 if (request == PTRACE_TRACEME) {
246 /* are we already being traced? */
247 if (current->ptrace & PT_PTRACED)
249 /* set the ptrace bit in the process flags. */
250 current->ptrace |= PT_PTRACED;
255 read_lock(&tasklist_lock);
256 child = find_task_by_pid(pid);
258 get_task_struct(child);
259 read_unlock(&tasklist_lock);
264 if (pid == 1) /* you may not mess with init */
267 if (request == PTRACE_ATTACH) {
268 ret = ptrace_attach(child);
272 if (!(child->ptrace & PT_PTRACED))
274 if (child->state != TASK_STOPPED) {
275 if (request != PTRACE_KILL)
278 ret = ptrace_check_attach(child, request == PTRACE_KILL);
283 case PTRACE_PEEKTEXT: /* read word at location addr. */
284 case PTRACE_PEEKDATA: {
287 ret = read_long(child, addr, &tmp);
290 ret = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
292 put_user(tmp, (unsigned long *) data);
296 /* read the word at location addr in the USER area. */
297 case PTRACE_PEEKUSR: {
300 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user))
303 ret = verify_area(VERIFY_WRITE, (void *) data,
307 tmp = 0; /* Default return condition */
308 addr = addr >> 2; /* temporary hack. */
310 tmp = get_reg(child, addr);
315 put_user(tmp,(unsigned long *) data);
320 /* when I and D space are separate, this will have to be fixed. */
321 case PTRACE_POKETEXT: /* write the word at location addr. */
322 case PTRACE_POKEDATA:
324 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
329 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
330 if ((addr & 3) || addr < 0 || addr >= sizeof(struct user)) {
334 addr = addr >> 2; /* temporary hack. */
336 if (addr == PT_ORIG_ER0) {
340 if (addr == PT_CCR) {
344 if (put_reg(child, addr, data))
352 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
353 case PTRACE_CONT: { /* restart after signal. */
355 if ((unsigned long) data >= _NSIG)
357 if (request == PTRACE_SYSCALL)
358 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
360 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
361 child->exit_code = data;
362 wake_up_process(child);
363 /* make sure the single step bit is not set. */
364 ptrace_cancel_bpt(child);
369 * make the child exit. Best I can do is send it a sigkill.
370 * perhaps it should be put in the status that it wants to
376 if (child->state == TASK_ZOMBIE) /* already dead */
378 child->exit_code = SIGKILL;
379 ptrace_cancel_bpt(child);
380 wake_up_process(child);
384 case PTRACE_SINGLESTEP: { /* set the trap flag. */
386 if ((unsigned long) data > _NSIG)
388 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
389 child->thread.debugreg[0]=-1;
390 child->exit_code = data;
391 wake_up_process(child);
396 case PTRACE_DETACH: /* detach a process that was attached. */
397 ret = ptrace_detach(child, data);
400 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
403 for (i = 0; i < 19; i++) {
404 tmp = get_reg(child, i);
405 if (put_user(tmp, (unsigned long *) data)) {
409 data += sizeof(long);
415 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
418 for (i = 0; i < 10; i++) {
419 if (get_user(tmp, (unsigned long *) data)) {
423 put_reg(child, i, tmp);
424 data += sizeof(long);
435 put_task_struct(child);
441 asmlinkage void syscall_trace(void)
443 if (!test_thread_flag(TIF_SYSCALL_TRACE))
445 if (!(current->ptrace & PT_PTRACED))
447 current->exit_code = SIGTRAP;
448 current->state = TASK_STOPPED;
449 notify_parent(current, SIGCHLD);
452 * this isn't the same as continuing with a signal, but it will do
453 * for normal use. strace only continues with a signal if the
454 * stopping signal is not SIGTRAP. -brl
456 if (current->exit_code) {
457 send_sig(current->exit_code, current, 1);
458 current->exit_code = 0;
462 asmlinkage void trace_trap(unsigned long bp)
464 if (current->thread.debugreg[0] == bp ||
465 current->thread.debugreg[1] == bp) {
466 ptrace_cancel_bpt(current);
467 force_sig(SIGTRAP,current);
469 force_sig(SIGILL,current);