ptrace.c 12 KB

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