ptrace.c 34 KB

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