ptrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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,
  222. unsigned long addr, unsigned long data)
  223. {
  224. int ret;
  225. void __user *addrp = (void __user *) addr;
  226. void __user *datavp = (void __user *) data;
  227. unsigned long __user *datalp = (void __user *) data;
  228. switch (request) {
  229. /* when I and D space are separate, these will need to be fixed. */
  230. case PTRACE_PEEKTEXT: /* read word at location addr. */
  231. case PTRACE_PEEKDATA:
  232. ret = generic_ptrace_peekdata(child, addr, data);
  233. break;
  234. /* Read the word at location addr in the USER area. */
  235. case PTRACE_PEEKUSR: {
  236. struct pt_regs *regs;
  237. unsigned long tmp = 0;
  238. regs = task_pt_regs(child);
  239. ret = 0; /* Default return value. */
  240. switch (addr) {
  241. case 0 ... 31:
  242. tmp = regs->regs[addr];
  243. break;
  244. case FPR_BASE ... FPR_BASE + 31:
  245. if (tsk_used_math(child)) {
  246. fpureg_t *fregs = get_fpu_regs(child);
  247. #ifdef CONFIG_32BIT
  248. /*
  249. * The odd registers are actually the high
  250. * order bits of the values stored in the even
  251. * registers - unless we're using r2k_switch.S.
  252. */
  253. if (addr & 1)
  254. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  255. else
  256. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  257. #endif
  258. #ifdef CONFIG_64BIT
  259. tmp = fregs[addr - FPR_BASE];
  260. #endif
  261. } else {
  262. tmp = -1; /* FP not yet used */
  263. }
  264. break;
  265. case PC:
  266. tmp = regs->cp0_epc;
  267. break;
  268. case CAUSE:
  269. tmp = regs->cp0_cause;
  270. break;
  271. case BADVADDR:
  272. tmp = regs->cp0_badvaddr;
  273. break;
  274. case MMHI:
  275. tmp = regs->hi;
  276. break;
  277. case MMLO:
  278. tmp = regs->lo;
  279. break;
  280. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  281. case ACX:
  282. tmp = regs->acx;
  283. break;
  284. #endif
  285. case FPC_CSR:
  286. tmp = child->thread.fpu.fcr31;
  287. break;
  288. case FPC_EIR: { /* implementation / version register */
  289. unsigned int flags;
  290. #ifdef CONFIG_MIPS_MT_SMTC
  291. unsigned long irqflags;
  292. unsigned int mtflags;
  293. #endif /* CONFIG_MIPS_MT_SMTC */
  294. preempt_disable();
  295. if (!cpu_has_fpu) {
  296. preempt_enable();
  297. break;
  298. }
  299. #ifdef CONFIG_MIPS_MT_SMTC
  300. /* Read-modify-write of Status must be atomic */
  301. local_irq_save(irqflags);
  302. mtflags = dmt();
  303. #endif /* CONFIG_MIPS_MT_SMTC */
  304. if (cpu_has_mipsmt) {
  305. unsigned int vpflags = dvpe();
  306. flags = read_c0_status();
  307. __enable_fpu();
  308. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  309. write_c0_status(flags);
  310. evpe(vpflags);
  311. } else {
  312. flags = read_c0_status();
  313. __enable_fpu();
  314. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  315. write_c0_status(flags);
  316. }
  317. #ifdef CONFIG_MIPS_MT_SMTC
  318. emt(mtflags);
  319. local_irq_restore(irqflags);
  320. #endif /* CONFIG_MIPS_MT_SMTC */
  321. preempt_enable();
  322. break;
  323. }
  324. case DSP_BASE ... DSP_BASE + 5: {
  325. dspreg_t *dregs;
  326. if (!cpu_has_dsp) {
  327. tmp = 0;
  328. ret = -EIO;
  329. goto out;
  330. }
  331. dregs = __get_dsp_regs(child);
  332. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  333. break;
  334. }
  335. case DSP_CONTROL:
  336. if (!cpu_has_dsp) {
  337. tmp = 0;
  338. ret = -EIO;
  339. goto out;
  340. }
  341. tmp = child->thread.dsp.dspcontrol;
  342. break;
  343. default:
  344. tmp = 0;
  345. ret = -EIO;
  346. goto out;
  347. }
  348. ret = put_user(tmp, datalp);
  349. break;
  350. }
  351. /* when I and D space are separate, this will have to be fixed. */
  352. case PTRACE_POKETEXT: /* write the word at location addr. */
  353. case PTRACE_POKEDATA:
  354. ret = generic_ptrace_pokedata(child, addr, data);
  355. break;
  356. case PTRACE_POKEUSR: {
  357. struct pt_regs *regs;
  358. ret = 0;
  359. regs = task_pt_regs(child);
  360. switch (addr) {
  361. case 0 ... 31:
  362. regs->regs[addr] = data;
  363. break;
  364. case FPR_BASE ... FPR_BASE + 31: {
  365. fpureg_t *fregs = get_fpu_regs(child);
  366. if (!tsk_used_math(child)) {
  367. /* FP not yet used */
  368. memset(&child->thread.fpu, ~0,
  369. sizeof(child->thread.fpu));
  370. child->thread.fpu.fcr31 = 0;
  371. }
  372. #ifdef CONFIG_32BIT
  373. /*
  374. * The odd registers are actually the high order bits
  375. * of the values stored in the even registers - unless
  376. * we're using r2k_switch.S.
  377. */
  378. if (addr & 1) {
  379. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  380. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  381. } else {
  382. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  383. fregs[addr - FPR_BASE] |= data;
  384. }
  385. #endif
  386. #ifdef CONFIG_64BIT
  387. fregs[addr - FPR_BASE] = data;
  388. #endif
  389. break;
  390. }
  391. case PC:
  392. regs->cp0_epc = data;
  393. break;
  394. case MMHI:
  395. regs->hi = data;
  396. break;
  397. case MMLO:
  398. regs->lo = data;
  399. break;
  400. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  401. case ACX:
  402. regs->acx = data;
  403. break;
  404. #endif
  405. case FPC_CSR:
  406. child->thread.fpu.fcr31 = data;
  407. break;
  408. case DSP_BASE ... DSP_BASE + 5: {
  409. dspreg_t *dregs;
  410. if (!cpu_has_dsp) {
  411. ret = -EIO;
  412. break;
  413. }
  414. dregs = __get_dsp_regs(child);
  415. dregs[addr - DSP_BASE] = data;
  416. break;
  417. }
  418. case DSP_CONTROL:
  419. if (!cpu_has_dsp) {
  420. ret = -EIO;
  421. break;
  422. }
  423. child->thread.dsp.dspcontrol = data;
  424. break;
  425. default:
  426. /* The rest are not allowed. */
  427. ret = -EIO;
  428. break;
  429. }
  430. break;
  431. }
  432. case PTRACE_GETREGS:
  433. ret = ptrace_getregs(child, datavp);
  434. break;
  435. case PTRACE_SETREGS:
  436. ret = ptrace_setregs(child, datavp);
  437. break;
  438. case PTRACE_GETFPREGS:
  439. ret = ptrace_getfpregs(child, datavp);
  440. break;
  441. case PTRACE_SETFPREGS:
  442. ret = ptrace_setfpregs(child, datavp);
  443. break;
  444. case PTRACE_GET_THREAD_AREA:
  445. ret = put_user(task_thread_info(child)->tp_value, datalp);
  446. break;
  447. case PTRACE_GET_WATCH_REGS:
  448. ret = ptrace_get_watch_regs(child, addrp);
  449. break;
  450. case PTRACE_SET_WATCH_REGS:
  451. ret = ptrace_set_watch_regs(child, addrp);
  452. break;
  453. default:
  454. ret = ptrace_request(child, request, addr, data);
  455. break;
  456. }
  457. out:
  458. return ret;
  459. }
  460. static inline int audit_arch(void)
  461. {
  462. int arch = EM_MIPS;
  463. #ifdef CONFIG_64BIT
  464. arch |= __AUDIT_ARCH_64BIT;
  465. #endif
  466. #if defined(__LITTLE_ENDIAN)
  467. arch |= __AUDIT_ARCH_LE;
  468. #endif
  469. return arch;
  470. }
  471. /*
  472. * Notification of system call entry/exit
  473. * - triggered by current->work.syscall_trace
  474. */
  475. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  476. {
  477. /* do the secure computing check first */
  478. secure_computing(regs->regs[2]);
  479. if (!(current->ptrace & PT_PTRACED))
  480. goto out;
  481. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  482. goto out;
  483. /* The 0x80 provides a way for the tracing parent to distinguish
  484. between a syscall stop and SIGTRAP delivery */
  485. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  486. 0x80 : 0));
  487. /*
  488. * this isn't the same as continuing with a signal, but it will do
  489. * for normal use. strace only continues with a signal if the
  490. * stopping signal is not SIGTRAP. -brl
  491. */
  492. if (current->exit_code) {
  493. send_sig(current->exit_code, current, 1);
  494. current->exit_code = 0;
  495. }
  496. out:
  497. if (unlikely(current->audit_context))
  498. audit_syscall_entry(audit_arch(), regs->regs[2],
  499. regs->regs[4], regs->regs[5],
  500. regs->regs[6], regs->regs[7]);
  501. }
  502. /*
  503. * Notification of system call entry/exit
  504. * - triggered by current->work.syscall_trace
  505. */
  506. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  507. {
  508. if (unlikely(current->audit_context))
  509. audit_syscall_exit(AUDITSC_RESULT(regs->regs[7]),
  510. -regs->regs[2]);
  511. if (!(current->ptrace & PT_PTRACED))
  512. return;
  513. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  514. return;
  515. /* The 0x80 provides a way for the tracing parent to distinguish
  516. between a syscall stop and SIGTRAP delivery */
  517. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  518. 0x80 : 0));
  519. /*
  520. * this isn't the same as continuing with a signal, but it will do
  521. * for normal use. strace only continues with a signal if the
  522. * stopping signal is not SIGTRAP. -brl
  523. */
  524. if (current->exit_code) {
  525. send_sig(current->exit_code, current, 1);
  526. current->exit_code = 0;
  527. }
  528. }