blob: bd0842059d115c86c01bae9981d2642fe6d0d6d7 [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). */
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;
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;
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;
Ahmed S. Darwishea5e1a82007-02-10 01:43:47 -080097 else if (regno < ARRAY_SIZE(regoff))
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Andreas Schwabfaa47b42009-05-10 21:14:52 +0200123void user_enable_single_step(struct task_struct *child)
124{
125 unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
126 put_reg(child, PT_SR, tmp | (T1_BIT << 16));
127 set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
128}
129
130void user_enable_block_step(struct task_struct *child)
131{
132 unsigned long tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
133 put_reg(child, PT_SR, tmp | (T0_BIT << 16));
134}
135
136void user_disable_single_step(struct task_struct *child)
137{
138 singlestep_disable(child);
139}
140
Christoph Hellwig481bed42005-11-07 00:59:47 -0800141long arch_ptrace(struct task_struct *child, long request, long addr, long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Roman Zippel69f447c2005-09-03 15:57:08 -0700143 unsigned long tmp;
144 int i, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 switch (request) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 /* read the word at location addr in the USER area. */
Roman Zippel69f447c2005-09-03 15:57:08 -0700148 case PTRACE_PEEKUSR:
149 if (addr & 3)
150 goto out_eio;
151 addr >>= 2; /* temporary hack. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Roman Zippel69f447c2005-09-03 15:57:08 -0700153 if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700154 tmp = get_reg(child, addr);
155 if (addr == PT_SR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 tmp >>= 16;
Roman Zippelb3319f52005-09-03 15:57:07 -0700157 } else if (addr >= 21 && addr < 49) {
158 tmp = child->thread.fp[addr - 21];
Roman Zippelb3319f52005-09-03 15:57:07 -0700159 /* Convert internal fpu reg representation
160 * into long double format
161 */
162 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
163 tmp = ((tmp & 0xffff0000) << 15) |
164 ((tmp & 0x0000ffff) << 16);
Roman Zippelb3319f52005-09-03 15:57:07 -0700165 } else
166 break;
167 ret = put_user(tmp, (unsigned long *)data);
168 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700169
Roman Zippelb3319f52005-09-03 15:57:07 -0700170 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
Roman Zippel69f447c2005-09-03 15:57:08 -0700171 if (addr & 3)
172 goto out_eio;
173 addr >>= 2; /* temporary hack. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700174
175 if (addr == PT_SR) {
176 data &= SR_MASK;
177 data <<= 16;
178 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Roman Zippel69f447c2005-09-03 15:57:08 -0700179 } else if (addr >= 0 && addr < 19) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700180 if (put_reg(child, addr, data))
Roman Zippel69f447c2005-09-03 15:57:08 -0700181 goto out_eio;
182 } else if (addr >= 21 && addr < 48) {
Roman Zippelb3319f52005-09-03 15:57:07 -0700183 /* Convert long double format
184 * into internal fpu reg representation
185 */
186 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
187 data = (unsigned long)data << 15;
188 data = (data & 0xffff0000) |
189 ((data & 0x0000ffff) >> 1);
190 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700191 child->thread.fp[addr - 21] = data;
Roman Zippel69f447c2005-09-03 15:57:08 -0700192 } else
193 goto out_eio;
Roman Zippelb3319f52005-09-03 15:57:07 -0700194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Roman Zippel69f447c2005-09-03 15:57:08 -0700196 case PTRACE_GETREGS: /* Get all gp regs from the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700197 for (i = 0; i < 19; i++) {
198 tmp = get_reg(child, i);
199 if (i == PT_SR)
200 tmp >>= 16;
Roman Zippel69f447c2005-09-03 15:57:08 -0700201 ret = put_user(tmp, (unsigned long *)data);
202 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700204 data += sizeof(long);
205 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700206 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700207
Roman Zippel69f447c2005-09-03 15:57:08 -0700208 case PTRACE_SETREGS: /* Set all gp regs in the child. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700209 for (i = 0; i < 19; i++) {
Roman Zippel69f447c2005-09-03 15:57:08 -0700210 ret = get_user(tmp, (unsigned long *)data);
211 if (ret)
Roman Zippelb3319f52005-09-03 15:57:07 -0700212 break;
Roman Zippelb3319f52005-09-03 15:57:07 -0700213 if (i == PT_SR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 tmp &= SR_MASK;
215 tmp <<= 16;
216 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700218 put_reg(child, i, tmp);
219 data += sizeof(long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Roman Zippelb3319f52005-09-03 15:57:07 -0700221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Roman Zippel69f447c2005-09-03 15:57:08 -0700223 case PTRACE_GETFPREGS: /* Get the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700224 if (copy_to_user((void *)data, &child->thread.fp,
225 sizeof(struct user_m68kfp_struct)))
226 ret = -EFAULT;
227 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Roman Zippel69f447c2005-09-03 15:57:08 -0700229 case PTRACE_SETFPREGS: /* Set the child FPU state. */
Roman Zippelb3319f52005-09-03 15:57:07 -0700230 if (copy_from_user(&child->thread.fp, (void *)data,
231 sizeof(struct user_m68kfp_struct)))
232 ret = -EFAULT;
233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Roman Zippelb3319f52005-09-03 15:57:07 -0700235 default:
236 ret = ptrace_request(child, request, addr, data);
237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
Christoph Hellwig481bed42005-11-07 00:59:47 -0800239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
Roman Zippel69f447c2005-09-03 15:57:08 -0700241out_eio:
Christoph Hellwig481bed42005-11-07 00:59:47 -0800242 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245asmlinkage void syscall_trace(void)
246{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
248 ? 0x80 : 0));
249 /*
250 * this isn't the same as continuing with a signal, but it will do
251 * for normal use. strace only continues with a signal if the
252 * stopping signal is not SIGTRAP. -brl
253 */
254 if (current->exit_code) {
255 send_sig(current->exit_code, current, 1);
256 current->exit_code = 0;
257 }
258}