Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
| 6 | * arch/sh64/kernel/ptrace.c |
| 7 | * |
| 8 | * Copyright (C) 2000, 2001 Paolo Alberelli |
| 9 | * Copyright (C) 2003 Paul Mundt |
| 10 | * |
| 11 | * Started from SH3/4 version: |
| 12 | * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka |
| 13 | * |
| 14 | * Original x86 implementation: |
| 15 | * By Ross Biro 1/23/92 |
| 16 | * edited by Linus Torvalds |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include <linux/config.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/rwsem.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/mm.h> |
| 25 | #include <linux/smp.h> |
| 26 | #include <linux/smp_lock.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/ptrace.h> |
| 29 | #include <linux/user.h> |
| 30 | |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/uaccess.h> |
| 33 | #include <asm/pgtable.h> |
| 34 | #include <asm/system.h> |
| 35 | #include <asm/processor.h> |
| 36 | #include <asm/mmu_context.h> |
| 37 | |
| 38 | /* This mask defines the bits of the SR which the user is not allowed to |
| 39 | change, which are everything except S, Q, M, PR, SZ, FR. */ |
| 40 | #define SR_MASK (0xffff8cfd) |
| 41 | |
| 42 | /* |
| 43 | * does not yet catch signals sent when the child dies. |
| 44 | * in exit.c or in signal.c. |
| 45 | */ |
| 46 | |
| 47 | /* |
| 48 | * This routine will get a word from the user area in the process kernel stack. |
| 49 | */ |
| 50 | static inline int get_stack_long(struct task_struct *task, int offset) |
| 51 | { |
| 52 | unsigned char *stack; |
| 53 | |
| 54 | stack = (unsigned char *)(task->thread.uregs); |
| 55 | stack += offset; |
| 56 | return (*((int *)stack)); |
| 57 | } |
| 58 | |
| 59 | static inline unsigned long |
| 60 | get_fpu_long(struct task_struct *task, unsigned long addr) |
| 61 | { |
| 62 | unsigned long tmp; |
| 63 | struct pt_regs *regs; |
| 64 | regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; |
| 65 | |
| 66 | if (!tsk_used_math(task)) { |
| 67 | if (addr == offsetof(struct user_fpu_struct, fpscr)) { |
| 68 | tmp = FPSCR_INIT; |
| 69 | } else { |
| 70 | tmp = 0xffffffffUL; /* matches initial value in fpu.c */ |
| 71 | } |
| 72 | return tmp; |
| 73 | } |
| 74 | |
| 75 | if (last_task_used_math == task) { |
| 76 | grab_fpu(); |
| 77 | fpsave(&task->thread.fpu.hard); |
| 78 | release_fpu(); |
| 79 | last_task_used_math = 0; |
| 80 | regs->sr |= SR_FD; |
| 81 | } |
| 82 | |
| 83 | tmp = ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)]; |
| 84 | return tmp; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * This routine will put a word into the user area in the process kernel stack. |
| 89 | */ |
| 90 | static inline int put_stack_long(struct task_struct *task, int offset, |
| 91 | unsigned long data) |
| 92 | { |
| 93 | unsigned char *stack; |
| 94 | |
| 95 | stack = (unsigned char *)(task->thread.uregs); |
| 96 | stack += offset; |
| 97 | *(unsigned long *) stack = data; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static inline int |
| 102 | put_fpu_long(struct task_struct *task, unsigned long addr, unsigned long data) |
| 103 | { |
| 104 | struct pt_regs *regs; |
| 105 | |
| 106 | regs = (struct pt_regs*)((unsigned char *)task + THREAD_SIZE) - 1; |
| 107 | |
| 108 | if (!tsk_used_math(task)) { |
| 109 | fpinit(&task->thread.fpu.hard); |
| 110 | set_stopped_child_used_math(task); |
| 111 | } else if (last_task_used_math == task) { |
| 112 | grab_fpu(); |
| 113 | fpsave(&task->thread.fpu.hard); |
| 114 | release_fpu(); |
| 115 | last_task_used_math = 0; |
| 116 | regs->sr |= SR_FD; |
| 117 | } |
| 118 | |
| 119 | ((long *)&task->thread.fpu)[addr / sizeof(unsigned long)] = data; |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | asmlinkage int sys_ptrace(long request, long pid, long addr, long data) |
| 124 | { |
| 125 | struct task_struct *child; |
| 126 | extern void poke_real_address_q(unsigned long long addr, unsigned long long data); |
| 127 | #define WPC_DBRMODE 0x0d104008 |
| 128 | static int first_call = 1; |
| 129 | int ret; |
| 130 | |
| 131 | lock_kernel(); |
| 132 | |
| 133 | if (first_call) { |
| 134 | /* Set WPC.DBRMODE to 0. This makes all debug events get |
| 135 | * delivered through RESVEC, i.e. into the handlers in entry.S. |
| 136 | * (If the kernel was downloaded using a remote gdb, WPC.DBRMODE |
| 137 | * would normally be left set to 1, which makes debug events get |
| 138 | * delivered through DBRVEC, i.e. into the remote gdb's |
| 139 | * handlers. This prevents ptrace getting them, and confuses |
| 140 | * the remote gdb.) */ |
| 141 | printk("DBRMODE set to 0 to permit native debugging\n"); |
| 142 | poke_real_address_q(WPC_DBRMODE, 0); |
| 143 | first_call = 0; |
| 144 | } |
| 145 | |
| 146 | ret = -EPERM; |
| 147 | if (request == PTRACE_TRACEME) { |
| 148 | /* are we already being traced? */ |
| 149 | if (current->ptrace & PT_PTRACED) |
| 150 | goto out; |
| 151 | /* set the ptrace bit in the process flags. */ |
| 152 | current->ptrace |= PT_PTRACED; |
| 153 | ret = 0; |
| 154 | goto out; |
| 155 | } |
| 156 | ret = -ESRCH; |
| 157 | read_lock(&tasklist_lock); |
| 158 | child = find_task_by_pid(pid); |
| 159 | if (child) |
| 160 | get_task_struct(child); |
| 161 | read_unlock(&tasklist_lock); |
| 162 | if (!child) |
| 163 | goto out; |
| 164 | |
| 165 | ret = -EPERM; |
| 166 | if (pid == 1) /* you may not mess with init */ |
| 167 | goto out_tsk; |
| 168 | |
| 169 | if (request == PTRACE_ATTACH) { |
| 170 | ret = ptrace_attach(child); |
| 171 | goto out_tsk; |
| 172 | } |
| 173 | |
| 174 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 175 | if (ret < 0) |
| 176 | goto out_tsk; |
| 177 | |
| 178 | switch (request) { |
| 179 | /* when I and D space are separate, these will need to be fixed. */ |
| 180 | case PTRACE_PEEKTEXT: /* read word at location addr. */ |
| 181 | case PTRACE_PEEKDATA: { |
| 182 | unsigned long tmp; |
| 183 | int copied; |
| 184 | |
| 185 | copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 186 | ret = -EIO; |
| 187 | if (copied != sizeof(tmp)) |
| 188 | break; |
| 189 | ret = put_user(tmp,(unsigned long *) data); |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | /* read the word at location addr in the USER area. */ |
| 194 | case PTRACE_PEEKUSR: { |
| 195 | unsigned long tmp; |
| 196 | |
| 197 | ret = -EIO; |
| 198 | if ((addr & 3) || addr < 0) |
| 199 | break; |
| 200 | |
| 201 | if (addr < sizeof(struct pt_regs)) |
| 202 | tmp = get_stack_long(child, addr); |
| 203 | else if ((addr >= offsetof(struct user, fpu)) && |
| 204 | (addr < offsetof(struct user, u_fpvalid))) { |
| 205 | tmp = get_fpu_long(child, addr - offsetof(struct user, fpu)); |
| 206 | } else if (addr == offsetof(struct user, u_fpvalid)) { |
| 207 | tmp = !!tsk_used_math(child); |
| 208 | } else { |
| 209 | break; |
| 210 | } |
| 211 | ret = put_user(tmp, (unsigned long *)data); |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | /* when I and D space are separate, this will have to be fixed. */ |
| 216 | case PTRACE_POKETEXT: /* write the word at location addr. */ |
| 217 | case PTRACE_POKEDATA: |
| 218 | ret = 0; |
| 219 | if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data)) |
| 220 | break; |
| 221 | ret = -EIO; |
| 222 | break; |
| 223 | |
| 224 | case PTRACE_POKEUSR: |
| 225 | /* write the word at location addr in the USER area. We must |
| 226 | disallow any changes to certain SR bits or u_fpvalid, since |
| 227 | this could crash the kernel or result in a security |
| 228 | loophole. */ |
| 229 | ret = -EIO; |
| 230 | if ((addr & 3) || addr < 0) |
| 231 | break; |
| 232 | |
| 233 | if (addr < sizeof(struct pt_regs)) { |
| 234 | /* Ignore change of top 32 bits of SR */ |
| 235 | if (addr == offsetof (struct pt_regs, sr)+4) |
| 236 | { |
| 237 | ret = 0; |
| 238 | break; |
| 239 | } |
| 240 | /* If lower 32 bits of SR, ignore non-user bits */ |
| 241 | if (addr == offsetof (struct pt_regs, sr)) |
| 242 | { |
| 243 | long cursr = get_stack_long(child, addr); |
| 244 | data &= ~(SR_MASK); |
| 245 | data |= (cursr & SR_MASK); |
| 246 | } |
| 247 | ret = put_stack_long(child, addr, data); |
| 248 | } |
| 249 | else if ((addr >= offsetof(struct user, fpu)) && |
| 250 | (addr < offsetof(struct user, u_fpvalid))) { |
| 251 | ret = put_fpu_long(child, addr - offsetof(struct user, fpu), data); |
| 252 | } |
| 253 | break; |
| 254 | |
| 255 | case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */ |
| 256 | case PTRACE_CONT: { /* restart after signal. */ |
| 257 | ret = -EIO; |
| 258 | if ((unsigned long) data > _NSIG) |
| 259 | break; |
| 260 | if (request == PTRACE_SYSCALL) |
| 261 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 262 | else |
| 263 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 264 | child->exit_code = data; |
| 265 | wake_up_process(child); |
| 266 | ret = 0; |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * make the child exit. Best I can do is send it a sigkill. |
| 272 | * perhaps it should be put in the status that it wants to |
| 273 | * exit. |
| 274 | */ |
| 275 | case PTRACE_KILL: { |
| 276 | ret = 0; |
| 277 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 278 | break; |
| 279 | child->exit_code = SIGKILL; |
| 280 | wake_up_process(child); |
| 281 | break; |
| 282 | } |
| 283 | |
| 284 | case PTRACE_SINGLESTEP: { /* set the trap flag. */ |
| 285 | struct pt_regs *regs; |
| 286 | |
| 287 | ret = -EIO; |
| 288 | if ((unsigned long) data > _NSIG) |
| 289 | break; |
| 290 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 291 | if ((child->ptrace & PT_DTRACE) == 0) { |
| 292 | /* Spurious delayed TF traps may occur */ |
| 293 | child->ptrace |= PT_DTRACE; |
| 294 | } |
| 295 | |
| 296 | regs = child->thread.uregs; |
| 297 | |
| 298 | regs->sr |= SR_SSTEP; /* auto-resetting upon exception */ |
| 299 | |
| 300 | child->exit_code = data; |
| 301 | /* give it a chance to run. */ |
| 302 | wake_up_process(child); |
| 303 | ret = 0; |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | case PTRACE_DETACH: /* detach a process that was attached. */ |
| 308 | ret = ptrace_detach(child, data); |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | ret = ptrace_request(child, request, addr, data); |
| 313 | break; |
| 314 | } |
| 315 | out_tsk: |
| 316 | put_task_struct(child); |
| 317 | out: |
| 318 | unlock_kernel(); |
| 319 | return ret; |
| 320 | } |
| 321 | |
| 322 | asmlinkage void syscall_trace(void) |
| 323 | { |
| 324 | struct task_struct *tsk = current; |
| 325 | |
| 326 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 327 | return; |
| 328 | if (!(tsk->ptrace & PT_PTRACED)) |
| 329 | return; |
| 330 | |
| 331 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 332 | ? 0x80 : 0)); |
| 333 | /* |
| 334 | * this isn't the same as continuing with a signal, but it will do |
| 335 | * for normal use. strace only continues with a signal if the |
| 336 | * stopping signal is not SIGTRAP. -brl |
| 337 | */ |
| 338 | if (tsk->exit_code) { |
| 339 | send_sig(tsk->exit_code, tsk, 1); |
| 340 | tsk->exit_code = 0; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* Called with interrupts disabled */ |
| 345 | asmlinkage void do_single_step(unsigned long long vec, struct pt_regs *regs) |
| 346 | { |
| 347 | /* This is called after a single step exception (DEBUGSS). |
| 348 | There is no need to change the PC, as it is a post-execution |
| 349 | exception, as entry.S does not do anything to the PC for DEBUGSS. |
| 350 | We need to clear the Single Step setting in SR to avoid |
| 351 | continually stepping. */ |
| 352 | local_irq_enable(); |
| 353 | regs->sr &= ~SR_SSTEP; |
| 354 | force_sig(SIGTRAP, current); |
| 355 | } |
| 356 | |
| 357 | /* Called with interrupts disabled */ |
| 358 | asmlinkage void do_software_break_point(unsigned long long vec, |
| 359 | struct pt_regs *regs) |
| 360 | { |
| 361 | /* We need to forward step the PC, to counteract the backstep done |
| 362 | in signal.c. */ |
| 363 | local_irq_enable(); |
| 364 | force_sig(SIGTRAP, current); |
| 365 | regs->pc += 4; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Called by kernel/ptrace.c when detaching.. |
| 370 | * |
| 371 | * Make sure single step bits etc are not set. |
| 372 | */ |
| 373 | void ptrace_disable(struct task_struct *child) |
| 374 | { |
| 375 | /* nothing to do.. */ |
| 376 | } |