ptrace.c 15 KB

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