blob: ef89060ea328374d25fe9d03a8a449cf9126d52f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68k/kernel/ptrace.c
3 *
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
7 *
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.
11 */
12
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/errno.h>
19#include <linux/ptrace.h>
20#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070021#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/uaccess.h>
24#include <asm/page.h>
25#include <asm/pgtable.h>
26#include <asm/system.h>
27#include <asm/processor.h>
28
29/*
30 * does not yet catch signals sent when the child dies.
31 * in exit.c or in signal.c.
32 */
33
34/* determines which bits in the SR the user has access to. */
35/* 1 = access 0 = no access */
36#define SR_MASK 0x001f
37
38/* sets the trace bits. */
39#define TRACE_BITS 0x8000
40
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). */
48static int regoff[] = {
49 [0] = PT_REG(d1),
50 [1] = PT_REG(d2),
51 [2] = PT_REG(d3),
52 [3] = PT_REG(d4),
53 [4] = PT_REG(d5),
54 [5] = SW_REG(d6),
55 [6] = SW_REG(d7),
56 [7] = PT_REG(a0),
57 [8] = PT_REG(a1),
58 [9] = PT_REG(a2),
59 [10] = SW_REG(a3),
60 [11] = SW_REG(a4),
61 [12] = SW_REG(a5),
62 [13] = SW_REG(a6),
63 [14] = PT_REG(d0),
64 [15] = -1,
65 [16] = PT_REG(orig_d0),
66 [17] = PT_REG(sr),
67 [18] = PT_REG(pc),
68};
69
70/*
71 * Get contents of register REGNO in task TASK.
72 */
73static inline long get_reg(struct task_struct *task, int regno)
74{
75 unsigned long *addr;
76
77 if (regno == PT_USP)
78 addr = &task->thread.usp;
79 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
80 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
81 else
82 return 0;
83 return *addr;
84}
85
86/*
87 * Write contents of register REGNO in task TASK.
88 */
89static inline int put_reg(struct task_struct *task, int regno,
90 unsigned long data)
91{
92 unsigned long *addr;
93
94 if (regno == PT_USP)
95 addr = &task->thread.usp;
96 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
Roman Zippelb3319f52005-09-03 15:57:07 -070097 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 else
99 return -1;
100 *addr = data;
101 return 0;
102}
103
104/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 * Make sure the single step bit is not set.
106 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700107static inline void singlestep_disable(struct task_struct *child)
108{
109 unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
110 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800111 clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700112}
113
114/*
115 * Called by kernel/ptrace.c when detaching..
116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117void ptrace_disable(struct task_struct *child)
118{
Roman Zippel69f447c2005-09-03 15:57:08 -0700119 singlestep_disable(child);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800120 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Christoph Hellwig481bed42005-11-07 00:59:47 -0800123long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Roman Zippel69f447c2005-09-03 15:57:08 -0700125 unsigned long tmp;
126 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 switch (request) {
129 /* when I and D space are separate, these will need to be fixed. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700130 case PTRACE_PEEKTEXT: /* read word at location addr. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700131 case PTRACE_PEEKDATA:
132 i = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
133 if (i != sizeof(tmp))
134 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700135 ret = put_user(tmp, (unsigned long *)data);
136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* read the word at location addr in the USER area. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700139 case PTRACE_PEEKUSR:
140 if (addr & 3)
141 goto out_eio;
142 addr >>= 2; /* temporary hack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Roman Zippel69f447c2005-09-03 15:57:08 -0700144 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700145 tmp = get_reg(child, addr);
146 if (addr == PT_SR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 tmp >>= 16;
Roman Zippelb3319f52005-09-03 15:57:07 -0700148 } else if (addr >= 21 && addr < 49) {
149 tmp = child->thread.fp[addr - 21];
Roman Zippelb3319f52005-09-03 15:57:07 -0700150 /* Convert internal fpu reg representation
151 * into long double format
152 */
153 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
154 tmp = ((tmp & 0xffff0000) << 15) |
155 ((tmp & 0x0000ffff) << 16);
Roman Zippelb3319f52005-09-03 15:57:07 -0700156 } else
157 break;
158 ret = put_user(tmp, (unsigned long *)data);
159 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700160
161 /* when I and D space are separate, this will have to be fixed. */
162 case PTRACE_POKETEXT: /* write the word at location addr. */
163 case PTRACE_POKEDATA:
Roman Zippel69f447c2005-09-03 15:57:08 -0700164 if (access_process_vm(child, addr, &data, sizeof(data), 1) != sizeof(data))
165 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700166 break;
167
168 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Roman Zippel69f447c2005-09-03 15:57:08 -0700169 if (addr & 3)
170 goto out_eio;
171 addr >>= 2; /* temporary hack. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700172
173 if (addr == PT_SR) {
174 data &= SR_MASK;
175 data <<= 16;
176 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Roman Zippel69f447c2005-09-03 15:57:08 -0700177 } else if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700178 if (put_reg(child, addr, data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700179 goto out_eio;
180 } else if (addr >= 21 && addr < 48) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700181 /* Convert long double format
182 * into internal fpu reg representation
183 */
184 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
185 data = (unsigned long)data << 15;
186 data = (data & 0xffff0000) |
187 ((data & 0x0000ffff) >> 1);
188 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700189 child->thread.fp[addr - 21] = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700190 } else
191 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700192 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Roman Zippelb3319f52005-09-03 15:57:07 -0700194 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
Roman Zippel69f447c2005-09-03 15:57:08 -0700195 case PTRACE_CONT: /* restart after signal. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700196 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700197 goto out_eio;
198
199 if (request == PTRACE_SYSCALL)
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800200 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700201 else
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800202 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700203 child->exit_code = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700204 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700205 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700206 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700207
208 /*
209 * make the child exit. Best I can do is send it a sigkill.
210 * perhaps it should be put in the status that it wants to
211 * exit.
212 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700213 case PTRACE_KILL:
Roman Zippelb3319f52005-09-03 15:57:07 -0700214 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
215 break;
216 child->exit_code = SIGKILL;
Roman Zippel69f447c2005-09-03 15:57:08 -0700217 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700218 wake_up_process(child);
219 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700220
Roman Zippel69f447c2005-09-03 15:57:08 -0700221 case PTRACE_SINGLESTEP: /* set the trap flag. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700222 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700223 goto out_eio;
224
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800225 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700226 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
227 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800228 set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700229
230 child->exit_code = data;
231 /* give it a chance to run. */
232 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700233 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700234
235 case PTRACE_DETACH: /* detach a process that was attached. */
236 ret = ptrace_detach(child, data);
237 break;
238
Roman Zippel69f447c2005-09-03 15:57:08 -0700239 case PTRACE_GETREGS: /* Get all gp regs from the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700240 for (i = 0; i < 19; i++) {
241 tmp = get_reg(child, i);
242 if (i == PT_SR)
243 tmp >>= 16;
Roman Zippel69f447c2005-09-03 15:57:08 -0700244 ret = put_user(tmp, (unsigned long *)data);
245 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700247 data += sizeof(long);
248 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700249 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700250
Roman Zippel69f447c2005-09-03 15:57:08 -0700251 case PTRACE_SETREGS: /* Set all gp regs in the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700252 for (i = 0; i < 19; i++) {
Roman Zippel69f447c2005-09-03 15:57:08 -0700253 ret = get_user(tmp, (unsigned long *)data);
254 if (ret)
Roman Zippelb3319f52005-09-03 15:57:07 -0700255 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700256 if (i == PT_SR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 tmp &= SR_MASK;
258 tmp <<= 16;
259 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700261 put_reg(child, i, tmp);
262 data += sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Roman Zippel69f447c2005-09-03 15:57:08 -0700266 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700267 if (copy_to_user((void *)data, &child->thread.fp,
268 sizeof(struct user_m68kfp_struct)))
269 ret = -EFAULT;
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Roman Zippel69f447c2005-09-03 15:57:08 -0700272 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700273 if (copy_from_user(&child->thread.fp, (void *)data,
274 sizeof(struct user_m68kfp_struct)))
275 ret = -EFAULT;
276 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Roman Zippelb3319f52005-09-03 15:57:07 -0700278 default:
279 ret = ptrace_request(child, request, addr, data);
280 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return ret;
Roman Zippel69f447c2005-09-03 15:57:08 -0700284out_eio:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800285 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288asmlinkage void syscall_trace(void)
289{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
291 ? 0x80 : 0));
292 /*
293 * this isn't the same as continuing with a signal, but it will do
294 * for normal use. strace only continues with a signal if the
295 * stopping signal is not SIGTRAP. -brl
296 */
297 if (current->exit_code) {
298 send_sig(current->exit_code, current, 1);
299 current->exit_code = 0;
300 }
301}