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