ptrace.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1992 Ross Biro
  7. * Copyright (C) Linus Torvalds
  8. * Copyright (C) 1994, 95, 96, 97, 98, 2000 Ralf Baechle
  9. * Copyright (C) 1996 David S. Miller
  10. * Kevin D. Kissell, kevink@mips.com and Carsten Langgaard, carstenl@mips.com
  11. * Copyright (C) 1999 MIPS Technologies, Inc.
  12. * Copyright (C) 2000 Ulf Carlsson
  13. *
  14. * At this time Linux/MIPS64 only supports syscall tracing, even for 32-bit
  15. * binaries.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/compiler.h>
  19. #include <linux/kernel.h>
  20. #include <linux/sched.h>
  21. #include <linux/mm.h>
  22. #include <linux/errno.h>
  23. #include <linux/ptrace.h>
  24. #include <linux/audit.h>
  25. #include <linux/smp.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/user.h>
  28. #include <linux/security.h>
  29. #include <linux/signal.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/cpu.h>
  32. #include <asm/dsp.h>
  33. #include <asm/fpu.h>
  34. #include <asm/mipsregs.h>
  35. #include <asm/pgtable.h>
  36. #include <asm/page.h>
  37. #include <asm/system.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/bootinfo.h>
  40. /*
  41. * Called by kernel/ptrace.c when detaching..
  42. *
  43. * Make sure single step bits etc are not set.
  44. */
  45. void ptrace_disable(struct task_struct *child)
  46. {
  47. /* Nothing to do.. */
  48. }
  49. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  50. {
  51. struct task_struct *child;
  52. int ret;
  53. #if 0
  54. printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
  55. (int) request, (int) pid, (unsigned long) addr,
  56. (unsigned long) data);
  57. #endif
  58. lock_kernel();
  59. ret = -EPERM;
  60. if (request == PTRACE_TRACEME) {
  61. /* are we already being traced? */
  62. if (current->ptrace & PT_PTRACED)
  63. goto out;
  64. if ((ret = security_ptrace(current->parent, current)))
  65. goto out;
  66. /* set the ptrace bit in the process flags. */
  67. current->ptrace |= PT_PTRACED;
  68. ret = 0;
  69. goto out;
  70. }
  71. ret = -ESRCH;
  72. read_lock(&tasklist_lock);
  73. child = find_task_by_pid(pid);
  74. if (child)
  75. get_task_struct(child);
  76. read_unlock(&tasklist_lock);
  77. if (!child)
  78. goto out;
  79. ret = -EPERM;
  80. if (pid == 1) /* you may not mess with init */
  81. goto out_tsk;
  82. if (request == PTRACE_ATTACH) {
  83. ret = ptrace_attach(child);
  84. goto out_tsk;
  85. }
  86. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  87. if (ret < 0)
  88. goto out_tsk;
  89. switch (request) {
  90. /* when I and D space are separate, these will need to be fixed. */
  91. case PTRACE_PEEKTEXT: /* read word at location addr. */
  92. case PTRACE_PEEKDATA: {
  93. unsigned long tmp;
  94. int copied;
  95. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  96. ret = -EIO;
  97. if (copied != sizeof(tmp))
  98. break;
  99. ret = put_user(tmp,(unsigned long __user *) data);
  100. break;
  101. }
  102. /* Read the word at location addr in the USER area. */
  103. case PTRACE_PEEKUSR: {
  104. struct pt_regs *regs;
  105. unsigned long tmp = 0;
  106. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  107. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  108. ret = 0; /* Default return value. */
  109. switch (addr) {
  110. case 0 ... 31:
  111. tmp = regs->regs[addr];
  112. break;
  113. case FPR_BASE ... FPR_BASE + 31:
  114. if (tsk_used_math(child)) {
  115. fpureg_t *fregs = get_fpu_regs(child);
  116. #ifdef CONFIG_32BIT
  117. /*
  118. * The odd registers are actually the high
  119. * order bits of the values stored in the even
  120. * registers - unless we're using r2k_switch.S.
  121. */
  122. if (addr & 1)
  123. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  124. else
  125. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  126. #endif
  127. #ifdef CONFIG_64BIT
  128. tmp = fregs[addr - FPR_BASE];
  129. #endif
  130. } else {
  131. tmp = -1; /* FP not yet used */
  132. }
  133. break;
  134. case PC:
  135. tmp = regs->cp0_epc;
  136. break;
  137. case CAUSE:
  138. tmp = regs->cp0_cause;
  139. break;
  140. case BADVADDR:
  141. tmp = regs->cp0_badvaddr;
  142. break;
  143. case MMHI:
  144. tmp = regs->hi;
  145. break;
  146. case MMLO:
  147. tmp = regs->lo;
  148. break;
  149. case FPC_CSR:
  150. if (cpu_has_fpu)
  151. tmp = child->thread.fpu.hard.fcr31;
  152. else
  153. tmp = child->thread.fpu.soft.fcr31;
  154. break;
  155. case FPC_EIR: { /* implementation / version register */
  156. unsigned int flags;
  157. if (!cpu_has_fpu)
  158. break;
  159. flags = read_c0_status();
  160. __enable_fpu();
  161. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  162. write_c0_status(flags);
  163. break;
  164. }
  165. case DSP_BASE ... DSP_BASE + 5: {
  166. dspreg_t *dregs;
  167. if (!cpu_has_dsp) {
  168. tmp = 0;
  169. ret = -EIO;
  170. goto out_tsk;
  171. }
  172. if (child->thread.dsp.used_dsp) {
  173. dregs = __get_dsp_regs(child);
  174. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  175. } else {
  176. tmp = -1; /* DSP registers yet used */
  177. }
  178. break;
  179. }
  180. case DSP_CONTROL:
  181. if (!cpu_has_dsp) {
  182. tmp = 0;
  183. ret = -EIO;
  184. goto out_tsk;
  185. }
  186. tmp = child->thread.dsp.dspcontrol;
  187. break;
  188. default:
  189. tmp = 0;
  190. ret = -EIO;
  191. goto out_tsk;
  192. }
  193. ret = put_user(tmp, (unsigned long __user *) data);
  194. break;
  195. }
  196. /* when I and D space are separate, this will have to be fixed. */
  197. case PTRACE_POKETEXT: /* write the word at location addr. */
  198. case PTRACE_POKEDATA:
  199. ret = 0;
  200. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  201. == sizeof(data))
  202. break;
  203. ret = -EIO;
  204. break;
  205. case PTRACE_POKEUSR: {
  206. struct pt_regs *regs;
  207. ret = 0;
  208. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  209. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  210. switch (addr) {
  211. case 0 ... 31:
  212. regs->regs[addr] = data;
  213. break;
  214. case FPR_BASE ... FPR_BASE + 31: {
  215. fpureg_t *fregs = get_fpu_regs(child);
  216. if (!tsk_used_math(child)) {
  217. /* FP not yet used */
  218. memset(&child->thread.fpu.hard, ~0,
  219. sizeof(child->thread.fpu.hard));
  220. child->thread.fpu.hard.fcr31 = 0;
  221. }
  222. #ifdef CONFIG_32BIT
  223. /*
  224. * The odd registers are actually the high order bits
  225. * of the values stored in the even registers - unless
  226. * we're using r2k_switch.S.
  227. */
  228. if (addr & 1) {
  229. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  230. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  231. } else {
  232. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  233. fregs[addr - FPR_BASE] |= data;
  234. }
  235. #endif
  236. #ifdef CONFIG_64BIT
  237. fregs[addr - FPR_BASE] = data;
  238. #endif
  239. break;
  240. }
  241. case PC:
  242. regs->cp0_epc = data;
  243. break;
  244. case MMHI:
  245. regs->hi = data;
  246. break;
  247. case MMLO:
  248. regs->lo = data;
  249. break;
  250. case FPC_CSR:
  251. if (cpu_has_fpu)
  252. child->thread.fpu.hard.fcr31 = data;
  253. else
  254. child->thread.fpu.soft.fcr31 = data;
  255. break;
  256. case DSP_BASE ... DSP_BASE + 5: {
  257. dspreg_t *dregs;
  258. if (!cpu_has_dsp) {
  259. ret = -EIO;
  260. break;
  261. }
  262. dregs = __get_dsp_regs(child);
  263. dregs[addr - DSP_BASE] = data;
  264. break;
  265. }
  266. case DSP_CONTROL:
  267. if (!cpu_has_dsp) {
  268. ret = -EIO;
  269. break;
  270. }
  271. child->thread.dsp.dspcontrol = data;
  272. break;
  273. default:
  274. /* The rest are not allowed. */
  275. ret = -EIO;
  276. break;
  277. }
  278. break;
  279. }
  280. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  281. case PTRACE_CONT: { /* restart after signal. */
  282. ret = -EIO;
  283. if (!valid_signal(data))
  284. break;
  285. if (request == PTRACE_SYSCALL) {
  286. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  287. }
  288. else {
  289. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  290. }
  291. child->exit_code = data;
  292. wake_up_process(child);
  293. ret = 0;
  294. break;
  295. }
  296. /*
  297. * make the child exit. Best I can do is send it a sigkill.
  298. * perhaps it should be put in the status that it wants to
  299. * exit.
  300. */
  301. case PTRACE_KILL:
  302. ret = 0;
  303. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  304. break;
  305. child->exit_code = SIGKILL;
  306. wake_up_process(child);
  307. break;
  308. case PTRACE_DETACH: /* detach a process that was attached. */
  309. ret = ptrace_detach(child, data);
  310. break;
  311. case PTRACE_GET_THREAD_AREA:
  312. ret = put_user(child->thread_info->tp_value,
  313. (unsigned long __user *) data);
  314. break;
  315. default:
  316. ret = ptrace_request(child, request, addr, data);
  317. break;
  318. }
  319. out_tsk:
  320. put_task_struct(child);
  321. out:
  322. unlock_kernel();
  323. return ret;
  324. }
  325. static inline int audit_arch(void)
  326. {
  327. int arch = EM_MIPS;
  328. #ifdef CONFIG_64BIT
  329. arch |= __AUDIT_ARCH_64BIT;
  330. #endif
  331. #if defined(__LITTLE_ENDIAN)
  332. arch |= __AUDIT_ARCH_LE;
  333. #endif
  334. return arch;
  335. }
  336. /*
  337. * Notification of system call entry/exit
  338. * - triggered by current->work.syscall_trace
  339. */
  340. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  341. {
  342. if (unlikely(current->audit_context) && entryexit)
  343. audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]),
  344. regs->regs[2]);
  345. if (!(current->ptrace & PT_PTRACED))
  346. goto out;
  347. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  348. goto out;
  349. /* The 0x80 provides a way for the tracing parent to distinguish
  350. between a syscall stop and SIGTRAP delivery */
  351. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  352. 0x80 : 0));
  353. /*
  354. * this isn't the same as continuing with a signal, but it will do
  355. * for normal use. strace only continues with a signal if the
  356. * stopping signal is not SIGTRAP. -brl
  357. */
  358. if (current->exit_code) {
  359. send_sig(current->exit_code, current, 1);
  360. current->exit_code = 0;
  361. }
  362. out:
  363. if (unlikely(current->audit_context) && !entryexit)
  364. audit_syscall_entry(current, audit_arch(), regs->regs[2],
  365. regs->regs[4], regs->regs[5],
  366. regs->regs[6], regs->regs[7]);
  367. }