ptrace.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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/elf.h>
  16. #include <linux/smp.h>
  17. #include <linux/ptrace.h>
  18. #include <linux/user.h>
  19. #include <linux/security.h>
  20. #include <linux/init.h>
  21. #include <linux/signal.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/perf_event.h>
  24. #include <linux/hw_breakpoint.h>
  25. #include <linux/regset.h>
  26. #include <linux/audit.h>
  27. #include <linux/tracehook.h>
  28. #include <linux/unistd.h>
  29. #include <asm/pgtable.h>
  30. #include <asm/traps.h>
  31. #define REG_PC 15
  32. #define REG_PSR 16
  33. /*
  34. * does not yet catch signals sent when the child dies.
  35. * in exit.c or in signal.c.
  36. */
  37. #if 0
  38. /*
  39. * Breakpoint SWI instruction: SWI &9F0001
  40. */
  41. #define BREAKINST_ARM 0xef9f0001
  42. #define BREAKINST_THUMB 0xdf00 /* fill this in later */
  43. #else
  44. /*
  45. * New breakpoints - use an undefined instruction. The ARM architecture
  46. * reference manual guarantees that the following instruction space
  47. * will produce an undefined instruction exception on all CPUs:
  48. *
  49. * ARM: xxxx 0111 1111 xxxx xxxx xxxx 1111 xxxx
  50. * Thumb: 1101 1110 xxxx xxxx
  51. */
  52. #define BREAKINST_ARM 0xe7f001f0
  53. #define BREAKINST_THUMB 0xde01
  54. #endif
  55. struct pt_regs_offset {
  56. const char *name;
  57. int offset;
  58. };
  59. #define REG_OFFSET_NAME(r) \
  60. {.name = #r, .offset = offsetof(struct pt_regs, ARM_##r)}
  61. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  62. static const struct pt_regs_offset regoffset_table[] = {
  63. REG_OFFSET_NAME(r0),
  64. REG_OFFSET_NAME(r1),
  65. REG_OFFSET_NAME(r2),
  66. REG_OFFSET_NAME(r3),
  67. REG_OFFSET_NAME(r4),
  68. REG_OFFSET_NAME(r5),
  69. REG_OFFSET_NAME(r6),
  70. REG_OFFSET_NAME(r7),
  71. REG_OFFSET_NAME(r8),
  72. REG_OFFSET_NAME(r9),
  73. REG_OFFSET_NAME(r10),
  74. REG_OFFSET_NAME(fp),
  75. REG_OFFSET_NAME(ip),
  76. REG_OFFSET_NAME(sp),
  77. REG_OFFSET_NAME(lr),
  78. REG_OFFSET_NAME(pc),
  79. REG_OFFSET_NAME(cpsr),
  80. REG_OFFSET_NAME(ORIG_r0),
  81. REG_OFFSET_END,
  82. };
  83. /**
  84. * regs_query_register_offset() - query register offset from its name
  85. * @name: the name of a register
  86. *
  87. * regs_query_register_offset() returns the offset of a register in struct
  88. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  89. */
  90. int regs_query_register_offset(const char *name)
  91. {
  92. const struct pt_regs_offset *roff;
  93. for (roff = regoffset_table; roff->name != NULL; roff++)
  94. if (!strcmp(roff->name, name))
  95. return roff->offset;
  96. return -EINVAL;
  97. }
  98. /**
  99. * regs_query_register_name() - query register name from its offset
  100. * @offset: the offset of a register in struct pt_regs.
  101. *
  102. * regs_query_register_name() returns the name of a register from its
  103. * offset in struct pt_regs. If the @offset is invalid, this returns NULL;
  104. */
  105. const char *regs_query_register_name(unsigned int offset)
  106. {
  107. const struct pt_regs_offset *roff;
  108. for (roff = regoffset_table; roff->name != NULL; roff++)
  109. if (roff->offset == offset)
  110. return roff->name;
  111. return NULL;
  112. }
  113. /**
  114. * regs_within_kernel_stack() - check the address in the stack
  115. * @regs: pt_regs which contains kernel stack pointer.
  116. * @addr: address which is checked.
  117. *
  118. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  119. * If @addr is within the kernel stack, it returns true. If not, returns false.
  120. */
  121. bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  122. {
  123. return ((addr & ~(THREAD_SIZE - 1)) ==
  124. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
  125. }
  126. /**
  127. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  128. * @regs: pt_regs which contains kernel stack pointer.
  129. * @n: stack entry number.
  130. *
  131. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  132. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  133. * this returns 0.
  134. */
  135. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  136. {
  137. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  138. addr += n;
  139. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  140. return *addr;
  141. else
  142. return 0;
  143. }
  144. /*
  145. * this routine will get a word off of the processes privileged stack.
  146. * the offset is how far from the base addr as stored in the THREAD.
  147. * this routine assumes that all the privileged stacks are in our
  148. * data space.
  149. */
  150. static inline long get_user_reg(struct task_struct *task, int offset)
  151. {
  152. return task_pt_regs(task)->uregs[offset];
  153. }
  154. /*
  155. * this routine will put a word on the processes privileged stack.
  156. * the offset is how far from the base addr as stored in the THREAD.
  157. * this routine assumes that all the privileged stacks are in our
  158. * data space.
  159. */
  160. static inline int
  161. put_user_reg(struct task_struct *task, int offset, long data)
  162. {
  163. struct pt_regs newregs, *regs = task_pt_regs(task);
  164. int ret = -EINVAL;
  165. newregs = *regs;
  166. newregs.uregs[offset] = data;
  167. if (valid_user_regs(&newregs)) {
  168. regs->uregs[offset] = data;
  169. ret = 0;
  170. }
  171. return ret;
  172. }
  173. /*
  174. * Called by kernel/ptrace.c when detaching..
  175. */
  176. void ptrace_disable(struct task_struct *child)
  177. {
  178. /* Nothing to do. */
  179. }
  180. /*
  181. * Handle hitting a breakpoint.
  182. */
  183. void ptrace_break(struct task_struct *tsk, struct pt_regs *regs)
  184. {
  185. siginfo_t info;
  186. info.si_signo = SIGTRAP;
  187. info.si_errno = 0;
  188. info.si_code = TRAP_BRKPT;
  189. info.si_addr = (void __user *)instruction_pointer(regs);
  190. force_sig_info(SIGTRAP, &info, tsk);
  191. }
  192. static int break_trap(struct pt_regs *regs, unsigned int instr)
  193. {
  194. ptrace_break(current, regs);
  195. return 0;
  196. }
  197. static struct undef_hook arm_break_hook = {
  198. .instr_mask = 0x0fffffff,
  199. .instr_val = 0x07f001f0,
  200. .cpsr_mask = PSR_T_BIT,
  201. .cpsr_val = 0,
  202. .fn = break_trap,
  203. };
  204. static struct undef_hook thumb_break_hook = {
  205. .instr_mask = 0xffff,
  206. .instr_val = 0xde01,
  207. .cpsr_mask = PSR_T_BIT,
  208. .cpsr_val = PSR_T_BIT,
  209. .fn = break_trap,
  210. };
  211. static struct undef_hook thumb2_break_hook = {
  212. .instr_mask = 0xffffffff,
  213. .instr_val = 0xf7f0a000,
  214. .cpsr_mask = PSR_T_BIT,
  215. .cpsr_val = PSR_T_BIT,
  216. .fn = break_trap,
  217. };
  218. static int __init ptrace_break_init(void)
  219. {
  220. register_undef_hook(&arm_break_hook);
  221. register_undef_hook(&thumb_break_hook);
  222. register_undef_hook(&thumb2_break_hook);
  223. return 0;
  224. }
  225. core_initcall(ptrace_break_init);
  226. /*
  227. * Read the word at offset "off" into the "struct user". We
  228. * actually access the pt_regs stored on the kernel stack.
  229. */
  230. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  231. unsigned long __user *ret)
  232. {
  233. unsigned long tmp;
  234. if (off & 3)
  235. return -EIO;
  236. tmp = 0;
  237. if (off == PT_TEXT_ADDR)
  238. tmp = tsk->mm->start_code;
  239. else if (off == PT_DATA_ADDR)
  240. tmp = tsk->mm->start_data;
  241. else if (off == PT_TEXT_END_ADDR)
  242. tmp = tsk->mm->end_code;
  243. else if (off < sizeof(struct pt_regs))
  244. tmp = get_user_reg(tsk, off >> 2);
  245. else if (off >= sizeof(struct user))
  246. return -EIO;
  247. return put_user(tmp, ret);
  248. }
  249. /*
  250. * Write the word at offset "off" into "struct user". We
  251. * actually access the pt_regs stored on the kernel stack.
  252. */
  253. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  254. unsigned long val)
  255. {
  256. if (off & 3 || off >= sizeof(struct user))
  257. return -EIO;
  258. if (off >= sizeof(struct pt_regs))
  259. return 0;
  260. return put_user_reg(tsk, off >> 2, val);
  261. }
  262. #ifdef CONFIG_IWMMXT
  263. /*
  264. * Get the child iWMMXt state.
  265. */
  266. static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
  267. {
  268. struct thread_info *thread = task_thread_info(tsk);
  269. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  270. return -ENODATA;
  271. iwmmxt_task_disable(thread); /* force it to ram */
  272. return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
  273. ? -EFAULT : 0;
  274. }
  275. /*
  276. * Set the child iWMMXt state.
  277. */
  278. static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
  279. {
  280. struct thread_info *thread = task_thread_info(tsk);
  281. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  282. return -EACCES;
  283. iwmmxt_task_release(thread); /* force a reload */
  284. return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
  285. ? -EFAULT : 0;
  286. }
  287. #endif
  288. #ifdef CONFIG_CRUNCH
  289. /*
  290. * Get the child Crunch state.
  291. */
  292. static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
  293. {
  294. struct thread_info *thread = task_thread_info(tsk);
  295. crunch_task_disable(thread); /* force it to ram */
  296. return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
  297. ? -EFAULT : 0;
  298. }
  299. /*
  300. * Set the child Crunch state.
  301. */
  302. static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
  303. {
  304. struct thread_info *thread = task_thread_info(tsk);
  305. crunch_task_release(thread); /* force a reload */
  306. return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
  307. ? -EFAULT : 0;
  308. }
  309. #endif
  310. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  311. /*
  312. * Convert a virtual register number into an index for a thread_info
  313. * breakpoint array. Breakpoints are identified using positive numbers
  314. * whilst watchpoints are negative. The registers are laid out as pairs
  315. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  316. * Register 0 is reserved for describing resource information.
  317. */
  318. static int ptrace_hbp_num_to_idx(long num)
  319. {
  320. if (num < 0)
  321. num = (ARM_MAX_BRP << 1) - num;
  322. return (num - 1) >> 1;
  323. }
  324. /*
  325. * Returns the virtual register number for the address of the
  326. * breakpoint at index idx.
  327. */
  328. static long ptrace_hbp_idx_to_num(int idx)
  329. {
  330. long mid = ARM_MAX_BRP << 1;
  331. long num = (idx << 1) + 1;
  332. return num > mid ? mid - num : num;
  333. }
  334. /*
  335. * Handle hitting a HW-breakpoint.
  336. */
  337. static void ptrace_hbptriggered(struct perf_event *bp,
  338. struct perf_sample_data *data,
  339. struct pt_regs *regs)
  340. {
  341. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  342. long num;
  343. int i;
  344. siginfo_t info;
  345. for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
  346. if (current->thread.debug.hbp[i] == bp)
  347. break;
  348. num = (i == ARM_MAX_HBP_SLOTS) ? 0 : ptrace_hbp_idx_to_num(i);
  349. info.si_signo = SIGTRAP;
  350. info.si_errno = (int)num;
  351. info.si_code = TRAP_HWBKPT;
  352. info.si_addr = (void __user *)(bkpt->trigger);
  353. force_sig_info(SIGTRAP, &info, current);
  354. }
  355. /*
  356. * Set ptrace breakpoint pointers to zero for this task.
  357. * This is required in order to prevent child processes from unregistering
  358. * breakpoints held by their parent.
  359. */
  360. void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
  361. {
  362. memset(tsk->thread.debug.hbp, 0, sizeof(tsk->thread.debug.hbp));
  363. }
  364. /*
  365. * Unregister breakpoints from this task and reset the pointers in
  366. * the thread_struct.
  367. */
  368. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  369. {
  370. int i;
  371. struct thread_struct *t = &tsk->thread;
  372. for (i = 0; i < ARM_MAX_HBP_SLOTS; i++) {
  373. if (t->debug.hbp[i]) {
  374. unregister_hw_breakpoint(t->debug.hbp[i]);
  375. t->debug.hbp[i] = NULL;
  376. }
  377. }
  378. }
  379. static u32 ptrace_get_hbp_resource_info(void)
  380. {
  381. u8 num_brps, num_wrps, debug_arch, wp_len;
  382. u32 reg = 0;
  383. num_brps = hw_breakpoint_slots(TYPE_INST);
  384. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  385. debug_arch = arch_get_debug_arch();
  386. wp_len = arch_get_max_wp_len();
  387. reg |= debug_arch;
  388. reg <<= 8;
  389. reg |= wp_len;
  390. reg <<= 8;
  391. reg |= num_wrps;
  392. reg <<= 8;
  393. reg |= num_brps;
  394. return reg;
  395. }
  396. static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
  397. {
  398. struct perf_event_attr attr;
  399. ptrace_breakpoint_init(&attr);
  400. /* Initialise fields to sane defaults. */
  401. attr.bp_addr = 0;
  402. attr.bp_len = HW_BREAKPOINT_LEN_4;
  403. attr.bp_type = type;
  404. attr.disabled = 1;
  405. return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
  406. tsk);
  407. }
  408. static int ptrace_gethbpregs(struct task_struct *tsk, long num,
  409. unsigned long __user *data)
  410. {
  411. u32 reg;
  412. int idx, ret = 0;
  413. struct perf_event *bp;
  414. struct arch_hw_breakpoint_ctrl arch_ctrl;
  415. if (num == 0) {
  416. reg = ptrace_get_hbp_resource_info();
  417. } else {
  418. idx = ptrace_hbp_num_to_idx(num);
  419. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  420. ret = -EINVAL;
  421. goto out;
  422. }
  423. bp = tsk->thread.debug.hbp[idx];
  424. if (!bp) {
  425. reg = 0;
  426. goto put;
  427. }
  428. arch_ctrl = counter_arch_bp(bp)->ctrl;
  429. /*
  430. * Fix up the len because we may have adjusted it
  431. * to compensate for an unaligned address.
  432. */
  433. while (!(arch_ctrl.len & 0x1))
  434. arch_ctrl.len >>= 1;
  435. if (num & 0x1)
  436. reg = bp->attr.bp_addr;
  437. else
  438. reg = encode_ctrl_reg(arch_ctrl);
  439. }
  440. put:
  441. if (put_user(reg, data))
  442. ret = -EFAULT;
  443. out:
  444. return ret;
  445. }
  446. static int ptrace_sethbpregs(struct task_struct *tsk, long num,
  447. unsigned long __user *data)
  448. {
  449. int idx, gen_len, gen_type, implied_type, ret = 0;
  450. u32 user_val;
  451. struct perf_event *bp;
  452. struct arch_hw_breakpoint_ctrl ctrl;
  453. struct perf_event_attr attr;
  454. if (num == 0)
  455. goto out;
  456. else if (num < 0)
  457. implied_type = HW_BREAKPOINT_RW;
  458. else
  459. implied_type = HW_BREAKPOINT_X;
  460. idx = ptrace_hbp_num_to_idx(num);
  461. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  462. ret = -EINVAL;
  463. goto out;
  464. }
  465. if (get_user(user_val, data)) {
  466. ret = -EFAULT;
  467. goto out;
  468. }
  469. bp = tsk->thread.debug.hbp[idx];
  470. if (!bp) {
  471. bp = ptrace_hbp_create(tsk, implied_type);
  472. if (IS_ERR(bp)) {
  473. ret = PTR_ERR(bp);
  474. goto out;
  475. }
  476. tsk->thread.debug.hbp[idx] = bp;
  477. }
  478. attr = bp->attr;
  479. if (num & 0x1) {
  480. /* Address */
  481. attr.bp_addr = user_val;
  482. } else {
  483. /* Control */
  484. decode_ctrl_reg(user_val, &ctrl);
  485. ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
  486. if (ret)
  487. goto out;
  488. if ((gen_type & implied_type) != gen_type) {
  489. ret = -EINVAL;
  490. goto out;
  491. }
  492. attr.bp_len = gen_len;
  493. attr.bp_type = gen_type;
  494. attr.disabled = !ctrl.enabled;
  495. }
  496. ret = modify_user_hw_breakpoint(bp, &attr);
  497. out:
  498. return ret;
  499. }
  500. #endif
  501. /* regset get/set implementations */
  502. static int gpr_get(struct task_struct *target,
  503. const struct user_regset *regset,
  504. unsigned int pos, unsigned int count,
  505. void *kbuf, void __user *ubuf)
  506. {
  507. struct pt_regs *regs = task_pt_regs(target);
  508. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  509. regs,
  510. 0, sizeof(*regs));
  511. }
  512. static int gpr_set(struct task_struct *target,
  513. const struct user_regset *regset,
  514. unsigned int pos, unsigned int count,
  515. const void *kbuf, const void __user *ubuf)
  516. {
  517. int ret;
  518. struct pt_regs newregs;
  519. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  520. &newregs,
  521. 0, sizeof(newregs));
  522. if (ret)
  523. return ret;
  524. if (!valid_user_regs(&newregs))
  525. return -EINVAL;
  526. *task_pt_regs(target) = newregs;
  527. return 0;
  528. }
  529. static int fpa_get(struct task_struct *target,
  530. const struct user_regset *regset,
  531. unsigned int pos, unsigned int count,
  532. void *kbuf, void __user *ubuf)
  533. {
  534. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  535. &task_thread_info(target)->fpstate,
  536. 0, sizeof(struct user_fp));
  537. }
  538. static int fpa_set(struct task_struct *target,
  539. const struct user_regset *regset,
  540. unsigned int pos, unsigned int count,
  541. const void *kbuf, const void __user *ubuf)
  542. {
  543. struct thread_info *thread = task_thread_info(target);
  544. thread->used_cp[1] = thread->used_cp[2] = 1;
  545. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  546. &thread->fpstate,
  547. 0, sizeof(struct user_fp));
  548. }
  549. #ifdef CONFIG_VFP
  550. /*
  551. * VFP register get/set implementations.
  552. *
  553. * With respect to the kernel, struct user_fp is divided into three chunks:
  554. * 16 or 32 real VFP registers (d0-d15 or d0-31)
  555. * These are transferred to/from the real registers in the task's
  556. * vfp_hard_struct. The number of registers depends on the kernel
  557. * configuration.
  558. *
  559. * 16 or 0 fake VFP registers (d16-d31 or empty)
  560. * i.e., the user_vfp structure has space for 32 registers even if
  561. * the kernel doesn't have them all.
  562. *
  563. * vfp_get() reads this chunk as zero where applicable
  564. * vfp_set() ignores this chunk
  565. *
  566. * 1 word for the FPSCR
  567. *
  568. * The bounds-checking logic built into user_regset_copyout and friends
  569. * means that we can make a simple sequence of calls to map the relevant data
  570. * to/from the specified slice of the user regset structure.
  571. */
  572. static int vfp_get(struct task_struct *target,
  573. const struct user_regset *regset,
  574. unsigned int pos, unsigned int count,
  575. void *kbuf, void __user *ubuf)
  576. {
  577. int ret;
  578. struct thread_info *thread = task_thread_info(target);
  579. struct vfp_hard_struct const *vfp = &thread->vfpstate.hard;
  580. const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
  581. const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
  582. vfp_sync_hwstate(thread);
  583. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  584. &vfp->fpregs,
  585. user_fpregs_offset,
  586. user_fpregs_offset + sizeof(vfp->fpregs));
  587. if (ret)
  588. return ret;
  589. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  590. user_fpregs_offset + sizeof(vfp->fpregs),
  591. user_fpscr_offset);
  592. if (ret)
  593. return ret;
  594. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  595. &vfp->fpscr,
  596. user_fpscr_offset,
  597. user_fpscr_offset + sizeof(vfp->fpscr));
  598. }
  599. /*
  600. * For vfp_set() a read-modify-write is done on the VFP registers,
  601. * in order to avoid writing back a half-modified set of registers on
  602. * failure.
  603. */
  604. static int vfp_set(struct task_struct *target,
  605. const struct user_regset *regset,
  606. unsigned int pos, unsigned int count,
  607. const void *kbuf, const void __user *ubuf)
  608. {
  609. int ret;
  610. struct thread_info *thread = task_thread_info(target);
  611. struct vfp_hard_struct new_vfp;
  612. const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
  613. const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
  614. vfp_sync_hwstate(thread);
  615. new_vfp = thread->vfpstate.hard;
  616. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  617. &new_vfp.fpregs,
  618. user_fpregs_offset,
  619. user_fpregs_offset + sizeof(new_vfp.fpregs));
  620. if (ret)
  621. return ret;
  622. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  623. user_fpregs_offset + sizeof(new_vfp.fpregs),
  624. user_fpscr_offset);
  625. if (ret)
  626. return ret;
  627. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  628. &new_vfp.fpscr,
  629. user_fpscr_offset,
  630. user_fpscr_offset + sizeof(new_vfp.fpscr));
  631. if (ret)
  632. return ret;
  633. vfp_flush_hwstate(thread);
  634. thread->vfpstate.hard = new_vfp;
  635. return 0;
  636. }
  637. #endif /* CONFIG_VFP */
  638. enum arm_regset {
  639. REGSET_GPR,
  640. REGSET_FPR,
  641. #ifdef CONFIG_VFP
  642. REGSET_VFP,
  643. #endif
  644. };
  645. static const struct user_regset arm_regsets[] = {
  646. [REGSET_GPR] = {
  647. .core_note_type = NT_PRSTATUS,
  648. .n = ELF_NGREG,
  649. .size = sizeof(u32),
  650. .align = sizeof(u32),
  651. .get = gpr_get,
  652. .set = gpr_set
  653. },
  654. [REGSET_FPR] = {
  655. /*
  656. * For the FPA regs in fpstate, the real fields are a mixture
  657. * of sizes, so pretend that the registers are word-sized:
  658. */
  659. .core_note_type = NT_PRFPREG,
  660. .n = sizeof(struct user_fp) / sizeof(u32),
  661. .size = sizeof(u32),
  662. .align = sizeof(u32),
  663. .get = fpa_get,
  664. .set = fpa_set
  665. },
  666. #ifdef CONFIG_VFP
  667. [REGSET_VFP] = {
  668. /*
  669. * Pretend that the VFP regs are word-sized, since the FPSCR is
  670. * a single word dangling at the end of struct user_vfp:
  671. */
  672. .core_note_type = NT_ARM_VFP,
  673. .n = ARM_VFPREGS_SIZE / sizeof(u32),
  674. .size = sizeof(u32),
  675. .align = sizeof(u32),
  676. .get = vfp_get,
  677. .set = vfp_set
  678. },
  679. #endif /* CONFIG_VFP */
  680. };
  681. static const struct user_regset_view user_arm_view = {
  682. .name = "arm", .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  683. .regsets = arm_regsets, .n = ARRAY_SIZE(arm_regsets)
  684. };
  685. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  686. {
  687. return &user_arm_view;
  688. }
  689. long arch_ptrace(struct task_struct *child, long request,
  690. unsigned long addr, unsigned long data)
  691. {
  692. int ret;
  693. unsigned long __user *datap = (unsigned long __user *) data;
  694. switch (request) {
  695. case PTRACE_PEEKUSR:
  696. ret = ptrace_read_user(child, addr, datap);
  697. break;
  698. case PTRACE_POKEUSR:
  699. ret = ptrace_write_user(child, addr, data);
  700. break;
  701. case PTRACE_GETREGS:
  702. ret = copy_regset_to_user(child,
  703. &user_arm_view, REGSET_GPR,
  704. 0, sizeof(struct pt_regs),
  705. datap);
  706. break;
  707. case PTRACE_SETREGS:
  708. ret = copy_regset_from_user(child,
  709. &user_arm_view, REGSET_GPR,
  710. 0, sizeof(struct pt_regs),
  711. datap);
  712. break;
  713. case PTRACE_GETFPREGS:
  714. ret = copy_regset_to_user(child,
  715. &user_arm_view, REGSET_FPR,
  716. 0, sizeof(union fp_state),
  717. datap);
  718. break;
  719. case PTRACE_SETFPREGS:
  720. ret = copy_regset_from_user(child,
  721. &user_arm_view, REGSET_FPR,
  722. 0, sizeof(union fp_state),
  723. datap);
  724. break;
  725. #ifdef CONFIG_IWMMXT
  726. case PTRACE_GETWMMXREGS:
  727. ret = ptrace_getwmmxregs(child, datap);
  728. break;
  729. case PTRACE_SETWMMXREGS:
  730. ret = ptrace_setwmmxregs(child, datap);
  731. break;
  732. #endif
  733. case PTRACE_GET_THREAD_AREA:
  734. ret = put_user(task_thread_info(child)->tp_value,
  735. datap);
  736. break;
  737. case PTRACE_SET_SYSCALL:
  738. task_thread_info(child)->syscall = data;
  739. ret = 0;
  740. break;
  741. #ifdef CONFIG_CRUNCH
  742. case PTRACE_GETCRUNCHREGS:
  743. ret = ptrace_getcrunchregs(child, datap);
  744. break;
  745. case PTRACE_SETCRUNCHREGS:
  746. ret = ptrace_setcrunchregs(child, datap);
  747. break;
  748. #endif
  749. #ifdef CONFIG_VFP
  750. case PTRACE_GETVFPREGS:
  751. ret = copy_regset_to_user(child,
  752. &user_arm_view, REGSET_VFP,
  753. 0, ARM_VFPREGS_SIZE,
  754. datap);
  755. break;
  756. case PTRACE_SETVFPREGS:
  757. ret = copy_regset_from_user(child,
  758. &user_arm_view, REGSET_VFP,
  759. 0, ARM_VFPREGS_SIZE,
  760. datap);
  761. break;
  762. #endif
  763. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  764. case PTRACE_GETHBPREGS:
  765. if (ptrace_get_breakpoints(child) < 0)
  766. return -ESRCH;
  767. ret = ptrace_gethbpregs(child, addr,
  768. (unsigned long __user *)data);
  769. ptrace_put_breakpoints(child);
  770. break;
  771. case PTRACE_SETHBPREGS:
  772. if (ptrace_get_breakpoints(child) < 0)
  773. return -ESRCH;
  774. ret = ptrace_sethbpregs(child, addr,
  775. (unsigned long __user *)data);
  776. ptrace_put_breakpoints(child);
  777. break;
  778. #endif
  779. default:
  780. ret = ptrace_request(child, request, addr, data);
  781. break;
  782. }
  783. return ret;
  784. }
  785. enum ptrace_syscall_dir {
  786. PTRACE_SYSCALL_ENTER = 0,
  787. PTRACE_SYSCALL_EXIT,
  788. };
  789. static int ptrace_syscall_trace(struct pt_regs *regs, int scno,
  790. enum ptrace_syscall_dir dir)
  791. {
  792. unsigned long ip;
  793. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  794. return scno;
  795. current_thread_info()->syscall = scno;
  796. /*
  797. * IP is used to denote syscall entry/exit:
  798. * IP = 0 -> entry, =1 -> exit
  799. */
  800. ip = regs->ARM_ip;
  801. regs->ARM_ip = dir;
  802. if (dir == PTRACE_SYSCALL_EXIT)
  803. tracehook_report_syscall_exit(regs, 0);
  804. else if (tracehook_report_syscall_entry(regs))
  805. current_thread_info()->syscall = -1;
  806. regs->ARM_ip = ip;
  807. return current_thread_info()->syscall;
  808. }
  809. asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
  810. {
  811. int ret = ptrace_syscall_trace(regs, scno, PTRACE_SYSCALL_ENTER);
  812. audit_syscall_entry(AUDIT_ARCH_ARM, scno, regs->ARM_r0, regs->ARM_r1,
  813. regs->ARM_r2, regs->ARM_r3);
  814. return ret;
  815. }
  816. asmlinkage int syscall_trace_exit(struct pt_regs *regs, int scno)
  817. {
  818. int ret = ptrace_syscall_trace(regs, scno, PTRACE_SYSCALL_EXIT);
  819. audit_syscall_exit(regs);
  820. return ret;
  821. }