ptrace.c 29 KB

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