ptrace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. asmlinkage long sys_ptrace(long request, long pid, long addr, long data)
  150. {
  151. struct task_struct *child;
  152. int ret;
  153. #if 0
  154. printk("ptrace(r=%d,pid=%d,addr=%08lx,data=%08lx)\n",
  155. (int) request, (int) pid, (unsigned long) addr,
  156. (unsigned long) data);
  157. #endif
  158. lock_kernel();
  159. ret = -EPERM;
  160. if (request == PTRACE_TRACEME) {
  161. /* are we already being traced? */
  162. if (current->ptrace & PT_PTRACED)
  163. goto out;
  164. if ((ret = security_ptrace(current->parent, current)))
  165. goto out;
  166. /* set the ptrace bit in the process flags. */
  167. current->ptrace |= PT_PTRACED;
  168. ret = 0;
  169. goto out;
  170. }
  171. ret = -ESRCH;
  172. read_lock(&tasklist_lock);
  173. child = find_task_by_pid(pid);
  174. if (child)
  175. get_task_struct(child);
  176. read_unlock(&tasklist_lock);
  177. if (!child)
  178. goto out;
  179. ret = -EPERM;
  180. if (pid == 1) /* you may not mess with init */
  181. goto out_tsk;
  182. if (request == PTRACE_ATTACH) {
  183. ret = ptrace_attach(child);
  184. goto out_tsk;
  185. }
  186. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  187. if (ret < 0)
  188. goto out_tsk;
  189. switch (request) {
  190. /* when I and D space are separate, these will need to be fixed. */
  191. case PTRACE_PEEKTEXT: /* read word at location addr. */
  192. case PTRACE_PEEKDATA: {
  193. unsigned long tmp;
  194. int copied;
  195. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  196. ret = -EIO;
  197. if (copied != sizeof(tmp))
  198. break;
  199. ret = put_user(tmp,(unsigned long __user *) data);
  200. break;
  201. }
  202. /* Read the word at location addr in the USER area. */
  203. case PTRACE_PEEKUSR: {
  204. struct pt_regs *regs;
  205. unsigned long tmp = 0;
  206. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  207. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  208. ret = 0; /* Default return value. */
  209. switch (addr) {
  210. case 0 ... 31:
  211. tmp = regs->regs[addr];
  212. break;
  213. case FPR_BASE ... FPR_BASE + 31:
  214. if (tsk_used_math(child)) {
  215. fpureg_t *fregs = get_fpu_regs(child);
  216. #ifdef CONFIG_32BIT
  217. /*
  218. * The odd registers are actually the high
  219. * order bits of the values stored in the even
  220. * registers - unless we're using r2k_switch.S.
  221. */
  222. if (addr & 1)
  223. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  224. else
  225. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  226. #endif
  227. #ifdef CONFIG_64BIT
  228. tmp = fregs[addr - FPR_BASE];
  229. #endif
  230. } else {
  231. tmp = -1; /* FP not yet used */
  232. }
  233. break;
  234. case PC:
  235. tmp = regs->cp0_epc;
  236. break;
  237. case CAUSE:
  238. tmp = regs->cp0_cause;
  239. break;
  240. case BADVADDR:
  241. tmp = regs->cp0_badvaddr;
  242. break;
  243. case MMHI:
  244. tmp = regs->hi;
  245. break;
  246. case MMLO:
  247. tmp = regs->lo;
  248. break;
  249. case FPC_CSR:
  250. if (cpu_has_fpu)
  251. tmp = child->thread.fpu.hard.fcr31;
  252. else
  253. tmp = child->thread.fpu.soft.fcr31;
  254. break;
  255. case FPC_EIR: { /* implementation / version register */
  256. unsigned int flags;
  257. if (!cpu_has_fpu)
  258. break;
  259. preempt_disable();
  260. if (cpu_has_mipsmt) {
  261. unsigned int vpflags = dvpe();
  262. flags = read_c0_status();
  263. __enable_fpu();
  264. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  265. write_c0_status(flags);
  266. evpe(vpflags);
  267. } else {
  268. flags = read_c0_status();
  269. __enable_fpu();
  270. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  271. write_c0_status(flags);
  272. }
  273. preempt_enable();
  274. break;
  275. }
  276. case DSP_BASE ... DSP_BASE + 5: {
  277. dspreg_t *dregs;
  278. if (!cpu_has_dsp) {
  279. tmp = 0;
  280. ret = -EIO;
  281. goto out_tsk;
  282. }
  283. if (child->thread.dsp.used_dsp) {
  284. dregs = __get_dsp_regs(child);
  285. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  286. } else {
  287. tmp = -1; /* DSP registers yet used */
  288. }
  289. break;
  290. }
  291. case DSP_CONTROL:
  292. if (!cpu_has_dsp) {
  293. tmp = 0;
  294. ret = -EIO;
  295. goto out_tsk;
  296. }
  297. tmp = child->thread.dsp.dspcontrol;
  298. break;
  299. default:
  300. tmp = 0;
  301. ret = -EIO;
  302. goto out_tsk;
  303. }
  304. ret = put_user(tmp, (unsigned long __user *) data);
  305. break;
  306. }
  307. /* when I and D space are separate, this will have to be fixed. */
  308. case PTRACE_POKETEXT: /* write the word at location addr. */
  309. case PTRACE_POKEDATA:
  310. ret = 0;
  311. if (access_process_vm(child, addr, &data, sizeof(data), 1)
  312. == sizeof(data))
  313. break;
  314. ret = -EIO;
  315. break;
  316. case PTRACE_POKEUSR: {
  317. struct pt_regs *regs;
  318. ret = 0;
  319. regs = (struct pt_regs *) ((unsigned long) child->thread_info +
  320. THREAD_SIZE - 32 - sizeof(struct pt_regs));
  321. switch (addr) {
  322. case 0 ... 31:
  323. regs->regs[addr] = data;
  324. break;
  325. case FPR_BASE ... FPR_BASE + 31: {
  326. fpureg_t *fregs = get_fpu_regs(child);
  327. if (!tsk_used_math(child)) {
  328. /* FP not yet used */
  329. memset(&child->thread.fpu.hard, ~0,
  330. sizeof(child->thread.fpu.hard));
  331. child->thread.fpu.hard.fcr31 = 0;
  332. }
  333. #ifdef CONFIG_32BIT
  334. /*
  335. * The odd registers are actually the high order bits
  336. * of the values stored in the even registers - unless
  337. * we're using r2k_switch.S.
  338. */
  339. if (addr & 1) {
  340. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  341. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  342. } else {
  343. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  344. fregs[addr - FPR_BASE] |= data;
  345. }
  346. #endif
  347. #ifdef CONFIG_64BIT
  348. fregs[addr - FPR_BASE] = data;
  349. #endif
  350. break;
  351. }
  352. case PC:
  353. regs->cp0_epc = data;
  354. break;
  355. case MMHI:
  356. regs->hi = data;
  357. break;
  358. case MMLO:
  359. regs->lo = data;
  360. break;
  361. case FPC_CSR:
  362. if (cpu_has_fpu)
  363. child->thread.fpu.hard.fcr31 = data;
  364. else
  365. child->thread.fpu.soft.fcr31 = data;
  366. break;
  367. case DSP_BASE ... DSP_BASE + 5: {
  368. dspreg_t *dregs;
  369. if (!cpu_has_dsp) {
  370. ret = -EIO;
  371. break;
  372. }
  373. dregs = __get_dsp_regs(child);
  374. dregs[addr - DSP_BASE] = data;
  375. break;
  376. }
  377. case DSP_CONTROL:
  378. if (!cpu_has_dsp) {
  379. ret = -EIO;
  380. break;
  381. }
  382. child->thread.dsp.dspcontrol = data;
  383. break;
  384. default:
  385. /* The rest are not allowed. */
  386. ret = -EIO;
  387. break;
  388. }
  389. break;
  390. }
  391. case PTRACE_GETREGS:
  392. ret = ptrace_getregs (child, (__u64 __user *) data);
  393. break;
  394. case PTRACE_SETREGS:
  395. ret = ptrace_setregs (child, (__u64 __user *) data);
  396. break;
  397. case PTRACE_GETFPREGS:
  398. ret = ptrace_getfpregs (child, (__u32 __user *) data);
  399. break;
  400. case PTRACE_SETFPREGS:
  401. ret = ptrace_setfpregs (child, (__u32 __user *) data);
  402. break;
  403. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  404. case PTRACE_CONT: { /* restart after signal. */
  405. ret = -EIO;
  406. if (!valid_signal(data))
  407. break;
  408. if (request == PTRACE_SYSCALL) {
  409. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  410. }
  411. else {
  412. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  413. }
  414. child->exit_code = data;
  415. wake_up_process(child);
  416. ret = 0;
  417. break;
  418. }
  419. /*
  420. * make the child exit. Best I can do is send it a sigkill.
  421. * perhaps it should be put in the status that it wants to
  422. * exit.
  423. */
  424. case PTRACE_KILL:
  425. ret = 0;
  426. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  427. break;
  428. child->exit_code = SIGKILL;
  429. wake_up_process(child);
  430. break;
  431. case PTRACE_DETACH: /* detach a process that was attached. */
  432. ret = ptrace_detach(child, data);
  433. break;
  434. case PTRACE_GET_THREAD_AREA:
  435. ret = put_user(child->thread_info->tp_value,
  436. (unsigned long __user *) data);
  437. break;
  438. default:
  439. ret = ptrace_request(child, request, addr, data);
  440. break;
  441. }
  442. out_tsk:
  443. put_task_struct(child);
  444. out:
  445. unlock_kernel();
  446. return ret;
  447. }
  448. static inline int audit_arch(void)
  449. {
  450. int arch = EM_MIPS;
  451. #ifdef CONFIG_64BIT
  452. arch |= __AUDIT_ARCH_64BIT;
  453. #endif
  454. #if defined(__LITTLE_ENDIAN)
  455. arch |= __AUDIT_ARCH_LE;
  456. #endif
  457. return arch;
  458. }
  459. /*
  460. * Notification of system call entry/exit
  461. * - triggered by current->work.syscall_trace
  462. */
  463. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  464. {
  465. if (unlikely(current->audit_context) && entryexit)
  466. audit_syscall_exit(current, AUDITSC_RESULT(regs->regs[2]),
  467. regs->regs[2]);
  468. if (!(current->ptrace & PT_PTRACED))
  469. goto out;
  470. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  471. goto out;
  472. /* The 0x80 provides a way for the tracing parent to distinguish
  473. between a syscall stop and SIGTRAP delivery */
  474. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  475. 0x80 : 0));
  476. /*
  477. * this isn't the same as continuing with a signal, but it will do
  478. * for normal use. strace only continues with a signal if the
  479. * stopping signal is not SIGTRAP. -brl
  480. */
  481. if (current->exit_code) {
  482. send_sig(current->exit_code, current, 1);
  483. current->exit_code = 0;
  484. }
  485. out:
  486. if (unlikely(current->audit_context) && !entryexit)
  487. audit_syscall_entry(current, audit_arch(), regs->regs[2],
  488. regs->regs[4], regs->regs[5],
  489. regs->regs[6], regs->regs[7]);
  490. }