ptrace.c 12 KB

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