/arch/arm/kernel/ptrace.c

https://bitbucket.org/evzijst/gittest · C · 861 lines · 577 code · 122 blank · 162 comment · 99 complexity · 2b3024a461367ee761f4c435fdb3161f MD5 · raw file

  1. /*
  2. * linux/arch/arm/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/init.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/traps.h>
  26. #include "ptrace.h"
  27. #define REG_PC 15
  28. #define REG_PSR 16
  29. /*
  30. * does not yet catch signals sent when the child dies.
  31. * in exit.c or in signal.c.
  32. */
  33. #if 0
  34. /*
  35. * Breakpoint SWI instruction: SWI &9F0001
  36. */
  37. #define BREAKINST_ARM 0xef9f0001
  38. #define BREAKINST_THUMB 0xdf00 /* fill this in later */
  39. #else
  40. /*
  41. * New breakpoints - use an undefined instruction. The ARM architecture
  42. * reference manual guarantees that the following instruction space
  43. * will produce an undefined instruction exception on all CPUs:
  44. *
  45. * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
  46. * Thumb: 1101 1110 xxxx xxxx
  47. */
  48. #define BREAKINST_ARM 0xe7f001f0
  49. #define BREAKINST_THUMB 0xde01
  50. #endif
  51. /*
  52. * Get the address of the live pt_regs for the specified task.
  53. * These are saved onto the top kernel stack when the process
  54. * is not running.
  55. *
  56. * Note: if a user thread is execve'd from kernel space, the
  57. * kernel stack will not be empty on entry to the kernel, so
  58. * ptracing these tasks will fail.
  59. */
  60. static inline struct pt_regs *
  61. get_user_regs(struct task_struct *task)
  62. {
  63. return (struct pt_regs *)
  64. ((unsigned long)task->thread_info + THREAD_SIZE -
  65. 8 - sizeof(struct pt_regs));
  66. }
  67. /*
  68. * this routine will get a word off of the processes privileged stack.
  69. * the offset is how far from the base addr as stored in the THREAD.
  70. * this routine assumes that all the privileged stacks are in our
  71. * data space.
  72. */
  73. static inline long get_user_reg(struct task_struct *task, int offset)
  74. {
  75. return get_user_regs(task)->uregs[offset];
  76. }
  77. /*
  78. * this routine will put a word on the processes privileged stack.
  79. * the offset is how far from the base addr as stored in the THREAD.
  80. * this routine assumes that all the privileged stacks are in our
  81. * data space.
  82. */
  83. static inline int
  84. put_user_reg(struct task_struct *task, int offset, long data)
  85. {
  86. struct pt_regs newregs, *regs = get_user_regs(task);
  87. int ret = -EINVAL;
  88. newregs = *regs;
  89. newregs.uregs[offset] = data;
  90. if (valid_user_regs(&newregs)) {
  91. regs->uregs[offset] = data;
  92. ret = 0;
  93. }
  94. return ret;
  95. }
  96. static inline int
  97. read_u32(struct task_struct *task, unsigned long addr, u32 *res)
  98. {
  99. int ret;
  100. ret = access_process_vm(task, addr, res, sizeof(*res), 0);
  101. return ret == sizeof(*res) ? 0 : -EIO;
  102. }
  103. static inline int
  104. read_instr(struct task_struct *task, unsigned long addr, u32 *res)
  105. {
  106. int ret;
  107. if (addr & 1) {
  108. u16 val;
  109. ret = access_process_vm(task, addr & ~1, &val, sizeof(val), 0);
  110. ret = ret == sizeof(val) ? 0 : -EIO;
  111. *res = val;
  112. } else {
  113. u32 val;
  114. ret = access_process_vm(task, addr & ~3, &val, sizeof(val), 0);
  115. ret = ret == sizeof(val) ? 0 : -EIO;
  116. *res = val;
  117. }
  118. return ret;
  119. }
  120. /*
  121. * Get value of register `rn' (in the instruction)
  122. */
  123. static unsigned long
  124. ptrace_getrn(struct task_struct *child, unsigned long insn)
  125. {
  126. unsigned int reg = (insn >> 16) & 15;
  127. unsigned long val;
  128. val = get_user_reg(child, reg);
  129. if (reg == 15)
  130. val = pc_pointer(val + 8);
  131. return val;
  132. }
  133. /*
  134. * Get value of operand 2 (in an ALU instruction)
  135. */
  136. static unsigned long
  137. ptrace_getaluop2(struct task_struct *child, unsigned long insn)
  138. {
  139. unsigned long val;
  140. int shift;
  141. int type;
  142. if (insn & 1 << 25) {
  143. val = insn & 255;
  144. shift = (insn >> 8) & 15;
  145. type = 3;
  146. } else {
  147. val = get_user_reg (child, insn & 15);
  148. if (insn & (1 << 4))
  149. shift = (int)get_user_reg (child, (insn >> 8) & 15);
  150. else
  151. shift = (insn >> 7) & 31;
  152. type = (insn >> 5) & 3;
  153. }
  154. switch (type) {
  155. case 0: val <<= shift; break;
  156. case 1: val >>= shift; break;
  157. case 2:
  158. val = (((signed long)val) >> shift);
  159. break;
  160. case 3:
  161. val = (val >> shift) | (val << (32 - shift));
  162. break;
  163. }
  164. return val;
  165. }
  166. /*
  167. * Get value of operand 2 (in a LDR instruction)
  168. */
  169. static unsigned long
  170. ptrace_getldrop2(struct task_struct *child, unsigned long insn)
  171. {
  172. unsigned long val;
  173. int shift;
  174. int type;
  175. val = get_user_reg(child, insn & 15);
  176. shift = (insn >> 7) & 31;
  177. type = (insn >> 5) & 3;
  178. switch (type) {
  179. case 0: val <<= shift; break;
  180. case 1: val >>= shift; break;
  181. case 2:
  182. val = (((signed long)val) >> shift);
  183. break;
  184. case 3:
  185. val = (val >> shift) | (val << (32 - shift));
  186. break;
  187. }
  188. return val;
  189. }
  190. #define OP_MASK 0x01e00000
  191. #define OP_AND 0x00000000
  192. #define OP_EOR 0x00200000
  193. #define OP_SUB 0x00400000
  194. #define OP_RSB 0x00600000
  195. #define OP_ADD 0x00800000
  196. #define OP_ADC 0x00a00000
  197. #define OP_SBC 0x00c00000
  198. #define OP_RSC 0x00e00000
  199. #define OP_ORR 0x01800000
  200. #define OP_MOV 0x01a00000
  201. #define OP_BIC 0x01c00000
  202. #define OP_MVN 0x01e00000
  203. static unsigned long
  204. get_branch_address(struct task_struct *child, unsigned long pc, unsigned long insn)
  205. {
  206. u32 alt = 0;
  207. switch (insn & 0x0e000000) {
  208. case 0x00000000:
  209. case 0x02000000: {
  210. /*
  211. * data processing
  212. */
  213. long aluop1, aluop2, ccbit;
  214. if ((insn & 0xf000) != 0xf000)
  215. break;
  216. aluop1 = ptrace_getrn(child, insn);
  217. aluop2 = ptrace_getaluop2(child, insn);
  218. ccbit = get_user_reg(child, REG_PSR) & PSR_C_BIT ? 1 : 0;
  219. switch (insn & OP_MASK) {
  220. case OP_AND: alt = aluop1 & aluop2; break;
  221. case OP_EOR: alt = aluop1 ^ aluop2; break;
  222. case OP_SUB: alt = aluop1 - aluop2; break;
  223. case OP_RSB: alt = aluop2 - aluop1; break;
  224. case OP_ADD: alt = aluop1 + aluop2; break;
  225. case OP_ADC: alt = aluop1 + aluop2 + ccbit; break;
  226. case OP_SBC: alt = aluop1 - aluop2 + ccbit; break;
  227. case OP_RSC: alt = aluop2 - aluop1 + ccbit; break;
  228. case OP_ORR: alt = aluop1 | aluop2; break;
  229. case OP_MOV: alt = aluop2; break;
  230. case OP_BIC: alt = aluop1 & ~aluop2; break;
  231. case OP_MVN: alt = ~aluop2; break;
  232. }
  233. break;
  234. }
  235. case 0x04000000:
  236. case 0x06000000:
  237. /*
  238. * ldr
  239. */
  240. if ((insn & 0x0010f000) == 0x0010f000) {
  241. unsigned long base;
  242. base = ptrace_getrn(child, insn);
  243. if (insn & 1 << 24) {
  244. long aluop2;
  245. if (insn & 0x02000000)
  246. aluop2 = ptrace_getldrop2(child, insn);
  247. else
  248. aluop2 = insn & 0xfff;
  249. if (insn & 1 << 23)
  250. base += aluop2;
  251. else
  252. base -= aluop2;
  253. }
  254. if (read_u32(child, base, &alt) == 0)
  255. alt = pc_pointer(alt);
  256. }
  257. break;
  258. case 0x08000000:
  259. /*
  260. * ldm
  261. */
  262. if ((insn & 0x00108000) == 0x00108000) {
  263. unsigned long base;
  264. unsigned int nr_regs;
  265. if (insn & (1 << 23)) {
  266. nr_regs = hweight16(insn & 65535) << 2;
  267. if (!(insn & (1 << 24)))
  268. nr_regs -= 4;
  269. } else {
  270. if (insn & (1 << 24))
  271. nr_regs = -4;
  272. else
  273. nr_regs = 0;
  274. }
  275. base = ptrace_getrn(child, insn);
  276. if (read_u32(child, base + nr_regs, &alt) == 0)
  277. alt = pc_pointer(alt);
  278. break;
  279. }
  280. break;
  281. case 0x0a000000: {
  282. /*
  283. * bl or b
  284. */
  285. signed long displ;
  286. /* It's a branch/branch link: instead of trying to
  287. * figure out whether the branch will be taken or not,
  288. * we'll put a breakpoint at both locations. This is
  289. * simpler, more reliable, and probably not a whole lot
  290. * slower than the alternative approach of emulating the
  291. * branch.
  292. */
  293. displ = (insn & 0x00ffffff) << 8;
  294. displ = (displ >> 6) + 8;
  295. if (displ != 0 && displ != 4)
  296. alt = pc + displ;
  297. }
  298. break;
  299. }
  300. return alt;
  301. }
  302. static int
  303. swap_insn(struct task_struct *task, unsigned long addr,
  304. void *old_insn, void *new_insn, int size)
  305. {
  306. int ret;
  307. ret = access_process_vm(task, addr, old_insn, size, 0);
  308. if (ret == size)
  309. ret = access_process_vm(task, addr, new_insn, size, 1);
  310. return ret;
  311. }
  312. static void
  313. add_breakpoint(struct task_struct *task, struct debug_info *dbg, unsigned long addr)
  314. {
  315. int nr = dbg->nsaved;
  316. if (nr < 2) {
  317. u32 new_insn = BREAKINST_ARM;
  318. int res;
  319. res = swap_insn(task, addr, &dbg->bp[nr].insn, &new_insn, 4);
  320. if (res == 4) {
  321. dbg->bp[nr].address = addr;
  322. dbg->nsaved += 1;
  323. }
  324. } else
  325. printk(KERN_ERR "ptrace: too many breakpoints\n");
  326. }
  327. /*
  328. * Clear one breakpoint in the user program. We copy what the hardware
  329. * does and use bit 0 of the address to indicate whether this is a Thumb
  330. * breakpoint or an ARM breakpoint.
  331. */
  332. static void clear_breakpoint(struct task_struct *task, struct debug_entry *bp)
  333. {
  334. unsigned long addr = bp->address;
  335. union debug_insn old_insn;
  336. int ret;
  337. if (addr & 1) {
  338. ret = swap_insn(task, addr & ~1, &old_insn.thumb,
  339. &bp->insn.thumb, 2);
  340. if (ret != 2 || old_insn.thumb != BREAKINST_THUMB)
  341. printk(KERN_ERR "%s:%d: corrupted Thumb breakpoint at "
  342. "0x%08lx (0x%04x)\n", task->comm, task->pid,
  343. addr, old_insn.thumb);
  344. } else {
  345. ret = swap_insn(task, addr & ~3, &old_insn.arm,
  346. &bp->insn.arm, 4);
  347. if (ret != 4 || old_insn.arm != BREAKINST_ARM)
  348. printk(KERN_ERR "%s:%d: corrupted ARM breakpoint at "
  349. "0x%08lx (0x%08x)\n", task->comm, task->pid,
  350. addr, old_insn.arm);
  351. }
  352. }
  353. void ptrace_set_bpt(struct task_struct *child)
  354. {
  355. struct pt_regs *regs;
  356. unsigned long pc;
  357. u32 insn;
  358. int res;
  359. regs = get_user_regs(child);
  360. pc = instruction_pointer(regs);
  361. if (thumb_mode(regs)) {
  362. printk(KERN_WARNING "ptrace: can't handle thumb mode\n");
  363. return;
  364. }
  365. res = read_instr(child, pc, &insn);
  366. if (!res) {
  367. struct debug_info *dbg = &child->thread.debug;
  368. unsigned long alt;
  369. dbg->nsaved = 0;
  370. alt = get_branch_address(child, pc, insn);
  371. if (alt)
  372. add_breakpoint(child, dbg, alt);
  373. /*
  374. * Note that we ignore the result of setting the above
  375. * breakpoint since it may fail. When it does, this is
  376. * not so much an error, but a forewarning that we may
  377. * be receiving a prefetch abort shortly.
  378. *
  379. * If we don't set this breakpoint here, then we can
  380. * lose control of the thread during single stepping.
  381. */
  382. if (!alt || predicate(insn) != PREDICATE_ALWAYS)
  383. add_breakpoint(child, dbg, pc + 4);
  384. }
  385. }
  386. /*
  387. * Ensure no single-step breakpoint is pending. Returns non-zero
  388. * value if child was being single-stepped.
  389. */
  390. void ptrace_cancel_bpt(struct task_struct *child)
  391. {
  392. int i, nsaved = child->thread.debug.nsaved;
  393. child->thread.debug.nsaved = 0;
  394. if (nsaved > 2) {
  395. printk("ptrace_cancel_bpt: bogus nsaved: %d!\n", nsaved);
  396. nsaved = 2;
  397. }
  398. for (i = 0; i < nsaved; i++)
  399. clear_breakpoint(child, &child->thread.debug.bp[i]);
  400. }
  401. /*
  402. * Called by kernel/ptrace.c when detaching..
  403. *
  404. * Make sure the single step bit is not set.
  405. */
  406. void ptrace_disable(struct task_struct *child)
  407. {
  408. child->ptrace &= ~PT_SINGLESTEP;
  409. ptrace_cancel_bpt(child);
  410. }
  411. /*
  412. * Handle hitting a breakpoint.
  413. */
  414. void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
  415. {
  416. siginfo_t info;
  417. ptrace_cancel_bpt(tsk);
  418. info.si_signo = SIGTRAP;
  419. info.si_errno = 0;
  420. info.si_code = TRAP_BRKPT;
  421. info.si_addr = (void __user *)instruction_pointer(regs);
  422. force_sig_info(SIGTRAP, &info, tsk);
  423. }
  424. static int break_trap(struct pt_regs *regs, unsigned int instr)
  425. {
  426. ptrace_break(current, regs);
  427. return 0;
  428. }
  429. static struct undef_hook arm_break_hook = {
  430. .instr_mask = 0x0fffffff,
  431. .instr_val = 0x07f001f0,
  432. .cpsr_mask = PSR_T_BIT,
  433. .cpsr_val = 0,
  434. .fn = break_trap,
  435. };
  436. static struct undef_hook thumb_break_hook = {
  437. .instr_mask = 0xffff,
  438. .instr_val = 0xde01,
  439. .cpsr_mask = PSR_T_BIT,
  440. .cpsr_val = PSR_T_BIT,
  441. .fn = break_trap,
  442. };
  443. static int __init ptrace_break_init(void)
  444. {
  445. register_undef_hook(&arm_break_hook);
  446. register_undef_hook(&thumb_break_hook);
  447. return 0;
  448. }
  449. core_initcall(ptrace_break_init);
  450. /*
  451. * Read the word at offset "off" into the "struct user". We
  452. * actually access the pt_regs stored on the kernel stack.
  453. */
  454. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  455. unsigned long __user *ret)
  456. {
  457. unsigned long tmp;
  458. if (off & 3 || off >= sizeof(struct user))
  459. return -EIO;
  460. tmp = 0;
  461. if (off < sizeof(struct pt_regs))
  462. tmp = get_user_reg(tsk, off >> 2);
  463. return put_user(tmp, ret);
  464. }
  465. /*
  466. * Write the word at offset "off" into "struct user". We
  467. * actually access the pt_regs stored on the kernel stack.
  468. */
  469. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  470. unsigned long val)
  471. {
  472. if (off & 3 || off >= sizeof(struct user))
  473. return -EIO;
  474. if (off >= sizeof(struct pt_regs))
  475. return 0;
  476. return put_user_reg(tsk, off >> 2, val);
  477. }
  478. /*
  479. * Get all user integer registers.
  480. */
  481. static int ptrace_getregs(struct task_struct *tsk, void __user *uregs)
  482. {
  483. struct pt_regs *regs = get_user_regs(tsk);
  484. return copy_to_user(uregs, regs, sizeof(struct pt_regs)) ? -EFAULT : 0;
  485. }
  486. /*
  487. * Set all user integer registers.
  488. */
  489. static int ptrace_setregs(struct task_struct *tsk, void __user *uregs)
  490. {
  491. struct pt_regs newregs;
  492. int ret;
  493. ret = -EFAULT;
  494. if (copy_from_user(&newregs, uregs, sizeof(struct pt_regs)) == 0) {
  495. struct pt_regs *regs = get_user_regs(tsk);
  496. ret = -EINVAL;
  497. if (valid_user_regs(&newregs)) {
  498. *regs = newregs;
  499. ret = 0;
  500. }
  501. }
  502. return ret;
  503. }
  504. /*
  505. * Get the child FPU state.
  506. */
  507. static int ptrace_getfpregs(struct task_struct *tsk, void __user *ufp)
  508. {
  509. return copy_to_user(ufp, &tsk->thread_info->fpstate,
  510. sizeof(struct user_fp)) ? -EFAULT : 0;
  511. }
  512. /*
  513. * Set the child FPU state.
  514. */
  515. static int ptrace_setfpregs(struct task_struct *tsk, void __user *ufp)
  516. {
  517. struct thread_info *thread = tsk->thread_info;
  518. thread->used_cp[1] = thread->used_cp[2] = 1;
  519. return copy_from_user(&thread->fpstate, ufp,
  520. sizeof(struct user_fp)) ? -EFAULT : 0;
  521. }
  522. #ifdef CONFIG_IWMMXT
  523. /*
  524. * Get the child iWMMXt state.
  525. */
  526. static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
  527. {
  528. struct thread_info *thread = tsk->thread_info;
  529. void *ptr = &thread->fpstate;
  530. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  531. return -ENODATA;
  532. iwmmxt_task_disable(thread); /* force it to ram */
  533. /* The iWMMXt state is stored doubleword-aligned. */
  534. if (((long) ptr) & 4)
  535. ptr += 4;
  536. return copy_to_user(ufp, ptr, 0x98) ? -EFAULT : 0;
  537. }
  538. /*
  539. * Set the child iWMMXt state.
  540. */
  541. static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
  542. {
  543. struct thread_info *thread = tsk->thread_info;
  544. void *ptr = &thread->fpstate;
  545. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  546. return -EACCES;
  547. iwmmxt_task_release(thread); /* force a reload */
  548. /* The iWMMXt state is stored doubleword-aligned. */
  549. if (((long) ptr) & 4)
  550. ptr += 4;
  551. return copy_from_user(ptr, ufp, 0x98) ? -EFAULT : 0;
  552. }
  553. #endif
  554. static int do_ptrace(int request, struct task_struct *child, long addr, long data)
  555. {
  556. unsigned long tmp;
  557. int ret;
  558. switch (request) {
  559. /*
  560. * read word at location "addr" in the child process.
  561. */
  562. case PTRACE_PEEKTEXT:
  563. case PTRACE_PEEKDATA:
  564. ret = access_process_vm(child, addr, &tmp,
  565. sizeof(unsigned long), 0);
  566. if (ret == sizeof(unsigned long))
  567. ret = put_user(tmp, (unsigned long __user *) data);
  568. else
  569. ret = -EIO;
  570. break;
  571. case PTRACE_PEEKUSR:
  572. ret = ptrace_read_user(child, addr, (unsigned long __user *)data);
  573. break;
  574. /*
  575. * write the word at location addr.
  576. */
  577. case PTRACE_POKETEXT:
  578. case PTRACE_POKEDATA:
  579. ret = access_process_vm(child, addr, &data,
  580. sizeof(unsigned long), 1);
  581. if (ret == sizeof(unsigned long))
  582. ret = 0;
  583. else
  584. ret = -EIO;
  585. break;
  586. case PTRACE_POKEUSR:
  587. ret = ptrace_write_user(child, addr, data);
  588. break;
  589. /*
  590. * continue/restart and stop at next (return from) syscall
  591. */
  592. case PTRACE_SYSCALL:
  593. case PTRACE_CONT:
  594. ret = -EIO;
  595. if ((unsigned long) data > _NSIG)
  596. break;
  597. if (request == PTRACE_SYSCALL)
  598. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  599. else
  600. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  601. child->exit_code = data;
  602. /* make sure single-step breakpoint is gone. */
  603. child->ptrace &= ~PT_SINGLESTEP;
  604. ptrace_cancel_bpt(child);
  605. wake_up_process(child);
  606. ret = 0;
  607. break;
  608. /*
  609. * make the child exit. Best I can do is send it a sigkill.
  610. * perhaps it should be put in the status that it wants to
  611. * exit.
  612. */
  613. case PTRACE_KILL:
  614. /* make sure single-step breakpoint is gone. */
  615. child->ptrace &= ~PT_SINGLESTEP;
  616. ptrace_cancel_bpt(child);
  617. if (child->exit_state != EXIT_ZOMBIE) {
  618. child->exit_code = SIGKILL;
  619. wake_up_process(child);
  620. }
  621. ret = 0;
  622. break;
  623. /*
  624. * execute single instruction.
  625. */
  626. case PTRACE_SINGLESTEP:
  627. ret = -EIO;
  628. if ((unsigned long) data > _NSIG)
  629. break;
  630. child->ptrace |= PT_SINGLESTEP;
  631. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  632. child->exit_code = data;
  633. /* give it a chance to run. */
  634. wake_up_process(child);
  635. ret = 0;
  636. break;
  637. case PTRACE_DETACH:
  638. ret = ptrace_detach(child, data);
  639. break;
  640. case PTRACE_GETREGS:
  641. ret = ptrace_getregs(child, (void __user *)data);
  642. break;
  643. case PTRACE_SETREGS:
  644. ret = ptrace_setregs(child, (void __user *)data);
  645. break;
  646. case PTRACE_GETFPREGS:
  647. ret = ptrace_getfpregs(child, (void __user *)data);
  648. break;
  649. case PTRACE_SETFPREGS:
  650. ret = ptrace_setfpregs(child, (void __user *)data);
  651. break;
  652. #ifdef CONFIG_IWMMXT
  653. case PTRACE_GETWMMXREGS:
  654. ret = ptrace_getwmmxregs(child, (void __user *)data);
  655. break;
  656. case PTRACE_SETWMMXREGS:
  657. ret = ptrace_setwmmxregs(child, (void __user *)data);
  658. break;
  659. #endif
  660. case PTRACE_GET_THREAD_AREA:
  661. ret = put_user(child->thread_info->tp_value,
  662. (unsigned long __user *) data);
  663. break;
  664. default:
  665. ret = ptrace_request(child, request, addr, data);
  666. break;
  667. }
  668. return ret;
  669. }
  670. asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
  671. {
  672. struct task_struct *child;
  673. int ret;
  674. lock_kernel();
  675. ret = -EPERM;
  676. if (request == PTRACE_TRACEME) {
  677. /* are we already being traced? */
  678. if (current->ptrace & PT_PTRACED)
  679. goto out;
  680. ret = security_ptrace(current->parent, current);
  681. if (ret)
  682. goto out;
  683. /* set the ptrace bit in the process flags. */
  684. current->ptrace |= PT_PTRACED;
  685. ret = 0;
  686. goto out;
  687. }
  688. ret = -ESRCH;
  689. read_lock(&tasklist_lock);
  690. child = find_task_by_pid(pid);
  691. if (child)
  692. get_task_struct(child);
  693. read_unlock(&tasklist_lock);
  694. if (!child)
  695. goto out;
  696. ret = -EPERM;
  697. if (pid == 1) /* you may not mess with init */
  698. goto out_tsk;
  699. if (request == PTRACE_ATTACH) {
  700. ret = ptrace_attach(child);
  701. goto out_tsk;
  702. }
  703. ret = ptrace_check_attach(child, request == PTRACE_KILL);
  704. if (ret == 0)
  705. ret = do_ptrace(request, child, addr, data);
  706. out_tsk:
  707. put_task_struct(child);
  708. out:
  709. unlock_kernel();
  710. return ret;
  711. }
  712. asmlinkage void syscall_trace(int why, struct pt_regs *regs)
  713. {
  714. unsigned long ip;
  715. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  716. return;
  717. if (!(current->ptrace & PT_PTRACED))
  718. return;
  719. /*
  720. * Save IP. IP is used to denote syscall entry/exit:
  721. * IP = 0 -> entry, = 1 -> exit
  722. */
  723. ip = regs->ARM_ip;
  724. regs->ARM_ip = why;
  725. /* the 0x80 provides a way for the tracing parent to distinguish
  726. between a syscall stop and SIGTRAP delivery */
  727. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  728. ? 0x80 : 0));
  729. /*
  730. * this isn't the same as continuing with a signal, but it will do
  731. * for normal use. strace only continues with a signal if the
  732. * stopping signal is not SIGTRAP. -brl
  733. */
  734. if (current->exit_code) {
  735. send_sig(current->exit_code, current, 1);
  736. current->exit_code = 0;
  737. }
  738. regs->ARM_ip = ip;
  739. }