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 <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)
  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. else if (off >= sizeof(struct user))
  244. return -EIO;
  245. return put_user(tmp, ret);
  246. }
  247. /*
  248. * Write the word at offset "off" into "struct user". We
  249. * actually access the pt_regs stored on the kernel stack.
  250. */
  251. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  252. unsigned long val)
  253. {
  254. if (off & 3 || off >= sizeof(struct user))
  255. return -EIO;
  256. if (off >= sizeof(struct pt_regs))
  257. return 0;
  258. return put_user_reg(tsk, off >> 2, val);
  259. }
  260. #ifdef CONFIG_IWMMXT
  261. /*
  262. * Get the child iWMMXt state.
  263. */
  264. static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
  265. {
  266. struct thread_info *thread = task_thread_info(tsk);
  267. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  268. return -ENODATA;
  269. iwmmxt_task_disable(thread); /* force it to ram */
  270. return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
  271. ? -EFAULT : 0;
  272. }
  273. /*
  274. * Set the child iWMMXt state.
  275. */
  276. static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
  277. {
  278. struct thread_info *thread = task_thread_info(tsk);
  279. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  280. return -EACCES;
  281. iwmmxt_task_release(thread); /* force a reload */
  282. return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
  283. ? -EFAULT : 0;
  284. }
  285. #endif
  286. #ifdef CONFIG_CRUNCH
  287. /*
  288. * Get the child Crunch state.
  289. */
  290. static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
  291. {
  292. struct thread_info *thread = task_thread_info(tsk);
  293. crunch_task_disable(thread); /* force it to ram */
  294. return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
  295. ? -EFAULT : 0;
  296. }
  297. /*
  298. * Set the child Crunch state.
  299. */
  300. static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
  301. {
  302. struct thread_info *thread = task_thread_info(tsk);
  303. crunch_task_release(thread); /* force a reload */
  304. return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
  305. ? -EFAULT : 0;
  306. }
  307. #endif
  308. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  309. /*
  310. * Convert a virtual register number into an index for a thread_info
  311. * breakpoint array. Breakpoints are identified using positive numbers
  312. * whilst watchpoints are negative. The registers are laid out as pairs
  313. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  314. * Register 0 is reserved for describing resource information.
  315. */
  316. static int ptrace_hbp_num_to_idx(long num)
  317. {
  318. if (num < 0)
  319. num = (ARM_MAX_BRP << 1) - num;
  320. return (num - 1) >> 1;
  321. }
  322. /*
  323. * Returns the virtual register number for the address of the
  324. * breakpoint at index idx.
  325. */
  326. static long ptrace_hbp_idx_to_num(int idx)
  327. {
  328. long mid = ARM_MAX_BRP << 1;
  329. long num = (idx << 1) + 1;
  330. return num > mid ? mid - num : num;
  331. }
  332. /*
  333. * Handle hitting a HW-breakpoint.
  334. */
  335. static void ptrace_hbptriggered(struct perf_event *bp,
  336. struct perf_sample_data *data,
  337. struct pt_regs *regs)
  338. {
  339. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  340. long num;
  341. int i;
  342. siginfo_t info;
  343. for (i = 0; i < ARM_MAX_HBP_SLOTS; ++i)
  344. if (current->thread.debug.hbp[i] == bp)
  345. break;
  346. num = (i == ARM_MAX_HBP_SLOTS) ? 0 : ptrace_hbp_idx_to_num(i);
  347. info.si_signo = SIGTRAP;
  348. info.si_errno = (int)num;
  349. info.si_code = TRAP_HWBKPT;
  350. info.si_addr = (void __user *)(bkpt->trigger);
  351. force_sig_info(SIGTRAP, &info, current);
  352. }
  353. /*
  354. * Set ptrace breakpoint pointers to zero for this task.
  355. * This is required in order to prevent child processes from unregistering
  356. * breakpoints held by their parent.
  357. */
  358. void clear_ptrace_hw_breakpoint(struct task_struct *tsk)
  359. {
  360. memset(tsk->thread.debug.hbp, 0, sizeof(tsk->thread.debug.hbp));
  361. }
  362. /*
  363. * Unregister breakpoints from this task and reset the pointers in
  364. * the thread_struct.
  365. */
  366. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  367. {
  368. int i;
  369. struct thread_struct *t = &tsk->thread;
  370. for (i = 0; i < ARM_MAX_HBP_SLOTS; i++) {
  371. if (t->debug.hbp[i]) {
  372. unregister_hw_breakpoint(t->debug.hbp[i]);
  373. t->debug.hbp[i] = NULL;
  374. }
  375. }
  376. }
  377. static u32 ptrace_get_hbp_resource_info(void)
  378. {
  379. u8 num_brps, num_wrps, debug_arch, wp_len;
  380. u32 reg = 0;
  381. num_brps = hw_breakpoint_slots(TYPE_INST);
  382. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  383. debug_arch = arch_get_debug_arch();
  384. wp_len = arch_get_max_wp_len();
  385. reg |= debug_arch;
  386. reg <<= 8;
  387. reg |= wp_len;
  388. reg <<= 8;
  389. reg |= num_wrps;
  390. reg <<= 8;
  391. reg |= num_brps;
  392. return reg;
  393. }
  394. static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type)
  395. {
  396. struct perf_event_attr attr;
  397. ptrace_breakpoint_init(&attr);
  398. /* Initialise fields to sane defaults. */
  399. attr.bp_addr = 0;
  400. attr.bp_len = HW_BREAKPOINT_LEN_4;
  401. attr.bp_type = type;
  402. attr.disabled = 1;
  403. return register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL,
  404. tsk);
  405. }
  406. static int ptrace_gethbpregs(struct task_struct *tsk, long num,
  407. unsigned long __user *data)
  408. {
  409. u32 reg;
  410. int idx, ret = 0;
  411. struct perf_event *bp;
  412. struct arch_hw_breakpoint_ctrl arch_ctrl;
  413. if (num == 0) {
  414. reg = ptrace_get_hbp_resource_info();
  415. } else {
  416. idx = ptrace_hbp_num_to_idx(num);
  417. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  418. ret = -EINVAL;
  419. goto out;
  420. }
  421. bp = tsk->thread.debug.hbp[idx];
  422. if (!bp) {
  423. reg = 0;
  424. goto put;
  425. }
  426. arch_ctrl = counter_arch_bp(bp)->ctrl;
  427. /*
  428. * Fix up the len because we may have adjusted it
  429. * to compensate for an unaligned address.
  430. */
  431. while (!(arch_ctrl.len & 0x1))
  432. arch_ctrl.len >>= 1;
  433. if (num & 0x1)
  434. reg = bp->attr.bp_addr;
  435. else
  436. reg = encode_ctrl_reg(arch_ctrl);
  437. }
  438. put:
  439. if (put_user(reg, data))
  440. ret = -EFAULT;
  441. out:
  442. return ret;
  443. }
  444. static int ptrace_sethbpregs(struct task_struct *tsk, long num,
  445. unsigned long __user *data)
  446. {
  447. int idx, gen_len, gen_type, implied_type, ret = 0;
  448. u32 user_val;
  449. struct perf_event *bp;
  450. struct arch_hw_breakpoint_ctrl ctrl;
  451. struct perf_event_attr attr;
  452. if (num == 0)
  453. goto out;
  454. else if (num < 0)
  455. implied_type = HW_BREAKPOINT_RW;
  456. else
  457. implied_type = HW_BREAKPOINT_X;
  458. idx = ptrace_hbp_num_to_idx(num);
  459. if (idx < 0 || idx >= ARM_MAX_HBP_SLOTS) {
  460. ret = -EINVAL;
  461. goto out;
  462. }
  463. if (get_user(user_val, data)) {
  464. ret = -EFAULT;
  465. goto out;
  466. }
  467. bp = tsk->thread.debug.hbp[idx];
  468. if (!bp) {
  469. bp = ptrace_hbp_create(tsk, implied_type);
  470. if (IS_ERR(bp)) {
  471. ret = PTR_ERR(bp);
  472. goto out;
  473. }
  474. tsk->thread.debug.hbp[idx] = bp;
  475. }
  476. attr = bp->attr;
  477. if (num & 0x1) {
  478. /* Address */
  479. attr.bp_addr = user_val;
  480. } else {
  481. /* Control */
  482. decode_ctrl_reg(user_val, &ctrl);
  483. ret = arch_bp_generic_fields(ctrl, &gen_len, &gen_type);
  484. if (ret)
  485. goto out;
  486. if ((gen_type & implied_type) != gen_type) {
  487. ret = -EINVAL;
  488. goto out;
  489. }
  490. attr.bp_len = gen_len;
  491. attr.bp_type = gen_type;
  492. attr.disabled = !ctrl.enabled;
  493. }
  494. ret = modify_user_hw_breakpoint(bp, &attr);
  495. out:
  496. return ret;
  497. }
  498. #endif
  499. /* regset get/set implementations */
  500. static int gpr_get(struct task_struct *target,
  501. const struct user_regset *regset,
  502. unsigned int pos, unsigned int count,
  503. void *kbuf, void __user *ubuf)
  504. {
  505. struct pt_regs *regs = task_pt_regs(target);
  506. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  507. regs,
  508. 0, sizeof(*regs));
  509. }
  510. static int gpr_set(struct task_struct *target,
  511. const struct user_regset *regset,
  512. unsigned int pos, unsigned int count,
  513. const void *kbuf, const void __user *ubuf)
  514. {
  515. int ret;
  516. struct pt_regs newregs;
  517. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  518. &newregs,
  519. 0, sizeof(newregs));
  520. if (ret)
  521. return ret;
  522. if (!valid_user_regs(&newregs))
  523. return -EINVAL;
  524. *task_pt_regs(target) = newregs;
  525. return 0;
  526. }
  527. static int fpa_get(struct task_struct *target,
  528. const struct user_regset *regset,
  529. unsigned int pos, unsigned int count,
  530. void *kbuf, void __user *ubuf)
  531. {
  532. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  533. &task_thread_info(target)->fpstate,
  534. 0, sizeof(struct user_fp));
  535. }
  536. static int fpa_set(struct task_struct *target,
  537. const struct user_regset *regset,
  538. unsigned int pos, unsigned int count,
  539. const void *kbuf, const void __user *ubuf)
  540. {
  541. struct thread_info *thread = task_thread_info(target);
  542. thread->used_cp[1] = thread->used_cp[2] = 1;
  543. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  544. &thread->fpstate,
  545. 0, sizeof(struct user_fp));
  546. }
  547. #ifdef CONFIG_VFP
  548. /*
  549. * VFP register get/set implementations.
  550. *
  551. * With respect to the kernel, struct user_fp is divided into three chunks:
  552. * 16 or 32 real VFP registers (d0-d15 or d0-31)
  553. * These are transferred to/from the real registers in the task's
  554. * vfp_hard_struct. The number of registers depends on the kernel
  555. * configuration.
  556. *
  557. * 16 or 0 fake VFP registers (d16-d31 or empty)
  558. * i.e., the user_vfp structure has space for 32 registers even if
  559. * the kernel doesn't have them all.
  560. *
  561. * vfp_get() reads this chunk as zero where applicable
  562. * vfp_set() ignores this chunk
  563. *
  564. * 1 word for the FPSCR
  565. *
  566. * The bounds-checking logic built into user_regset_copyout and friends
  567. * means that we can make a simple sequence of calls to map the relevant data
  568. * to/from the specified slice of the user regset structure.
  569. */
  570. static int vfp_get(struct task_struct *target,
  571. const struct user_regset *regset,
  572. unsigned int pos, unsigned int count,
  573. void *kbuf, void __user *ubuf)
  574. {
  575. int ret;
  576. struct thread_info *thread = task_thread_info(target);
  577. struct vfp_hard_struct const *vfp = &thread->vfpstate.hard;
  578. const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
  579. const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
  580. vfp_sync_hwstate(thread);
  581. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  582. &vfp->fpregs,
  583. user_fpregs_offset,
  584. user_fpregs_offset + sizeof(vfp->fpregs));
  585. if (ret)
  586. return ret;
  587. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  588. user_fpregs_offset + sizeof(vfp->fpregs),
  589. user_fpscr_offset);
  590. if (ret)
  591. return ret;
  592. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  593. &vfp->fpscr,
  594. user_fpscr_offset,
  595. user_fpscr_offset + sizeof(vfp->fpscr));
  596. }
  597. /*
  598. * For vfp_set() a read-modify-write is done on the VFP registers,
  599. * in order to avoid writing back a half-modified set of registers on
  600. * failure.
  601. */
  602. static int vfp_set(struct task_struct *target,
  603. const struct user_regset *regset,
  604. unsigned int pos, unsigned int count,
  605. const void *kbuf, const void __user *ubuf)
  606. {
  607. int ret;
  608. struct thread_info *thread = task_thread_info(target);
  609. struct vfp_hard_struct new_vfp;
  610. const size_t user_fpregs_offset = offsetof(struct user_vfp, fpregs);
  611. const size_t user_fpscr_offset = offsetof(struct user_vfp, fpscr);
  612. vfp_sync_hwstate(thread);
  613. new_vfp = thread->vfpstate.hard;
  614. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  615. &new_vfp.fpregs,
  616. user_fpregs_offset,
  617. user_fpregs_offset + sizeof(new_vfp.fpregs));
  618. if (ret)
  619. return ret;
  620. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  621. user_fpregs_offset + sizeof(new_vfp.fpregs),
  622. user_fpscr_offset);
  623. if (ret)
  624. return ret;
  625. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  626. &new_vfp.fpscr,
  627. user_fpscr_offset,
  628. user_fpscr_offset + sizeof(new_vfp.fpscr));
  629. if (ret)
  630. return ret;
  631. vfp_flush_hwstate(thread);
  632. thread->vfpstate.hard = new_vfp;
  633. return 0;
  634. }
  635. #endif /* CONFIG_VFP */
  636. enum arm_regset {
  637. REGSET_GPR,
  638. REGSET_FPR,
  639. #ifdef CONFIG_VFP
  640. REGSET_VFP,
  641. #endif
  642. };
  643. static const struct user_regset arm_regsets[] = {
  644. [REGSET_GPR] = {
  645. .core_note_type = NT_PRSTATUS,
  646. .n = ELF_NGREG,
  647. .size = sizeof(u32),
  648. .align = sizeof(u32),
  649. .get = gpr_get,
  650. .set = gpr_set
  651. },
  652. [REGSET_FPR] = {
  653. /*
  654. * For the FPA regs in fpstate, the real fields are a mixture
  655. * of sizes, so pretend that the registers are word-sized:
  656. */
  657. .core_note_type = NT_PRFPREG,
  658. .n = sizeof(struct user_fp) / sizeof(u32),
  659. .size = sizeof(u32),
  660. .align = sizeof(u32),
  661. .get = fpa_get,
  662. .set = fpa_set
  663. },
  664. #ifdef CONFIG_VFP
  665. [REGSET_VFP] = {
  666. /*
  667. * Pretend that the VFP regs are word-sized, since the FPSCR is
  668. * a single word dangling at the end of struct user_vfp:
  669. */
  670. .core_note_type = NT_ARM_VFP,
  671. .n = ARM_VFPREGS_SIZE / sizeof(u32),
  672. .size = sizeof(u32),
  673. .align = sizeof(u32),
  674. .get = vfp_get,
  675. .set = vfp_set
  676. },
  677. #endif /* CONFIG_VFP */
  678. };
  679. static const struct user_regset_view user_arm_view = {
  680. .name = "arm", .e_machine = ELF_ARCH, .ei_osabi = ELF_OSABI,
  681. .regsets = arm_regsets, .n = ARRAY_SIZE(arm_regsets)
  682. };
  683. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  684. {
  685. return &user_arm_view;
  686. }
  687. long arch_ptrace(struct task_struct *child, long request,
  688. unsigned long addr, unsigned long data)
  689. {
  690. int ret;
  691. unsigned long __user *datap = (unsigned long __user *) data;
  692. switch (request) {
  693. case PTRACE_PEEKUSR:
  694. ret = ptrace_read_user(child, addr, datap);
  695. break;
  696. case PTRACE_POKEUSR:
  697. ret = ptrace_write_user(child, addr, data);
  698. break;
  699. case PTRACE_GETREGS:
  700. ret = copy_regset_to_user(child,
  701. &user_arm_view, REGSET_GPR,
  702. 0, sizeof(struct pt_regs),
  703. datap);
  704. break;
  705. case PTRACE_SETREGS:
  706. ret = copy_regset_from_user(child,
  707. &user_arm_view, REGSET_GPR,
  708. 0, sizeof(struct pt_regs),
  709. datap);
  710. break;
  711. case PTRACE_GETFPREGS:
  712. ret = copy_regset_to_user(child,
  713. &user_arm_view, REGSET_FPR,
  714. 0, sizeof(union fp_state),
  715. datap);
  716. break;
  717. case PTRACE_SETFPREGS:
  718. ret = copy_regset_from_user(child,
  719. &user_arm_view, REGSET_FPR,
  720. 0, sizeof(union fp_state),
  721. datap);
  722. break;
  723. #ifdef CONFIG_IWMMXT
  724. case PTRACE_GETWMMXREGS:
  725. ret = ptrace_getwmmxregs(child, datap);
  726. break;
  727. case PTRACE_SETWMMXREGS:
  728. ret = ptrace_setwmmxregs(child, datap);
  729. break;
  730. #endif
  731. case PTRACE_GET_THREAD_AREA:
  732. ret = put_user(task_thread_info(child)->tp_value,
  733. datap);
  734. break;
  735. case PTRACE_SET_SYSCALL:
  736. task_thread_info(child)->syscall = data;
  737. ret = 0;
  738. break;
  739. #ifdef CONFIG_CRUNCH
  740. case PTRACE_GETCRUNCHREGS:
  741. ret = ptrace_getcrunchregs(child, datap);
  742. break;
  743. case PTRACE_SETCRUNCHREGS:
  744. ret = ptrace_setcrunchregs(child, datap);
  745. break;
  746. #endif
  747. #ifdef CONFIG_VFP
  748. case PTRACE_GETVFPREGS:
  749. ret = copy_regset_to_user(child,
  750. &user_arm_view, REGSET_VFP,
  751. 0, ARM_VFPREGS_SIZE,
  752. datap);
  753. break;
  754. case PTRACE_SETVFPREGS:
  755. ret = copy_regset_from_user(child,
  756. &user_arm_view, REGSET_VFP,
  757. 0, ARM_VFPREGS_SIZE,
  758. datap);
  759. break;
  760. #endif
  761. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  762. case PTRACE_GETHBPREGS:
  763. if (ptrace_get_breakpoints(child) < 0)
  764. return -ESRCH;
  765. ret = ptrace_gethbpregs(child, addr,
  766. (unsigned long __user *)data);
  767. ptrace_put_breakpoints(child);
  768. break;
  769. case PTRACE_SETHBPREGS:
  770. if (ptrace_get_breakpoints(child) < 0)
  771. return -ESRCH;
  772. ret = ptrace_sethbpregs(child, addr,
  773. (unsigned long __user *)data);
  774. ptrace_put_breakpoints(child);
  775. break;
  776. #endif
  777. default:
  778. ret = ptrace_request(child, request, addr, data);
  779. break;
  780. }
  781. return ret;
  782. }
  783. #ifdef __ARMEB__
  784. #define AUDIT_ARCH_NR AUDIT_ARCH_ARMEB
  785. #else
  786. #define AUDIT_ARCH_NR AUDIT_ARCH_ARM
  787. #endif
  788. asmlinkage int syscall_trace(int why, struct pt_regs *regs, int scno)
  789. {
  790. unsigned long ip;
  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. if (!ip)
  798. audit_syscall_exit(regs);
  799. else
  800. audit_syscall_entry(AUDIT_ARCH_NR, scno, regs->ARM_r0,
  801. regs->ARM_r1, regs->ARM_r2, regs->ARM_r3);
  802. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  803. return scno;
  804. if (!(current->ptrace & PT_PTRACED))
  805. return scno;
  806. current_thread_info()->syscall = scno;
  807. /* the 0x80 provides a way for the tracing parent to distinguish
  808. between a syscall stop and SIGTRAP delivery */
  809. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  810. ? 0x80 : 0));
  811. /*
  812. * this isn't the same as continuing with a signal, but it will do
  813. * for normal use. strace only continues with a signal if the
  814. * stopping signal is not SIGTRAP. -brl
  815. */
  816. if (current->exit_code) {
  817. send_sig(current->exit_code, current, 1);
  818. current->exit_code = 0;
  819. }
  820. regs->ARM_ip = ip;
  821. return current_thread_info()->syscall;
  822. }