ptrace.c 11 KB

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