ptrace.c 12 KB

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