OMAP: Add new function to check wether there is irq pending
[linux-ginger.git] / arch / avr32 / kernel / ptrace.c
blob1fed38fcf594cdfd043707a1d31b958ce8c4f8b0
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, PC=0x%08lx, SR=0x%08lx\n",
34 tsk->pid, task_pt_regs(tsk)->pc, task_pt_regs(tsk)->sr);
37 * We can't schedule in Debug mode, so when TIF_BREAKPOINT is
38 * set, the system call or exception handler will do a
39 * breakpoint to enter monitor mode before returning to
40 * userspace.
42 * The monitor code will then notice that TIF_SINGLE_STEP is
43 * set and return to userspace with single stepping enabled.
44 * The CPU will then enter monitor mode again after exactly
45 * one instruction has been executed, and the monitor code
46 * will then send a SIGTRAP to the process.
48 set_tsk_thread_flag(tsk, TIF_BREAKPOINT);
49 set_tsk_thread_flag(tsk, TIF_SINGLE_STEP);
53 * Called by kernel/ptrace.c when detaching
55 * Make sure any single step bits, etc. are not set
57 void ptrace_disable(struct task_struct *child)
59 clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
60 clear_tsk_thread_flag(child, TIF_BREAKPOINT);
61 ocd_disable(child);
65 * Read the word at offset "offset" into the task's "struct user". We
66 * actually access the pt_regs struct stored on the kernel stack.
68 static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
69 unsigned long __user *data)
71 unsigned long *regs;
72 unsigned long value;
74 if (offset & 3 || offset >= sizeof(struct user)) {
75 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
76 return -EIO;
79 regs = (unsigned long *)get_user_regs(tsk);
81 value = 0;
82 if (offset < sizeof(struct pt_regs))
83 value = regs[offset / sizeof(regs[0])];
85 pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n",
86 tsk->comm, tsk->pid, offset, data, value);
88 return put_user(value, data);
92 * Write the word "value" to offset "offset" into the task's "struct
93 * user". We actually access the pt_regs struct stored on the kernel
94 * stack.
96 static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
97 unsigned long value)
99 unsigned long *regs;
101 pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n",
102 tsk->comm, tsk->pid, offset, value);
104 if (offset & 3 || offset >= sizeof(struct user)) {
105 pr_debug(" invalid offset 0x%08lx\n", offset);
106 return -EIO;
109 if (offset >= sizeof(struct pt_regs))
110 return 0;
112 regs = (unsigned long *)get_user_regs(tsk);
113 regs[offset / sizeof(regs[0])] = value;
115 return 0;
118 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
120 struct pt_regs *regs = get_user_regs(tsk);
122 return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
125 static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
127 struct pt_regs newregs;
128 int ret;
130 ret = -EFAULT;
131 if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
132 struct pt_regs *regs = get_user_regs(tsk);
134 ret = -EINVAL;
135 if (valid_user_regs(&newregs)) {
136 *regs = newregs;
137 ret = 0;
141 return ret;
144 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
146 int ret;
148 switch (request) {
149 /* Read the word at location addr in the child process */
150 case PTRACE_PEEKTEXT:
151 case PTRACE_PEEKDATA:
152 ret = generic_ptrace_peekdata(child, addr, data);
153 break;
155 case PTRACE_PEEKUSR:
156 ret = ptrace_read_user(child, addr,
157 (unsigned long __user *)data);
158 break;
160 /* Write the word in data at location addr */
161 case PTRACE_POKETEXT:
162 case PTRACE_POKEDATA:
163 ret = generic_ptrace_pokedata(child, addr, data);
164 break;
166 case PTRACE_POKEUSR:
167 ret = ptrace_write_user(child, addr, data);
168 break;
170 /* continue and stop at next (return from) syscall */
171 case PTRACE_SYSCALL:
172 /* restart after signal */
173 case PTRACE_CONT:
174 ret = -EIO;
175 if (!valid_signal(data))
176 break;
177 if (request == PTRACE_SYSCALL)
178 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
179 else
180 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
181 child->exit_code = data;
182 /* XXX: Are we sure no breakpoints are active here? */
183 wake_up_process(child);
184 ret = 0;
185 break;
188 * Make the child exit. Best I can do is send it a
189 * SIGKILL. Perhaps it should be put in the status that it
190 * wants to exit.
192 case PTRACE_KILL:
193 ret = 0;
194 if (child->exit_state == EXIT_ZOMBIE)
195 break;
196 child->exit_code = SIGKILL;
197 wake_up_process(child);
198 break;
201 * execute single instruction.
203 case PTRACE_SINGLESTEP:
204 ret = -EIO;
205 if (!valid_signal(data))
206 break;
207 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
208 ptrace_single_step(child);
209 child->exit_code = data;
210 wake_up_process(child);
211 ret = 0;
212 break;
214 case PTRACE_GETREGS:
215 ret = ptrace_getregs(child, (void __user *)data);
216 break;
218 case PTRACE_SETREGS:
219 ret = ptrace_setregs(child, (const void __user *)data);
220 break;
222 default:
223 ret = ptrace_request(child, request, addr, data);
224 break;
227 return ret;
230 asmlinkage void syscall_trace(void)
232 if (!test_thread_flag(TIF_SYSCALL_TRACE))
233 return;
234 if (!(current->ptrace & PT_PTRACED))
235 return;
237 /* The 0x80 provides a way for the tracing parent to
238 * distinguish between a syscall stop and SIGTRAP delivery */
239 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
240 ? 0x80 : 0));
243 * this isn't the same as continuing with a signal, but it
244 * will do for normal use. strace only continues with a
245 * signal if the stopping signal is not SIGTRAP. -brl
247 if (current->exit_code) {
248 pr_debug("syscall_trace: sending signal %d to PID %u\n",
249 current->exit_code, current->pid);
250 send_sig(current->exit_code, current, 1);
251 current->exit_code = 0;
256 * debug_trampoline() is an assembly stub which will store all user
257 * registers on the stack and execute a breakpoint instruction.
259 * If we single-step into an exception handler which runs with
260 * interrupts disabled the whole time so it doesn't have to check for
261 * pending work, its return address will be modified so that it ends
262 * up returning to debug_trampoline.
264 * If the exception handler decides to store the user context and
265 * enable interrupts after all, it will restore the original return
266 * address and status register value. Before it returns, it will
267 * notice that TIF_BREAKPOINT is set and execute a breakpoint
268 * instruction.
270 extern void debug_trampoline(void);
272 asmlinkage struct pt_regs *do_debug(struct pt_regs *regs)
274 struct thread_info *ti;
275 unsigned long trampoline_addr;
276 u32 status;
277 u32 ctrl;
278 int code;
280 status = ocd_read(DS);
281 ti = current_thread_info();
282 code = TRAP_BRKPT;
284 pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n",
285 status, regs->pc, regs->sr, ti->flags);
287 if (!user_mode(regs)) {
288 unsigned long die_val = DIE_BREAKPOINT;
290 if (status & (1 << OCD_DS_SSS_BIT))
291 die_val = DIE_SSTEP;
293 if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP)
294 == NOTIFY_STOP)
295 return regs;
297 if ((status & (1 << OCD_DS_SWB_BIT))
298 && test_and_clear_ti_thread_flag(
299 ti, TIF_BREAKPOINT)) {
301 * Explicit breakpoint from trampoline or
302 * exception/syscall/interrupt handler.
304 * The real saved regs are on the stack right
305 * after the ones we saved on entry.
307 regs++;
308 pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:"
309 "PC=0x%08lx SR=0x%08lx\n",
310 regs->pc, regs->sr);
311 BUG_ON(!user_mode(regs));
313 if (test_thread_flag(TIF_SINGLE_STEP)) {
314 pr_debug("Going to do single step...\n");
315 return regs;
319 * No TIF_SINGLE_STEP means we're done
320 * stepping over a syscall. Do the trap now.
322 code = TRAP_TRACE;
323 } else if ((status & (1 << OCD_DS_SSS_BIT))
324 && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) {
326 pr_debug("Stepped into something, "
327 "setting TIF_BREAKPOINT...\n");
328 set_ti_thread_flag(ti, TIF_BREAKPOINT);
331 * We stepped into an exception, interrupt or
332 * syscall handler. Some exception handlers
333 * don't check for pending work, so we need to
334 * set up a trampoline just in case.
336 * The exception entry code will undo the
337 * trampoline stuff if it does a full context
338 * save (which also means that it'll check for
339 * pending work later.)
341 if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) {
342 trampoline_addr
343 = (unsigned long)&debug_trampoline;
345 pr_debug("Setting up trampoline...\n");
346 ti->rar_saved = sysreg_read(RAR_EX);
347 ti->rsr_saved = sysreg_read(RSR_EX);
348 sysreg_write(RAR_EX, trampoline_addr);
349 sysreg_write(RSR_EX, (MODE_EXCEPTION
350 | SR_EM | SR_GM));
351 BUG_ON(ti->rsr_saved & MODE_MASK);
355 * If we stepped into a system call, we
356 * shouldn't do a single step after we return
357 * since the return address is right after the
358 * "scall" instruction we were told to step
359 * over.
361 if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) {
362 pr_debug("Supervisor; no single step\n");
363 clear_ti_thread_flag(ti, TIF_SINGLE_STEP);
366 ctrl = ocd_read(DC);
367 ctrl &= ~(1 << OCD_DC_SS_BIT);
368 ocd_write(DC, ctrl);
370 return regs;
371 } else {
372 printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n",
373 status);
374 printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags);
375 die("Unhandled debug trap in kernel mode",
376 regs, SIGTRAP);
378 } else if (status & (1 << OCD_DS_SSS_BIT)) {
379 /* Single step in user mode */
380 code = TRAP_TRACE;
382 ctrl = ocd_read(DC);
383 ctrl &= ~(1 << OCD_DC_SS_BIT);
384 ocd_write(DC, ctrl);
387 pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n",
388 code, regs->pc, regs->sr);
390 clear_thread_flag(TIF_SINGLE_STEP);
391 _exception(SIGTRAP, regs, code, instruction_pointer(regs));
393 return regs;