ptrace.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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. clear_siginfo(&info);
  190. info.si_signo = SIGTRAP;
  191. info.si_errno = 0;
  192. info.si_code = TRAP_BRKPT;
  193. info.si_addr = (void __user *)instruction_pointer(regs);
  194. force_sig_info(SIGTRAP, &info, tsk);
  195. }
  196. static int break_trap(struct pt_regs *regs, unsigned int instr)
  197. {
  198. ptrace_break(current, regs);
  199. return 0;
  200. }
  201. static struct undef_hook arm_break_hook = {
  202. .instr_mask = 0x0fffffff,
  203. .instr_val = 0x07f001f0,
  204. .cpsr_mask = PSR_T_BIT,
  205. .cpsr_val = 0,
  206. .fn = break_trap,
  207. };
  208. static struct undef_hook thumb_break_hook = {
  209. .instr_mask = 0xffff,
  210. .instr_val = 0xde01,
  211. .cpsr_mask = PSR_T_BIT,
  212. .cpsr_val = PSR_T_BIT,
  213. .fn = break_trap,
  214. };
  215. static struct undef_hook thumb2_break_hook = {
  216. .instr_mask = 0xffffffff,
  217. .instr_val = 0xf7f0a000,
  218. .cpsr_mask = PSR_T_BIT,
  219. .cpsr_val = PSR_T_BIT,
  220. .fn = break_trap,
  221. };
  222. static int __init ptrace_break_init(void)
  223. {
  224. register_undef_hook(&arm_break_hook);
  225. register_undef_hook(&thumb_break_hook);
  226. register_undef_hook(&thumb2_break_hook);
  227. return 0;
  228. }
  229. core_initcall(ptrace_break_init);
  230. /*
  231. * Read the word at offset "off" into the "struct user". We
  232. * actually access the pt_regs stored on the kernel stack.
  233. */
  234. static int ptrace_read_user(struct task_struct *tsk, unsigned long off,
  235. unsigned long __user *ret)
  236. {
  237. unsigned long tmp;
  238. if (off & 3)
  239. return -EIO;
  240. tmp = 0;
  241. if (off == PT_TEXT_ADDR)
  242. tmp = tsk->mm->start_code;
  243. else if (off == PT_DATA_ADDR)
  244. tmp = tsk->mm->start_data;
  245. else if (off == PT_TEXT_END_ADDR)
  246. tmp = tsk->mm->end_code;
  247. else if (off < sizeof(struct pt_regs))
  248. tmp = get_user_reg(tsk, off >> 2);
  249. else if (off >= sizeof(struct user))
  250. return -EIO;
  251. return put_user(tmp, ret);
  252. }
  253. /*
  254. * Write the word at offset "off" into "struct user". We
  255. * actually access the pt_regs stored on the kernel stack.
  256. */
  257. static int ptrace_write_user(struct task_struct *tsk, unsigned long off,
  258. unsigned long val)
  259. {
  260. if (off & 3 || off >= sizeof(struct user))
  261. return -EIO;
  262. if (off >= sizeof(struct pt_regs))
  263. return 0;
  264. return put_user_reg(tsk, off >> 2, val);
  265. }
  266. #ifdef CONFIG_IWMMXT
  267. /*
  268. * Get the child iWMMXt state.
  269. */
  270. static int ptrace_getwmmxregs(struct task_struct *tsk, void __user *ufp)
  271. {
  272. struct thread_info *thread = task_thread_info(tsk);
  273. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  274. return -ENODATA;
  275. iwmmxt_task_disable(thread); /* force it to ram */
  276. return copy_to_user(ufp, &thread->fpstate.iwmmxt, IWMMXT_SIZE)
  277. ? -EFAULT : 0;
  278. }
  279. /*
  280. * Set the child iWMMXt state.
  281. */
  282. static int ptrace_setwmmxregs(struct task_struct *tsk, void __user *ufp)
  283. {
  284. struct thread_info *thread = task_thread_info(tsk);
  285. if (!test_ti_thread_flag(thread, TIF_USING_IWMMXT))
  286. return -EACCES;
  287. iwmmxt_task_release(thread); /* force a reload */
  288. return copy_from_user(&thread->fpstate.iwmmxt, ufp, IWMMXT_SIZE)
  289. ? -EFAULT : 0;
  290. }
  291. #endif
  292. #ifdef CONFIG_CRUNCH
  293. /*
  294. * Get the child Crunch state.
  295. */
  296. static int ptrace_getcrunchregs(struct task_struct *tsk, void __user *ufp)
  297. {
  298. struct thread_info *thread = task_thread_info(tsk);
  299. crunch_task_disable(thread); /* force it to ram */
  300. return copy_to_user(ufp, &thread->crunchstate, CRUNCH_SIZE)
  301. ? -EFAULT : 0;
  302. }
  303. /*
  304. * Set the child Crunch state.
  305. */
  306. static int ptrace_setcrunchregs(struct task_struct *tsk, void __user *ufp)
  307. {
  308. struct thread_info *thread = task_thread_info(tsk);
  309. crunch_task_release(thread); /* force a reload */
  310. return copy_from_user(&thread->crunchstate, ufp, CRUNCH_SIZE)
  311. ? -EFAULT : 0;
  312. }
  313. #endif
  314. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  315. /*
  316. * Convert a virtual register number into an index for a thread_info
  317. * breakpoint array. Breakpoints are identified using positive numbers
  318. * whilst watchpoints are negative. The registers are laid out as pairs
  319. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  320. * Register 0 is reserved for describing resource information.
  321. */
  322. static int ptrace_hbp_num_to_idx(long num)
  323. {
  324. if (num < 0)
  325. num = (ARM_MAX_BRP << 1) - num;
  326. return (num - 1) >> 1;
  327. }
  328. /*
  329. * Returns the virtual register number for the address of the
  330. * breakpoint at index idx.
  331. */
  332. static long ptrace_hbp_idx_to_num(int idx)
  333. {
  334. long mid = ARM_MAX_BRP << 1;
  335. long num = (idx << 1) + 1;
  336. return num > mid ? mid - num : num;
  337. }
  338. /*
  339. * Handle hitting a HW-breakpoint.
  340. */
  341. static void ptrace_hbptriggered(struct perf_event *bp,
  342. struct perf_sample_data *data,
  343. struct pt_regs *regs)
  344. {
  345. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  346. long num;
  347. int i;
  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. force_sig_ptrace_errno_trap((int)num, (void __user *)(bkpt->trigger));
  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 = *task_pt_regs(target);
  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. thread->vfpstate.hard = new_vfp;
  633. vfp_flush_hwstate(thread);
  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[0],
  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. ret = ptrace_gethbpregs(child, addr,
  765. (unsigned long __user *)data);
  766. break;
  767. case PTRACE_SETHBPREGS:
  768. ret = ptrace_sethbpregs(child, addr,
  769. (unsigned long __user *)data);
  770. break;
  771. #endif
  772. default:
  773. ret = ptrace_request(child, request, addr, data);
  774. break;
  775. }
  776. return ret;
  777. }
  778. enum ptrace_syscall_dir {
  779. PTRACE_SYSCALL_ENTER = 0,
  780. PTRACE_SYSCALL_EXIT,
  781. };
  782. static void tracehook_report_syscall(struct pt_regs *regs,
  783. enum ptrace_syscall_dir dir)
  784. {
  785. unsigned long ip;
  786. /*
  787. * IP is used to denote syscall entry/exit:
  788. * IP = 0 -> entry, =1 -> exit
  789. */
  790. ip = regs->ARM_ip;
  791. regs->ARM_ip = dir;
  792. if (dir == PTRACE_SYSCALL_EXIT)
  793. tracehook_report_syscall_exit(regs, 0);
  794. else if (tracehook_report_syscall_entry(regs))
  795. current_thread_info()->syscall = -1;
  796. regs->ARM_ip = ip;
  797. }
  798. asmlinkage int syscall_trace_enter(struct pt_regs *regs, int scno)
  799. {
  800. current_thread_info()->syscall = scno;
  801. if (test_thread_flag(TIF_SYSCALL_TRACE))
  802. tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
  803. /* Do seccomp after ptrace; syscall may have changed. */
  804. #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  805. if (secure_computing(NULL) == -1)
  806. return -1;
  807. #else
  808. /* XXX: remove this once OABI gets fixed */
  809. secure_computing_strict(current_thread_info()->syscall);
  810. #endif
  811. /* Tracer or seccomp may have changed syscall. */
  812. scno = current_thread_info()->syscall;
  813. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  814. trace_sys_enter(regs, scno);
  815. audit_syscall_entry(scno, regs->ARM_r0, regs->ARM_r1, regs->ARM_r2,
  816. regs->ARM_r3);
  817. return scno;
  818. }
  819. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  820. {
  821. /*
  822. * Audit the syscall before anything else, as a debugger may
  823. * come in and change the current registers.
  824. */
  825. audit_syscall_exit(regs);
  826. /*
  827. * Note that we haven't updated the ->syscall field for the
  828. * current thread. This isn't a problem because it will have
  829. * been set on syscall entry and there hasn't been an opportunity
  830. * for a PTRACE_SET_SYSCALL since then.
  831. */
  832. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  833. trace_sys_exit(regs, regs_return_value(regs));
  834. if (test_thread_flag(TIF_SYSCALL_TRACE))
  835. tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
  836. }