ptrace.c 15 KB

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