blob: 540638ca81f9832fe390b1f0b381d54ea7b2b20e [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>
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 [0] = PT_REG(d1),
51 [1] = PT_REG(d2),
52 [2] = PT_REG(d3),
53 [3] = PT_REG(d4),
54 [4] = PT_REG(d5),
55 [5] = SW_REG(d6),
56 [6] = SW_REG(d7),
57 [7] = PT_REG(a0),
58 [8] = PT_REG(a1),
59 [9] = PT_REG(a2),
60 [10] = SW_REG(a3),
61 [11] = SW_REG(a4),
62 [12] = SW_REG(a5),
63 [13] = SW_REG(a6),
64 [14] = PT_REG(d0),
65 [15] = -1,
66 [16] = PT_REG(orig_d0),
67 [17] = PT_REG(sr),
68 [18] = PT_REG(pc),
69};
70
71/*
72 * Get contents of register REGNO in task TASK.
73 */
74static inline long get_reg(struct task_struct *task, int regno)
75{
76 unsigned long *addr;
77
78 if (regno == PT_USP)
79 addr = &task->thread.usp;
80 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
81 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
82 else
83 return 0;
84 return *addr;
85}
86
87/*
88 * Write contents of register REGNO in task TASK.
89 */
90static inline int put_reg(struct task_struct *task, int regno,
91 unsigned long data)
92{
93 unsigned long *addr;
94
95 if (regno == PT_USP)
96 addr = &task->thread.usp;
97 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
Roman Zippelb3319f52005-09-03 15:57:07 -070098 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 else
100 return -1;
101 *addr = data;
102 return 0;
103}
104
105/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * Make sure the single step bit is not set.
107 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700108static inline void singlestep_disable(struct task_struct *child)
109{
110 unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
111 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800112 clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700113}
114
115/*
116 * Called by kernel/ptrace.c when detaching..
117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118void ptrace_disable(struct task_struct *child)
119{
Roman Zippel69f447c2005-09-03 15:57:08 -0700120 singlestep_disable(child);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800121 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Christoph Hellwig481bed42005-11-07 00:59:47 -0800124long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Roman Zippel69f447c2005-09-03 15:57:08 -0700126 unsigned long tmp;
127 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 switch (request) {
130 /* when I and D space are separate, these will need to be fixed. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700131 case PTRACE_PEEKTEXT: /* read word at location addr. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700132 case PTRACE_PEEKDATA:
133 i = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
134 if (i != sizeof(tmp))
135 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700136 ret = put_user(tmp, (unsigned long *)data);
137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* read the word at location addr in the USER area. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700140 case PTRACE_PEEKUSR:
141 if (addr & 3)
142 goto out_eio;
143 addr >>= 2; /* temporary hack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Roman Zippel69f447c2005-09-03 15:57:08 -0700145 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700146 tmp = get_reg(child, addr);
147 if (addr == PT_SR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 tmp >>= 16;
Roman Zippelb3319f52005-09-03 15:57:07 -0700149 } else if (addr >= 21 && addr < 49) {
150 tmp = child->thread.fp[addr - 21];
Roman Zippelb3319f52005-09-03 15:57:07 -0700151 /* Convert internal fpu reg representation
152 * into long double format
153 */
154 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
155 tmp = ((tmp & 0xffff0000) << 15) |
156 ((tmp & 0x0000ffff) << 16);
Roman Zippelb3319f52005-09-03 15:57:07 -0700157 } else
158 break;
159 ret = put_user(tmp, (unsigned long *)data);
160 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700161
162 /* when I and D space are separate, this will have to be fixed. */
163 case PTRACE_POKETEXT: /* write the word at location addr. */
164 case PTRACE_POKEDATA:
Roman Zippel69f447c2005-09-03 15:57:08 -0700165 if (access_process_vm(child, addr, &data, sizeof(data), 1) != sizeof(data))
166 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700167 break;
168
169 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Roman Zippel69f447c2005-09-03 15:57:08 -0700170 if (addr & 3)
171 goto out_eio;
172 addr >>= 2; /* temporary hack. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700173
174 if (addr == PT_SR) {
175 data &= SR_MASK;
176 data <<= 16;
177 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Roman Zippel69f447c2005-09-03 15:57:08 -0700178 } else if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700179 if (put_reg(child, addr, data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700180 goto out_eio;
181 } else if (addr >= 21 && addr < 48) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700182 /* Convert long double format
183 * into internal fpu reg representation
184 */
185 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
186 data = (unsigned long)data << 15;
187 data = (data & 0xffff0000) |
188 ((data & 0x0000ffff) >> 1);
189 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700190 child->thread.fp[addr - 21] = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700191 } else
192 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700193 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Roman Zippelb3319f52005-09-03 15:57:07 -0700195 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
Roman Zippel69f447c2005-09-03 15:57:08 -0700196 case PTRACE_CONT: /* restart after signal. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700197 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700198 goto out_eio;
199
200 if (request == PTRACE_SYSCALL)
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800201 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700202 else
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800203 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700204 child->exit_code = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700205 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700206 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700207 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700208
209 /*
210 * make the child exit. Best I can do is send it a sigkill.
211 * perhaps it should be put in the status that it wants to
212 * exit.
213 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700214 case PTRACE_KILL:
Roman Zippelb3319f52005-09-03 15:57:07 -0700215 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
216 break;
217 child->exit_code = SIGKILL;
Roman Zippel69f447c2005-09-03 15:57:08 -0700218 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700219 wake_up_process(child);
220 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700221
Roman Zippel69f447c2005-09-03 15:57:08 -0700222 case PTRACE_SINGLESTEP: /* set the trap flag. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700223 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700224 goto out_eio;
225
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800226 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700227 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
228 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800229 set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700230
231 child->exit_code = data;
232 /* give it a chance to run. */
233 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700234 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700235
236 case PTRACE_DETACH: /* detach a process that was attached. */
237 ret = ptrace_detach(child, data);
238 break;
239
Roman Zippel69f447c2005-09-03 15:57:08 -0700240 case PTRACE_GETREGS: /* Get all gp regs from the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700241 for (i = 0; i < 19; i++) {
242 tmp = get_reg(child, i);
243 if (i == PT_SR)
244 tmp >>= 16;
Roman Zippel69f447c2005-09-03 15:57:08 -0700245 ret = put_user(tmp, (unsigned long *)data);
246 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700248 data += sizeof(long);
249 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700250 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700251
Roman Zippel69f447c2005-09-03 15:57:08 -0700252 case PTRACE_SETREGS: /* Set all gp regs in the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700253 for (i = 0; i < 19; i++) {
Roman Zippel69f447c2005-09-03 15:57:08 -0700254 ret = get_user(tmp, (unsigned long *)data);
255 if (ret)
Roman Zippelb3319f52005-09-03 15:57:07 -0700256 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700257 if (i == PT_SR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 tmp &= SR_MASK;
259 tmp <<= 16;
260 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700262 put_reg(child, i, tmp);
263 data += sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Roman Zippel69f447c2005-09-03 15:57:08 -0700267 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700268 if (copy_to_user((void *)data, &child->thread.fp,
269 sizeof(struct user_m68kfp_struct)))
270 ret = -EFAULT;
271 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Roman Zippel69f447c2005-09-03 15:57:08 -0700273 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700274 if (copy_from_user(&child->thread.fp, (void *)data,
275 sizeof(struct user_m68kfp_struct)))
276 ret = -EFAULT;
277 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Roman Zippelb3319f52005-09-03 15:57:07 -0700279 default:
280 ret = ptrace_request(child, request, addr, data);
281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return ret;
Roman Zippel69f447c2005-09-03 15:57:08 -0700285out_eio:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800286 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289asmlinkage void syscall_trace(void)
290{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
292 ? 0x80 : 0));
293 /*
294 * this isn't the same as continuing with a signal, but it will do
295 * for normal use. strace only continues with a signal if the
296 * stopping signal is not SIGTRAP. -brl
297 */
298 if (current->exit_code) {
299 send_sig(current->exit_code, current, 1);
300 current->exit_code = 0;
301 }
302}