ptrace.c 13 KB

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