ptrace.c 41 KB

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