ptrace.c 8.3 KB

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