ptrace.c 22 KB

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