ptrace.c 12 KB

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