blob: 4d3828959fb05465d97de3e4e12cf288f8bd0276 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ptrace.h>
19#include <linux/user.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070020#include <linux/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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 PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
49 PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
50 PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
51 SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
52 PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
53};
54
55/*
56 * Get contents of register REGNO in task TASK.
57 */
58static inline long get_reg(struct task_struct *task, int regno)
59{
60 unsigned long *addr;
61
62 if (regno == PT_USP)
63 addr = &task->thread.usp;
Ahmed S. Darwishbf0059b2007-02-10 01:43:46 -080064 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
66 else
67 return 0;
68 return *addr;
69}
70
71/*
72 * Write contents of register REGNO in task TASK.
73 */
74static inline int put_reg(struct task_struct *task, int regno,
75 unsigned long data)
76{
77 unsigned long *addr;
78
79 if (regno == PT_USP)
80 addr = &task->thread.usp;
Ahmed S. Darwishbf0059b2007-02-10 01:43:46 -080081 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
83 else
84 return -1;
85 *addr = data;
86 return 0;
87}
88
Greg Ungererf60a5572009-07-07 15:54:54 +100089void user_enable_single_step(struct task_struct *task)
90{
91 unsigned long srflags;
92 srflags = get_reg(task, PT_SR) | (TRACE_BITS << 16);
93 put_reg(task, PT_SR, srflags);
94}
95
96void user_disable_single_step(struct task_struct *task)
97{
98 unsigned long srflags;
99 srflags = get_reg(task, PT_SR) & ~(TRACE_BITS << 16);
100 put_reg(task, PT_SR, srflags);
101}
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Called by kernel/ptrace.c when detaching..
105 *
106 * Make sure the single step bit is not set.
107 */
108void ptrace_disable(struct task_struct *child)
109{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* make sure the single step bit is not set. */
Greg Ungererf60a5572009-07-07 15:54:54 +1000111 user_disable_single_step(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Greg Ungererafc7cd82006-01-10 16:42:18 +1000114long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int ret;
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 switch (request) {
119 /* when I and D space are separate, these will need to be fixed. */
120 case PTRACE_PEEKTEXT: /* read word at location addr. */
Alexey Dobriyan76647322007-07-17 04:03:43 -0700121 case PTRACE_PEEKDATA:
122 ret = generic_ptrace_peekdata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 /* read the word at location addr in the USER area. */
126 case PTRACE_PEEKUSR: {
127 unsigned long tmp;
128
129 ret = -EIO;
130 if ((addr & 3) || addr < 0 ||
131 addr > sizeof(struct user) - 3)
132 break;
133
134 tmp = 0; /* Default return condition */
135 addr = addr >> 2; /* temporary hack. */
136 ret = -EIO;
137 if (addr < 19) {
138 tmp = get_reg(child, addr);
139 if (addr == PT_SR)
140 tmp >>= 16;
141 } else if (addr >= 21 && addr < 49) {
142 tmp = child->thread.fp[addr - 21];
143#ifdef CONFIG_M68KFPU_EMU
144 /* Convert internal fpu reg representation
145 * into long double format
146 */
147 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
148 tmp = ((tmp & 0xffff0000) << 15) |
149 ((tmp & 0x0000ffff) << 16);
150#endif
151 } else if (addr == 49) {
152 tmp = child->mm->start_code;
153 } else if (addr == 50) {
154 tmp = child->mm->start_data;
155 } else if (addr == 51) {
156 tmp = child->mm->end_code;
157 } else
158 break;
159 ret = put_user(tmp,(unsigned long *) data);
160 break;
161 }
162
163 /* when I and D space are separate, this will have to be fixed. */
164 case PTRACE_POKETEXT: /* write the word at location addr. */
165 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700166 ret = generic_ptrace_pokedata(child, addr, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168
169 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
170 ret = -EIO;
171 if ((addr & 3) || addr < 0 ||
172 addr > sizeof(struct user) - 3)
173 break;
174
175 addr = addr >> 2; /* temporary hack. */
176
177 if (addr == PT_SR) {
178 data &= SR_MASK;
179 data <<= 16;
180 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
181 }
182 if (addr < 19) {
183 if (put_reg(child, addr, data))
184 break;
185 ret = 0;
186 break;
187 }
188 if (addr >= 21 && addr < 48)
189 {
190#ifdef CONFIG_M68KFPU_EMU
191 /* Convert long double format
192 * into internal fpu reg representation
193 */
194 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
195 data = (unsigned long)data << 15;
196 data = (data & 0xffff0000) |
197 ((data & 0x0000ffff) >> 1);
198 }
199#endif
200 child->thread.fp[addr - 21] = data;
201 ret = 0;
202 }
203 break;
204
205 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
206 case PTRACE_CONT: { /* restart after signal. */
207 long tmp;
208
209 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700210 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 if (request == PTRACE_SYSCALL)
213 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
214 else
215 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
216 child->exit_code = data;
217 /* make sure the single step bit is not set. */
218 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
219 put_reg(child, PT_SR, tmp);
220 wake_up_process(child);
221 ret = 0;
222 break;
223 }
224
225 /*
226 * make the child exit. Best I can do is send it a sigkill.
227 * perhaps it should be put in the status that it wants to
228 * exit.
229 */
230 case PTRACE_KILL: {
231 long tmp;
232
233 ret = 0;
234 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
235 break;
236 child->exit_code = SIGKILL;
237 /* make sure the single step bit is not set. */
238 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
239 put_reg(child, PT_SR, tmp);
240 wake_up_process(child);
241 break;
242 }
243
244 case PTRACE_SINGLESTEP: { /* set the trap flag. */
245 long tmp;
246
247 ret = -EIO;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700248 if (!valid_signal(data))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 break;
250 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
251 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
252 put_reg(child, PT_SR, tmp);
253
254 child->exit_code = data;
255 /* give it a chance to run. */
256 wake_up_process(child);
257 ret = 0;
258 break;
259 }
260
261 case PTRACE_DETACH: /* detach a process that was attached. */
262 ret = ptrace_detach(child, data);
263 break;
264
265 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
266 int i;
267 unsigned long tmp;
268 for (i = 0; i < 19; i++) {
269 tmp = get_reg(child, i);
270 if (i == PT_SR)
271 tmp >>= 16;
272 if (put_user(tmp, (unsigned long *) data)) {
273 ret = -EFAULT;
274 break;
275 }
276 data += sizeof(long);
277 }
278 ret = 0;
279 break;
280 }
281
282 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
283 int i;
284 unsigned long tmp;
285 for (i = 0; i < 19; i++) {
286 if (get_user(tmp, (unsigned long *) data)) {
287 ret = -EFAULT;
288 break;
289 }
290 if (i == PT_SR) {
291 tmp &= SR_MASK;
292 tmp <<= 16;
293 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
294 }
295 put_reg(child, i, tmp);
296 data += sizeof(long);
297 }
298 ret = 0;
299 break;
300 }
301
302#ifdef PTRACE_GETFPREGS
303 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
304 ret = 0;
305 if (copy_to_user((void *)data, &child->thread.fp,
306 sizeof(struct user_m68kfp_struct)))
307 ret = -EFAULT;
308 break;
309 }
310#endif
311
312#ifdef PTRACE_SETFPREGS
313 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
314 ret = 0;
315 if (copy_from_user(&child->thread.fp, (void *)data,
316 sizeof(struct user_m68kfp_struct)))
317 ret = -EFAULT;
318 break;
319 }
320#endif
321
322 default:
323 ret = -EIO;
324 break;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return ret;
327}
328
329asmlinkage void syscall_trace(void)
330{
331 if (!test_thread_flag(TIF_SYSCALL_TRACE))
332 return;
333 if (!(current->ptrace & PT_PTRACED))
334 return;
335 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
336 ? 0x80 : 0));
337 /*
338 * this isn't the same as continuing with a signal, but it will do
339 * for normal use. strace only continues with a signal if the
340 * stopping signal is not SIGTRAP. -brl
341 */
342 if (current->exit_code) {
343 send_sig(current->exit_code, current, 1);
344 current->exit_code = 0;
345 }
346}