ptrace.c 9.1 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 <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. #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 (!valid_signal(data))
  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 (!valid_signal(data))
  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. }