ptrace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  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. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/mm.h>
  19. #include <linux/err.h>
  20. #include <linux/smp.h>
  21. #include <linux/errno.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/user.h>
  24. #include <linux/string.h>
  25. #include <linux/signal.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/io.h>
  28. #include <asm/uaccess.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/processor.h>
  31. #include <asm/mmu_context.h>
  32. /*
  33. * This routine will get a word off of the process kernel stack.
  34. */
  35. static inline unsigned long int
  36. get_stack_long(struct task_struct *task, int offset)
  37. {
  38. unsigned long *stack;
  39. stack = (unsigned long *)task_pt_regs(task);
  40. return stack[offset];
  41. }
  42. /*
  43. * This routine will put a word on the process kernel stack.
  44. */
  45. static inline int
  46. put_stack_long(struct task_struct *task, int offset, unsigned long data)
  47. {
  48. unsigned long *stack;
  49. stack = (unsigned long *)task_pt_regs(task);
  50. stack[offset] = data;
  51. return 0;
  52. }
  53. static int reg_offset[] = {
  54. PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5, PT_R6, PT_R7,
  55. PT_R8, PT_R9, PT_R10, PT_R11, PT_R12, PT_FP, PT_LR, PT_SPU,
  56. };
  57. /*
  58. * Read the word at offset "off" into the "struct user". We
  59. * actually access the pt_regs stored on the kernel stack.
  60. */
  61. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  62. unsigned long __user *data)
  63. {
  64. unsigned long tmp;
  65. #ifndef NO_FPU
  66. struct user * dummy = NULL;
  67. #endif
  68. if ((off & 3) || off > sizeof(struct user) - 3)
  69. return -EIO;
  70. off >>= 2;
  71. switch (off) {
  72. case PT_EVB:
  73. __asm__ __volatile__ (
  74. "mvfc %0, cr5 \n\t"
  75. : "=r" (tmp)
  76. );
  77. break;
  78. case PT_CBR: {
  79. unsigned long psw;
  80. psw = get_stack_long(tsk, PT_PSW);
  81. tmp = ((psw >> 8) & 1);
  82. }
  83. break;
  84. case PT_PSW: {
  85. unsigned long psw, bbpsw;
  86. psw = get_stack_long(tsk, PT_PSW);
  87. bbpsw = get_stack_long(tsk, PT_BBPSW);
  88. tmp = ((psw >> 8) & 0xff) | ((bbpsw & 0xff) << 8);
  89. }
  90. break;
  91. case PT_PC:
  92. tmp = get_stack_long(tsk, PT_BPC);
  93. break;
  94. case PT_BPC:
  95. off = PT_BBPC;
  96. /* fall through */
  97. default:
  98. if (off < (sizeof(struct pt_regs) >> 2))
  99. tmp = get_stack_long(tsk, off);
  100. #ifndef NO_FPU
  101. else if (off >= (long)(&dummy->fpu >> 2) &&
  102. off < (long)(&dummy->u_fpvalid >> 2)) {
  103. if (!tsk_used_math(tsk)) {
  104. if (off == (long)(&dummy->fpu.fpscr >> 2))
  105. tmp = FPSCR_INIT;
  106. else
  107. tmp = 0;
  108. } else
  109. tmp = ((long *)(&tsk->thread.fpu >> 2))
  110. [off - (long)&dummy->fpu];
  111. } else if (off == (long)(&dummy->u_fpvalid >> 2))
  112. tmp = !!tsk_used_math(tsk);
  113. #endif /* not NO_FPU */
  114. else
  115. tmp = 0;
  116. }
  117. return put_user(tmp, data);
  118. }
  119. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  120. unsigned long data)
  121. {
  122. int ret = -EIO;
  123. #ifndef NO_FPU
  124. struct user * dummy = NULL;
  125. #endif
  126. if ((off & 3) || off > sizeof(struct user) - 3)
  127. return -EIO;
  128. off >>= 2;
  129. switch (off) {
  130. case PT_EVB:
  131. case PT_BPC:
  132. case PT_SPI:
  133. /* We don't allow to modify evb. */
  134. ret = 0;
  135. break;
  136. case PT_PSW:
  137. case PT_CBR: {
  138. /* We allow to modify only cbr in psw */
  139. unsigned long psw;
  140. psw = get_stack_long(tsk, PT_PSW);
  141. psw = (psw & ~0x100) | ((data & 1) << 8);
  142. ret = put_stack_long(tsk, PT_PSW, psw);
  143. }
  144. break;
  145. case PT_PC:
  146. off = PT_BPC;
  147. data &= ~1;
  148. /* fall through */
  149. default:
  150. if (off < (sizeof(struct pt_regs) >> 2))
  151. ret = put_stack_long(tsk, off, data);
  152. #ifndef NO_FPU
  153. else if (off >= (long)(&dummy->fpu >> 2) &&
  154. off < (long)(&dummy->u_fpvalid >> 2)) {
  155. set_stopped_child_used_math(tsk);
  156. ((long *)&tsk->thread.fpu)
  157. [off - (long)&dummy->fpu] = data;
  158. ret = 0;
  159. } else if (off == (long)(&dummy->u_fpvalid >> 2)) {
  160. conditional_stopped_child_used_math(data, tsk);
  161. ret = 0;
  162. }
  163. #endif /* not NO_FPU */
  164. break;
  165. }
  166. return ret;
  167. }
  168. /*
  169. * Get all user integer registers.
  170. */
  171. static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  172. {
  173. struct pt_regs *regs = task_pt_regs(tsk);
  174. return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  175. }
  176. /*
  177. * Set all user integer registers.
  178. */
  179. static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
  180. {
  181. struct pt_regs newregs;
  182. int ret;
  183. ret = -EFAULT;
  184. if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
  185. struct pt_regs *regs = task_pt_regs(tsk);
  186. *regs = newregs;
  187. ret = 0;
  188. }
  189. return ret;
  190. }
  191. static inline int
  192. check_condition_bit(struct task_struct *child)
  193. {
  194. return (int)((get_stack_long(child, PT_PSW) >> 8) & 1);
  195. }
  196. static int
  197. check_condition_src(unsigned long op, unsigned long regno1,
  198. unsigned long regno2, struct task_struct *child)
  199. {
  200. unsigned long reg1, reg2;
  201. reg2 = get_stack_long(child, reg_offset[regno2]);
  202. switch (op) {
  203. case 0x0: /* BEQ */
  204. reg1 = get_stack_long(child, reg_offset[regno1]);
  205. return reg1 == reg2;
  206. case 0x1: /* BNE */
  207. reg1 = get_stack_long(child, reg_offset[regno1]);
  208. return reg1 != reg2;
  209. case 0x8: /* BEQZ */
  210. return reg2 == 0;
  211. case 0x9: /* BNEZ */
  212. return reg2 != 0;
  213. case 0xa: /* BLTZ */
  214. return (int)reg2 < 0;
  215. case 0xb: /* BGEZ */
  216. return (int)reg2 >= 0;
  217. case 0xc: /* BLEZ */
  218. return (int)reg2 <= 0;
  219. case 0xd: /* BGTZ */
  220. return (int)reg2 > 0;
  221. default:
  222. /* never reached */
  223. return 0;
  224. }
  225. }
  226. static void
  227. compute_next_pc_for_16bit_insn(unsigned long insn, unsigned long pc,
  228. unsigned long *next_pc,
  229. struct task_struct *child)
  230. {
  231. unsigned long op, op2, op3;
  232. unsigned long disp;
  233. unsigned long regno;
  234. int parallel = 0;
  235. if (insn & 0x00008000)
  236. parallel = 1;
  237. if (pc & 3)
  238. insn &= 0x7fff; /* right slot */
  239. else
  240. insn >>= 16; /* left slot */
  241. op = (insn >> 12) & 0xf;
  242. op2 = (insn >> 8) & 0xf;
  243. op3 = (insn >> 4) & 0xf;
  244. if (op == 0x7) {
  245. switch (op2) {
  246. case 0xd: /* BNC */
  247. case 0x9: /* BNCL */
  248. if (!check_condition_bit(child)) {
  249. disp = (long)(insn << 24) >> 22;
  250. *next_pc = (pc & ~0x3) + disp;
  251. return;
  252. }
  253. break;
  254. case 0x8: /* BCL */
  255. case 0xc: /* BC */
  256. if (check_condition_bit(child)) {
  257. disp = (long)(insn << 24) >> 22;
  258. *next_pc = (pc & ~0x3) + disp;
  259. return;
  260. }
  261. break;
  262. case 0xe: /* BL */
  263. case 0xf: /* BRA */
  264. disp = (long)(insn << 24) >> 22;
  265. *next_pc = (pc & ~0x3) + disp;
  266. return;
  267. break;
  268. }
  269. } else if (op == 0x1) {
  270. switch (op2) {
  271. case 0x0:
  272. if (op3 == 0xf) { /* TRAP */
  273. #if 1
  274. /* pass through */
  275. #else
  276. /* kernel space is not allowed as next_pc */
  277. unsigned long evb;
  278. unsigned long trapno;
  279. trapno = insn & 0xf;
  280. __asm__ __volatile__ (
  281. "mvfc %0, cr5\n"
  282. :"=r"(evb)
  283. :
  284. );
  285. *next_pc = evb + (trapno << 2);
  286. return;
  287. #endif
  288. } else if (op3 == 0xd) { /* RTE */
  289. *next_pc = get_stack_long(child, PT_BPC);
  290. return;
  291. }
  292. break;
  293. case 0xc: /* JC */
  294. if (op3 == 0xc && check_condition_bit(child)) {
  295. regno = insn & 0xf;
  296. *next_pc = get_stack_long(child,
  297. reg_offset[regno]);
  298. return;
  299. }
  300. break;
  301. case 0xd: /* JNC */
  302. if (op3 == 0xc && !check_condition_bit(child)) {
  303. regno = insn & 0xf;
  304. *next_pc = get_stack_long(child,
  305. reg_offset[regno]);
  306. return;
  307. }
  308. break;
  309. case 0xe: /* JL */
  310. case 0xf: /* JMP */
  311. if (op3 == 0xc) { /* JMP */
  312. regno = insn & 0xf;
  313. *next_pc = get_stack_long(child,
  314. reg_offset[regno]);
  315. return;
  316. }
  317. break;
  318. }
  319. }
  320. if (parallel)
  321. *next_pc = pc + 4;
  322. else
  323. *next_pc = pc + 2;
  324. }
  325. static void
  326. compute_next_pc_for_32bit_insn(unsigned long insn, unsigned long pc,
  327. unsigned long *next_pc,
  328. struct task_struct *child)
  329. {
  330. unsigned long op;
  331. unsigned long op2;
  332. unsigned long disp;
  333. unsigned long regno1, regno2;
  334. op = (insn >> 28) & 0xf;
  335. if (op == 0xf) { /* branch 24-bit relative */
  336. op2 = (insn >> 24) & 0xf;
  337. switch (op2) {
  338. case 0xd: /* BNC */
  339. case 0x9: /* BNCL */
  340. if (!check_condition_bit(child)) {
  341. disp = (long)(insn << 8) >> 6;
  342. *next_pc = (pc & ~0x3) + disp;
  343. return;
  344. }
  345. break;
  346. case 0x8: /* BCL */
  347. case 0xc: /* BC */
  348. if (check_condition_bit(child)) {
  349. disp = (long)(insn << 8) >> 6;
  350. *next_pc = (pc & ~0x3) + disp;
  351. return;
  352. }
  353. break;
  354. case 0xe: /* BL */
  355. case 0xf: /* BRA */
  356. disp = (long)(insn << 8) >> 6;
  357. *next_pc = (pc & ~0x3) + disp;
  358. return;
  359. }
  360. } else if (op == 0xb) { /* branch 16-bit relative */
  361. op2 = (insn >> 20) & 0xf;
  362. switch (op2) {
  363. case 0x0: /* BEQ */
  364. case 0x1: /* BNE */
  365. case 0x8: /* BEQZ */
  366. case 0x9: /* BNEZ */
  367. case 0xa: /* BLTZ */
  368. case 0xb: /* BGEZ */
  369. case 0xc: /* BLEZ */
  370. case 0xd: /* BGTZ */
  371. regno1 = ((insn >> 24) & 0xf);
  372. regno2 = ((insn >> 16) & 0xf);
  373. if (check_condition_src(op2, regno1, regno2, child)) {
  374. disp = (long)(insn << 16) >> 14;
  375. *next_pc = (pc & ~0x3) + disp;
  376. return;
  377. }
  378. break;
  379. }
  380. }
  381. *next_pc = pc + 4;
  382. }
  383. static inline void
  384. compute_next_pc(unsigned long insn, unsigned long pc,
  385. unsigned long *next_pc, struct task_struct *child)
  386. {
  387. if (insn & 0x80000000)
  388. compute_next_pc_for_32bit_insn(insn, pc, next_pc, child);
  389. else
  390. compute_next_pc_for_16bit_insn(insn, pc, next_pc, child);
  391. }
  392. static int
  393. register_debug_trap(struct task_struct *child, unsigned long next_pc,
  394. unsigned long next_insn, unsigned long *code)
  395. {
  396. struct debug_trap *p = &child->thread.debug_trap;
  397. unsigned long addr = next_pc & ~3;
  398. if (p->nr_trap == MAX_TRAPS) {
  399. printk("kernel BUG at %s %d: p->nr_trap = %d\n",
  400. __FILE__, __LINE__, p->nr_trap);
  401. return -1;
  402. }
  403. p->addr[p->nr_trap] = addr;
  404. p->insn[p->nr_trap] = next_insn;
  405. p->nr_trap++;
  406. if (next_pc & 3) {
  407. *code = (next_insn & 0xffff0000) | 0x10f1;
  408. /* xxx --> TRAP1 */
  409. } else {
  410. if ((next_insn & 0x80000000) || (next_insn & 0x8000)) {
  411. *code = 0x10f17000;
  412. /* TRAP1 --> NOP */
  413. } else {
  414. *code = (next_insn & 0xffff) | 0x10f10000;
  415. /* TRAP1 --> xxx */
  416. }
  417. }
  418. return 0;
  419. }
  420. static int
  421. unregister_debug_trap(struct task_struct *child, unsigned long addr,
  422. unsigned long *code)
  423. {
  424. struct debug_trap *p = &child->thread.debug_trap;
  425. int i;
  426. /* Search debug trap entry. */
  427. for (i = 0; i < p->nr_trap; i++) {
  428. if (p->addr[i] == addr)
  429. break;
  430. }
  431. if (i >= p->nr_trap) {
  432. /* The trap may be requested from debugger.
  433. * ptrace should do nothing in this case.
  434. */
  435. return 0;
  436. }
  437. /* Recover original instruction code. */
  438. *code = p->insn[i];
  439. /* Shift debug trap entries. */
  440. while (i < p->nr_trap - 1) {
  441. p->insn[i] = p->insn[i + 1];
  442. p->addr[i] = p->addr[i + 1];
  443. i++;
  444. }
  445. p->nr_trap--;
  446. return 1;
  447. }
  448. static void
  449. unregister_all_debug_traps(struct task_struct *child)
  450. {
  451. struct debug_trap *p = &child->thread.debug_trap;
  452. int i;
  453. for (i = 0; i < p->nr_trap; i++)
  454. access_process_vm(child, p->addr[i], &p->insn[i], sizeof(p->insn[i]), 1);
  455. p->nr_trap = 0;
  456. }
  457. static inline void
  458. invalidate_cache(void)
  459. {
  460. #if defined(CONFIG_CHIP_M32700) || defined(CONFIG_CHIP_OPSP)
  461. _flush_cache_copyback_all();
  462. #else /* ! CONFIG_CHIP_M32700 */
  463. /* Invalidate cache */
  464. __asm__ __volatile__ (
  465. "ldi r0, #-1 \n\t"
  466. "ldi r1, #0 \n\t"
  467. "stb r1, @r0 ; cache off \n\t"
  468. "; \n\t"
  469. "ldi r0, #-2 \n\t"
  470. "ldi r1, #1 \n\t"
  471. "stb r1, @r0 ; cache invalidate \n\t"
  472. ".fillinsn \n"
  473. "0: \n\t"
  474. "ldb r1, @r0 ; invalidate check \n\t"
  475. "bnez r1, 0b \n\t"
  476. "; \n\t"
  477. "ldi r0, #-1 \n\t"
  478. "ldi r1, #1 \n\t"
  479. "stb r1, @r0 ; cache on \n\t"
  480. : : : "r0", "r1", "memory"
  481. );
  482. /* FIXME: copying-back d-cache and invalidating i-cache are needed.
  483. */
  484. #endif /* CONFIG_CHIP_M32700 */
  485. }
  486. /* Embed a debug trap (TRAP1) code */
  487. static int
  488. embed_debug_trap(struct task_struct *child, unsigned long next_pc)
  489. {
  490. unsigned long next_insn, code;
  491. unsigned long addr = next_pc & ~3;
  492. if (access_process_vm(child, addr, &next_insn, sizeof(next_insn), 0)
  493. != sizeof(next_insn)) {
  494. return -1; /* error */
  495. }
  496. /* Set a trap code. */
  497. if (register_debug_trap(child, next_pc, next_insn, &code)) {
  498. return -1; /* error */
  499. }
  500. if (access_process_vm(child, addr, &code, sizeof(code), 1)
  501. != sizeof(code)) {
  502. return -1; /* error */
  503. }
  504. return 0; /* success */
  505. }
  506. void
  507. withdraw_debug_trap(struct pt_regs *regs)
  508. {
  509. unsigned long addr;
  510. unsigned long code;
  511. addr = (regs->bpc - 2) & ~3;
  512. regs->bpc -= 2;
  513. if (unregister_debug_trap(current, addr, &code)) {
  514. access_process_vm(current, addr, &code, sizeof(code), 1);
  515. invalidate_cache();
  516. }
  517. }
  518. void
  519. init_debug_traps(struct task_struct *child)
  520. {
  521. struct debug_trap *p = &child->thread.debug_trap;
  522. int i;
  523. p->nr_trap = 0;
  524. for (i = 0; i < MAX_TRAPS; i++) {
  525. p->addr[i] = 0;
  526. p->insn[i] = 0;
  527. }
  528. }
  529. void user_enable_single_step(struct task_struct *child)
  530. {
  531. unsigned long next_pc;
  532. unsigned long pc, insn;
  533. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  534. /* Compute next pc. */
  535. pc = get_stack_long(child, PT_BPC);
  536. if (access_process_vm(child, pc&~3, &insn, sizeof(insn), 0)
  537. != sizeof(insn))
  538. return;
  539. compute_next_pc(insn, pc, &next_pc, child);
  540. if (next_pc & 0x80000000)
  541. return;
  542. if (embed_debug_trap(child, next_pc))
  543. return;
  544. invalidate_cache();
  545. }
  546. void user_disable_single_step(struct task_struct *child)
  547. {
  548. unregister_all_debug_traps(child);
  549. invalidate_cache();
  550. }
  551. /*
  552. * Called by kernel/ptrace.c when detaching..
  553. *
  554. * Make sure single step bits etc are not set.
  555. */
  556. void ptrace_disable(struct task_struct *child)
  557. {
  558. /* nothing to do.. */
  559. }
  560. long
  561. arch_ptrace(struct task_struct *child, long request,
  562. unsigned long addr, unsigned long data)
  563. {
  564. int ret;
  565. unsigned long __user *datap = (unsigned long __user *) data;
  566. switch (request) {
  567. /*
  568. * read word at location "addr" in the child process.
  569. */
  570. case PTRACE_PEEKTEXT:
  571. case PTRACE_PEEKDATA:
  572. ret = generic_ptrace_peekdata(child, addr, data);
  573. break;
  574. /*
  575. * read the word at location addr in the USER area.
  576. */
  577. case PTRACE_PEEKUSR:
  578. ret = ptrace_read_user(child, addr, datap);
  579. break;
  580. /*
  581. * write the word at location addr.
  582. */
  583. case PTRACE_POKETEXT:
  584. case PTRACE_POKEDATA:
  585. ret = generic_ptrace_pokedata(child, addr, data);
  586. if (ret == 0 && request == PTRACE_POKETEXT)
  587. invalidate_cache();
  588. break;
  589. /*
  590. * write the word at location addr in the USER area.
  591. */
  592. case PTRACE_POKEUSR:
  593. ret = ptrace_write_user(child, addr, data);
  594. break;
  595. case PTRACE_GETREGS:
  596. ret = ptrace_getregs(child, datap);
  597. break;
  598. case PTRACE_SETREGS:
  599. ret = ptrace_setregs(child, datap);
  600. break;
  601. default:
  602. ret = ptrace_request(child, request, addr, data);
  603. break;
  604. }
  605. return ret;
  606. }
  607. /* notification of system call entry/exit
  608. * - triggered by current->work.syscall_trace
  609. */
  610. void do_syscall_trace(void)
  611. {
  612. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  613. return;
  614. if (!(current->ptrace & PT_PTRACED))
  615. return;
  616. /* the 0x80 provides a way for the tracing parent to distinguish
  617. between a syscall stop and SIGTRAP delivery */
  618. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  619. ? 0x80 : 0));
  620. /*
  621. * this isn't the same as continuing with a signal, but it will do
  622. * for normal use. strace only continues with a signal if the
  623. * stopping signal is not SIGTRAP. -brl
  624. */
  625. if (current->exit_code) {
  626. send_sig(current->exit_code, current, 1);
  627. current->exit_code = 0;
  628. }
  629. }