of: make of_find_device_by_node generic
[linux-2.6/btrfs-unstable.git] / arch / avr32 / kernel / ptrace.c
blob5e73c25f8f85b2165652b30555c07813accfb0d1
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 void user_enable_single_step(struct task_struct *tsk)
33 pr_debug("user_enable_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);
52 void user_disable_single_step(struct task_struct *child)
54 /* XXX(hch): a no-op here seems wrong.. */
58 * Called by kernel/ptrace.c when detaching
60 * Make sure any single step bits, etc. are not set
62 void ptrace_disable(struct task_struct *child)
64 clear_tsk_thread_flag(child, TIF_SINGLE_STEP);
65 clear_tsk_thread_flag(child, TIF_BREAKPOINT);
66 ocd_disable(child);
70 * Read the word at offset "offset" into the task's "struct user". We
71 * actually access the pt_regs struct stored on the kernel stack.
73 static int ptrace_read_user(struct task_struct *tsk, unsigned long offset,
74 unsigned long __user *data)
76 unsigned long *regs;
77 unsigned long value;
79 if (offset & 3 || offset >= sizeof(struct user)) {
80 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset);
81 return -EIO;
84 regs = (unsigned long *)get_user_regs(tsk);
86 value = 0;
87 if (offset < sizeof(struct pt_regs))
88 value = regs[offset / sizeof(regs[0])];
90 pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n",
91 tsk->comm, tsk->pid, offset, data, value);
93 return put_user(value, data);
97 * Write the word "value" to offset "offset" into the task's "struct
98 * user". We actually access the pt_regs struct stored on the kernel
99 * stack.
101 static int ptrace_write_user(struct task_struct *tsk, unsigned long offset,
102 unsigned long value)
104 unsigned long *regs;
106 pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n",
107 tsk->comm, tsk->pid, offset, value);
109 if (offset & 3 || offset >= sizeof(struct user)) {
110 pr_debug(" invalid offset 0x%08lx\n", offset);
111 return -EIO;
114 if (offset >= sizeof(struct pt_regs))
115 return 0;
117 regs = (unsigned long *)get_user_regs(tsk);
118 regs[offset / sizeof(regs[0])] = value;
120 return 0;
123 static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
125 struct pt_regs *regs = get_user_regs(tsk);
127 return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0;
130 static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs)
132 struct pt_regs newregs;
133 int ret;
135 ret = -EFAULT;
136 if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) {
137 struct pt_regs *regs = get_user_regs(tsk);
139 ret = -EINVAL;
140 if (valid_user_regs(&newregs)) {
141 *regs = newregs;
142 ret = 0;
146 return ret;
149 long arch_ptrace(struct task_struct *child, long request, long addr, long data)
151 int ret;
153 switch (request) {
154 /* Read the word at location addr in the child process */
155 case PTRACE_PEEKTEXT:
156 case PTRACE_PEEKDATA:
157 ret = generic_ptrace_peekdata(child, addr, data);
158 break;
160 case PTRACE_PEEKUSR:
161 ret = ptrace_read_user(child, addr,
162 (unsigned long __user *)data);
163 break;
165 /* Write the word in data at location addr */
166 case PTRACE_POKETEXT:
167 case PTRACE_POKEDATA:
168 ret = generic_ptrace_pokedata(child, addr, data);
169 break;
171 case PTRACE_POKEUSR:
172 ret = ptrace_write_user(child, addr, data);
173 break;
175 case PTRACE_GETREGS:
176 ret = ptrace_getregs(child, (void __user *)data);
177 break;
179 case PTRACE_SETREGS:
180 ret = ptrace_setregs(child, (const void __user *)data);
181 break;
183 default:
184 ret = ptrace_request(child, request, addr, data);
185 break;
188 return ret;
191 asmlinkage void syscall_trace(void)
193 if (!test_thread_flag(TIF_SYSCALL_TRACE))
194 return;
195 if (!(current->ptrace & PT_PTRACED))
196 return;
198 /* The 0x80 provides a way for the tracing parent to
199 * distinguish between a syscall stop and SIGTRAP delivery */
200 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
201 ? 0x80 : 0));
204 * this isn't the same as continuing with a signal, but it
205 * will do for normal use. strace only continues with a
206 * signal if the stopping signal is not SIGTRAP. -brl
208 if (current->exit_code) {
209 pr_debug("syscall_trace: sending signal %d to PID %u\n",
210 current->exit_code, current->pid);
211 send_sig(current->exit_code, current, 1);
212 current->exit_code = 0;
217 * debug_trampoline() is an assembly stub which will store all user
218 * registers on the stack and execute a breakpoint instruction.
220 * If we single-step into an exception handler which runs with
221 * interrupts disabled the whole time so it doesn't have to check for
222 * pending work, its return address will be modified so that it ends
223 * up returning to debug_trampoline.
225 * If the exception handler decides to store the user context and
226 * enable interrupts after all, it will restore the original return
227 * address and status register value. Before it returns, it will
228 * notice that TIF_BREAKPOINT is set and execute a breakpoint
229 * instruction.
231 extern void debug_trampoline(void);
233 asmlinkage struct pt_regs *do_debug(struct pt_regs *regs)
235 struct thread_info *ti;
236 unsigned long trampoline_addr;
237 u32 status;
238 u32 ctrl;
239 int code;
241 status = ocd_read(DS);
242 ti = current_thread_info();
243 code = TRAP_BRKPT;
245 pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n",
246 status, regs->pc, regs->sr, ti->flags);
248 if (!user_mode(regs)) {
249 unsigned long die_val = DIE_BREAKPOINT;
251 if (status & (1 << OCD_DS_SSS_BIT))
252 die_val = DIE_SSTEP;
254 if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP)
255 == NOTIFY_STOP)
256 return regs;
258 if ((status & (1 << OCD_DS_SWB_BIT))
259 && test_and_clear_ti_thread_flag(
260 ti, TIF_BREAKPOINT)) {
262 * Explicit breakpoint from trampoline or
263 * exception/syscall/interrupt handler.
265 * The real saved regs are on the stack right
266 * after the ones we saved on entry.
268 regs++;
269 pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:"
270 "PC=0x%08lx SR=0x%08lx\n",
271 regs->pc, regs->sr);
272 BUG_ON(!user_mode(regs));
274 if (test_thread_flag(TIF_SINGLE_STEP)) {
275 pr_debug("Going to do single step...\n");
276 return regs;
280 * No TIF_SINGLE_STEP means we're done
281 * stepping over a syscall. Do the trap now.
283 code = TRAP_TRACE;
284 } else if ((status & (1 << OCD_DS_SSS_BIT))
285 && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) {
287 pr_debug("Stepped into something, "
288 "setting TIF_BREAKPOINT...\n");
289 set_ti_thread_flag(ti, TIF_BREAKPOINT);
292 * We stepped into an exception, interrupt or
293 * syscall handler. Some exception handlers
294 * don't check for pending work, so we need to
295 * set up a trampoline just in case.
297 * The exception entry code will undo the
298 * trampoline stuff if it does a full context
299 * save (which also means that it'll check for
300 * pending work later.)
302 if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) {
303 trampoline_addr
304 = (unsigned long)&debug_trampoline;
306 pr_debug("Setting up trampoline...\n");
307 ti->rar_saved = sysreg_read(RAR_EX);
308 ti->rsr_saved = sysreg_read(RSR_EX);
309 sysreg_write(RAR_EX, trampoline_addr);
310 sysreg_write(RSR_EX, (MODE_EXCEPTION
311 | SR_EM | SR_GM));
312 BUG_ON(ti->rsr_saved & MODE_MASK);
316 * If we stepped into a system call, we
317 * shouldn't do a single step after we return
318 * since the return address is right after the
319 * "scall" instruction we were told to step
320 * over.
322 if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) {
323 pr_debug("Supervisor; no single step\n");
324 clear_ti_thread_flag(ti, TIF_SINGLE_STEP);
327 ctrl = ocd_read(DC);
328 ctrl &= ~(1 << OCD_DC_SS_BIT);
329 ocd_write(DC, ctrl);
331 return regs;
332 } else {
333 printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n",
334 status);
335 printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags);
336 die("Unhandled debug trap in kernel mode",
337 regs, SIGTRAP);
339 } else if (status & (1 << OCD_DS_SSS_BIT)) {
340 /* Single step in user mode */
341 code = TRAP_TRACE;
343 ctrl = ocd_read(DC);
344 ctrl &= ~(1 << OCD_DC_SS_BIT);
345 ocd_write(DC, ctrl);
348 pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n",
349 code, regs->pc, regs->sr);
351 clear_thread_flag(TIF_SINGLE_STEP);
352 _exception(SIGTRAP, regs, code, instruction_pointer(regs));
354 return regs;