ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/mipsmtregs.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/page.h>
  38. #include <asm/system.h>
  39. #include <asm/uaccess.h>
  40. #include <asm/bootinfo.h>
  41. #include <asm/reg.h>
  42. /*
  43. * Called by kernel/ptrace.c when detaching..
  44. *
  45. * Make sure single step bits etc are not set.
  46. */
  47. void ptrace_disable(struct task_struct *child)
  48. {
  49. /* Nothing to do.. */
  50. }
  51. /*
  52. * Read a general register set. We always use the 64-bit format, even
  53. * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
  54. * Registers are sign extended to fill the available space.
  55. */
  56. int ptrace_getregs (struct task_struct *child, __s64 __user *data)
  57. {
  58. struct pt_regs *regs;
  59. int i;
  60. if (!access_ok(VERIFY_WRITE, data, 38 * 8))
  61. return -EIO;
  62. regs = task_pt_regs(child);
  63. for (i = 0; i < 32; i++)
  64. __put_user (regs->regs[i], data + i);
  65. __put_user (regs->lo, data + EF_LO - EF_R0);
  66. __put_user (regs->hi, data + EF_HI - EF_R0);
  67. __put_user (regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
  68. __put_user (regs->cp0_badvaddr, data + EF_CP0_BADVADDR - EF_R0);
  69. __put_user (regs->cp0_status, data + EF_CP0_STATUS - EF_R0);
  70. __put_user (regs->cp0_cause, data + EF_CP0_CAUSE - EF_R0);
  71. return 0;
  72. }
  73. /*
  74. * Write a general register set. As for PTRACE_GETREGS, we always use
  75. * the 64-bit format. On a 32-bit kernel only the lower order half
  76. * (according to endianness) will be used.
  77. */
  78. int ptrace_setregs (struct task_struct *child, __s64 __user *data)
  79. {
  80. struct pt_regs *regs;
  81. int i;
  82. if (!access_ok(VERIFY_READ, data, 38 * 8))
  83. return -EIO;
  84. regs = task_pt_regs(child);
  85. for (i = 0; i < 32; i++)
  86. __get_user (regs->regs[i], data + i);
  87. __get_user (regs->lo, data + EF_LO - EF_R0);
  88. __get_user (regs->hi, data + EF_HI - EF_R0);
  89. __get_user (regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
  90. /* badvaddr, status, and cause may not be written. */
  91. return 0;
  92. }
  93. int ptrace_getfpregs (struct task_struct *child, __u32 __user *data)
  94. {
  95. int i;
  96. if (!access_ok(VERIFY_WRITE, data, 33 * 8))
  97. return -EIO;
  98. if (tsk_used_math(child)) {
  99. fpureg_t *fregs = get_fpu_regs(child);
  100. for (i = 0; i < 32; i++)
  101. __put_user (fregs[i], i + (__u64 __user *) data);
  102. } else {
  103. for (i = 0; i < 32; i++)
  104. __put_user ((__u64) -1, i + (__u64 __user *) data);
  105. }
  106. if (cpu_has_fpu) {
  107. unsigned int flags, tmp;
  108. __put_user (child->thread.fpu.hard.fcr31, data + 64);
  109. preempt_disable();
  110. if (cpu_has_mipsmt) {
  111. unsigned int vpflags = dvpe();
  112. flags = read_c0_status();
  113. __enable_fpu();
  114. __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
  115. write_c0_status(flags);
  116. evpe(vpflags);
  117. } else {
  118. flags = read_c0_status();
  119. __enable_fpu();
  120. __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
  121. write_c0_status(flags);
  122. }
  123. preempt_enable();
  124. __put_user (tmp, data + 65);
  125. } else {
  126. __put_user (child->thread.fpu.soft.fcr31, data + 64);
  127. __put_user ((__u32) 0, data + 65);
  128. }
  129. return 0;
  130. }
  131. int ptrace_setfpregs (struct task_struct *child, __u32 __user *data)
  132. {
  133. fpureg_t *fregs;
  134. int i;
  135. if (!access_ok(VERIFY_READ, data, 33 * 8))
  136. return -EIO;
  137. fregs = get_fpu_regs(child);
  138. for (i = 0; i < 32; i++)
  139. __get_user (fregs[i], i + (__u64 __user *) data);
  140. if (cpu_has_fpu)
  141. __get_user (child->thread.fpu.hard.fcr31, data + 64);
  142. else
  143. __get_user (child->thread.fpu.soft.fcr31, data + 64);
  144. /* FIR may not be written. */
  145. return 0;
  146. }
  147. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  148. {
  149. int ret;
  150. switch (request) {
  151. /* when I and D space are separate, these will need to be fixed. */
  152. case PTRACE_PEEKTEXT: /* read word at location addr. */
  153. case PTRACE_PEEKDATA: {
  154. unsigned long tmp;
  155. int copied;
  156. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  157. ret = -EIO;
  158. if (copied != sizeof(tmp))
  159. break;
  160. ret = put_user(tmp,(unsigned long __user *) data);
  161. break;
  162. }
  163. /* Read the word at location addr in the USER area. */
  164. case PTRACE_PEEKUSR: {
  165. struct pt_regs *regs;
  166. unsigned long tmp = 0;
  167. regs = task_pt_regs(child);
  168. ret = 0; /* Default return value. */
  169. switch (addr) {
  170. case 0 ... 31:
  171. tmp = regs->regs[addr];
  172. break;
  173. case FPR_BASE ... FPR_BASE + 31:
  174. if (tsk_used_math(child)) {
  175. fpureg_t *fregs = get_fpu_regs(child);
  176. #ifdef CONFIG_32BIT
  177. /*
  178. * The odd registers are actually the high
  179. * order bits of the values stored in the even
  180. * registers - unless we're using r2k_switch.S.
  181. */
  182. if (addr & 1)
  183. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  184. else
  185. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  186. #endif
  187. #ifdef CONFIG_64BIT
  188. tmp = fregs[addr - FPR_BASE];
  189. #endif
  190. } else {
  191. tmp = -1; /* FP not yet used */
  192. }
  193. break;
  194. case PC:
  195. tmp = regs->cp0_epc;
  196. break;
  197. case CAUSE:
  198. tmp = regs->cp0_cause;
  199. break;
  200. case BADVADDR:
  201. tmp = regs->cp0_badvaddr;
  202. break;
  203. case MMHI:
  204. tmp = regs->hi;
  205. break;
  206. case MMLO:
  207. tmp = regs->lo;
  208. break;
  209. case FPC_CSR:
  210. if (cpu_has_fpu)
  211. tmp = child->thread.fpu.hard.fcr31;
  212. else
  213. tmp = child->thread.fpu.soft.fcr31;
  214. break;
  215. case FPC_EIR: { /* implementation / version register */
  216. unsigned int flags;
  217. if (!cpu_has_fpu)
  218. break;
  219. preempt_disable();
  220. if (cpu_has_mipsmt) {
  221. unsigned int vpflags = dvpe();
  222. flags = read_c0_status();
  223. __enable_fpu();
  224. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  225. write_c0_status(flags);
  226. evpe(vpflags);
  227. } else {
  228. flags = read_c0_status();
  229. __enable_fpu();
  230. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  231. write_c0_status(flags);
  232. }
  233. preempt_enable();
  234. break;
  235. }
  236. case DSP_BASE ... DSP_BASE + 5: {
  237. dspreg_t *dregs;
  238. if (!cpu_has_dsp) {
  239. tmp = 0;
  240. ret = -EIO;
  241. goto out;
  242. }
  243. dregs = __get_dsp_regs(child);
  244. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  245. break;
  246. }
  247. case DSP_CONTROL:
  248. if (!cpu_has_dsp) {
  249. tmp = 0;
  250. ret = -EIO;
  251. goto out;
  252. }
  253. tmp = child->thread.dsp.dspcontrol;
  254. break;
  255. default:
  256. tmp = 0;
  257. ret = -EIO;
  258. goto out;
  259. }
  260. ret = put_user(tmp, (unsigned long __user *) data);
  261. break;
  262. }
  263. /* when I and D space are separate, this will have to be fixed. */
  264. case PTRACE_POKETEXT: /* write the word at location addr. */
  265. case PTRACE_POKEDATA:
  266. ret = 0;
  267. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  268. == sizeof(data))
  269. break;
  270. ret = -EIO;
  271. break;
  272. case PTRACE_POKEUSR: {
  273. struct pt_regs *regs;
  274. ret = 0;
  275. regs = task_pt_regs(child);
  276. switch (addr) {
  277. case 0 ... 31:
  278. regs->regs[addr] = data;
  279. break;
  280. case FPR_BASE ... FPR_BASE + 31: {
  281. fpureg_t *fregs = get_fpu_regs(child);
  282. if (!tsk_used_math(child)) {
  283. /* FP not yet used */
  284. memset(&child->thread.fpu.hard, ~0,
  285. sizeof(child->thread.fpu.hard));
  286. child->thread.fpu.hard.fcr31 = 0;
  287. }
  288. #ifdef CONFIG_32BIT
  289. /*
  290. * The odd registers are actually the high order bits
  291. * of the values stored in the even registers - unless
  292. * we're using r2k_switch.S.
  293. */
  294. if (addr & 1) {
  295. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  296. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  297. } else {
  298. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  299. fregs[addr - FPR_BASE] |= data;
  300. }
  301. #endif
  302. #ifdef CONFIG_64BIT
  303. fregs[addr - FPR_BASE] = data;
  304. #endif
  305. break;
  306. }
  307. case PC:
  308. regs->cp0_epc = data;
  309. break;
  310. case MMHI:
  311. regs->hi = data;
  312. break;
  313. case MMLO:
  314. regs->lo = data;
  315. break;
  316. case FPC_CSR:
  317. if (cpu_has_fpu)
  318. child->thread.fpu.hard.fcr31 = data;
  319. else
  320. child->thread.fpu.soft.fcr31 = data;
  321. break;
  322. case DSP_BASE ... DSP_BASE + 5: {
  323. dspreg_t *dregs;
  324. if (!cpu_has_dsp) {
  325. ret = -EIO;
  326. break;
  327. }
  328. dregs = __get_dsp_regs(child);
  329. dregs[addr - DSP_BASE] = data;
  330. break;
  331. }
  332. case DSP_CONTROL:
  333. if (!cpu_has_dsp) {
  334. ret = -EIO;
  335. break;
  336. }
  337. child->thread.dsp.dspcontrol = data;
  338. break;
  339. default:
  340. /* The rest are not allowed. */
  341. ret = -EIO;
  342. break;
  343. }
  344. break;
  345. }
  346. case PTRACE_GETREGS:
  347. ret = ptrace_getregs (child, (__u64 __user *) data);
  348. break;
  349. case PTRACE_SETREGS:
  350. ret = ptrace_setregs (child, (__u64 __user *) data);
  351. break;
  352. case PTRACE_GETFPREGS:
  353. ret = ptrace_getfpregs (child, (__u32 __user *) data);
  354. break;
  355. case PTRACE_SETFPREGS:
  356. ret = ptrace_setfpregs (child, (__u32 __user *) data);
  357. break;
  358. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  359. case PTRACE_CONT: { /* restart after signal. */
  360. ret = -EIO;
  361. if (!valid_signal(data))
  362. break;
  363. if (request == PTRACE_SYSCALL) {
  364. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  365. }
  366. else {
  367. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  368. }
  369. child->exit_code = data;
  370. wake_up_process(child);
  371. ret = 0;
  372. break;
  373. }
  374. /*
  375. * make the child exit. Best I can do is send it a sigkill.
  376. * perhaps it should be put in the status that it wants to
  377. * exit.
  378. */
  379. case PTRACE_KILL:
  380. ret = 0;
  381. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  382. break;
  383. child->exit_code = SIGKILL;
  384. wake_up_process(child);
  385. break;
  386. case PTRACE_DETACH: /* detach a process that was attached. */
  387. ret = ptrace_detach(child, data);
  388. break;
  389. case PTRACE_GET_THREAD_AREA:
  390. ret = put_user(task_thread_info(child)->tp_value,
  391. (unsigned long __user *) data);
  392. break;
  393. default:
  394. ret = ptrace_request(child, request, addr, data);
  395. break;
  396. }
  397. out:
  398. return ret;
  399. }
  400. static inline int audit_arch(void)
  401. {
  402. int arch = EM_MIPS;
  403. #ifdef CONFIG_64BIT
  404. arch |= __AUDIT_ARCH_64BIT;
  405. #endif
  406. #if defined(__LITTLE_ENDIAN)
  407. arch |= __AUDIT_ARCH_LE;
  408. #endif
  409. return arch;
  410. }
  411. /*
  412. * Notification of system call entry/exit
  413. * - triggered by current->work.syscall_trace
  414. */
  415. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  416. {
  417. if (unlikely(current->audit_context) && entryexit)
  418. audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]),
  419. regs->regs[2]);
  420. if (!(current->ptrace & PT_PTRACED))
  421. goto out;
  422. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  423. goto out;
  424. /* The 0x80 provides a way for the tracing parent to distinguish
  425. between a syscall stop and SIGTRAP delivery */
  426. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  427. 0x80 : 0));
  428. /*
  429. * this isn't the same as continuing with a signal, but it will do
  430. * for normal use. strace only continues with a signal if the
  431. * stopping signal is not SIGTRAP. -brl
  432. */
  433. if (current->exit_code) {
  434. send_sig(current->exit_code, current, 1);
  435. current->exit_code = 0;
  436. }
  437. out:
  438. if (unlikely(current->audit_context) && !entryexit)
  439. audit_syscall_entry(current, audit_arch(), regs->regs[2],
  440. regs->regs[4], regs->regs[5],
  441. regs->regs[6], regs->regs[7]);
  442. }