1/* 2 * Copyright (C) 2004-2006 Atmel Corporation 3 * 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> 18 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> 24 25static struct pt_regs *get_user_regs(struct task_struct *tsk) 26{ 27 return (struct pt_regs *)((unsigned long)task_stack_page(tsk) + 28 THREAD_SIZE - sizeof(struct pt_regs)); 29} 30 31void user_enable_single_step(struct task_struct *tsk) 32{ 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); 35 36 /* 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. 41 * 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. 47 */ 48 set_tsk_thread_flag(tsk, TIF_BREAKPOINT); 49 set_tsk_thread_flag(tsk, TIF_SINGLE_STEP); 50} 51 52void user_disable_single_step(struct task_struct *child) 53{ 54} 55 56/* 57 * Called by kernel/ptrace.c when detaching 58 * 59 * Make sure any single step bits, etc. are not set 60 */ 61void ptrace_disable(struct task_struct *child) 62{ 63 clear_tsk_thread_flag(child, TIF_SINGLE_STEP); 64 clear_tsk_thread_flag(child, TIF_BREAKPOINT); 65 ocd_disable(child); 66} 67 68/* 69 * Read the word at offset "offset" into the task's "struct user". We 70 * actually access the pt_regs struct stored on the kernel stack. 71 */ 72static int ptrace_read_user(struct task_struct *tsk, unsigned long offset, 73 unsigned long __user *data) 74{ 75 unsigned long *regs; 76 unsigned long value; 77 78 if (offset & 3 || offset >= sizeof(struct user)) { 79 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset); 80 return -EIO; 81 } 82 83 regs = (unsigned long *)get_user_regs(tsk); 84 85 value = 0; 86 if (offset < sizeof(struct pt_regs)) 87 value = regs[offset / sizeof(regs[0])]; 88 89 pr_debug("ptrace_read_user(%s[%u], %#lx, %p) -> %#lx\n", 90 tsk->comm, tsk->pid, offset, data, value); 91 92 return put_user(value, data); 93} 94 95/* 96 * Write the word "value" to offset "offset" into the task's "struct 97 * user". We actually access the pt_regs struct stored on the kernel 98 * stack. 99 */ 100static int ptrace_write_user(struct task_struct *tsk, unsigned long offset, 101 unsigned long value) 102{ 103 unsigned long *regs; 104 105 pr_debug("ptrace_write_user(%s[%u], %#lx, %#lx)\n", 106 tsk->comm, tsk->pid, offset, value); 107 108 if (offset & 3 || offset >= sizeof(struct user)) { 109 pr_debug(" invalid offset 0x%08lx\n", offset); 110 return -EIO; 111 } 112 113 if (offset >= sizeof(struct pt_regs)) 114 return 0; 115 116 regs = (unsigned long *)get_user_regs(tsk); 117 regs[offset / sizeof(regs[0])] = value; 118 119 return 0; 120} 121 122static int ptrace_getregs(struct task_struct *tsk, void __user *uregs) 123{ 124 struct pt_regs *regs = get_user_regs(tsk); 125 126 return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0; 127} 128 129static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs) 130{ 131 struct pt_regs newregs; 132 int ret; 133 134 ret = -EFAULT; 135 if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) { 136 struct pt_regs *regs = get_user_regs(tsk); 137 138 ret = -EINVAL; 139 if (valid_user_regs(&newregs)) { 140 *regs = newregs; 141 ret = 0; 142 } 143 } 144 145 return ret; 146} 147 148long arch_ptrace(struct task_struct *child, long request, long addr, long data) 149{ 150 int ret; 151 152 switch (request) { 153 /* Read the word at location addr in the child process */ 154 case PTRACE_PEEKTEXT: 155 case PTRACE_PEEKDATA: 156 ret = generic_ptrace_peekdata(child, addr, data); 157 break; 158 159 case PTRACE_PEEKUSR: 160 ret = ptrace_read_user(child, addr, 161 (unsigned long __user *)data); 162 break; 163 164 /* Write the word in data at location addr */ 165 case PTRACE_POKETEXT: 166 case PTRACE_POKEDATA: 167 ret = generic_ptrace_pokedata(child, addr, data); 168 break; 169 170 case PTRACE_POKEUSR: 171 ret = ptrace_write_user(child, addr, data); 172 break; 173 174 case PTRACE_GETREGS: 175 ret = ptrace_getregs(child, (void __user *)data); 176 break; 177 178 case PTRACE_SETREGS: 179 ret = ptrace_setregs(child, (const void __user *)data); 180 break; 181 182 default: 183 ret = ptrace_request(child, request, addr, data); 184 break; 185 } 186 187 return ret; 188} 189 190asmlinkage void syscall_trace(void) 191{ 192 if (!test_thread_flag(TIF_SYSCALL_TRACE)) 193 return; 194 if (!(current->ptrace & PT_PTRACED)) 195 return; 196 197 /* The 0x80 provides a way for the tracing parent to 198 * distinguish between a syscall stop and SIGTRAP delivery */ 199 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) 200 ? 0x80 : 0)); 201 202 /* 203 * this isn't the same as continuing with a signal, but it 204 * will do for normal use. strace only continues with a 205 * signal if the stopping signal is not SIGTRAP. -brl 206 */ 207 if (current->exit_code) { 208 pr_debug("syscall_trace: sending signal %d to PID %u\n", 209 current->exit_code, current->pid); 210 send_sig(current->exit_code, current, 1); 211 current->exit_code = 0; 212 } 213} 214 215/* 216 * debug_trampoline() is an assembly stub which will store all user 217 * registers on the stack and execute a breakpoint instruction. 218 * 219 * If we single-step into an exception handler which runs with 220 * interrupts disabled the whole time so it doesn't have to check for 221 * pending work, its return address will be modified so that it ends 222 * up returning to debug_trampoline. 223 * 224 * If the exception handler decides to store the user context and 225 * enable interrupts after all, it will restore the original return 226 * address and status register value. Before it returns, it will 227 * notice that TIF_BREAKPOINT is set and execute a breakpoint 228 * instruction. 229 */ 230extern void debug_trampoline(void); 231 232asmlinkage struct pt_regs *do_debug(struct pt_regs *regs) 233{ 234 struct thread_info *ti; 235 unsigned long trampoline_addr; 236 u32 status; 237 u32 ctrl; 238 int code; 239 240 status = ocd_read(DS); 241 ti = current_thread_info(); 242 code = TRAP_BRKPT; 243 244 pr_debug("do_debug: status=0x%08x PC=0x%08lx SR=0x%08lx tif=0x%08lx\n", 245 status, regs->pc, regs->sr, ti->flags); 246 247 if (!user_mode(regs)) { 248 unsigned long die_val = DIE_BREAKPOINT; 249 250 if (status & (1 << OCD_DS_SSS_BIT)) 251 die_val = DIE_SSTEP; 252 253 if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) 254 == NOTIFY_STOP) 255 return regs; 256 257 if ((status & (1 << OCD_DS_SWB_BIT)) 258 && test_and_clear_ti_thread_flag( 259 ti, TIF_BREAKPOINT)) { 260 /* 261 * Explicit breakpoint from trampoline or 262 * exception/syscall/interrupt handler. 263 * 264 * The real saved regs are on the stack right 265 * after the ones we saved on entry. 266 */ 267 regs++; 268 pr_debug(" -> TIF_BREAKPOINT done, adjusted regs:" 269 "PC=0x%08lx SR=0x%08lx\n", 270 regs->pc, regs->sr); 271 BUG_ON(!user_mode(regs)); 272 273 if (test_thread_flag(TIF_SINGLE_STEP)) { 274 pr_debug("Going to do single step...\n"); 275 return regs; 276 } 277 278 /* 279 * No TIF_SINGLE_STEP means we're done 280 * stepping over a syscall. Do the trap now. 281 */ 282 code = TRAP_TRACE; 283 } else if ((status & (1 << OCD_DS_SSS_BIT)) 284 && test_ti_thread_flag(ti, TIF_SINGLE_STEP)) { 285 286 pr_debug("Stepped into something, " 287 "setting TIF_BREAKPOINT...\n"); 288 set_ti_thread_flag(ti, TIF_BREAKPOINT); 289 290 /* 291 * We stepped into an exception, interrupt or 292 * syscall handler. Some exception handlers 293 * don't check for pending work, so we need to 294 * set up a trampoline just in case. 295 * 296 * The exception entry code will undo the 297 * trampoline stuff if it does a full context 298 * save (which also means that it'll check for 299 * pending work later.) 300 */ 301 if ((regs->sr & MODE_MASK) == MODE_EXCEPTION) { 302 trampoline_addr 303 = (unsigned long)&debug_trampoline; 304 305 pr_debug("Setting up trampoline...\n"); 306 ti->rar_saved = sysreg_read(RAR_EX); 307 ti->rsr_saved = sysreg_read(RSR_EX); 308 sysreg_write(RAR_EX, trampoline_addr); 309 sysreg_write(RSR_EX, (MODE_EXCEPTION 310 | SR_EM | SR_GM)); 311 BUG_ON(ti->rsr_saved & MODE_MASK); 312 } 313 314 /* 315 * If we stepped into a system call, we 316 * shouldn't do a single step after we return 317 * since the return address is right after the 318 * "scall" instruction we were told to step 319 * over. 320 */ 321 if ((regs->sr & MODE_MASK) == MODE_SUPERVISOR) { 322 pr_debug("Supervisor; no single step\n"); 323 clear_ti_thread_flag(ti, TIF_SINGLE_STEP); 324 } 325 326 ctrl = ocd_read(DC); 327 ctrl &= ~(1 << OCD_DC_SS_BIT); 328 ocd_write(DC, ctrl); 329 330 return regs; 331 } else { 332 printk(KERN_ERR "Unexpected OCD_DS value: 0x%08x\n", 333 status); 334 printk(KERN_ERR "Thread flags: 0x%08lx\n", ti->flags); 335 die("Unhandled debug trap in kernel mode", 336 regs, SIGTRAP); 337 } 338 } else if (status & (1 << OCD_DS_SSS_BIT)) { 339 /* Single step in user mode */ 340 code = TRAP_TRACE; 341 342 ctrl = ocd_read(DC); 343 ctrl &= ~(1 << OCD_DC_SS_BIT); 344 ocd_write(DC, ctrl); 345 } 346 347 pr_debug("Sending SIGTRAP: code=%d PC=0x%08lx SR=0x%08lx\n", 348 code, regs->pc, regs->sr); 349 350 clear_thread_flag(TIF_SINGLE_STEP); 351 _exception(SIGTRAP, regs, code, instruction_pointer(regs)); 352 353 return regs; 354} 355