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