MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / nios2nommu / kernel / ptrace.c
blobe6ff3b3baaa759af137a9ae38434ebeab6b4997e
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/smp_lock.h>
18 #include <linux/errno.h>
19 #include <linux/ptrace.h>
20 #include <linux/user.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 0x00000000
37 /* Find the stack offset for a register, relative to thread.ksp. */
38 #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
39 #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
40 - sizeof(struct switch_stack))
41 /* Mapping from PT_xxx to the stack offset at which the register is
42 saved. Notice that usp has no stack-slot and needs to be treated
43 specially (see get_reg/put_reg below). */
44 static int regoff[] = {
45 -1, PT_REG(r1), PT_REG(r2), PT_REG(r3), PT_REG(r4),
46 PT_REG(r5), PT_REG(r6), PT_REG(r7), PT_REG(r8),
47 PT_REG(r9), PT_REG(r10), PT_REG(r11), PT_REG(r12),
48 PT_REG(r13), PT_REG(r14), PT_REG(r15), SW_REG(r16),
49 SW_REG(r17), SW_REG(r18), SW_REG(r19), SW_REG(r20),
50 SW_REG(r21), SW_REG(r22), SW_REG(r23), -1, -1,
51 PT_REG(gp), PT_REG(sp), -1, -1, PT_REG(ra), -1,
52 PT_REG(estatus), -1, -1, -1
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 == PTR_R0)
63 return 0;
64 else if (regno == PTR_BA)
65 return 0;
66 else if (regno == PTR_STATUS)
67 return 0;
68 else if (regno == PTR_IENABLE)
69 return 0;
70 else if (regno == PTR_IPENDING)
71 return 0;
72 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
73 addr = (unsigned long *)(task->thread.kregs + regoff[regno]);
74 else
75 return 0;
76 return *addr;
80 * Write contents of register REGNO in task TASK.
82 static inline int put_reg(struct task_struct *task, int regno,
83 unsigned long data)
85 unsigned long *addr;
87 if (regno == PTR_R0)
88 return -1;
89 else if (regno == PTR_BA)
90 return -1;
91 else if (regno == PTR_STATUS)
92 return -1;
93 else if (regno == PTR_IENABLE)
94 return -1;
95 else if (regno == PTR_IPENDING)
96 return -1;
97 else if (regno < sizeof(regoff)/sizeof(regoff[0]))
98 addr = (unsigned long *) (task->thread.kregs + regoff[regno]);
99 else
100 return -1;
101 *addr = data;
102 return 0;
106 * Called by kernel/ptrace.c when detaching..
108 * Nothing special to do here, no processor debug support.
110 void ptrace_disable(struct task_struct *child)
114 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
116 int ret;
118 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. */
121 case PTRACE_PEEKDATA: {
122 unsigned long tmp;
123 int copied;
125 copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
126 ret = -EIO;
127 if (copied != sizeof(tmp))
128 break;
129 ret = put_user(tmp,(unsigned long *) data);
130 break;
133 /* read the word at location addr in the USER area. */
134 case PTRACE_PEEKUSR: {
135 unsigned long tmp;
137 ret = -EIO;
138 if ((addr & 3) || addr < 0 ||
139 addr > sizeof(struct user) - 3)
140 break;
142 tmp = 0; /* Default return condition */
143 addr = addr >> 2; /* temporary hack. */
144 ret = -EIO;
145 if (addr < 19) {
146 tmp = get_reg(child, addr);
147 #if 0 // No FPU stuff
148 } else if (addr >= 21 && addr < 49) {
149 tmp = child->thread.fp[addr - 21];
150 #ifdef CONFIG_M68KFPU_EMU
151 /* Convert internal fpu reg representation
152 * into long double format
154 if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
155 tmp = ((tmp & 0xffff0000) << 15) |
156 ((tmp & 0x0000ffff) << 16);
157 #endif
158 #endif
159 } else if (addr == 49) {
160 tmp = child->mm->start_code;
161 } else if (addr == 50) {
162 tmp = child->mm->start_data;
163 } else if (addr == 51) {
164 tmp = child->mm->end_code;
165 } else
166 break;
167 ret = put_user(tmp,(unsigned long *) data);
168 break;
171 /* when I and D space are separate, this will have to be fixed. */
172 case PTRACE_POKETEXT: /* write the word at location addr. */
173 case PTRACE_POKEDATA:
174 ret = 0;
175 if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
176 break;
177 ret = -EIO;
178 break;
180 case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
181 ret = -EIO;
182 if ((addr & 3) || addr < 0 ||
183 addr > sizeof(struct user) - 3)
184 break;
186 addr = addr >> 2; /* temporary hack. */
188 if (addr == PTR_ESTATUS) {
189 data &= SR_MASK;
190 data |= get_reg(child, PTR_ESTATUS) & ~(SR_MASK);
192 if (addr < 19) {
193 if (put_reg(child, addr, data))
194 break;
195 ret = 0;
196 break;
198 #if 0 // No FPU stuff
199 if (addr >= 21 && addr < 48)
201 #ifdef CONFIG_M68KFPU_EMU
202 /* Convert long double format
203 * into internal fpu reg representation
205 if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
206 data = (unsigned long)data << 15;
207 data = (data & 0xffff0000) |
208 ((data & 0x0000ffff) >> 1);
210 #endif
211 child->thread.fp[addr - 21] = data;
212 ret = 0;
214 #endif
215 break;
217 case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
218 case PTRACE_CONT: { /* restart after signal. */
220 ret = -EIO;
221 if ((unsigned long) data > _NSIG)
222 break;
223 if (request == PTRACE_SYSCALL)
224 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
225 else
226 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
227 child->exit_code = data;
228 wake_up_process(child);
229 ret = 0;
230 break;
234 * make the child exit. Best I can do is send it a sigkill.
235 * perhaps it should be put in the status that it wants to
236 * exit.
238 case PTRACE_KILL: {
240 ret = 0;
241 if (child->state == EXIT_ZOMBIE) /* already dead */
242 break;
243 child->exit_code = SIGKILL;
244 wake_up_process(child);
245 break;
249 * Single stepping requires placing break instructions in
250 * the code to break back. If you are stepping through a
251 * conditional branch you need to decode the test and put
252 * the break in the correct location.
254 case PTRACE_SINGLESTEP: { /* set the trap flag. */
256 ret = -EIO;
257 if ((unsigned long) data > _NSIG)
258 break;
259 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
261 child->exit_code = data;
262 /* give it a chance to run. */
263 wake_up_process(child);
264 ret = 0;
265 break;
268 case PTRACE_DETACH: /* detach a process that was attached. */
269 ret = ptrace_detach(child, data);
270 break;
272 case PTRACE_GETREGS: { /* Get all gp regs from the child. */
273 int i;
274 unsigned long tmp;
275 for (i = 0; i < 19; i++) {
276 tmp = get_reg(child, i);
277 if (put_user(tmp, (unsigned long *) data)) {
278 ret = -EFAULT;
279 break;
281 data += sizeof(long);
283 ret = 0;
284 break;
287 case PTRACE_SETREGS: { /* Set all gp regs in the child. */
288 int i;
289 unsigned long tmp;
290 for (i = 0; i < 19; i++) {
291 if (get_user(tmp, (unsigned long *) data)) {
292 ret = -EFAULT;
293 break;
295 if (i == PTR_ESTATUS) {
296 tmp &= SR_MASK;
297 tmp |= get_reg(child, PTR_ESTATUS) & ~(SR_MASK);
299 put_reg(child, i, tmp);
300 data += sizeof(long);
302 ret = 0;
303 break;
306 #ifdef PTRACE_GETFPREGS
307 case PTRACE_GETFPREGS: { /* Get the child FPU state. */
308 ret = 0;
309 if (copy_to_user((void *)data, &child->thread.fp,
310 sizeof(struct user_m68kfp_struct)))
311 ret = -EFAULT;
312 break;
314 #endif
316 #ifdef PTRACE_SETFPREGS
317 case PTRACE_SETFPREGS: { /* Set the child FPU state. */
318 ret = 0;
319 if (copy_from_user(&child->thread.fp, (void *)data,
320 sizeof(struct user_m68kfp_struct)))
321 ret = -EFAULT;
322 break;
324 #endif
326 default:
327 ret = -EIO;
328 break;
330 return ret;
333 asmlinkage void syscall_trace(void)
335 if (!test_thread_flag(TIF_SYSCALL_TRACE))
336 return;
337 if (!(current->ptrace & PT_PTRACED))
338 return;
339 current->exit_code = SIGTRAP;
340 current->state = TASK_STOPPED;
341 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
342 ? 0x80 : 0));
344 * this isn't the same as continuing with a signal, but it will do
345 * for normal use. strace only continues with a signal if the
346 * stopping signal is not SIGTRAP. -brl
348 if (current->exit_code) {
349 send_sig(current->exit_code, current, 1);
350 current->exit_code = 0;