ptrace.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/kernel/ptrace.c
  4. *
  5. * By Ross Biro 1/23/92
  6. * edited by Linus Torvalds
  7. * ARM modifications Copyright (C) 2000 Russell King
  8. * Copyright (C) 2012 ARM Ltd.
  9. */
  10. #include <linux/audit.h>
  11. #include <linux/compat.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/sched/task_stack.h>
  15. #include <linux/mm.h>
  16. #include <linux/nospec.h>
  17. #include <linux/smp.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/user.h>
  20. #include <linux/seccomp.h>
  21. #include <linux/security.h>
  22. #include <linux/init.h>
  23. #include <linux/signal.h>
  24. #include <linux/string.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/perf_event.h>
  27. #include <linux/hw_breakpoint.h>
  28. #include <linux/regset.h>
  29. #include <linux/elf.h>
  30. #include <asm/compat.h>
  31. #include <asm/cpufeature.h>
  32. #include <asm/debug-monitors.h>
  33. #include <asm/fpsimd.h>
  34. #include <asm/mte.h>
  35. #include <asm/pointer_auth.h>
  36. #include <asm/stacktrace.h>
  37. #include <asm/syscall.h>
  38. #include <asm/traps.h>
  39. #include <asm/system_misc.h>
  40. #define CREATE_TRACE_POINTS
  41. #include <trace/events/syscalls.h>
  42. struct pt_regs_offset {
  43. const char *name;
  44. int offset;
  45. };
  46. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  47. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  48. #define GPR_OFFSET_NAME(r) \
  49. {.name = "x" #r, .offset = offsetof(struct pt_regs, regs[r])}
  50. static const struct pt_regs_offset regoffset_table[] = {
  51. GPR_OFFSET_NAME(0),
  52. GPR_OFFSET_NAME(1),
  53. GPR_OFFSET_NAME(2),
  54. GPR_OFFSET_NAME(3),
  55. GPR_OFFSET_NAME(4),
  56. GPR_OFFSET_NAME(5),
  57. GPR_OFFSET_NAME(6),
  58. GPR_OFFSET_NAME(7),
  59. GPR_OFFSET_NAME(8),
  60. GPR_OFFSET_NAME(9),
  61. GPR_OFFSET_NAME(10),
  62. GPR_OFFSET_NAME(11),
  63. GPR_OFFSET_NAME(12),
  64. GPR_OFFSET_NAME(13),
  65. GPR_OFFSET_NAME(14),
  66. GPR_OFFSET_NAME(15),
  67. GPR_OFFSET_NAME(16),
  68. GPR_OFFSET_NAME(17),
  69. GPR_OFFSET_NAME(18),
  70. GPR_OFFSET_NAME(19),
  71. GPR_OFFSET_NAME(20),
  72. GPR_OFFSET_NAME(21),
  73. GPR_OFFSET_NAME(22),
  74. GPR_OFFSET_NAME(23),
  75. GPR_OFFSET_NAME(24),
  76. GPR_OFFSET_NAME(25),
  77. GPR_OFFSET_NAME(26),
  78. GPR_OFFSET_NAME(27),
  79. GPR_OFFSET_NAME(28),
  80. GPR_OFFSET_NAME(29),
  81. GPR_OFFSET_NAME(30),
  82. {.name = "lr", .offset = offsetof(struct pt_regs, regs[30])},
  83. REG_OFFSET_NAME(sp),
  84. REG_OFFSET_NAME(pc),
  85. REG_OFFSET_NAME(pstate),
  86. REG_OFFSET_END,
  87. };
  88. /**
  89. * regs_query_register_offset() - query register offset from its name
  90. * @name: the name of a register
  91. *
  92. * regs_query_register_offset() returns the offset of a register in struct
  93. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  94. */
  95. int regs_query_register_offset(const char *name)
  96. {
  97. const struct pt_regs_offset *roff;
  98. for (roff = regoffset_table; roff->name != NULL; roff++)
  99. if (!strcmp(roff->name, name))
  100. return roff->offset;
  101. return -EINVAL;
  102. }
  103. /**
  104. * regs_within_kernel_stack() - check the address in the stack
  105. * @regs: pt_regs which contains kernel stack pointer.
  106. * @addr: address which is checked.
  107. *
  108. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  109. * If @addr is within the kernel stack, it returns true. If not, returns false.
  110. */
  111. static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  112. {
  113. return ((addr & ~(THREAD_SIZE - 1)) ==
  114. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1))) ||
  115. on_irq_stack(addr, sizeof(unsigned long));
  116. }
  117. /**
  118. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  119. * @regs: pt_regs which contains kernel stack pointer.
  120. * @n: stack entry number.
  121. *
  122. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  123. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  124. * this returns 0.
  125. */
  126. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  127. {
  128. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  129. addr += n;
  130. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  131. return *addr;
  132. else
  133. return 0;
  134. }
  135. /*
  136. * TODO: does not yet catch signals sent when the child dies.
  137. * in exit.c or in signal.c.
  138. */
  139. /*
  140. * Called by kernel/ptrace.c when detaching..
  141. */
  142. void ptrace_disable(struct task_struct *child)
  143. {
  144. /*
  145. * This would be better off in core code, but PTRACE_DETACH has
  146. * grown its fair share of arch-specific worts and changing it
  147. * is likely to cause regressions on obscure architectures.
  148. */
  149. user_disable_single_step(child);
  150. }
  151. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  152. /*
  153. * Handle hitting a HW-breakpoint.
  154. */
  155. static void ptrace_hbptriggered(struct perf_event *bp,
  156. struct perf_sample_data *data,
  157. struct pt_regs *regs)
  158. {
  159. struct arch_hw_breakpoint *bkpt = counter_arch_bp(bp);
  160. const char *desc = "Hardware breakpoint trap (ptrace)";
  161. #ifdef CONFIG_COMPAT
  162. if (is_compat_task()) {
  163. int si_errno = 0;
  164. int i;
  165. for (i = 0; i < ARM_MAX_BRP; ++i) {
  166. if (current->thread.debug.hbp_break[i] == bp) {
  167. si_errno = (i << 1) + 1;
  168. break;
  169. }
  170. }
  171. for (i = 0; i < ARM_MAX_WRP; ++i) {
  172. if (current->thread.debug.hbp_watch[i] == bp) {
  173. si_errno = -((i << 1) + 1);
  174. break;
  175. }
  176. }
  177. arm64_force_sig_ptrace_errno_trap(si_errno, bkpt->trigger,
  178. desc);
  179. return;
  180. }
  181. #endif
  182. arm64_force_sig_fault(SIGTRAP, TRAP_HWBKPT, bkpt->trigger, desc);
  183. }
  184. /*
  185. * Unregister breakpoints from this task and reset the pointers in
  186. * the thread_struct.
  187. */
  188. void flush_ptrace_hw_breakpoint(struct task_struct *tsk)
  189. {
  190. int i;
  191. struct thread_struct *t = &tsk->thread;
  192. for (i = 0; i < ARM_MAX_BRP; i++) {
  193. if (t->debug.hbp_break[i]) {
  194. unregister_hw_breakpoint(t->debug.hbp_break[i]);
  195. t->debug.hbp_break[i] = NULL;
  196. }
  197. }
  198. for (i = 0; i < ARM_MAX_WRP; i++) {
  199. if (t->debug.hbp_watch[i]) {
  200. unregister_hw_breakpoint(t->debug.hbp_watch[i]);
  201. t->debug.hbp_watch[i] = NULL;
  202. }
  203. }
  204. }
  205. void ptrace_hw_copy_thread(struct task_struct *tsk)
  206. {
  207. memset(&tsk->thread.debug, 0, sizeof(struct debug_info));
  208. }
  209. static struct perf_event *ptrace_hbp_get_event(unsigned int note_type,
  210. struct task_struct *tsk,
  211. unsigned long idx)
  212. {
  213. struct perf_event *bp = ERR_PTR(-EINVAL);
  214. switch (note_type) {
  215. case NT_ARM_HW_BREAK:
  216. if (idx >= ARM_MAX_BRP)
  217. goto out;
  218. idx = array_index_nospec(idx, ARM_MAX_BRP);
  219. bp = tsk->thread.debug.hbp_break[idx];
  220. break;
  221. case NT_ARM_HW_WATCH:
  222. if (idx >= ARM_MAX_WRP)
  223. goto out;
  224. idx = array_index_nospec(idx, ARM_MAX_WRP);
  225. bp = tsk->thread.debug.hbp_watch[idx];
  226. break;
  227. }
  228. out:
  229. return bp;
  230. }
  231. static int ptrace_hbp_set_event(unsigned int note_type,
  232. struct task_struct *tsk,
  233. unsigned long idx,
  234. struct perf_event *bp)
  235. {
  236. int err = -EINVAL;
  237. switch (note_type) {
  238. case NT_ARM_HW_BREAK:
  239. if (idx >= ARM_MAX_BRP)
  240. goto out;
  241. idx = array_index_nospec(idx, ARM_MAX_BRP);
  242. tsk->thread.debug.hbp_break[idx] = bp;
  243. err = 0;
  244. break;
  245. case NT_ARM_HW_WATCH:
  246. if (idx >= ARM_MAX_WRP)
  247. goto out;
  248. idx = array_index_nospec(idx, ARM_MAX_WRP);
  249. tsk->thread.debug.hbp_watch[idx] = bp;
  250. err = 0;
  251. break;
  252. }
  253. out:
  254. return err;
  255. }
  256. static struct perf_event *ptrace_hbp_create(unsigned int note_type,
  257. struct task_struct *tsk,
  258. unsigned long idx)
  259. {
  260. struct perf_event *bp;
  261. struct perf_event_attr attr;
  262. int err, type;
  263. switch (note_type) {
  264. case NT_ARM_HW_BREAK:
  265. type = HW_BREAKPOINT_X;
  266. break;
  267. case NT_ARM_HW_WATCH:
  268. type = HW_BREAKPOINT_RW;
  269. break;
  270. default:
  271. return ERR_PTR(-EINVAL);
  272. }
  273. ptrace_breakpoint_init(&attr);
  274. /*
  275. * Initialise fields to sane defaults
  276. * (i.e. values that will pass validation).
  277. */
  278. attr.bp_addr = 0;
  279. attr.bp_len = HW_BREAKPOINT_LEN_4;
  280. attr.bp_type = type;
  281. attr.disabled = 1;
  282. bp = register_user_hw_breakpoint(&attr, ptrace_hbptriggered, NULL, tsk);
  283. if (IS_ERR(bp))
  284. return bp;
  285. err = ptrace_hbp_set_event(note_type, tsk, idx, bp);
  286. if (err)
  287. return ERR_PTR(err);
  288. return bp;
  289. }
  290. static int ptrace_hbp_fill_attr_ctrl(unsigned int note_type,
  291. struct arch_hw_breakpoint_ctrl ctrl,
  292. struct perf_event_attr *attr)
  293. {
  294. int err, len, type, offset, disabled = !ctrl.enabled;
  295. attr->disabled = disabled;
  296. if (disabled)
  297. return 0;
  298. err = arch_bp_generic_fields(ctrl, &len, &type, &offset);
  299. if (err)
  300. return err;
  301. switch (note_type) {
  302. case NT_ARM_HW_BREAK:
  303. if ((type & HW_BREAKPOINT_X) != type)
  304. return -EINVAL;
  305. break;
  306. case NT_ARM_HW_WATCH:
  307. if ((type & HW_BREAKPOINT_RW) != type)
  308. return -EINVAL;
  309. break;
  310. default:
  311. return -EINVAL;
  312. }
  313. attr->bp_len = len;
  314. attr->bp_type = type;
  315. attr->bp_addr += offset;
  316. return 0;
  317. }
  318. static int ptrace_hbp_get_resource_info(unsigned int note_type, u32 *info)
  319. {
  320. u8 num;
  321. u32 reg = 0;
  322. switch (note_type) {
  323. case NT_ARM_HW_BREAK:
  324. num = hw_breakpoint_slots(TYPE_INST);
  325. break;
  326. case NT_ARM_HW_WATCH:
  327. num = hw_breakpoint_slots(TYPE_DATA);
  328. break;
  329. default:
  330. return -EINVAL;
  331. }
  332. reg |= debug_monitors_arch();
  333. reg <<= 8;
  334. reg |= num;
  335. *info = reg;
  336. return 0;
  337. }
  338. static int ptrace_hbp_get_ctrl(unsigned int note_type,
  339. struct task_struct *tsk,
  340. unsigned long idx,
  341. u32 *ctrl)
  342. {
  343. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  344. if (IS_ERR(bp))
  345. return PTR_ERR(bp);
  346. *ctrl = bp ? encode_ctrl_reg(counter_arch_bp(bp)->ctrl) : 0;
  347. return 0;
  348. }
  349. static int ptrace_hbp_get_addr(unsigned int note_type,
  350. struct task_struct *tsk,
  351. unsigned long idx,
  352. u64 *addr)
  353. {
  354. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  355. if (IS_ERR(bp))
  356. return PTR_ERR(bp);
  357. *addr = bp ? counter_arch_bp(bp)->address : 0;
  358. return 0;
  359. }
  360. static struct perf_event *ptrace_hbp_get_initialised_bp(unsigned int note_type,
  361. struct task_struct *tsk,
  362. unsigned long idx)
  363. {
  364. struct perf_event *bp = ptrace_hbp_get_event(note_type, tsk, idx);
  365. if (!bp)
  366. bp = ptrace_hbp_create(note_type, tsk, idx);
  367. return bp;
  368. }
  369. static int ptrace_hbp_set_ctrl(unsigned int note_type,
  370. struct task_struct *tsk,
  371. unsigned long idx,
  372. u32 uctrl)
  373. {
  374. int err;
  375. struct perf_event *bp;
  376. struct perf_event_attr attr;
  377. struct arch_hw_breakpoint_ctrl ctrl;
  378. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  379. if (IS_ERR(bp)) {
  380. err = PTR_ERR(bp);
  381. return err;
  382. }
  383. attr = bp->attr;
  384. decode_ctrl_reg(uctrl, &ctrl);
  385. err = ptrace_hbp_fill_attr_ctrl(note_type, ctrl, &attr);
  386. if (err)
  387. return err;
  388. return modify_user_hw_breakpoint(bp, &attr);
  389. }
  390. static int ptrace_hbp_set_addr(unsigned int note_type,
  391. struct task_struct *tsk,
  392. unsigned long idx,
  393. u64 addr)
  394. {
  395. int err;
  396. struct perf_event *bp;
  397. struct perf_event_attr attr;
  398. bp = ptrace_hbp_get_initialised_bp(note_type, tsk, idx);
  399. if (IS_ERR(bp)) {
  400. err = PTR_ERR(bp);
  401. return err;
  402. }
  403. attr = bp->attr;
  404. attr.bp_addr = addr;
  405. err = modify_user_hw_breakpoint(bp, &attr);
  406. return err;
  407. }
  408. #define PTRACE_HBP_ADDR_SZ sizeof(u64)
  409. #define PTRACE_HBP_CTRL_SZ sizeof(u32)
  410. #define PTRACE_HBP_PAD_SZ sizeof(u32)
  411. static int hw_break_get(struct task_struct *target,
  412. const struct user_regset *regset,
  413. struct membuf to)
  414. {
  415. unsigned int note_type = regset->core_note_type;
  416. int ret, idx = 0;
  417. u32 info, ctrl;
  418. u64 addr;
  419. /* Resource info */
  420. ret = ptrace_hbp_get_resource_info(note_type, &info);
  421. if (ret)
  422. return ret;
  423. membuf_write(&to, &info, sizeof(info));
  424. membuf_zero(&to, sizeof(u32));
  425. /* (address, ctrl) registers */
  426. while (to.left) {
  427. ret = ptrace_hbp_get_addr(note_type, target, idx, &addr);
  428. if (ret)
  429. return ret;
  430. ret = ptrace_hbp_get_ctrl(note_type, target, idx, &ctrl);
  431. if (ret)
  432. return ret;
  433. membuf_store(&to, addr);
  434. membuf_store(&to, ctrl);
  435. membuf_zero(&to, sizeof(u32));
  436. idx++;
  437. }
  438. return 0;
  439. }
  440. static int hw_break_set(struct task_struct *target,
  441. const struct user_regset *regset,
  442. unsigned int pos, unsigned int count,
  443. const void *kbuf, const void __user *ubuf)
  444. {
  445. unsigned int note_type = regset->core_note_type;
  446. int ret, idx = 0, offset, limit;
  447. u32 ctrl;
  448. u64 addr;
  449. /* Resource info and pad */
  450. offset = offsetof(struct user_hwdebug_state, dbg_regs);
  451. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf, 0, offset);
  452. if (ret)
  453. return ret;
  454. /* (address, ctrl) registers */
  455. limit = regset->n * regset->size;
  456. while (count && offset < limit) {
  457. if (count < PTRACE_HBP_ADDR_SZ)
  458. return -EINVAL;
  459. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &addr,
  460. offset, offset + PTRACE_HBP_ADDR_SZ);
  461. if (ret)
  462. return ret;
  463. ret = ptrace_hbp_set_addr(note_type, target, idx, addr);
  464. if (ret)
  465. return ret;
  466. offset += PTRACE_HBP_ADDR_SZ;
  467. if (!count)
  468. break;
  469. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl,
  470. offset, offset + PTRACE_HBP_CTRL_SZ);
  471. if (ret)
  472. return ret;
  473. ret = ptrace_hbp_set_ctrl(note_type, target, idx, ctrl);
  474. if (ret)
  475. return ret;
  476. offset += PTRACE_HBP_CTRL_SZ;
  477. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  478. offset,
  479. offset + PTRACE_HBP_PAD_SZ);
  480. if (ret)
  481. return ret;
  482. offset += PTRACE_HBP_PAD_SZ;
  483. idx++;
  484. }
  485. return 0;
  486. }
  487. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  488. static int gpr_get(struct task_struct *target,
  489. const struct user_regset *regset,
  490. struct membuf to)
  491. {
  492. struct user_pt_regs *uregs = &task_pt_regs(target)->user_regs;
  493. return membuf_write(&to, uregs, sizeof(*uregs));
  494. }
  495. static int gpr_set(struct task_struct *target, const struct user_regset *regset,
  496. unsigned int pos, unsigned int count,
  497. const void *kbuf, const void __user *ubuf)
  498. {
  499. int ret;
  500. struct user_pt_regs newregs = task_pt_regs(target)->user_regs;
  501. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newregs, 0, -1);
  502. if (ret)
  503. return ret;
  504. if (!valid_user_regs(&newregs, target))
  505. return -EINVAL;
  506. task_pt_regs(target)->user_regs = newregs;
  507. return 0;
  508. }
  509. static int fpr_active(struct task_struct *target, const struct user_regset *regset)
  510. {
  511. if (!system_supports_fpsimd())
  512. return -ENODEV;
  513. return regset->n;
  514. }
  515. /*
  516. * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
  517. */
  518. static int __fpr_get(struct task_struct *target,
  519. const struct user_regset *regset,
  520. struct membuf to)
  521. {
  522. struct user_fpsimd_state *uregs;
  523. sve_sync_to_fpsimd(target);
  524. uregs = &target->thread.uw.fpsimd_state;
  525. return membuf_write(&to, uregs, sizeof(*uregs));
  526. }
  527. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  528. struct membuf to)
  529. {
  530. if (!system_supports_fpsimd())
  531. return -EINVAL;
  532. if (target == current)
  533. fpsimd_preserve_current_state();
  534. return __fpr_get(target, regset, to);
  535. }
  536. static int __fpr_set(struct task_struct *target,
  537. const struct user_regset *regset,
  538. unsigned int pos, unsigned int count,
  539. const void *kbuf, const void __user *ubuf,
  540. unsigned int start_pos)
  541. {
  542. int ret;
  543. struct user_fpsimd_state newstate;
  544. /*
  545. * Ensure target->thread.uw.fpsimd_state is up to date, so that a
  546. * short copyin can't resurrect stale data.
  547. */
  548. sve_sync_to_fpsimd(target);
  549. newstate = target->thread.uw.fpsimd_state;
  550. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
  551. start_pos, start_pos + sizeof(newstate));
  552. if (ret)
  553. return ret;
  554. target->thread.uw.fpsimd_state = newstate;
  555. return ret;
  556. }
  557. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  558. unsigned int pos, unsigned int count,
  559. const void *kbuf, const void __user *ubuf)
  560. {
  561. int ret;
  562. if (!system_supports_fpsimd())
  563. return -EINVAL;
  564. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
  565. if (ret)
  566. return ret;
  567. sve_sync_from_fpsimd_zeropad(target);
  568. fpsimd_flush_task_state(target);
  569. return ret;
  570. }
  571. static int tls_get(struct task_struct *target, const struct user_regset *regset,
  572. struct membuf to)
  573. {
  574. int ret;
  575. if (target == current)
  576. tls_preserve_current_state();
  577. ret = membuf_store(&to, target->thread.uw.tp_value);
  578. if (system_supports_tpidr2())
  579. ret = membuf_store(&to, target->thread.tpidr2_el0);
  580. else
  581. ret = membuf_zero(&to, sizeof(u64));
  582. return ret;
  583. }
  584. static int tls_set(struct task_struct *target, const struct user_regset *regset,
  585. unsigned int pos, unsigned int count,
  586. const void *kbuf, const void __user *ubuf)
  587. {
  588. int ret;
  589. unsigned long tls[2];
  590. tls[0] = target->thread.uw.tp_value;
  591. if (system_supports_sme())
  592. tls[1] = target->thread.tpidr2_el0;
  593. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, tls, 0, count);
  594. if (ret)
  595. return ret;
  596. target->thread.uw.tp_value = tls[0];
  597. if (system_supports_sme())
  598. target->thread.tpidr2_el0 = tls[1];
  599. return ret;
  600. }
  601. static int system_call_get(struct task_struct *target,
  602. const struct user_regset *regset,
  603. struct membuf to)
  604. {
  605. return membuf_store(&to, task_pt_regs(target)->syscallno);
  606. }
  607. static int system_call_set(struct task_struct *target,
  608. const struct user_regset *regset,
  609. unsigned int pos, unsigned int count,
  610. const void *kbuf, const void __user *ubuf)
  611. {
  612. int syscallno = task_pt_regs(target)->syscallno;
  613. int ret;
  614. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
  615. if (ret)
  616. return ret;
  617. task_pt_regs(target)->syscallno = syscallno;
  618. return ret;
  619. }
  620. #ifdef CONFIG_ARM64_SVE
  621. static void sve_init_header_from_task(struct user_sve_header *header,
  622. struct task_struct *target,
  623. enum vec_type type)
  624. {
  625. unsigned int vq;
  626. bool active;
  627. bool fpsimd_only;
  628. enum vec_type task_type;
  629. memset(header, 0, sizeof(*header));
  630. /* Check if the requested registers are active for the task */
  631. if (thread_sm_enabled(&target->thread))
  632. task_type = ARM64_VEC_SME;
  633. else
  634. task_type = ARM64_VEC_SVE;
  635. active = (task_type == type);
  636. switch (type) {
  637. case ARM64_VEC_SVE:
  638. if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
  639. header->flags |= SVE_PT_VL_INHERIT;
  640. fpsimd_only = !test_tsk_thread_flag(target, TIF_SVE);
  641. break;
  642. case ARM64_VEC_SME:
  643. if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT))
  644. header->flags |= SVE_PT_VL_INHERIT;
  645. fpsimd_only = false;
  646. break;
  647. default:
  648. WARN_ON_ONCE(1);
  649. return;
  650. }
  651. if (active) {
  652. if (fpsimd_only) {
  653. header->flags |= SVE_PT_REGS_FPSIMD;
  654. } else {
  655. header->flags |= SVE_PT_REGS_SVE;
  656. }
  657. }
  658. header->vl = task_get_vl(target, type);
  659. vq = sve_vq_from_vl(header->vl);
  660. header->max_vl = vec_max_vl(type);
  661. header->size = SVE_PT_SIZE(vq, header->flags);
  662. header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
  663. SVE_PT_REGS_SVE);
  664. }
  665. static unsigned int sve_size_from_header(struct user_sve_header const *header)
  666. {
  667. return ALIGN(header->size, SVE_VQ_BYTES);
  668. }
  669. static int sve_get_common(struct task_struct *target,
  670. const struct user_regset *regset,
  671. struct membuf to,
  672. enum vec_type type)
  673. {
  674. struct user_sve_header header;
  675. unsigned int vq;
  676. unsigned long start, end;
  677. /* Header */
  678. sve_init_header_from_task(&header, target, type);
  679. vq = sve_vq_from_vl(header.vl);
  680. membuf_write(&to, &header, sizeof(header));
  681. if (target == current)
  682. fpsimd_preserve_current_state();
  683. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  684. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  685. switch ((header.flags & SVE_PT_REGS_MASK)) {
  686. case SVE_PT_REGS_FPSIMD:
  687. return __fpr_get(target, regset, to);
  688. case SVE_PT_REGS_SVE:
  689. start = SVE_PT_SVE_OFFSET;
  690. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  691. membuf_write(&to, target->thread.sve_state, end - start);
  692. start = end;
  693. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  694. membuf_zero(&to, end - start);
  695. /*
  696. * Copy fpsr, and fpcr which must follow contiguously in
  697. * struct fpsimd_state:
  698. */
  699. start = end;
  700. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  701. membuf_write(&to, &target->thread.uw.fpsimd_state.fpsr,
  702. end - start);
  703. start = end;
  704. end = sve_size_from_header(&header);
  705. return membuf_zero(&to, end - start);
  706. default:
  707. return 0;
  708. }
  709. }
  710. static int sve_get(struct task_struct *target,
  711. const struct user_regset *regset,
  712. struct membuf to)
  713. {
  714. if (!system_supports_sve())
  715. return -EINVAL;
  716. return sve_get_common(target, regset, to, ARM64_VEC_SVE);
  717. }
  718. static int sve_set_common(struct task_struct *target,
  719. const struct user_regset *regset,
  720. unsigned int pos, unsigned int count,
  721. const void *kbuf, const void __user *ubuf,
  722. enum vec_type type)
  723. {
  724. int ret;
  725. struct user_sve_header header;
  726. unsigned int vq;
  727. unsigned long start, end;
  728. /* Header */
  729. if (count < sizeof(header))
  730. return -EINVAL;
  731. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
  732. 0, sizeof(header));
  733. if (ret)
  734. goto out;
  735. /*
  736. * Apart from SVE_PT_REGS_MASK, all SVE_PT_* flags are consumed by
  737. * vec_set_vector_length(), which will also validate them for us:
  738. */
  739. ret = vec_set_vector_length(target, type, header.vl,
  740. ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
  741. if (ret)
  742. goto out;
  743. /* Actual VL set may be less than the user asked for: */
  744. vq = sve_vq_from_vl(task_get_vl(target, type));
  745. /* Enter/exit streaming mode */
  746. if (system_supports_sme()) {
  747. u64 old_svcr = target->thread.svcr;
  748. switch (type) {
  749. case ARM64_VEC_SVE:
  750. target->thread.svcr &= ~SVCR_SM_MASK;
  751. break;
  752. case ARM64_VEC_SME:
  753. target->thread.svcr |= SVCR_SM_MASK;
  754. /*
  755. * Disable traps and ensure there is SME storage but
  756. * preserve any currently set values in ZA/ZT.
  757. */
  758. sme_alloc(target, false);
  759. set_tsk_thread_flag(target, TIF_SME);
  760. break;
  761. default:
  762. WARN_ON_ONCE(1);
  763. ret = -EINVAL;
  764. goto out;
  765. }
  766. /*
  767. * If we switched then invalidate any existing SVE
  768. * state and ensure there's storage.
  769. */
  770. if (target->thread.svcr != old_svcr)
  771. sve_alloc(target, true);
  772. }
  773. /* Registers: FPSIMD-only case */
  774. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  775. if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
  776. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
  777. SVE_PT_FPSIMD_OFFSET);
  778. clear_tsk_thread_flag(target, TIF_SVE);
  779. if (type == ARM64_VEC_SME)
  780. fpsimd_force_sync_to_sve(target);
  781. goto out;
  782. }
  783. /*
  784. * Otherwise: no registers or full SVE case. For backwards
  785. * compatibility reasons we treat empty flags as SVE registers.
  786. */
  787. /*
  788. * If setting a different VL from the requested VL and there is
  789. * register data, the data layout will be wrong: don't even
  790. * try to set the registers in this case.
  791. */
  792. if (count && vq != sve_vq_from_vl(header.vl)) {
  793. ret = -EIO;
  794. goto out;
  795. }
  796. sve_alloc(target, true);
  797. if (!target->thread.sve_state) {
  798. ret = -ENOMEM;
  799. clear_tsk_thread_flag(target, TIF_SVE);
  800. goto out;
  801. }
  802. /*
  803. * Ensure target->thread.sve_state is up to date with target's
  804. * FPSIMD regs, so that a short copyin leaves trailing
  805. * registers unmodified. Only enable SVE if we are
  806. * configuring normal SVE, a system with streaming SVE may not
  807. * have normal SVE.
  808. */
  809. fpsimd_sync_to_sve(target);
  810. if (type == ARM64_VEC_SVE)
  811. set_tsk_thread_flag(target, TIF_SVE);
  812. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  813. start = SVE_PT_SVE_OFFSET;
  814. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  815. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  816. target->thread.sve_state,
  817. start, end);
  818. if (ret)
  819. goto out;
  820. start = end;
  821. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  822. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  823. start, end);
  824. if (ret)
  825. goto out;
  826. /*
  827. * Copy fpsr, and fpcr which must follow contiguously in
  828. * struct fpsimd_state:
  829. */
  830. start = end;
  831. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  832. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  833. &target->thread.uw.fpsimd_state.fpsr,
  834. start, end);
  835. out:
  836. fpsimd_flush_task_state(target);
  837. return ret;
  838. }
  839. static int sve_set(struct task_struct *target,
  840. const struct user_regset *regset,
  841. unsigned int pos, unsigned int count,
  842. const void *kbuf, const void __user *ubuf)
  843. {
  844. if (!system_supports_sve())
  845. return -EINVAL;
  846. return sve_set_common(target, regset, pos, count, kbuf, ubuf,
  847. ARM64_VEC_SVE);
  848. }
  849. #endif /* CONFIG_ARM64_SVE */
  850. #ifdef CONFIG_ARM64_SME
  851. static int ssve_get(struct task_struct *target,
  852. const struct user_regset *regset,
  853. struct membuf to)
  854. {
  855. if (!system_supports_sme())
  856. return -EINVAL;
  857. return sve_get_common(target, regset, to, ARM64_VEC_SME);
  858. }
  859. static int ssve_set(struct task_struct *target,
  860. const struct user_regset *regset,
  861. unsigned int pos, unsigned int count,
  862. const void *kbuf, const void __user *ubuf)
  863. {
  864. if (!system_supports_sme())
  865. return -EINVAL;
  866. return sve_set_common(target, regset, pos, count, kbuf, ubuf,
  867. ARM64_VEC_SME);
  868. }
  869. static int za_get(struct task_struct *target,
  870. const struct user_regset *regset,
  871. struct membuf to)
  872. {
  873. struct user_za_header header;
  874. unsigned int vq;
  875. unsigned long start, end;
  876. if (!system_supports_sme())
  877. return -EINVAL;
  878. /* Header */
  879. memset(&header, 0, sizeof(header));
  880. if (test_tsk_thread_flag(target, TIF_SME_VL_INHERIT))
  881. header.flags |= ZA_PT_VL_INHERIT;
  882. header.vl = task_get_sme_vl(target);
  883. vq = sve_vq_from_vl(header.vl);
  884. header.max_vl = sme_max_vl();
  885. header.max_size = ZA_PT_SIZE(vq);
  886. /* If ZA is not active there is only the header */
  887. if (thread_za_enabled(&target->thread))
  888. header.size = ZA_PT_SIZE(vq);
  889. else
  890. header.size = ZA_PT_ZA_OFFSET;
  891. membuf_write(&to, &header, sizeof(header));
  892. BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header));
  893. end = ZA_PT_ZA_OFFSET;
  894. if (target == current)
  895. fpsimd_preserve_current_state();
  896. /* Any register data to include? */
  897. if (thread_za_enabled(&target->thread)) {
  898. start = end;
  899. end = ZA_PT_SIZE(vq);
  900. membuf_write(&to, target->thread.za_state, end - start);
  901. }
  902. /* Zero any trailing padding */
  903. start = end;
  904. end = ALIGN(header.size, SVE_VQ_BYTES);
  905. return membuf_zero(&to, end - start);
  906. }
  907. static int za_set(struct task_struct *target,
  908. const struct user_regset *regset,
  909. unsigned int pos, unsigned int count,
  910. const void *kbuf, const void __user *ubuf)
  911. {
  912. int ret;
  913. struct user_za_header header;
  914. unsigned int vq;
  915. unsigned long start, end;
  916. if (!system_supports_sme())
  917. return -EINVAL;
  918. /* Header */
  919. if (count < sizeof(header))
  920. return -EINVAL;
  921. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
  922. 0, sizeof(header));
  923. if (ret)
  924. goto out;
  925. /*
  926. * All current ZA_PT_* flags are consumed by
  927. * vec_set_vector_length(), which will also validate them for
  928. * us:
  929. */
  930. ret = vec_set_vector_length(target, ARM64_VEC_SME, header.vl,
  931. ((unsigned long)header.flags) << 16);
  932. if (ret)
  933. goto out;
  934. /* Actual VL set may be less than the user asked for: */
  935. vq = sve_vq_from_vl(task_get_sme_vl(target));
  936. /* Ensure there is some SVE storage for streaming mode */
  937. if (!target->thread.sve_state) {
  938. sve_alloc(target, false);
  939. if (!target->thread.sve_state) {
  940. ret = -ENOMEM;
  941. goto out;
  942. }
  943. }
  944. /* Allocate/reinit ZA storage */
  945. sme_alloc(target, true);
  946. if (!target->thread.za_state) {
  947. ret = -ENOMEM;
  948. goto out;
  949. }
  950. /* If there is no data then disable ZA */
  951. if (!count) {
  952. target->thread.svcr &= ~SVCR_ZA_MASK;
  953. goto out;
  954. }
  955. /*
  956. * If setting a different VL from the requested VL and there is
  957. * register data, the data layout will be wrong: don't even
  958. * try to set the registers in this case.
  959. */
  960. if (vq != sve_vq_from_vl(header.vl)) {
  961. ret = -EIO;
  962. goto out;
  963. }
  964. BUILD_BUG_ON(ZA_PT_ZA_OFFSET != sizeof(header));
  965. start = ZA_PT_ZA_OFFSET;
  966. end = ZA_PT_SIZE(vq);
  967. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  968. target->thread.za_state,
  969. start, end);
  970. if (ret)
  971. goto out;
  972. /* Mark ZA as active and let userspace use it */
  973. set_tsk_thread_flag(target, TIF_SME);
  974. target->thread.svcr |= SVCR_ZA_MASK;
  975. out:
  976. fpsimd_flush_task_state(target);
  977. return ret;
  978. }
  979. #endif /* CONFIG_ARM64_SME */
  980. #ifdef CONFIG_ARM64_PTR_AUTH
  981. static int pac_mask_get(struct task_struct *target,
  982. const struct user_regset *regset,
  983. struct membuf to)
  984. {
  985. /*
  986. * The PAC bits can differ across data and instruction pointers
  987. * depending on TCR_EL1.TBID*, which we may make use of in future, so
  988. * we expose separate masks.
  989. */
  990. unsigned long mask = ptrauth_user_pac_mask();
  991. struct user_pac_mask uregs = {
  992. .data_mask = mask,
  993. .insn_mask = mask,
  994. };
  995. if (!system_supports_address_auth())
  996. return -EINVAL;
  997. return membuf_write(&to, &uregs, sizeof(uregs));
  998. }
  999. static int pac_enabled_keys_get(struct task_struct *target,
  1000. const struct user_regset *regset,
  1001. struct membuf to)
  1002. {
  1003. long enabled_keys = ptrauth_get_enabled_keys(target);
  1004. if (IS_ERR_VALUE(enabled_keys))
  1005. return enabled_keys;
  1006. return membuf_write(&to, &enabled_keys, sizeof(enabled_keys));
  1007. }
  1008. static int pac_enabled_keys_set(struct task_struct *target,
  1009. const struct user_regset *regset,
  1010. unsigned int pos, unsigned int count,
  1011. const void *kbuf, const void __user *ubuf)
  1012. {
  1013. int ret;
  1014. long enabled_keys = ptrauth_get_enabled_keys(target);
  1015. if (IS_ERR_VALUE(enabled_keys))
  1016. return enabled_keys;
  1017. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &enabled_keys, 0,
  1018. sizeof(long));
  1019. if (ret)
  1020. return ret;
  1021. return ptrauth_set_enabled_keys(target, PR_PAC_ENABLED_KEYS_MASK,
  1022. enabled_keys);
  1023. }
  1024. #ifdef CONFIG_CHECKPOINT_RESTORE
  1025. static __uint128_t pac_key_to_user(const struct ptrauth_key *key)
  1026. {
  1027. return (__uint128_t)key->hi << 64 | key->lo;
  1028. }
  1029. static struct ptrauth_key pac_key_from_user(__uint128_t ukey)
  1030. {
  1031. struct ptrauth_key key = {
  1032. .lo = (unsigned long)ukey,
  1033. .hi = (unsigned long)(ukey >> 64),
  1034. };
  1035. return key;
  1036. }
  1037. static void pac_address_keys_to_user(struct user_pac_address_keys *ukeys,
  1038. const struct ptrauth_keys_user *keys)
  1039. {
  1040. ukeys->apiakey = pac_key_to_user(&keys->apia);
  1041. ukeys->apibkey = pac_key_to_user(&keys->apib);
  1042. ukeys->apdakey = pac_key_to_user(&keys->apda);
  1043. ukeys->apdbkey = pac_key_to_user(&keys->apdb);
  1044. }
  1045. static void pac_address_keys_from_user(struct ptrauth_keys_user *keys,
  1046. const struct user_pac_address_keys *ukeys)
  1047. {
  1048. keys->apia = pac_key_from_user(ukeys->apiakey);
  1049. keys->apib = pac_key_from_user(ukeys->apibkey);
  1050. keys->apda = pac_key_from_user(ukeys->apdakey);
  1051. keys->apdb = pac_key_from_user(ukeys->apdbkey);
  1052. }
  1053. static int pac_address_keys_get(struct task_struct *target,
  1054. const struct user_regset *regset,
  1055. struct membuf to)
  1056. {
  1057. struct ptrauth_keys_user *keys = &target->thread.keys_user;
  1058. struct user_pac_address_keys user_keys;
  1059. if (!system_supports_address_auth())
  1060. return -EINVAL;
  1061. pac_address_keys_to_user(&user_keys, keys);
  1062. return membuf_write(&to, &user_keys, sizeof(user_keys));
  1063. }
  1064. static int pac_address_keys_set(struct task_struct *target,
  1065. const struct user_regset *regset,
  1066. unsigned int pos, unsigned int count,
  1067. const void *kbuf, const void __user *ubuf)
  1068. {
  1069. struct ptrauth_keys_user *keys = &target->thread.keys_user;
  1070. struct user_pac_address_keys user_keys;
  1071. int ret;
  1072. if (!system_supports_address_auth())
  1073. return -EINVAL;
  1074. pac_address_keys_to_user(&user_keys, keys);
  1075. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1076. &user_keys, 0, -1);
  1077. if (ret)
  1078. return ret;
  1079. pac_address_keys_from_user(keys, &user_keys);
  1080. return 0;
  1081. }
  1082. static void pac_generic_keys_to_user(struct user_pac_generic_keys *ukeys,
  1083. const struct ptrauth_keys_user *keys)
  1084. {
  1085. ukeys->apgakey = pac_key_to_user(&keys->apga);
  1086. }
  1087. static void pac_generic_keys_from_user(struct ptrauth_keys_user *keys,
  1088. const struct user_pac_generic_keys *ukeys)
  1089. {
  1090. keys->apga = pac_key_from_user(ukeys->apgakey);
  1091. }
  1092. static int pac_generic_keys_get(struct task_struct *target,
  1093. const struct user_regset *regset,
  1094. struct membuf to)
  1095. {
  1096. struct ptrauth_keys_user *keys = &target->thread.keys_user;
  1097. struct user_pac_generic_keys user_keys;
  1098. if (!system_supports_generic_auth())
  1099. return -EINVAL;
  1100. pac_generic_keys_to_user(&user_keys, keys);
  1101. return membuf_write(&to, &user_keys, sizeof(user_keys));
  1102. }
  1103. static int pac_generic_keys_set(struct task_struct *target,
  1104. const struct user_regset *regset,
  1105. unsigned int pos, unsigned int count,
  1106. const void *kbuf, const void __user *ubuf)
  1107. {
  1108. struct ptrauth_keys_user *keys = &target->thread.keys_user;
  1109. struct user_pac_generic_keys user_keys;
  1110. int ret;
  1111. if (!system_supports_generic_auth())
  1112. return -EINVAL;
  1113. pac_generic_keys_to_user(&user_keys, keys);
  1114. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  1115. &user_keys, 0, -1);
  1116. if (ret)
  1117. return ret;
  1118. pac_generic_keys_from_user(keys, &user_keys);
  1119. return 0;
  1120. }
  1121. #endif /* CONFIG_CHECKPOINT_RESTORE */
  1122. #endif /* CONFIG_ARM64_PTR_AUTH */
  1123. #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
  1124. static int tagged_addr_ctrl_get(struct task_struct *target,
  1125. const struct user_regset *regset,
  1126. struct membuf to)
  1127. {
  1128. long ctrl = get_tagged_addr_ctrl(target);
  1129. if (IS_ERR_VALUE(ctrl))
  1130. return ctrl;
  1131. return membuf_write(&to, &ctrl, sizeof(ctrl));
  1132. }
  1133. static int tagged_addr_ctrl_set(struct task_struct *target, const struct
  1134. user_regset *regset, unsigned int pos,
  1135. unsigned int count, const void *kbuf, const
  1136. void __user *ubuf)
  1137. {
  1138. int ret;
  1139. long ctrl;
  1140. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ctrl, 0, -1);
  1141. if (ret)
  1142. return ret;
  1143. return set_tagged_addr_ctrl(target, ctrl);
  1144. }
  1145. #endif
  1146. enum aarch64_regset {
  1147. REGSET_GPR,
  1148. REGSET_FPR,
  1149. REGSET_TLS,
  1150. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1151. REGSET_HW_BREAK,
  1152. REGSET_HW_WATCH,
  1153. #endif
  1154. REGSET_SYSTEM_CALL,
  1155. #ifdef CONFIG_ARM64_SVE
  1156. REGSET_SVE,
  1157. #endif
  1158. #ifdef CONFIG_ARM64_SME
  1159. REGSET_SSVE,
  1160. REGSET_ZA,
  1161. #endif
  1162. #ifdef CONFIG_ARM64_PTR_AUTH
  1163. REGSET_PAC_MASK,
  1164. REGSET_PAC_ENABLED_KEYS,
  1165. #ifdef CONFIG_CHECKPOINT_RESTORE
  1166. REGSET_PACA_KEYS,
  1167. REGSET_PACG_KEYS,
  1168. #endif
  1169. #endif
  1170. #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
  1171. REGSET_TAGGED_ADDR_CTRL,
  1172. #endif
  1173. };
  1174. static const struct user_regset aarch64_regsets[] = {
  1175. [REGSET_GPR] = {
  1176. .core_note_type = NT_PRSTATUS,
  1177. .n = sizeof(struct user_pt_regs) / sizeof(u64),
  1178. .size = sizeof(u64),
  1179. .align = sizeof(u64),
  1180. .regset_get = gpr_get,
  1181. .set = gpr_set
  1182. },
  1183. [REGSET_FPR] = {
  1184. .core_note_type = NT_PRFPREG,
  1185. .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
  1186. /*
  1187. * We pretend we have 32-bit registers because the fpsr and
  1188. * fpcr are 32-bits wide.
  1189. */
  1190. .size = sizeof(u32),
  1191. .align = sizeof(u32),
  1192. .active = fpr_active,
  1193. .regset_get = fpr_get,
  1194. .set = fpr_set
  1195. },
  1196. [REGSET_TLS] = {
  1197. .core_note_type = NT_ARM_TLS,
  1198. .n = 2,
  1199. .size = sizeof(void *),
  1200. .align = sizeof(void *),
  1201. .regset_get = tls_get,
  1202. .set = tls_set,
  1203. },
  1204. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1205. [REGSET_HW_BREAK] = {
  1206. .core_note_type = NT_ARM_HW_BREAK,
  1207. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1208. .size = sizeof(u32),
  1209. .align = sizeof(u32),
  1210. .regset_get = hw_break_get,
  1211. .set = hw_break_set,
  1212. },
  1213. [REGSET_HW_WATCH] = {
  1214. .core_note_type = NT_ARM_HW_WATCH,
  1215. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1216. .size = sizeof(u32),
  1217. .align = sizeof(u32),
  1218. .regset_get = hw_break_get,
  1219. .set = hw_break_set,
  1220. },
  1221. #endif
  1222. [REGSET_SYSTEM_CALL] = {
  1223. .core_note_type = NT_ARM_SYSTEM_CALL,
  1224. .n = 1,
  1225. .size = sizeof(int),
  1226. .align = sizeof(int),
  1227. .regset_get = system_call_get,
  1228. .set = system_call_set,
  1229. },
  1230. #ifdef CONFIG_ARM64_SVE
  1231. [REGSET_SVE] = { /* Scalable Vector Extension */
  1232. .core_note_type = NT_ARM_SVE,
  1233. .n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
  1234. SVE_VQ_BYTES),
  1235. .size = SVE_VQ_BYTES,
  1236. .align = SVE_VQ_BYTES,
  1237. .regset_get = sve_get,
  1238. .set = sve_set,
  1239. },
  1240. #endif
  1241. #ifdef CONFIG_ARM64_SME
  1242. [REGSET_SSVE] = { /* Streaming mode SVE */
  1243. .core_note_type = NT_ARM_SSVE,
  1244. .n = DIV_ROUND_UP(SVE_PT_SIZE(SME_VQ_MAX, SVE_PT_REGS_SVE),
  1245. SVE_VQ_BYTES),
  1246. .size = SVE_VQ_BYTES,
  1247. .align = SVE_VQ_BYTES,
  1248. .regset_get = ssve_get,
  1249. .set = ssve_set,
  1250. },
  1251. [REGSET_ZA] = { /* SME ZA */
  1252. .core_note_type = NT_ARM_ZA,
  1253. /*
  1254. * ZA is a single register but it's variably sized and
  1255. * the ptrace core requires that the size of any data
  1256. * be an exact multiple of the configured register
  1257. * size so report as though we had SVE_VQ_BYTES
  1258. * registers. These values aren't exposed to
  1259. * userspace.
  1260. */
  1261. .n = DIV_ROUND_UP(ZA_PT_SIZE(SME_VQ_MAX), SVE_VQ_BYTES),
  1262. .size = SVE_VQ_BYTES,
  1263. .align = SVE_VQ_BYTES,
  1264. .regset_get = za_get,
  1265. .set = za_set,
  1266. },
  1267. #endif
  1268. #ifdef CONFIG_ARM64_PTR_AUTH
  1269. [REGSET_PAC_MASK] = {
  1270. .core_note_type = NT_ARM_PAC_MASK,
  1271. .n = sizeof(struct user_pac_mask) / sizeof(u64),
  1272. .size = sizeof(u64),
  1273. .align = sizeof(u64),
  1274. .regset_get = pac_mask_get,
  1275. /* this cannot be set dynamically */
  1276. },
  1277. [REGSET_PAC_ENABLED_KEYS] = {
  1278. .core_note_type = NT_ARM_PAC_ENABLED_KEYS,
  1279. .n = 1,
  1280. .size = sizeof(long),
  1281. .align = sizeof(long),
  1282. .regset_get = pac_enabled_keys_get,
  1283. .set = pac_enabled_keys_set,
  1284. },
  1285. #ifdef CONFIG_CHECKPOINT_RESTORE
  1286. [REGSET_PACA_KEYS] = {
  1287. .core_note_type = NT_ARM_PACA_KEYS,
  1288. .n = sizeof(struct user_pac_address_keys) / sizeof(__uint128_t),
  1289. .size = sizeof(__uint128_t),
  1290. .align = sizeof(__uint128_t),
  1291. .regset_get = pac_address_keys_get,
  1292. .set = pac_address_keys_set,
  1293. },
  1294. [REGSET_PACG_KEYS] = {
  1295. .core_note_type = NT_ARM_PACG_KEYS,
  1296. .n = sizeof(struct user_pac_generic_keys) / sizeof(__uint128_t),
  1297. .size = sizeof(__uint128_t),
  1298. .align = sizeof(__uint128_t),
  1299. .regset_get = pac_generic_keys_get,
  1300. .set = pac_generic_keys_set,
  1301. },
  1302. #endif
  1303. #endif
  1304. #ifdef CONFIG_ARM64_TAGGED_ADDR_ABI
  1305. [REGSET_TAGGED_ADDR_CTRL] = {
  1306. .core_note_type = NT_ARM_TAGGED_ADDR_CTRL,
  1307. .n = 1,
  1308. .size = sizeof(long),
  1309. .align = sizeof(long),
  1310. .regset_get = tagged_addr_ctrl_get,
  1311. .set = tagged_addr_ctrl_set,
  1312. },
  1313. #endif
  1314. };
  1315. static const struct user_regset_view user_aarch64_view = {
  1316. .name = "aarch64", .e_machine = EM_AARCH64,
  1317. .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
  1318. };
  1319. #ifdef CONFIG_COMPAT
  1320. enum compat_regset {
  1321. REGSET_COMPAT_GPR,
  1322. REGSET_COMPAT_VFP,
  1323. };
  1324. static inline compat_ulong_t compat_get_user_reg(struct task_struct *task, int idx)
  1325. {
  1326. struct pt_regs *regs = task_pt_regs(task);
  1327. switch (idx) {
  1328. case 15:
  1329. return regs->pc;
  1330. case 16:
  1331. return pstate_to_compat_psr(regs->pstate);
  1332. case 17:
  1333. return regs->orig_x0;
  1334. default:
  1335. return regs->regs[idx];
  1336. }
  1337. }
  1338. static int compat_gpr_get(struct task_struct *target,
  1339. const struct user_regset *regset,
  1340. struct membuf to)
  1341. {
  1342. int i = 0;
  1343. while (to.left)
  1344. membuf_store(&to, compat_get_user_reg(target, i++));
  1345. return 0;
  1346. }
  1347. static int compat_gpr_set(struct task_struct *target,
  1348. const struct user_regset *regset,
  1349. unsigned int pos, unsigned int count,
  1350. const void *kbuf, const void __user *ubuf)
  1351. {
  1352. struct pt_regs newregs;
  1353. int ret = 0;
  1354. unsigned int i, start, num_regs;
  1355. /* Calculate the number of AArch32 registers contained in count */
  1356. num_regs = count / regset->size;
  1357. /* Convert pos into an register number */
  1358. start = pos / regset->size;
  1359. if (start + num_regs > regset->n)
  1360. return -EIO;
  1361. newregs = *task_pt_regs(target);
  1362. for (i = 0; i < num_regs; ++i) {
  1363. unsigned int idx = start + i;
  1364. compat_ulong_t reg;
  1365. if (kbuf) {
  1366. memcpy(&reg, kbuf, sizeof(reg));
  1367. kbuf += sizeof(reg);
  1368. } else {
  1369. ret = copy_from_user(&reg, ubuf, sizeof(reg));
  1370. if (ret) {
  1371. ret = -EFAULT;
  1372. break;
  1373. }
  1374. ubuf += sizeof(reg);
  1375. }
  1376. switch (idx) {
  1377. case 15:
  1378. newregs.pc = reg;
  1379. break;
  1380. case 16:
  1381. reg = compat_psr_to_pstate(reg);
  1382. newregs.pstate = reg;
  1383. break;
  1384. case 17:
  1385. newregs.orig_x0 = reg;
  1386. break;
  1387. default:
  1388. newregs.regs[idx] = reg;
  1389. }
  1390. }
  1391. if (valid_user_regs(&newregs.user_regs, target))
  1392. *task_pt_regs(target) = newregs;
  1393. else
  1394. ret = -EINVAL;
  1395. return ret;
  1396. }
  1397. static int compat_vfp_get(struct task_struct *target,
  1398. const struct user_regset *regset,
  1399. struct membuf to)
  1400. {
  1401. struct user_fpsimd_state *uregs;
  1402. compat_ulong_t fpscr;
  1403. if (!system_supports_fpsimd())
  1404. return -EINVAL;
  1405. uregs = &target->thread.uw.fpsimd_state;
  1406. if (target == current)
  1407. fpsimd_preserve_current_state();
  1408. /*
  1409. * The VFP registers are packed into the fpsimd_state, so they all sit
  1410. * nicely together for us. We just need to create the fpscr separately.
  1411. */
  1412. membuf_write(&to, uregs, VFP_STATE_SIZE - sizeof(compat_ulong_t));
  1413. fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
  1414. (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
  1415. return membuf_store(&to, fpscr);
  1416. }
  1417. static int compat_vfp_set(struct task_struct *target,
  1418. const struct user_regset *regset,
  1419. unsigned int pos, unsigned int count,
  1420. const void *kbuf, const void __user *ubuf)
  1421. {
  1422. struct user_fpsimd_state *uregs;
  1423. compat_ulong_t fpscr;
  1424. int ret, vregs_end_pos;
  1425. if (!system_supports_fpsimd())
  1426. return -EINVAL;
  1427. uregs = &target->thread.uw.fpsimd_state;
  1428. vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
  1429. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  1430. vregs_end_pos);
  1431. if (count && !ret) {
  1432. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
  1433. vregs_end_pos, VFP_STATE_SIZE);
  1434. if (!ret) {
  1435. uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  1436. uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  1437. }
  1438. }
  1439. fpsimd_flush_task_state(target);
  1440. return ret;
  1441. }
  1442. static int compat_tls_get(struct task_struct *target,
  1443. const struct user_regset *regset,
  1444. struct membuf to)
  1445. {
  1446. return membuf_store(&to, (compat_ulong_t)target->thread.uw.tp_value);
  1447. }
  1448. static int compat_tls_set(struct task_struct *target,
  1449. const struct user_regset *regset, unsigned int pos,
  1450. unsigned int count, const void *kbuf,
  1451. const void __user *ubuf)
  1452. {
  1453. int ret;
  1454. compat_ulong_t tls = target->thread.uw.tp_value;
  1455. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  1456. if (ret)
  1457. return ret;
  1458. target->thread.uw.tp_value = tls;
  1459. return ret;
  1460. }
  1461. static const struct user_regset aarch32_regsets[] = {
  1462. [REGSET_COMPAT_GPR] = {
  1463. .core_note_type = NT_PRSTATUS,
  1464. .n = COMPAT_ELF_NGREG,
  1465. .size = sizeof(compat_elf_greg_t),
  1466. .align = sizeof(compat_elf_greg_t),
  1467. .regset_get = compat_gpr_get,
  1468. .set = compat_gpr_set
  1469. },
  1470. [REGSET_COMPAT_VFP] = {
  1471. .core_note_type = NT_ARM_VFP,
  1472. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1473. .size = sizeof(compat_ulong_t),
  1474. .align = sizeof(compat_ulong_t),
  1475. .active = fpr_active,
  1476. .regset_get = compat_vfp_get,
  1477. .set = compat_vfp_set
  1478. },
  1479. };
  1480. static const struct user_regset_view user_aarch32_view = {
  1481. .name = "aarch32", .e_machine = EM_ARM,
  1482. .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
  1483. };
  1484. static const struct user_regset aarch32_ptrace_regsets[] = {
  1485. [REGSET_GPR] = {
  1486. .core_note_type = NT_PRSTATUS,
  1487. .n = COMPAT_ELF_NGREG,
  1488. .size = sizeof(compat_elf_greg_t),
  1489. .align = sizeof(compat_elf_greg_t),
  1490. .regset_get = compat_gpr_get,
  1491. .set = compat_gpr_set
  1492. },
  1493. [REGSET_FPR] = {
  1494. .core_note_type = NT_ARM_VFP,
  1495. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1496. .size = sizeof(compat_ulong_t),
  1497. .align = sizeof(compat_ulong_t),
  1498. .regset_get = compat_vfp_get,
  1499. .set = compat_vfp_set
  1500. },
  1501. [REGSET_TLS] = {
  1502. .core_note_type = NT_ARM_TLS,
  1503. .n = 1,
  1504. .size = sizeof(compat_ulong_t),
  1505. .align = sizeof(compat_ulong_t),
  1506. .regset_get = compat_tls_get,
  1507. .set = compat_tls_set,
  1508. },
  1509. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1510. [REGSET_HW_BREAK] = {
  1511. .core_note_type = NT_ARM_HW_BREAK,
  1512. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1513. .size = sizeof(u32),
  1514. .align = sizeof(u32),
  1515. .regset_get = hw_break_get,
  1516. .set = hw_break_set,
  1517. },
  1518. [REGSET_HW_WATCH] = {
  1519. .core_note_type = NT_ARM_HW_WATCH,
  1520. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1521. .size = sizeof(u32),
  1522. .align = sizeof(u32),
  1523. .regset_get = hw_break_get,
  1524. .set = hw_break_set,
  1525. },
  1526. #endif
  1527. [REGSET_SYSTEM_CALL] = {
  1528. .core_note_type = NT_ARM_SYSTEM_CALL,
  1529. .n = 1,
  1530. .size = sizeof(int),
  1531. .align = sizeof(int),
  1532. .regset_get = system_call_get,
  1533. .set = system_call_set,
  1534. },
  1535. };
  1536. static const struct user_regset_view user_aarch32_ptrace_view = {
  1537. .name = "aarch32", .e_machine = EM_ARM,
  1538. .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
  1539. };
  1540. static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
  1541. compat_ulong_t __user *ret)
  1542. {
  1543. compat_ulong_t tmp;
  1544. if (off & 3)
  1545. return -EIO;
  1546. if (off == COMPAT_PT_TEXT_ADDR)
  1547. tmp = tsk->mm->start_code;
  1548. else if (off == COMPAT_PT_DATA_ADDR)
  1549. tmp = tsk->mm->start_data;
  1550. else if (off == COMPAT_PT_TEXT_END_ADDR)
  1551. tmp = tsk->mm->end_code;
  1552. else if (off < sizeof(compat_elf_gregset_t))
  1553. tmp = compat_get_user_reg(tsk, off >> 2);
  1554. else if (off >= COMPAT_USER_SZ)
  1555. return -EIO;
  1556. else
  1557. tmp = 0;
  1558. return put_user(tmp, ret);
  1559. }
  1560. static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
  1561. compat_ulong_t val)
  1562. {
  1563. struct pt_regs newregs = *task_pt_regs(tsk);
  1564. unsigned int idx = off / 4;
  1565. if (off & 3 || off >= COMPAT_USER_SZ)
  1566. return -EIO;
  1567. if (off >= sizeof(compat_elf_gregset_t))
  1568. return 0;
  1569. switch (idx) {
  1570. case 15:
  1571. newregs.pc = val;
  1572. break;
  1573. case 16:
  1574. newregs.pstate = compat_psr_to_pstate(val);
  1575. break;
  1576. case 17:
  1577. newregs.orig_x0 = val;
  1578. break;
  1579. default:
  1580. newregs.regs[idx] = val;
  1581. }
  1582. if (!valid_user_regs(&newregs.user_regs, tsk))
  1583. return -EINVAL;
  1584. *task_pt_regs(tsk) = newregs;
  1585. return 0;
  1586. }
  1587. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1588. /*
  1589. * Convert a virtual register number into an index for a thread_info
  1590. * breakpoint array. Breakpoints are identified using positive numbers
  1591. * whilst watchpoints are negative. The registers are laid out as pairs
  1592. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  1593. * Register 0 is reserved for describing resource information.
  1594. */
  1595. static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
  1596. {
  1597. return (abs(num) - 1) >> 1;
  1598. }
  1599. static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
  1600. {
  1601. u8 num_brps, num_wrps, debug_arch, wp_len;
  1602. u32 reg = 0;
  1603. num_brps = hw_breakpoint_slots(TYPE_INST);
  1604. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  1605. debug_arch = debug_monitors_arch();
  1606. wp_len = 8;
  1607. reg |= debug_arch;
  1608. reg <<= 8;
  1609. reg |= wp_len;
  1610. reg <<= 8;
  1611. reg |= num_wrps;
  1612. reg <<= 8;
  1613. reg |= num_brps;
  1614. *kdata = reg;
  1615. return 0;
  1616. }
  1617. static int compat_ptrace_hbp_get(unsigned int note_type,
  1618. struct task_struct *tsk,
  1619. compat_long_t num,
  1620. u32 *kdata)
  1621. {
  1622. u64 addr = 0;
  1623. u32 ctrl = 0;
  1624. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1625. if (num & 1) {
  1626. err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
  1627. *kdata = (u32)addr;
  1628. } else {
  1629. err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
  1630. *kdata = ctrl;
  1631. }
  1632. return err;
  1633. }
  1634. static int compat_ptrace_hbp_set(unsigned int note_type,
  1635. struct task_struct *tsk,
  1636. compat_long_t num,
  1637. u32 *kdata)
  1638. {
  1639. u64 addr;
  1640. u32 ctrl;
  1641. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1642. if (num & 1) {
  1643. addr = *kdata;
  1644. err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
  1645. } else {
  1646. ctrl = *kdata;
  1647. err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
  1648. }
  1649. return err;
  1650. }
  1651. static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
  1652. compat_ulong_t __user *data)
  1653. {
  1654. int ret;
  1655. u32 kdata;
  1656. /* Watchpoint */
  1657. if (num < 0) {
  1658. ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1659. /* Resource info */
  1660. } else if (num == 0) {
  1661. ret = compat_ptrace_hbp_get_resource_info(&kdata);
  1662. /* Breakpoint */
  1663. } else {
  1664. ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1665. }
  1666. if (!ret)
  1667. ret = put_user(kdata, data);
  1668. return ret;
  1669. }
  1670. static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
  1671. compat_ulong_t __user *data)
  1672. {
  1673. int ret;
  1674. u32 kdata = 0;
  1675. if (num == 0)
  1676. return 0;
  1677. ret = get_user(kdata, data);
  1678. if (ret)
  1679. return ret;
  1680. if (num < 0)
  1681. ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1682. else
  1683. ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1684. return ret;
  1685. }
  1686. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1687. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1688. compat_ulong_t caddr, compat_ulong_t cdata)
  1689. {
  1690. unsigned long addr = caddr;
  1691. unsigned long data = cdata;
  1692. void __user *datap = compat_ptr(data);
  1693. int ret;
  1694. switch (request) {
  1695. case PTRACE_PEEKUSR:
  1696. ret = compat_ptrace_read_user(child, addr, datap);
  1697. break;
  1698. case PTRACE_POKEUSR:
  1699. ret = compat_ptrace_write_user(child, addr, data);
  1700. break;
  1701. case COMPAT_PTRACE_GETREGS:
  1702. ret = copy_regset_to_user(child,
  1703. &user_aarch32_view,
  1704. REGSET_COMPAT_GPR,
  1705. 0, sizeof(compat_elf_gregset_t),
  1706. datap);
  1707. break;
  1708. case COMPAT_PTRACE_SETREGS:
  1709. ret = copy_regset_from_user(child,
  1710. &user_aarch32_view,
  1711. REGSET_COMPAT_GPR,
  1712. 0, sizeof(compat_elf_gregset_t),
  1713. datap);
  1714. break;
  1715. case COMPAT_PTRACE_GET_THREAD_AREA:
  1716. ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
  1717. (compat_ulong_t __user *)datap);
  1718. break;
  1719. case COMPAT_PTRACE_SET_SYSCALL:
  1720. task_pt_regs(child)->syscallno = data;
  1721. ret = 0;
  1722. break;
  1723. case COMPAT_PTRACE_GETVFPREGS:
  1724. ret = copy_regset_to_user(child,
  1725. &user_aarch32_view,
  1726. REGSET_COMPAT_VFP,
  1727. 0, VFP_STATE_SIZE,
  1728. datap);
  1729. break;
  1730. case COMPAT_PTRACE_SETVFPREGS:
  1731. ret = copy_regset_from_user(child,
  1732. &user_aarch32_view,
  1733. REGSET_COMPAT_VFP,
  1734. 0, VFP_STATE_SIZE,
  1735. datap);
  1736. break;
  1737. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1738. case COMPAT_PTRACE_GETHBPREGS:
  1739. ret = compat_ptrace_gethbpregs(child, addr, datap);
  1740. break;
  1741. case COMPAT_PTRACE_SETHBPREGS:
  1742. ret = compat_ptrace_sethbpregs(child, addr, datap);
  1743. break;
  1744. #endif
  1745. default:
  1746. ret = compat_ptrace_request(child, request, addr,
  1747. data);
  1748. break;
  1749. }
  1750. return ret;
  1751. }
  1752. #endif /* CONFIG_COMPAT */
  1753. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1754. {
  1755. #ifdef CONFIG_COMPAT
  1756. /*
  1757. * Core dumping of 32-bit tasks or compat ptrace requests must use the
  1758. * user_aarch32_view compatible with arm32. Native ptrace requests on
  1759. * 32-bit children use an extended user_aarch32_ptrace_view to allow
  1760. * access to the TLS register.
  1761. */
  1762. if (is_compat_task())
  1763. return &user_aarch32_view;
  1764. else if (is_compat_thread(task_thread_info(task)))
  1765. return &user_aarch32_ptrace_view;
  1766. #endif
  1767. return &user_aarch64_view;
  1768. }
  1769. long arch_ptrace(struct task_struct *child, long request,
  1770. unsigned long addr, unsigned long data)
  1771. {
  1772. switch (request) {
  1773. case PTRACE_PEEKMTETAGS:
  1774. case PTRACE_POKEMTETAGS:
  1775. return mte_ptrace_copy_tags(child, request, addr, data);
  1776. }
  1777. return ptrace_request(child, request, addr, data);
  1778. }
  1779. enum ptrace_syscall_dir {
  1780. PTRACE_SYSCALL_ENTER = 0,
  1781. PTRACE_SYSCALL_EXIT,
  1782. };
  1783. static void report_syscall(struct pt_regs *regs, enum ptrace_syscall_dir dir)
  1784. {
  1785. int regno;
  1786. unsigned long saved_reg;
  1787. /*
  1788. * We have some ABI weirdness here in the way that we handle syscall
  1789. * exit stops because we indicate whether or not the stop has been
  1790. * signalled from syscall entry or syscall exit by clobbering a general
  1791. * purpose register (ip/r12 for AArch32, x7 for AArch64) in the tracee
  1792. * and restoring its old value after the stop. This means that:
  1793. *
  1794. * - Any writes by the tracer to this register during the stop are
  1795. * ignored/discarded.
  1796. *
  1797. * - The actual value of the register is not available during the stop,
  1798. * so the tracer cannot save it and restore it later.
  1799. *
  1800. * - Syscall stops behave differently to seccomp and pseudo-step traps
  1801. * (the latter do not nobble any registers).
  1802. */
  1803. regno = (is_compat_task() ? 12 : 7);
  1804. saved_reg = regs->regs[regno];
  1805. regs->regs[regno] = dir;
  1806. if (dir == PTRACE_SYSCALL_ENTER) {
  1807. if (ptrace_report_syscall_entry(regs))
  1808. forget_syscall(regs);
  1809. regs->regs[regno] = saved_reg;
  1810. } else if (!test_thread_flag(TIF_SINGLESTEP)) {
  1811. ptrace_report_syscall_exit(regs, 0);
  1812. regs->regs[regno] = saved_reg;
  1813. } else {
  1814. regs->regs[regno] = saved_reg;
  1815. /*
  1816. * Signal a pseudo-step exception since we are stepping but
  1817. * tracer modifications to the registers may have rewound the
  1818. * state machine.
  1819. */
  1820. ptrace_report_syscall_exit(regs, 1);
  1821. }
  1822. }
  1823. int syscall_trace_enter(struct pt_regs *regs)
  1824. {
  1825. unsigned long flags = read_thread_flags();
  1826. if (flags & (_TIF_SYSCALL_EMU | _TIF_SYSCALL_TRACE)) {
  1827. report_syscall(regs, PTRACE_SYSCALL_ENTER);
  1828. if (flags & _TIF_SYSCALL_EMU)
  1829. return NO_SYSCALL;
  1830. }
  1831. /* Do the secure computing after ptrace; failures should be fast. */
  1832. if (secure_computing() == -1)
  1833. return NO_SYSCALL;
  1834. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  1835. trace_sys_enter(regs, regs->syscallno);
  1836. audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
  1837. regs->regs[2], regs->regs[3]);
  1838. return regs->syscallno;
  1839. }
  1840. void syscall_trace_exit(struct pt_regs *regs)
  1841. {
  1842. unsigned long flags = read_thread_flags();
  1843. audit_syscall_exit(regs);
  1844. if (flags & _TIF_SYSCALL_TRACEPOINT)
  1845. trace_sys_exit(regs, syscall_get_return_value(current, regs));
  1846. if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
  1847. report_syscall(regs, PTRACE_SYSCALL_EXIT);
  1848. rseq_syscall(regs);
  1849. }
  1850. /*
  1851. * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
  1852. * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
  1853. * not described in ARM DDI 0487D.a.
  1854. * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
  1855. * be allocated an EL0 meaning in future.
  1856. * Userspace cannot use these until they have an architectural meaning.
  1857. * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
  1858. * We also reserve IL for the kernel; SS is handled dynamically.
  1859. */
  1860. #define SPSR_EL1_AARCH64_RES0_BITS \
  1861. (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 26) | GENMASK_ULL(23, 22) | \
  1862. GENMASK_ULL(20, 13) | GENMASK_ULL(5, 5))
  1863. #define SPSR_EL1_AARCH32_RES0_BITS \
  1864. (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
  1865. static int valid_compat_regs(struct user_pt_regs *regs)
  1866. {
  1867. regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
  1868. if (!system_supports_mixed_endian_el0()) {
  1869. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1870. regs->pstate |= PSR_AA32_E_BIT;
  1871. else
  1872. regs->pstate &= ~PSR_AA32_E_BIT;
  1873. }
  1874. if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
  1875. (regs->pstate & PSR_AA32_A_BIT) == 0 &&
  1876. (regs->pstate & PSR_AA32_I_BIT) == 0 &&
  1877. (regs->pstate & PSR_AA32_F_BIT) == 0) {
  1878. return 1;
  1879. }
  1880. /*
  1881. * Force PSR to a valid 32-bit EL0t, preserving the same bits as
  1882. * arch/arm.
  1883. */
  1884. regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
  1885. PSR_AA32_C_BIT | PSR_AA32_V_BIT |
  1886. PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
  1887. PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
  1888. PSR_AA32_T_BIT;
  1889. regs->pstate |= PSR_MODE32_BIT;
  1890. return 0;
  1891. }
  1892. static int valid_native_regs(struct user_pt_regs *regs)
  1893. {
  1894. regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
  1895. if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
  1896. (regs->pstate & PSR_D_BIT) == 0 &&
  1897. (regs->pstate & PSR_A_BIT) == 0 &&
  1898. (regs->pstate & PSR_I_BIT) == 0 &&
  1899. (regs->pstate & PSR_F_BIT) == 0) {
  1900. return 1;
  1901. }
  1902. /* Force PSR to a valid 64-bit EL0t */
  1903. regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
  1904. return 0;
  1905. }
  1906. /*
  1907. * Are the current registers suitable for user mode? (used to maintain
  1908. * security in signal handlers)
  1909. */
  1910. int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
  1911. {
  1912. /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
  1913. user_regs_reset_single_step(regs, task);
  1914. if (is_compat_thread(task_thread_info(task)))
  1915. return valid_compat_regs(regs);
  1916. else
  1917. return valid_native_regs(regs);
  1918. }