ptrace.c 19 KB

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