ptrace.c 9.3 KB

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