ptrace.c 24 KB

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