ptrace.c 22 KB

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