ptrace.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * linux/arch/m68k/kernel/ptrace.c
  3. *
  4. * Copyright (C) 1994 by Hamish Macdonald
  5. * Taken from linux/kernel/ptrace.c and modified for M680x0.
  6. * linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
  7. *
  8. * This file is subject to the terms and conditions of the GNU General
  9. * Public License. See the file COPYING in the main directory of
  10. * this archive for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/user.h>
  20. #include <linux/signal.h>
  21. #include <linux/regset.h>
  22. #include <linux/elf.h>
  23. #include <linux/uaccess.h>
  24. #include <asm/page.h>
  25. #include <asm/processor.h>
  26. /*
  27. * does not yet catch signals sent when the child dies.
  28. * in exit.c or in signal.c.
  29. */
  30. /* determines which bits in the SR the user has access to. */
  31. /* 1 = access 0 = no access */
  32. #define SR_MASK 0x001f
  33. /* sets the trace bits. */
  34. #define TRACE_BITS 0xC000
  35. #define T1_BIT 0x8000
  36. #define T0_BIT 0x4000
  37. /* Find the stack offset for a register, relative to thread.esp0. */
  38. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  39. #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
  40. - sizeof(struct switch_stack))
  41. /* Mapping from PT_xxx to the stack offset at which the register is
  42. saved. Notice that usp has no stack-slot and needs to be treated
  43. specially (see get_reg/put_reg below). */
  44. static const int regoff[] = {
  45. [0] = PT_REG(d1),
  46. [1] = PT_REG(d2),
  47. [2] = PT_REG(d3),
  48. [3] = PT_REG(d4),
  49. [4] = PT_REG(d5),
  50. [5] = SW_REG(d6),
  51. [6] = SW_REG(d7),
  52. [7] = PT_REG(a0),
  53. [8] = PT_REG(a1),
  54. [9] = PT_REG(a2),
  55. [10] = SW_REG(a3),
  56. [11] = SW_REG(a4),
  57. [12] = SW_REG(a5),
  58. [13] = SW_REG(a6),
  59. [14] = PT_REG(d0),
  60. [15] = -1,
  61. [16] = PT_REG(orig_d0),
  62. [17] = PT_REG(sr),
  63. [18] = PT_REG(pc),
  64. };
  65. /*
  66. * Get contents of register REGNO in task TASK.
  67. */
  68. static inline long get_reg(struct task_struct *task, int regno)
  69. {
  70. unsigned long *addr;
  71. if (regno == PT_USP)
  72. addr = &task->thread.usp;
  73. else if (regno < ARRAY_SIZE(regoff))
  74. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  75. else
  76. return 0;
  77. /* Need to take stkadj into account. */
  78. if (regno == PT_SR || regno == PT_PC) {
  79. long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
  80. addr = (unsigned long *) ((unsigned long)addr + stkadj);
  81. /* The sr is actually a 16 bit register. */
  82. if (regno == PT_SR)
  83. return *(unsigned short *)addr;
  84. }
  85. return *addr;
  86. }
  87. /*
  88. * Write contents of register REGNO in task TASK.
  89. */
  90. static inline int put_reg(struct task_struct *task, int regno,
  91. unsigned long data)
  92. {
  93. unsigned long *addr;
  94. if (regno == PT_USP)
  95. addr = &task->thread.usp;
  96. else if (regno < ARRAY_SIZE(regoff))
  97. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  98. else
  99. return -1;
  100. /* Need to take stkadj into account. */
  101. if (regno == PT_SR || regno == PT_PC) {
  102. long stkadj = *(long *)(task->thread.esp0 + PT_REG(stkadj));
  103. addr = (unsigned long *) ((unsigned long)addr + stkadj);
  104. /* The sr is actually a 16 bit register. */
  105. if (regno == PT_SR) {
  106. *(unsigned short *)addr = data;
  107. return 0;
  108. }
  109. }
  110. *addr = data;
  111. return 0;
  112. }
  113. /*
  114. * Make sure the single step bit is not set.
  115. */
  116. static inline void singlestep_disable(struct task_struct *child)
  117. {
  118. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  119. put_reg(child, PT_SR, tmp);
  120. clear_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  121. }
  122. /*
  123. * Called by kernel/ptrace.c when detaching..
  124. */
  125. void ptrace_disable(struct task_struct *child)
  126. {
  127. singlestep_disable(child);
  128. }
  129. void user_enable_single_step(struct task_struct *child)
  130. {
  131. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  132. put_reg(child, PT_SR, tmp | T1_BIT);
  133. set_tsk_thread_flag(child, TIF_DELAYED_TRACE);
  134. }
  135. #ifdef CONFIG_MMU
  136. void user_enable_block_step(struct task_struct *child)
  137. {
  138. unsigned long tmp = get_reg(child, PT_SR) & ~TRACE_BITS;
  139. put_reg(child, PT_SR, tmp | T0_BIT);
  140. }
  141. #endif
  142. void user_disable_single_step(struct task_struct *child)
  143. {
  144. singlestep_disable(child);
  145. }
  146. long arch_ptrace(struct task_struct *child, long request,
  147. unsigned long addr, unsigned long data)
  148. {
  149. unsigned long tmp;
  150. int i, ret = 0;
  151. int regno = addr >> 2; /* temporary hack. */
  152. unsigned long __user *datap = (unsigned long __user *) data;
  153. switch (request) {
  154. /* read the word at location addr in the USER area. */
  155. case PTRACE_PEEKUSR:
  156. if (addr & 3)
  157. goto out_eio;
  158. if (regno >= 0 && regno < 19) {
  159. tmp = get_reg(child, regno);
  160. } else if (regno >= 21 && regno < 49) {
  161. tmp = child->thread.fp[regno - 21];
  162. /* Convert internal fpu reg representation
  163. * into long double format
  164. */
  165. if (FPU_IS_EMU && (regno < 45) && !(regno % 3))
  166. tmp = ((tmp & 0xffff0000) << 15) |
  167. ((tmp & 0x0000ffff) << 16);
  168. #ifndef CONFIG_MMU
  169. } else if (regno == 49) {
  170. tmp = child->mm->start_code;
  171. } else if (regno == 50) {
  172. tmp = child->mm->start_data;
  173. } else if (regno == 51) {
  174. tmp = child->mm->end_code;
  175. #endif
  176. } else
  177. goto out_eio;
  178. ret = put_user(tmp, datap);
  179. break;
  180. case PTRACE_POKEUSR:
  181. /* write the word at location addr in the USER area */
  182. if (addr & 3)
  183. goto out_eio;
  184. if (regno == PT_SR) {
  185. data &= SR_MASK;
  186. data |= get_reg(child, PT_SR) & ~SR_MASK;
  187. }
  188. if (regno >= 0 && regno < 19) {
  189. if (put_reg(child, regno, data))
  190. goto out_eio;
  191. } else if (regno >= 21 && regno < 48) {
  192. /* Convert long double format
  193. * into internal fpu reg representation
  194. */
  195. if (FPU_IS_EMU && (regno < 45) && !(regno % 3)) {
  196. data <<= 15;
  197. data = (data & 0xffff0000) |
  198. ((data & 0x0000ffff) >> 1);
  199. }
  200. child->thread.fp[regno - 21] = data;
  201. } else
  202. goto out_eio;
  203. break;
  204. case PTRACE_GETREGS: /* Get all gp regs from the child. */
  205. for (i = 0; i < 19; i++) {
  206. tmp = get_reg(child, i);
  207. ret = put_user(tmp, datap);
  208. if (ret)
  209. break;
  210. datap++;
  211. }
  212. break;
  213. case PTRACE_SETREGS: /* Set all gp regs in the child. */
  214. for (i = 0; i < 19; i++) {
  215. ret = get_user(tmp, datap);
  216. if (ret)
  217. break;
  218. if (i == PT_SR) {
  219. tmp &= SR_MASK;
  220. tmp |= get_reg(child, PT_SR) & ~SR_MASK;
  221. }
  222. put_reg(child, i, tmp);
  223. datap++;
  224. }
  225. break;
  226. case PTRACE_GETFPREGS: /* Get the child FPU state. */
  227. if (copy_to_user(datap, &child->thread.fp,
  228. sizeof(struct user_m68kfp_struct)))
  229. ret = -EFAULT;
  230. break;
  231. case PTRACE_SETFPREGS: /* Set the child FPU state. */
  232. if (copy_from_user(&child->thread.fp, datap,
  233. sizeof(struct user_m68kfp_struct)))
  234. ret = -EFAULT;
  235. break;
  236. case PTRACE_GET_THREAD_AREA:
  237. ret = put_user(task_thread_info(child)->tp_value, datap);
  238. break;
  239. default:
  240. ret = ptrace_request(child, request, addr, data);
  241. break;
  242. }
  243. return ret;
  244. out_eio:
  245. return -EIO;
  246. }
  247. asmlinkage int syscall_trace_enter(void)
  248. {
  249. int ret = 0;
  250. if (test_thread_flag(TIF_SYSCALL_TRACE))
  251. ret = ptrace_report_syscall_entry(task_pt_regs(current));
  252. return ret;
  253. }
  254. asmlinkage void syscall_trace_leave(void)
  255. {
  256. if (test_thread_flag(TIF_SYSCALL_TRACE))
  257. ptrace_report_syscall_exit(task_pt_regs(current), 0);
  258. }
  259. #if defined(CONFIG_BINFMT_ELF_FDPIC) && defined(CONFIG_ELF_CORE)
  260. /*
  261. * Currently the only thing that needs to use regsets for m68k is the
  262. * coredump support of the elf_fdpic loader. Implement the minimum
  263. * definitions required for that.
  264. */
  265. static int m68k_regset_get(struct task_struct *target,
  266. const struct user_regset *regset,
  267. struct membuf to)
  268. {
  269. struct pt_regs *ptregs = task_pt_regs(target);
  270. u32 uregs[ELF_NGREG];
  271. ELF_CORE_COPY_REGS(uregs, ptregs);
  272. return membuf_write(&to, uregs, sizeof(uregs));
  273. }
  274. enum m68k_regset {
  275. REGSET_GPR,
  276. #ifdef CONFIG_FPU
  277. REGSET_FPU,
  278. #endif
  279. };
  280. static const struct user_regset m68k_user_regsets[] = {
  281. [REGSET_GPR] = {
  282. .core_note_type = NT_PRSTATUS,
  283. .n = ELF_NGREG,
  284. .size = sizeof(u32),
  285. .align = sizeof(u16),
  286. .regset_get = m68k_regset_get,
  287. },
  288. #ifdef CONFIG_FPU
  289. [REGSET_FPU] = {
  290. .core_note_type = NT_PRFPREG,
  291. .n = sizeof(struct user_m68kfp_struct) / sizeof(u32),
  292. .size = sizeof(u32),
  293. .align = sizeof(u32),
  294. }
  295. #endif /* CONFIG_FPU */
  296. };
  297. static const struct user_regset_view user_m68k_view = {
  298. .name = "m68k",
  299. .e_machine = EM_68K,
  300. .ei_osabi = ELF_OSABI,
  301. .regsets = m68k_user_regsets,
  302. .n = ARRAY_SIZE(m68k_user_regsets)
  303. };
  304. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  305. {
  306. return &user_m68k_view;
  307. }
  308. #endif /* CONFIG_BINFMT_ELF_FDPIC && CONFIG_ELF_CORE */