ptrace.c 22 KB

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