ptrace.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * linux/arch/unicore32/kernel/ptrace.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * By Ross Biro 1/23/92
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/signal.h>
  17. #include <linux/uaccess.h>
  18. /*
  19. * this routine will get a word off of the processes privileged stack.
  20. * the offset is how far from the base addr as stored in the THREAD.
  21. * this routine assumes that all the privileged stacks are in our
  22. * data space.
  23. */
  24. static inline long get_user_reg(struct task_struct *task, int offset)
  25. {
  26. return task_pt_regs(task)->uregs[offset];
  27. }
  28. /*
  29. * this routine will put a word on the processes privileged stack.
  30. * the offset is how far from the base addr as stored in the THREAD.
  31. * this routine assumes that all the privileged stacks are in our
  32. * data space.
  33. */
  34. static inline int
  35. put_user_reg(struct task_struct *task, int offset, long data)
  36. {
  37. struct pt_regs newregs, *regs = task_pt_regs(task);
  38. int ret = -EINVAL;
  39. newregs = *regs;
  40. newregs.uregs[offset] = data;
  41. if (valid_user_regs(&newregs)) {
  42. regs->uregs[offset] = data;
  43. ret = 0;
  44. }
  45. return ret;
  46. }
  47. /*
  48. * Called by kernel/ptrace.c when detaching..
  49. */
  50. void ptrace_disable(struct task_struct *child)
  51. {
  52. }
  53. /*
  54. * We actually access the pt_regs stored on the kernel stack.
  55. */
  56. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  57. unsigned long __user *ret)
  58. {
  59. unsigned long tmp;
  60. tmp = 0;
  61. if (off < sizeof(struct pt_regs))
  62. tmp = get_user_reg(tsk, off >> 2);
  63. return put_user(tmp, ret);
  64. }
  65. /*
  66. * We actually access the pt_regs stored on the kernel stack.
  67. */
  68. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  69. unsigned long val)
  70. {
  71. if (off >= sizeof(struct pt_regs))
  72. return 0;
  73. return put_user_reg(tsk, off >> 2, val);
  74. }
  75. long arch_ptrace(struct task_struct *child, long request,
  76. unsigned long addr, unsigned long data)
  77. {
  78. int ret;
  79. unsigned long __user *datap = (unsigned long __user *) data;
  80. switch (request) {
  81. case PTRACE_PEEKUSR:
  82. ret = ptrace_read_user(child, addr, datap);
  83. break;
  84. case PTRACE_POKEUSR:
  85. ret = ptrace_write_user(child, addr, data);
  86. break;
  87. case PTRACE_GET_THREAD_AREA:
  88. ret = put_user(task_pt_regs(child)->UCreg_16,
  89. datap);
  90. break;
  91. default:
  92. ret = ptrace_request(child, request, addr, data);
  93. break;
  94. }
  95. return ret;
  96. }
  97. asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
  98. {
  99. unsigned long ip;
  100. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  101. return scno;
  102. if (!(current->ptrace & PT_PTRACED))
  103. return scno;
  104. /*
  105. * Save IP. IP is used to denote syscall entry/exit:
  106. * IP = 0 -> entry, = 1 -> exit
  107. */
  108. ip = regs->UCreg_ip;
  109. regs->UCreg_ip = why;
  110. current_thread_info()->syscall = scno;
  111. /* the 0x80 provides a way for the tracing parent to distinguish
  112. between a syscall stop and SIGTRAP delivery */
  113. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  114. ? 0x80 : 0));
  115. /*
  116. * this isn't the same as continuing with a signal, but it will do
  117. * for normal use. strace only continues with a signal if the
  118. * stopping signal is not SIGTRAP. -brl
  119. */
  120. if (current->exit_code) {
  121. send_sig(current->exit_code, current, 1);
  122. current->exit_code = 0;
  123. }
  124. regs->UCreg_ip = ip;
  125. return current_thread_info()->syscall;
  126. }