ptrace.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  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/signal.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/mm.h>
  27. #include <linux/nospec.h>
  28. #include <linux/smp.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/user.h>
  31. #include <linux/seccomp.h>
  32. #include <linux/security.h>
  33. #include <linux/init.h>
  34. #include <linux/signal.h>
  35. #include <linux/string.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/perf_event.h>
  38. #include <linux/hw_breakpoint.h>
  39. #include <linux/regset.h>
  40. #include <linux/tracehook.h>
  41. #include <linux/elf.h>
  42. #include <asm/compat.h>
  43. #include <asm/cpufeature.h>
  44. #include <asm/debug-monitors.h>
  45. #include <asm/fpsimd.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/stacktrace.h>
  48. #include <asm/syscall.h>
  49. #include <asm/traps.h>
  50. #include <asm/system_misc.h>
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/syscalls.h>
  53. struct pt_regs_offset {
  54. const char *name;
  55. int offset;
  56. };
  57. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  58. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  59. #define GPR_OFFSET_NAME(r) \
  60. {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
  61. static const struct pt_regs_offset regoffset_table[] = {
  62. GPR_OFFSET_NAME(0),
  63. GPR_OFFSET_NAME(1),
  64. GPR_OFFSET_NAME(2),
  65. GPR_OFFSET_NAME(3),
  66. GPR_OFFSET_NAME(4),
  67. GPR_OFFSET_NAME(5),
  68. GPR_OFFSET_NAME(6),
  69. GPR_OFFSET_NAME(7),
  70. GPR_OFFSET_NAME(8),
  71. GPR_OFFSET_NAME(9),
  72. GPR_OFFSET_NAME(10),
  73. GPR_OFFSET_NAME(11),
  74. GPR_OFFSET_NAME(12),
  75. GPR_OFFSET_NAME(13),
  76. GPR_OFFSET_NAME(14),
  77. GPR_OFFSET_NAME(15),
  78. GPR_OFFSET_NAME(16),
  79. GPR_OFFSET_NAME(17),
  80. GPR_OFFSET_NAME(18),
  81. GPR_OFFSET_NAME(19),
  82. GPR_OFFSET_NAME(20),
  83. GPR_OFFSET_NAME(21),
  84. GPR_OFFSET_NAME(22),
  85. GPR_OFFSET_NAME(23),
  86. GPR_OFFSET_NAME(24),
  87. GPR_OFFSET_NAME(25),
  88. GPR_OFFSET_NAME(26),
  89. GPR_OFFSET_NAME(27),
  90. GPR_OFFSET_NAME(28),
  91. GPR_OFFSET_NAME(29),
  92. GPR_OFFSET_NAME(30),
  93. {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
  94. REG_OFFSET_NAME(sp),
  95. REG_OFFSET_NAME(pc),
  96. REG_OFFSET_NAME(pstate),
  97. REG_OFFSET_END,
  98. };
  99. /**
  100. * regs_query_register_offset() - query register offset from its name
  101. * @name: the name of a register
  102. *
  103. * regs_query_register_offset() returns the offset of a register in struct
  104. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  105. */
  106. int regs_query_register_offset(const char *name)
  107. {
  108. const struct pt_regs_offset *roff;
  109. for (roff = regoffset_table; roff->name != NULL; roff++)
  110. if (!strcmp(roff->name, name))
  111. return roff->offset;
  112. return -EINVAL;
  113. }
  114. /**
  115. * regs_within_kernel_stack() - check the address in the stack
  116. * @regs: pt_regs which contains kernel stack pointer.
  117. * @addr: address which is checked.
  118. *
  119. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  120. * If @addr is within the kernel stack, it returns true. If not, returns false.
  121. */
  122. static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  123. {
  124. return ((addr & ~(THREAD_SIZE - 1)) ==
  125. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
  126. on_irq_stack(addr, NULL);
  127. }
  128. /**
  129. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  130. * @regs: pt_regs which contains kernel stack pointer.
  131. * @n: stack entry number.
  132. *
  133. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  134. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  135. * this returns 0.
  136. */
  137. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  138. {
  139. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  140. addr += n;
  141. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  142. return *addr;
  143. else
  144. return 0;
  145. }
  146. /*
  147. * TODO: does not yet catch signals sent when the child dies.
  148. * in exit.c or in signal.c.
  149. */
  150. /*
  151. * Called by kernel/ptrace.c when detaching..
  152. */
  153. void ptrace_disable(struct task_struct *child)
  154. {
  155. /*
  156. * This would be better off in core code, but PTRACE_DETACH has
  157. * grown its fair share of arch-specific worts and changing it
  158. * is likely to cause regressions on obscure architectures.
  159. */
  160. user_disable_single_step(child);
  161. }
  162. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  163. /*
  164. * Handle hitting a HW-breakpoint.
  165. */
  166. static void ptrace_hbptriggered(struct perf_event *bp,
  167. struct perf_sample_data *data,
  168. struct pt_regs *regs)
  169. {
  170. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  171. siginfo_t info;
  172. clear_siginfo(&info);
  173. info.si_signo = SIGTRAP;
  174. info.si_errno = 0;
  175. info.si_code = TRAP_HWBKPT;
  176. info.si_addr = (void __user *)(bkpt->trigger);
  177. #ifdef CONFIG_COMPAT
  178. if (is_compat_task()) {
  179. int si_errno = 0;
  180. int i;
  181. for (i = 0; i < ARM_MAX_BRP; ++i) {
  182. if (current->thread.debug.hbp_break[i] == bp) {
  183. si_errno = (i << 1) + 1;
  184. break;
  185. }
  186. }
  187. for (i = 0; i < ARM_MAX_WRP; ++i) {
  188. if (current->thread.debug.hbp_watch[i] == bp) {
  189. si_errno = -((i << 1) + 1);
  190. break;
  191. }
  192. }
  193. force_sig_ptrace_errno_trap(si_errno, (void __user *)bkpt->trigger);
  194. }
  195. #endif
  196. arm64_force_sig_info(&info, "Hardware breakpoint trap (ptrace)", current);
  197. }
  198. /*
  199. * Unregister breakpoints from this task and reset the pointers in
  200. * the thread_struct.
  201. */
  202. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  203. {
  204. int i;
  205. struct thread_struct *t = &tsk->thread;
  206. for (i = 0; i < ARM_MAX_BRP; i++) {
  207. if (t->debug.hbp_break[i]) {
  208. unregister_hw_breakpoint(t->debug.hbp_break[i]);
  209. t->debug.hbp_break[i] = NULL;
  210. }
  211. }
  212. for (i = 0; i < ARM_MAX_WRP; i++) {
  213. if (t->debug.hbp_watch[i]) {
  214. unregister_hw_breakpoint(t->debug.hbp_watch[i]);
  215. t->debug.hbp_watch[i] = NULL;
  216. }
  217. }
  218. }
  219. void ptrace_hw_copy_thread(struct task_struct *tsk)
  220. {
  221. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  222. }
  223. static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
  224. struct task_struct *tsk,
  225. unsigned long idx)
  226. {
  227. struct perf_event *bp = ERR_PTR(-EINVAL);
  228. switch (note_type) {
  229. case NT_ARM_HW_BREAK:
  230. if (idx >= ARM_MAX_BRP)
  231. goto out;
  232. idx = array_index_nospec(idx, ARM_MAX_BRP);
  233. bp = tsk->thread.debug.hbp_break[idx];
  234. break;
  235. case NT_ARM_HW_WATCH:
  236. if (idx >= ARM_MAX_WRP)
  237. goto out;
  238. idx = array_index_nospec(idx, ARM_MAX_WRP);
  239. bp = tsk->thread.debug.hbp_watch[idx];
  240. break;
  241. }
  242. out:
  243. return bp;
  244. }
  245. static int ptrace_hbp_set_event(unsigned int note_type,
  246. struct task_struct *tsk,
  247. unsigned long idx,
  248. struct perf_event *bp)
  249. {
  250. int err = -EINVAL;
  251. switch (note_type) {
  252. case NT_ARM_HW_BREAK:
  253. if (idx >= ARM_MAX_BRP)
  254. goto out;
  255. idx = array_index_nospec(idx, ARM_MAX_BRP);
  256. tsk->thread.debug.hbp_break[idx] = bp;
  257. err = 0;
  258. break;
  259. case NT_ARM_HW_WATCH:
  260. if (idx >= ARM_MAX_WRP)
  261. goto out;
  262. idx = array_index_nospec(idx, ARM_MAX_WRP);
  263. tsk->thread.debug.hbp_watch[idx] = bp;
  264. err = 0;
  265. break;
  266. }
  267. out:
  268. return err;
  269. }
  270. static struct perf_event *ptrace_hbp_create(unsigned int note_type,
  271. struct task_struct *tsk,
  272. unsigned long idx)
  273. {
  274. struct perf_event *bp;
  275. struct perf_event_attr attr;
  276. int err, type;
  277. switch (note_type) {
  278. case NT_ARM_HW_BREAK:
  279. type = HW_BREAKPOINT_X;
  280. break;
  281. case NT_ARM_HW_WATCH:
  282. type = HW_BREAKPOINT_RW;
  283. break;
  284. default:
  285. return ERR_PTR(-EINVAL);
  286. }
  287. ptrace_breakpoint_init(&attr);
  288. /*
  289. * Initialise fields to sane defaults
  290. * (i.e. values that will pass validation).
  291. */
  292. attr.bp_addr = 0;
  293. attr.bp_len = HW_BREAKPOINT_LEN_4;
  294. attr.bp_type = type;
  295. attr.disabled = 1;
  296. bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
  297. if (IS_ERR(bp))
  298. return bp;
  299. err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
  300. if (err)
  301. return ERR_PTR(err);
  302. return bp;
  303. }
  304. static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
  305. struct arch_hw_breakpoint_ctrl ctrl,
  306. struct perf_event_attr *attr)
  307. {
  308. int err, len, type, offset, disabled = !ctrl.enabled;
  309. attr->disabled = disabled;
  310. if (disabled)
  311. return 0;
  312. err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
  313. if (err)
  314. return err;
  315. switch (note_type) {
  316. case NT_ARM_HW_BREAK:
  317. if ((type & HW_BREAKPOINT_X) != type)
  318. return -EINVAL;
  319. break;
  320. case NT_ARM_HW_WATCH:
  321. if ((type & HW_BREAKPOINT_RW) != type)
  322. return -EINVAL;
  323. break;
  324. default:
  325. return -EINVAL;
  326. }
  327. attr->bp_len = len;
  328. attr->bp_type = type;
  329. attr->bp_addr += offset;
  330. return 0;
  331. }
  332. static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
  333. {
  334. u8 num;
  335. u32 reg = 0;
  336. switch (note_type) {
  337. case NT_ARM_HW_BREAK:
  338. num = hw_breakpoint_slots(TYPE_INST);
  339. break;
  340. case NT_ARM_HW_WATCH:
  341. num = hw_breakpoint_slots(TYPE_DATA);
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. reg |= debug_monitors_arch();
  347. reg <<= 8;
  348. reg |= num;
  349. *info = reg;
  350. return 0;
  351. }
  352. static int ptrace_hbp_get_ctrl(unsigned int note_type,
  353. struct task_struct *tsk,
  354. unsigned long idx,
  355. u32 *ctrl)
  356. {
  357. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  358. if (IS_ERR(bp))
  359. return PTR_ERR(bp);
  360. *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
  361. return 0;
  362. }
  363. static int ptrace_hbp_get_addr(unsigned int note_type,
  364. struct task_struct *tsk,
  365. unsigned long idx,
  366. u64 *addr)
  367. {
  368. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  369. if (IS_ERR(bp))
  370. return PTR_ERR(bp);
  371. *addr = bp ? counter_arch_bp(bp)->address : 0;
  372. return 0;
  373. }
  374. static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
  375. struct task_struct *tsk,
  376. unsigned long idx)
  377. {
  378. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  379. if (!bp)
  380. bp = ptrace_hbp_create(note_type, tsk, idx);
  381. return bp;
  382. }
  383. static int ptrace_hbp_set_ctrl(unsigned int note_type,
  384. struct task_struct *tsk,
  385. unsigned long idx,
  386. u32 uctrl)
  387. {
  388. int err;
  389. struct perf_event *bp;
  390. struct perf_event_attr attr;
  391. struct arch_hw_breakpoint_ctrl ctrl;
  392. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  393. if (IS_ERR(bp)) {
  394. err = PTR_ERR(bp);
  395. return err;
  396. }
  397. attr = bp->attr;
  398. decode_ctrl_reg(uctrl, &ctrl);
  399. err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
  400. if (err)
  401. return err;
  402. return modify_user_hw_breakpoint(bp, &attr);
  403. }
  404. static int ptrace_hbp_set_addr(unsigned int note_type,
  405. struct task_struct *tsk,
  406. unsigned long idx,
  407. u64 addr)
  408. {
  409. int err;
  410. struct perf_event *bp;
  411. struct perf_event_attr attr;
  412. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  413. if (IS_ERR(bp)) {
  414. err = PTR_ERR(bp);
  415. return err;
  416. }
  417. attr = bp->attr;
  418. attr.bp_addr = addr;
  419. err = modify_user_hw_breakpoint(bp, &attr);
  420. return err;
  421. }
  422. #define PTRACE_HBP_ADDR_SZ sizeof(u64)
  423. #define PTRACE_HBP_CTRL_SZ sizeof(u32)
  424. #define PTRACE_HBP_PAD_SZ sizeof(u32)
  425. static int hw_break_get(struct task_struct *target,
  426. const struct user_regset *regset,
  427. unsigned int pos, unsigned int count,
  428. void *kbuf, void __user *ubuf)
  429. {
  430. unsigned int note_type = regset->core_note_type;
  431. int ret, idx = 0, offset, limit;
  432. u32 info, ctrl;
  433. u64 addr;
  434. /* Resource info */
  435. ret = ptrace_hbp_get_resource_info(note_type, &info);
  436. if (ret)
  437. return ret;
  438. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &info, 0,
  439. sizeof(info));
  440. if (ret)
  441. return ret;
  442. /* Pad */
  443. offset = offsetof(struct user_hwdebug_state, pad);
  444. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf, offset,
  445. offset + PTRACE_HBP_PAD_SZ);
  446. if (ret)
  447. return ret;
  448. /* (address, ctrl) registers */
  449. offset = offsetof(struct user_hwdebug_state, dbg_regs);
  450. limit = regset->n * regset->size;
  451. while (count && offset < limit) {
  452. ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
  453. if (ret)
  454. return ret;
  455. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &addr,
  456. offset, offset + PTRACE_HBP_ADDR_SZ);
  457. if (ret)
  458. return ret;
  459. offset += PTRACE_HBP_ADDR_SZ;
  460. ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
  461. if (ret)
  462. return ret;
  463. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &ctrl,
  464. offset, offset + PTRACE_HBP_CTRL_SZ);
  465. if (ret)
  466. return ret;
  467. offset += PTRACE_HBP_CTRL_SZ;
  468. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  469. offset,
  470. offset + PTRACE_HBP_PAD_SZ);
  471. if (ret)
  472. return ret;
  473. offset += PTRACE_HBP_PAD_SZ;
  474. idx++;
  475. }
  476. return 0;
  477. }
  478. static int hw_break_set(struct task_struct *target,
  479. const struct user_regset *regset,
  480. unsigned int pos, unsigned int count,
  481. const void *kbuf, const void __user *ubuf)
  482. {
  483. unsigned int note_type = regset->core_note_type;
  484. int ret, idx = 0, offset, limit;
  485. u32 ctrl;
  486. u64 addr;
  487. /* Resource info and pad */
  488. offset = offsetof(struct user_hwdebug_state, dbg_regs);
  489. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
  490. if (ret)
  491. return ret;
  492. /* (address, ctrl) registers */
  493. limit = regset->n * regset->size;
  494. while (count && offset < limit) {
  495. if (count < PTRACE_HBP_ADDR_SZ)
  496. return -EINVAL;
  497. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
  498. offset, offset + PTRACE_HBP_ADDR_SZ);
  499. if (ret)
  500. return ret;
  501. ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
  502. if (ret)
  503. return ret;
  504. offset += PTRACE_HBP_ADDR_SZ;
  505. if (!count)
  506. break;
  507. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
  508. offset, offset + PTRACE_HBP_CTRL_SZ);
  509. if (ret)
  510. return ret;
  511. ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
  512. if (ret)
  513. return ret;
  514. offset += PTRACE_HBP_CTRL_SZ;
  515. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  516. offset,
  517. offset + PTRACE_HBP_PAD_SZ);
  518. if (ret)
  519. return ret;
  520. offset += PTRACE_HBP_PAD_SZ;
  521. idx++;
  522. }
  523. return 0;
  524. }
  525. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  526. static int gpr_get(struct task_struct *target,
  527. const struct user_regset *regset,
  528. unsigned int pos, unsigned int count,
  529. void *kbuf, void __user *ubuf)
  530. {
  531. struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
  532. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs, 0, -1);
  533. }
  534. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  535. unsigned int pos, unsigned int count,
  536. const void *kbuf, const void __user *ubuf)
  537. {
  538. int ret;
  539. struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
  540. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
  541. if (ret)
  542. return ret;
  543. if (!valid_user_regs(&newregs, target))
  544. return -EINVAL;
  545. task_pt_regs(target)->user_regs = newregs;
  546. return 0;
  547. }
  548. /*
  549. * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
  550. */
  551. static int __fpr_get(struct task_struct *target,
  552. const struct user_regset *regset,
  553. unsigned int pos, unsigned int count,
  554. void *kbuf, void __user *ubuf, unsigned int start_pos)
  555. {
  556. struct user_fpsimd_state *uregs;
  557. sve_sync_to_fpsimd(target);
  558. uregs = &target->thread.uw.fpsimd_state;
  559. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
  560. start_pos, start_pos + sizeof(*uregs));
  561. }
  562. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  563. unsigned int pos, unsigned int count,
  564. void *kbuf, void __user *ubuf)
  565. {
  566. if (target == current)
  567. fpsimd_preserve_current_state();
  568. return __fpr_get(target, regset, pos, count, kbuf, ubuf, 0);
  569. }
  570. static int __fpr_set(struct task_struct *target,
  571. const struct user_regset *regset,
  572. unsigned int pos, unsigned int count,
  573. const void *kbuf, const void __user *ubuf,
  574. unsigned int start_pos)
  575. {
  576. int ret;
  577. struct user_fpsimd_state newstate;
  578. /*
  579. * Ensure target->thread.uw.fpsimd_state is up to date, so that a
  580. * short copyin can't resurrect stale data.
  581. */
  582. sve_sync_to_fpsimd(target);
  583. newstate = target->thread.uw.fpsimd_state;
  584. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
  585. start_pos, start_pos + sizeof(newstate));
  586. if (ret)
  587. return ret;
  588. target->thread.uw.fpsimd_state = newstate;
  589. return ret;
  590. }
  591. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  592. unsigned int pos, unsigned int count,
  593. const void *kbuf, const void __user *ubuf)
  594. {
  595. int ret;
  596. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
  597. if (ret)
  598. return ret;
  599. sve_sync_from_fpsimd_zeropad(target);
  600. fpsimd_flush_task_state(target);
  601. return ret;
  602. }
  603. static int tls_get(struct task_struct *target, const struct user_regset *regset,
  604. unsigned int pos, unsigned int count,
  605. void *kbuf, void __user *ubuf)
  606. {
  607. unsigned long *tls = &target->thread.uw.tp_value;
  608. if (target == current)
  609. tls_preserve_current_state();
  610. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
  611. }
  612. static int tls_set(struct task_struct *target, const struct user_regset *regset,
  613. unsigned int pos, unsigned int count,
  614. const void *kbuf, const void __user *ubuf)
  615. {
  616. int ret;
  617. unsigned long tls = target->thread.uw.tp_value;
  618. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  619. if (ret)
  620. return ret;
  621. target->thread.uw.tp_value = tls;
  622. return ret;
  623. }
  624. static int system_call_get(struct task_struct *target,
  625. const struct user_regset *regset,
  626. unsigned int pos, unsigned int count,
  627. void *kbuf, void __user *ubuf)
  628. {
  629. int syscallno = task_pt_regs(target)->syscallno;
  630. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  631. &syscallno, 0, -1);
  632. }
  633. static int system_call_set(struct task_struct *target,
  634. const struct user_regset *regset,
  635. unsigned int pos, unsigned int count,
  636. const void *kbuf, const void __user *ubuf)
  637. {
  638. int syscallno = task_pt_regs(target)->syscallno;
  639. int ret;
  640. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
  641. if (ret)
  642. return ret;
  643. task_pt_regs(target)->syscallno = syscallno;
  644. return ret;
  645. }
  646. #ifdef CONFIG_ARM64_SVE
  647. static void sve_init_header_from_task(struct user_sve_header *header,
  648. struct task_struct *target)
  649. {
  650. unsigned int vq;
  651. memset(header, 0, sizeof(*header));
  652. header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
  653. SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
  654. if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
  655. header->flags |= SVE_PT_VL_INHERIT;
  656. header->vl = target->thread.sve_vl;
  657. vq = sve_vq_from_vl(header->vl);
  658. header->max_vl = sve_max_vl;
  659. header->size = SVE_PT_SIZE(vq, header->flags);
  660. header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
  661. SVE_PT_REGS_SVE);
  662. }
  663. static unsigned int sve_size_from_header(struct user_sve_header const *header)
  664. {
  665. return ALIGN(header->size, SVE_VQ_BYTES);
  666. }
  667. static unsigned int sve_get_size(struct task_struct *target,
  668. const struct user_regset *regset)
  669. {
  670. struct user_sve_header header;
  671. if (!system_supports_sve())
  672. return 0;
  673. sve_init_header_from_task(&header, target);
  674. return sve_size_from_header(&header);
  675. }
  676. static int sve_get(struct task_struct *target,
  677. const struct user_regset *regset,
  678. unsigned int pos, unsigned int count,
  679. void *kbuf, void __user *ubuf)
  680. {
  681. int ret;
  682. struct user_sve_header header;
  683. unsigned int vq;
  684. unsigned long start, end;
  685. if (!system_supports_sve())
  686. return -EINVAL;
  687. /* Header */
  688. sve_init_header_from_task(&header, target);
  689. vq = sve_vq_from_vl(header.vl);
  690. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &header,
  691. 0, sizeof(header));
  692. if (ret)
  693. return ret;
  694. if (target == current)
  695. fpsimd_preserve_current_state();
  696. /* Registers: FPSIMD-only case */
  697. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  698. if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD)
  699. return __fpr_get(target, regset, pos, count, kbuf, ubuf,
  700. SVE_PT_FPSIMD_OFFSET);
  701. /* Otherwise: full SVE case */
  702. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  703. start = SVE_PT_SVE_OFFSET;
  704. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  705. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  706. target->thread.sve_state,
  707. start, end);
  708. if (ret)
  709. return ret;
  710. start = end;
  711. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  712. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  713. start, end);
  714. if (ret)
  715. return ret;
  716. /*
  717. * Copy fpsr, and fpcr which must follow contiguously in
  718. * struct fpsimd_state:
  719. */
  720. start = end;
  721. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  722. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  723. &target->thread.uw.fpsimd_state.fpsr,
  724. start, end);
  725. if (ret)
  726. return ret;
  727. start = end;
  728. end = sve_size_from_header(&header);
  729. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  730. start, end);
  731. }
  732. static int sve_set(struct task_struct *target,
  733. const struct user_regset *regset,
  734. unsigned int pos, unsigned int count,
  735. const void *kbuf, const void __user *ubuf)
  736. {
  737. int ret;
  738. struct user_sve_header header;
  739. unsigned int vq;
  740. unsigned long start, end;
  741. if (!system_supports_sve())
  742. return -EINVAL;
  743. /* Header */
  744. if (count < sizeof(header))
  745. return -EINVAL;
  746. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
  747. 0, sizeof(header));
  748. if (ret)
  749. goto out;
  750. /*
  751. * Apart from PT_SVE_REGS_MASK, all PT_SVE_* flags are consumed by
  752. * sve_set_vector_length(), which will also validate them for us:
  753. */
  754. ret = sve_set_vector_length(target, header.vl,
  755. ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
  756. if (ret)
  757. goto out;
  758. /* Actual VL set may be less than the user asked for: */
  759. vq = sve_vq_from_vl(target->thread.sve_vl);
  760. /* Registers: FPSIMD-only case */
  761. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  762. if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
  763. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
  764. SVE_PT_FPSIMD_OFFSET);
  765. clear_tsk_thread_flag(target, TIF_SVE);
  766. goto out;
  767. }
  768. /* Otherwise: full SVE case */
  769. /*
  770. * If setting a different VL from the requested VL and there is
  771. * register data, the data layout will be wrong: don't even
  772. * try to set the registers in this case.
  773. */
  774. if (count && vq != sve_vq_from_vl(header.vl)) {
  775. ret = -EIO;
  776. goto out;
  777. }
  778. sve_alloc(target);
  779. /*
  780. * Ensure target->thread.sve_state is up to date with target's
  781. * FPSIMD regs, so that a short copyin leaves trailing registers
  782. * unmodified.
  783. */
  784. fpsimd_sync_to_sve(target);
  785. set_tsk_thread_flag(target, TIF_SVE);
  786. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  787. start = SVE_PT_SVE_OFFSET;
  788. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  789. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  790. target->thread.sve_state,
  791. start, end);
  792. if (ret)
  793. goto out;
  794. start = end;
  795. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  796. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  797. start, end);
  798. if (ret)
  799. goto out;
  800. /*
  801. * Copy fpsr, and fpcr which must follow contiguously in
  802. * struct fpsimd_state:
  803. */
  804. start = end;
  805. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  806. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  807. &target->thread.uw.fpsimd_state.fpsr,
  808. start, end);
  809. out:
  810. fpsimd_flush_task_state(target);
  811. return ret;
  812. }
  813. #endif /* CONFIG_ARM64_SVE */
  814. enum aarch64_regset {
  815. REGSET_GPR,
  816. REGSET_FPR,
  817. REGSET_TLS,
  818. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  819. REGSET_HW_BREAK,
  820. REGSET_HW_WATCH,
  821. #endif
  822. REGSET_SYSTEM_CALL,
  823. #ifdef CONFIG_ARM64_SVE
  824. REGSET_SVE,
  825. #endif
  826. };
  827. static const struct user_regset aarch64_regsets[] = {
  828. [REGSET_GPR] = {
  829. .core_note_type = NT_PRSTATUS,
  830. .n = sizeof(struct user_pt_regs) / sizeof(u64),
  831. .size = sizeof(u64),
  832. .align = sizeof(u64),
  833. .get = gpr_get,
  834. .set = gpr_set
  835. },
  836. [REGSET_FPR] = {
  837. .core_note_type = NT_PRFPREG,
  838. .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
  839. /*
  840. * We pretend we have 32-bit registers because the fpsr and
  841. * fpcr are 32-bits wide.
  842. */
  843. .size = sizeof(u32),
  844. .align = sizeof(u32),
  845. .get = fpr_get,
  846. .set = fpr_set
  847. },
  848. [REGSET_TLS] = {
  849. .core_note_type = NT_ARM_TLS,
  850. .n = 1,
  851. .size = sizeof(void *),
  852. .align = sizeof(void *),
  853. .get = tls_get,
  854. .set = tls_set,
  855. },
  856. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  857. [REGSET_HW_BREAK] = {
  858. .core_note_type = NT_ARM_HW_BREAK,
  859. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  860. .size = sizeof(u32),
  861. .align = sizeof(u32),
  862. .get = hw_break_get,
  863. .set = hw_break_set,
  864. },
  865. [REGSET_HW_WATCH] = {
  866. .core_note_type = NT_ARM_HW_WATCH,
  867. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  868. .size = sizeof(u32),
  869. .align = sizeof(u32),
  870. .get = hw_break_get,
  871. .set = hw_break_set,
  872. },
  873. #endif
  874. [REGSET_SYSTEM_CALL] = {
  875. .core_note_type = NT_ARM_SYSTEM_CALL,
  876. .n = 1,
  877. .size = sizeof(int),
  878. .align = sizeof(int),
  879. .get = system_call_get,
  880. .set = system_call_set,
  881. },
  882. #ifdef CONFIG_ARM64_SVE
  883. [REGSET_SVE] = { /* Scalable Vector Extension */
  884. .core_note_type = NT_ARM_SVE,
  885. .n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
  886. SVE_VQ_BYTES),
  887. .size = SVE_VQ_BYTES,
  888. .align = SVE_VQ_BYTES,
  889. .get = sve_get,
  890. .set = sve_set,
  891. .get_size = sve_get_size,
  892. },
  893. #endif
  894. };
  895. static const struct user_regset_view user_aarch64_view = {
  896. .name = "aarch64", .e_machine = EM_AARCH64,
  897. .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
  898. };
  899. #ifdef CONFIG_COMPAT
  900. enum compat_regset {
  901. REGSET_COMPAT_GPR,
  902. REGSET_COMPAT_VFP,
  903. };
  904. static int compat_gpr_get(struct task_struct *target,
  905. const struct user_regset *regset,
  906. unsigned int pos, unsigned int count,
  907. void *kbuf, void __user *ubuf)
  908. {
  909. int ret = 0;
  910. unsigned int i, start, num_regs;
  911. /* Calculate the number of AArch32 registers contained in count */
  912. num_regs = count / regset->size;
  913. /* Convert pos into an register number */
  914. start = pos / regset->size;
  915. if (start + num_regs > regset->n)
  916. return -EIO;
  917. for (i = 0; i < num_regs; ++i) {
  918. unsigned int idx = start + i;
  919. compat_ulong_t reg;
  920. switch (idx) {
  921. case 15:
  922. reg = task_pt_regs(target)->pc;
  923. break;
  924. case 16:
  925. reg = task_pt_regs(target)->pstate;
  926. reg = pstate_to_compat_psr(reg);
  927. break;
  928. case 17:
  929. reg = task_pt_regs(target)->orig_x0;
  930. break;
  931. default:
  932. reg = task_pt_regs(target)->regs[idx];
  933. }
  934. if (kbuf) {
  935. memcpy(kbuf, &reg, sizeof(reg));
  936. kbuf += sizeof(reg);
  937. } else {
  938. ret = copy_to_user(ubuf, &reg, sizeof(reg));
  939. if (ret) {
  940. ret = -EFAULT;
  941. break;
  942. }
  943. ubuf += sizeof(reg);
  944. }
  945. }
  946. return ret;
  947. }
  948. static int compat_gpr_set(struct task_struct *target,
  949. const struct user_regset *regset,
  950. unsigned int pos, unsigned int count,
  951. const void *kbuf, const void __user *ubuf)
  952. {
  953. struct pt_regs newregs;
  954. int ret = 0;
  955. unsigned int i, start, num_regs;
  956. /* Calculate the number of AArch32 registers contained in count */
  957. num_regs = count / regset->size;
  958. /* Convert pos into an register number */
  959. start = pos / regset->size;
  960. if (start + num_regs > regset->n)
  961. return -EIO;
  962. newregs = *task_pt_regs(target);
  963. for (i = 0; i < num_regs; ++i) {
  964. unsigned int idx = start + i;
  965. compat_ulong_t reg;
  966. if (kbuf) {
  967. memcpy(&reg, kbuf, sizeof(reg));
  968. kbuf += sizeof(reg);
  969. } else {
  970. ret = copy_from_user(&reg, ubuf, sizeof(reg));
  971. if (ret) {
  972. ret = -EFAULT;
  973. break;
  974. }
  975. ubuf += sizeof(reg);
  976. }
  977. switch (idx) {
  978. case 15:
  979. newregs.pc = reg;
  980. break;
  981. case 16:
  982. reg = compat_psr_to_pstate(reg);
  983. newregs.pstate = reg;
  984. break;
  985. case 17:
  986. newregs.orig_x0 = reg;
  987. break;
  988. default:
  989. newregs.regs[idx] = reg;
  990. }
  991. }
  992. if (valid_user_regs(&newregs.user_regs, target))
  993. *task_pt_regs(target) = newregs;
  994. else
  995. ret = -EINVAL;
  996. return ret;
  997. }
  998. static int compat_vfp_get(struct task_struct *target,
  999. const struct user_regset *regset,
  1000. unsigned int pos, unsigned int count,
  1001. void *kbuf, void __user *ubuf)
  1002. {
  1003. struct user_fpsimd_state *uregs;
  1004. compat_ulong_t fpscr;
  1005. int ret, vregs_end_pos;
  1006. uregs = &target->thread.uw.fpsimd_state;
  1007. if (target == current)
  1008. fpsimd_preserve_current_state();
  1009. /*
  1010. * The VFP registers are packed into the fpsimd_state, so they all sit
  1011. * nicely together for us. We just need to create the fpscr separately.
  1012. */
  1013. vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
  1014. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
  1015. 0, vregs_end_pos);
  1016. if (count && !ret) {
  1017. fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
  1018. (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
  1019. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
  1020. vregs_end_pos, VFP_STATE_SIZE);
  1021. }
  1022. return ret;
  1023. }
  1024. static int compat_vfp_set(struct task_struct *target,
  1025. const struct user_regset *regset,
  1026. unsigned int pos, unsigned int count,
  1027. const void *kbuf, const void __user *ubuf)
  1028. {
  1029. struct user_fpsimd_state *uregs;
  1030. compat_ulong_t fpscr;
  1031. int ret, vregs_end_pos;
  1032. uregs = &target->thread.uw.fpsimd_state;
  1033. vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
  1034. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  1035. vregs_end_pos);
  1036. if (count && !ret) {
  1037. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
  1038. vregs_end_pos, VFP_STATE_SIZE);
  1039. if (!ret) {
  1040. uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  1041. uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  1042. }
  1043. }
  1044. fpsimd_flush_task_state(target);
  1045. return ret;
  1046. }
  1047. static int compat_tls_get(struct task_struct *target,
  1048. const struct user_regset *regset, unsigned int pos,
  1049. unsigned int count, void *kbuf, void __user *ubuf)
  1050. {
  1051. compat_ulong_t tls = (compat_ulong_t)target->thread.uw.tp_value;
  1052. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  1053. }
  1054. static int compat_tls_set(struct task_struct *target,
  1055. const struct user_regset *regset, unsigned int pos,
  1056. unsigned int count, const void *kbuf,
  1057. const void __user *ubuf)
  1058. {
  1059. int ret;
  1060. compat_ulong_t tls = target->thread.uw.tp_value;
  1061. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  1062. if (ret)
  1063. return ret;
  1064. target->thread.uw.tp_value = tls;
  1065. return ret;
  1066. }
  1067. static const struct user_regset aarch32_regsets[] = {
  1068. [REGSET_COMPAT_GPR] = {
  1069. .core_note_type = NT_PRSTATUS,
  1070. .n = COMPAT_ELF_NGREG,
  1071. .size = sizeof(compat_elf_greg_t),
  1072. .align = sizeof(compat_elf_greg_t),
  1073. .get = compat_gpr_get,
  1074. .set = compat_gpr_set
  1075. },
  1076. [REGSET_COMPAT_VFP] = {
  1077. .core_note_type = NT_ARM_VFP,
  1078. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1079. .size = sizeof(compat_ulong_t),
  1080. .align = sizeof(compat_ulong_t),
  1081. .get = compat_vfp_get,
  1082. .set = compat_vfp_set
  1083. },
  1084. };
  1085. static const struct user_regset_view user_aarch32_view = {
  1086. .name = "aarch32", .e_machine = EM_ARM,
  1087. .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
  1088. };
  1089. static const struct user_regset aarch32_ptrace_regsets[] = {
  1090. [REGSET_GPR] = {
  1091. .core_note_type = NT_PRSTATUS,
  1092. .n = COMPAT_ELF_NGREG,
  1093. .size = sizeof(compat_elf_greg_t),
  1094. .align = sizeof(compat_elf_greg_t),
  1095. .get = compat_gpr_get,
  1096. .set = compat_gpr_set
  1097. },
  1098. [REGSET_FPR] = {
  1099. .core_note_type = NT_ARM_VFP,
  1100. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1101. .size = sizeof(compat_ulong_t),
  1102. .align = sizeof(compat_ulong_t),
  1103. .get = compat_vfp_get,
  1104. .set = compat_vfp_set
  1105. },
  1106. [REGSET_TLS] = {
  1107. .core_note_type = NT_ARM_TLS,
  1108. .n = 1,
  1109. .size = sizeof(compat_ulong_t),
  1110. .align = sizeof(compat_ulong_t),
  1111. .get = compat_tls_get,
  1112. .set = compat_tls_set,
  1113. },
  1114. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1115. [REGSET_HW_BREAK] = {
  1116. .core_note_type = NT_ARM_HW_BREAK,
  1117. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1118. .size = sizeof(u32),
  1119. .align = sizeof(u32),
  1120. .get = hw_break_get,
  1121. .set = hw_break_set,
  1122. },
  1123. [REGSET_HW_WATCH] = {
  1124. .core_note_type = NT_ARM_HW_WATCH,
  1125. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1126. .size = sizeof(u32),
  1127. .align = sizeof(u32),
  1128. .get = hw_break_get,
  1129. .set = hw_break_set,
  1130. },
  1131. #endif
  1132. [REGSET_SYSTEM_CALL] = {
  1133. .core_note_type = NT_ARM_SYSTEM_CALL,
  1134. .n = 1,
  1135. .size = sizeof(int),
  1136. .align = sizeof(int),
  1137. .get = system_call_get,
  1138. .set = system_call_set,
  1139. },
  1140. };
  1141. static const struct user_regset_view user_aarch32_ptrace_view = {
  1142. .name = "aarch32", .e_machine = EM_ARM,
  1143. .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
  1144. };
  1145. static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
  1146. compat_ulong_t __user *ret)
  1147. {
  1148. compat_ulong_t tmp;
  1149. if (off & 3)
  1150. return -EIO;
  1151. if (off == COMPAT_PT_TEXT_ADDR)
  1152. tmp = tsk->mm->start_code;
  1153. else if (off == COMPAT_PT_DATA_ADDR)
  1154. tmp = tsk->mm->start_data;
  1155. else if (off == COMPAT_PT_TEXT_END_ADDR)
  1156. tmp = tsk->mm->end_code;
  1157. else if (off < sizeof(compat_elf_gregset_t))
  1158. return copy_regset_to_user(tsk, &user_aarch32_view,
  1159. REGSET_COMPAT_GPR, off,
  1160. sizeof(compat_ulong_t), ret);
  1161. else if (off >= COMPAT_USER_SZ)
  1162. return -EIO;
  1163. else
  1164. tmp = 0;
  1165. return put_user(tmp, ret);
  1166. }
  1167. static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
  1168. compat_ulong_t val)
  1169. {
  1170. int ret;
  1171. mm_segment_t old_fs = get_fs();
  1172. if (off & 3 || off >= COMPAT_USER_SZ)
  1173. return -EIO;
  1174. if (off >= sizeof(compat_elf_gregset_t))
  1175. return 0;
  1176. set_fs(KERNEL_DS);
  1177. ret = copy_regset_from_user(tsk, &user_aarch32_view,
  1178. REGSET_COMPAT_GPR, off,
  1179. sizeof(compat_ulong_t),
  1180. &val);
  1181. set_fs(old_fs);
  1182. return ret;
  1183. }
  1184. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1185. /*
  1186. * Convert a virtual register number into an index for a thread_info
  1187. * breakpoint array. Breakpoints are identified using positive numbers
  1188. * whilst watchpoints are negative. The registers are laid out as pairs
  1189. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  1190. * Register 0 is reserved for describing resource information.
  1191. */
  1192. static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
  1193. {
  1194. return (abs(num) - 1) >> 1;
  1195. }
  1196. static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
  1197. {
  1198. u8 num_brps, num_wrps, debug_arch, wp_len;
  1199. u32 reg = 0;
  1200. num_brps = hw_breakpoint_slots(TYPE_INST);
  1201. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  1202. debug_arch = debug_monitors_arch();
  1203. wp_len = 8;
  1204. reg |= debug_arch;
  1205. reg <<= 8;
  1206. reg |= wp_len;
  1207. reg <<= 8;
  1208. reg |= num_wrps;
  1209. reg <<= 8;
  1210. reg |= num_brps;
  1211. *kdata = reg;
  1212. return 0;
  1213. }
  1214. static int compat_ptrace_hbp_get(unsigned int note_type,
  1215. struct task_struct *tsk,
  1216. compat_long_t num,
  1217. u32 *kdata)
  1218. {
  1219. u64 addr = 0;
  1220. u32 ctrl = 0;
  1221. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1222. if (num & 1) {
  1223. err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
  1224. *kdata = (u32)addr;
  1225. } else {
  1226. err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
  1227. *kdata = ctrl;
  1228. }
  1229. return err;
  1230. }
  1231. static int compat_ptrace_hbp_set(unsigned int note_type,
  1232. struct task_struct *tsk,
  1233. compat_long_t num,
  1234. u32 *kdata)
  1235. {
  1236. u64 addr;
  1237. u32 ctrl;
  1238. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1239. if (num & 1) {
  1240. addr = *kdata;
  1241. err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
  1242. } else {
  1243. ctrl = *kdata;
  1244. err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
  1245. }
  1246. return err;
  1247. }
  1248. static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
  1249. compat_ulong_t __user *data)
  1250. {
  1251. int ret;
  1252. u32 kdata;
  1253. /* Watchpoint */
  1254. if (num < 0) {
  1255. ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1256. /* Resource info */
  1257. } else if (num == 0) {
  1258. ret = compat_ptrace_hbp_get_resource_info(&kdata);
  1259. /* Breakpoint */
  1260. } else {
  1261. ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1262. }
  1263. if (!ret)
  1264. ret = put_user(kdata, data);
  1265. return ret;
  1266. }
  1267. static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
  1268. compat_ulong_t __user *data)
  1269. {
  1270. int ret;
  1271. u32 kdata = 0;
  1272. if (num == 0)
  1273. return 0;
  1274. ret = get_user(kdata, data);
  1275. if (ret)
  1276. return ret;
  1277. if (num < 0)
  1278. ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1279. else
  1280. ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1281. return ret;
  1282. }
  1283. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1284. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1285. compat_ulong_t caddr, compat_ulong_t cdata)
  1286. {
  1287. unsigned long addr = caddr;
  1288. unsigned long data = cdata;
  1289. void __user *datap = compat_ptr(data);
  1290. int ret;
  1291. switch (request) {
  1292. case PTRACE_PEEKUSR:
  1293. ret = compat_ptrace_read_user(child, addr, datap);
  1294. break;
  1295. case PTRACE_POKEUSR:
  1296. ret = compat_ptrace_write_user(child, addr, data);
  1297. break;
  1298. case COMPAT_PTRACE_GETREGS:
  1299. ret = copy_regset_to_user(child,
  1300. &user_aarch32_view,
  1301. REGSET_COMPAT_GPR,
  1302. 0, sizeof(compat_elf_gregset_t),
  1303. datap);
  1304. break;
  1305. case COMPAT_PTRACE_SETREGS:
  1306. ret = copy_regset_from_user(child,
  1307. &user_aarch32_view,
  1308. REGSET_COMPAT_GPR,
  1309. 0, sizeof(compat_elf_gregset_t),
  1310. datap);
  1311. break;
  1312. case COMPAT_PTRACE_GET_THREAD_AREA:
  1313. ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
  1314. (compat_ulong_t __user *)datap);
  1315. break;
  1316. case COMPAT_PTRACE_SET_SYSCALL:
  1317. task_pt_regs(child)->syscallno = data;
  1318. ret = 0;
  1319. break;
  1320. case COMPAT_PTRACE_GETVFPREGS:
  1321. ret = copy_regset_to_user(child,
  1322. &user_aarch32_view,
  1323. REGSET_COMPAT_VFP,
  1324. 0, VFP_STATE_SIZE,
  1325. datap);
  1326. break;
  1327. case COMPAT_PTRACE_SETVFPREGS:
  1328. ret = copy_regset_from_user(child,
  1329. &user_aarch32_view,
  1330. REGSET_COMPAT_VFP,
  1331. 0, VFP_STATE_SIZE,
  1332. datap);
  1333. break;
  1334. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1335. case COMPAT_PTRACE_GETHBPREGS:
  1336. ret = compat_ptrace_gethbpregs(child, addr, datap);
  1337. break;
  1338. case COMPAT_PTRACE_SETHBPREGS:
  1339. ret = compat_ptrace_sethbpregs(child, addr, datap);
  1340. break;
  1341. #endif
  1342. default:
  1343. ret = compat_ptrace_request(child, request, addr,
  1344. data);
  1345. break;
  1346. }
  1347. return ret;
  1348. }
  1349. #endif /* CONFIG_COMPAT */
  1350. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1351. {
  1352. #ifdef CONFIG_COMPAT
  1353. /*
  1354. * Core dumping of 32-bit tasks or compat ptrace requests must use the
  1355. * user_aarch32_view compatible with arm32. Native ptrace requests on
  1356. * 32-bit children use an extended user_aarch32_ptrace_view to allow
  1357. * access to the TLS register.
  1358. */
  1359. if (is_compat_task())
  1360. return &user_aarch32_view;
  1361. else if (is_compat_thread(task_thread_info(task)))
  1362. return &user_aarch32_ptrace_view;
  1363. #endif
  1364. return &user_aarch64_view;
  1365. }
  1366. long arch_ptrace(struct task_struct *child, long request,
  1367. unsigned long addr, unsigned long data)
  1368. {
  1369. return ptrace_request(child, request, addr, data);
  1370. }
  1371. enum ptrace_syscall_dir {
  1372. PTRACE_SYSCALL_ENTER = 0,
  1373. PTRACE_SYSCALL_EXIT,
  1374. };
  1375. static void tracehook_report_syscall(struct pt_regs *regs,
  1376. enum ptrace_syscall_dir dir)
  1377. {
  1378. int regno;
  1379. unsigned long saved_reg;
  1380. /*
  1381. * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
  1382. * used to denote syscall entry/exit:
  1383. */
  1384. regno = (is_compat_task() ? 12 : 7);
  1385. saved_reg = regs->regs[regno];
  1386. regs->regs[regno] = dir;
  1387. if (dir == PTRACE_SYSCALL_EXIT)
  1388. tracehook_report_syscall_exit(regs, 0);
  1389. else if (tracehook_report_syscall_entry(regs))
  1390. forget_syscall(regs);
  1391. regs->regs[regno] = saved_reg;
  1392. }
  1393. int syscall_trace_enter(struct pt_regs *regs)
  1394. {
  1395. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1396. tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
  1397. /* Do the secure computing after ptrace; failures should be fast. */
  1398. if (secure_computing(NULL) == -1)
  1399. return -1;
  1400. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  1401. trace_sys_enter(regs, regs->syscallno);
  1402. audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
  1403. regs->regs[2], regs->regs[3]);
  1404. return regs->syscallno;
  1405. }
  1406. void syscall_trace_exit(struct pt_regs *regs)
  1407. {
  1408. audit_syscall_exit(regs);
  1409. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  1410. trace_sys_exit(regs, regs_return_value(regs));
  1411. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1412. tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
  1413. rseq_syscall(regs);
  1414. }
  1415. /*
  1416. * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
  1417. * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
  1418. * not described in ARM DDI 0487D.a.
  1419. * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
  1420. * be allocated an EL0 meaning in future.
  1421. * Userspace cannot use these until they have an architectural meaning.
  1422. * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
  1423. * We also reserve IL for the kernel; SS is handled dynamically.
  1424. */
  1425. #define SPSR_EL1_AARCH64_RES0_BITS \
  1426. (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
  1427. GENMASK_ULL(20, 13) | GENMASK_ULL(11, 10) | GENMASK_ULL(5, 5))
  1428. #define SPSR_EL1_AARCH32_RES0_BITS \
  1429. (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
  1430. static int valid_compat_regs(struct user_pt_regs *regs)
  1431. {
  1432. regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
  1433. if (!system_supports_mixed_endian_el0()) {
  1434. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1435. regs->pstate |= PSR_AA32_E_BIT;
  1436. else
  1437. regs->pstate &= ~PSR_AA32_E_BIT;
  1438. }
  1439. if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
  1440. (regs->pstate & PSR_AA32_A_BIT) == 0 &&
  1441. (regs->pstate & PSR_AA32_I_BIT) == 0 &&
  1442. (regs->pstate & PSR_AA32_F_BIT) == 0) {
  1443. return 1;
  1444. }
  1445. /*
  1446. * Force PSR to a valid 32-bit EL0t, preserving the same bits as
  1447. * arch/arm.
  1448. */
  1449. regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
  1450. PSR_AA32_C_BIT | PSR_AA32_V_BIT |
  1451. PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
  1452. PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
  1453. PSR_AA32_T_BIT;
  1454. regs->pstate |= PSR_MODE32_BIT;
  1455. return 0;
  1456. }
  1457. static int valid_native_regs(struct user_pt_regs *regs)
  1458. {
  1459. regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
  1460. if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
  1461. (regs->pstate & PSR_D_BIT) == 0 &&
  1462. (regs->pstate & PSR_A_BIT) == 0 &&
  1463. (regs->pstate & PSR_I_BIT) == 0 &&
  1464. (regs->pstate & PSR_F_BIT) == 0) {
  1465. return 1;
  1466. }
  1467. /* Force PSR to a valid 64-bit EL0t */
  1468. regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
  1469. return 0;
  1470. }
  1471. /*
  1472. * Are the current registers suitable for user mode? (used to maintain
  1473. * security in signal handlers)
  1474. */
  1475. int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
  1476. {
  1477. if (!test_tsk_thread_flag(task, TIF_SINGLESTEP))
  1478. regs->pstate &= ~DBG_SPSR_SS;
  1479. if (is_compat_thread(task_thread_info(task)))
  1480. return valid_compat_regs(regs);
  1481. else
  1482. return valid_native_regs(regs);
  1483. }