Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/arch/m32r/kernel/ptrace.c |
| 3 | * |
| 4 | * Copyright (C) 2002 Hirokazu Takata, Takeo Takahashi |
| 5 | * Copyright (C) 2004 Hirokazu Takata, Kei Sakamoto |
| 6 | * |
| 7 | * Original x86 implementation: |
| 8 | * By Ross Biro 1/23/92 |
| 9 | * edited by Linus Torvalds |
| 10 | * |
| 11 | * Some code taken from sh version: |
| 12 | * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka |
| 13 | * Some code taken from arm version: |
| 14 | * Copyright (C) 2000 Russell King |
| 15 | */ |
| 16 | |
| 17 | #include <linux/config.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/mm.h> |
| 21 | #include <linux/smp.h> |
| 22 | #include <linux/smp_lock.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/ptrace.h> |
| 25 | #include <linux/user.h> |
| 26 | #include <linux/string.h> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 27 | #include <linux/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <asm/cacheflush.h> |
| 30 | #include <asm/io.h> |
| 31 | #include <asm/uaccess.h> |
| 32 | #include <asm/pgtable.h> |
| 33 | #include <asm/system.h> |
| 34 | #include <asm/processor.h> |
| 35 | #include <asm/mmu_context.h> |
| 36 | |
| 37 | /* |
| 38 | * Get the address of the live pt_regs for the specified task. |
| 39 | * These are saved onto the top kernel stack when the process |
| 40 | * is not running. |
| 41 | * |
| 42 | * Note: if a user thread is execve'd from kernel space, the |
| 43 | * kernel stack will not be empty on entry to the kernel, so |
| 44 | * ptracing these tasks will fail. |
| 45 | */ |
| 46 | static inline struct pt_regs * |
| 47 | get_user_regs(struct task_struct *task) |
| 48 | { |
| 49 | return (struct pt_regs *) |
| 50 | ((unsigned long)task->thread_info + THREAD_SIZE |
| 51 | - sizeof(struct pt_regs)); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * This routine will get a word off of the process kernel stack. |
| 56 | */ |
| 57 | static inline unsigned long int |
| 58 | get_stack_long(struct task_struct *task, int offset) |
| 59 | { |
| 60 | unsigned long *stack; |
| 61 | |
| 62 | stack = (unsigned long *)get_user_regs(task); |
| 63 | |
| 64 | return stack[offset]; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * This routine will put a word on the process kernel stack. |
| 69 | */ |
| 70 | static inline int |
| 71 | put_stack_long(struct task_struct *task, int offset, unsigned long data) |
| 72 | { |
| 73 | unsigned long *stack; |
| 74 | |
| 75 | stack = (unsigned long *)get_user_regs(task); |
| 76 | stack[offset] = data; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int reg_offset[] = { |
| 82 | PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7, |
| 83 | PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_FP, PT_LR, PT_SPU, |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * Read the word at offset "off" into the "struct user". We |
| 88 | * actually access the pt_regs stored on the kernel stack. |
| 89 | */ |
| 90 | static int ptrace_read_user(struct task_struct *tsk, unsigned long off, |
| 91 | unsigned long __user *data) |
| 92 | { |
| 93 | unsigned long tmp; |
| 94 | #ifndef NO_FPU |
| 95 | struct user * dummy = NULL; |
| 96 | #endif |
| 97 | |
| 98 | if ((off & 3) || (off < 0) || (off > sizeof(struct user) - 3)) |
| 99 | return -EIO; |
| 100 | |
| 101 | off >>= 2; |
| 102 | switch (off) { |
| 103 | case PT_EVB: |
| 104 | __asm__ __volatile__ ( |
| 105 | "mvfc %0, cr5 \n\t" |
| 106 | : "=r" (tmp) |
| 107 | ); |
| 108 | break; |
| 109 | case PT_CBR: { |
| 110 | unsigned long psw; |
| 111 | psw = get_stack_long(tsk, PT_PSW); |
| 112 | tmp = ((psw >> 8) & 1); |
| 113 | } |
| 114 | break; |
| 115 | case PT_PSW: { |
| 116 | unsigned long psw, bbpsw; |
| 117 | psw = get_stack_long(tsk, PT_PSW); |
| 118 | bbpsw = get_stack_long(tsk, PT_BBPSW); |
| 119 | tmp = ((psw >> 8) & 0xff) | ((bbpsw & 0xff) << 8); |
| 120 | } |
| 121 | break; |
| 122 | case PT_PC: |
| 123 | tmp = get_stack_long(tsk, PT_BPC); |
| 124 | break; |
| 125 | case PT_BPC: |
| 126 | off = PT_BBPC; |
| 127 | /* fall through */ |
| 128 | default: |
| 129 | if (off < (sizeof(struct pt_regs) >> 2)) |
| 130 | tmp = get_stack_long(tsk, off); |
| 131 | #ifndef NO_FPU |
| 132 | else if (off >= (long)(&dummy->fpu >> 2) && |
| 133 | off < (long)(&dummy->u_fpvalid >> 2)) { |
| 134 | if (!tsk_used_math(tsk)) { |
| 135 | if (off == (long)(&dummy->fpu.fpscr >> 2)) |
| 136 | tmp = FPSCR_INIT; |
| 137 | else |
| 138 | tmp = 0; |
| 139 | } else |
| 140 | tmp = ((long *)(&tsk->thread.fpu >> 2)) |
| 141 | [off - (long)&dummy->fpu]; |
| 142 | } else if (off == (long)(&dummy->u_fpvalid >> 2)) |
| 143 | tmp = !!tsk_used_math(tsk); |
| 144 | #endif /* not NO_FPU */ |
| 145 | else |
| 146 | tmp = 0; |
| 147 | } |
| 148 | |
| 149 | return put_user(tmp, data); |
| 150 | } |
| 151 | |
| 152 | static int ptrace_write_user(struct task_struct *tsk, unsigned long off, |
| 153 | unsigned long data) |
| 154 | { |
| 155 | int ret = -EIO; |
| 156 | #ifndef NO_FPU |
| 157 | struct user * dummy = NULL; |
| 158 | #endif |
| 159 | |
| 160 | if ((off & 3) || off < 0 || |
| 161 | off > sizeof(struct user) - 3) |
| 162 | return -EIO; |
| 163 | |
| 164 | off >>= 2; |
| 165 | switch (off) { |
| 166 | case PT_EVB: |
| 167 | case PT_BPC: |
| 168 | case PT_SPI: |
| 169 | /* We don't allow to modify evb. */ |
| 170 | ret = 0; |
| 171 | break; |
| 172 | case PT_PSW: |
| 173 | case PT_CBR: { |
| 174 | /* We allow to modify only cbr in psw */ |
| 175 | unsigned long psw; |
| 176 | psw = get_stack_long(tsk, PT_PSW); |
| 177 | psw = (psw & ~0x100) | ((data & 1) << 8); |
| 178 | ret = put_stack_long(tsk, PT_PSW, psw); |
| 179 | } |
| 180 | break; |
| 181 | case PT_PC: |
| 182 | off = PT_BPC; |
| 183 | data &= ~1; |
| 184 | /* fall through */ |
| 185 | default: |
| 186 | if (off < (sizeof(struct pt_regs) >> 2)) |
| 187 | ret = put_stack_long(tsk, off, data); |
| 188 | #ifndef NO_FPU |
| 189 | else if (off >= (long)(&dummy->fpu >> 2) && |
| 190 | off < (long)(&dummy->u_fpvalid >> 2)) { |
| 191 | set_stopped_child_used_math(tsk); |
| 192 | ((long *)&tsk->thread.fpu) |
| 193 | [off - (long)&dummy->fpu] = data; |
| 194 | ret = 0; |
| 195 | } else if (off == (long)(&dummy->u_fpvalid >> 2)) { |
| 196 | conditional_stopped_child_used_math(data, tsk); |
| 197 | ret = 0; |
| 198 | } |
| 199 | #endif /* not NO_FPU */ |
| 200 | break; |
| 201 | } |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Get all user integer registers. |
| 208 | */ |
| 209 | static int ptrace_getregs(struct task_struct *tsk, void __user *uregs) |
| 210 | { |
| 211 | struct pt_regs *regs = get_user_regs(tsk); |
| 212 | |
| 213 | return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Set all user integer registers. |
| 218 | */ |
| 219 | static int ptrace_setregs(struct task_struct *tsk, void __user *uregs) |
| 220 | { |
| 221 | struct pt_regs newregs; |
| 222 | int ret; |
| 223 | |
| 224 | ret = -EFAULT; |
| 225 | if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) { |
| 226 | struct pt_regs *regs = get_user_regs(tsk); |
| 227 | *regs = newregs; |
| 228 | ret = 0; |
| 229 | } |
| 230 | |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static inline int |
| 236 | check_condition_bit(struct task_struct *child) |
| 237 | { |
| 238 | return (int)((get_stack_long(child, PT_PSW) >> 8) & 1); |
| 239 | } |
| 240 | |
| 241 | static int |
| 242 | check_condition_src(unsigned long op, unsigned long regno1, |
| 243 | unsigned long regno2, struct task_struct *child) |
| 244 | { |
| 245 | unsigned long reg1, reg2; |
| 246 | |
| 247 | reg2 = get_stack_long(child, reg_offset[regno2]); |
| 248 | |
| 249 | switch (op) { |
| 250 | case 0x0: /* BEQ */ |
| 251 | reg1 = get_stack_long(child, reg_offset[regno1]); |
| 252 | return reg1 == reg2; |
| 253 | case 0x1: /* BNE */ |
| 254 | reg1 = get_stack_long(child, reg_offset[regno1]); |
| 255 | return reg1 != reg2; |
| 256 | case 0x8: /* BEQZ */ |
| 257 | return reg2 == 0; |
| 258 | case 0x9: /* BNEZ */ |
| 259 | return reg2 != 0; |
| 260 | case 0xa: /* BLTZ */ |
| 261 | return (int)reg2 < 0; |
| 262 | case 0xb: /* BGEZ */ |
| 263 | return (int)reg2 >= 0; |
| 264 | case 0xc: /* BLEZ */ |
| 265 | return (int)reg2 <= 0; |
| 266 | case 0xd: /* BGTZ */ |
| 267 | return (int)reg2 > 0; |
| 268 | default: |
| 269 | /* never reached */ |
| 270 | return 0; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | static void |
| 275 | compute_next_pc_for_16bit_insn(unsigned long insn, unsigned long pc, |
| 276 | unsigned long *next_pc, |
| 277 | struct task_struct *child) |
| 278 | { |
| 279 | unsigned long op, op2, op3; |
| 280 | unsigned long disp; |
| 281 | unsigned long regno; |
| 282 | int parallel = 0; |
| 283 | |
| 284 | if (insn & 0x00008000) |
| 285 | parallel = 1; |
| 286 | if (pc & 3) |
| 287 | insn &= 0x7fff; /* right slot */ |
| 288 | else |
| 289 | insn >>= 16; /* left slot */ |
| 290 | |
| 291 | op = (insn >> 12) & 0xf; |
| 292 | op2 = (insn >> 8) & 0xf; |
| 293 | op3 = (insn >> 4) & 0xf; |
| 294 | |
| 295 | if (op == 0x7) { |
| 296 | switch (op2) { |
| 297 | case 0xd: /* BNC */ |
| 298 | case 0x9: /* BNCL */ |
| 299 | if (!check_condition_bit(child)) { |
| 300 | disp = (long)(insn << 24) >> 22; |
| 301 | *next_pc = (pc & ~0x3) + disp; |
| 302 | return; |
| 303 | } |
| 304 | break; |
| 305 | case 0x8: /* BCL */ |
| 306 | case 0xc: /* BC */ |
| 307 | if (check_condition_bit(child)) { |
| 308 | disp = (long)(insn << 24) >> 22; |
| 309 | *next_pc = (pc & ~0x3) + disp; |
| 310 | return; |
| 311 | } |
| 312 | break; |
| 313 | case 0xe: /* BL */ |
| 314 | case 0xf: /* BRA */ |
| 315 | disp = (long)(insn << 24) >> 22; |
| 316 | *next_pc = (pc & ~0x3) + disp; |
| 317 | return; |
| 318 | break; |
| 319 | } |
| 320 | } else if (op == 0x1) { |
| 321 | switch (op2) { |
| 322 | case 0x0: |
| 323 | if (op3 == 0xf) { /* TRAP */ |
| 324 | #if 1 |
| 325 | /* pass through */ |
| 326 | #else |
| 327 | /* kernel space is not allowed as next_pc */ |
| 328 | unsigned long evb; |
| 329 | unsigned long trapno; |
| 330 | trapno = insn & 0xf; |
| 331 | __asm__ __volatile__ ( |
| 332 | "mvfc %0, cr5\n" |
| 333 | :"=r"(evb) |
| 334 | : |
| 335 | ); |
| 336 | *next_pc = evb + (trapno << 2); |
| 337 | return; |
| 338 | #endif |
| 339 | } else if (op3 == 0xd) { /* RTE */ |
| 340 | *next_pc = get_stack_long(child, PT_BPC); |
| 341 | return; |
| 342 | } |
| 343 | break; |
| 344 | case 0xc: /* JC */ |
| 345 | if (op3 == 0xc && check_condition_bit(child)) { |
| 346 | regno = insn & 0xf; |
| 347 | *next_pc = get_stack_long(child, |
| 348 | reg_offset[regno]); |
| 349 | return; |
| 350 | } |
| 351 | break; |
| 352 | case 0xd: /* JNC */ |
| 353 | if (op3 == 0xc && !check_condition_bit(child)) { |
| 354 | regno = insn & 0xf; |
| 355 | *next_pc = get_stack_long(child, |
| 356 | reg_offset[regno]); |
| 357 | return; |
| 358 | } |
| 359 | break; |
| 360 | case 0xe: /* JL */ |
| 361 | case 0xf: /* JMP */ |
| 362 | if (op3 == 0xc) { /* JMP */ |
| 363 | regno = insn & 0xf; |
| 364 | *next_pc = get_stack_long(child, |
| 365 | reg_offset[regno]); |
| 366 | return; |
| 367 | } |
| 368 | break; |
| 369 | } |
| 370 | } |
| 371 | if (parallel) |
| 372 | *next_pc = pc + 4; |
| 373 | else |
| 374 | *next_pc = pc + 2; |
| 375 | } |
| 376 | |
| 377 | static void |
| 378 | compute_next_pc_for_32bit_insn(unsigned long insn, unsigned long pc, |
| 379 | unsigned long *next_pc, |
| 380 | struct task_struct *child) |
| 381 | { |
| 382 | unsigned long op; |
| 383 | unsigned long op2; |
| 384 | unsigned long disp; |
| 385 | unsigned long regno1, regno2; |
| 386 | |
| 387 | op = (insn >> 28) & 0xf; |
| 388 | if (op == 0xf) { /* branch 24-bit relative */ |
| 389 | op2 = (insn >> 24) & 0xf; |
| 390 | switch (op2) { |
| 391 | case 0xd: /* BNC */ |
| 392 | case 0x9: /* BNCL */ |
| 393 | if (!check_condition_bit(child)) { |
| 394 | disp = (long)(insn << 8) >> 6; |
| 395 | *next_pc = (pc & ~0x3) + disp; |
| 396 | return; |
| 397 | } |
| 398 | break; |
| 399 | case 0x8: /* BCL */ |
| 400 | case 0xc: /* BC */ |
| 401 | if (check_condition_bit(child)) { |
| 402 | disp = (long)(insn << 8) >> 6; |
| 403 | *next_pc = (pc & ~0x3) + disp; |
| 404 | return; |
| 405 | } |
| 406 | break; |
| 407 | case 0xe: /* BL */ |
| 408 | case 0xf: /* BRA */ |
| 409 | disp = (long)(insn << 8) >> 6; |
| 410 | *next_pc = (pc & ~0x3) + disp; |
| 411 | return; |
| 412 | } |
| 413 | } else if (op == 0xb) { /* branch 16-bit relative */ |
| 414 | op2 = (insn >> 20) & 0xf; |
| 415 | switch (op2) { |
| 416 | case 0x0: /* BEQ */ |
| 417 | case 0x1: /* BNE */ |
| 418 | case 0x8: /* BEQZ */ |
| 419 | case 0x9: /* BNEZ */ |
| 420 | case 0xa: /* BLTZ */ |
| 421 | case 0xb: /* BGEZ */ |
| 422 | case 0xc: /* BLEZ */ |
| 423 | case 0xd: /* BGTZ */ |
| 424 | regno1 = ((insn >> 24) & 0xf); |
| 425 | regno2 = ((insn >> 16) & 0xf); |
| 426 | if (check_condition_src(op2, regno1, regno2, child)) { |
| 427 | disp = (long)(insn << 16) >> 14; |
| 428 | *next_pc = (pc & ~0x3) + disp; |
| 429 | return; |
| 430 | } |
| 431 | break; |
| 432 | } |
| 433 | } |
| 434 | *next_pc = pc + 4; |
| 435 | } |
| 436 | |
| 437 | static inline void |
| 438 | compute_next_pc(unsigned long insn, unsigned long pc, |
| 439 | unsigned long *next_pc, struct task_struct *child) |
| 440 | { |
| 441 | if (insn & 0x80000000) |
| 442 | compute_next_pc_for_32bit_insn(insn, pc, next_pc, child); |
| 443 | else |
| 444 | compute_next_pc_for_16bit_insn(insn, pc, next_pc, child); |
| 445 | } |
| 446 | |
| 447 | static int |
| 448 | register_debug_trap(struct task_struct *child, unsigned long next_pc, |
| 449 | unsigned long next_insn, unsigned long *code) |
| 450 | { |
| 451 | struct debug_trap *p = &child->thread.debug_trap; |
| 452 | unsigned long addr = next_pc & ~3; |
| 453 | |
| 454 | if (p->nr_trap == MAX_TRAPS) { |
| 455 | printk("kernel BUG at %s %d: p->nr_trap = %d\n", |
| 456 | __FILE__, __LINE__, p->nr_trap); |
| 457 | return -1; |
| 458 | } |
| 459 | p->addr[p->nr_trap] = addr; |
| 460 | p->insn[p->nr_trap] = next_insn; |
| 461 | p->nr_trap++; |
| 462 | if (next_pc & 3) { |
| 463 | *code = (next_insn & 0xffff0000) | 0x10f1; |
| 464 | /* xxx --> TRAP1 */ |
| 465 | } else { |
| 466 | if ((next_insn & 0x80000000) || (next_insn & 0x8000)) { |
| 467 | *code = 0x10f17000; |
| 468 | /* TRAP1 --> NOP */ |
| 469 | } else { |
| 470 | *code = (next_insn & 0xffff) | 0x10f10000; |
| 471 | /* TRAP1 --> xxx */ |
| 472 | } |
| 473 | } |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static int |
| 478 | unregister_debug_trap(struct task_struct *child, unsigned long addr, |
| 479 | unsigned long *code) |
| 480 | { |
| 481 | struct debug_trap *p = &child->thread.debug_trap; |
| 482 | int i; |
| 483 | |
| 484 | /* Search debug trap entry. */ |
| 485 | for (i = 0; i < p->nr_trap; i++) { |
| 486 | if (p->addr[i] == addr) |
| 487 | break; |
| 488 | } |
| 489 | if (i >= p->nr_trap) { |
| 490 | /* The trap may be requested from debugger. |
| 491 | * ptrace should do nothing in this case. |
| 492 | */ |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | /* Recover orignal instruction code. */ |
| 497 | *code = p->insn[i]; |
| 498 | |
| 499 | /* Shift debug trap entries. */ |
| 500 | while (i < p->nr_trap - 1) { |
| 501 | p->insn[i] = p->insn[i + 1]; |
| 502 | p->addr[i] = p->addr[i + 1]; |
| 503 | i++; |
| 504 | } |
| 505 | p->nr_trap--; |
| 506 | return 1; |
| 507 | } |
| 508 | |
| 509 | static void |
| 510 | unregister_all_debug_traps(struct task_struct *child) |
| 511 | { |
| 512 | struct debug_trap *p = &child->thread.debug_trap; |
| 513 | int i; |
| 514 | |
| 515 | for (i = 0; i < p->nr_trap; i++) |
| 516 | access_process_vm(child, p->addr[i], &p->insn[i], sizeof(p->insn[i]), 1); |
| 517 | p->nr_trap = 0; |
| 518 | } |
| 519 | |
| 520 | static inline void |
| 521 | invalidate_cache(void) |
| 522 | { |
| 523 | #if defined(CONFIG_CHIP_M32700) || defined(CONFIG_CHIP_OPSP) |
| 524 | |
| 525 | _flush_cache_copyback_all(); |
| 526 | |
| 527 | #else /* ! CONFIG_CHIP_M32700 */ |
| 528 | |
| 529 | /* Invalidate cache */ |
| 530 | __asm__ __volatile__ ( |
| 531 | "ldi r0, #-1 \n\t" |
| 532 | "ldi r1, #0 \n\t" |
| 533 | "stb r1, @r0 ; cache off \n\t" |
| 534 | "; \n\t" |
| 535 | "ldi r0, #-2 \n\t" |
| 536 | "ldi r1, #1 \n\t" |
| 537 | "stb r1, @r0 ; cache invalidate \n\t" |
| 538 | ".fillinsn \n" |
| 539 | "0: \n\t" |
| 540 | "ldb r1, @r0 ; invalidate check \n\t" |
| 541 | "bnez r1, 0b \n\t" |
| 542 | "; \n\t" |
| 543 | "ldi r0, #-1 \n\t" |
| 544 | "ldi r1, #1 \n\t" |
| 545 | "stb r1, @r0 ; cache on \n\t" |
| 546 | : : : "r0", "r1", "memory" |
| 547 | ); |
| 548 | /* FIXME: copying-back d-cache and invalidating i-cache are needed. |
| 549 | */ |
| 550 | #endif /* CONFIG_CHIP_M32700 */ |
| 551 | } |
| 552 | |
| 553 | /* Embed a debug trap (TRAP1) code */ |
| 554 | static int |
| 555 | embed_debug_trap(struct task_struct *child, unsigned long next_pc) |
| 556 | { |
| 557 | unsigned long next_insn, code; |
| 558 | unsigned long addr = next_pc & ~3; |
| 559 | |
| 560 | if (access_process_vm(child, addr, &next_insn, sizeof(next_insn), 0) |
| 561 | != sizeof(next_insn)) { |
| 562 | return -1; /* error */ |
| 563 | } |
| 564 | |
| 565 | /* Set a trap code. */ |
| 566 | if (register_debug_trap(child, next_pc, next_insn, &code)) { |
| 567 | return -1; /* error */ |
| 568 | } |
| 569 | if (access_process_vm(child, addr, &code, sizeof(code), 1) |
| 570 | != sizeof(code)) { |
| 571 | return -1; /* error */ |
| 572 | } |
| 573 | return 0; /* success */ |
| 574 | } |
| 575 | |
| 576 | void |
| 577 | withdraw_debug_trap(struct pt_regs *regs) |
| 578 | { |
| 579 | unsigned long addr; |
| 580 | unsigned long code; |
| 581 | |
| 582 | addr = (regs->bpc - 2) & ~3; |
| 583 | regs->bpc -= 2; |
| 584 | if (unregister_debug_trap(current, addr, &code)) { |
| 585 | access_process_vm(current, addr, &code, sizeof(code), 1); |
| 586 | invalidate_cache(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | static void |
| 591 | init_debug_traps(struct task_struct *child) |
| 592 | { |
| 593 | struct debug_trap *p = &child->thread.debug_trap; |
| 594 | int i; |
| 595 | p->nr_trap = 0; |
| 596 | for (i = 0; i < MAX_TRAPS; i++) { |
| 597 | p->addr[i] = 0; |
| 598 | p->insn[i] = 0; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | |
| 603 | /* |
| 604 | * Called by kernel/ptrace.c when detaching.. |
| 605 | * |
| 606 | * Make sure single step bits etc are not set. |
| 607 | */ |
| 608 | void ptrace_disable(struct task_struct *child) |
| 609 | { |
| 610 | /* nothing to do.. */ |
| 611 | } |
| 612 | |
| 613 | static int |
| 614 | do_ptrace(long request, struct task_struct *child, long addr, long data) |
| 615 | { |
| 616 | unsigned long tmp; |
| 617 | int ret; |
| 618 | |
| 619 | switch (request) { |
| 620 | /* |
| 621 | * read word at location "addr" in the child process. |
| 622 | */ |
| 623 | case PTRACE_PEEKTEXT: |
| 624 | case PTRACE_PEEKDATA: |
| 625 | ret = access_process_vm(child, addr, &tmp, sizeof(tmp), 0); |
| 626 | if (ret == sizeof(tmp)) |
| 627 | ret = put_user(tmp,(unsigned long __user *) data); |
| 628 | else |
| 629 | ret = -EIO; |
| 630 | break; |
| 631 | |
| 632 | /* |
| 633 | * read the word at location addr in the USER area. |
| 634 | */ |
| 635 | case PTRACE_PEEKUSR: |
| 636 | ret = ptrace_read_user(child, addr, |
| 637 | (unsigned long __user *)data); |
| 638 | break; |
| 639 | |
| 640 | /* |
| 641 | * write the word at location addr. |
| 642 | */ |
| 643 | case PTRACE_POKETEXT: |
| 644 | case PTRACE_POKEDATA: |
| 645 | ret = access_process_vm(child, addr, &data, sizeof(data), 1); |
| 646 | if (ret == sizeof(data)) { |
| 647 | ret = 0; |
| 648 | if (request == PTRACE_POKETEXT) { |
| 649 | invalidate_cache(); |
| 650 | } |
| 651 | } else { |
| 652 | ret = -EIO; |
| 653 | } |
| 654 | break; |
| 655 | |
| 656 | /* |
| 657 | * write the word at location addr in the USER area. |
| 658 | */ |
| 659 | case PTRACE_POKEUSR: |
| 660 | ret = ptrace_write_user(child, addr, data); |
| 661 | break; |
| 662 | |
| 663 | /* |
| 664 | * continue/restart and stop at next (return from) syscall |
| 665 | */ |
| 666 | case PTRACE_SYSCALL: |
| 667 | case PTRACE_CONT: |
| 668 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 669 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | break; |
| 671 | if (request == PTRACE_SYSCALL) |
| 672 | set_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 673 | else |
| 674 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 675 | child->exit_code = data; |
| 676 | wake_up_process(child); |
| 677 | ret = 0; |
| 678 | break; |
| 679 | |
| 680 | /* |
| 681 | * make the child exit. Best I can do is send it a sigkill. |
| 682 | * perhaps it should be put in the status that it wants to |
| 683 | * exit. |
| 684 | */ |
| 685 | case PTRACE_KILL: { |
| 686 | ret = 0; |
| 687 | unregister_all_debug_traps(child); |
| 688 | invalidate_cache(); |
| 689 | if (child->exit_state == EXIT_ZOMBIE) /* already dead */ |
| 690 | break; |
| 691 | child->exit_code = SIGKILL; |
| 692 | wake_up_process(child); |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * execute single instruction. |
| 698 | */ |
| 699 | case PTRACE_SINGLESTEP: { |
| 700 | unsigned long next_pc; |
| 701 | unsigned long pc, insn; |
| 702 | |
| 703 | ret = -EIO; |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 704 | if (!valid_signal(data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | break; |
| 706 | clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE); |
| 707 | if ((child->ptrace & PT_DTRACE) == 0) { |
| 708 | /* Spurious delayed TF traps may occur */ |
| 709 | child->ptrace |= PT_DTRACE; |
| 710 | } |
| 711 | |
| 712 | /* Compute next pc. */ |
| 713 | pc = get_stack_long(child, PT_BPC); |
| 714 | |
| 715 | if (access_process_vm(child, pc&~3, &insn, sizeof(insn), 0) |
| 716 | != sizeof(insn)) |
| 717 | break; |
| 718 | |
| 719 | compute_next_pc(insn, pc, &next_pc, child); |
| 720 | if (next_pc & 0x80000000) |
| 721 | break; |
| 722 | |
| 723 | if (embed_debug_trap(child, next_pc)) |
| 724 | break; |
| 725 | |
| 726 | invalidate_cache(); |
| 727 | child->exit_code = data; |
| 728 | |
| 729 | /* give it a chance to run. */ |
| 730 | wake_up_process(child); |
| 731 | ret = 0; |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * detach a process that was attached. |
| 737 | */ |
| 738 | case PTRACE_DETACH: |
| 739 | ret = 0; |
| 740 | ret = ptrace_detach(child, data); |
| 741 | break; |
| 742 | |
| 743 | case PTRACE_GETREGS: |
| 744 | ret = ptrace_getregs(child, (void __user *)data); |
| 745 | break; |
| 746 | |
| 747 | case PTRACE_SETREGS: |
| 748 | ret = ptrace_setregs(child, (void __user *)data); |
| 749 | break; |
| 750 | |
| 751 | default: |
| 752 | ret = ptrace_request(child, request, addr, data); |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | return ret; |
| 757 | } |
| 758 | |
Christoph Hellwig | dfb7dac | 2005-10-30 15:02:22 -0800 | [diff] [blame] | 759 | asmlinkage long sys_ptrace(long request, long pid, long addr, long data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | { |
| 761 | struct task_struct *child; |
| 762 | int ret; |
| 763 | |
| 764 | lock_kernel(); |
| 765 | ret = -EPERM; |
| 766 | if (request == PTRACE_TRACEME) { |
| 767 | /* are we already being traced? */ |
| 768 | if (current->ptrace & PT_PTRACED) |
| 769 | goto out; |
| 770 | /* set the ptrace bit in the process flags. */ |
| 771 | current->ptrace |= PT_PTRACED; |
| 772 | ret = 0; |
| 773 | goto out; |
| 774 | } |
| 775 | ret = -ESRCH; |
| 776 | read_lock(&tasklist_lock); |
| 777 | child = find_task_by_pid(pid); |
| 778 | if (child) |
| 779 | get_task_struct(child); |
| 780 | read_unlock(&tasklist_lock); |
| 781 | if (!child) |
| 782 | goto out; |
| 783 | |
| 784 | ret = -EPERM; |
| 785 | if (pid == 1) /* you may not mess with init */ |
| 786 | goto out; |
| 787 | |
| 788 | if (request == PTRACE_ATTACH) { |
| 789 | ret = ptrace_attach(child); |
| 790 | if (ret == 0) |
| 791 | init_debug_traps(child); |
| 792 | goto out_tsk; |
| 793 | } |
| 794 | |
| 795 | ret = ptrace_check_attach(child, request == PTRACE_KILL); |
| 796 | if (ret == 0) |
| 797 | ret = do_ptrace(request, child, addr, data); |
| 798 | |
| 799 | out_tsk: |
| 800 | put_task_struct(child); |
| 801 | out: |
| 802 | unlock_kernel(); |
| 803 | |
| 804 | return ret; |
| 805 | } |
| 806 | |
| 807 | /* notification of system call entry/exit |
| 808 | * - triggered by current->work.syscall_trace |
| 809 | */ |
| 810 | void do_syscall_trace(void) |
| 811 | { |
| 812 | if (!test_thread_flag(TIF_SYSCALL_TRACE)) |
| 813 | return; |
| 814 | if (!(current->ptrace & PT_PTRACED)) |
| 815 | return; |
| 816 | /* the 0x80 provides a way for the tracing parent to distinguish |
| 817 | between a syscall stop and SIGTRAP delivery */ |
| 818 | ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) |
| 819 | ? 0x80 : 0)); |
| 820 | |
| 821 | /* |
| 822 | * this isn't the same as continuing with a signal, but it will do |
| 823 | * for normal use. strace only continues with a signal if the |
| 824 | * stopping signal is not SIGTRAP. -brl |
| 825 | */ |
| 826 | if (current->exit_code) { |
| 827 | send_sig(current->exit_code, current, 1); |
| 828 | current->exit_code = 0; |
| 829 | } |
| 830 | } |