ptrace.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  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 = 0; i < ARM_MAX_WRP; ++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. ret = -EFAULT;
  574. break;
  575. }
  576. ubuf += sizeof(reg);
  577. }
  578. }
  579. return ret;
  580. }
  581. static int compat_gpr_set(struct task_struct *target,
  582. const struct user_regset *regset,
  583. unsigned int pos, unsigned int count,
  584. const void *kbuf, const void __user *ubuf)
  585. {
  586. struct pt_regs newregs;
  587. int ret = 0;
  588. unsigned int i, start, num_regs;
  589. /* Calculate the number of AArch32 registers contained in count */
  590. num_regs = count / regset->size;
  591. /* Convert pos into an register number */
  592. start = pos / regset->size;
  593. if (start + num_regs > regset->n)
  594. return -EIO;
  595. newregs = *task_pt_regs(target);
  596. for (i = 0; i < num_regs; ++i) {
  597. unsigned int idx = start + i;
  598. compat_ulong_t reg;
  599. if (kbuf) {
  600. memcpy(&reg, kbuf, sizeof(reg));
  601. kbuf += sizeof(reg);
  602. } else {
  603. ret = copy_from_user(&reg, ubuf, sizeof(reg));
  604. if (ret) {
  605. ret = -EFAULT;
  606. break;
  607. }
  608. ubuf += sizeof(reg);
  609. }
  610. switch (idx) {
  611. case 15:
  612. newregs.pc = reg;
  613. break;
  614. case 16:
  615. newregs.pstate = reg;
  616. break;
  617. case 17:
  618. newregs.orig_x0 = reg;
  619. break;
  620. default:
  621. newregs.regs[idx] = reg;
  622. }
  623. }
  624. if (valid_user_regs(&newregs.user_regs))
  625. *task_pt_regs(target) = newregs;
  626. else
  627. ret = -EINVAL;
  628. return ret;
  629. }
  630. static int compat_vfp_get(struct task_struct *target,
  631. const struct user_regset *regset,
  632. unsigned int pos, unsigned int count,
  633. void *kbuf, void __user *ubuf)
  634. {
  635. struct user_fpsimd_state *uregs;
  636. compat_ulong_t fpscr;
  637. int ret;
  638. uregs = &target->thread.fpsimd_state.user_fpsimd;
  639. /*
  640. * The VFP registers are packed into the fpsimd_state, so they all sit
  641. * nicely together for us. We just need to create the fpscr separately.
  642. */
  643. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0,
  644. VFP_STATE_SIZE - sizeof(compat_ulong_t));
  645. if (count && !ret) {
  646. fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
  647. (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
  648. ret = put_user(fpscr, (compat_ulong_t *)ubuf);
  649. }
  650. return ret;
  651. }
  652. static int compat_vfp_set(struct task_struct *target,
  653. const struct user_regset *regset,
  654. unsigned int pos, unsigned int count,
  655. const void *kbuf, const void __user *ubuf)
  656. {
  657. struct user_fpsimd_state *uregs;
  658. compat_ulong_t fpscr;
  659. int ret;
  660. if (pos + count > VFP_STATE_SIZE)
  661. return -EIO;
  662. uregs = &target->thread.fpsimd_state.user_fpsimd;
  663. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  664. VFP_STATE_SIZE - sizeof(compat_ulong_t));
  665. if (count && !ret) {
  666. ret = get_user(fpscr, (compat_ulong_t *)ubuf);
  667. uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  668. uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  669. }
  670. fpsimd_flush_task_state(target);
  671. return ret;
  672. }
  673. static const struct user_regset aarch32_regsets[] = {
  674. [REGSET_COMPAT_GPR] = {
  675. .core_note_type = NT_PRSTATUS,
  676. .n = COMPAT_ELF_NGREG,
  677. .size = sizeof(compat_elf_greg_t),
  678. .align = sizeof(compat_elf_greg_t),
  679. .get = compat_gpr_get,
  680. .set = compat_gpr_set
  681. },
  682. [REGSET_COMPAT_VFP] = {
  683. .core_note_type = NT_ARM_VFP,
  684. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  685. .size = sizeof(compat_ulong_t),
  686. .align = sizeof(compat_ulong_t),
  687. .get = compat_vfp_get,
  688. .set = compat_vfp_set
  689. },
  690. };
  691. static const struct user_regset_view user_aarch32_view = {
  692. .name = "aarch32", .e_machine = EM_ARM,
  693. .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
  694. };
  695. static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
  696. compat_ulong_t __user *ret)
  697. {
  698. compat_ulong_t tmp;
  699. if (off & 3)
  700. return -EIO;
  701. if (off == COMPAT_PT_TEXT_ADDR)
  702. tmp = tsk->mm->start_code;
  703. else if (off == COMPAT_PT_DATA_ADDR)
  704. tmp = tsk->mm->start_data;
  705. else if (off == COMPAT_PT_TEXT_END_ADDR)
  706. tmp = tsk->mm->end_code;
  707. else if (off < sizeof(compat_elf_gregset_t))
  708. return copy_regset_to_user(tsk, &user_aarch32_view,
  709. REGSET_COMPAT_GPR, off,
  710. sizeof(compat_ulong_t), ret);
  711. else if (off >= COMPAT_USER_SZ)
  712. return -EIO;
  713. else
  714. tmp = 0;
  715. return put_user(tmp, ret);
  716. }
  717. static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
  718. compat_ulong_t val)
  719. {
  720. int ret;
  721. mm_segment_t old_fs = get_fs();
  722. if (off & 3 || off >= COMPAT_USER_SZ)
  723. return -EIO;
  724. if (off >= sizeof(compat_elf_gregset_t))
  725. return 0;
  726. set_fs(KERNEL_DS);
  727. ret = copy_regset_from_user(tsk, &user_aarch32_view,
  728. REGSET_COMPAT_GPR, off,
  729. sizeof(compat_ulong_t),
  730. &val);
  731. set_fs(old_fs);
  732. return ret;
  733. }
  734. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  735. /*
  736. * Convert a virtual register number into an index for a thread_info
  737. * breakpoint array. Breakpoints are identified using positive numbers
  738. * whilst watchpoints are negative. The registers are laid out as pairs
  739. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  740. * Register 0 is reserved for describing resource information.
  741. */
  742. static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
  743. {
  744. return (abs(num) - 1) >> 1;
  745. }
  746. static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
  747. {
  748. u8 num_brps, num_wrps, debug_arch, wp_len;
  749. u32 reg = 0;
  750. num_brps = hw_breakpoint_slots(TYPE_INST);
  751. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  752. debug_arch = debug_monitors_arch();
  753. wp_len = 8;
  754. reg |= debug_arch;
  755. reg <<= 8;
  756. reg |= wp_len;
  757. reg <<= 8;
  758. reg |= num_wrps;
  759. reg <<= 8;
  760. reg |= num_brps;
  761. *kdata = reg;
  762. return 0;
  763. }
  764. static int compat_ptrace_hbp_get(unsigned int note_type,
  765. struct task_struct *tsk,
  766. compat_long_t num,
  767. u32 *kdata)
  768. {
  769. u64 addr = 0;
  770. u32 ctrl = 0;
  771. int err, idx = compat_ptrace_hbp_num_to_idx(num);;
  772. if (num & 1) {
  773. err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
  774. *kdata = (u32)addr;
  775. } else {
  776. err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
  777. *kdata = ctrl;
  778. }
  779. return err;
  780. }
  781. static int compat_ptrace_hbp_set(unsigned int note_type,
  782. struct task_struct *tsk,
  783. compat_long_t num,
  784. u32 *kdata)
  785. {
  786. u64 addr;
  787. u32 ctrl;
  788. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  789. if (num & 1) {
  790. addr = *kdata;
  791. err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
  792. } else {
  793. ctrl = *kdata;
  794. err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
  795. }
  796. return err;
  797. }
  798. static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
  799. compat_ulong_t __user *data)
  800. {
  801. int ret;
  802. u32 kdata;
  803. mm_segment_t old_fs = get_fs();
  804. set_fs(KERNEL_DS);
  805. /* Watchpoint */
  806. if (num < 0) {
  807. ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
  808. /* Resource info */
  809. } else if (num == 0) {
  810. ret = compat_ptrace_hbp_get_resource_info(&kdata);
  811. /* Breakpoint */
  812. } else {
  813. ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
  814. }
  815. set_fs(old_fs);
  816. if (!ret)
  817. ret = put_user(kdata, data);
  818. return ret;
  819. }
  820. static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
  821. compat_ulong_t __user *data)
  822. {
  823. int ret;
  824. u32 kdata = 0;
  825. mm_segment_t old_fs = get_fs();
  826. if (num == 0)
  827. return 0;
  828. ret = get_user(kdata, data);
  829. if (ret)
  830. return ret;
  831. set_fs(KERNEL_DS);
  832. if (num < 0)
  833. ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
  834. else
  835. ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
  836. set_fs(old_fs);
  837. return ret;
  838. }
  839. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  840. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  841. compat_ulong_t caddr, compat_ulong_t cdata)
  842. {
  843. unsigned long addr = caddr;
  844. unsigned long data = cdata;
  845. void __user *datap = compat_ptr(data);
  846. int ret;
  847. switch (request) {
  848. case PTRACE_PEEKUSR:
  849. ret = compat_ptrace_read_user(child, addr, datap);
  850. break;
  851. case PTRACE_POKEUSR:
  852. ret = compat_ptrace_write_user(child, addr, data);
  853. break;
  854. case COMPAT_PTRACE_GETREGS:
  855. ret = copy_regset_to_user(child,
  856. &user_aarch32_view,
  857. REGSET_COMPAT_GPR,
  858. 0, sizeof(compat_elf_gregset_t),
  859. datap);
  860. break;
  861. case COMPAT_PTRACE_SETREGS:
  862. ret = copy_regset_from_user(child,
  863. &user_aarch32_view,
  864. REGSET_COMPAT_GPR,
  865. 0, sizeof(compat_elf_gregset_t),
  866. datap);
  867. break;
  868. case COMPAT_PTRACE_GET_THREAD_AREA:
  869. ret = put_user((compat_ulong_t)child->thread.tp_value,
  870. (compat_ulong_t __user *)datap);
  871. break;
  872. case COMPAT_PTRACE_SET_SYSCALL:
  873. task_pt_regs(child)->syscallno = data;
  874. ret = 0;
  875. break;
  876. case COMPAT_PTRACE_GETVFPREGS:
  877. ret = copy_regset_to_user(child,
  878. &user_aarch32_view,
  879. REGSET_COMPAT_VFP,
  880. 0, VFP_STATE_SIZE,
  881. datap);
  882. break;
  883. case COMPAT_PTRACE_SETVFPREGS:
  884. ret = copy_regset_from_user(child,
  885. &user_aarch32_view,
  886. REGSET_COMPAT_VFP,
  887. 0, VFP_STATE_SIZE,
  888. datap);
  889. break;
  890. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  891. case COMPAT_PTRACE_GETHBPREGS:
  892. ret = compat_ptrace_gethbpregs(child, addr, datap);
  893. break;
  894. case COMPAT_PTRACE_SETHBPREGS:
  895. ret = compat_ptrace_sethbpregs(child, addr, datap);
  896. break;
  897. #endif
  898. default:
  899. ret = compat_ptrace_request(child, request, addr,
  900. data);
  901. break;
  902. }
  903. return ret;
  904. }
  905. #endif /* CONFIG_COMPAT */
  906. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  907. {
  908. #ifdef CONFIG_COMPAT
  909. if (is_compat_thread(task_thread_info(task)))
  910. return &user_aarch32_view;
  911. #endif
  912. return &user_aarch64_view;
  913. }
  914. long arch_ptrace(struct task_struct *child, long request,
  915. unsigned long addr, unsigned long data)
  916. {
  917. return ptrace_request(child, request, addr, data);
  918. }
  919. enum ptrace_syscall_dir {
  920. PTRACE_SYSCALL_ENTER = 0,
  921. PTRACE_SYSCALL_EXIT,
  922. };
  923. static void tracehook_report_syscall(struct pt_regs *regs,
  924. enum ptrace_syscall_dir dir)
  925. {
  926. int regno;
  927. unsigned long saved_reg;
  928. /*
  929. * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
  930. * used to denote syscall entry/exit:
  931. */
  932. regno = (is_compat_task() ? 12 : 7);
  933. saved_reg = regs->regs[regno];
  934. regs->regs[regno] = dir;
  935. if (dir == PTRACE_SYSCALL_EXIT)
  936. tracehook_report_syscall_exit(regs, 0);
  937. else if (tracehook_report_syscall_entry(regs))
  938. regs->syscallno = ~0UL;
  939. regs->regs[regno] = saved_reg;
  940. }
  941. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  942. {
  943. if (test_thread_flag(TIF_SYSCALL_TRACE))
  944. tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
  945. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  946. trace_sys_enter(regs, regs->syscallno);
  947. audit_syscall_entry(syscall_get_arch(), regs->syscallno,
  948. regs->orig_x0, regs->regs[1], regs->regs[2], regs->regs[3]);
  949. return regs->syscallno;
  950. }
  951. asmlinkage void syscall_trace_exit(struct pt_regs *regs)
  952. {
  953. audit_syscall_exit(regs);
  954. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  955. trace_sys_exit(regs, regs_return_value(regs));
  956. if (test_thread_flag(TIF_SYSCALL_TRACE))
  957. tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
  958. }