blob: 2075543c2d92182ce03d89a05479edf912559e36 [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>
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 [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;
Ahmed S. Darwishea5e1a82007-02-10 01:43:47 -080078 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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;
Ahmed S. Darwishea5e1a82007-02-10 01:43:47 -080095 else if (regno < ARRAY_SIZE(regoff))
Roman Zippelb3319f52005-09-03 15:57:07 -070096 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 else
98 return -1;
99 *addr = data;
100 return 0;
101}
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Make sure the single step bit is not set.
105 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700106static 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);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800110 clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700111}
112
113/*
114 * Called by kernel/ptrace.c when detaching..
115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116void ptrace_disable(struct task_struct *child)
117{
Roman Zippel69f447c2005-09-03 15:57:08 -0700118 singlestep_disable(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Christoph Hellwig481bed42005-11-07 00:59:47 -0800121long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Roman Zippel69f447c2005-09-03 15:57:08 -0700123 unsigned long tmp;
124 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 switch (request) {
127 /* when I and D space are separate, these will need to be fixed. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700128 case PTRACE_PEEKTEXT: /* read word at location addr. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700129 case PTRACE_PEEKDATA:
Alexey Dobriyan76647322007-07-17 04:03:43 -0700130 ret = generic_ptrace_peekdata(child, addr, data);
Roman Zippelb3319f52005-09-03 15:57:07 -0700131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* read the word at location addr in the USER area. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700134 case PTRACE_PEEKUSR:
135 if (addr & 3)
136 goto out_eio;
137 addr >>= 2; /* temporary hack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Roman Zippel69f447c2005-09-03 15:57:08 -0700139 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700140 tmp = get_reg(child, addr);
141 if (addr == PT_SR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 tmp >>= 16;
Roman Zippelb3319f52005-09-03 15:57:07 -0700143 } else if (addr >= 21 && addr < 49) {
144 tmp = child->thread.fp[addr - 21];
Roman Zippelb3319f52005-09-03 15:57:07 -0700145 /* Convert internal fpu reg representation
146 * into long double format
147 */
148 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
149 tmp = ((tmp & 0xffff0000) << 15) |
150 ((tmp & 0x0000ffff) << 16);
Roman Zippelb3319f52005-09-03 15:57:07 -0700151 } else
152 break;
153 ret = put_user(tmp, (unsigned long *)data);
154 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700155
156 /* when I and D space are separate, this will have to be fixed. */
157 case PTRACE_POKETEXT: /* write the word at location addr. */
158 case PTRACE_POKEDATA:
Alexey Dobriyanf284ce72007-07-17 04:03:44 -0700159 ret = generic_ptrace_pokedata(child, addr, data);
Roman Zippelb3319f52005-09-03 15:57:07 -0700160 break;
161
162 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Roman Zippel69f447c2005-09-03 15:57:08 -0700163 if (addr & 3)
164 goto out_eio;
165 addr >>= 2; /* temporary hack. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700166
167 if (addr == PT_SR) {
168 data &= SR_MASK;
169 data <<= 16;
170 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Roman Zippel69f447c2005-09-03 15:57:08 -0700171 } else if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700172 if (put_reg(child, addr, data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700173 goto out_eio;
174 } else if (addr >= 21 && addr < 48) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700175 /* Convert long double format
176 * into internal fpu reg representation
177 */
178 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
179 data = (unsigned long)data << 15;
180 data = (data & 0xffff0000) |
181 ((data & 0x0000ffff) >> 1);
182 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700183 child->thread.fp[addr - 21] = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700184 } else
185 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Roman Zippelb3319f52005-09-03 15:57:07 -0700188 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
Roman Zippel69f447c2005-09-03 15:57:08 -0700189 case PTRACE_CONT: /* restart after signal. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700190 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700191 goto out_eio;
192
193 if (request == PTRACE_SYSCALL)
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800194 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700195 else
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800196 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700197 child->exit_code = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700198 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700199 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700200 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700201
202 /*
203 * make the child exit. Best I can do is send it a sigkill.
204 * perhaps it should be put in the status that it wants to
205 * exit.
206 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700207 case PTRACE_KILL:
Roman Zippelb3319f52005-09-03 15:57:07 -0700208 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
209 break;
210 child->exit_code = SIGKILL;
Roman Zippel69f447c2005-09-03 15:57:08 -0700211 singlestep_disable(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700212 wake_up_process(child);
213 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700214
Roman Zippel69f447c2005-09-03 15:57:08 -0700215 case PTRACE_SINGLESTEP: /* set the trap flag. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700216 if (!valid_signal(data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700217 goto out_eio;
218
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800219 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700220 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
221 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800222 set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippelb3319f52005-09-03 15:57:07 -0700223
224 child->exit_code = data;
225 /* give it a chance to run. */
226 wake_up_process(child);
Roman Zippelb3319f52005-09-03 15:57:07 -0700227 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700228
Roman Zippel69f447c2005-09-03 15:57:08 -0700229 case PTRACE_GETREGS: /* Get all gp regs from the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700230 for (i = 0; i < 19; i++) {
231 tmp = get_reg(child, i);
232 if (i == PT_SR)
233 tmp >>= 16;
Roman Zippel69f447c2005-09-03 15:57:08 -0700234 ret = put_user(tmp, (unsigned long *)data);
235 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700237 data += sizeof(long);
238 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700239 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700240
Roman Zippel69f447c2005-09-03 15:57:08 -0700241 case PTRACE_SETREGS: /* Set all gp regs in the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700242 for (i = 0; i < 19; i++) {
Roman Zippel69f447c2005-09-03 15:57:08 -0700243 ret = get_user(tmp, (unsigned long *)data);
244 if (ret)
Roman Zippelb3319f52005-09-03 15:57:07 -0700245 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700246 if (i == PT_SR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 tmp &= SR_MASK;
248 tmp <<= 16;
249 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700251 put_reg(child, i, tmp);
252 data += sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700254 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Roman Zippel69f447c2005-09-03 15:57:08 -0700256 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700257 if (copy_to_user((void *)data, &child->thread.fp,
258 sizeof(struct user_m68kfp_struct)))
259 ret = -EFAULT;
260 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Roman Zippel69f447c2005-09-03 15:57:08 -0700262 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700263 if (copy_from_user(&child->thread.fp, (void *)data,
264 sizeof(struct user_m68kfp_struct)))
265 ret = -EFAULT;
266 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Roman Zippelb3319f52005-09-03 15:57:07 -0700268 default:
269 ret = ptrace_request(child, request, addr, data);
270 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return ret;
Roman Zippel69f447c2005-09-03 15:57:08 -0700274out_eio:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800275 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278asmlinkage void syscall_trace(void)
279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
281 ? 0x80 : 0));
282 /*
283 * this isn't the same as continuing with a signal, but it will do
284 * for normal use. strace only continues with a signal if the
285 * stopping signal is not SIGTRAP. -brl
286 */
287 if (current->exit_code) {
288 send_sig(current->exit_code, current, 1);
289 current->exit_code = 0;
290 }
291}