PTRACE_PEEKDATA consolidation
[linux-2.6/openmoko-kernel.git] / arch / m68knommu / kernel / ptrace.c
blobf550e614aa787d352ae7ec80db5dc3bb8503fdba
1 /*
2 * linux/arch/m68knommu/kernel/ptrace.c
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
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.
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>
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>
29 * does not yet catch signals sent when the child dies.
30 * in exit.c or in signal.c.
33 /* determines which bits in the SR the user has access to. */
34 /* 1 = access 0 = no access */
35 #define SR_MASK 0x001f
37 /* sets the trace bits. */
38 #define TRACE_BITS 0x8000
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). */
47 static 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),
56 * Get contents of register REGNO in task TASK.
58 static inline long get_reg(struct task_struct *task, int regno)
60 unsigned long *addr;
62 if (regno == PT_USP)
63 addr = &task->thread.usp;
64 else if (regno < ARRAY_SIZE(regoff))
65 addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
66 else
67 return 0;
68 return *addr;
72 * Write contents of register REGNO in task TASK.
74 static inline int put_reg(struct task_struct *task, int regno,
75 unsigned long data)
77 unsigned long *addr;
79 if (regno == PT_USP)
80 addr = &task->thread.usp;
81 else if (regno < ARRAY_SIZE(regoff))
82 addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
83 else
84 return -1;
85 *addr = data;
86 return 0;
90 * Called by kernel/ptrace.c when detaching..
92 * Make sure the single step bit is not set.
94 void ptrace_disable(struct task_struct *child)
96 unsigned long tmp;
97 /* make sure the single step bit is not set. */
98 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
99 put_reg(child, PT_SR, tmp);
102 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
104 int ret;
106 switch (request) {
107 /* when I and D space are separate, these will need to be fixed. */
108 case PTRACE_PEEKTEXT: /* read word at location addr. */
109 case PTRACE_PEEKDATA:
110 ret = generic_ptrace_peekdata(child, addr, data);
111 break;
113 /* read the word at location addr in the USER area. */
114 case PTRACE_PEEKUSR: {
115 unsigned long tmp;
117 ret = -EIO;
118 if ((addr & 3) || addr < 0 ||
119 addr > sizeof(struct user) - 3)
120 break;
122 tmp = 0; /* Default return condition */
123 addr = addr >> 2; /* temporary hack. */
124 ret = -EIO;
125 if (addr < 19) {
126 tmp = get_reg(child, addr);
127 if (addr == PT_SR)
128 tmp >>= 16;
129 } else if (addr >= 21 && addr < 49) {
130 tmp = child->thread.fp[addr - 21];
131 #ifdef CONFIG_M68KFPU_EMU
132 /* Convert internal fpu reg representation
133 * into long double format
135 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
136 tmp = ((tmp & 0xffff0000) << 15) |
137 ((tmp & 0x0000ffff) << 16);
138 #endif
139 } else if (addr == 49) {
140 tmp = child->mm->start_code;
141 } else if (addr == 50) {
142 tmp = child->mm->start_data;
143 } else if (addr == 51) {
144 tmp = child->mm->end_code;
145 } else
146 break;
147 ret = put_user(tmp,(unsigned long *) data);
148 break;
151 /* when I and D space are separate, this will have to be fixed. */
152 case PTRACE_POKETEXT: /* write the word at location addr. */
153 case PTRACE_POKEDATA:
154 ret = 0;
155 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
156 break;
157 ret = -EIO;
158 break;
160 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
161 ret = -EIO;
162 if ((addr & 3) || addr < 0 ||
163 addr > sizeof(struct user) - 3)
164 break;
166 addr = addr >> 2; /* temporary hack. */
168 if (addr == PT_SR) {
169 data &= SR_MASK;
170 data <<= 16;
171 data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
173 if (addr < 19) {
174 if (put_reg(child, addr, data))
175 break;
176 ret = 0;
177 break;
179 if (addr >= 21 && addr < 48)
181 #ifdef CONFIG_M68KFPU_EMU
182 /* Convert long double format
183 * into internal fpu reg representation
185 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
186 data = (unsigned long)data << 15;
187 data = (data & 0xffff0000) |
188 ((data & 0x0000ffff) >> 1);
190 #endif
191 child->thread.fp[addr - 21] = data;
192 ret = 0;
194 break;
196 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
197 case PTRACE_CONT: { /* restart after signal. */
198 long tmp;
200 ret = -EIO;
201 if (!valid_signal(data))
202 break;
203 if (request == PTRACE_SYSCALL)
204 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
205 else
206 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
207 child->exit_code = data;
208 /* make sure the single step bit is not set. */
209 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
210 put_reg(child, PT_SR, tmp);
211 wake_up_process(child);
212 ret = 0;
213 break;
217 * make the child exit. Best I can do is send it a sigkill.
218 * perhaps it should be put in the status that it wants to
219 * exit.
221 case PTRACE_KILL: {
222 long tmp;
224 ret = 0;
225 if (child->exit_state == EXIT_ZOMBIE) /* already dead */
226 break;
227 child->exit_code = SIGKILL;
228 /* make sure the single step bit is not set. */
229 tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
230 put_reg(child, PT_SR, tmp);
231 wake_up_process(child);
232 break;
235 case PTRACE_SINGLESTEP: { /* set the trap flag. */
236 long tmp;
238 ret = -EIO;
239 if (!valid_signal(data))
240 break;
241 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
242 tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
243 put_reg(child, PT_SR, tmp);
245 child->exit_code = data;
246 /* give it a chance to run. */
247 wake_up_process(child);
248 ret = 0;
249 break;
252 case PTRACE_DETACH: /* detach a process that was attached. */
253 ret = ptrace_detach(child, data);
254 break;
256 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
257 int i;
258 unsigned long tmp;
259 for (i = 0; i < 19; i++) {
260 tmp = get_reg(child, i);
261 if (i == PT_SR)
262 tmp >>= 16;
263 if (put_user(tmp, (unsigned long *) data)) {
264 ret = -EFAULT;
265 break;
267 data += sizeof(long);
269 ret = 0;
270 break;
273 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
274 int i;
275 unsigned long tmp;
276 for (i = 0; i < 19; i++) {
277 if (get_user(tmp, (unsigned long *) data)) {
278 ret = -EFAULT;
279 break;
281 if (i == PT_SR) {
282 tmp &= SR_MASK;
283 tmp <<= 16;
284 tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
286 put_reg(child, i, tmp);
287 data += sizeof(long);
289 ret = 0;
290 break;
293 #ifdef PTRACE_GETFPREGS
294 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
295 ret = 0;
296 if (copy_to_user((void *)data, &child->thread.fp,
297 sizeof(struct user_m68kfp_struct)))
298 ret = -EFAULT;
299 break;
301 #endif
303 #ifdef PTRACE_SETFPREGS
304 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
305 ret = 0;
306 if (copy_from_user(&child->thread.fp, (void *)data,
307 sizeof(struct user_m68kfp_struct)))
308 ret = -EFAULT;
309 break;
311 #endif
313 default:
314 ret = -EIO;
315 break;
317 return ret;
320 asmlinkage void syscall_trace(void)
322 if (!test_thread_flag(TIF_SYSCALL_TRACE))
323 return;
324 if (!(current->ptrace & PT_PTRACED))
325 return;
326 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
327 ? 0x80 : 0));
329 * this isn't the same as continuing with a signal, but it will do
330 * for normal use. strace only continues with a signal if the
331 * stopping signal is not SIGTRAP. -brl
333 if (current->exit_code) {
334 send_sig(current->exit_code, current, 1);
335 current->exit_code = 0;