ptrace.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/uaccess.h>
  37. #include <asm/bootinfo.h>
  38. #include <asm/reg.h>
  39. /*
  40. * Called by kernel/ptrace.c when detaching..
  41. *
  42. * Make sure single step bits etc are not set.
  43. */
  44. void ptrace_disable(struct task_struct *child)
  45. {
  46. /* Don't load the watchpoint registers for the ex-child. */
  47. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  48. }
  49. /*
  50. * Read a general register set. We always use the 64-bit format, even
  51. * for 32-bit kernels and for 32-bit processes on a 64-bit kernel.
  52. * Registers are sign extended to fill the available space.
  53. */
  54. int ptrace_getregs(struct task_struct *child, __s64 __user *data)
  55. {
  56. struct pt_regs *regs;
  57. int i;
  58. if (!access_ok(VERIFY_WRITE, data, 38 * 8))
  59. return -EIO;
  60. regs = task_pt_regs(child);
  61. for (i = 0; i < 32; i++)
  62. __put_user((long)regs->regs[i], data + i);
  63. __put_user((long)regs->lo, data + EF_LO - EF_R0);
  64. __put_user((long)regs->hi, data + EF_HI - EF_R0);
  65. __put_user((long)regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
  66. __put_user((long)regs->cp0_badvaddr, data + EF_CP0_BADVADDR - EF_R0);
  67. __put_user((long)regs->cp0_status, data + EF_CP0_STATUS - EF_R0);
  68. __put_user((long)regs->cp0_cause, data + EF_CP0_CAUSE - EF_R0);
  69. return 0;
  70. }
  71. /*
  72. * Write a general register set. As for PTRACE_GETREGS, we always use
  73. * the 64-bit format. On a 32-bit kernel only the lower order half
  74. * (according to endianness) will be used.
  75. */
  76. int ptrace_setregs(struct task_struct *child, __s64 __user *data)
  77. {
  78. struct pt_regs *regs;
  79. int i;
  80. if (!access_ok(VERIFY_READ, data, 38 * 8))
  81. return -EIO;
  82. regs = task_pt_regs(child);
  83. for (i = 0; i < 32; i++)
  84. __get_user(regs->regs[i], data + i);
  85. __get_user(regs->lo, data + EF_LO - EF_R0);
  86. __get_user(regs->hi, data + EF_HI - EF_R0);
  87. __get_user(regs->cp0_epc, data + EF_CP0_EPC - EF_R0);
  88. /* badvaddr, status, and cause may not be written. */
  89. return 0;
  90. }
  91. int ptrace_getfpregs(struct task_struct *child, __u32 __user *data)
  92. {
  93. int i;
  94. unsigned int tmp;
  95. if (!access_ok(VERIFY_WRITE, data, 33 * 8))
  96. return -EIO;
  97. if (tsk_used_math(child)) {
  98. fpureg_t *fregs = get_fpu_regs(child);
  99. for (i = 0; i < 32; i++)
  100. __put_user(fregs[i], i + (__u64 __user *) data);
  101. } else {
  102. for (i = 0; i < 32; i++)
  103. __put_user((__u64) -1, i + (__u64 __user *) data);
  104. }
  105. __put_user(child->thread.fpu.fcr31, data + 64);
  106. preempt_disable();
  107. if (cpu_has_fpu) {
  108. unsigned int flags;
  109. if (cpu_has_mipsmt) {
  110. unsigned int vpflags = dvpe();
  111. flags = read_c0_status();
  112. __enable_fpu();
  113. __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
  114. write_c0_status(flags);
  115. evpe(vpflags);
  116. } else {
  117. flags = read_c0_status();
  118. __enable_fpu();
  119. __asm__ __volatile__("cfc1\t%0,$0" : "=r" (tmp));
  120. write_c0_status(flags);
  121. }
  122. } else {
  123. tmp = 0;
  124. }
  125. preempt_enable();
  126. __put_user(tmp, data + 65);
  127. return 0;
  128. }
  129. int ptrace_setfpregs(struct task_struct *child, __u32 __user *data)
  130. {
  131. fpureg_t *fregs;
  132. int i;
  133. if (!access_ok(VERIFY_READ, data, 33 * 8))
  134. return -EIO;
  135. fregs = get_fpu_regs(child);
  136. for (i = 0; i < 32; i++)
  137. __get_user(fregs[i], i + (__u64 __user *) data);
  138. __get_user(child->thread.fpu.fcr31, data + 64);
  139. /* FIR may not be written. */
  140. return 0;
  141. }
  142. int ptrace_get_watch_regs(struct task_struct *child,
  143. struct pt_watch_regs __user *addr)
  144. {
  145. enum pt_watch_style style;
  146. int i;
  147. if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
  148. return -EIO;
  149. if (!access_ok(VERIFY_WRITE, addr, sizeof(struct pt_watch_regs)))
  150. return -EIO;
  151. #ifdef CONFIG_32BIT
  152. style = pt_watch_style_mips32;
  153. #define WATCH_STYLE mips32
  154. #else
  155. style = pt_watch_style_mips64;
  156. #define WATCH_STYLE mips64
  157. #endif
  158. __put_user(style, &addr->style);
  159. __put_user(current_cpu_data.watch_reg_use_cnt,
  160. &addr->WATCH_STYLE.num_valid);
  161. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  162. __put_user(child->thread.watch.mips3264.watchlo[i],
  163. &addr->WATCH_STYLE.watchlo[i]);
  164. __put_user(child->thread.watch.mips3264.watchhi[i] & 0xfff,
  165. &addr->WATCH_STYLE.watchhi[i]);
  166. __put_user(current_cpu_data.watch_reg_masks[i],
  167. &addr->WATCH_STYLE.watch_masks[i]);
  168. }
  169. for (; i < 8; i++) {
  170. __put_user(0, &addr->WATCH_STYLE.watchlo[i]);
  171. __put_user(0, &addr->WATCH_STYLE.watchhi[i]);
  172. __put_user(0, &addr->WATCH_STYLE.watch_masks[i]);
  173. }
  174. return 0;
  175. }
  176. int ptrace_set_watch_regs(struct task_struct *child,
  177. struct pt_watch_regs __user *addr)
  178. {
  179. int i;
  180. int watch_active = 0;
  181. unsigned long lt[NUM_WATCH_REGS];
  182. u16 ht[NUM_WATCH_REGS];
  183. if (!cpu_has_watch || current_cpu_data.watch_reg_use_cnt == 0)
  184. return -EIO;
  185. if (!access_ok(VERIFY_READ, addr, sizeof(struct pt_watch_regs)))
  186. return -EIO;
  187. /* Check the values. */
  188. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  189. __get_user(lt[i], &addr->WATCH_STYLE.watchlo[i]);
  190. #ifdef CONFIG_32BIT
  191. if (lt[i] & __UA_LIMIT)
  192. return -EINVAL;
  193. #else
  194. if (test_tsk_thread_flag(child, TIF_32BIT_ADDR)) {
  195. if (lt[i] & 0xffffffff80000000UL)
  196. return -EINVAL;
  197. } else {
  198. if (lt[i] & __UA_LIMIT)
  199. return -EINVAL;
  200. }
  201. #endif
  202. __get_user(ht[i], &addr->WATCH_STYLE.watchhi[i]);
  203. if (ht[i] & ~0xff8)
  204. return -EINVAL;
  205. }
  206. /* Install them. */
  207. for (i = 0; i < current_cpu_data.watch_reg_use_cnt; i++) {
  208. if (lt[i] & 7)
  209. watch_active = 1;
  210. child->thread.watch.mips3264.watchlo[i] = lt[i];
  211. /* Set the G bit. */
  212. child->thread.watch.mips3264.watchhi[i] = ht[i];
  213. }
  214. if (watch_active)
  215. set_tsk_thread_flag(child, TIF_LOAD_WATCH);
  216. else
  217. clear_tsk_thread_flag(child, TIF_LOAD_WATCH);
  218. return 0;
  219. }
  220. long arch_ptrace(struct task_struct *child, long request,
  221. unsigned long addr, unsigned long data)
  222. {
  223. int ret;
  224. void __user *addrp = (void __user *) addr;
  225. void __user *datavp = (void __user *) data;
  226. unsigned long __user *datalp = (void __user *) data;
  227. switch (request) {
  228. /* when I and D space are separate, these will need to be fixed. */
  229. case PTRACE_PEEKTEXT: /* read word at location addr. */
  230. case PTRACE_PEEKDATA:
  231. ret = generic_ptrace_peekdata(child, addr, data);
  232. break;
  233. /* Read the word at location addr in the USER area. */
  234. case PTRACE_PEEKUSR: {
  235. struct pt_regs *regs;
  236. unsigned long tmp = 0;
  237. regs = task_pt_regs(child);
  238. ret = 0; /* Default return value. */
  239. switch (addr) {
  240. case 0 ... 31:
  241. tmp = regs->regs[addr];
  242. break;
  243. case FPR_BASE ... FPR_BASE + 31:
  244. if (tsk_used_math(child)) {
  245. fpureg_t *fregs = get_fpu_regs(child);
  246. #ifdef CONFIG_32BIT
  247. /*
  248. * The odd registers are actually the high
  249. * order bits of the values stored in the even
  250. * registers - unless we're using r2k_switch.S.
  251. */
  252. if (addr & 1)
  253. tmp = (unsigned long) (fregs[((addr & ~1) - 32)] >> 32);
  254. else
  255. tmp = (unsigned long) (fregs[(addr - 32)] & 0xffffffff);
  256. #endif
  257. #ifdef CONFIG_64BIT
  258. tmp = fregs[addr - FPR_BASE];
  259. #endif
  260. } else {
  261. tmp = -1; /* FP not yet used */
  262. }
  263. break;
  264. case PC:
  265. tmp = regs->cp0_epc;
  266. break;
  267. case CAUSE:
  268. tmp = regs->cp0_cause;
  269. break;
  270. case BADVADDR:
  271. tmp = regs->cp0_badvaddr;
  272. break;
  273. case MMHI:
  274. tmp = regs->hi;
  275. break;
  276. case MMLO:
  277. tmp = regs->lo;
  278. break;
  279. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  280. case ACX:
  281. tmp = regs->acx;
  282. break;
  283. #endif
  284. case FPC_CSR:
  285. tmp = child->thread.fpu.fcr31;
  286. break;
  287. case FPC_EIR: { /* implementation / version register */
  288. unsigned int flags;
  289. #ifdef CONFIG_MIPS_MT_SMTC
  290. unsigned long irqflags;
  291. unsigned int mtflags;
  292. #endif /* CONFIG_MIPS_MT_SMTC */
  293. preempt_disable();
  294. if (!cpu_has_fpu) {
  295. preempt_enable();
  296. break;
  297. }
  298. #ifdef CONFIG_MIPS_MT_SMTC
  299. /* Read-modify-write of Status must be atomic */
  300. local_irq_save(irqflags);
  301. mtflags = dmt();
  302. #endif /* CONFIG_MIPS_MT_SMTC */
  303. if (cpu_has_mipsmt) {
  304. unsigned int vpflags = dvpe();
  305. flags = read_c0_status();
  306. __enable_fpu();
  307. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  308. write_c0_status(flags);
  309. evpe(vpflags);
  310. } else {
  311. flags = read_c0_status();
  312. __enable_fpu();
  313. __asm__ __volatile__("cfc1\t%0,$0": "=r" (tmp));
  314. write_c0_status(flags);
  315. }
  316. #ifdef CONFIG_MIPS_MT_SMTC
  317. emt(mtflags);
  318. local_irq_restore(irqflags);
  319. #endif /* CONFIG_MIPS_MT_SMTC */
  320. preempt_enable();
  321. break;
  322. }
  323. case DSP_BASE ... DSP_BASE + 5: {
  324. dspreg_t *dregs;
  325. if (!cpu_has_dsp) {
  326. tmp = 0;
  327. ret = -EIO;
  328. goto out;
  329. }
  330. dregs = __get_dsp_regs(child);
  331. tmp = (unsigned long) (dregs[addr - DSP_BASE]);
  332. break;
  333. }
  334. case DSP_CONTROL:
  335. if (!cpu_has_dsp) {
  336. tmp = 0;
  337. ret = -EIO;
  338. goto out;
  339. }
  340. tmp = child->thread.dsp.dspcontrol;
  341. break;
  342. default:
  343. tmp = 0;
  344. ret = -EIO;
  345. goto out;
  346. }
  347. ret = put_user(tmp, datalp);
  348. break;
  349. }
  350. /* when I and D space are separate, this will have to be fixed. */
  351. case PTRACE_POKETEXT: /* write the word at location addr. */
  352. case PTRACE_POKEDATA:
  353. ret = generic_ptrace_pokedata(child, addr, data);
  354. break;
  355. case PTRACE_POKEUSR: {
  356. struct pt_regs *regs;
  357. ret = 0;
  358. regs = task_pt_regs(child);
  359. switch (addr) {
  360. case 0 ... 31:
  361. regs->regs[addr] = data;
  362. break;
  363. case FPR_BASE ... FPR_BASE + 31: {
  364. fpureg_t *fregs = get_fpu_regs(child);
  365. if (!tsk_used_math(child)) {
  366. /* FP not yet used */
  367. memset(&child->thread.fpu, ~0,
  368. sizeof(child->thread.fpu));
  369. child->thread.fpu.fcr31 = 0;
  370. }
  371. #ifdef CONFIG_32BIT
  372. /*
  373. * The odd registers are actually the high order bits
  374. * of the values stored in the even registers - unless
  375. * we're using r2k_switch.S.
  376. */
  377. if (addr & 1) {
  378. fregs[(addr & ~1) - FPR_BASE] &= 0xffffffff;
  379. fregs[(addr & ~1) - FPR_BASE] |= ((unsigned long long) data) << 32;
  380. } else {
  381. fregs[addr - FPR_BASE] &= ~0xffffffffLL;
  382. fregs[addr - FPR_BASE] |= data;
  383. }
  384. #endif
  385. #ifdef CONFIG_64BIT
  386. fregs[addr - FPR_BASE] = data;
  387. #endif
  388. break;
  389. }
  390. case PC:
  391. regs->cp0_epc = data;
  392. break;
  393. case MMHI:
  394. regs->hi = data;
  395. break;
  396. case MMLO:
  397. regs->lo = data;
  398. break;
  399. #ifdef CONFIG_CPU_HAS_SMARTMIPS
  400. case ACX:
  401. regs->acx = data;
  402. break;
  403. #endif
  404. case FPC_CSR:
  405. child->thread.fpu.fcr31 = data;
  406. break;
  407. case DSP_BASE ... DSP_BASE + 5: {
  408. dspreg_t *dregs;
  409. if (!cpu_has_dsp) {
  410. ret = -EIO;
  411. break;
  412. }
  413. dregs = __get_dsp_regs(child);
  414. dregs[addr - DSP_BASE] = data;
  415. break;
  416. }
  417. case DSP_CONTROL:
  418. if (!cpu_has_dsp) {
  419. ret = -EIO;
  420. break;
  421. }
  422. child->thread.dsp.dspcontrol = data;
  423. break;
  424. default:
  425. /* The rest are not allowed. */
  426. ret = -EIO;
  427. break;
  428. }
  429. break;
  430. }
  431. case PTRACE_GETREGS:
  432. ret = ptrace_getregs(child, datavp);
  433. break;
  434. case PTRACE_SETREGS:
  435. ret = ptrace_setregs(child, datavp);
  436. break;
  437. case PTRACE_GETFPREGS:
  438. ret = ptrace_getfpregs(child, datavp);
  439. break;
  440. case PTRACE_SETFPREGS:
  441. ret = ptrace_setfpregs(child, datavp);
  442. break;
  443. case PTRACE_GET_THREAD_AREA:
  444. ret = put_user(task_thread_info(child)->tp_value, datalp);
  445. break;
  446. case PTRACE_GET_WATCH_REGS:
  447. ret = ptrace_get_watch_regs(child, addrp);
  448. break;
  449. case PTRACE_SET_WATCH_REGS:
  450. ret = ptrace_set_watch_regs(child, addrp);
  451. break;
  452. default:
  453. ret = ptrace_request(child, request, addr, data);
  454. break;
  455. }
  456. out:
  457. return ret;
  458. }
  459. static inline int audit_arch(void)
  460. {
  461. int arch = EM_MIPS;
  462. #ifdef CONFIG_64BIT
  463. arch |= __AUDIT_ARCH_64BIT;
  464. #endif
  465. #if defined(__LITTLE_ENDIAN)
  466. arch |= __AUDIT_ARCH_LE;
  467. #endif
  468. return arch;
  469. }
  470. /*
  471. * Notification of system call entry/exit
  472. * - triggered by current->work.syscall_trace
  473. */
  474. asmlinkage void syscall_trace_enter(struct pt_regs *regs)
  475. {
  476. /* do the secure computing check first */
  477. secure_computing_strict(regs->regs[2]);
  478. if (!(current->ptrace & PT_PTRACED))
  479. goto out;
  480. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  481. goto out;
  482. /* The 0x80 provides a way for the tracing parent to distinguish
  483. between a syscall stop and SIGTRAP delivery */
  484. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  485. 0x80 : 0));
  486. /*
  487. * this isn't the same as continuing with a signal, but it will do
  488. * for normal use. strace only continues with a signal if the
  489. * stopping signal is not SIGTRAP. -brl
  490. */
  491. if (current->exit_code) {
  492. send_sig(current->exit_code, current, 1);
  493. current->exit_code = 0;
  494. }
  495. out:
  496. audit_syscall_entry(audit_arch(), regs->regs[2],
  497. regs->regs[4], regs->regs[5],
  498. regs->regs[6], regs->regs[7]);
  499. }
  500. /*
  501. * Notification of system call entry/exit
  502. * - triggered by current->work.syscall_trace
  503. */
  504. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  505. {
  506. audit_syscall_exit(regs);
  507. if (!(current->ptrace & PT_PTRACED))
  508. return;
  509. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  510. return;
  511. /* The 0x80 provides a way for the tracing parent to distinguish
  512. between a syscall stop and SIGTRAP delivery */
  513. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  514. 0x80 : 0));
  515. /*
  516. * this isn't the same as continuing with a signal, but it will do
  517. * for normal use. strace only continues with a signal if the
  518. * stopping signal is not SIGTRAP. -brl
  519. */
  520. if (current->exit_code) {
  521. send_sig(current->exit_code, current, 1);
  522. current->exit_code = 0;
  523. }
  524. }