ptrace.c revision 8dfe8f29cd371affcc3c6b35658dc4bd95ee7b61
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 31static void ptrace_single_step(struct task_struct *tsk) 32{ 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)) { 36 /* 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. 41 */ 42 pr_debug("ptrace_single_step: Setting TIF_BREAKPOINT\n"); 43 set_tsk_thread_flag(tsk, TIF_BREAKPOINT); 44 } 45 46 /* The monitor code will do the actual step for us */ 47 set_tsk_thread_flag(tsk, TIF_SINGLE_STEP); 48} 49 50/* 51 * Called by kernel/ptrace.c when detaching 52 * 53 * Make sure any single step bits, etc. are not set 54 */ 55void ptrace_disable(struct task_struct *child) 56{ 57 clear_tsk_thread_flag(child, TIF_SINGLE_STEP); 58} 59 60/* 61 * Handle hitting a breakpoint 62 */ 63static void ptrace_break(struct task_struct *tsk, struct pt_regs *regs) 64{ 65 siginfo_t info; 66 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); 71 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); 75} 76 77/* 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. 80 */ 81static int ptrace_read_user(struct task_struct *tsk, unsigned long offset, 82 unsigned long __user *data) 83{ 84 unsigned long *regs; 85 unsigned long value; 86 87 pr_debug("ptrace_read_user(%p, %#lx, %p)\n", 88 tsk, offset, data); 89 90 if (offset & 3 || offset >= sizeof(struct user)) { 91 printk("ptrace_read_user: invalid offset 0x%08lx\n", offset); 92 return -EIO; 93 } 94 95 regs = (unsigned long *)get_user_regs(tsk); 96 97 value = 0; 98 if (offset < sizeof(struct pt_regs)) 99 value = regs[offset / sizeof(regs[0])]; 100 101 return put_user(value, data); 102} 103 104/* 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. 108 */ 109static int ptrace_write_user(struct task_struct *tsk, unsigned long offset, 110 unsigned long value) 111{ 112 unsigned long *regs; 113 114 if (offset & 3 || offset >= sizeof(struct user)) { 115 printk("ptrace_write_user: invalid offset 0x%08lx\n", offset); 116 return -EIO; 117 } 118 119 if (offset >= sizeof(struct pt_regs)) 120 return 0; 121 122 regs = (unsigned long *)get_user_regs(tsk); 123 regs[offset / sizeof(regs[0])] = value; 124 125 return 0; 126} 127 128static int ptrace_getregs(struct task_struct *tsk, void __user *uregs) 129{ 130 struct pt_regs *regs = get_user_regs(tsk); 131 132 return copy_to_user(uregs, regs, sizeof(*regs)) ? -EFAULT : 0; 133} 134 135static int ptrace_setregs(struct task_struct *tsk, const void __user *uregs) 136{ 137 struct pt_regs newregs; 138 int ret; 139 140 ret = -EFAULT; 141 if (copy_from_user(&newregs, uregs, sizeof(newregs)) == 0) { 142 struct pt_regs *regs = get_user_regs(tsk); 143 144 ret = -EINVAL; 145 if (valid_user_regs(&newregs)) { 146 *regs = newregs; 147 ret = 0; 148 } 149 } 150 151 return ret; 152} 153 154long arch_ptrace(struct task_struct *child, long request, long addr, long data) 155{ 156 int ret; 157 158 pr_debug("arch_ptrace(%ld, %d, %#lx, %#lx)\n", 159 request, child->pid, addr, data); 160 161 pr_debug("ptrace: Enabling monitor mode...\n"); 162 ocd_write(DC, ocd_read(DC) | (1 << OCD_DC_MM_BIT) 163 | (1 << OCD_DC_DBE_BIT)); 164 165 switch (request) { 166 /* Read the word at location addr in the child process */ 167 case PTRACE_PEEKTEXT: 168 case PTRACE_PEEKDATA: 169 ret = generic_ptrace_peekdata(child, addr, data); 170 break; 171 172 case PTRACE_PEEKUSR: 173 ret = ptrace_read_user(child, addr, 174 (unsigned long __user *)data); 175 break; 176 177 /* Write the word in data at location addr */ 178 case PTRACE_POKETEXT: 179 case PTRACE_POKEDATA: 180 ret = generic_ptrace_pokedata(child, addr, data); 181 break; 182 183 case PTRACE_POKEUSR: 184 ret = ptrace_write_user(child, addr, data); 185 break; 186 187 /* continue and stop at next (return from) syscall */ 188 case PTRACE_SYSCALL: 189 /* restart after signal */ 190 case PTRACE_CONT: 191 ret = -EIO; 192 if (!valid_signal(data)) 193 break; 194 if (request == PTRACE_SYSCALL) 195 set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 196 else 197 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 198 child->exit_code = data; 199 /* XXX: Are we sure no breakpoints are active here? */ 200 wake_up_process(child); 201 ret = 0; 202 break; 203 204 /* 205 * Make the child exit. Best I can do is send it a 206 * SIGKILL. Perhaps it should be put in the status that it 207 * wants to exit. 208 */ 209 case PTRACE_KILL: 210 ret = 0; 211 if (child->exit_state == EXIT_ZOMBIE) 212 break; 213 child->exit_code = SIGKILL; 214 wake_up_process(child); 215 break; 216 217 /* 218 * execute single instruction. 219 */ 220 case PTRACE_SINGLESTEP: 221 ret = -EIO; 222 if (!valid_signal(data)) 223 break; 224 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); 225 ptrace_single_step(child); 226 child->exit_code = data; 227 wake_up_process(child); 228 ret = 0; 229 break; 230 231 case PTRACE_GETREGS: 232 ret = ptrace_getregs(child, (void __user *)data); 233 break; 234 235 case PTRACE_SETREGS: 236 ret = ptrace_setregs(child, (const void __user *)data); 237 break; 238 239 default: 240 ret = ptrace_request(child, request, addr, data); 241 break; 242 } 243 244 pr_debug("sys_ptrace returning %d (DC = 0x%08lx)\n", 245 ret, ocd_read(DC)); 246 return ret; 247} 248 249asmlinkage void syscall_trace(void) 250{ 251 pr_debug("syscall_trace called\n"); 252 if (!test_thread_flag(TIF_SYSCALL_TRACE)) 253 return; 254 if (!(current->ptrace & PT_PTRACED)) 255 return; 256 257 pr_debug("syscall_trace: notifying parent\n"); 258 /* The 0x80 provides a way for the tracing parent to 259 * distinguish between a syscall stop and SIGTRAP delivery */ 260 ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) 261 ? 0x80 : 0)); 262 263 /* 264 * this isn't the same as continuing with a signal, but it 265 * will do for normal use. strace only continues with a 266 * signal if the stopping signal is not SIGTRAP. -brl 267 */ 268 if (current->exit_code) { 269 pr_debug("syscall_trace: sending signal %d to PID %u\n", 270 current->exit_code, current->pid); 271 send_sig(current->exit_code, current, 1); 272 current->exit_code = 0; 273 } 274} 275 276asmlinkage void do_debug_priv(struct pt_regs *regs) 277{ 278 unsigned long dc, ds; 279 unsigned long die_val; 280 281 ds = ocd_read(DS); 282 283 pr_debug("do_debug_priv: pc = %08lx, ds = %08lx\n", regs->pc, ds); 284 285 if (ds & (1 << OCD_DS_SSS_BIT)) 286 die_val = DIE_SSTEP; 287 else 288 die_val = DIE_BREAKPOINT; 289 290 if (notify_die(die_val, "ptrace", regs, 0, 0, SIGTRAP) == NOTIFY_STOP) 291 return; 292 293 if (likely(ds & (1 << OCD_DS_SSS_BIT))) { 294 extern void itlb_miss(void); 295 extern void tlb_miss_common(void); 296 struct thread_info *ti; 297 298 dc = ocd_read(DC); 299 dc &= ~(1 << OCD_DC_SS_BIT); 300 ocd_write(DC, dc); 301 302 ti = current_thread_info(); 303 set_ti_thread_flag(ti, TIF_BREAKPOINT); 304 305 /* The TLB miss handlers don't check thread flags */ 306 if ((regs->pc >= (unsigned long)&itlb_miss) 307 && (regs->pc <= (unsigned long)&tlb_miss_common)) { 308 ocd_write(BWA2A, sysreg_read(RAR_EX)); 309 ocd_write(BWC2A, 0x40000001 | (get_asid() << 1)); 310 } 311 312 /* 313 * If we're running in supervisor mode, the breakpoint 314 * will take us where we want directly, no need to 315 * single step. 316 */ 317 if ((regs->sr & MODE_MASK) != MODE_SUPERVISOR) 318 set_ti_thread_flag(ti, TIF_SINGLE_STEP); 319 } else { 320 panic("Unable to handle debug trap at pc = %08lx\n", 321 regs->pc); 322 } 323} 324 325/* 326 * Handle breakpoints, single steps and other debuggy things. To keep 327 * things simple initially, we run with interrupts and exceptions 328 * disabled all the time. 329 */ 330asmlinkage void do_debug(struct pt_regs *regs) 331{ 332 unsigned long dc, ds; 333 334 ds = ocd_read(DS); 335 pr_debug("do_debug: pc = %08lx, ds = %08lx\n", regs->pc, ds); 336 337 if (test_thread_flag(TIF_BREAKPOINT)) { 338 pr_debug("TIF_BREAKPOINT set\n"); 339 /* We're taking care of it */ 340 clear_thread_flag(TIF_BREAKPOINT); 341 ocd_write(BWC2A, 0); 342 } 343 344 if (test_thread_flag(TIF_SINGLE_STEP)) { 345 pr_debug("TIF_SINGLE_STEP set, ds = 0x%08lx\n", ds); 346 if (ds & (1 << OCD_DS_SSS_BIT)) { 347 dc = ocd_read(DC); 348 dc &= ~(1 << OCD_DC_SS_BIT); 349 ocd_write(DC, dc); 350 351 clear_thread_flag(TIF_SINGLE_STEP); 352 ptrace_break(current, regs); 353 } 354 } else { 355 /* regular breakpoint */ 356 ptrace_break(current, regs); 357 } 358} 359