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