ptrace.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * linux/arch/m68knommu/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/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/user.h>
  20. #include <linux/config.h>
  21. #include <linux/signal.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/page.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/system.h>
  26. #include <asm/processor.h>
  27. /*
  28. * does not yet catch signals sent when the child dies.
  29. * in exit.c or in signal.c.
  30. */
  31. /* determines which bits in the SR the user has access to. */
  32. /* 1 = access 0 = no access */
  33. #define SR_MASK 0x001f
  34. /* sets the trace bits. */
  35. #define TRACE_BITS 0x8000
  36. /* Find the stack offset for a register, relative to thread.esp0. */
  37. #define PT_REG(reg) ((long)&((struct pt_regs *)0)->reg)
  38. #define SW_REG(reg) ((long)&((struct switch_stack *)0)->reg \
  39. - sizeof(struct switch_stack))
  40. /* Mapping from PT_xxx to the stack offset at which the register is
  41. saved. Notice that usp has no stack-slot and needs to be treated
  42. specially (see get_reg/put_reg below). */
  43. static int regoff[] = {
  44. PT_REG(d1), PT_REG(d2), PT_REG(d3), PT_REG(d4),
  45. PT_REG(d5), SW_REG(d6), SW_REG(d7), PT_REG(a0),
  46. PT_REG(a1), PT_REG(a2), SW_REG(a3), SW_REG(a4),
  47. SW_REG(a5), SW_REG(a6), PT_REG(d0), -1,
  48. PT_REG(orig_d0), PT_REG(sr), PT_REG(pc),
  49. };
  50. /*
  51. * Get contents of register REGNO in task TASK.
  52. */
  53. static inline long get_reg(struct task_struct *task, int regno)
  54. {
  55. unsigned long *addr;
  56. if (regno == PT_USP)
  57. addr = &task->thread.usp;
  58. else if (regno < sizeof(regoff)/sizeof(regoff[0]))
  59. addr = (unsigned long *)(task->thread.esp0 + regoff[regno]);
  60. else
  61. return 0;
  62. return *addr;
  63. }
  64. /*
  65. * Write contents of register REGNO in task TASK.
  66. */
  67. static inline int put_reg(struct task_struct *task, int regno,
  68. unsigned long data)
  69. {
  70. unsigned long *addr;
  71. if (regno == PT_USP)
  72. addr = &task->thread.usp;
  73. else if (regno < sizeof(regoff)/sizeof(regoff[0]))
  74. addr = (unsigned long *) (task->thread.esp0 + regoff[regno]);
  75. else
  76. return -1;
  77. *addr = data;
  78. return 0;
  79. }
  80. /*
  81. * Called by kernel/ptrace.c when detaching..
  82. *
  83. * Make sure the single step bit is not set.
  84. */
  85. void ptrace_disable(struct task_struct *child)
  86. {
  87. unsigned long tmp;
  88. /* make sure the single step bit is not set. */
  89. tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
  90. put_reg(child, PT_SR, tmp);
  91. }
  92. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  93. {
  94. struct task_struct *child;
  95. int ret;
  96. lock_kernel();
  97. ret = -EPERM;
  98. if (request == PTRACE_TRACEME) {
  99. /* are we already being traced? */
  100. if (current->ptrace & PT_PTRACED)
  101. goto out;
  102. /* set the ptrace bit in the process flags. */
  103. current->ptrace |= PT_PTRACED;
  104. ret = 0;
  105. goto out;
  106. }
  107. ret = -ESRCH;
  108. read_lock(&tasklist_lock);
  109. child = find_task_by_pid(pid);
  110. if (child)
  111. get_task_struct(child);
  112. read_unlock(&tasklist_lock);
  113. if (!child)
  114. goto out;
  115. ret = -EPERM;
  116. if (pid == 1) /* you may not mess with init */
  117. goto out_tsk;
  118. if (request == PTRACE_ATTACH) {
  119. ret = ptrace_attach(child);
  120. goto out_tsk;
  121. }
  122. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  123. if (ret < 0)
  124. goto out_tsk;
  125. switch (request) {
  126. /* when I and D space are separate, these will need to be fixed. */
  127. case PTRACE_PEEKTEXT: /* read word at location addr. */
  128. case PTRACE_PEEKDATA: {
  129. unsigned long tmp;
  130. int copied;
  131. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  132. ret = -EIO;
  133. if (copied != sizeof(tmp))
  134. break;
  135. ret = put_user(tmp,(unsigned long *) data);
  136. break;
  137. }
  138. /* read the word at location addr in the USER area. */
  139. case PTRACE_PEEKUSR: {
  140. unsigned long tmp;
  141. ret = -EIO;
  142. if ((addr & 3) || addr < 0 ||
  143. addr > sizeof(struct user) - 3)
  144. break;
  145. tmp = 0; /* Default return condition */
  146. addr = addr >> 2; /* temporary hack. */
  147. ret = -EIO;
  148. if (addr < 19) {
  149. tmp = get_reg(child, addr);
  150. if (addr == PT_SR)
  151. tmp >>= 16;
  152. } else if (addr >= 21 && addr < 49) {
  153. tmp = child->thread.fp[addr - 21];
  154. #ifdef CONFIG_M68KFPU_EMU
  155. /* Convert internal fpu reg representation
  156. * into long double format
  157. */
  158. if (FPU_IS_EMU && (addr < 45) && !(addr % 3))
  159. tmp = ((tmp & 0xffff0000) << 15) |
  160. ((tmp & 0x0000ffff) << 16);
  161. #endif
  162. } else if (addr == 49) {
  163. tmp = child->mm->start_code;
  164. } else if (addr == 50) {
  165. tmp = child->mm->start_data;
  166. } else if (addr == 51) {
  167. tmp = child->mm->end_code;
  168. } else
  169. break;
  170. ret = put_user(tmp,(unsigned long *) data);
  171. break;
  172. }
  173. /* when I and D space are separate, this will have to be fixed. */
  174. case PTRACE_POKETEXT: /* write the word at location addr. */
  175. case PTRACE_POKEDATA:
  176. ret = 0;
  177. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  178. break;
  179. ret = -EIO;
  180. break;
  181. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  182. ret = -EIO;
  183. if ((addr & 3) || addr < 0 ||
  184. addr > sizeof(struct user) - 3)
  185. break;
  186. addr = addr >> 2; /* temporary hack. */
  187. if (addr == PT_SR) {
  188. data &= SR_MASK;
  189. data <<= 16;
  190. data |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  191. }
  192. if (addr < 19) {
  193. if (put_reg(child, addr, data))
  194. break;
  195. ret = 0;
  196. break;
  197. }
  198. if (addr >= 21 && addr < 48)
  199. {
  200. #ifdef CONFIG_M68KFPU_EMU
  201. /* Convert long double format
  202. * into internal fpu reg representation
  203. */
  204. if (FPU_IS_EMU && (addr < 45) && !(addr % 3)) {
  205. data = (unsigned long)data << 15;
  206. data = (data & 0xffff0000) |
  207. ((data & 0x0000ffff) >> 1);
  208. }
  209. #endif
  210. child->thread.fp[addr - 21] = data;
  211. ret = 0;
  212. }
  213. break;
  214. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  215. case PTRACE_CONT: { /* restart after signal. */
  216. long tmp;
  217. ret = -EIO;
  218. if (!valid_signal(data))
  219. break;
  220. if (request == PTRACE_SYSCALL)
  221. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  222. else
  223. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  224. child->exit_code = data;
  225. /* make sure the single step bit is not set. */
  226. tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
  227. put_reg(child, PT_SR, tmp);
  228. wake_up_process(child);
  229. ret = 0;
  230. break;
  231. }
  232. /*
  233. * make the child exit. Best I can do is send it a sigkill.
  234. * perhaps it should be put in the status that it wants to
  235. * exit.
  236. */
  237. case PTRACE_KILL: {
  238. long tmp;
  239. ret = 0;
  240. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  241. break;
  242. child->exit_code = SIGKILL;
  243. /* make sure the single step bit is not set. */
  244. tmp = get_reg(child, PT_SR) & ~(TRACE_BITS << 16);
  245. put_reg(child, PT_SR, tmp);
  246. wake_up_process(child);
  247. break;
  248. }
  249. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  250. long tmp;
  251. ret = -EIO;
  252. if (!valid_signal(data))
  253. break;
  254. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  255. tmp = get_reg(child, PT_SR) | (TRACE_BITS << 16);
  256. put_reg(child, PT_SR, tmp);
  257. child->exit_code = data;
  258. /* give it a chance to run. */
  259. wake_up_process(child);
  260. ret = 0;
  261. break;
  262. }
  263. case PTRACE_DETACH: /* detach a process that was attached. */
  264. ret = ptrace_detach(child, data);
  265. break;
  266. case PTRACE_GETREGS: { /* Get all gp regs from the child. */
  267. int i;
  268. unsigned long tmp;
  269. for (i = 0; i < 19; i++) {
  270. tmp = get_reg(child, i);
  271. if (i == PT_SR)
  272. tmp >>= 16;
  273. if (put_user(tmp, (unsigned long *) data)) {
  274. ret = -EFAULT;
  275. break;
  276. }
  277. data += sizeof(long);
  278. }
  279. ret = 0;
  280. break;
  281. }
  282. case PTRACE_SETREGS: { /* Set all gp regs in the child. */
  283. int i;
  284. unsigned long tmp;
  285. for (i = 0; i < 19; i++) {
  286. if (get_user(tmp, (unsigned long *) data)) {
  287. ret = -EFAULT;
  288. break;
  289. }
  290. if (i == PT_SR) {
  291. tmp &= SR_MASK;
  292. tmp <<= 16;
  293. tmp |= get_reg(child, PT_SR) & ~(SR_MASK << 16);
  294. }
  295. put_reg(child, i, tmp);
  296. data += sizeof(long);
  297. }
  298. ret = 0;
  299. break;
  300. }
  301. #ifdef PTRACE_GETFPREGS
  302. case PTRACE_GETFPREGS: { /* Get the child FPU state. */
  303. ret = 0;
  304. if (copy_to_user((void *)data, &child->thread.fp,
  305. sizeof(struct user_m68kfp_struct)))
  306. ret = -EFAULT;
  307. break;
  308. }
  309. #endif
  310. #ifdef PTRACE_SETFPREGS
  311. case PTRACE_SETFPREGS: { /* Set the child FPU state. */
  312. ret = 0;
  313. if (copy_from_user(&child->thread.fp, (void *)data,
  314. sizeof(struct user_m68kfp_struct)))
  315. ret = -EFAULT;
  316. break;
  317. }
  318. #endif
  319. default:
  320. ret = -EIO;
  321. break;
  322. }
  323. out_tsk:
  324. put_task_struct(child);
  325. out:
  326. unlock_kernel();
  327. return ret;
  328. }
  329. asmlinkage void syscall_trace(void)
  330. {
  331. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  332. return;
  333. if (!(current->ptrace & PT_PTRACED))
  334. return;
  335. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  336. ? 0x80 : 0));
  337. /*
  338. * this isn't the same as continuing with a signal, but it will do
  339. * for normal use. strace only continues with a signal if the
  340. * stopping signal is not SIGTRAP. -brl
  341. */
  342. if (current->exit_code) {
  343. send_sig(current->exit_code, current, 1);
  344. current->exit_code = 0;
  345. }
  346. }