ptrace.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  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. static int fpr_active(struct task_struct *target, const struct user_regset *regset)
  549. {
  550. if (!system_supports_fpsimd())
  551. return -ENODEV;
  552. return regset->n;
  553. }
  554. /*
  555. * TODO: update fp accessors for lazy context switching (sync/flush hwstate)
  556. */
  557. static int __fpr_get(struct task_struct *target,
  558. const struct user_regset *regset,
  559. unsigned int pos, unsigned int count,
  560. void *kbuf, void __user *ubuf, unsigned int start_pos)
  561. {
  562. struct user_fpsimd_state *uregs;
  563. sve_sync_to_fpsimd(target);
  564. uregs = &target->thread.uw.fpsimd_state;
  565. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
  566. start_pos, start_pos + sizeof(*uregs));
  567. }
  568. static int fpr_get(struct task_struct *target, const struct user_regset *regset,
  569. unsigned int pos, unsigned int count,
  570. void *kbuf, void __user *ubuf)
  571. {
  572. if (!system_supports_fpsimd())
  573. return -EINVAL;
  574. if (target == current)
  575. fpsimd_preserve_current_state();
  576. return __fpr_get(target, regset, pos, count, kbuf, ubuf, 0);
  577. }
  578. static int __fpr_set(struct task_struct *target,
  579. const struct user_regset *regset,
  580. unsigned int pos, unsigned int count,
  581. const void *kbuf, const void __user *ubuf,
  582. unsigned int start_pos)
  583. {
  584. int ret;
  585. struct user_fpsimd_state newstate;
  586. /*
  587. * Ensure target->thread.uw.fpsimd_state is up to date, so that a
  588. * short copyin can't resurrect stale data.
  589. */
  590. sve_sync_to_fpsimd(target);
  591. newstate = target->thread.uw.fpsimd_state;
  592. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &newstate,
  593. start_pos, start_pos + sizeof(newstate));
  594. if (ret)
  595. return ret;
  596. target->thread.uw.fpsimd_state = newstate;
  597. return ret;
  598. }
  599. static int fpr_set(struct task_struct *target, const struct user_regset *regset,
  600. unsigned int pos, unsigned int count,
  601. const void *kbuf, const void __user *ubuf)
  602. {
  603. int ret;
  604. if (!system_supports_fpsimd())
  605. return -EINVAL;
  606. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf, 0);
  607. if (ret)
  608. return ret;
  609. sve_sync_from_fpsimd_zeropad(target);
  610. fpsimd_flush_task_state(target);
  611. return ret;
  612. }
  613. static int tls_get(struct task_struct *target, const struct user_regset *regset,
  614. unsigned int pos, unsigned int count,
  615. void *kbuf, void __user *ubuf)
  616. {
  617. unsigned long *tls = &target->thread.uw.tp_value;
  618. if (target == current)
  619. tls_preserve_current_state();
  620. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, tls, 0, -1);
  621. }
  622. static int tls_set(struct task_struct *target, const struct user_regset *regset,
  623. unsigned int pos, unsigned int count,
  624. const void *kbuf, const void __user *ubuf)
  625. {
  626. int ret;
  627. unsigned long tls = target->thread.uw.tp_value;
  628. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  629. if (ret)
  630. return ret;
  631. target->thread.uw.tp_value = tls;
  632. return ret;
  633. }
  634. static int system_call_get(struct task_struct *target,
  635. const struct user_regset *regset,
  636. unsigned int pos, unsigned int count,
  637. void *kbuf, void __user *ubuf)
  638. {
  639. int syscallno = task_pt_regs(target)->syscallno;
  640. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  641. &syscallno, 0, -1);
  642. }
  643. static int system_call_set(struct task_struct *target,
  644. const struct user_regset *regset,
  645. unsigned int pos, unsigned int count,
  646. const void *kbuf, const void __user *ubuf)
  647. {
  648. int syscallno = task_pt_regs(target)->syscallno;
  649. int ret;
  650. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &syscallno, 0, -1);
  651. if (ret)
  652. return ret;
  653. task_pt_regs(target)->syscallno = syscallno;
  654. return ret;
  655. }
  656. #ifdef CONFIG_ARM64_SVE
  657. static void sve_init_header_from_task(struct user_sve_header *header,
  658. struct task_struct *target)
  659. {
  660. unsigned int vq;
  661. memset(header, 0, sizeof(*header));
  662. header->flags = test_tsk_thread_flag(target, TIF_SVE) ?
  663. SVE_PT_REGS_SVE : SVE_PT_REGS_FPSIMD;
  664. if (test_tsk_thread_flag(target, TIF_SVE_VL_INHERIT))
  665. header->flags |= SVE_PT_VL_INHERIT;
  666. header->vl = target->thread.sve_vl;
  667. vq = sve_vq_from_vl(header->vl);
  668. header->max_vl = sve_max_vl;
  669. header->size = SVE_PT_SIZE(vq, header->flags);
  670. header->max_size = SVE_PT_SIZE(sve_vq_from_vl(header->max_vl),
  671. SVE_PT_REGS_SVE);
  672. }
  673. static unsigned int sve_size_from_header(struct user_sve_header const *header)
  674. {
  675. return ALIGN(header->size, SVE_VQ_BYTES);
  676. }
  677. static unsigned int sve_get_size(struct task_struct *target,
  678. const struct user_regset *regset)
  679. {
  680. struct user_sve_header header;
  681. if (!system_supports_sve())
  682. return 0;
  683. sve_init_header_from_task(&header, target);
  684. return sve_size_from_header(&header);
  685. }
  686. static int sve_get(struct task_struct *target,
  687. const struct user_regset *regset,
  688. unsigned int pos, unsigned int count,
  689. void *kbuf, void __user *ubuf)
  690. {
  691. int ret;
  692. struct user_sve_header header;
  693. unsigned int vq;
  694. unsigned long start, end;
  695. if (!system_supports_sve())
  696. return -EINVAL;
  697. /* Header */
  698. sve_init_header_from_task(&header, target);
  699. vq = sve_vq_from_vl(header.vl);
  700. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &header,
  701. 0, sizeof(header));
  702. if (ret)
  703. return ret;
  704. if (target == current)
  705. fpsimd_preserve_current_state();
  706. /* Registers: FPSIMD-only case */
  707. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  708. if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD)
  709. return __fpr_get(target, regset, pos, count, kbuf, ubuf,
  710. SVE_PT_FPSIMD_OFFSET);
  711. /* Otherwise: full SVE case */
  712. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  713. start = SVE_PT_SVE_OFFSET;
  714. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  715. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  716. target->thread.sve_state,
  717. start, end);
  718. if (ret)
  719. return ret;
  720. start = end;
  721. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  722. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  723. start, end);
  724. if (ret)
  725. return ret;
  726. /*
  727. * Copy fpsr, and fpcr which must follow contiguously in
  728. * struct fpsimd_state:
  729. */
  730. start = end;
  731. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  732. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  733. &target->thread.uw.fpsimd_state.fpsr,
  734. start, end);
  735. if (ret)
  736. return ret;
  737. start = end;
  738. end = sve_size_from_header(&header);
  739. return user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  740. start, end);
  741. }
  742. static int sve_set(struct task_struct *target,
  743. const struct user_regset *regset,
  744. unsigned int pos, unsigned int count,
  745. const void *kbuf, const void __user *ubuf)
  746. {
  747. int ret;
  748. struct user_sve_header header;
  749. unsigned int vq;
  750. unsigned long start, end;
  751. if (!system_supports_sve())
  752. return -EINVAL;
  753. /* Header */
  754. if (count < sizeof(header))
  755. return -EINVAL;
  756. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &header,
  757. 0, sizeof(header));
  758. if (ret)
  759. goto out;
  760. /*
  761. * Apart from PT_SVE_REGS_MASK, all PT_SVE_* flags are consumed by
  762. * sve_set_vector_length(), which will also validate them for us:
  763. */
  764. ret = sve_set_vector_length(target, header.vl,
  765. ((unsigned long)header.flags & ~SVE_PT_REGS_MASK) << 16);
  766. if (ret)
  767. goto out;
  768. /* Actual VL set may be less than the user asked for: */
  769. vq = sve_vq_from_vl(target->thread.sve_vl);
  770. /* Registers: FPSIMD-only case */
  771. BUILD_BUG_ON(SVE_PT_FPSIMD_OFFSET != sizeof(header));
  772. if ((header.flags & SVE_PT_REGS_MASK) == SVE_PT_REGS_FPSIMD) {
  773. ret = __fpr_set(target, regset, pos, count, kbuf, ubuf,
  774. SVE_PT_FPSIMD_OFFSET);
  775. clear_tsk_thread_flag(target, TIF_SVE);
  776. goto out;
  777. }
  778. /* Otherwise: full SVE case */
  779. /*
  780. * If setting a different VL from the requested VL and there is
  781. * register data, the data layout will be wrong: don't even
  782. * try to set the registers in this case.
  783. */
  784. if (count && vq != sve_vq_from_vl(header.vl)) {
  785. ret = -EIO;
  786. goto out;
  787. }
  788. sve_alloc(target);
  789. /*
  790. * Ensure target->thread.sve_state is up to date with target's
  791. * FPSIMD regs, so that a short copyin leaves trailing registers
  792. * unmodified.
  793. */
  794. fpsimd_sync_to_sve(target);
  795. set_tsk_thread_flag(target, TIF_SVE);
  796. BUILD_BUG_ON(SVE_PT_SVE_OFFSET != sizeof(header));
  797. start = SVE_PT_SVE_OFFSET;
  798. end = SVE_PT_SVE_FFR_OFFSET(vq) + SVE_PT_SVE_FFR_SIZE(vq);
  799. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  800. target->thread.sve_state,
  801. start, end);
  802. if (ret)
  803. goto out;
  804. start = end;
  805. end = SVE_PT_SVE_FPSR_OFFSET(vq);
  806. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  807. start, end);
  808. if (ret)
  809. goto out;
  810. /*
  811. * Copy fpsr, and fpcr which must follow contiguously in
  812. * struct fpsimd_state:
  813. */
  814. start = end;
  815. end = SVE_PT_SVE_FPCR_OFFSET(vq) + SVE_PT_SVE_FPCR_SIZE;
  816. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  817. &target->thread.uw.fpsimd_state.fpsr,
  818. start, end);
  819. out:
  820. fpsimd_flush_task_state(target);
  821. return ret;
  822. }
  823. #endif /* CONFIG_ARM64_SVE */
  824. enum aarch64_regset {
  825. REGSET_GPR,
  826. REGSET_FPR,
  827. REGSET_TLS,
  828. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  829. REGSET_HW_BREAK,
  830. REGSET_HW_WATCH,
  831. #endif
  832. REGSET_SYSTEM_CALL,
  833. #ifdef CONFIG_ARM64_SVE
  834. REGSET_SVE,
  835. #endif
  836. };
  837. static const struct user_regset aarch64_regsets[] = {
  838. [REGSET_GPR] = {
  839. .core_note_type = NT_PRSTATUS,
  840. .n = sizeof(struct user_pt_regs) / sizeof(u64),
  841. .size = sizeof(u64),
  842. .align = sizeof(u64),
  843. .get = gpr_get,
  844. .set = gpr_set
  845. },
  846. [REGSET_FPR] = {
  847. .core_note_type = NT_PRFPREG,
  848. .n = sizeof(struct user_fpsimd_state) / sizeof(u32),
  849. /*
  850. * We pretend we have 32-bit registers because the fpsr and
  851. * fpcr are 32-bits wide.
  852. */
  853. .size = sizeof(u32),
  854. .align = sizeof(u32),
  855. .active = fpr_active,
  856. .get = fpr_get,
  857. .set = fpr_set
  858. },
  859. [REGSET_TLS] = {
  860. .core_note_type = NT_ARM_TLS,
  861. .n = 1,
  862. .size = sizeof(void *),
  863. .align = sizeof(void *),
  864. .get = tls_get,
  865. .set = tls_set,
  866. },
  867. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  868. [REGSET_HW_BREAK] = {
  869. .core_note_type = NT_ARM_HW_BREAK,
  870. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  871. .size = sizeof(u32),
  872. .align = sizeof(u32),
  873. .get = hw_break_get,
  874. .set = hw_break_set,
  875. },
  876. [REGSET_HW_WATCH] = {
  877. .core_note_type = NT_ARM_HW_WATCH,
  878. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  879. .size = sizeof(u32),
  880. .align = sizeof(u32),
  881. .get = hw_break_get,
  882. .set = hw_break_set,
  883. },
  884. #endif
  885. [REGSET_SYSTEM_CALL] = {
  886. .core_note_type = NT_ARM_SYSTEM_CALL,
  887. .n = 1,
  888. .size = sizeof(int),
  889. .align = sizeof(int),
  890. .get = system_call_get,
  891. .set = system_call_set,
  892. },
  893. #ifdef CONFIG_ARM64_SVE
  894. [REGSET_SVE] = { /* Scalable Vector Extension */
  895. .core_note_type = NT_ARM_SVE,
  896. .n = DIV_ROUND_UP(SVE_PT_SIZE(SVE_VQ_MAX, SVE_PT_REGS_SVE),
  897. SVE_VQ_BYTES),
  898. .size = SVE_VQ_BYTES,
  899. .align = SVE_VQ_BYTES,
  900. .get = sve_get,
  901. .set = sve_set,
  902. .get_size = sve_get_size,
  903. },
  904. #endif
  905. };
  906. static const struct user_regset_view user_aarch64_view = {
  907. .name = "aarch64", .e_machine = EM_AARCH64,
  908. .regsets = aarch64_regsets, .n = ARRAY_SIZE(aarch64_regsets)
  909. };
  910. #ifdef CONFIG_COMPAT
  911. enum compat_regset {
  912. REGSET_COMPAT_GPR,
  913. REGSET_COMPAT_VFP,
  914. };
  915. static int compat_gpr_get(struct task_struct *target,
  916. const struct user_regset *regset,
  917. unsigned int pos, unsigned int count,
  918. void *kbuf, void __user *ubuf)
  919. {
  920. int ret = 0;
  921. unsigned int i, start, num_regs;
  922. /* Calculate the number of AArch32 registers contained in count */
  923. num_regs = count / regset->size;
  924. /* Convert pos into an register number */
  925. start = pos / regset->size;
  926. if (start + num_regs > regset->n)
  927. return -EIO;
  928. for (i = 0; i < num_regs; ++i) {
  929. unsigned int idx = start + i;
  930. compat_ulong_t reg;
  931. switch (idx) {
  932. case 15:
  933. reg = task_pt_regs(target)->pc;
  934. break;
  935. case 16:
  936. reg = task_pt_regs(target)->pstate;
  937. reg = pstate_to_compat_psr(reg);
  938. break;
  939. case 17:
  940. reg = task_pt_regs(target)->orig_x0;
  941. break;
  942. default:
  943. reg = task_pt_regs(target)->regs[idx];
  944. }
  945. if (kbuf) {
  946. memcpy(kbuf, &reg, sizeof(reg));
  947. kbuf += sizeof(reg);
  948. } else {
  949. ret = copy_to_user(ubuf, &reg, sizeof(reg));
  950. if (ret) {
  951. ret = -EFAULT;
  952. break;
  953. }
  954. ubuf += sizeof(reg);
  955. }
  956. }
  957. return ret;
  958. }
  959. static int compat_gpr_set(struct task_struct *target,
  960. const struct user_regset *regset,
  961. unsigned int pos, unsigned int count,
  962. const void *kbuf, const void __user *ubuf)
  963. {
  964. struct pt_regs newregs;
  965. int ret = 0;
  966. unsigned int i, start, num_regs;
  967. /* Calculate the number of AArch32 registers contained in count */
  968. num_regs = count / regset->size;
  969. /* Convert pos into an register number */
  970. start = pos / regset->size;
  971. if (start + num_regs > regset->n)
  972. return -EIO;
  973. newregs = *task_pt_regs(target);
  974. for (i = 0; i < num_regs; ++i) {
  975. unsigned int idx = start + i;
  976. compat_ulong_t reg;
  977. if (kbuf) {
  978. memcpy(&reg, kbuf, sizeof(reg));
  979. kbuf += sizeof(reg);
  980. } else {
  981. ret = copy_from_user(&reg, ubuf, sizeof(reg));
  982. if (ret) {
  983. ret = -EFAULT;
  984. break;
  985. }
  986. ubuf += sizeof(reg);
  987. }
  988. switch (idx) {
  989. case 15:
  990. newregs.pc = reg;
  991. break;
  992. case 16:
  993. reg = compat_psr_to_pstate(reg);
  994. newregs.pstate = reg;
  995. break;
  996. case 17:
  997. newregs.orig_x0 = reg;
  998. break;
  999. default:
  1000. newregs.regs[idx] = reg;
  1001. }
  1002. }
  1003. if (valid_user_regs(&newregs.user_regs, target))
  1004. *task_pt_regs(target) = newregs;
  1005. else
  1006. ret = -EINVAL;
  1007. return ret;
  1008. }
  1009. static int compat_vfp_get(struct task_struct *target,
  1010. const struct user_regset *regset,
  1011. unsigned int pos, unsigned int count,
  1012. void *kbuf, void __user *ubuf)
  1013. {
  1014. struct user_fpsimd_state *uregs;
  1015. compat_ulong_t fpscr;
  1016. int ret, vregs_end_pos;
  1017. if (!system_supports_fpsimd())
  1018. return -EINVAL;
  1019. uregs = &target->thread.uw.fpsimd_state;
  1020. if (target == current)
  1021. fpsimd_preserve_current_state();
  1022. /*
  1023. * The VFP registers are packed into the fpsimd_state, so they all sit
  1024. * nicely together for us. We just need to create the fpscr separately.
  1025. */
  1026. vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
  1027. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, uregs,
  1028. 0, vregs_end_pos);
  1029. if (count && !ret) {
  1030. fpscr = (uregs->fpsr & VFP_FPSCR_STAT_MASK) |
  1031. (uregs->fpcr & VFP_FPSCR_CTRL_MASK);
  1032. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, &fpscr,
  1033. vregs_end_pos, VFP_STATE_SIZE);
  1034. }
  1035. return ret;
  1036. }
  1037. static int compat_vfp_set(struct task_struct *target,
  1038. const struct user_regset *regset,
  1039. unsigned int pos, unsigned int count,
  1040. const void *kbuf, const void __user *ubuf)
  1041. {
  1042. struct user_fpsimd_state *uregs;
  1043. compat_ulong_t fpscr;
  1044. int ret, vregs_end_pos;
  1045. if (!system_supports_fpsimd())
  1046. return -EINVAL;
  1047. uregs = &target->thread.uw.fpsimd_state;
  1048. vregs_end_pos = VFP_STATE_SIZE - sizeof(compat_ulong_t);
  1049. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, uregs, 0,
  1050. vregs_end_pos);
  1051. if (count && !ret) {
  1052. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &fpscr,
  1053. vregs_end_pos, VFP_STATE_SIZE);
  1054. if (!ret) {
  1055. uregs->fpsr = fpscr & VFP_FPSCR_STAT_MASK;
  1056. uregs->fpcr = fpscr & VFP_FPSCR_CTRL_MASK;
  1057. }
  1058. }
  1059. fpsimd_flush_task_state(target);
  1060. return ret;
  1061. }
  1062. static int compat_tls_get(struct task_struct *target,
  1063. const struct user_regset *regset, unsigned int pos,
  1064. unsigned int count, void *kbuf, void __user *ubuf)
  1065. {
  1066. compat_ulong_t tls = (compat_ulong_t)target->thread.uw.tp_value;
  1067. return user_regset_copyout(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  1068. }
  1069. static int compat_tls_set(struct task_struct *target,
  1070. const struct user_regset *regset, unsigned int pos,
  1071. unsigned int count, const void *kbuf,
  1072. const void __user *ubuf)
  1073. {
  1074. int ret;
  1075. compat_ulong_t tls = target->thread.uw.tp_value;
  1076. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tls, 0, -1);
  1077. if (ret)
  1078. return ret;
  1079. target->thread.uw.tp_value = tls;
  1080. return ret;
  1081. }
  1082. static const struct user_regset aarch32_regsets[] = {
  1083. [REGSET_COMPAT_GPR] = {
  1084. .core_note_type = NT_PRSTATUS,
  1085. .n = COMPAT_ELF_NGREG,
  1086. .size = sizeof(compat_elf_greg_t),
  1087. .align = sizeof(compat_elf_greg_t),
  1088. .get = compat_gpr_get,
  1089. .set = compat_gpr_set
  1090. },
  1091. [REGSET_COMPAT_VFP] = {
  1092. .core_note_type = NT_ARM_VFP,
  1093. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1094. .size = sizeof(compat_ulong_t),
  1095. .align = sizeof(compat_ulong_t),
  1096. .active = fpr_active,
  1097. .get = compat_vfp_get,
  1098. .set = compat_vfp_set
  1099. },
  1100. };
  1101. static const struct user_regset_view user_aarch32_view = {
  1102. .name = "aarch32", .e_machine = EM_ARM,
  1103. .regsets = aarch32_regsets, .n = ARRAY_SIZE(aarch32_regsets)
  1104. };
  1105. static const struct user_regset aarch32_ptrace_regsets[] = {
  1106. [REGSET_GPR] = {
  1107. .core_note_type = NT_PRSTATUS,
  1108. .n = COMPAT_ELF_NGREG,
  1109. .size = sizeof(compat_elf_greg_t),
  1110. .align = sizeof(compat_elf_greg_t),
  1111. .get = compat_gpr_get,
  1112. .set = compat_gpr_set
  1113. },
  1114. [REGSET_FPR] = {
  1115. .core_note_type = NT_ARM_VFP,
  1116. .n = VFP_STATE_SIZE / sizeof(compat_ulong_t),
  1117. .size = sizeof(compat_ulong_t),
  1118. .align = sizeof(compat_ulong_t),
  1119. .get = compat_vfp_get,
  1120. .set = compat_vfp_set
  1121. },
  1122. [REGSET_TLS] = {
  1123. .core_note_type = NT_ARM_TLS,
  1124. .n = 1,
  1125. .size = sizeof(compat_ulong_t),
  1126. .align = sizeof(compat_ulong_t),
  1127. .get = compat_tls_get,
  1128. .set = compat_tls_set,
  1129. },
  1130. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1131. [REGSET_HW_BREAK] = {
  1132. .core_note_type = NT_ARM_HW_BREAK,
  1133. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1134. .size = sizeof(u32),
  1135. .align = sizeof(u32),
  1136. .get = hw_break_get,
  1137. .set = hw_break_set,
  1138. },
  1139. [REGSET_HW_WATCH] = {
  1140. .core_note_type = NT_ARM_HW_WATCH,
  1141. .n = sizeof(struct user_hwdebug_state) / sizeof(u32),
  1142. .size = sizeof(u32),
  1143. .align = sizeof(u32),
  1144. .get = hw_break_get,
  1145. .set = hw_break_set,
  1146. },
  1147. #endif
  1148. [REGSET_SYSTEM_CALL] = {
  1149. .core_note_type = NT_ARM_SYSTEM_CALL,
  1150. .n = 1,
  1151. .size = sizeof(int),
  1152. .align = sizeof(int),
  1153. .get = system_call_get,
  1154. .set = system_call_set,
  1155. },
  1156. };
  1157. static const struct user_regset_view user_aarch32_ptrace_view = {
  1158. .name = "aarch32", .e_machine = EM_ARM,
  1159. .regsets = aarch32_ptrace_regsets, .n = ARRAY_SIZE(aarch32_ptrace_regsets)
  1160. };
  1161. static int compat_ptrace_read_user(struct task_struct *tsk, compat_ulong_t off,
  1162. compat_ulong_t __user *ret)
  1163. {
  1164. compat_ulong_t tmp;
  1165. if (off & 3)
  1166. return -EIO;
  1167. if (off == COMPAT_PT_TEXT_ADDR)
  1168. tmp = tsk->mm->start_code;
  1169. else if (off == COMPAT_PT_DATA_ADDR)
  1170. tmp = tsk->mm->start_data;
  1171. else if (off == COMPAT_PT_TEXT_END_ADDR)
  1172. tmp = tsk->mm->end_code;
  1173. else if (off < sizeof(compat_elf_gregset_t))
  1174. return copy_regset_to_user(tsk, &user_aarch32_view,
  1175. REGSET_COMPAT_GPR, off,
  1176. sizeof(compat_ulong_t), ret);
  1177. else if (off >= COMPAT_USER_SZ)
  1178. return -EIO;
  1179. else
  1180. tmp = 0;
  1181. return put_user(tmp, ret);
  1182. }
  1183. static int compat_ptrace_write_user(struct task_struct *tsk, compat_ulong_t off,
  1184. compat_ulong_t val)
  1185. {
  1186. int ret;
  1187. mm_segment_t old_fs = get_fs();
  1188. if (off & 3 || off >= COMPAT_USER_SZ)
  1189. return -EIO;
  1190. if (off >= sizeof(compat_elf_gregset_t))
  1191. return 0;
  1192. set_fs(KERNEL_DS);
  1193. ret = copy_regset_from_user(tsk, &user_aarch32_view,
  1194. REGSET_COMPAT_GPR, off,
  1195. sizeof(compat_ulong_t),
  1196. &val);
  1197. set_fs(old_fs);
  1198. return ret;
  1199. }
  1200. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1201. /*
  1202. * Convert a virtual register number into an index for a thread_info
  1203. * breakpoint array. Breakpoints are identified using positive numbers
  1204. * whilst watchpoints are negative. The registers are laid out as pairs
  1205. * of (address, control), each pair mapping to a unique hw_breakpoint struct.
  1206. * Register 0 is reserved for describing resource information.
  1207. */
  1208. static int compat_ptrace_hbp_num_to_idx(compat_long_t num)
  1209. {
  1210. return (abs(num) - 1) >> 1;
  1211. }
  1212. static int compat_ptrace_hbp_get_resource_info(u32 *kdata)
  1213. {
  1214. u8 num_brps, num_wrps, debug_arch, wp_len;
  1215. u32 reg = 0;
  1216. num_brps = hw_breakpoint_slots(TYPE_INST);
  1217. num_wrps = hw_breakpoint_slots(TYPE_DATA);
  1218. debug_arch = debug_monitors_arch();
  1219. wp_len = 8;
  1220. reg |= debug_arch;
  1221. reg <<= 8;
  1222. reg |= wp_len;
  1223. reg <<= 8;
  1224. reg |= num_wrps;
  1225. reg <<= 8;
  1226. reg |= num_brps;
  1227. *kdata = reg;
  1228. return 0;
  1229. }
  1230. static int compat_ptrace_hbp_get(unsigned int note_type,
  1231. struct task_struct *tsk,
  1232. compat_long_t num,
  1233. u32 *kdata)
  1234. {
  1235. u64 addr = 0;
  1236. u32 ctrl = 0;
  1237. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1238. if (num & 1) {
  1239. err = ptrace_hbp_get_addr(note_type, tsk, idx, &addr);
  1240. *kdata = (u32)addr;
  1241. } else {
  1242. err = ptrace_hbp_get_ctrl(note_type, tsk, idx, &ctrl);
  1243. *kdata = ctrl;
  1244. }
  1245. return err;
  1246. }
  1247. static int compat_ptrace_hbp_set(unsigned int note_type,
  1248. struct task_struct *tsk,
  1249. compat_long_t num,
  1250. u32 *kdata)
  1251. {
  1252. u64 addr;
  1253. u32 ctrl;
  1254. int err, idx = compat_ptrace_hbp_num_to_idx(num);
  1255. if (num & 1) {
  1256. addr = *kdata;
  1257. err = ptrace_hbp_set_addr(note_type, tsk, idx, addr);
  1258. } else {
  1259. ctrl = *kdata;
  1260. err = ptrace_hbp_set_ctrl(note_type, tsk, idx, ctrl);
  1261. }
  1262. return err;
  1263. }
  1264. static int compat_ptrace_gethbpregs(struct task_struct *tsk, compat_long_t num,
  1265. compat_ulong_t __user *data)
  1266. {
  1267. int ret;
  1268. u32 kdata;
  1269. /* Watchpoint */
  1270. if (num < 0) {
  1271. ret = compat_ptrace_hbp_get(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1272. /* Resource info */
  1273. } else if (num == 0) {
  1274. ret = compat_ptrace_hbp_get_resource_info(&kdata);
  1275. /* Breakpoint */
  1276. } else {
  1277. ret = compat_ptrace_hbp_get(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1278. }
  1279. if (!ret)
  1280. ret = put_user(kdata, data);
  1281. return ret;
  1282. }
  1283. static int compat_ptrace_sethbpregs(struct task_struct *tsk, compat_long_t num,
  1284. compat_ulong_t __user *data)
  1285. {
  1286. int ret;
  1287. u32 kdata = 0;
  1288. if (num == 0)
  1289. return 0;
  1290. ret = get_user(kdata, data);
  1291. if (ret)
  1292. return ret;
  1293. if (num < 0)
  1294. ret = compat_ptrace_hbp_set(NT_ARM_HW_WATCH, tsk, num, &kdata);
  1295. else
  1296. ret = compat_ptrace_hbp_set(NT_ARM_HW_BREAK, tsk, num, &kdata);
  1297. return ret;
  1298. }
  1299. #endif /* CONFIG_HAVE_HW_BREAKPOINT */
  1300. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  1301. compat_ulong_t caddr, compat_ulong_t cdata)
  1302. {
  1303. unsigned long addr = caddr;
  1304. unsigned long data = cdata;
  1305. void __user *datap = compat_ptr(data);
  1306. int ret;
  1307. switch (request) {
  1308. case PTRACE_PEEKUSR:
  1309. ret = compat_ptrace_read_user(child, addr, datap);
  1310. break;
  1311. case PTRACE_POKEUSR:
  1312. ret = compat_ptrace_write_user(child, addr, data);
  1313. break;
  1314. case COMPAT_PTRACE_GETREGS:
  1315. ret = copy_regset_to_user(child,
  1316. &user_aarch32_view,
  1317. REGSET_COMPAT_GPR,
  1318. 0, sizeof(compat_elf_gregset_t),
  1319. datap);
  1320. break;
  1321. case COMPAT_PTRACE_SETREGS:
  1322. ret = copy_regset_from_user(child,
  1323. &user_aarch32_view,
  1324. REGSET_COMPAT_GPR,
  1325. 0, sizeof(compat_elf_gregset_t),
  1326. datap);
  1327. break;
  1328. case COMPAT_PTRACE_GET_THREAD_AREA:
  1329. ret = put_user((compat_ulong_t)child->thread.uw.tp_value,
  1330. (compat_ulong_t __user *)datap);
  1331. break;
  1332. case COMPAT_PTRACE_SET_SYSCALL:
  1333. task_pt_regs(child)->syscallno = data;
  1334. ret = 0;
  1335. break;
  1336. case COMPAT_PTRACE_GETVFPREGS:
  1337. ret = copy_regset_to_user(child,
  1338. &user_aarch32_view,
  1339. REGSET_COMPAT_VFP,
  1340. 0, VFP_STATE_SIZE,
  1341. datap);
  1342. break;
  1343. case COMPAT_PTRACE_SETVFPREGS:
  1344. ret = copy_regset_from_user(child,
  1345. &user_aarch32_view,
  1346. REGSET_COMPAT_VFP,
  1347. 0, VFP_STATE_SIZE,
  1348. datap);
  1349. break;
  1350. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  1351. case COMPAT_PTRACE_GETHBPREGS:
  1352. ret = compat_ptrace_gethbpregs(child, addr, datap);
  1353. break;
  1354. case COMPAT_PTRACE_SETHBPREGS:
  1355. ret = compat_ptrace_sethbpregs(child, addr, datap);
  1356. break;
  1357. #endif
  1358. default:
  1359. ret = compat_ptrace_request(child, request, addr,
  1360. data);
  1361. break;
  1362. }
  1363. return ret;
  1364. }
  1365. #endif /* CONFIG_COMPAT */
  1366. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  1367. {
  1368. #ifdef CONFIG_COMPAT
  1369. /*
  1370. * Core dumping of 32-bit tasks or compat ptrace requests must use the
  1371. * user_aarch32_view compatible with arm32. Native ptrace requests on
  1372. * 32-bit children use an extended user_aarch32_ptrace_view to allow
  1373. * access to the TLS register.
  1374. */
  1375. if (is_compat_task())
  1376. return &user_aarch32_view;
  1377. else if (is_compat_thread(task_thread_info(task)))
  1378. return &user_aarch32_ptrace_view;
  1379. #endif
  1380. return &user_aarch64_view;
  1381. }
  1382. long arch_ptrace(struct task_struct *child, long request,
  1383. unsigned long addr, unsigned long data)
  1384. {
  1385. return ptrace_request(child, request, addr, data);
  1386. }
  1387. enum ptrace_syscall_dir {
  1388. PTRACE_SYSCALL_ENTER = 0,
  1389. PTRACE_SYSCALL_EXIT,
  1390. };
  1391. static void tracehook_report_syscall(struct pt_regs *regs,
  1392. enum ptrace_syscall_dir dir)
  1393. {
  1394. int regno;
  1395. unsigned long saved_reg;
  1396. /*
  1397. * A scratch register (ip(r12) on AArch32, x7 on AArch64) is
  1398. * used to denote syscall entry/exit:
  1399. */
  1400. regno = (is_compat_task() ? 12 : 7);
  1401. saved_reg = regs->regs[regno];
  1402. regs->regs[regno] = dir;
  1403. if (dir == PTRACE_SYSCALL_ENTER) {
  1404. if (tracehook_report_syscall_entry(regs))
  1405. forget_syscall(regs);
  1406. regs->regs[regno] = saved_reg;
  1407. } else if (!test_thread_flag(TIF_SINGLESTEP)) {
  1408. tracehook_report_syscall_exit(regs, 0);
  1409. regs->regs[regno] = saved_reg;
  1410. } else {
  1411. regs->regs[regno] = saved_reg;
  1412. /*
  1413. * Signal a pseudo-step exception since we are stepping but
  1414. * tracer modifications to the registers may have rewound the
  1415. * state machine.
  1416. */
  1417. tracehook_report_syscall_exit(regs, 1);
  1418. }
  1419. }
  1420. int syscall_trace_enter(struct pt_regs *regs)
  1421. {
  1422. if (test_thread_flag(TIF_SYSCALL_TRACE))
  1423. tracehook_report_syscall(regs, PTRACE_SYSCALL_ENTER);
  1424. /* Do the secure computing after ptrace; failures should be fast. */
  1425. if (secure_computing(NULL) == -1)
  1426. return -1;
  1427. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  1428. trace_sys_enter(regs, regs->syscallno);
  1429. audit_syscall_entry(regs->syscallno, regs->orig_x0, regs->regs[1],
  1430. regs->regs[2], regs->regs[3]);
  1431. return regs->syscallno;
  1432. }
  1433. void syscall_trace_exit(struct pt_regs *regs)
  1434. {
  1435. unsigned long flags = READ_ONCE(current_thread_info()->flags);
  1436. audit_syscall_exit(regs);
  1437. if (flags & _TIF_SYSCALL_TRACEPOINT)
  1438. trace_sys_exit(regs, regs_return_value(regs));
  1439. if (flags & (_TIF_SYSCALL_TRACE | _TIF_SINGLESTEP))
  1440. tracehook_report_syscall(regs, PTRACE_SYSCALL_EXIT);
  1441. rseq_syscall(regs);
  1442. }
  1443. /*
  1444. * SPSR_ELx bits which are always architecturally RES0 per ARM DDI 0487D.a.
  1445. * We permit userspace to set SSBS (AArch64 bit 12, AArch32 bit 23) which is
  1446. * not described in ARM DDI 0487D.a.
  1447. * We treat PAN and UAO as RES0 bits, as they are meaningless at EL0, and may
  1448. * be allocated an EL0 meaning in future.
  1449. * Userspace cannot use these until they have an architectural meaning.
  1450. * Note that this follows the SPSR_ELx format, not the AArch32 PSR format.
  1451. * We also reserve IL for the kernel; SS is handled dynamically.
  1452. */
  1453. #define SPSR_EL1_AARCH64_RES0_BITS \
  1454. (GENMASK_ULL(63, 32) | GENMASK_ULL(27, 25) | GENMASK_ULL(23, 22) | \
  1455. GENMASK_ULL(20, 13) | GENMASK_ULL(11, 10) | GENMASK_ULL(5, 5))
  1456. #define SPSR_EL1_AARCH32_RES0_BITS \
  1457. (GENMASK_ULL(63, 32) | GENMASK_ULL(22, 22) | GENMASK_ULL(20, 20))
  1458. static int valid_compat_regs(struct user_pt_regs *regs)
  1459. {
  1460. regs->pstate &= ~SPSR_EL1_AARCH32_RES0_BITS;
  1461. if (!system_supports_mixed_endian_el0()) {
  1462. if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
  1463. regs->pstate |= PSR_AA32_E_BIT;
  1464. else
  1465. regs->pstate &= ~PSR_AA32_E_BIT;
  1466. }
  1467. if (user_mode(regs) && (regs->pstate & PSR_MODE32_BIT) &&
  1468. (regs->pstate & PSR_AA32_A_BIT) == 0 &&
  1469. (regs->pstate & PSR_AA32_I_BIT) == 0 &&
  1470. (regs->pstate & PSR_AA32_F_BIT) == 0) {
  1471. return 1;
  1472. }
  1473. /*
  1474. * Force PSR to a valid 32-bit EL0t, preserving the same bits as
  1475. * arch/arm.
  1476. */
  1477. regs->pstate &= PSR_AA32_N_BIT | PSR_AA32_Z_BIT |
  1478. PSR_AA32_C_BIT | PSR_AA32_V_BIT |
  1479. PSR_AA32_Q_BIT | PSR_AA32_IT_MASK |
  1480. PSR_AA32_GE_MASK | PSR_AA32_E_BIT |
  1481. PSR_AA32_T_BIT;
  1482. regs->pstate |= PSR_MODE32_BIT;
  1483. return 0;
  1484. }
  1485. static int valid_native_regs(struct user_pt_regs *regs)
  1486. {
  1487. regs->pstate &= ~SPSR_EL1_AARCH64_RES0_BITS;
  1488. if (user_mode(regs) && !(regs->pstate & PSR_MODE32_BIT) &&
  1489. (regs->pstate & PSR_D_BIT) == 0 &&
  1490. (regs->pstate & PSR_A_BIT) == 0 &&
  1491. (regs->pstate & PSR_I_BIT) == 0 &&
  1492. (regs->pstate & PSR_F_BIT) == 0) {
  1493. return 1;
  1494. }
  1495. /* Force PSR to a valid 64-bit EL0t */
  1496. regs->pstate &= PSR_N_BIT | PSR_Z_BIT | PSR_C_BIT | PSR_V_BIT;
  1497. return 0;
  1498. }
  1499. /*
  1500. * Are the current registers suitable for user mode? (used to maintain
  1501. * security in signal handlers)
  1502. */
  1503. int valid_user_regs(struct user_pt_regs *regs, struct task_struct *task)
  1504. {
  1505. /* https://lore.kernel.org/lkml/20191118131525.GA4180@willie-the-truck */
  1506. user_regs_reset_single_step(regs, task);
  1507. if (is_compat_thread(task_thread_info(task)))
  1508. return valid_compat_regs(regs);
  1509. else
  1510. return valid_native_regs(regs);
  1511. }