1/*
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/errno.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
20#include <linux/signal.h>
21
22#include <asm/uaccess.h>
23#include <asm/page.h>
24#include <asm/pgtable.h>
25#include <asm/system.h>
26#include <asm/processor.h>
27
28/*
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
31 */
32
33/* determines which bits in the SR the user has access to. */
34/* 1 = access 0 = no access */
35#define SR_MASK 0x001f
36
37/* sets the trace bits. */
38#define TRACE_BITS 0x8000
39
40/* Find the stack offset for a register, relative to thread.esp0. */
41#define PT_REG(reg)	((long)&((struct pt_regs *)0)->reg)
42#define SW_REG(reg)	((long)&((struct switch_stack *)0)->reg \
43			 - sizeof(struct switch_stack))
44/* Mapping from PT_xxx to the stack offset at which the register is
45   saved.  Notice that usp has no stack-slot and needs to be treated
46   specially (see get_reg/put_reg below). */
47static int regoff[] = {
48	[0]	= PT_REG(d1),
49	[1]	= PT_REG(d2),
50	[2]	= PT_REG(d3),
51	[3]	= PT_REG(d4),
52	[4]	= PT_REG(d5),
53	[5]	= SW_REG(d6),
54	[6]	= SW_REG(d7),
55	[7]	= PT_REG(a0),
56	[8]	= PT_REG(a1),
57	[9]	= PT_REG(a2),
58	[10]	= SW_REG(a3),
59	[11]	= SW_REG(a4),
60	[12]	= SW_REG(a5),
61	[13]	= SW_REG(a6),
62	[14]	= PT_REG(d0),
63	[15]	= -1,
64	[16]	= PT_REG(orig_d0),
65	[17]	= PT_REG(sr),
66	[18]	= PT_REG(pc),
67};
68
69/*
70 * Get contents of register REGNO in task TASK.
71 */
72static inline long get_reg(struct task_struct *task, int regno)
73{
74	unsigned long *addr;
75
76	if (regno == PT_USP)
77		addr = &task->thread.usp;
78	else if (regno < ARRAY_SIZE(regoff))
79		addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
80	else
81		return 0;
82	return *addr;
83}
84
85/*
86 * Write contents of register REGNO in task TASK.
87 */
88static inline int put_reg(struct task_struct *task, int regno,
89			  unsigned long data)
90{
91	unsigned long *addr;
92
93	if (regno == PT_USP)
94		addr = &task->thread.usp;
95	else if (regno < ARRAY_SIZE(regoff))
96		addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
97	else
98		return -1;
99	*addr = data;
100	return 0;
101}
102
103/*
104 * Make sure the single step bit is not set.
105 */
106static inline void singlestep_disable(struct task_struct *child)
107{
108	unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
109	put_reg(child, PT_SR, tmp);
110	clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
111}
112
113/*
114 * Called by kernel/ptrace.c when detaching..
115 */
116void ptrace_disable(struct task_struct *child)
117{
118	singlestep_disable(child);
119	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
120}
121
122long arch_ptrace(struct task_struct *child, long request, long addr, long data)
123{
124	unsigned long tmp;
125	int i, ret = 0;
126
127	switch (request) {
128	/* when I and D space are separate, these will need to be fixed. */
129	case PTRACE_PEEKTEXT:	/* read word at location addr. */
130	case PTRACE_PEEKDATA:
131		i = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
132		if (i != sizeof(tmp))
133			goto out_eio;
134		ret = put_user(tmp, (unsigned long *)data);
135		break;
136
137	/* read the word at location addr in the USER area. */
138	case PTRACE_PEEKUSR:
139		if (addr & 3)
140			goto out_eio;
141		addr >>= 2;	/* temporary hack. */
142
143		if (addr >= 0 && addr < 19) {
144			tmp = get_reg(child, addr);
145			if (addr == PT_SR)
146				tmp >>= 16;
147		} else if (addr >= 21 && addr < 49) {
148			tmp = child->thread.fp[addr - 21];
149			/* Convert internal fpu reg representation
150			 * into long double format
151			 */
152			if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
153				tmp = ((tmp & 0xffff0000) << 15) |
154				      ((tmp & 0x0000ffff) << 16);
155		} else
156			break;
157		ret = put_user(tmp, (unsigned long *)data);
158		break;
159
160	/* when I and D space are separate, this will have to be fixed. */
161	case PTRACE_POKETEXT:	/* write the word at location addr. */
162	case PTRACE_POKEDATA:
163		if (access_process_vm(child, addr, &data, sizeof(data), 1) != sizeof(data))
164			goto out_eio;
165		break;
166
167	case PTRACE_POKEUSR:	/* write the word at location addr in the USER area */
168		if (addr & 3)
169			goto out_eio;
170		addr >>= 2;	/* temporary hack. */
171
172		if (addr == PT_SR) {
173			data &= SR_MASK;
174			data <<= 16;
175			data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
176		} else if (addr >= 0 && addr < 19) {
177			if (put_reg(child, addr, data))
178				goto out_eio;
179		} else if (addr >= 21 && addr < 48) {
180			/* Convert long double format
181			 * into internal fpu reg representation
182			 */
183			if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
184				data = (unsigned long)data << 15;
185				data = (data & 0xffff0000) |
186				       ((data & 0x0000ffff) >> 1);
187			}
188			child->thread.fp[addr - 21] = data;
189		} else
190			goto out_eio;
191		break;
192
193	case PTRACE_SYSCALL:	/* continue and stop at next (return from) syscall */
194	case PTRACE_CONT:	/* restart after signal. */
195		if (!valid_signal(data))
196			goto out_eio;
197
198		if (request == PTRACE_SYSCALL)
199			set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
200		else
201			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
202		child->exit_code = data;
203		singlestep_disable(child);
204		wake_up_process(child);
205		break;
206
207	/*
208	 * make the child exit.  Best I can do is send it a sigkill.
209	 * perhaps it should be put in the status that it wants to
210	 * exit.
211	 */
212	case PTRACE_KILL:
213		if (child->exit_state == EXIT_ZOMBIE) /* already dead */
214			break;
215		child->exit_code = SIGKILL;
216		singlestep_disable(child);
217		wake_up_process(child);
218		break;
219
220	case PTRACE_SINGLESTEP:	/* set the trap flag. */
221		if (!valid_signal(data))
222			goto out_eio;
223
224		clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
225		tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
226		put_reg(child, PT_SR, tmp);
227		set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
228
229		child->exit_code = data;
230		/* give it a chance to run. */
231		wake_up_process(child);
232		break;
233
234	case PTRACE_DETACH:	/* detach a process that was attached. */
235		ret = ptrace_detach(child, data);
236		break;
237
238	case PTRACE_GETREGS:	/* Get all gp regs from the child. */
239		for (i = 0; i < 19; i++) {
240			tmp = get_reg(child, i);
241			if (i == PT_SR)
242				tmp >>= 16;
243			ret = put_user(tmp, (unsigned long *)data);
244			if (ret)
245				break;
246			data += sizeof(long);
247		}
248		break;
249
250	case PTRACE_SETREGS:	/* Set all gp regs in the child. */
251		for (i = 0; i < 19; i++) {
252			ret = get_user(tmp, (unsigned long *)data);
253			if (ret)
254				break;
255			if (i == PT_SR) {
256				tmp &= SR_MASK;
257				tmp <<= 16;
258				tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
259			}
260			put_reg(child, i, tmp);
261			data += sizeof(long);
262		}
263		break;
264
265	case PTRACE_GETFPREGS:	/* Get the child FPU state. */
266		if (copy_to_user((void *)data, &child->thread.fp,
267				 sizeof(struct user_m68kfp_struct)))
268			ret = -EFAULT;
269		break;
270
271	case PTRACE_SETFPREGS:	/* Set the child FPU state. */
272		if (copy_from_user(&child->thread.fp, (void *)data,
273				   sizeof(struct user_m68kfp_struct)))
274			ret = -EFAULT;
275		break;
276
277	default:
278		ret = ptrace_request(child, request, addr, data);
279		break;
280	}
281
282	return ret;
283out_eio:
284	return -EIO;
285}
286
287asmlinkage void syscall_trace(void)
288{
289	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
290				 ? 0x80 : 0));
291	/*
292	 * this isn't the same as continuing with a signal, but it will do
293	 * for normal use.  strace only continues with a signal if the
294	 * stopping signal is not SIGTRAP.  -brl
295	 */
296	if (current->exit_code) {
297		send_sig(current->exit_code, current, 1);
298		current->exit_code = 0;
299	}
300}
301