ptrace.c 23 KB

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