ptrace.c 22 KB

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