PageRenderTime 93ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/arch/m32r/kernel/ptrace.c

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