ptrace.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*
  2. * linux/arch/arm26/kernel/ptrace.c
  3. *
  4. * By Ross Biro 1/23/92
  5. * edited by Linus Torvalds
  6. * ARM modifications Copyright (C) 2000 Russell King
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15. #include <linux/smp.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/security.h>
  19. #include <linux/signal.h>
  20. #include <asm/uaccess.h>
  21. #include <asm/pgtable.h>
  22. #include <asm/system.h>
  23. //#include <asm/processor.h>
  24. #include "ptrace.h"
  25. #define REG_PC 15
  26. #define REG_PSR 15
  27. /*
  28. * does not yet catch signals sent when the child dies.
  29. * in exit.c or in signal.c.
  30. */
  31. /*
  32. * Breakpoint SWI instruction: SWI &9F0001
  33. */
  34. #define BREAKINST_ARM 0xef9f0001
  35. /*
  36. * this routine will get a word off of the processes privileged stack.
  37. * the offset is how far from the base addr as stored in the THREAD.
  38. * this routine assumes that all the privileged stacks are in our
  39. * data space.
  40. */
  41. static inline long get_user_reg(struct task_struct *task, int offset)
  42. {
  43. return task_pt_regs(task)->uregs[offset];
  44. }
  45. /*
  46. * this routine will put a word on the processes privileged stack.
  47. * the offset is how far from the base addr as stored in the THREAD.
  48. * this routine assumes that all the privileged stacks are in our
  49. * data space.
  50. */
  51. static inline int
  52. put_user_reg(struct task_struct *task, int offset, long data)
  53. {
  54. struct pt_regs newregs, *regs = task_pt_regs(task);
  55. int ret = -EINVAL;
  56. newregs = *regs;
  57. newregs.uregs[offset] = data;
  58. if (valid_user_regs(&newregs)) {
  59. regs->uregs[offset] = data;
  60. ret = 0;
  61. }
  62. return ret;
  63. }
  64. static inline int
  65. read_u32(struct task_struct *task, unsigned long addr, u32 *res)
  66. {
  67. int ret;
  68. ret = access_process_vm(task, addr, res, sizeof(*res), 0);
  69. return ret == sizeof(*res) ? 0 : -EIO;
  70. }
  71. static inline int
  72. read_instr(struct task_struct *task, unsigned long addr, u32 *res)
  73. {
  74. int ret;
  75. u32 val;
  76. ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
  77. ret = ret == sizeof(val) ? 0 : -EIO;
  78. *res = val;
  79. return ret;
  80. }
  81. /*
  82. * Get value of register `rn' (in the instruction)
  83. */
  84. static unsigned long
  85. ptrace_getrn(struct task_struct *child, unsigned long insn)
  86. {
  87. unsigned int reg = (insn >> 16) & 15;
  88. unsigned long val;
  89. val = get_user_reg(child, reg);
  90. if (reg == 15)
  91. val = pc_pointer(val + 8); //FIXME - correct for arm26?
  92. return val;
  93. }
  94. /*
  95. * Get value of operand 2 (in an ALU instruction)
  96. */
  97. static unsigned long
  98. ptrace_getaluop2(struct task_struct *child, unsigned long insn)
  99. {
  100. unsigned long val;
  101. int shift;
  102. int type;
  103. if (insn & 1 << 25) {
  104. val = insn & 255;
  105. shift = (insn >> 8) & 15;
  106. type = 3;
  107. } else {
  108. val = get_user_reg (child, insn & 15);
  109. if (insn & (1 << 4))
  110. shift = (int)get_user_reg (child, (insn >> 8) & 15);
  111. else
  112. shift = (insn >> 7) & 31;
  113. type = (insn >> 5) & 3;
  114. }
  115. switch (type) {
  116. case 0: val <<= shift; break;
  117. case 1: val >>= shift; break;
  118. case 2:
  119. val = (((signed long)val) >> shift);
  120. break;
  121. case 3:
  122. val = (val >> shift) | (val << (32 - shift));
  123. break;
  124. }
  125. return val;
  126. }
  127. /*
  128. * Get value of operand 2 (in a LDR instruction)
  129. */
  130. static unsigned long
  131. ptrace_getldrop2(struct task_struct *child, unsigned long insn)
  132. {
  133. unsigned long val;
  134. int shift;
  135. int type;
  136. val = get_user_reg(child, insn & 15);
  137. shift = (insn >> 7) & 31;
  138. type = (insn >> 5) & 3;
  139. switch (type) {
  140. case 0: val <<= shift; break;
  141. case 1: val >>= shift; break;
  142. case 2:
  143. val = (((signed long)val) >> shift);
  144. break;
  145. case 3:
  146. val = (val >> shift) | (val << (32 - shift));
  147. break;
  148. }
  149. return val;
  150. }
  151. #define OP_MASK 0x01e00000
  152. #define OP_AND 0x00000000
  153. #define OP_EOR 0x00200000
  154. #define OP_SUB 0x00400000
  155. #define OP_RSB 0x00600000
  156. #define OP_ADD 0x00800000
  157. #define OP_ADC 0x00a00000
  158. #define OP_SBC 0x00c00000
  159. #define OP_RSC 0x00e00000
  160. #define OP_ORR 0x01800000
  161. #define OP_MOV 0x01a00000
  162. #define OP_BIC 0x01c00000
  163. #define OP_MVN 0x01e00000
  164. static unsigned long
  165. get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
  166. {
  167. u32 alt = 0;
  168. switch (insn & 0x0e000000) {
  169. case 0x00000000:
  170. case 0x02000000: {
  171. /*
  172. * data processing
  173. */
  174. long aluop1, aluop2, ccbit;
  175. if ((insn & 0xf000) != 0xf000)
  176. break;
  177. aluop1 = ptrace_getrn(child, insn);
  178. aluop2 = ptrace_getaluop2(child, insn);
  179. ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
  180. switch (insn & OP_MASK) {
  181. case OP_AND: alt = aluop1 & aluop2; break;
  182. case OP_EOR: alt = aluop1 ^ aluop2; break;
  183. case OP_SUB: alt = aluop1 - aluop2; break;
  184. case OP_RSB: alt = aluop2 - aluop1; break;
  185. case OP_ADD: alt = aluop1 + aluop2; break;
  186. case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
  187. case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
  188. case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
  189. case OP_ORR: alt = aluop1 | aluop2; break;
  190. case OP_MOV: alt = aluop2; break;
  191. case OP_BIC: alt = aluop1 & ~aluop2; break;
  192. case OP_MVN: alt = ~aluop2; break;
  193. }
  194. break;
  195. }
  196. case 0x04000000:
  197. case 0x06000000:
  198. /*
  199. * ldr
  200. */
  201. if ((insn & 0x0010f000) == 0x0010f000) {
  202. unsigned long base;
  203. base = ptrace_getrn(child, insn);
  204. if (insn & 1 << 24) {
  205. long aluop2;
  206. if (insn & 0x02000000)
  207. aluop2 = ptrace_getldrop2(child, insn);
  208. else
  209. aluop2 = insn & 0xfff;
  210. if (insn & 1 << 23)
  211. base += aluop2;
  212. else
  213. base -= aluop2;
  214. }
  215. if (read_u32(child, base, &alt) == 0)
  216. alt = pc_pointer(alt);
  217. }
  218. break;
  219. case 0x08000000:
  220. /*
  221. * ldm
  222. */
  223. if ((insn & 0x00108000) == 0x00108000) {
  224. unsigned long base;
  225. unsigned int nr_regs;
  226. if (insn & (1 << 23)) {
  227. nr_regs = hweight16(insn & 65535) << 2;
  228. if (!(insn & (1 << 24)))
  229. nr_regs -= 4;
  230. } else {
  231. if (insn & (1 << 24))
  232. nr_regs = -4;
  233. else
  234. nr_regs = 0;
  235. }
  236. base = ptrace_getrn(child, insn);
  237. if (read_u32(child, base + nr_regs, &alt) == 0)
  238. alt = pc_pointer(alt);
  239. break;
  240. }
  241. break;
  242. case 0x0a000000: {
  243. /*
  244. * bl or b
  245. */
  246. signed long displ;
  247. /* It's a branch/branch link: instead of trying to
  248. * figure out whether the branch will be taken or not,
  249. * we'll put a breakpoint at both locations. This is
  250. * simpler, more reliable, and probably not a whole lot
  251. * slower than the alternative approach of emulating the
  252. * branch.
  253. */
  254. displ = (insn & 0x00ffffff) << 8;
  255. displ = (displ >> 6) + 8;
  256. if (displ != 0 && displ != 4)
  257. alt = pc + displ;
  258. }
  259. break;
  260. }
  261. return alt;
  262. }
  263. static int
  264. swap_insn(struct task_struct *task, unsigned long addr,
  265. void *old_insn, void *new_insn, int size)
  266. {
  267. int ret;
  268. ret = access_process_vm(task, addr, old_insn, size, 0);
  269. if (ret == size)
  270. ret = access_process_vm(task, addr, new_insn, size, 1);
  271. return ret;
  272. }
  273. static void
  274. add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
  275. {
  276. int nr = dbg->nsaved;
  277. if (nr < 2) {
  278. u32 new_insn = BREAKINST_ARM;
  279. int res;
  280. res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
  281. if (res == 4) {
  282. dbg->bp[nr].address = addr;
  283. dbg->nsaved += 1;
  284. }
  285. } else
  286. printk(KERN_ERR "ptrace: too many breakpoints\n");
  287. }
  288. /*
  289. * Clear one breakpoint in the user program. We copy what the hardware
  290. * does and use bit 0 of the address to indicate whether this is a Thumb
  291. * breakpoint or an ARM breakpoint.
  292. */
  293. static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
  294. {
  295. unsigned long addr = bp->address;
  296. u32 old_insn;
  297. int ret;
  298. ret = swap_insn(task, addr & ~3, &old_insn,
  299. &bp->insn, 4);
  300. if (ret != 4 || old_insn != BREAKINST_ARM)
  301. printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
  302. "0x%08lx (0x%08x)\n", task->comm, task->pid,
  303. addr, old_insn);
  304. }
  305. void ptrace_set_bpt(struct task_struct *child)
  306. {
  307. struct pt_regs *regs;
  308. unsigned long pc;
  309. u32 insn;
  310. int res;
  311. regs = task_pt_regs(child);
  312. pc = instruction_pointer(regs);
  313. res = read_instr(child, pc, &insn);
  314. if (!res) {
  315. struct debug_info *dbg = &child->thread.debug;
  316. unsigned long alt;
  317. dbg->nsaved = 0;
  318. alt = get_branch_address(child, pc, insn);
  319. if (alt)
  320. add_breakpoint(child, dbg, alt);
  321. /*
  322. * Note that we ignore the result of setting the above
  323. * breakpoint since it may fail. When it does, this is
  324. * not so much an error, but a forewarning that we may
  325. * be receiving a prefetch abort shortly.
  326. *
  327. * If we don't set this breakpoint here, then we can
  328. * lose control of the thread during single stepping.
  329. */
  330. if (!alt || predicate(insn) != PREDICATE_ALWAYS)
  331. add_breakpoint(child, dbg, pc + 4);
  332. }
  333. }
  334. /*
  335. * Ensure no single-step breakpoint is pending. Returns non-zero
  336. * value if child was being single-stepped.
  337. */
  338. void ptrace_cancel_bpt(struct task_struct *child)
  339. {
  340. int i, nsaved = child->thread.debug.nsaved;
  341. child->thread.debug.nsaved = 0;
  342. if (nsaved > 2) {
  343. printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
  344. nsaved = 2;
  345. }
  346. for (i = 0; i < nsaved; i++)
  347. clear_breakpoint(child, &child->thread.debug.bp[i]);
  348. }
  349. /*
  350. * Called by kernel/ptrace.c when detaching..
  351. *
  352. * Make sure the single step bit is not set.
  353. */
  354. void ptrace_disable(struct task_struct *child)
  355. {
  356. child->ptrace &= ~PT_SINGLESTEP;
  357. ptrace_cancel_bpt(child);
  358. }
  359. /*
  360. * Handle hitting a breakpoint.
  361. */
  362. void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
  363. {
  364. siginfo_t info;
  365. /*
  366. * The PC is always left pointing at the next instruction. Fix this.
  367. */
  368. regs->ARM_pc -= 4;
  369. if (tsk->thread.debug.nsaved == 0)
  370. printk(KERN_ERR "ptrace: bogus breakpoint trap\n");
  371. ptrace_cancel_bpt(tsk);
  372. info.si_signo = SIGTRAP;
  373. info.si_errno = 0;
  374. info.si_code = TRAP_BRKPT;
  375. info.si_addr = (void *)instruction_pointer(regs) - 4;
  376. force_sig_info(SIGTRAP, &info, tsk);
  377. }
  378. /*
  379. * Read the word at offset "off" into the "struct user". We
  380. * actually access the pt_regs stored on the kernel stack.
  381. */
  382. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  383. unsigned long *ret)
  384. {
  385. unsigned long tmp;
  386. if (off & 3 || off >= sizeof(struct user))
  387. return -EIO;
  388. tmp = 0;
  389. if (off < sizeof(struct pt_regs))
  390. tmp = get_user_reg(tsk, off >> 2);
  391. return put_user(tmp, ret);
  392. }
  393. /*
  394. * Write the word at offset "off" into "struct user". We
  395. * actually access the pt_regs stored on the kernel stack.
  396. */
  397. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  398. unsigned long val)
  399. {
  400. if (off & 3 || off >= sizeof(struct user))
  401. return -EIO;
  402. if (off >= sizeof(struct pt_regs))
  403. return 0;
  404. return put_user_reg(tsk, off >> 2, val);
  405. }
  406. /*
  407. * Get all user integer registers.
  408. */
  409. static int ptrace_getregs(struct task_struct *tsk, void *uregs)
  410. {
  411. struct pt_regs *regs = task_pt_regs(tsk);
  412. return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  413. }
  414. /*
  415. * Set all user integer registers.
  416. */
  417. static int ptrace_setregs(struct task_struct *tsk, void *uregs)
  418. {
  419. struct pt_regs newregs;
  420. int ret;
  421. ret = -EFAULT;
  422. if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
  423. struct pt_regs *regs = task_pt_regs(tsk);
  424. ret = -EINVAL;
  425. if (valid_user_regs(&newregs)) {
  426. *regs = newregs;
  427. ret = 0;
  428. }
  429. }
  430. return ret;
  431. }
  432. /*
  433. * Get the child FPU state.
  434. */
  435. static int ptrace_getfpregs(struct task_struct *tsk, void *ufp)
  436. {
  437. return copy_to_user(ufp, &task_thread_info(tsk)->fpstate,
  438. sizeof(struct user_fp)) ? -EFAULT : 0;
  439. }
  440. /*
  441. * Set the child FPU state.
  442. */
  443. static int ptrace_setfpregs(struct task_struct *tsk, void *ufp)
  444. {
  445. set_stopped_child_used_math(tsk);
  446. return copy_from_user(&task_thread_info(tsk)->fpstate, ufp,
  447. sizeof(struct user_fp)) ? -EFAULT : 0;
  448. }
  449. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  450. {
  451. unsigned long tmp;
  452. int ret;
  453. switch (request) {
  454. /*
  455. * read word at location "addr" in the child process.
  456. */
  457. case PTRACE_PEEKTEXT:
  458. case PTRACE_PEEKDATA:
  459. ret = access_process_vm(child, addr, &tmp,
  460. sizeof(unsigned long), 0);
  461. if (ret == sizeof(unsigned long))
  462. ret = put_user(tmp, (unsigned long *) data);
  463. else
  464. ret = -EIO;
  465. break;
  466. case PTRACE_PEEKUSR:
  467. ret = ptrace_read_user(child, addr, (unsigned long *)data);
  468. break;
  469. /*
  470. * write the word at location addr.
  471. */
  472. case PTRACE_POKETEXT:
  473. case PTRACE_POKEDATA:
  474. ret = access_process_vm(child, addr, &data,
  475. sizeof(unsigned long), 1);
  476. if (ret == sizeof(unsigned long))
  477. ret = 0;
  478. else
  479. ret = -EIO;
  480. break;
  481. case PTRACE_POKEUSR:
  482. ret = ptrace_write_user(child, addr, data);
  483. break;
  484. /*
  485. * continue/restart and stop at next (return from) syscall
  486. */
  487. case PTRACE_SYSCALL:
  488. case PTRACE_CONT:
  489. ret = -EIO;
  490. if (!valid_signal(data))
  491. break;
  492. if (request == PTRACE_SYSCALL)
  493. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  494. else
  495. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  496. child->exit_code = data;
  497. /* make sure single-step breakpoint is gone. */
  498. child->ptrace &= ~PT_SINGLESTEP;
  499. ptrace_cancel_bpt(child);
  500. wake_up_process(child);
  501. ret = 0;
  502. break;
  503. /*
  504. * make the child exit. Best I can do is send it a sigkill.
  505. * perhaps it should be put in the status that it wants to
  506. * exit.
  507. */
  508. case PTRACE_KILL:
  509. /* make sure single-step breakpoint is gone. */
  510. child->ptrace &= ~PT_SINGLESTEP;
  511. ptrace_cancel_bpt(child);
  512. if (child->exit_state != EXIT_ZOMBIE) {
  513. child->exit_code = SIGKILL;
  514. wake_up_process(child);
  515. }
  516. ret = 0;
  517. break;
  518. /*
  519. * execute single instruction.
  520. */
  521. case PTRACE_SINGLESTEP:
  522. ret = -EIO;
  523. if (!valid_signal(data))
  524. break;
  525. child->ptrace |= PT_SINGLESTEP;
  526. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  527. child->exit_code = data;
  528. /* give it a chance to run. */
  529. wake_up_process(child);
  530. ret = 0;
  531. break;
  532. case PTRACE_DETACH:
  533. ret = ptrace_detach(child, data);
  534. break;
  535. case PTRACE_GETREGS:
  536. ret = ptrace_getregs(child, (void *)data);
  537. break;
  538. case PTRACE_SETREGS:
  539. ret = ptrace_setregs(child, (void *)data);
  540. break;
  541. case PTRACE_GETFPREGS:
  542. ret = ptrace_getfpregs(child, (void *)data);
  543. break;
  544. case PTRACE_SETFPREGS:
  545. ret = ptrace_setfpregs(child, (void *)data);
  546. break;
  547. default:
  548. ret = ptrace_request(child, request, addr, data);
  549. break;
  550. }
  551. return ret;
  552. }
  553. asmlinkage void syscall_trace(int why, struct pt_regs *regs)
  554. {
  555. unsigned long ip;
  556. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  557. return;
  558. if (!(current->ptrace & PT_PTRACED))
  559. return;
  560. /*
  561. * Save IP. IP is used to denote syscall entry/exit:
  562. * IP = 0 -> entry, = 1 -> exit
  563. */
  564. ip = regs->ARM_ip;
  565. regs->ARM_ip = why;
  566. /* the 0x80 provides a way for the tracing parent to distinguish
  567. between a syscall stop and SIGTRAP delivery */
  568. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  569. ? 0x80 : 0));
  570. /*
  571. * this isn't the same as continuing with a signal, but it will do
  572. * for normal use. strace only continues with a signal if the
  573. * stopping signal is not SIGTRAP. -brl
  574. */
  575. if (current->exit_code) {
  576. send_sig(current->exit_code, current, 1);
  577. current->exit_code = 0;
  578. }
  579. regs->ARM_ip = ip;
  580. }