2 * linux/arch/m68k/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>
22 #include <linux/signal.h>
24 #include <asm/uaccess.h>
26 #include <asm/pgtable.h>
27 #include <asm/system.h>
28 #include <asm/processor.h>
31 * does not yet catch signals sent when the child dies.
32 * in exit.c or in signal.c.
35 /* determines which bits in the SR the user has access to. */
36 /* 1 = access 0 = no access */
37 #define SR_MASK 0x001f
39 /* sets the trace bits. */
40 #define TRACE_BITS 0x8000
42 /* Find the stack offset for a register, relative to thread.esp0. */
43 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
44 #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
45 - sizeof(struct switch_stack))
46 /* Mapping from PT_xxx to the stack offset at which the register is
47 saved. Notice that usp has no stack-slot and needs to be treated
48 specially (see get_reg/put_reg below). */
49 static int regoff[] = {
66 [16] = PT_REG(orig_d0),
72 * Get contents of register REGNO in task TASK.
74 static inline long get_reg(struct task_struct *task, int regno)
79 addr = &task->thread.usp;
80 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
81 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
88 * Write contents of register REGNO in task TASK.
90 static inline int put_reg(struct task_struct *task, int regno,
96 addr = &task->thread.usp;
97 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
98 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
106 * Called by kernel/ptrace.c when detaching..
108 * Make sure the single step bit is not set.
110 void ptrace_disable(struct task_struct *child)
113 /* make sure the single step bit is not set. */
114 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
115 put_reg(child, PT_SR, tmp);
116 child->thread.work.delayed_trace = 0;
117 child->thread.work.syscall_trace = 0;
120 asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
122 struct task_struct *child;
127 if (request == PTRACE_TRACEME) {
128 /* are we already being traced? */
129 if (current->ptrace & PT_PTRACED)
131 /* set the ptrace bit in the process flags. */
132 current->ptrace |= PT_PTRACED;
137 read_lock(&tasklist_lock);
138 child = find_task_by_pid(pid);
140 get_task_struct(child);
141 read_unlock(&tasklist_lock);
146 if (pid == 1) /* you may not mess with init */
149 if (request == PTRACE_ATTACH) {
150 ret = ptrace_attach(child);
154 ret = ptrace_check_attach(child, request == PTRACE_KILL);
159 /* when I and D space are separate, these will need to be fixed. */
160 case PTRACE_PEEKTEXT: /* read word at location addr. */
161 case PTRACE_PEEKDATA: {
165 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
167 if (copied != sizeof(tmp))
169 ret = put_user(tmp, (unsigned long *)data);
173 /* read the word at location addr in the USER area. */
174 case PTRACE_PEEKUSR: {
178 if ((addr & 3) || addr < 0 ||
179 addr > sizeof(struct user) - 3)
182 tmp = 0; /* Default return condition */
183 addr = addr >> 2; /* temporary hack. */
186 tmp = get_reg(child, addr);
189 } else if (addr >= 21 && addr < 49) {
190 tmp = child->thread.fp[addr - 21];
191 #ifdef CONFIG_M68KFPU_EMU
192 /* Convert internal fpu reg representation
193 * into long double format
195 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
196 tmp = ((tmp & 0xffff0000) << 15) |
197 ((tmp & 0x0000ffff) << 16);
201 ret = put_user(tmp, (unsigned long *)data);
205 /* when I and D space are separate, this will have to be fixed. */
206 case PTRACE_POKETEXT: /* write the word at location addr. */
207 case PTRACE_POKEDATA:
209 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
214 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
216 if ((addr & 3) || addr < 0 ||
217 addr > sizeof(struct user) - 3)
220 addr = addr >> 2; /* temporary hack. */
225 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
228 if (put_reg(child, addr, data))
233 if (addr >= 21 && addr < 48) {
234 #ifdef CONFIG_M68KFPU_EMU
235 /* Convert long double format
236 * into internal fpu reg representation
238 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
239 data = (unsigned long)data << 15;
240 data = (data & 0xffff0000) |
241 ((data & 0x0000ffff) >> 1);
244 child->thread.fp[addr - 21] = data;
249 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
250 case PTRACE_CONT: { /* restart after signal. */
254 if (!valid_signal(data))
256 if (request == PTRACE_SYSCALL) {
257 child->thread.work.syscall_trace = ~0;
259 child->thread.work.syscall_trace = 0;
261 child->exit_code = data;
262 /* make sure the single step bit is not set. */
263 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
264 put_reg(child, PT_SR, tmp);
265 child->thread.work.delayed_trace = 0;
266 wake_up_process(child);
272 * make the child exit. Best I can do is send it a sigkill.
273 * perhaps it should be put in the status that it wants to
280 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
282 child->exit_code = SIGKILL;
283 /* make sure the single step bit is not set. */
284 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
285 put_reg(child, PT_SR, tmp);
286 child->thread.work.delayed_trace = 0;
287 wake_up_process(child);
291 case PTRACE_SINGLESTEP: { /* set the trap flag. */
295 if (!valid_signal(data))
297 child->thread.work.syscall_trace = 0;
298 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
299 put_reg(child, PT_SR, tmp);
300 child->thread.work.delayed_trace = 1;
302 child->exit_code = data;
303 /* give it a chance to run. */
304 wake_up_process(child);
309 case PTRACE_DETACH: /* detach a process that was attached. */
310 ret = ptrace_detach(child, data);
313 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
316 for (i = 0; i < 19; i++) {
317 tmp = get_reg(child, i);
320 if (put_user(tmp, (unsigned long *)data)) {
324 data += sizeof(long);
330 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
333 for (i = 0; i < 19; i++) {
334 if (get_user(tmp, (unsigned long *)data)) {
341 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
343 put_reg(child, i, tmp);
344 data += sizeof(long);
350 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
352 if (copy_to_user((void *)data, &child->thread.fp,
353 sizeof(struct user_m68kfp_struct)))
358 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
360 if (copy_from_user(&child->thread.fp, (void *)data,
361 sizeof(struct user_m68kfp_struct)))
367 ret = ptrace_request(child, request, addr, data);
371 put_task_struct(child);
377 asmlinkage void syscall_trace(void)
379 if (!current->thread.work.delayed_trace &&
380 !current->thread.work.syscall_trace)
382 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
385 * this isn't the same as continuing with a signal, but it will do
386 * for normal use. strace only continues with a signal if the
387 * stopping signal is not SIGTRAP. -brl
389 if (current->exit_code) {
390 send_sig(current->exit_code, current, 1);
391 current->exit_code = 0;