ptrace.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * Copyright (C) 2000-2003, 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. case PTRACE_DETACH:
  202. ret = ptrace_detach(child, data);
  203. break;
  204. /* Get all GP registers from the child. */
  205. case PTRACE_GETREGS: {
  206. int i;
  207. unsigned long tmp;
  208. for (i = 0; i <= PT_MAX; i++) {
  209. tmp = get_reg(child, i);
  210. if (put_user(tmp, datap)) {
  211. ret = -EFAULT;
  212. goto out_tsk;
  213. }
  214. datap++;
  215. }
  216. ret = 0;
  217. break;
  218. }
  219. /* Set all GP registers in the child. */
  220. case PTRACE_SETREGS: {
  221. int i;
  222. unsigned long tmp;
  223. for (i = 0; i <= PT_MAX; i++) {
  224. if (get_user(tmp, datap)) {
  225. ret = -EFAULT;
  226. goto out_tsk;
  227. }
  228. if (i == PT_CCS) {
  229. tmp &= CCS_MASK;
  230. tmp |= get_reg(child, PT_CCS) & ~CCS_MASK;
  231. }
  232. put_reg(child, i, tmp);
  233. datap++;
  234. }
  235. ret = 0;
  236. break;
  237. }
  238. default:
  239. ret = ptrace_request(child, request, addr, data);
  240. break;
  241. }
  242. return ret;
  243. }
  244. void do_syscall_trace(void)
  245. {
  246. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  247. return;
  248. if (!(current->ptrace & PT_PTRACED))
  249. return;
  250. /* the 0x80 provides a way for the tracing parent to distinguish
  251. between a syscall stop and SIGTRAP delivery */
  252. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  253. ? 0x80 : 0));
  254. /*
  255. * This isn't the same as continuing with a signal, but it will do for
  256. * normal use.
  257. */
  258. if (current->exit_code) {
  259. send_sig(current->exit_code, current, 1);
  260. current->exit_code = 0;
  261. }
  262. }
  263. /* Returns the size of an instruction that has a delay slot. */
  264. static int insn_size(struct task_struct *child, unsigned long pc)
  265. {
  266. unsigned long opcode;
  267. int copied;
  268. int opsize = 0;
  269. /* Read the opcode at pc (do what PTRACE_PEEKTEXT would do). */
  270. copied = access_process_vm(child, pc, &opcode, sizeof(opcode), 0);
  271. if (copied != sizeof(opcode))
  272. return 0;
  273. switch ((opcode & 0x0f00) >> 8) {
  274. case 0x0:
  275. case 0x9:
  276. case 0xb:
  277. opsize = 2;
  278. break;
  279. case 0xe:
  280. case 0xf:
  281. opsize = 6;
  282. break;
  283. case 0xd:
  284. /* Could be 4 or 6; check more bits. */
  285. if ((opcode & 0xff) == 0xff)
  286. opsize = 4;
  287. else
  288. opsize = 6;
  289. break;
  290. default:
  291. panic("ERROR: Couldn't find size of opcode 0x%lx at 0x%lx\n",
  292. opcode, pc);
  293. }
  294. return opsize;
  295. }
  296. static unsigned long get_pseudo_pc(struct task_struct *child)
  297. {
  298. /* Default value for PC is ERP. */
  299. unsigned long pc = get_reg(child, PT_ERP);
  300. if (pc & 0x1) {
  301. unsigned long spc = get_reg(child, PT_SPC);
  302. /* Delay slot bit set. Report as stopped on proper
  303. instruction. */
  304. if (spc) {
  305. /* Rely on SPC if set. FIXME: We might want to check
  306. that EXS indicates we stopped due to a single-step
  307. exception. */
  308. pc = spc;
  309. } else {
  310. /* Calculate the PC from the size of the instruction
  311. that the delay slot we're in belongs to. */
  312. pc += insn_size(child, pc & ~1) - 1;
  313. }
  314. }
  315. return pc;
  316. }
  317. static long bp_owner = 0;
  318. /* Reachable from exit_thread in signal.c, so not static. */
  319. void deconfigure_bp(long pid)
  320. {
  321. int bp;
  322. /* Only deconfigure if the pid is the owner. */
  323. if (bp_owner != pid)
  324. return;
  325. for (bp = 0; bp < 6; bp++) {
  326. unsigned long tmp;
  327. /* Deconfigure start and end address (also gets rid of ownership). */
  328. put_debugreg(pid, PT_BP + 3 + (bp * 2), 0);
  329. put_debugreg(pid, PT_BP + 4 + (bp * 2), 0);
  330. /* Deconfigure relevant bits in control register. */
  331. tmp = get_debugreg(pid, PT_BP_CTRL) & ~(3 << (2 + (bp * 4)));
  332. put_debugreg(pid, PT_BP_CTRL, tmp);
  333. }
  334. /* No owner now. */
  335. bp_owner = 0;
  336. }
  337. static int put_debugreg(long pid, unsigned int regno, long data)
  338. {
  339. int ret = 0;
  340. register int old_srs;
  341. #ifdef CONFIG_ETRAX_KGDB
  342. /* Ignore write, but pretend it was ok if value is 0
  343. (we don't want POKEUSR/SETREGS failing unnessecarily). */
  344. return (data == 0) ? ret : -1;
  345. #endif
  346. /* Simple owner management. */
  347. if (!bp_owner)
  348. bp_owner = pid;
  349. else if (bp_owner != pid) {
  350. /* Ignore write, but pretend it was ok if value is 0
  351. (we don't want POKEUSR/SETREGS failing unnessecarily). */
  352. return (data == 0) ? ret : -1;
  353. }
  354. /* Remember old SRS. */
  355. SPEC_REG_RD(SPEC_REG_SRS, old_srs);
  356. /* Switch to BP bank. */
  357. SUPP_BANK_SEL(BANK_BP);
  358. switch (regno - PT_BP) {
  359. case 0:
  360. SUPP_REG_WR(0, data); break;
  361. case 1:
  362. case 2:
  363. if (data)
  364. ret = -1;
  365. break;
  366. case 3:
  367. SUPP_REG_WR(3, data); break;
  368. case 4:
  369. SUPP_REG_WR(4, data); break;
  370. case 5:
  371. SUPP_REG_WR(5, data); break;
  372. case 6:
  373. SUPP_REG_WR(6, data); break;
  374. case 7:
  375. SUPP_REG_WR(7, data); break;
  376. case 8:
  377. SUPP_REG_WR(8, data); break;
  378. case 9:
  379. SUPP_REG_WR(9, data); break;
  380. case 10:
  381. SUPP_REG_WR(10, data); break;
  382. case 11:
  383. SUPP_REG_WR(11, data); break;
  384. case 12:
  385. SUPP_REG_WR(12, data); break;
  386. case 13:
  387. SUPP_REG_WR(13, data); break;
  388. case 14:
  389. SUPP_REG_WR(14, data); break;
  390. default:
  391. ret = -1;
  392. break;
  393. }
  394. /* Restore SRS. */
  395. SPEC_REG_WR(SPEC_REG_SRS, old_srs);
  396. /* Just for show. */
  397. NOP();
  398. NOP();
  399. NOP();
  400. return ret;
  401. }
  402. static long get_debugreg(long pid, unsigned int regno)
  403. {
  404. register int old_srs;
  405. register long data;
  406. if (pid != bp_owner) {
  407. return 0;
  408. }
  409. /* Remember old SRS. */
  410. SPEC_REG_RD(SPEC_REG_SRS, old_srs);
  411. /* Switch to BP bank. */
  412. SUPP_BANK_SEL(BANK_BP);
  413. switch (regno - PT_BP) {
  414. case 0:
  415. SUPP_REG_RD(0, data); break;
  416. case 1:
  417. case 2:
  418. /* error return value? */
  419. data = 0;
  420. break;
  421. case 3:
  422. SUPP_REG_RD(3, data); break;
  423. case 4:
  424. SUPP_REG_RD(4, data); break;
  425. case 5:
  426. SUPP_REG_RD(5, data); break;
  427. case 6:
  428. SUPP_REG_RD(6, data); break;
  429. case 7:
  430. SUPP_REG_RD(7, data); break;
  431. case 8:
  432. SUPP_REG_RD(8, data); break;
  433. case 9:
  434. SUPP_REG_RD(9, data); break;
  435. case 10:
  436. SUPP_REG_RD(10, data); break;
  437. case 11:
  438. SUPP_REG_RD(11, data); break;
  439. case 12:
  440. SUPP_REG_RD(12, data); break;
  441. case 13:
  442. SUPP_REG_RD(13, data); break;
  443. case 14:
  444. SUPP_REG_RD(14, data); break;
  445. default:
  446. /* error return value? */
  447. data = 0;
  448. }
  449. /* Restore SRS. */
  450. SPEC_REG_WR(SPEC_REG_SRS, old_srs);
  451. /* Just for show. */
  452. NOP();
  453. NOP();
  454. NOP();
  455. return data;
  456. }