ptrace.c 9.3 KB

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