allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / avr32 / kernel / ptrace.c
blob3c36c2d1614827894a4bfff8d823ce9bf194d1ba
1 /*
2 * Copyright (C) 2004-2006 Atmel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8 #undef DEBUG
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/mm.h>
12 #include <linux/ptrace.h>
13 #include <linux/errno.h>
14 #include <linux/user.h>
15 #include <linux/security.h>
16 #include <linux/unistd.h>
17 #include <linux/notifier.h>
19 #include <asm/traps.h>
20 #include <asm/uaccess.h>
21 #include <asm/ocd.h>
22 #include <asm/mmu_context.h>
23 #include <linux/kdebug.h>
25 static struct pt_regs *get_user_regs(struct task_struct *tsk)
27 return (struct pt_regs *)((unsigned long)task_stack_page(tsk) +
28 THREAD_SIZE - sizeof(struct pt_regs));
31 static void ptrace_single_step(struct task_struct *tsk)
33 pr_debug("ptrace_single_step: pid=%u, SR=0x%08lx\n",
34 tsk->pid, tsk->thread.cpu_context.sr);
35 if (!(tsk->thread.cpu_context.sr & SR_D)) {
37 * Set a breakpoint at the current pc to force the
38 * process into debug mode. The syscall/exception
39 * exit code will set a breakpoint at the return
40 * address when this flag is set.
42 pr_debug("ptrace_single_step: Setting TIF_BREAKPOINT\n");
43 set_tsk_thread_flag(tsk, TIF_BREAKPOINT);
46 /* The monitor code will do the actual step for us */
47 set_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
51 * Called by kernel/ptrace.c when detaching
53 * Make sure any single step bits, etc. are not set
55 void ptrace_disable(struct task_struct *child)
57 clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
61 * Handle hitting a breakpoint
63 static void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
65 siginfo_t info;
67 info.si_signo = SIGTRAP;
68 info.si_errno = 0;
69 info.si_code = TRAP_BRKPT;
70 info.si_addr = (void __user *)instruction_pointer(regs);
72 pr_debug("ptrace_break: Sending SIGTRAP to PID %u (pc = 0x%p)\n",
73 tsk->pid, info.si_addr);
74 force_sig_info(SIGTRAP, &info, tsk);
78 * Read the word at offset "offset" into the task's "struct user". We
79 * actually access the pt_regs struct stored on the kernel stack.
81 static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
82 unsigned long __user *data)
84 unsigned long *regs;
85 unsigned long value;
87 pr_debug("ptrace_read_user(%p, %#lx, %p)\n",
88 tsk, offset, data);
90 if (offset & 3 || offset >= sizeof(struct user)) {
91 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
92 return -EIO;
95 regs = (unsigned long *)get_user_regs(tsk);
97 value = 0;
98 if (offset < sizeof(struct pt_regs))
99 value = regs[offset / sizeof(regs[0])];
101 return put_user(value, data);
105 * Write the word "value" to offset "offset" into the task's "struct
106 * user". We actually access the pt_regs struct stored on the kernel
107 * stack.
109 static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
110 unsigned long value)
112 unsigned long *regs;
114 if (offset & 3 || offset >= sizeof(struct user)) {
115 printk("ptrace_write_user: invalid offset 0x%08lx\n", offset);
116 return -EIO;
119 if (offset >= sizeof(struct pt_regs))
120 return 0;
122 regs = (unsigned long *)get_user_regs(tsk);
123 regs[offset / sizeof(regs[0])] = value;
125 return 0;
128 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
130 struct pt_regs *regs = get_user_regs(tsk);
132 return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
135 static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
137 struct pt_regs newregs;
138 int ret;
140 ret = -EFAULT;
141 if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
142 struct pt_regs *regs = get_user_regs(tsk);
144 ret = -EINVAL;
145 if (valid_user_regs(&newregs)) {
146 *regs = newregs;
147 ret = 0;
151 return ret;
154 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
156 unsigned long tmp;
157 int ret;
159 pr_debug("arch_ptrace(%ld, %d, %#lx, %#lx)\n",
160 request, child->pid, addr, data);
162 pr_debug("ptrace: Enabling monitor mode...\n");
163 __mtdr(DBGREG_DC, __mfdr(DBGREG_DC) | DC_MM | DC_DBE);
165 switch (request) {
166 /* Read the word at location addr in the child process */
167 case PTRACE_PEEKTEXT:
168 case PTRACE_PEEKDATA:
169 ret = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
170 if (ret == sizeof(tmp))
171 ret = put_user(tmp, (unsigned long __user *)data);
172 else
173 ret = -EIO;
174 break;
176 case PTRACE_PEEKUSR:
177 ret = ptrace_read_user(child, addr,
178 (unsigned long __user *)data);
179 break;
181 /* Write the word in data at location addr */
182 case PTRACE_POKETEXT:
183 case PTRACE_POKEDATA:
184 ret = access_process_vm(child, addr, &data, sizeof(data), 1);
185 if (ret == sizeof(data))
186 ret = 0;
187 else
188 ret = -EIO;
189 break;
191 case PTRACE_POKEUSR:
192 ret = ptrace_write_user(child, addr, data);
193 break;
195 /* continue and stop at next (return from) syscall */
196 case PTRACE_SYSCALL:
197 /* restart after signal */
198 case PTRACE_CONT:
199 ret = -EIO;
200 if (!valid_signal(data))
201 break;
202 if (request == PTRACE_SYSCALL)
203 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
204 else
205 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
206 child->exit_code = data;
207 /* XXX: Are we sure no breakpoints are active here? */
208 wake_up_process(child);
209 ret = 0;
210 break;
213 * Make the child exit. Best I can do is send it a
214 * SIGKILL. Perhaps it should be put in the status that it
215 * wants to exit.
217 case PTRACE_KILL:
218 ret = 0;
219 if (child->exit_state == EXIT_ZOMBIE)
220 break;
221 child->exit_code = SIGKILL;
222 wake_up_process(child);
223 break;
226 * execute single instruction.
228 case PTRACE_SINGLESTEP:
229 ret = -EIO;
230 if (!valid_signal(data))
231 break;
232 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
233 ptrace_single_step(child);
234 child->exit_code = data;
235 wake_up_process(child);
236 ret = 0;
237 break;
239 /* Detach a process that was attached */
240 case PTRACE_DETACH:
241 ret = ptrace_detach(child, data);
242 break;
244 case PTRACE_GETREGS:
245 ret = ptrace_getregs(child, (void __user *)data);
246 break;
248 case PTRACE_SETREGS:
249 ret = ptrace_setregs(child, (const void __user *)data);
250 break;
252 default:
253 ret = ptrace_request(child, request, addr, data);
254 break;
257 pr_debug("sys_ptrace returning %d (DC = 0x%08lx)\n", ret, __mfdr(DBGREG_DC));
258 return ret;
261 asmlinkage void syscall_trace(void)
263 pr_debug("syscall_trace called\n");
264 if (!test_thread_flag(TIF_SYSCALL_TRACE))
265 return;
266 if (!(current->ptrace & PT_PTRACED))
267 return;
269 pr_debug("syscall_trace: notifying parent\n");
270 /* The 0x80 provides a way for the tracing parent to
271 * distinguish between a syscall stop and SIGTRAP delivery */
272 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
273 ? 0x80 : 0));
276 * this isn't the same as continuing with a signal, but it
277 * will do for normal use. strace only continues with a
278 * signal if the stopping signal is not SIGTRAP. -brl
280 if (current->exit_code) {
281 pr_debug("syscall_trace: sending signal %d to PID %u\n",
282 current->exit_code, current->pid);
283 send_sig(current->exit_code, current, 1);
284 current->exit_code = 0;
288 asmlinkage void do_debug_priv(struct pt_regs *regs)
290 unsigned long dc, ds;
291 unsigned long die_val;
293 ds = __mfdr(DBGREG_DS);
295 pr_debug("do_debug_priv: pc = %08lx, ds = %08lx\n", regs->pc, ds);
297 if (ds & DS_SSS)
298 die_val = DIE_SSTEP;
299 else
300 die_val = DIE_BREAKPOINT;
302 if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
303 return;
305 if (likely(ds & DS_SSS)) {
306 extern void itlb_miss(void);
307 extern void tlb_miss_common(void);
308 struct thread_info *ti;
310 dc = __mfdr(DBGREG_DC);
311 dc &= ~DC_SS;
312 __mtdr(DBGREG_DC, dc);
314 ti = current_thread_info();
315 set_ti_thread_flag(ti, TIF_BREAKPOINT);
317 /* The TLB miss handlers don't check thread flags */
318 if ((regs->pc >= (unsigned long)&itlb_miss)
319 && (regs->pc <= (unsigned long)&tlb_miss_common)) {
320 __mtdr(DBGREG_BWA2A, sysreg_read(RAR_EX));
321 __mtdr(DBGREG_BWC2A, 0x40000001 | (get_asid() << 1));
325 * If we're running in supervisor mode, the breakpoint
326 * will take us where we want directly, no need to
327 * single step.
329 if ((regs->sr & MODE_MASK) != MODE_SUPERVISOR)
330 set_ti_thread_flag(ti, TIF_SINGLE_STEP);
331 } else {
332 panic("Unable to handle debug trap at pc = %08lx\n",
333 regs->pc);
338 * Handle breakpoints, single steps and other debuggy things. To keep
339 * things simple initially, we run with interrupts and exceptions
340 * disabled all the time.
342 asmlinkage void do_debug(struct pt_regs *regs)
344 unsigned long dc, ds;
346 ds = __mfdr(DBGREG_DS);
347 pr_debug("do_debug: pc = %08lx, ds = %08lx\n", regs->pc, ds);
349 if (test_thread_flag(TIF_BREAKPOINT)) {
350 pr_debug("TIF_BREAKPOINT set\n");
351 /* We're taking care of it */
352 clear_thread_flag(TIF_BREAKPOINT);
353 __mtdr(DBGREG_BWC2A, 0);
356 if (test_thread_flag(TIF_SINGLE_STEP)) {
357 pr_debug("TIF_SINGLE_STEP set, ds = 0x%08lx\n", ds);
358 if (ds & DS_SSS) {
359 dc = __mfdr(DBGREG_DC);
360 dc &= ~DC_SS;
361 __mtdr(DBGREG_DC, dc);
363 clear_thread_flag(TIF_SINGLE_STEP);
364 ptrace_break(current, regs);
366 } else {
367 /* regular breakpoint */
368 ptrace_break(current, regs);