blob: 616e59752c29bc15f6c0e64c219c626caf755dca [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. */
Andreas Schwabfaa47b42009-05-10 21:14:52 +020038#define TRACE_BITS 0xC000
39#define T1_BIT 0x8000
40#define T0_BIT 0x4000
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
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). */
Andreas Schwabf195e2b2009-05-19 15:38:01 +020049static const int regoff[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 [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;
Ahmed S. Darwishea5e1a82007-02-10 01:43:47 -080080 else if (regno < ARRAY_SIZE(regoff))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
82 else
83 return 0;
Andreas Schwabf195e2b2009-05-19 15:38:01 +020084 /* Need to take stkadj into account. */
85 if (regno == PT_SR || regno == PT_PC) {
86 long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
87 addr = (unsigned long *) ((unsigned long)addr + stkadj);
88 /* The sr is actually a 16 bit register. */
89 if (regno == PT_SR)
90 return *(unsigned short *)addr;
91 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return *addr;
93}
94
95/*
96 * Write contents of register REGNO in task TASK.
97 */
98static inline int put_reg(struct task_struct *task, int regno,
99 unsigned long data)
100{
101 unsigned long *addr;
102
103 if (regno == PT_USP)
104 addr = &task->thread.usp;
Ahmed S. Darwishea5e1a82007-02-10 01:43:47 -0800105 else if (regno < ARRAY_SIZE(regoff))
Roman Zippelb3319f52005-09-03 15:57:07 -0700106 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 else
108 return -1;
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200109 /* Need to take stkadj into account. */
110 if (regno == PT_SR || regno == PT_PC) {
111 long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
112 addr = (unsigned long *) ((unsigned long)addr + stkadj);
113 /* The sr is actually a 16 bit register. */
114 if (regno == PT_SR) {
115 *(unsigned short *)addr = data;
116 return 0;
117 }
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *addr = data;
120 return 0;
121}
122
123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * Make sure the single step bit is not set.
125 */
Roman Zippel69f447c2005-09-03 15:57:08 -0700126static inline void singlestep_disable(struct task_struct *child)
127{
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200128 unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
Roman Zippel69f447c2005-09-03 15:57:08 -0700129 put_reg(child, PT_SR, tmp);
Roman Zippel3b66a1e2005-11-13 16:06:59 -0800130 clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
Roman Zippel69f447c2005-09-03 15:57:08 -0700131}
132
133/*
134 * Called by kernel/ptrace.c when detaching..
135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136void ptrace_disable(struct task_struct *child)
137{
Roman Zippel69f447c2005-09-03 15:57:08 -0700138 singlestep_disable(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Andreas Schwabfaa47b42009-05-10 21:14:52 +0200141void user_enable_single_step(struct task_struct *child)
142{
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200143 unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
144 put_reg(child, PT_SR, tmp | T1_BIT);
Andreas Schwabfaa47b42009-05-10 21:14:52 +0200145 set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
146}
147
148void user_enable_block_step(struct task_struct *child)
149{
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200150 unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
151 put_reg(child, PT_SR, tmp | T0_BIT);
Andreas Schwabfaa47b42009-05-10 21:14:52 +0200152}
153
154void user_disable_single_step(struct task_struct *child)
155{
156 singlestep_disable(child);
157}
158
Christoph Hellwig481bed42005-11-07 00:59:47 -0800159long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Roman Zippel69f447c2005-09-03 15:57:08 -0700161 unsigned long tmp;
162 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 /* read the word at location addr in the USER area. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700166 case PTRACE_PEEKUSR:
167 if (addr & 3)
168 goto out_eio;
169 addr >>= 2; /* temporary hack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Roman Zippel69f447c2005-09-03 15:57:08 -0700171 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700172 tmp = get_reg(child, addr);
Roman Zippelb3319f52005-09-03 15:57:07 -0700173 } else if (addr >= 21 && addr < 49) {
174 tmp = child->thread.fp[addr - 21];
Roman Zippelb3319f52005-09-03 15:57:07 -0700175 /* 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);
Roman Zippelb3319f52005-09-03 15:57:07 -0700181 } else
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200182 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700183 ret = put_user(tmp, (unsigned long *)data);
184 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700185
Roman Zippelb3319f52005-09-03 15:57:07 -0700186 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Roman Zippel69f447c2005-09-03 15:57:08 -0700187 if (addr & 3)
188 goto out_eio;
189 addr >>= 2; /* temporary hack. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700190
191 if (addr == PT_SR) {
192 data &= SR_MASK;
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200193 data |= get_reg(child, PT_SR) & ~SR_MASK;
194 }
195 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700196 if (put_reg(child, addr, data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700197 goto out_eio;
198 } else if (addr >= 21 && addr < 48) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700199 /* Convert long double format
200 * into internal fpu reg representation
201 */
202 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
203 data = (unsigned long)data << 15;
204 data = (data & 0xffff0000) |
205 ((data & 0x0000ffff) >> 1);
206 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700207 child->thread.fp[addr - 21] = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700208 } else
209 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Roman Zippel69f447c2005-09-03 15:57:08 -0700212 case PTRACE_GETREGS: /* Get all gp regs from the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700213 for (i = 0; i < 19; i++) {
214 tmp = get_reg(child, i);
Roman Zippel69f447c2005-09-03 15:57:08 -0700215 ret = put_user(tmp, (unsigned long *)data);
216 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700218 data += sizeof(long);
219 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700220 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700221
Roman Zippel69f447c2005-09-03 15:57:08 -0700222 case PTRACE_SETREGS: /* Set all gp regs in the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700223 for (i = 0; i < 19; i++) {
Roman Zippel69f447c2005-09-03 15:57:08 -0700224 ret = get_user(tmp, (unsigned long *)data);
225 if (ret)
Roman Zippelb3319f52005-09-03 15:57:07 -0700226 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700227 if (i == PT_SR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 tmp &= SR_MASK;
Andreas Schwabf195e2b2009-05-19 15:38:01 +0200229 tmp |= get_reg(child, PT_SR) & ~SR_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700231 put_reg(child, i, tmp);
232 data += sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700234 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Roman Zippel69f447c2005-09-03 15:57:08 -0700236 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700237 if (copy_to_user((void *)data, &child->thread.fp,
238 sizeof(struct user_m68kfp_struct)))
239 ret = -EFAULT;
240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Roman Zippel69f447c2005-09-03 15:57:08 -0700242 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700243 if (copy_from_user(&child->thread.fp, (void *)data,
244 sizeof(struct user_m68kfp_struct)))
245 ret = -EFAULT;
246 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Maxim Kuvyrkov9674cdc2009-12-07 00:24:27 -0800248 case PTRACE_GET_THREAD_AREA:
249 ret = put_user(task_thread_info(child)->tp_value,
250 (unsigned long __user *)data);
251 break;
252
Roman Zippelb3319f52005-09-03 15:57:07 -0700253 default:
254 ret = ptrace_request(child, request, addr, data);
255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return ret;
Roman Zippel69f447c2005-09-03 15:57:08 -0700259out_eio:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800260 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263asmlinkage void syscall_trace(void)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
266 ? 0x80 : 0));
267 /*
268 * this isn't the same as continuing with a signal, but it will do
269 * for normal use. strace only continues with a signal if the
270 * stopping signal is not SIGTRAP. -brl
271 */
272 if (current->exit_code) {
273 send_sig(current->exit_code, current, 1);
274 current->exit_code = 0;
275 }
276}