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