ptrace.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /*
  2. * Based on 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. * Copyright (C) 2012 ARM Ltd.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <linux/audit.h>
  22. #include <linux/compat.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/mm.h>
  26. #include <linux/smp.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/user.h>
  29. #include <linux/security.h>
  30. #include <linux/init.h>
  31. #include <linux/signal.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/perf_event.h>
  34. #include <linux/hw_breakpoint.h>
  35. #include <linux/regset.h>
  36. #include <linux/tracehook.h>
  37. #include <linux/elf.h>
  38. #include <asm/compat.h>
  39. #include <asm/debug-monitors.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/syscall.h>
  42. #include <asm/traps.h>
  43. #include <asm/system_misc.h>
  44. #define CREATE_TRACE_POINTS
  45. #include <trace/events/syscalls.h>
  46. /*
  47. * TODO: does not yet catch signals sent when the child dies.
  48. * in exit.c or in signal.c.
  49. */
  50. /*
  51. * Called by kernel/ptrace.c when detaching..
  52. */
  53. void ptrace_disable(struct task_struct *child)
  54. {
  55. }
  56. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  57. /*
  58. * Handle hitting a HW-breakpoint.
  59. */
  60. static void ptrace_hbptriggered(struct perf_event *bp,
  61. struct perf_sample_data *data,
  62. struct pt_regs *regs)
  63. {
  64. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  65. siginfo_t info = {
  66. .si_signo = SIGTRAP,
  67. .si_errno = 0,
  68. .si_code = TRAP_HWBKPT,
  69. .si_addr = (void __user *)(bkpt->trigger),
  70. };
  71. #ifdef CONFIG_COMPAT
  72. int i;
  73. if (!is_compat_task())
  74. goto send_sig;
  75. for (i = 0; i < ARM_MAX_BRP; ++i) {
  76. if (current->thread.debug.hbp_break[i] == bp) {
  77. info.si_errno = (i << 1) + 1;
  78. break;
  79. }
  80. }
  81. for (i = ARM_MAX_BRP; i < ARM_MAX_HBP_SLOTS && !bp; ++i) {
  82. if (current->thread.debug.hbp_watch[i] == bp) {
  83. info.si_errno = -((i << 1) + 1);
  84. break;
  85. }
  86. }
  87. send_sig:
  88. #endif
  89. force_sig_info(SIGTRAP, &info, current);
  90. }
  91. /*
  92. * Unregister breakpoints from this task and reset the pointers in
  93. * the thread_struct.
  94. */
  95. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  96. {
  97. int i;
  98. struct thread_struct *t = &tsk->thread;
  99. for (i = 0; i < ARM_MAX_BRP; i++) {
  100. if (t->debug.hbp_break[i]) {
  101. unregister_hw_breakpoint(t->debug.hbp_break[i]);
  102. t->debug.hbp_break[i] = NULL;
  103. }
  104. }
  105. for (i = 0; i < ARM_MAX_WRP; i++) {
  106. if (t->debug.hbp_watch[i]) {
  107. unregister_hw_breakpoint(t->debug.hbp_watch[i]);
  108. t->debug.hbp_watch[i] = NULL;
  109. }
  110. }
  111. }
  112. void ptrace_hw_copy_thread(struct task_struct *tsk)
  113. {
  114. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  115. }
  116. static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
  117. struct task_struct *tsk,
  118. unsigned long idx)
  119. {
  120. struct perf_event *bp = ERR_PTR(-EINVAL);
  121. switch (note_type) {
  122. case NT_ARM_HW_BREAK:
  123. if (idx < ARM_MAX_BRP)
  124. bp = tsk->thread.debug.hbp_break[idx];
  125. break;
  126. case NT_ARM_HW_WATCH:
  127. if (idx < ARM_MAX_WRP)
  128. bp = tsk->thread.debug.hbp_watch[idx];
  129. break;
  130. }
  131. return bp;
  132. }
  133. static int ptrace_hbp_set_event(unsigned int note_type,
  134. struct task_struct *tsk,
  135. unsigned long idx,
  136. struct perf_event *bp)
  137. {
  138. int err = -EINVAL;
  139. switch (note_type) {
  140. case NT_ARM_HW_BREAK:
  141. if (idx < ARM_MAX_BRP) {
  142. tsk->thread.debug.hbp_break[idx] = bp;
  143. err = 0;
  144. }
  145. break;
  146. case NT_ARM_HW_WATCH:
  147. if (idx < ARM_MAX_WRP) {
  148. tsk->thread.debug.hbp_watch[idx] = bp;
  149. err = 0;
  150. }
  151. break;
  152. }
  153. return err;
  154. }
  155. static struct perf_event *ptrace_hbp_create(unsigned int note_type,
  156. struct task_struct *tsk,
  157. unsigned long idx)
  158. {
  159. struct perf_event *bp;
  160. struct perf_event_attr attr;
  161. int err, type;
  162. switch (note_type) {
  163. case NT_ARM_HW_BREAK:
  164. type = HW_BREAKPOINT_X;
  165. break;
  166. case NT_ARM_HW_WATCH:
  167. type = HW_BREAKPOINT_RW;
  168. break;
  169. default:
  170. return ERR_PTR(-EINVAL);
  171. }
  172. ptrace_breakpoint_init(&attr);
  173. /*
  174. * Initialise fields to sane defaults
  175. * (i.e. values that will pass validation).
  176. */
  177. attr.bp_addr = 0;
  178. attr.bp_len = HW_BREAKPOINT_LEN_4;
  179. attr.bp_type = type;
  180. attr.disabled = 1;
  181. bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
  182. if (IS_ERR(bp))
  183. return bp;
  184. err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
  185. if (err)
  186. return ERR_PTR(err);
  187. return bp;
  188. }
  189. static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
  190. struct arch_hw_breakpoint_ctrl ctrl,
  191. struct perf_event_attr *attr)
  192. {
  193. int err, len, type, disabled = !ctrl.enabled;
  194. attr->disabled = disabled;
  195. if (disabled)
  196. return 0;
  197. err = arch_bp_generic_fields(ctrl, &len, &type);
  198. if (err)
  199. return err;
  200. switch (note_type) {
  201. case NT_ARM_HW_BREAK:
  202. if ((type & HW_BREAKPOINT_X) != type)
  203. return -EINVAL;
  204. break;
  205. case NT_ARM_HW_WATCH:
  206. if ((type & HW_BREAKPOINT_RW) != type)
  207. return -EINVAL;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. attr->bp_len = len;
  213. attr->bp_type = type;
  214. return 0;
  215. }
  216. static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
  217. {
  218. u8 num;
  219. u32 reg = 0;
  220. switch (note_type) {
  221. case NT_ARM_HW_BREAK:
  222. num = hw_breakpoint_slots(TYPE_INST);
  223. break;
  224. case NT_ARM_HW_WATCH:
  225. num = hw_breakpoint_slots(TYPE_DATA);
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. reg |= debug_monitors_arch();
  231. reg <<= 8;
  232. reg |= num;
  233. *info = reg;
  234. return 0;
  235. }
  236. static int ptrace_hbp_get_ctrl(unsigned int note_type,
  237. struct task_struct *tsk,
  238. unsigned long idx,
  239. u32 *ctrl)
  240. {
  241. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  242. if (IS_ERR(bp))
  243. return PTR_ERR(bp);
  244. *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
  245. return 0;
  246. }
  247. static int ptrace_hbp_get_addr(unsigned int note_type,
  248. struct task_struct *tsk,
  249. unsigned long idx,
  250. u64 *addr)
  251. {
  252. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  253. if (IS_ERR(bp))
  254. return PTR_ERR(bp);
  255. *addr = bp ? bp->attr.bp_addr : 0;
  256. return 0;
  257. }
  258. static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
  259. struct task_struct *tsk,
  260. unsigned long idx)
  261. {
  262. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  263. if (!bp)
  264. bp = ptrace_hbp_create(note_type, tsk, idx);
  265. return bp;
  266. }
  267. static int ptrace_hbp_set_ctrl(unsigned int note_type,
  268. struct task_struct *tsk,
  269. unsigned long idx,
  270. u32 uctrl)
  271. {
  272. int err;
  273. struct perf_event *bp;
  274. struct perf_event_attr attr;
  275. struct arch_hw_breakpoint_ctrl ctrl;
  276. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  277. if (IS_ERR(bp)) {
  278. err = PTR_ERR(bp);
  279. return err;
  280. }
  281. attr = bp->attr;
  282. decode_ctrl_reg(uctrl, &ctrl);
  283. err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
  284. if (err)
  285. return err;
  286. return modify_user_hw_breakpoint(bp, &attr);
  287. }
  288. static int ptrace_hbp_set_addr(unsigned int note_type,
  289. struct task_struct *tsk,
  290. unsigned long idx,
  291. u64 addr)
  292. {
  293. int err;
  294. struct perf_event *bp;
  295. struct perf_event_attr attr;
  296. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  297. if (IS_ERR(bp)) {
  298. err = PTR_ERR(bp);
  299. return err;
  300. }
  301. attr = bp->attr;
  302. attr.bp_addr = addr;
  303. err = modify_user_hw_breakpoint(bp, &attr);
  304. return err;
  305. }
  306. #define PTRACE_HBP_ADDR_SZ sizeof(u64)
  307. #define PTRACE_HBP_CTRL_SZ sizeof(u32)
  308. #define PTRACE_HBP_PAD_SZ sizeof(u32)
  309. static int hw_break_get(struct task_struct *target,
  310. const struct user_regset *regset,
  311. unsigned int pos, unsigned int count,
  312. void *kbuf, void __user *ubuf)
  313. {
  314. unsigned int note_type = regset->core_note_type;
  315. int ret, idx = 0, offset, limit;
  316. u32 info, ctrl;
  317. u64 addr;
  318. /* Resource info */
  319. ret = ptrace_hbp_get_resource_info(note_type, &info);
  320. if (ret)
  321. return ret;
  322. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
  323. sizeof(info));
  324. if (ret)
  325. return ret;
  326. /* Pad */
  327. offset = offsetof(struct user_hwdebug_state, pad);
  328. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
  329. offset + PTRACE_HBP_PAD_SZ);
  330. if (ret)
  331. return ret;
  332. /* (address, ctrl) registers */
  333. offset = offsetof(struct user_hwdebug_state, dbg_regs);
  334. limit = regset->n * regset->size;
  335. while (count && offset < limit) {
  336. ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
  337. if (ret)
  338. return ret;
  339. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
  340. offset, offset + PTRACE_HBP_ADDR_SZ);
  341. if (ret)
  342. return ret;
  343. offset += PTRACE_HBP_ADDR_SZ;
  344. ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
  345. if (ret)
  346. return ret;
  347. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
  348. offset, offset + PTRACE_HBP_CTRL_SZ);
  349. if (ret)
  350. return ret;
  351. offset += PTRACE_HBP_CTRL_SZ;
  352. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  353. offset,
  354. offset + PTRACE_HBP_PAD_SZ);
  355. if (ret)
  356. return ret;
  357. offset += PTRACE_HBP_PAD_SZ;
  358. idx++;
  359. }
  360. return 0;
  361. }
  362. static int hw_break_set(struct task_struct *target,
  363. const struct user_regset *regset,
  364. unsigned int pos, unsigned int count,
  365. const void *kbuf, const void __user *ubuf)
  366. {
  367. unsigned int note_type = regset->core_note_type;
  368. int ret, idx = 0, offset, limit;
  369. u32 ctrl;
  370. u64 addr;
  371. /* Resource info and pad */
  372. offset = offsetof(struct user_hwdebug_state, dbg_regs);
  373. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
  374. if (ret)
  375. return ret;
  376. /* (address, ctrl) registers */
  377. limit = regset->n * regset->size;
  378. while (count && offset < limit) {
  379. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
  380. offset, offset + PTRACE_HBP_ADDR_SZ);
  381. if (ret)
  382. return ret;
  383. ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
  384. if (ret)
  385. return ret;
  386. offset += PTRACE_HBP_ADDR_SZ;
  387. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
  388. offset, offset + PTRACE_HBP_CTRL_SZ);
  389. if (ret)
  390. return ret;
  391. ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
  392. if (ret)
  393. return ret;
  394. offset += PTRACE_HBP_CTRL_SZ;
  395. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  396. offset,
  397. offset + PTRACE_HBP_PAD_SZ);
  398. if (ret)
  399. return ret;
  400. offset += PTRACE_HBP_PAD_SZ;
  401. idx++;
  402. }
  403. return 0;
  404. }
  405. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  406. static int gpr_get(struct task_struct *target,
  407. const struct user_regset *regset,
  408. unsigned int pos, unsigned int count,
  409. void *kbuf, void __user *ubuf)
  410. {
  411. struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
  412. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
  413. }
  414. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  415. unsigned int pos, unsigned int count,
  416. const void *kbuf, const void __user *ubuf)
  417. {
  418. int ret;
  419. struct user_pt_regs newregs;
  420. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
  421. if (ret)
  422. return ret;
  423. if (!valid_user_regs(&newregs))
  424. return -EINVAL;
  425. task_pt_regs(target)->user_regs = newregs;
  426. return 0;
  427. }
  428. /*
  429. * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
  430. */
  431. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  432. unsigned int pos, unsigned int count,
  433. void *kbuf, void __user *ubuf)
  434. {
  435. struct user_fpsimd_state *uregs;
  436. uregs = &target->thread.fpsimd_state.user_fpsimd;
  437. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
  438. }
  439. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  440. unsigned int pos, unsigned int count,
  441. const void *kbuf, const void __user *ubuf)
  442. {
  443. int ret;
  444. struct user_fpsimd_state newstate;
  445. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate, 0, -1);
  446. if (ret)
  447. return ret;
  448. target->thread.fpsimd_state.user_fpsimd = newstate;
  449. fpsimd_flush_task_state(target);
  450. return ret;
  451. }
  452. static int tls_get(struct task_struct *target, const struct user_regset *regset,
  453. unsigned int pos, unsigned int count,
  454. void *kbuf, void __user *ubuf)
  455. {
  456. unsigned long *tls = &target->thread.tp_value;
  457. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
  458. }
  459. static int tls_set(struct task_struct *target, const struct user_regset *regset,
  460. unsigned int pos, unsigned int count,
  461. const void *kbuf, const void __user *ubuf)
  462. {
  463. int ret;
  464. unsigned long tls;
  465. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  466. if (ret)
  467. return ret;
  468. target->thread.tp_value = tls;
  469. return ret;
  470. }
  471. enum aarch64_regset {
  472. REGSET_GPR,
  473. REGSET_FPR,
  474. REGSET_TLS,
  475. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  476. REGSET_HW_BREAK,
  477. REGSET_HW_WATCH,
  478. #endif
  479. };
  480. static const struct user_regset aarch64_regsets[] = {
  481. [REGSET_GPR] = {
  482. .core_note_type = NT_PRSTATUS,
  483. .n = sizeof(struct user_pt_regs) / sizeof(u64),
  484. .size = sizeof(u64),
  485. .align = sizeof(u64),
  486. .get = gpr_get,
  487. .set = gpr_set
  488. },
  489. [REGSET_FPR] = {
  490. .core_note_type = NT_PRFPREG,
  491. .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
  492. /*
  493. * We pretend we have 32-bit registers because the fpsr and
  494. * fpcr are 32-bits wide.
  495. */
  496. .size = sizeof(u32),
  497. .align = sizeof(u32),
  498. .get = fpr_get,
  499. .set = fpr_set
  500. },
  501. [REGSET_TLS] = {
  502. .core_note_type = NT_ARM_TLS,
  503. .n = 1,
  504. .size = sizeof(void *),
  505. .align = sizeof(void *),
  506. .get = tls_get,
  507. .set = tls_set,
  508. },
  509. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  510. [REGSET_HW_BREAK] = {
  511. .core_note_type = NT_ARM_HW_BREAK,
  512. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  513. .size = sizeof(u32),
  514. .align = sizeof(u32),
  515. .get = hw_break_get,
  516. .set = hw_break_set,
  517. },
  518. [REGSET_HW_WATCH] = {
  519. .core_note_type = NT_ARM_HW_WATCH,
  520. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  521. .size = sizeof(u32),
  522. .align = sizeof(u32),
  523. .get = hw_break_get,
  524. .set = hw_break_set,
  525. },
  526. #endif
  527. };
  528. static const struct user_regset_view user_aarch64_view = {
  529. .name = "aarch64", .e_machine = EM_AARCH64,
  530. .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
  531. };
  532. #ifdef CONFIG_COMPAT
  533. #include <linux/compat.h>
  534. enum compat_regset {
  535. REGSET_COMPAT_GPR,
  536. REGSET_COMPAT_VFP,
  537. };
  538. static int compat_gpr_get(struct task_struct *target,
  539. const struct user_regset *regset,
  540. unsigned int pos, unsigned int count,
  541. void *kbuf, void __user *ubuf)
  542. {
  543. int ret = 0;
  544. unsigned int i, start, num_regs;
  545. /* Calculate the number of AArch32 registers contained in count */
  546. num_regs = count / regset->size;
  547. /* Convert pos into an register number */
  548. start = pos / regset->size;
  549. if (start + num_regs > regset->n)
  550. return -EIO;
  551. for (i = 0; i < num_regs; ++i) {
  552. unsigned int idx = start + i;
  553. compat_ulong_t reg;
  554. switch (idx) {
  555. case 15:
  556. reg = task_pt_regs(target)->pc;
  557. break;
  558. case 16:
  559. reg = task_pt_regs(target)->pstate;
  560. break;
  561. case 17:
  562. reg = task_pt_regs(target)->orig_x0;
  563. break;
  564. default:
  565. reg = task_pt_regs(target)->regs[idx];
  566. }
  567. if (kbuf) {
  568. memcpy(kbuf, &reg, sizeof(reg));
  569. kbuf += sizeof(reg);
  570. } else {
  571. ret = copy_to_user(ubuf, &reg, sizeof(reg));
  572. if (ret)
  573. break;
  574. ubuf += sizeof(reg);
  575. }
  576. }
  577. return ret;
  578. }
  579. static int compat_gpr_set(struct task_struct *target,
  580. const struct user_regset *regset,
  581. unsigned int pos, unsigned int count,
  582. const void *kbuf, const void __user *ubuf)
  583. {
  584. struct pt_regs newregs;
  585. int ret = 0;
  586. unsigned int i, start, num_regs;
  587. /* Calculate the number of AArch32 registers contained in count */
  588. num_regs = count / regset->size;
  589. /* Convert pos into an register number */
  590. start = pos / regset->size;
  591. if (start + num_regs > regset->n)
  592. return -EIO;
  593. newregs = *task_pt_regs(target);
  594. for (i = 0; i < num_regs; ++i) {
  595. unsigned int idx = start + i;
  596. compat_ulong_t reg;
  597. if (kbuf) {
  598. memcpy(&reg, kbuf, sizeof(reg));
  599. kbuf += sizeof(reg);
  600. } else {
  601. ret = copy_from_user(&reg, ubuf, sizeof(reg));
  602. if (ret)
  603. return ret;
  604. ubuf += sizeof(reg);
  605. }
  606. switch (idx) {
  607. case 15:
  608. newregs.pc = reg;
  609. break;
  610. case 16:
  611. newregs.pstate = reg;
  612. break;
  613. case 17:
  614. newregs.orig_x0 = reg;
  615. break;
  616. default:
  617. newregs.regs[idx] = reg;
  618. }
  619. }
  620. if (valid_user_regs(&newregs.user_regs))
  621. *task_pt_regs(target) = newregs;
  622. else
  623. ret = -EINVAL;
  624. return ret;
  625. }
  626. static int compat_vfp_get(struct task_struct *target,
  627. const struct user_regset *regset,
  628. unsigned int pos, unsigned int count,
  629. void *kbuf, void __user *ubuf)
  630. {
  631. struct user_fpsimd_state *uregs;
  632. compat_ulong_t fpscr;
  633. int ret;
  634. uregs = &target->thread.fpsimd_state.user_fpsimd;
  635. /*
  636. * The VFP registers are packed into the fpsimd_state, so they all sit
  637. * nicely together for us. We just need to create the fpscr separately.
  638. */
  639. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
  640. VFP_STATE_SIZE - sizeof(compat_ulong_t));
  641. if (count && !ret) {
  642. fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
  643. (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
  644. ret = put_user(fpscr, (compat_ulong_t *)ubuf);
  645. }
  646. return ret;
  647. }
  648. static int compat_vfp_set(struct task_struct *target,
  649. const struct user_regset *regset,
  650. unsigned int pos, unsigned int count,
  651. const void *kbuf, const void __user *ubuf)
  652. {
  653. struct user_fpsimd_state *uregs;
  654. compat_ulong_t fpscr;
  655. int ret;
  656. if (pos + count > VFP_STATE_SIZE)
  657. return -EIO;
  658. uregs = &target->thread.fpsimd_state.user_fpsimd;
  659. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  660. VFP_STATE_SIZE - sizeof(compat_ulong_t));
  661. if (count && !ret) {
  662. ret = get_user(fpscr, (compat_ulong_t *)ubuf);
  663. uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  664. uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  665. }
  666. fpsimd_flush_task_state(target);
  667. return ret;
  668. }
  669. static const struct user_regset aarch32_regsets[] = {
  670. [REGSET_COMPAT_GPR] = {
  671. .core_note_type = NT_PRSTATUS,
  672. .n = COMPAT_ELF_NGREG,
  673. .size = sizeof(compat_elf_greg_t),
  674. .align = sizeof(compat_elf_greg_t),
  675. .get = compat_gpr_get,
  676. .set = compat_gpr_set
  677. },
  678. [REGSET_COMPAT_VFP] = {
  679. .core_note_type = NT_ARM_VFP,
  680. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  681. .size = sizeof(compat_ulong_t),
  682. .align = sizeof(compat_ulong_t),
  683. .get = compat_vfp_get,
  684. .set = compat_vfp_set
  685. },
  686. };
  687. static const struct user_regset_view user_aarch32_view = {
  688. .name = "aarch32", .e_machine = EM_ARM,
  689. .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
  690. };
  691. static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
  692. compat_ulong_t __user *ret)
  693. {
  694. compat_ulong_t tmp;
  695. if (off & 3)
  696. return -EIO;
  697. if (off == COMPAT_PT_TEXT_ADDR)
  698. tmp = tsk->mm->start_code;
  699. else if (off == COMPAT_PT_DATA_ADDR)
  700. tmp = tsk->mm->start_data;
  701. else if (off == COMPAT_PT_TEXT_END_ADDR)
  702. tmp = tsk->mm->end_code;
  703. else if (off < sizeof(compat_elf_gregset_t))
  704. return copy_regset_to_user(tsk, &user_aarch32_view,
  705. REGSET_COMPAT_GPR, off,
  706. sizeof(compat_ulong_t), ret);
  707. else if (off >= COMPAT_USER_SZ)
  708. return -EIO;
  709. else
  710. tmp = 0;
  711. return put_user(tmp, ret);
  712. }
  713. static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
  714. compat_ulong_t val)
  715. {
  716. int ret;
  717. mm_segment_t old_fs = get_fs();
  718. if (off & 3 || off >= COMPAT_USER_SZ)
  719. return -EIO;
  720. if (off >= sizeof(compat_elf_gregset_t))
  721. return 0;
  722. set_fs(KERNEL_DS);
  723. ret = copy_regset_from_user(tsk, &user_aarch32_view,
  724. REGSET_COMPAT_GPR, off,
  725. sizeof(compat_ulong_t),
  726. &val);
  727. set_fs(old_fs);
  728. return ret;
  729. }
  730. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  731. /*
  732. * Convert a virtual register number into an index for a thread_info
  733. * breakpoint array. Breakpoints are identified using positive numbers
  734. * whilst watchpoints are negative. The registers are laid out as pairs
  735. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  736. * Register 0 is reserved for describing resource information.
  737. */
  738. static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
  739. {
  740. return (abs(num) - 1) >> 1;
  741. }
  742. static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
  743. {
  744. u8 num_brps, num_wrps, debug_arch, wp_len;
  745. u32 reg = 0;
  746. num_brps = hw_breakpoint_slots(TYPE_INST);
  747. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  748. debug_arch = debug_monitors_arch();
  749. wp_len = 8;
  750. reg |= debug_arch;
  751. reg <<= 8;
  752. reg |= wp_len;
  753. reg <<= 8;
  754. reg |= num_wrps;
  755. reg <<= 8;
  756. reg |= num_brps;
  757. *kdata = reg;
  758. return 0;
  759. }
  760. static int compat_ptrace_hbp_get(unsigned int note_type,
  761. struct task_struct *tsk,
  762. compat_long_t num,
  763. u32 *kdata)
  764. {
  765. u64 addr = 0;
  766. u32 ctrl = 0;
  767. int err, idx = compat_ptrace_hbp_num_to_idx(num);;
  768. if (num & 1) {
  769. err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
  770. *kdata = (u32)addr;
  771. } else {
  772. err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
  773. *kdata = ctrl;
  774. }
  775. return err;
  776. }
  777. static int compat_ptrace_hbp_set(unsigned int note_type,
  778. struct task_struct *tsk,
  779. compat_long_t num,
  780. u32 *kdata)
  781. {
  782. u64 addr;
  783. u32 ctrl;
  784. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  785. if (num & 1) {
  786. addr = *kdata;
  787. err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
  788. } else {
  789. ctrl = *kdata;
  790. err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
  791. }
  792. return err;
  793. }
  794. static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
  795. compat_ulong_t __user *data)
  796. {
  797. int ret;
  798. u32 kdata;
  799. mm_segment_t old_fs = get_fs();
  800. set_fs(KERNEL_DS);
  801. /* Watchpoint */
  802. if (num < 0) {
  803. ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
  804. /* Resource info */
  805. } else if (num == 0) {
  806. ret = compat_ptrace_hbp_get_resource_info(&kdata);
  807. /* Breakpoint */
  808. } else {
  809. ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
  810. }
  811. set_fs(old_fs);
  812. if (!ret)
  813. ret = put_user(kdata, data);
  814. return ret;
  815. }
  816. static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
  817. compat_ulong_t __user *data)
  818. {
  819. int ret;
  820. u32 kdata = 0;
  821. mm_segment_t old_fs = get_fs();
  822. if (num == 0)
  823. return 0;
  824. ret = get_user(kdata, data);
  825. if (ret)
  826. return ret;
  827. set_fs(KERNEL_DS);
  828. if (num < 0)
  829. ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
  830. else
  831. ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
  832. set_fs(old_fs);
  833. return ret;
  834. }
  835. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  836. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  837. compat_ulong_t caddr, compat_ulong_t cdata)
  838. {
  839. unsigned long addr = caddr;
  840. unsigned long data = cdata;
  841. void __user *datap = compat_ptr(data);
  842. int ret;
  843. switch (request) {
  844. case PTRACE_PEEKUSR:
  845. ret = compat_ptrace_read_user(child, addr, datap);
  846. break;
  847. case PTRACE_POKEUSR:
  848. ret = compat_ptrace_write_user(child, addr, data);
  849. break;
  850. case COMPAT_PTRACE_GETREGS:
  851. ret = copy_regset_to_user(child,
  852. &user_aarch32_view,
  853. REGSET_COMPAT_GPR,
  854. 0, sizeof(compat_elf_gregset_t),
  855. datap);
  856. break;
  857. case COMPAT_PTRACE_SETREGS:
  858. ret = copy_regset_from_user(child,
  859. &user_aarch32_view,
  860. REGSET_COMPAT_GPR,
  861. 0, sizeof(compat_elf_gregset_t),
  862. datap);
  863. break;
  864. case COMPAT_PTRACE_GET_THREAD_AREA:
  865. ret = put_user((compat_ulong_t)child->thread.tp_value,
  866. (compat_ulong_t __user *)datap);
  867. break;
  868. case COMPAT_PTRACE_SET_SYSCALL:
  869. task_pt_regs(child)->syscallno = data;
  870. ret = 0;
  871. break;
  872. case COMPAT_PTRACE_GETVFPREGS:
  873. ret = copy_regset_to_user(child,
  874. &user_aarch32_view,
  875. REGSET_COMPAT_VFP,
  876. 0, VFP_STATE_SIZE,
  877. datap);
  878. break;
  879. case COMPAT_PTRACE_SETVFPREGS:
  880. ret = copy_regset_from_user(child,
  881. &user_aarch32_view,
  882. REGSET_COMPAT_VFP,
  883. 0, VFP_STATE_SIZE,
  884. datap);
  885. break;
  886. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  887. case COMPAT_PTRACE_GETHBPREGS:
  888. ret = compat_ptrace_gethbpregs(child, addr, datap);
  889. break;
  890. case COMPAT_PTRACE_SETHBPREGS:
  891. ret = compat_ptrace_sethbpregs(child, addr, datap);
  892. break;
  893. #endif
  894. default:
  895. ret = compat_ptrace_request(child, request, addr,
  896. data);
  897. break;
  898. }
  899. return ret;
  900. }
  901. #endif /* CONFIG_COMPAT */
  902. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  903. {
  904. #ifdef CONFIG_COMPAT
  905. if (is_compat_thread(task_thread_info(task)))
  906. return &user_aarch32_view;
  907. #endif
  908. return &user_aarch64_view;
  909. }
  910. long arch_ptrace(struct task_struct *child, long request,
  911. unsigned long addr, unsigned long data)
  912. {
  913. return ptrace_request(child, request, addr, data);
  914. }
  915. enum ptrace_syscall_dir {
  916. PTRACE_SYSCALL_ENTER = 0,
  917. PTRACE_SYSCALL_EXIT,
  918. };
  919. static void tracehook_report_syscall(struct pt_regs *regs,
  920. enum ptrace_syscall_dir dir)
  921. {
  922. int regno;
  923. unsigned long saved_reg;
  924. /*
  925. * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
  926. * used to denote syscall entry/exit:
  927. */
  928. regno = (is_compat_task() ? 12 : 7);
  929. saved_reg = regs->regs[regno];
  930. regs->regs[regno] = dir;
  931. if (dir == PTRACE_SYSCALL_EXIT)
  932. tracehook_report_syscall_exit(regs, 0);
  933. else if (tracehook_report_syscall_entry(regs))
  934. regs->syscallno = ~0UL;
  935. regs->regs[regno] = saved_reg;
  936. }
  937. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  938. {
  939. if (test_thread_flag(TIF_SYSCALL_TRACE))
  940. tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
  941. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  942. trace_sys_enter(regs, regs->syscallno);
  943. audit_syscall_entry(syscall_get_arch(), regs->syscallno,
  944. regs->orig_x0, regs->regs[1], regs->regs[2], regs->regs[3]);
  945. return regs->syscallno;
  946. }
  947. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  948. {
  949. audit_syscall_exit(regs);
  950. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  951. trace_sys_exit(regs, regs_return_value(regs));
  952. if (test_thread_flag(TIF_SYSCALL_TRACE))
  953. tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
  954. }