2 * linux/arch/m68knommu/kernel/ptrace.c
4 * Copyright (C) 1994 by Hamish Macdonald
5 * Taken from linux/kernel/ptrace.c and modified for M680x0.
6 * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
8 * This file is subject to the terms and conditions of the GNU General
9 * Public License. See the file COPYING in the main directory of
10 * this archive for more details.
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.h>
21 #include <linux/config.h>
23 #include <asm/uaccess.h>
25 #include <asm/pgtable.h>
26 #include <asm/system.h>
27 #include <asm/processor.h>
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
34 /* determines which bits in the SR the user has access to. */
35 /* 1 = access 0 = no access */
36 #define SR_MASK 0x001f
38 /* sets the trace bits. */
39 #define TRACE_BITS 0x8000
41 /* Find the stack offset for a register, relative to thread.esp0. */
42 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
43 #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
44 - sizeof(struct switch_stack))
45 /* Mapping from PT_xxx to the stack offset at which the register is
46 saved. Notice that usp has no stack-slot and needs to be treated
47 specially (see get_reg/put_reg below). */
48 static int regoff[] = {
49 PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
50 PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
51 PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
52 SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
53 PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
57 * Get contents of register REGNO in task TASK.
59 static inline long get_reg(struct task_struct *task, int regno)
64 addr = &task->thread.usp;
65 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
66 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
73 * Write contents of register REGNO in task TASK.
75 static inline int put_reg(struct task_struct *task, int regno,
81 addr = &task->thread.usp;
82 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
83 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
91 * Called by kernel/ptrace.c when detaching..
93 * Make sure the single step bit is not set.
95 void ptrace_disable(struct task_struct *child)
98 /* make sure the single step bit is not set. */
99 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
100 put_reg(child, PT_SR, tmp);
103 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
105 struct task_struct *child;
110 if (request == PTRACE_TRACEME) {
111 /* are we already being traced? */
112 if (current->ptrace & PT_PTRACED)
114 /* set the ptrace bit in the process flags. */
115 current->ptrace |= PT_PTRACED;
120 read_lock(&tasklist_lock);
121 child = find_task_by_pid(pid);
123 get_task_struct(child);
124 read_unlock(&tasklist_lock);
127 if (!vx_check(vx_task_xid(child), VX_WATCH|VX_IDENT))
131 if (pid == 1) /* you may not mess with init */
134 if (request == PTRACE_ATTACH) {
135 ret = ptrace_attach(child);
139 if (!(child->ptrace & PT_PTRACED))
141 if (child->state != TASK_STOPPED) {
142 if (request != PTRACE_KILL)
145 ret = ptrace_check_attach(child, request == PTRACE_KILL);
150 /* when I and D space are separate, these will need to be fixed. */
151 case PTRACE_PEEKTEXT: /* read word at location addr. */
152 case PTRACE_PEEKDATA: {
156 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
158 if (copied != sizeof(tmp))
160 ret = put_user(tmp,(unsigned long *) data);
164 /* read the word at location addr in the USER area. */
165 case PTRACE_PEEKUSR: {
169 if ((addr & 3) || addr < 0 ||
170 addr > sizeof(struct user) - 3)
173 tmp = 0; /* Default return condition */
174 addr = addr >> 2; /* temporary hack. */
177 tmp = get_reg(child, addr);
180 } else if (addr >= 21 && addr < 49) {
181 tmp = child->thread.fp[addr - 21];
182 #ifdef CONFIG_M68KFPU_EMU
183 /* Convert internal fpu reg representation
184 * into long double format
186 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
187 tmp = ((tmp & 0xffff0000) << 15) |
188 ((tmp & 0x0000ffff) << 16);
190 } else if (addr == 49) {
191 tmp = child->mm->start_code;
192 } else if (addr == 50) {
193 tmp = child->mm->start_data;
194 } else if (addr == 51) {
195 tmp = child->mm->end_code;
198 ret = put_user(tmp,(unsigned long *) data);
202 /* when I and D space are separate, this will have to be fixed. */
203 case PTRACE_POKETEXT: /* write the word at location addr. */
204 case PTRACE_POKEDATA:
206 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
211 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
213 if ((addr & 3) || addr < 0 ||
214 addr > sizeof(struct user) - 3)
217 addr = addr >> 2; /* temporary hack. */
222 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
225 if (put_reg(child, addr, data))
230 if (addr >= 21 && addr < 48)
232 #ifdef CONFIG_M68KFPU_EMU
233 /* Convert long double format
234 * into internal fpu reg representation
236 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
237 data = (unsigned long)data << 15;
238 data = (data & 0xffff0000) |
239 ((data & 0x0000ffff) >> 1);
242 child->thread.fp[addr - 21] = data;
247 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
248 case PTRACE_CONT: { /* restart after signal. */
252 if ((unsigned long) data > _NSIG)
254 if (request == PTRACE_SYSCALL)
255 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
257 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
258 child->exit_code = data;
259 /* make sure the single step bit is not set. */
260 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
261 put_reg(child, PT_SR, tmp);
262 wake_up_process(child);
268 * make the child exit. Best I can do is send it a sigkill.
269 * perhaps it should be put in the status that it wants to
276 if (child->state == TASK_ZOMBIE) /* already dead */
278 child->exit_code = SIGKILL;
279 /* make sure the single step bit is not set. */
280 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
281 put_reg(child, PT_SR, tmp);
282 wake_up_process(child);
286 case PTRACE_SINGLESTEP: { /* set the trap flag. */
290 if ((unsigned long) data > _NSIG)
292 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
293 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
294 put_reg(child, PT_SR, tmp);
296 child->exit_code = data;
297 /* give it a chance to run. */
298 wake_up_process(child);
303 case PTRACE_DETACH: /* detach a process that was attached. */
304 ret = ptrace_detach(child, data);
307 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
310 for (i = 0; i < 19; i++) {
311 tmp = get_reg(child, i);
314 if (put_user(tmp, (unsigned long *) data)) {
318 data += sizeof(long);
324 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
327 for (i = 0; i < 19; i++) {
328 if (get_user(tmp, (unsigned long *) data)) {
335 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
337 put_reg(child, i, tmp);
338 data += sizeof(long);
344 #ifdef PTRACE_GETFPREGS
345 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
347 if (copy_to_user((void *)data, &child->thread.fp,
348 sizeof(struct user_m68kfp_struct)))
354 #ifdef PTRACE_SETFPREGS
355 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
357 if (copy_from_user(&child->thread.fp, (void *)data,
358 sizeof(struct user_m68kfp_struct)))
369 put_task_struct(child);
375 asmlinkage void syscall_trace(void)
377 if (!test_thread_flag(TIF_SYSCALL_TRACE))
379 if (!(current->ptrace & PT_PTRACED))
381 current->exit_code = SIGTRAP;
382 current->state = TASK_STOPPED;
383 notify_parent(current, SIGCHLD);
386 * this isn't the same as continuing with a signal, but it will do
387 * for normal use. strace only continues with a signal if the
388 * stopping signal is not SIGTRAP. -brl
390 if (current->exit_code) {
391 send_sig(current->exit_code, current, 1);
392 current->exit_code = 0;