blob: 9724e1cd82e5325dba472f78cb18554d165b70ba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/m68knommu/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>
21#include <linux/config.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070022#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <asm/uaccess.h>
25#include <asm/page.h>
26#include <asm/pgtable.h>
27#include <asm/system.h>
28#include <asm/processor.h>
29
30/*
31 * does not yet catch signals sent when the child dies.
32 * in exit.c or in signal.c.
33 */
34
35/* determines which bits in the SR the user has access to. */
36/* 1 = access 0 = no access */
37#define SR_MASK 0x001f
38
39/* sets the trace bits. */
40#define TRACE_BITS 0x8000
41
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). */
49static int regoff[] = {
50 PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
51 PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
52 PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
53 SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
54 PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
55};
56
57/*
58 * Get contents of register REGNO in task TASK.
59 */
60static inline long get_reg(struct task_struct *task, int regno)
61{
62 unsigned long *addr;
63
64 if (regno == PT_USP)
65 addr = &task->thread.usp;
66 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
67 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
68 else
69 return 0;
70 return *addr;
71}
72
73/*
74 * Write contents of register REGNO in task TASK.
75 */
76static inline int put_reg(struct task_struct *task, int regno,
77 unsigned long data)
78{
79 unsigned long *addr;
80
81 if (regno == PT_USP)
82 addr = &task->thread.usp;
83 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
84 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
85 else
86 return -1;
87 *addr = data;
88 return 0;
89}
90
91/*
92 * Called by kernel/ptrace.c when detaching..
93 *
94 * Make sure the single step bit is not set.
95 */
96void ptrace_disable(struct task_struct *child)
97{
98 unsigned long tmp;
99 /* make sure the single step bit is not set. */
100 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
101 put_reg(child, PT_SR, tmp);
102}
103
104asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
105{
106 struct task_struct *child;
107 int ret;
108
109 lock_kernel();
110 ret = -EPERM;
111 if (request == PTRACE_TRACEME) {
112 /* are we already being traced? */
113 if (current->ptrace & PT_PTRACED)
114 goto out;
115 /* set the ptrace bit in the process flags. */
116 current->ptrace |= PT_PTRACED;
117 ret = 0;
118 goto out;
119 }
120 ret = -ESRCH;
121 read_lock(&tasklist_lock);
122 child = find_task_by_pid(pid);
123 if (child)
124 get_task_struct(child);
125 read_unlock(&tasklist_lock);
126 if (!child)
127 goto out;
128
129 ret = -EPERM;
130 if (pid == 1) /* you may not mess with init */
131 goto out_tsk;
132
133 if (request == PTRACE_ATTACH) {
134 ret = ptrace_attach(child);
135 goto out_tsk;
136 }
137 ret = ptrace_check_attach(child, request == PTRACE_KILL);
138 if (ret < 0)
139 goto out_tsk;
140
141 switch (request) {
142 /* when I and D space are separate, these will need to be fixed. */
143 case PTRACE_PEEKTEXT: /* read word at location addr. */
144 case PTRACE_PEEKDATA: {
145 unsigned long tmp;
146 int copied;
147
148 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
149 ret = -EIO;
150 if (copied != sizeof(tmp))
151 break;
152 ret = put_user(tmp,(unsigned long *) data);
153 break;
154 }
155
156 /* read the word at location addr in the USER area. */
157 case PTRACE_PEEKUSR: {
158 unsigned long tmp;
159
160 ret = -EIO;
161 if ((addr & 3) || addr < 0 ||
162 addr > sizeof(struct user) - 3)
163 break;
164
165 tmp = 0; /* Default return condition */
166 addr = addr >> 2; /* temporary hack. */
167 ret = -EIO;
168 if (addr < 19) {
169 tmp = get_reg(child, addr);
170 if (addr == PT_SR)
171 tmp >>= 16;
172 } else if (addr >= 21 && addr < 49) {
173 tmp = child->thread.fp[addr - 21];
174#ifdef CONFIG_M68KFPU_EMU
175 /* Convert internal fpu reg representation
176 * into long double format
177 */
178 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
179 tmp = ((tmp & 0xffff0000) << 15) |
180 ((tmp & 0x0000ffff) << 16);
181#endif
182 } else if (addr == 49) {
183 tmp = child->mm->start_code;
184 } else if (addr == 50) {
185 tmp = child->mm->start_data;
186 } else if (addr == 51) {
187 tmp = child->mm->end_code;
188 } else
189 break;
190 ret = put_user(tmp,(unsigned long *) data);
191 break;
192 }
193
194 /* when I and D space are separate, this will have to be fixed. */
195 case PTRACE_POKETEXT: /* write the word at location addr. */
196 case PTRACE_POKEDATA:
197 ret = 0;
198 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
199 break;
200 ret = -EIO;
201 break;
202
203 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
204 ret = -EIO;
205 if ((addr & 3) || addr < 0 ||
206 addr > sizeof(struct user) - 3)
207 break;
208
209 addr = addr >> 2; /* temporary hack. */
210
211 if (addr == PT_SR) {
212 data &= SR_MASK;
213 data <<= 16;
214 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
215 }
216 if (addr < 19) {
217 if (put_reg(child, addr, data))
218 break;
219 ret = 0;
220 break;
221 }
222 if (addr >= 21 && addr < 48)
223 {
224#ifdef CONFIG_M68KFPU_EMU
225 /* Convert long double format
226 * into internal fpu reg representation
227 */
228 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
229 data = (unsigned long)data << 15;
230 data = (data & 0xffff0000) |
231 ((data & 0x0000ffff) >> 1);
232 }
233#endif
234 child->thread.fp[addr - 21] = data;
235 ret = 0;
236 }
237 break;
238
239 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
240 case PTRACE_CONT: { /* restart after signal. */
241 long tmp;
242
243 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700244 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 break;
246 if (request == PTRACE_SYSCALL)
247 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
248 else
249 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
250 child->exit_code = data;
251 /* make sure the single step bit is not set. */
252 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
253 put_reg(child, PT_SR, tmp);
254 wake_up_process(child);
255 ret = 0;
256 break;
257 }
258
259 /*
260 * make the child exit. Best I can do is send it a sigkill.
261 * perhaps it should be put in the status that it wants to
262 * exit.
263 */
264 case PTRACE_KILL: {
265 long tmp;
266
267 ret = 0;
268 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
269 break;
270 child->exit_code = SIGKILL;
271 /* make sure the single step bit is not set. */
272 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
273 put_reg(child, PT_SR, tmp);
274 wake_up_process(child);
275 break;
276 }
277
278 case PTRACE_SINGLESTEP: { /* set the trap flag. */
279 long tmp;
280
281 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700282 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
285 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
286 put_reg(child, PT_SR, tmp);
287
288 child->exit_code = data;
289 /* give it a chance to run. */
290 wake_up_process(child);
291 ret = 0;
292 break;
293 }
294
295 case PTRACE_DETACH: /* detach a process that was attached. */
296 ret = ptrace_detach(child, data);
297 break;
298
299 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
300 int i;
301 unsigned long tmp;
302 for (i = 0; i < 19; i++) {
303 tmp = get_reg(child, i);
304 if (i == PT_SR)
305 tmp >>= 16;
306 if (put_user(tmp, (unsigned long *) data)) {
307 ret = -EFAULT;
308 break;
309 }
310 data += sizeof(long);
311 }
312 ret = 0;
313 break;
314 }
315
316 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
317 int i;
318 unsigned long tmp;
319 for (i = 0; i < 19; i++) {
320 if (get_user(tmp, (unsigned long *) data)) {
321 ret = -EFAULT;
322 break;
323 }
324 if (i == PT_SR) {
325 tmp &= SR_MASK;
326 tmp <<= 16;
327 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
328 }
329 put_reg(child, i, tmp);
330 data += sizeof(long);
331 }
332 ret = 0;
333 break;
334 }
335
336#ifdef PTRACE_GETFPREGS
337 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
338 ret = 0;
339 if (copy_to_user((void *)data, &child->thread.fp,
340 sizeof(struct user_m68kfp_struct)))
341 ret = -EFAULT;
342 break;
343 }
344#endif
345
346#ifdef PTRACE_SETFPREGS
347 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
348 ret = 0;
349 if (copy_from_user(&child->thread.fp, (void *)data,
350 sizeof(struct user_m68kfp_struct)))
351 ret = -EFAULT;
352 break;
353 }
354#endif
355
356 default:
357 ret = -EIO;
358 break;
359 }
360out_tsk:
361 put_task_struct(child);
362out:
363 unlock_kernel();
364 return ret;
365}
366
367asmlinkage void syscall_trace(void)
368{
369 if (!test_thread_flag(TIF_SYSCALL_TRACE))
370 return;
371 if (!(current->ptrace & PT_PTRACED))
372 return;
373 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
374 ? 0x80 : 0));
375 /*
376 * this isn't the same as continuing with a signal, but it will do
377 * for normal use. strace only continues with a signal if the
378 * stopping signal is not SIGTRAP. -brl
379 */
380 if (current->exit_code) {
381 send_sig(current->exit_code, current, 1);
382 current->exit_code = 0;
383 }
384}