ptrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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/smp.h>
  24. #include <linux/user.h>
  25. #include <linux/security.h>
  26. #include <linux/audit.h>
  27. #include <linux/seccomp.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. /* Don't load the watchpoint registers for the ex-child. */
  48. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  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((long)regs->regs[i], data + i);
  64. __put_user((long)regs->lo, data + EF_LO - EF_R0);
  65. __put_user((long)regs->hi, data + EF_HI - EF_R0);
  66. __put_user((long)regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
  67. __put_user((long)regs->cp0_badvaddr, data + EF_CP0_BADVADDR - EF_R0);
  68. __put_user((long)regs->cp0_status, data + EF_CP0_STATUS - EF_R0);
  69. __put_user((long)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. int ptrace_get_watch_regs(struct task_struct *child,
  144. struct pt_watch_regs __user *addr)
  145. {
  146. enum pt_watch_style style;
  147. int i;
  148. if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
  149. return -EIO;
  150. if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
  151. return -EIO;
  152. #ifdef CONFIG_32BIT
  153. style = pt_watch_style_mips32;
  154. #define WATCH_STYLE mips32
  155. #else
  156. style = pt_watch_style_mips64;
  157. #define WATCH_STYLE mips64
  158. #endif
  159. __put_user(style, &addr->style);
  160. __put_user(current_cpu_data.watch_reg_use_cnt,
  161. &addr->WATCH_STYLE.num_valid);
  162. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  163. __put_user(child->thread.watch.mips3264.watchlo[i],
  164. &addr->WATCH_STYLE.watchlo[i]);
  165. __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
  166. &addr->WATCH_STYLE.watchhi[i]);
  167. __put_user(current_cpu_data.watch_reg_masks[i],
  168. &addr->WATCH_STYLE.watch_masks[i]);
  169. }
  170. for (; i < 8; i++) {
  171. __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
  172. __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
  173. __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
  174. }
  175. return 0;
  176. }
  177. int ptrace_set_watch_regs(struct task_struct *child,
  178. struct pt_watch_regs __user *addr)
  179. {
  180. int i;
  181. int watch_active = 0;
  182. unsigned long lt[NUM_WATCH_REGS];
  183. u16 ht[NUM_WATCH_REGS];
  184. if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
  185. return -EIO;
  186. if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
  187. return -EIO;
  188. /* Check the values. */
  189. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  190. __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
  191. #ifdef CONFIG_32BIT
  192. if (lt[i] & __UA_LIMIT)
  193. return -EINVAL;
  194. #else
  195. if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
  196. if (lt[i] & 0xffffffff80000000UL)
  197. return -EINVAL;
  198. } else {
  199. if (lt[i] & __UA_LIMIT)
  200. return -EINVAL;
  201. }
  202. #endif
  203. __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
  204. if (ht[i] & ~0xff8)
  205. return -EINVAL;
  206. }
  207. /* Install them. */
  208. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  209. if (lt[i] & 7)
  210. watch_active = 1;
  211. child->thread.watch.mips3264.watchlo[i] = lt[i];
  212. /* Set the G bit. */
  213. child->thread.watch.mips3264.watchhi[i] = ht[i];
  214. }
  215. if (watch_active)
  216. set_tsk_thread_flag(child, TIF_LOAD_WATCH);
  217. else
  218. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  219. return 0;
  220. }
  221. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  222. {
  223. int ret;
  224. switch (request) {
  225. /* when I and D space are separate, these will need to be fixed. */
  226. case PTRACE_PEEKTEXT: /* read word at location addr. */
  227. case PTRACE_PEEKDATA:
  228. ret = generic_ptrace_peekdata(child, addr, data);
  229. break;
  230. /* Read the word at location addr in the USER area. */
  231. case PTRACE_PEEKUSR: {
  232. struct pt_regs *regs;
  233. unsigned long tmp = 0;
  234. regs = task_pt_regs(child);
  235. ret = 0; /* Default return value. */
  236. switch (addr) {
  237. case 0 ... 31:
  238. tmp = regs->regs[addr];
  239. break;
  240. case FPR_BASE ... FPR_BASE + 31:
  241. if (tsk_used_math(child)) {
  242. fpureg_t *fregs = get_fpu_regs(child);
  243. #ifdef CONFIG_32BIT
  244. /*
  245. * The odd registers are actually the high
  246. * order bits of the values stored in the even
  247. * registers - unless we're using r2k_switch.S.
  248. */
  249. if (addr & 1)
  250. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  251. else
  252. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  253. #endif
  254. #ifdef CONFIG_64BIT
  255. tmp = fregs[addr - FPR_BASE];
  256. #endif
  257. } else {
  258. tmp = -1; /* FP not yet used */
  259. }
  260. break;
  261. case PC:
  262. tmp = regs->cp0_epc;
  263. break;
  264. case CAUSE:
  265. tmp = regs->cp0_cause;
  266. break;
  267. case BADVADDR:
  268. tmp = regs->cp0_badvaddr;
  269. break;
  270. case MMHI:
  271. tmp = regs->hi;
  272. break;
  273. case MMLO:
  274. tmp = regs->lo;
  275. break;
  276. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  277. case ACX:
  278. tmp = regs->acx;
  279. break;
  280. #endif
  281. case FPC_CSR:
  282. tmp = child->thread.fpu.fcr31;
  283. break;
  284. case FPC_EIR: { /* implementation / version register */
  285. unsigned int flags;
  286. #ifdef CONFIG_MIPS_MT_SMTC
  287. unsigned long irqflags;
  288. unsigned int mtflags;
  289. #endif /* CONFIG_MIPS_MT_SMTC */
  290. preempt_disable();
  291. if (!cpu_has_fpu) {
  292. preempt_enable();
  293. break;
  294. }
  295. #ifdef CONFIG_MIPS_MT_SMTC
  296. /* Read-modify-write of Status must be atomic */
  297. local_irq_save(irqflags);
  298. mtflags = dmt();
  299. #endif /* CONFIG_MIPS_MT_SMTC */
  300. if (cpu_has_mipsmt) {
  301. unsigned int vpflags = dvpe();
  302. flags = read_c0_status();
  303. __enable_fpu();
  304. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  305. write_c0_status(flags);
  306. evpe(vpflags);
  307. } else {
  308. flags = read_c0_status();
  309. __enable_fpu();
  310. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  311. write_c0_status(flags);
  312. }
  313. #ifdef CONFIG_MIPS_MT_SMTC
  314. emt(mtflags);
  315. local_irq_restore(irqflags);
  316. #endif /* CONFIG_MIPS_MT_SMTC */
  317. preempt_enable();
  318. break;
  319. }
  320. case DSP_BASE ... DSP_BASE + 5: {
  321. dspreg_t *dregs;
  322. if (!cpu_has_dsp) {
  323. tmp = 0;
  324. ret = -EIO;
  325. goto out;
  326. }
  327. dregs = __get_dsp_regs(child);
  328. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  329. break;
  330. }
  331. case DSP_CONTROL:
  332. if (!cpu_has_dsp) {
  333. tmp = 0;
  334. ret = -EIO;
  335. goto out;
  336. }
  337. tmp = child->thread.dsp.dspcontrol;
  338. break;
  339. default:
  340. tmp = 0;
  341. ret = -EIO;
  342. goto out;
  343. }
  344. ret = put_user(tmp, (unsigned long __user *) data);
  345. break;
  346. }
  347. /* when I and D space are separate, this will have to be fixed. */
  348. case PTRACE_POKETEXT: /* write the word at location addr. */
  349. case PTRACE_POKEDATA:
  350. ret = generic_ptrace_pokedata(child, addr, data);
  351. break;
  352. case PTRACE_POKEUSR: {
  353. struct pt_regs *regs;
  354. ret = 0;
  355. regs = task_pt_regs(child);
  356. switch (addr) {
  357. case 0 ... 31:
  358. regs->regs[addr] = data;
  359. break;
  360. case FPR_BASE ... FPR_BASE + 31: {
  361. fpureg_t *fregs = get_fpu_regs(child);
  362. if (!tsk_used_math(child)) {
  363. /* FP not yet used */
  364. memset(&child->thread.fpu, ~0,
  365. sizeof(child->thread.fpu));
  366. child->thread.fpu.fcr31 = 0;
  367. }
  368. #ifdef CONFIG_32BIT
  369. /*
  370. * The odd registers are actually the high order bits
  371. * of the values stored in the even registers - unless
  372. * we're using r2k_switch.S.
  373. */
  374. if (addr & 1) {
  375. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  376. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  377. } else {
  378. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  379. fregs[addr - FPR_BASE] |= data;
  380. }
  381. #endif
  382. #ifdef CONFIG_64BIT
  383. fregs[addr - FPR_BASE] = data;
  384. #endif
  385. break;
  386. }
  387. case PC:
  388. regs->cp0_epc = data;
  389. break;
  390. case MMHI:
  391. regs->hi = data;
  392. break;
  393. case MMLO:
  394. regs->lo = data;
  395. break;
  396. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  397. case ACX:
  398. regs->acx = data;
  399. break;
  400. #endif
  401. case FPC_CSR:
  402. child->thread.fpu.fcr31 = data;
  403. break;
  404. case DSP_BASE ... DSP_BASE + 5: {
  405. dspreg_t *dregs;
  406. if (!cpu_has_dsp) {
  407. ret = -EIO;
  408. break;
  409. }
  410. dregs = __get_dsp_regs(child);
  411. dregs[addr - DSP_BASE] = data;
  412. break;
  413. }
  414. case DSP_CONTROL:
  415. if (!cpu_has_dsp) {
  416. ret = -EIO;
  417. break;
  418. }
  419. child->thread.dsp.dspcontrol = data;
  420. break;
  421. default:
  422. /* The rest are not allowed. */
  423. ret = -EIO;
  424. break;
  425. }
  426. break;
  427. }
  428. case PTRACE_GETREGS:
  429. ret = ptrace_getregs(child, (__s64 __user *) data);
  430. break;
  431. case PTRACE_SETREGS:
  432. ret = ptrace_setregs(child, (__s64 __user *) data);
  433. break;
  434. case PTRACE_GETFPREGS:
  435. ret = ptrace_getfpregs(child, (__u32 __user *) data);
  436. break;
  437. case PTRACE_SETFPREGS:
  438. ret = ptrace_setfpregs(child, (__u32 __user *) data);
  439. break;
  440. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  441. case PTRACE_CONT: { /* restart after signal. */
  442. ret = -EIO;
  443. if (!valid_signal(data))
  444. break;
  445. if (request == PTRACE_SYSCALL) {
  446. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  447. }
  448. else {
  449. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  450. }
  451. child->exit_code = data;
  452. wake_up_process(child);
  453. ret = 0;
  454. break;
  455. }
  456. /*
  457. * make the child exit. Best I can do is send it a sigkill.
  458. * perhaps it should be put in the status that it wants to
  459. * exit.
  460. */
  461. case PTRACE_KILL:
  462. ret = 0;
  463. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  464. break;
  465. child->exit_code = SIGKILL;
  466. wake_up_process(child);
  467. break;
  468. case PTRACE_GET_THREAD_AREA:
  469. ret = put_user(task_thread_info(child)->tp_value,
  470. (unsigned long __user *) data);
  471. break;
  472. case PTRACE_GET_WATCH_REGS:
  473. ret = ptrace_get_watch_regs(child,
  474. (struct pt_watch_regs __user *) addr);
  475. break;
  476. case PTRACE_SET_WATCH_REGS:
  477. ret = ptrace_set_watch_regs(child,
  478. (struct pt_watch_regs __user *) addr);
  479. break;
  480. default:
  481. ret = ptrace_request(child, request, addr, data);
  482. break;
  483. }
  484. out:
  485. return ret;
  486. }
  487. static inline int audit_arch(void)
  488. {
  489. int arch = EM_MIPS;
  490. #ifdef CONFIG_64BIT
  491. arch |= __AUDIT_ARCH_64BIT;
  492. #endif
  493. #if defined(__LITTLE_ENDIAN)
  494. arch |= __AUDIT_ARCH_LE;
  495. #endif
  496. return arch;
  497. }
  498. /*
  499. * Notification of system call entry/exit
  500. * - triggered by current->work.syscall_trace
  501. */
  502. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  503. {
  504. /* do the secure computing check first */
  505. if (!entryexit)
  506. secure_computing(regs->regs[0]);
  507. if (unlikely(current->audit_context) && entryexit)
  508. audit_syscall_exit(AUDITSC_RESULT(regs->regs[2]),
  509. regs->regs[2]);
  510. if (!(current->ptrace & PT_PTRACED))
  511. goto out;
  512. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  513. goto out;
  514. /* The 0x80 provides a way for the tracing parent to distinguish
  515. between a syscall stop and SIGTRAP delivery */
  516. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  517. 0x80 : 0));
  518. /*
  519. * this isn't the same as continuing with a signal, but it will do
  520. * for normal use. strace only continues with a signal if the
  521. * stopping signal is not SIGTRAP. -brl
  522. */
  523. if (current->exit_code) {
  524. send_sig(current->exit_code, current, 1);
  525. current->exit_code = 0;
  526. }
  527. out:
  528. if (unlikely(current->audit_context) && !entryexit)
  529. audit_syscall_entry(audit_arch(), regs->regs[0],
  530. regs->regs[4], regs->regs[5],
  531. regs->regs[6], regs->regs[7]);
  532. }