ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. /*
  2. * Copyright (C) 2000-2007, Axis Communications AB.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/sched.h>
  6. #include <linux/mm.h>
  7. #include <linux/smp.h>
  8. #include <linux/errno.h>
  9. #include <linux/ptrace.h>
  10. #include <linux/user.h>
  11. #include <linux/signal.h>
  12. #include <linux/security.h>
  13. #include <asm/uaccess.h>
  14. #include <asm/page.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/system.h>
  17. #include <asm/processor.h>
  18. #include <asm/arch/hwregs/supp_reg.h>
  19. /*
  20. * Determines which bits in CCS the user has access to.
  21. * 1 = access, 0 = no access.
  22. */
  23. #define CCS_MASK 0x00087c00 /* SXNZVC */
  24. #define SBIT_USER (1 << (S_CCS_BITNR + CCS_SHIFT))
  25. static int put_debugreg(long pid, unsigned int regno, long data);
  26. static long get_debugreg(long pid, unsigned int regno);
  27. static unsigned long get_pseudo_pc(struct task_struct *child);
  28. void deconfigure_bp(long pid);
  29. extern unsigned long cris_signal_return_page;
  30. /*
  31. * Get contents of register REGNO in task TASK.
  32. */
  33. long get_reg(struct task_struct *task, unsigned int regno)
  34. {
  35. /* USP is a special case, it's not in the pt_regs struct but
  36. * in the tasks thread struct
  37. */
  38. unsigned long ret;
  39. if (regno <= PT_EDA)
  40. ret = ((unsigned long *)task_pt_regs(task))[regno];
  41. else if (regno == PT_USP)
  42. ret = task->thread.usp;
  43. else if (regno == PT_PPC)
  44. ret = get_pseudo_pc(task);
  45. else if (regno <= PT_MAX)
  46. ret = get_debugreg(task->pid, regno);
  47. else
  48. ret = 0;
  49. return ret;
  50. }
  51. /*
  52. * Write contents of register REGNO in task TASK.
  53. */
  54. int put_reg(struct task_struct *task, unsigned int regno, unsigned long data)
  55. {
  56. if (regno <= PT_EDA)
  57. ((unsigned long *)task_pt_regs(task))[regno] = data;
  58. else if (regno == PT_USP)
  59. task->thread.usp = data;
  60. else if (regno == PT_PPC) {
  61. /* Write pseudo-PC to ERP only if changed. */
  62. if (data != get_pseudo_pc(task))
  63. task_pt_regs(task)->erp = data;
  64. } else if (regno <= PT_MAX)
  65. return put_debugreg(task->pid, regno, data);
  66. else
  67. return -1;
  68. return 0;
  69. }
  70. /*
  71. * Called by kernel/ptrace.c when detaching.
  72. *
  73. * Make sure the single step bit is not set.
  74. */
  75. void
  76. ptrace_disable(struct task_struct *child)
  77. {
  78. unsigned long tmp;
  79. /* Deconfigure SPC and S-bit. */
  80. tmp = get_reg(child, PT_CCS) & ~SBIT_USER;
  81. put_reg(child, PT_CCS, tmp);
  82. put_reg(child, PT_SPC, 0);
  83. /* Deconfigure any watchpoints associated with the child. */
  84. deconfigure_bp(child->pid);
  85. }
  86. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  87. {
  88. int ret;
  89. unsigned long __user *datap = (unsigned long __user *)data;
  90. switch (request) {
  91. /* Read word at location address. */
  92. case PTRACE_PEEKTEXT:
  93. case PTRACE_PEEKDATA: {
  94. unsigned long tmp;
  95. int copied;
  96. ret = -EIO;
  97. /* The signal trampoline page is outside the normal user-addressable
  98. * space but still accessible. This is hack to make it possible to
  99. * access the signal handler code in GDB.
  100. */
  101. if ((addr & PAGE_MASK) == cris_signal_return_page) {
  102. /* The trampoline page is globally mapped, no page table to traverse.*/
  103. tmp = *(unsigned long*)addr;
  104. } else {
  105. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  106. if (copied != sizeof(tmp))
  107. break;
  108. }
  109. ret = put_user(tmp,datap);
  110. break;
  111. }
  112. /* Read the word at location address in the USER area. */
  113. case PTRACE_PEEKUSR: {
  114. unsigned long tmp;
  115. ret = -EIO;
  116. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  117. break;
  118. tmp = get_reg(child, addr >> 2);
  119. ret = put_user(tmp, datap);
  120. break;
  121. }
  122. /* Write the word at location address. */
  123. case PTRACE_POKETEXT:
  124. case PTRACE_POKEDATA:
  125. ret = generic_ptrace_pokedata(child, addr, data);
  126. break;
  127. /* Write the word at location address in the USER area. */
  128. case PTRACE_POKEUSR:
  129. ret = -EIO;
  130. if ((addr & 3) || addr < 0 || addr > PT_MAX << 2)
  131. break;
  132. addr >>= 2;
  133. if (addr == PT_CCS) {
  134. /* don't allow the tracing process to change stuff like
  135. * interrupt enable, kernel/user bit, dma enables etc.
  136. */
  137. data &= CCS_MASK;
  138. data |= get_reg(child, PT_CCS) & ~CCS_MASK;
  139. }
  140. if (put_reg(child, addr, data))
  141. break;
  142. ret = 0;
  143. break;
  144. case PTRACE_SYSCALL:
  145. case PTRACE_CONT:
  146. ret = -EIO;
  147. if (!valid_signal(data))
  148. break;
  149. /* Continue means no single-step. */
  150. put_reg(child, PT_SPC, 0);
  151. if (!get_debugreg(child->pid, PT_BP_CTRL)) {
  152. unsigned long tmp;
  153. /* If no h/w bp configured, disable S bit. */
  154. tmp = get_reg(child, PT_CCS) & ~SBIT_USER;
  155. put_reg(child, PT_CCS, tmp);
  156. }
  157. if (request == PTRACE_SYSCALL) {
  158. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  159. }
  160. else {
  161. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  162. }
  163. child->exit_code = data;
  164. /* TODO: make sure any pending breakpoint is killed */
  165. wake_up_process(child);
  166. ret = 0;
  167. break;
  168. /* Make the child exit by sending it a sigkill. */
  169. case PTRACE_KILL:
  170. ret = 0;
  171. if (child->exit_state == EXIT_ZOMBIE)
  172. break;
  173. child->exit_code = SIGKILL;
  174. /* Deconfigure single-step and h/w bp. */
  175. ptrace_disable(child);
  176. /* TODO: make sure any pending breakpoint is killed */
  177. wake_up_process(child);
  178. break;
  179. /* Set the trap flag. */
  180. case PTRACE_SINGLESTEP: {
  181. unsigned long tmp;
  182. ret = -EIO;
  183. /* Set up SPC if not set already (in which case we have
  184. no other choice but to trust it). */
  185. if (!get_reg(child, PT_SPC)) {
  186. /* In case we're stopped in a delay slot. */
  187. tmp = get_reg(child, PT_ERP) & ~1;
  188. put_reg(child, PT_SPC, tmp);
  189. }
  190. tmp = get_reg(child, PT_CCS) | SBIT_USER;
  191. put_reg(child, PT_CCS, tmp);
  192. if (!valid_signal(data))
  193. break;
  194. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  195. /* TODO: set some clever breakpoint mechanism... */
  196. child->exit_code = data;
  197. wake_up_process(child);
  198. ret = 0;
  199. break;
  200. }
  201. /* Get all GP registers from the child. */
  202. case PTRACE_GETREGS: {
  203. int i;
  204. unsigned long tmp;
  205. for (i = 0; i <= PT_MAX; i++) {
  206. tmp = get_reg(child, i);
  207. if (put_user(tmp, datap)) {
  208. ret = -EFAULT;
  209. goto out_tsk;
  210. }
  211. datap++;
  212. }
  213. ret = 0;
  214. break;
  215. }
  216. /* Set all GP registers in the child. */
  217. case PTRACE_SETREGS: {
  218. int i;
  219. unsigned long tmp;
  220. for (i = 0; i <= PT_MAX; i++) {
  221. if (get_user(tmp, datap)) {
  222. ret = -EFAULT;
  223. goto out_tsk;
  224. }
  225. if (i == PT_CCS) {
  226. tmp &= CCS_MASK;
  227. tmp |= get_reg(child, PT_CCS) & ~CCS_MASK;
  228. }
  229. put_reg(child, i, tmp);
  230. datap++;
  231. }
  232. ret = 0;
  233. break;
  234. }
  235. default:
  236. ret = ptrace_request(child, request, addr, data);
  237. break;
  238. }
  239. out_tsk:
  240. return ret;
  241. }
  242. void do_syscall_trace(void)
  243. {
  244. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  245. return;
  246. if (!(current->ptrace & PT_PTRACED))
  247. return;
  248. /* the 0x80 provides a way for the tracing parent to distinguish
  249. between a syscall stop and SIGTRAP delivery */
  250. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  251. ? 0x80 : 0));
  252. /*
  253. * This isn't the same as continuing with a signal, but it will do for
  254. * normal use.
  255. */
  256. if (current->exit_code) {
  257. send_sig(current->exit_code, current, 1);
  258. current->exit_code = 0;
  259. }
  260. }
  261. /* Returns the size of an instruction that has a delay slot. */
  262. static int insn_size(struct task_struct *child, unsigned long pc)
  263. {
  264. unsigned long opcode;
  265. int copied;
  266. int opsize = 0;
  267. /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
  268. copied = access_process_vm(child, pc, &opcode, sizeof(opcode), 0);
  269. if (copied != sizeof(opcode))
  270. return 0;
  271. switch ((opcode & 0x0f00) >> 8) {
  272. case 0x0:
  273. case 0x9:
  274. case 0xb:
  275. opsize = 2;
  276. break;
  277. case 0xe:
  278. case 0xf:
  279. opsize = 6;
  280. break;
  281. case 0xd:
  282. /* Could be 4 or 6; check more bits. */
  283. if ((opcode & 0xff) == 0xff)
  284. opsize = 4;
  285. else
  286. opsize = 6;
  287. break;
  288. default:
  289. panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
  290. opcode, pc);
  291. }
  292. return opsize;
  293. }
  294. static unsigned long get_pseudo_pc(struct task_struct *child)
  295. {
  296. /* Default value for PC is ERP. */
  297. unsigned long pc = get_reg(child, PT_ERP);
  298. if (pc & 0x1) {
  299. unsigned long spc = get_reg(child, PT_SPC);
  300. /* Delay slot bit set. Report as stopped on proper
  301. instruction. */
  302. if (spc) {
  303. /* Rely on SPC if set. FIXME: We might want to check
  304. that EXS indicates we stopped due to a single-step
  305. exception. */
  306. pc = spc;
  307. } else {
  308. /* Calculate the PC from the size of the instruction
  309. that the delay slot we're in belongs to. */
  310. pc += insn_size(child, pc & ~1) - 1;
  311. }
  312. }
  313. return pc;
  314. }
  315. static long bp_owner = 0;
  316. /* Reachable from exit_thread in signal.c, so not static. */
  317. void deconfigure_bp(long pid)
  318. {
  319. int bp;
  320. /* Only deconfigure if the pid is the owner. */
  321. if (bp_owner != pid)
  322. return;
  323. for (bp = 0; bp < 6; bp++) {
  324. unsigned long tmp;
  325. /* Deconfigure start and end address (also gets rid of ownership). */
  326. put_debugreg(pid, PT_BP + 3 + (bp * 2), 0);
  327. put_debugreg(pid, PT_BP + 4 + (bp * 2), 0);
  328. /* Deconfigure relevant bits in control register. */
  329. tmp = get_debugreg(pid, PT_BP_CTRL) & ~(3 << (2 + (bp * 4)));
  330. put_debugreg(pid, PT_BP_CTRL, tmp);
  331. }
  332. /* No owner now. */
  333. bp_owner = 0;
  334. }
  335. static int put_debugreg(long pid, unsigned int regno, long data)
  336. {
  337. int ret = 0;
  338. register int old_srs;
  339. #ifdef CONFIG_ETRAX_KGDB
  340. /* Ignore write, but pretend it was ok if value is 0
  341. (we don't want POKEUSR/SETREGS failing unnessecarily). */
  342. return (data == 0) ? ret : -1;
  343. #endif
  344. /* Simple owner management. */
  345. if (!bp_owner)
  346. bp_owner = pid;
  347. else if (bp_owner != pid) {
  348. /* Ignore write, but pretend it was ok if value is 0
  349. (we don't want POKEUSR/SETREGS failing unnessecarily). */
  350. return (data == 0) ? ret : -1;
  351. }
  352. /* Remember old SRS. */
  353. SPEC_REG_RD(SPEC_REG_SRS, old_srs);
  354. /* Switch to BP bank. */
  355. SUPP_BANK_SEL(BANK_BP);
  356. switch (regno - PT_BP) {
  357. case 0:
  358. SUPP_REG_WR(0, data); break;
  359. case 1:
  360. case 2:
  361. if (data)
  362. ret = -1;
  363. break;
  364. case 3:
  365. SUPP_REG_WR(3, data); break;
  366. case 4:
  367. SUPP_REG_WR(4, data); break;
  368. case 5:
  369. SUPP_REG_WR(5, data); break;
  370. case 6:
  371. SUPP_REG_WR(6, data); break;
  372. case 7:
  373. SUPP_REG_WR(7, data); break;
  374. case 8:
  375. SUPP_REG_WR(8, data); break;
  376. case 9:
  377. SUPP_REG_WR(9, data); break;
  378. case 10:
  379. SUPP_REG_WR(10, data); break;
  380. case 11:
  381. SUPP_REG_WR(11, data); break;
  382. case 12:
  383. SUPP_REG_WR(12, data); break;
  384. case 13:
  385. SUPP_REG_WR(13, data); break;
  386. case 14:
  387. SUPP_REG_WR(14, data); break;
  388. default:
  389. ret = -1;
  390. break;
  391. }
  392. /* Restore SRS. */
  393. SPEC_REG_WR(SPEC_REG_SRS, old_srs);
  394. /* Just for show. */
  395. NOP();
  396. NOP();
  397. NOP();
  398. return ret;
  399. }
  400. static long get_debugreg(long pid, unsigned int regno)
  401. {
  402. register int old_srs;
  403. register long data;
  404. if (pid != bp_owner) {
  405. return 0;
  406. }
  407. /* Remember old SRS. */
  408. SPEC_REG_RD(SPEC_REG_SRS, old_srs);
  409. /* Switch to BP bank. */
  410. SUPP_BANK_SEL(BANK_BP);
  411. switch (regno - PT_BP) {
  412. case 0:
  413. SUPP_REG_RD(0, data); break;
  414. case 1:
  415. case 2:
  416. /* error return value? */
  417. data = 0;
  418. break;
  419. case 3:
  420. SUPP_REG_RD(3, data); break;
  421. case 4:
  422. SUPP_REG_RD(4, data); break;
  423. case 5:
  424. SUPP_REG_RD(5, data); break;
  425. case 6:
  426. SUPP_REG_RD(6, data); break;
  427. case 7:
  428. SUPP_REG_RD(7, data); break;
  429. case 8:
  430. SUPP_REG_RD(8, data); break;
  431. case 9:
  432. SUPP_REG_RD(9, data); break;
  433. case 10:
  434. SUPP_REG_RD(10, data); break;
  435. case 11:
  436. SUPP_REG_RD(11, data); break;
  437. case 12:
  438. SUPP_REG_RD(12, data); break;
  439. case 13:
  440. SUPP_REG_RD(13, data); break;
  441. case 14:
  442. SUPP_REG_RD(14, data); break;
  443. default:
  444. /* error return value? */
  445. data = 0;
  446. }
  447. /* Restore SRS. */
  448. SPEC_REG_WR(SPEC_REG_SRS, old_srs);
  449. /* Just for show. */
  450. NOP();
  451. NOP();
  452. NOP();
  453. return data;
  454. }