ptrace.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // TODO some minor issues
  2. /*
  3. * This file is subject to the terms and conditions of the GNU General Public
  4. * License. See the file "COPYING" in the main directory of this archive
  5. * for more details.
  6. *
  7. * Copyright (C) 2001 - 2007 Tensilica Inc.
  8. *
  9. * Joe Taylor <joe@tensilica.com, joetylr@yahoo.com>
  10. * Chris Zankel <chris@zankel.net>
  11. * Scott Foehner<sfoehner@yahoo.com>,
  12. * Kevin Chea
  13. * Marc Gauthier<marc@tensilica.com> <marc@alumni.uwaterloo.ca>
  14. */
  15. #include <linux/errno.h>
  16. #include <linux/hw_breakpoint.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mm.h>
  19. #include <linux/perf_event.h>
  20. #include <linux/ptrace.h>
  21. #include <linux/sched.h>
  22. #include <linux/security.h>
  23. #include <linux/signal.h>
  24. #include <linux/smp.h>
  25. #include <asm/coprocessor.h>
  26. #include <asm/elf.h>
  27. #include <asm/page.h>
  28. #include <asm/pgtable.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/uaccess.h>
  31. void user_enable_single_step(struct task_struct *child)
  32. {
  33. child->ptrace |= PT_SINGLESTEP;
  34. }
  35. void user_disable_single_step(struct task_struct *child)
  36. {
  37. child->ptrace &= ~PT_SINGLESTEP;
  38. }
  39. /*
  40. * Called by kernel/ptrace.c when detaching to disable single stepping.
  41. */
  42. void ptrace_disable(struct task_struct *child)
  43. {
  44. /* Nothing to do.. */
  45. }
  46. int ptrace_getregs(struct task_struct *child, void __user *uregs)
  47. {
  48. struct pt_regs *regs = task_pt_regs(child);
  49. xtensa_gregset_t __user *gregset = uregs;
  50. unsigned long wb = regs->windowbase;
  51. int i;
  52. if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
  53. return -EIO;
  54. __put_user(regs->pc, &gregset->pc);
  55. __put_user(regs->ps & ~(1 << PS_EXCM_BIT), &gregset->ps);
  56. __put_user(regs->lbeg, &gregset->lbeg);
  57. __put_user(regs->lend, &gregset->lend);
  58. __put_user(regs->lcount, &gregset->lcount);
  59. __put_user(regs->windowstart, &gregset->windowstart);
  60. __put_user(regs->windowbase, &gregset->windowbase);
  61. __put_user(regs->threadptr, &gregset->threadptr);
  62. for (i = 0; i < XCHAL_NUM_AREGS; i++)
  63. __put_user(regs->areg[i],
  64. gregset->a + ((wb * 4 + i) % XCHAL_NUM_AREGS));
  65. return 0;
  66. }
  67. int ptrace_setregs(struct task_struct *child, void __user *uregs)
  68. {
  69. struct pt_regs *regs = task_pt_regs(child);
  70. xtensa_gregset_t *gregset = uregs;
  71. const unsigned long ps_mask = PS_CALLINC_MASK | PS_OWB_MASK;
  72. unsigned long ps;
  73. unsigned long wb, ws;
  74. if (!access_ok(VERIFY_WRITE, uregs, sizeof(xtensa_gregset_t)))
  75. return -EIO;
  76. __get_user(regs->pc, &gregset->pc);
  77. __get_user(ps, &gregset->ps);
  78. __get_user(regs->lbeg, &gregset->lbeg);
  79. __get_user(regs->lend, &gregset->lend);
  80. __get_user(regs->lcount, &gregset->lcount);
  81. __get_user(ws, &gregset->windowstart);
  82. __get_user(wb, &gregset->windowbase);
  83. __get_user(regs->threadptr, &gregset->threadptr);
  84. regs->ps = (regs->ps & ~ps_mask) | (ps & ps_mask) | (1 << PS_EXCM_BIT);
  85. if (wb >= XCHAL_NUM_AREGS / 4)
  86. return -EFAULT;
  87. if (wb != regs->windowbase || ws != regs->windowstart) {
  88. unsigned long rotws, wmask;
  89. rotws = (((ws | (ws << WSBITS)) >> wb) &
  90. ((1 << WSBITS) - 1)) & ~1;
  91. wmask = ((rotws ? WSBITS + 1 - ffs(rotws) : 0) << 4) |
  92. (rotws & 0xF) | 1;
  93. regs->windowbase = wb;
  94. regs->windowstart = ws;
  95. regs->wmask = wmask;
  96. }
  97. if (wb != 0 && __copy_from_user(regs->areg + XCHAL_NUM_AREGS - wb * 4,
  98. gregset->a, wb * 16))
  99. return -EFAULT;
  100. if (__copy_from_user(regs->areg, gregset->a + wb * 4,
  101. (WSBITS - wb) * 16))
  102. return -EFAULT;
  103. return 0;
  104. }
  105. int ptrace_getxregs(struct task_struct *child, void __user *uregs)
  106. {
  107. struct pt_regs *regs = task_pt_regs(child);
  108. struct thread_info *ti = task_thread_info(child);
  109. elf_xtregs_t __user *xtregs = uregs;
  110. int ret = 0;
  111. if (!access_ok(VERIFY_WRITE, uregs, sizeof(elf_xtregs_t)))
  112. return -EIO;
  113. #if XTENSA_HAVE_COPROCESSORS
  114. /* Flush all coprocessor registers to memory. */
  115. coprocessor_flush_all(ti);
  116. ret |= __copy_to_user(&xtregs->cp0, &ti->xtregs_cp,
  117. sizeof(xtregs_coprocessor_t));
  118. #endif
  119. ret |= __copy_to_user(&xtregs->opt, &regs->xtregs_opt,
  120. sizeof(xtregs->opt));
  121. ret |= __copy_to_user(&xtregs->user,&ti->xtregs_user,
  122. sizeof(xtregs->user));
  123. return ret ? -EFAULT : 0;
  124. }
  125. int ptrace_setxregs(struct task_struct *child, void __user *uregs)
  126. {
  127. struct thread_info *ti = task_thread_info(child);
  128. struct pt_regs *regs = task_pt_regs(child);
  129. elf_xtregs_t *xtregs = uregs;
  130. int ret = 0;
  131. if (!access_ok(VERIFY_READ, uregs, sizeof(elf_xtregs_t)))
  132. return -EFAULT;
  133. #if XTENSA_HAVE_COPROCESSORS
  134. /* Flush all coprocessors before we overwrite them. */
  135. coprocessor_flush_all(ti);
  136. coprocessor_release_all(ti);
  137. ret |= __copy_from_user(&ti->xtregs_cp, &xtregs->cp0,
  138. sizeof(xtregs_coprocessor_t));
  139. #endif
  140. ret |= __copy_from_user(&regs->xtregs_opt, &xtregs->opt,
  141. sizeof(xtregs->opt));
  142. ret |= __copy_from_user(&ti->xtregs_user, &xtregs->user,
  143. sizeof(xtregs->user));
  144. return ret ? -EFAULT : 0;
  145. }
  146. int ptrace_peekusr(struct task_struct *child, long regno, long __user *ret)
  147. {
  148. struct pt_regs *regs;
  149. unsigned long tmp;
  150. regs = task_pt_regs(child);
  151. tmp = 0; /* Default return value. */
  152. switch(regno) {
  153. case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
  154. tmp = regs->areg[regno - REG_AR_BASE];
  155. break;
  156. case REG_A_BASE ... REG_A_BASE + 15:
  157. tmp = regs->areg[regno - REG_A_BASE];
  158. break;
  159. case REG_PC:
  160. tmp = regs->pc;
  161. break;
  162. case REG_PS:
  163. /* Note: PS.EXCM is not set while user task is running;
  164. * its being set in regs is for exception handling
  165. * convenience. */
  166. tmp = (regs->ps & ~(1 << PS_EXCM_BIT));
  167. break;
  168. case REG_WB:
  169. break; /* tmp = 0 */
  170. case REG_WS:
  171. {
  172. unsigned long wb = regs->windowbase;
  173. unsigned long ws = regs->windowstart;
  174. tmp = ((ws>>wb) | (ws<<(WSBITS-wb))) & ((1<<WSBITS)-1);
  175. break;
  176. }
  177. case REG_LBEG:
  178. tmp = regs->lbeg;
  179. break;
  180. case REG_LEND:
  181. tmp = regs->lend;
  182. break;
  183. case REG_LCOUNT:
  184. tmp = regs->lcount;
  185. break;
  186. case REG_SAR:
  187. tmp = regs->sar;
  188. break;
  189. case SYSCALL_NR:
  190. tmp = regs->syscall;
  191. break;
  192. default:
  193. return -EIO;
  194. }
  195. return put_user(tmp, ret);
  196. }
  197. int ptrace_pokeusr(struct task_struct *child, long regno, long val)
  198. {
  199. struct pt_regs *regs;
  200. regs = task_pt_regs(child);
  201. switch (regno) {
  202. case REG_AR_BASE ... REG_AR_BASE + XCHAL_NUM_AREGS - 1:
  203. regs->areg[regno - REG_AR_BASE] = val;
  204. break;
  205. case REG_A_BASE ... REG_A_BASE + 15:
  206. regs->areg[regno - REG_A_BASE] = val;
  207. break;
  208. case REG_PC:
  209. regs->pc = val;
  210. break;
  211. case SYSCALL_NR:
  212. regs->syscall = val;
  213. break;
  214. default:
  215. return -EIO;
  216. }
  217. return 0;
  218. }
  219. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  220. static void ptrace_hbptriggered(struct perf_event *bp,
  221. struct perf_sample_data *data,
  222. struct pt_regs *regs)
  223. {
  224. int i;
  225. siginfo_t info;
  226. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  227. if (bp->attr.bp_type & HW_BREAKPOINT_X) {
  228. for (i = 0; i < XCHAL_NUM_IBREAK; ++i)
  229. if (current->thread.ptrace_bp[i] == bp)
  230. break;
  231. i <<= 1;
  232. } else {
  233. for (i = 0; i < XCHAL_NUM_DBREAK; ++i)
  234. if (current->thread.ptrace_wp[i] == bp)
  235. break;
  236. i = (i << 1) | 1;
  237. }
  238. info.si_signo = SIGTRAP;
  239. info.si_errno = i;
  240. info.si_code = TRAP_HWBKPT;
  241. info.si_addr = (void __user *)bkpt->address;
  242. force_sig_info(SIGTRAP, &info, current);
  243. }
  244. static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
  245. {
  246. struct perf_event_attr attr;
  247. ptrace_breakpoint_init(&attr);
  248. /* Initialise fields to sane defaults. */
  249. attr.bp_addr = 0;
  250. attr.bp_len = 1;
  251. attr.bp_type = type;
  252. attr.disabled = 1;
  253. return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
  254. tsk);
  255. }
  256. /*
  257. * Address bit 0 choose instruction (0) or data (1) break register, bits
  258. * 31..1 are the register number.
  259. * Both PTRACE_GETHBPREGS and PTRACE_SETHBPREGS transfer two 32-bit words:
  260. * address (0) and control (1).
  261. * Instruction breakpoint contorl word is 0 to clear breakpoint, 1 to set.
  262. * Data breakpoint control word bit 31 is 'trigger on store', bit 30 is
  263. * 'trigger on load, bits 29..0 are length. Length 0 is used to clear a
  264. * breakpoint. To set a breakpoint length must be a power of 2 in the range
  265. * 1..64 and the address must be length-aligned.
  266. */
  267. static long ptrace_gethbpregs(struct task_struct *child, long addr,
  268. long __user *datap)
  269. {
  270. struct perf_event *bp;
  271. u32 user_data[2] = {0};
  272. bool dbreak = addr & 1;
  273. unsigned idx = addr >> 1;
  274. if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
  275. (dbreak && idx >= XCHAL_NUM_DBREAK))
  276. return -EINVAL;
  277. if (dbreak)
  278. bp = child->thread.ptrace_wp[idx];
  279. else
  280. bp = child->thread.ptrace_bp[idx];
  281. if (bp) {
  282. user_data[0] = bp->attr.bp_addr;
  283. user_data[1] = bp->attr.disabled ? 0 : bp->attr.bp_len;
  284. if (dbreak) {
  285. if (bp->attr.bp_type & HW_BREAKPOINT_R)
  286. user_data[1] |= DBREAKC_LOAD_MASK;
  287. if (bp->attr.bp_type & HW_BREAKPOINT_W)
  288. user_data[1] |= DBREAKC_STOR_MASK;
  289. }
  290. }
  291. if (copy_to_user(datap, user_data, sizeof(user_data)))
  292. return -EFAULT;
  293. return 0;
  294. }
  295. static long ptrace_sethbpregs(struct task_struct *child, long addr,
  296. long __user *datap)
  297. {
  298. struct perf_event *bp;
  299. struct perf_event_attr attr;
  300. u32 user_data[2];
  301. bool dbreak = addr & 1;
  302. unsigned idx = addr >> 1;
  303. int bp_type = 0;
  304. if ((!dbreak && idx >= XCHAL_NUM_IBREAK) ||
  305. (dbreak && idx >= XCHAL_NUM_DBREAK))
  306. return -EINVAL;
  307. if (copy_from_user(user_data, datap, sizeof(user_data)))
  308. return -EFAULT;
  309. if (dbreak) {
  310. bp = child->thread.ptrace_wp[idx];
  311. if (user_data[1] & DBREAKC_LOAD_MASK)
  312. bp_type |= HW_BREAKPOINT_R;
  313. if (user_data[1] & DBREAKC_STOR_MASK)
  314. bp_type |= HW_BREAKPOINT_W;
  315. } else {
  316. bp = child->thread.ptrace_bp[idx];
  317. bp_type = HW_BREAKPOINT_X;
  318. }
  319. if (!bp) {
  320. bp = ptrace_hbp_create(child,
  321. bp_type ? bp_type : HW_BREAKPOINT_RW);
  322. if (IS_ERR(bp))
  323. return PTR_ERR(bp);
  324. if (dbreak)
  325. child->thread.ptrace_wp[idx] = bp;
  326. else
  327. child->thread.ptrace_bp[idx] = bp;
  328. }
  329. attr = bp->attr;
  330. attr.bp_addr = user_data[0];
  331. attr.bp_len = user_data[1] & ~(DBREAKC_LOAD_MASK | DBREAKC_STOR_MASK);
  332. attr.bp_type = bp_type;
  333. attr.disabled = !attr.bp_len;
  334. return modify_user_hw_breakpoint(bp, &attr);
  335. }
  336. #endif
  337. long arch_ptrace(struct task_struct *child, long request,
  338. unsigned long addr, unsigned long data)
  339. {
  340. int ret = -EPERM;
  341. void __user *datap = (void __user *) data;
  342. switch (request) {
  343. case PTRACE_PEEKTEXT: /* read word at location addr. */
  344. case PTRACE_PEEKDATA:
  345. ret = generic_ptrace_peekdata(child, addr, data);
  346. break;
  347. case PTRACE_PEEKUSR: /* read register specified by addr. */
  348. ret = ptrace_peekusr(child, addr, datap);
  349. break;
  350. case PTRACE_POKETEXT: /* write the word at location addr. */
  351. case PTRACE_POKEDATA:
  352. ret = generic_ptrace_pokedata(child, addr, data);
  353. break;
  354. case PTRACE_POKEUSR: /* write register specified by addr. */
  355. ret = ptrace_pokeusr(child, addr, data);
  356. break;
  357. case PTRACE_GETREGS:
  358. ret = ptrace_getregs(child, datap);
  359. break;
  360. case PTRACE_SETREGS:
  361. ret = ptrace_setregs(child, datap);
  362. break;
  363. case PTRACE_GETXTREGS:
  364. ret = ptrace_getxregs(child, datap);
  365. break;
  366. case PTRACE_SETXTREGS:
  367. ret = ptrace_setxregs(child, datap);
  368. break;
  369. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  370. case PTRACE_GETHBPREGS:
  371. ret = ptrace_gethbpregs(child, addr, datap);
  372. break;
  373. case PTRACE_SETHBPREGS:
  374. ret = ptrace_sethbpregs(child, addr, datap);
  375. break;
  376. #endif
  377. default:
  378. ret = ptrace_request(child, request, addr, data);
  379. break;
  380. }
  381. return ret;
  382. }
  383. void do_syscall_trace(void)
  384. {
  385. /*
  386. * The 0x80 provides a way for the tracing parent to distinguish
  387. * between a syscall stop and SIGTRAP delivery
  388. */
  389. ptrace_notify(SIGTRAP|((current->ptrace & PT_TRACESYSGOOD) ? 0x80 : 0));
  390. /*
  391. * this isn't the same as continuing with a signal, but it will do
  392. * for normal use. strace only continues with a signal if the
  393. * stopping signal is not SIGTRAP. -brl
  394. */
  395. if (current->exit_code) {
  396. send_sig(current->exit_code, current, 1);
  397. current->exit_code = 0;
  398. }
  399. }
  400. void do_syscall_trace_enter(struct pt_regs *regs)
  401. {
  402. if (test_thread_flag(TIF_SYSCALL_TRACE)
  403. && (current->ptrace & PT_PTRACED))
  404. do_syscall_trace();
  405. #if 0
  406. audit_syscall_entry(...);
  407. #endif
  408. }
  409. void do_syscall_trace_leave(struct pt_regs *regs)
  410. {
  411. if ((test_thread_flag(TIF_SYSCALL_TRACE))
  412. && (current->ptrace & PT_PTRACED))
  413. do_syscall_trace();
  414. }