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.
9 #include <linux/kernel.h>
10 #include <linux/sched.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>
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
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
);
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
)
74 if (offset
& 3 || offset
>= sizeof(struct user
)) {
75 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset
);
79 regs
= (unsigned long *)get_user_regs(tsk
);
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
96 static int ptrace_write_user(struct task_struct
*tsk
, unsigned long offset
,
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
);
109 if (offset
>= sizeof(struct pt_regs
))
112 regs
= (unsigned long *)get_user_regs(tsk
);
113 regs
[offset
/ sizeof(regs
[0])] = value
;
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
;
131 if (copy_from_user(&newregs
, uregs
, sizeof(newregs
)) == 0) {
132 struct pt_regs
*regs
= get_user_regs(tsk
);
135 if (valid_user_regs(&newregs
)) {
144 long arch_ptrace(struct task_struct
*child
, long request
, long addr
, long data
)
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
);
156 ret
= ptrace_read_user(child
, addr
,
157 (unsigned long __user
*)data
);
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
);
167 ret
= ptrace_write_user(child
, addr
, data
);
170 /* continue and stop at next (return from) syscall */
172 /* restart after signal */
175 if (!valid_signal(data
))
177 if (request
== PTRACE_SYSCALL
)
178 set_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
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
);
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
194 if (child
->exit_state
== EXIT_ZOMBIE
)
196 child
->exit_code
= SIGKILL
;
197 wake_up_process(child
);
201 * execute single instruction.
203 case PTRACE_SINGLESTEP
:
205 if (!valid_signal(data
))
207 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
208 ptrace_single_step(child
);
209 child
->exit_code
= data
;
210 wake_up_process(child
);
215 ret
= ptrace_getregs(child
, (void __user
*)data
);
219 ret
= ptrace_setregs(child
, (const void __user
*)data
);
223 ret
= ptrace_request(child
, request
, addr
, data
);
230 asmlinkage
void syscall_trace(void)
232 if (!test_thread_flag(TIF_SYSCALL_TRACE
))
234 if (!(current
->ptrace
& PT_PTRACED
))
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
)
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
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
;
280 status
= ocd_read(DS
);
281 ti
= current_thread_info();
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
))
293 if (notify_die(die_val
, "ptrace", regs
, 0, 0, SIGTRAP
)
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.
308 pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:"
309 "PC=0x%08lx SR=0x%08lx\n",
311 BUG_ON(!user_mode(regs
));
313 if (test_thread_flag(TIF_SINGLE_STEP
)) {
314 pr_debug("Going to do single step...\n");
319 * No TIF_SINGLE_STEP means we're done
320 * stepping over a syscall. Do the trap now.
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
) {
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
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
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
);
367 ctrl
&= ~(1 << OCD_DC_SS_BIT
);
372 printk(KERN_ERR
"Unexpected OCD_DS value: 0x%08x\n",
374 printk(KERN_ERR
"Thread flags: 0x%08lx\n", ti
->flags
);
375 die("Unhandled debug trap in kernel mode",
378 } else if (status
& (1 << OCD_DS_SSS_BIT
)) {
379 /* Single step in user mode */
383 ctrl
&= ~(1 << OCD_DC_SS_BIT
);
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
));