ptrace_64.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* ptrace.c: Sparc process tracing support.
  3. *
  4. * Copyright (C) 1996, 2008 David S. Miller ([email protected])
  5. * Copyright (C) 1997 Jakub Jelinek ([email protected])
  6. *
  7. * Based upon code written by Ross Biro, Linus Torvalds, Bob Manson,
  8. * and David Mosberger.
  9. *
  10. * Added Linux support -miguel (weird, eh?, the original code was meant
  11. * to emulate SunOS).
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/sched/task_stack.h>
  16. #include <linux/mm.h>
  17. #include <linux/errno.h>
  18. #include <linux/export.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/user.h>
  21. #include <linux/smp.h>
  22. #include <linux/security.h>
  23. #include <linux/seccomp.h>
  24. #include <linux/audit.h>
  25. #include <linux/signal.h>
  26. #include <linux/regset.h>
  27. #include <trace/syscall.h>
  28. #include <linux/compat.h>
  29. #include <linux/elf.h>
  30. #include <linux/context_tracking.h>
  31. #include <asm/asi.h>
  32. #include <linux/uaccess.h>
  33. #include <asm/psrcompat.h>
  34. #include <asm/visasm.h>
  35. #include <asm/spitfire.h>
  36. #include <asm/page.h>
  37. #include <asm/cpudata.h>
  38. #include <asm/cacheflush.h>
  39. #define CREATE_TRACE_POINTS
  40. #include <trace/events/syscalls.h>
  41. #include "entry.h"
  42. /* #define ALLOW_INIT_TRACING */
  43. struct pt_regs_offset {
  44. const char *name;
  45. int offset;
  46. };
  47. #define REG_OFFSET_NAME(n, r) \
  48. {.name = n, .offset = (PT_V9_##r)}
  49. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  50. static const struct pt_regs_offset regoffset_table[] = {
  51. REG_OFFSET_NAME("g0", G0),
  52. REG_OFFSET_NAME("g1", G1),
  53. REG_OFFSET_NAME("g2", G2),
  54. REG_OFFSET_NAME("g3", G3),
  55. REG_OFFSET_NAME("g4", G4),
  56. REG_OFFSET_NAME("g5", G5),
  57. REG_OFFSET_NAME("g6", G6),
  58. REG_OFFSET_NAME("g7", G7),
  59. REG_OFFSET_NAME("i0", I0),
  60. REG_OFFSET_NAME("i1", I1),
  61. REG_OFFSET_NAME("i2", I2),
  62. REG_OFFSET_NAME("i3", I3),
  63. REG_OFFSET_NAME("i4", I4),
  64. REG_OFFSET_NAME("i5", I5),
  65. REG_OFFSET_NAME("i6", I6),
  66. REG_OFFSET_NAME("i7", I7),
  67. REG_OFFSET_NAME("tstate", TSTATE),
  68. REG_OFFSET_NAME("pc", TPC),
  69. REG_OFFSET_NAME("npc", TNPC),
  70. REG_OFFSET_NAME("y", Y),
  71. REG_OFFSET_NAME("lr", I7),
  72. REG_OFFSET_END,
  73. };
  74. /*
  75. * Called by kernel/ptrace.c when detaching..
  76. *
  77. * Make sure single step bits etc are not set.
  78. */
  79. void ptrace_disable(struct task_struct *child)
  80. {
  81. /* nothing to do */
  82. }
  83. /* To get the necessary page struct, access_process_vm() first calls
  84. * get_user_pages(). This has done a flush_dcache_page() on the
  85. * accessed page. Then our caller (copy_{to,from}_user_page()) did
  86. * to memcpy to read/write the data from that page.
  87. *
  88. * Now, the only thing we have to do is:
  89. * 1) flush the D-cache if it's possible than an illegal alias
  90. * has been created
  91. * 2) flush the I-cache if this is pre-cheetah and we did a write
  92. */
  93. void flush_ptrace_access(struct vm_area_struct *vma, struct page *page,
  94. unsigned long uaddr, void *kaddr,
  95. unsigned long len, int write)
  96. {
  97. BUG_ON(len > PAGE_SIZE);
  98. if (tlb_type == hypervisor)
  99. return;
  100. preempt_disable();
  101. #ifdef DCACHE_ALIASING_POSSIBLE
  102. /* If bit 13 of the kernel address we used to access the
  103. * user page is the same as the virtual address that page
  104. * is mapped to in the user's address space, we can skip the
  105. * D-cache flush.
  106. */
  107. if ((uaddr ^ (unsigned long) kaddr) & (1UL << 13)) {
  108. unsigned long start = __pa(kaddr);
  109. unsigned long end = start + len;
  110. unsigned long dcache_line_size;
  111. dcache_line_size = local_cpu_data().dcache_line_size;
  112. if (tlb_type == spitfire) {
  113. for (; start < end; start += dcache_line_size)
  114. spitfire_put_dcache_tag(start & 0x3fe0, 0x0);
  115. } else {
  116. start &= ~(dcache_line_size - 1);
  117. for (; start < end; start += dcache_line_size)
  118. __asm__ __volatile__(
  119. "stxa %%g0, [%0] %1\n\t"
  120. "membar #Sync"
  121. : /* no outputs */
  122. : "r" (start),
  123. "i" (ASI_DCACHE_INVALIDATE));
  124. }
  125. }
  126. #endif
  127. if (write && tlb_type == spitfire) {
  128. unsigned long start = (unsigned long) kaddr;
  129. unsigned long end = start + len;
  130. unsigned long icache_line_size;
  131. icache_line_size = local_cpu_data().icache_line_size;
  132. for (; start < end; start += icache_line_size)
  133. flushi(start);
  134. }
  135. preempt_enable();
  136. }
  137. EXPORT_SYMBOL_GPL(flush_ptrace_access);
  138. static int get_from_target(struct task_struct *target, unsigned long uaddr,
  139. void *kbuf, int len)
  140. {
  141. if (target == current) {
  142. if (copy_from_user(kbuf, (void __user *) uaddr, len))
  143. return -EFAULT;
  144. } else {
  145. int len2 = access_process_vm(target, uaddr, kbuf, len,
  146. FOLL_FORCE);
  147. if (len2 != len)
  148. return -EFAULT;
  149. }
  150. return 0;
  151. }
  152. static int set_to_target(struct task_struct *target, unsigned long uaddr,
  153. void *kbuf, int len)
  154. {
  155. if (target == current) {
  156. if (copy_to_user((void __user *) uaddr, kbuf, len))
  157. return -EFAULT;
  158. } else {
  159. int len2 = access_process_vm(target, uaddr, kbuf, len,
  160. FOLL_FORCE | FOLL_WRITE);
  161. if (len2 != len)
  162. return -EFAULT;
  163. }
  164. return 0;
  165. }
  166. static int regwindow64_get(struct task_struct *target,
  167. const struct pt_regs *regs,
  168. struct reg_window *wbuf)
  169. {
  170. unsigned long rw_addr = regs->u_regs[UREG_I6];
  171. if (!test_thread_64bit_stack(rw_addr)) {
  172. struct reg_window32 win32;
  173. int i;
  174. if (get_from_target(target, rw_addr, &win32, sizeof(win32)))
  175. return -EFAULT;
  176. for (i = 0; i < 8; i++)
  177. wbuf->locals[i] = win32.locals[i];
  178. for (i = 0; i < 8; i++)
  179. wbuf->ins[i] = win32.ins[i];
  180. } else {
  181. rw_addr += STACK_BIAS;
  182. if (get_from_target(target, rw_addr, wbuf, sizeof(*wbuf)))
  183. return -EFAULT;
  184. }
  185. return 0;
  186. }
  187. static int regwindow64_set(struct task_struct *target,
  188. const struct pt_regs *regs,
  189. struct reg_window *wbuf)
  190. {
  191. unsigned long rw_addr = regs->u_regs[UREG_I6];
  192. if (!test_thread_64bit_stack(rw_addr)) {
  193. struct reg_window32 win32;
  194. int i;
  195. for (i = 0; i < 8; i++)
  196. win32.locals[i] = wbuf->locals[i];
  197. for (i = 0; i < 8; i++)
  198. win32.ins[i] = wbuf->ins[i];
  199. if (set_to_target(target, rw_addr, &win32, sizeof(win32)))
  200. return -EFAULT;
  201. } else {
  202. rw_addr += STACK_BIAS;
  203. if (set_to_target(target, rw_addr, wbuf, sizeof(*wbuf)))
  204. return -EFAULT;
  205. }
  206. return 0;
  207. }
  208. enum sparc_regset {
  209. REGSET_GENERAL,
  210. REGSET_FP,
  211. };
  212. static int genregs64_get(struct task_struct *target,
  213. const struct user_regset *regset,
  214. struct membuf to)
  215. {
  216. const struct pt_regs *regs = task_pt_regs(target);
  217. struct reg_window window;
  218. if (target == current)
  219. flushw_user();
  220. membuf_write(&to, regs->u_regs, 16 * sizeof(u64));
  221. if (!to.left)
  222. return 0;
  223. if (regwindow64_get(target, regs, &window))
  224. return -EFAULT;
  225. membuf_write(&to, &window, 16 * sizeof(u64));
  226. /* TSTATE, TPC, TNPC */
  227. membuf_write(&to, &regs->tstate, 3 * sizeof(u64));
  228. return membuf_store(&to, (u64)regs->y);
  229. }
  230. static int genregs64_set(struct task_struct *target,
  231. const struct user_regset *regset,
  232. unsigned int pos, unsigned int count,
  233. const void *kbuf, const void __user *ubuf)
  234. {
  235. struct pt_regs *regs = task_pt_regs(target);
  236. int ret;
  237. if (target == current)
  238. flushw_user();
  239. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  240. regs->u_regs,
  241. 0, 16 * sizeof(u64));
  242. if (!ret && count && pos < (32 * sizeof(u64))) {
  243. struct reg_window window;
  244. if (regwindow64_get(target, regs, &window))
  245. return -EFAULT;
  246. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  247. &window,
  248. 16 * sizeof(u64),
  249. 32 * sizeof(u64));
  250. if (!ret &&
  251. regwindow64_set(target, regs, &window))
  252. return -EFAULT;
  253. }
  254. if (!ret && count > 0) {
  255. unsigned long tstate;
  256. /* TSTATE */
  257. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  258. &tstate,
  259. 32 * sizeof(u64),
  260. 33 * sizeof(u64));
  261. if (!ret) {
  262. /* Only the condition codes and the "in syscall"
  263. * state can be modified in the %tstate register.
  264. */
  265. tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  266. regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  267. regs->tstate |= tstate;
  268. }
  269. }
  270. if (!ret) {
  271. /* TPC, TNPC */
  272. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  273. &regs->tpc,
  274. 33 * sizeof(u64),
  275. 35 * sizeof(u64));
  276. }
  277. if (!ret) {
  278. unsigned long y = regs->y;
  279. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  280. &y,
  281. 35 * sizeof(u64),
  282. 36 * sizeof(u64));
  283. if (!ret)
  284. regs->y = y;
  285. }
  286. if (!ret)
  287. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  288. 36 * sizeof(u64), -1);
  289. return ret;
  290. }
  291. static int fpregs64_get(struct task_struct *target,
  292. const struct user_regset *regset,
  293. struct membuf to)
  294. {
  295. struct thread_info *t = task_thread_info(target);
  296. unsigned long fprs;
  297. if (target == current)
  298. save_and_clear_fpu();
  299. fprs = t->fpsaved[0];
  300. if (fprs & FPRS_DL)
  301. membuf_write(&to, t->fpregs, 16 * sizeof(u64));
  302. else
  303. membuf_zero(&to, 16 * sizeof(u64));
  304. if (fprs & FPRS_DU)
  305. membuf_write(&to, t->fpregs + 16, 16 * sizeof(u64));
  306. else
  307. membuf_zero(&to, 16 * sizeof(u64));
  308. if (fprs & FPRS_FEF) {
  309. membuf_store(&to, t->xfsr[0]);
  310. membuf_store(&to, t->gsr[0]);
  311. } else {
  312. membuf_zero(&to, 2 * sizeof(u64));
  313. }
  314. return membuf_store(&to, fprs);
  315. }
  316. static int fpregs64_set(struct task_struct *target,
  317. const struct user_regset *regset,
  318. unsigned int pos, unsigned int count,
  319. const void *kbuf, const void __user *ubuf)
  320. {
  321. unsigned long *fpregs = task_thread_info(target)->fpregs;
  322. unsigned long fprs;
  323. int ret;
  324. if (target == current)
  325. save_and_clear_fpu();
  326. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  327. fpregs,
  328. 0, 32 * sizeof(u64));
  329. if (!ret)
  330. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  331. task_thread_info(target)->xfsr,
  332. 32 * sizeof(u64),
  333. 33 * sizeof(u64));
  334. if (!ret)
  335. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  336. task_thread_info(target)->gsr,
  337. 33 * sizeof(u64),
  338. 34 * sizeof(u64));
  339. fprs = task_thread_info(target)->fpsaved[0];
  340. if (!ret && count > 0) {
  341. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  342. &fprs,
  343. 34 * sizeof(u64),
  344. 35 * sizeof(u64));
  345. }
  346. fprs |= (FPRS_FEF | FPRS_DL | FPRS_DU);
  347. task_thread_info(target)->fpsaved[0] = fprs;
  348. if (!ret)
  349. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  350. 35 * sizeof(u64), -1);
  351. return ret;
  352. }
  353. static const struct user_regset sparc64_regsets[] = {
  354. /* Format is:
  355. * G0 --> G7
  356. * O0 --> O7
  357. * L0 --> L7
  358. * I0 --> I7
  359. * TSTATE, TPC, TNPC, Y
  360. */
  361. [REGSET_GENERAL] = {
  362. .core_note_type = NT_PRSTATUS,
  363. .n = 36,
  364. .size = sizeof(u64), .align = sizeof(u64),
  365. .regset_get = genregs64_get, .set = genregs64_set
  366. },
  367. /* Format is:
  368. * F0 --> F63
  369. * FSR
  370. * GSR
  371. * FPRS
  372. */
  373. [REGSET_FP] = {
  374. .core_note_type = NT_PRFPREG,
  375. .n = 35,
  376. .size = sizeof(u64), .align = sizeof(u64),
  377. .regset_get = fpregs64_get, .set = fpregs64_set
  378. },
  379. };
  380. static int getregs64_get(struct task_struct *target,
  381. const struct user_regset *regset,
  382. struct membuf to)
  383. {
  384. const struct pt_regs *regs = task_pt_regs(target);
  385. if (target == current)
  386. flushw_user();
  387. membuf_write(&to, regs->u_regs + 1, 15 * sizeof(u64));
  388. membuf_store(&to, (u64)0);
  389. membuf_write(&to, &regs->tstate, 3 * sizeof(u64));
  390. return membuf_store(&to, (u64)regs->y);
  391. }
  392. static int setregs64_set(struct task_struct *target,
  393. const struct user_regset *regset,
  394. unsigned int pos, unsigned int count,
  395. const void *kbuf, const void __user *ubuf)
  396. {
  397. struct pt_regs *regs = task_pt_regs(target);
  398. unsigned long y = regs->y;
  399. unsigned long tstate;
  400. int ret;
  401. if (target == current)
  402. flushw_user();
  403. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  404. regs->u_regs + 1,
  405. 0 * sizeof(u64),
  406. 15 * sizeof(u64));
  407. if (ret)
  408. return ret;
  409. ret =user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  410. 15 * sizeof(u64), 16 * sizeof(u64));
  411. if (ret)
  412. return ret;
  413. /* TSTATE */
  414. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  415. &tstate,
  416. 16 * sizeof(u64),
  417. 17 * sizeof(u64));
  418. if (ret)
  419. return ret;
  420. /* Only the condition codes and the "in syscall"
  421. * state can be modified in the %tstate register.
  422. */
  423. tstate &= (TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  424. regs->tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  425. regs->tstate |= tstate;
  426. /* TPC, TNPC */
  427. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  428. &regs->tpc,
  429. 17 * sizeof(u64),
  430. 19 * sizeof(u64));
  431. if (ret)
  432. return ret;
  433. /* Y */
  434. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  435. &y,
  436. 19 * sizeof(u64),
  437. 20 * sizeof(u64));
  438. if (!ret)
  439. regs->y = y;
  440. return ret;
  441. }
  442. static const struct user_regset ptrace64_regsets[] = {
  443. /* Format is:
  444. * G1 --> G7
  445. * O0 --> O7
  446. * 0
  447. * TSTATE, TPC, TNPC, Y
  448. */
  449. [REGSET_GENERAL] = {
  450. .n = 20, .size = sizeof(u64),
  451. .regset_get = getregs64_get, .set = setregs64_set,
  452. },
  453. };
  454. static const struct user_regset_view ptrace64_view = {
  455. .regsets = ptrace64_regsets, .n = ARRAY_SIZE(ptrace64_regsets)
  456. };
  457. static const struct user_regset_view user_sparc64_view = {
  458. .name = "sparc64", .e_machine = EM_SPARCV9,
  459. .regsets = sparc64_regsets, .n = ARRAY_SIZE(sparc64_regsets)
  460. };
  461. #ifdef CONFIG_COMPAT
  462. static int genregs32_get(struct task_struct *target,
  463. const struct user_regset *regset,
  464. struct membuf to)
  465. {
  466. const struct pt_regs *regs = task_pt_regs(target);
  467. u32 uregs[16];
  468. int i;
  469. if (target == current)
  470. flushw_user();
  471. for (i = 0; i < 16; i++)
  472. membuf_store(&to, (u32)regs->u_regs[i]);
  473. if (!to.left)
  474. return 0;
  475. if (get_from_target(target, regs->u_regs[UREG_I6],
  476. uregs, sizeof(uregs)))
  477. return -EFAULT;
  478. membuf_write(&to, uregs, 16 * sizeof(u32));
  479. membuf_store(&to, (u32)tstate_to_psr(regs->tstate));
  480. membuf_store(&to, (u32)(regs->tpc));
  481. membuf_store(&to, (u32)(regs->tnpc));
  482. membuf_store(&to, (u32)(regs->y));
  483. return membuf_zero(&to, 2 * sizeof(u32));
  484. }
  485. static int genregs32_set(struct task_struct *target,
  486. const struct user_regset *regset,
  487. unsigned int pos, unsigned int count,
  488. const void *kbuf, const void __user *ubuf)
  489. {
  490. struct pt_regs *regs = task_pt_regs(target);
  491. compat_ulong_t __user *reg_window;
  492. const compat_ulong_t *k = kbuf;
  493. const compat_ulong_t __user *u = ubuf;
  494. compat_ulong_t reg;
  495. if (target == current)
  496. flushw_user();
  497. pos /= sizeof(reg);
  498. count /= sizeof(reg);
  499. if (kbuf) {
  500. for (; count > 0 && pos < 16; count--)
  501. regs->u_regs[pos++] = *k++;
  502. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  503. reg_window -= 16;
  504. if (target == current) {
  505. for (; count > 0 && pos < 32; count--) {
  506. if (put_user(*k++, &reg_window[pos++]))
  507. return -EFAULT;
  508. }
  509. } else {
  510. for (; count > 0 && pos < 32; count--) {
  511. if (access_process_vm(target,
  512. (unsigned long)
  513. &reg_window[pos],
  514. (void *) k,
  515. sizeof(*k),
  516. FOLL_FORCE | FOLL_WRITE)
  517. != sizeof(*k))
  518. return -EFAULT;
  519. k++;
  520. pos++;
  521. }
  522. }
  523. } else {
  524. for (; count > 0 && pos < 16; count--) {
  525. if (get_user(reg, u++))
  526. return -EFAULT;
  527. regs->u_regs[pos++] = reg;
  528. }
  529. reg_window = (compat_ulong_t __user *) regs->u_regs[UREG_I6];
  530. reg_window -= 16;
  531. if (target == current) {
  532. for (; count > 0 && pos < 32; count--) {
  533. if (get_user(reg, u++) ||
  534. put_user(reg, &reg_window[pos++]))
  535. return -EFAULT;
  536. }
  537. } else {
  538. for (; count > 0 && pos < 32; count--) {
  539. if (get_user(reg, u++))
  540. return -EFAULT;
  541. if (access_process_vm(target,
  542. (unsigned long)
  543. &reg_window[pos],
  544. &reg, sizeof(reg),
  545. FOLL_FORCE | FOLL_WRITE)
  546. != sizeof(reg))
  547. return -EFAULT;
  548. pos++;
  549. u++;
  550. }
  551. }
  552. }
  553. while (count > 0) {
  554. unsigned long tstate;
  555. if (kbuf)
  556. reg = *k++;
  557. else if (get_user(reg, u++))
  558. return -EFAULT;
  559. switch (pos) {
  560. case 32: /* PSR */
  561. tstate = regs->tstate;
  562. tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  563. tstate |= psr_to_tstate_icc(reg);
  564. if (reg & PSR_SYSCALL)
  565. tstate |= TSTATE_SYSCALL;
  566. regs->tstate = tstate;
  567. break;
  568. case 33: /* PC */
  569. regs->tpc = reg;
  570. break;
  571. case 34: /* NPC */
  572. regs->tnpc = reg;
  573. break;
  574. case 35: /* Y */
  575. regs->y = reg;
  576. break;
  577. case 36: /* WIM */
  578. case 37: /* TBR */
  579. break;
  580. default:
  581. goto finish;
  582. }
  583. pos++;
  584. count--;
  585. }
  586. finish:
  587. pos *= sizeof(reg);
  588. count *= sizeof(reg);
  589. return user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  590. 38 * sizeof(reg), -1);
  591. }
  592. static int fpregs32_get(struct task_struct *target,
  593. const struct user_regset *regset,
  594. struct membuf to)
  595. {
  596. struct thread_info *t = task_thread_info(target);
  597. bool enabled;
  598. if (target == current)
  599. save_and_clear_fpu();
  600. enabled = t->fpsaved[0] & FPRS_FEF;
  601. membuf_write(&to, t->fpregs, 32 * sizeof(u32));
  602. membuf_zero(&to, sizeof(u32));
  603. if (enabled)
  604. membuf_store(&to, (u32)t->xfsr[0]);
  605. else
  606. membuf_zero(&to, sizeof(u32));
  607. membuf_store(&to, (u32)((enabled << 8) | (8 << 16)));
  608. return membuf_zero(&to, 64 * sizeof(u32));
  609. }
  610. static int fpregs32_set(struct task_struct *target,
  611. const struct user_regset *regset,
  612. unsigned int pos, unsigned int count,
  613. const void *kbuf, const void __user *ubuf)
  614. {
  615. unsigned long *fpregs = task_thread_info(target)->fpregs;
  616. unsigned long fprs;
  617. int ret;
  618. if (target == current)
  619. save_and_clear_fpu();
  620. fprs = task_thread_info(target)->fpsaved[0];
  621. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  622. fpregs,
  623. 0, 32 * sizeof(u32));
  624. if (!ret)
  625. user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  626. 32 * sizeof(u32),
  627. 33 * sizeof(u32));
  628. if (!ret && count > 0) {
  629. compat_ulong_t fsr;
  630. unsigned long val;
  631. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  632. &fsr,
  633. 33 * sizeof(u32),
  634. 34 * sizeof(u32));
  635. if (!ret) {
  636. val = task_thread_info(target)->xfsr[0];
  637. val &= 0xffffffff00000000UL;
  638. val |= fsr;
  639. task_thread_info(target)->xfsr[0] = val;
  640. }
  641. }
  642. fprs |= (FPRS_FEF | FPRS_DL);
  643. task_thread_info(target)->fpsaved[0] = fprs;
  644. if (!ret)
  645. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  646. 34 * sizeof(u32), -1);
  647. return ret;
  648. }
  649. static const struct user_regset sparc32_regsets[] = {
  650. /* Format is:
  651. * G0 --> G7
  652. * O0 --> O7
  653. * L0 --> L7
  654. * I0 --> I7
  655. * PSR, PC, nPC, Y, WIM, TBR
  656. */
  657. [REGSET_GENERAL] = {
  658. .core_note_type = NT_PRSTATUS,
  659. .n = 38,
  660. .size = sizeof(u32), .align = sizeof(u32),
  661. .regset_get = genregs32_get, .set = genregs32_set
  662. },
  663. /* Format is:
  664. * F0 --> F31
  665. * empty 32-bit word
  666. * FSR (32--bit word)
  667. * FPU QUEUE COUNT (8-bit char)
  668. * FPU QUEUE ENTRYSIZE (8-bit char)
  669. * FPU ENABLED (8-bit char)
  670. * empty 8-bit char
  671. * FPU QUEUE (64 32-bit ints)
  672. */
  673. [REGSET_FP] = {
  674. .core_note_type = NT_PRFPREG,
  675. .n = 99,
  676. .size = sizeof(u32), .align = sizeof(u32),
  677. .regset_get = fpregs32_get, .set = fpregs32_set
  678. },
  679. };
  680. static int getregs_get(struct task_struct *target,
  681. const struct user_regset *regset,
  682. struct membuf to)
  683. {
  684. const struct pt_regs *regs = task_pt_regs(target);
  685. int i;
  686. if (target == current)
  687. flushw_user();
  688. membuf_store(&to, (u32)tstate_to_psr(regs->tstate));
  689. membuf_store(&to, (u32)(regs->tpc));
  690. membuf_store(&to, (u32)(regs->tnpc));
  691. membuf_store(&to, (u32)(regs->y));
  692. for (i = 1; i < 16; i++)
  693. membuf_store(&to, (u32)regs->u_regs[i]);
  694. return to.left;
  695. }
  696. static int setregs_set(struct task_struct *target,
  697. const struct user_regset *regset,
  698. unsigned int pos, unsigned int count,
  699. const void *kbuf, const void __user *ubuf)
  700. {
  701. struct pt_regs *regs = task_pt_regs(target);
  702. unsigned long tstate;
  703. u32 uregs[19];
  704. int i, ret;
  705. if (target == current)
  706. flushw_user();
  707. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  708. uregs,
  709. 0, 19 * sizeof(u32));
  710. if (ret)
  711. return ret;
  712. tstate = regs->tstate;
  713. tstate &= ~(TSTATE_ICC | TSTATE_XCC | TSTATE_SYSCALL);
  714. tstate |= psr_to_tstate_icc(uregs[0]);
  715. if (uregs[0] & PSR_SYSCALL)
  716. tstate |= TSTATE_SYSCALL;
  717. regs->tstate = tstate;
  718. regs->tpc = uregs[1];
  719. regs->tnpc = uregs[2];
  720. regs->y = uregs[3];
  721. for (i = 1; i < 15; i++)
  722. regs->u_regs[i] = uregs[3 + i];
  723. return 0;
  724. }
  725. static int getfpregs_get(struct task_struct *target,
  726. const struct user_regset *regset,
  727. struct membuf to)
  728. {
  729. struct thread_info *t = task_thread_info(target);
  730. if (target == current)
  731. save_and_clear_fpu();
  732. membuf_write(&to, t->fpregs, 32 * sizeof(u32));
  733. if (t->fpsaved[0] & FPRS_FEF)
  734. membuf_store(&to, (u32)t->xfsr[0]);
  735. else
  736. membuf_zero(&to, sizeof(u32));
  737. return membuf_zero(&to, 35 * sizeof(u32));
  738. }
  739. static int setfpregs_set(struct task_struct *target,
  740. const struct user_regset *regset,
  741. unsigned int pos, unsigned int count,
  742. const void *kbuf, const void __user *ubuf)
  743. {
  744. unsigned long *fpregs = task_thread_info(target)->fpregs;
  745. unsigned long fprs;
  746. int ret;
  747. if (target == current)
  748. save_and_clear_fpu();
  749. fprs = task_thread_info(target)->fpsaved[0];
  750. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  751. fpregs,
  752. 0, 32 * sizeof(u32));
  753. if (!ret) {
  754. compat_ulong_t fsr;
  755. unsigned long val;
  756. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  757. &fsr,
  758. 32 * sizeof(u32),
  759. 33 * sizeof(u32));
  760. if (!ret) {
  761. val = task_thread_info(target)->xfsr[0];
  762. val &= 0xffffffff00000000UL;
  763. val |= fsr;
  764. task_thread_info(target)->xfsr[0] = val;
  765. }
  766. }
  767. fprs |= (FPRS_FEF | FPRS_DL);
  768. task_thread_info(target)->fpsaved[0] = fprs;
  769. return ret;
  770. }
  771. static const struct user_regset ptrace32_regsets[] = {
  772. [REGSET_GENERAL] = {
  773. .n = 19, .size = sizeof(u32),
  774. .regset_get = getregs_get, .set = setregs_set,
  775. },
  776. [REGSET_FP] = {
  777. .n = 68, .size = sizeof(u32),
  778. .regset_get = getfpregs_get, .set = setfpregs_set,
  779. },
  780. };
  781. static const struct user_regset_view ptrace32_view = {
  782. .regsets = ptrace32_regsets, .n = ARRAY_SIZE(ptrace32_regsets)
  783. };
  784. static const struct user_regset_view user_sparc32_view = {
  785. .name = "sparc", .e_machine = EM_SPARC,
  786. .regsets = sparc32_regsets, .n = ARRAY_SIZE(sparc32_regsets)
  787. };
  788. #endif /* CONFIG_COMPAT */
  789. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  790. {
  791. #ifdef CONFIG_COMPAT
  792. if (test_tsk_thread_flag(task, TIF_32BIT))
  793. return &user_sparc32_view;
  794. #endif
  795. return &user_sparc64_view;
  796. }
  797. #ifdef CONFIG_COMPAT
  798. struct compat_fps {
  799. unsigned int regs[32];
  800. unsigned int fsr;
  801. unsigned int flags;
  802. unsigned int extra;
  803. unsigned int fpqd;
  804. struct compat_fq {
  805. unsigned int insnaddr;
  806. unsigned int insn;
  807. } fpq[16];
  808. };
  809. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  810. compat_ulong_t caddr, compat_ulong_t cdata)
  811. {
  812. compat_ulong_t caddr2 = task_pt_regs(current)->u_regs[UREG_I4];
  813. struct pt_regs32 __user *pregs;
  814. struct compat_fps __user *fps;
  815. unsigned long addr2 = caddr2;
  816. unsigned long addr = caddr;
  817. unsigned long data = cdata;
  818. int ret;
  819. pregs = (struct pt_regs32 __user *) addr;
  820. fps = (struct compat_fps __user *) addr;
  821. switch (request) {
  822. case PTRACE_PEEKUSR:
  823. ret = (addr != 0) ? -EIO : 0;
  824. break;
  825. case PTRACE_GETREGS:
  826. ret = copy_regset_to_user(child, &ptrace32_view,
  827. REGSET_GENERAL, 0,
  828. 19 * sizeof(u32),
  829. pregs);
  830. break;
  831. case PTRACE_SETREGS:
  832. ret = copy_regset_from_user(child, &ptrace32_view,
  833. REGSET_GENERAL, 0,
  834. 19 * sizeof(u32),
  835. pregs);
  836. break;
  837. case PTRACE_GETFPREGS:
  838. ret = copy_regset_to_user(child, &ptrace32_view,
  839. REGSET_FP, 0,
  840. 68 * sizeof(u32),
  841. fps);
  842. break;
  843. case PTRACE_SETFPREGS:
  844. ret = copy_regset_from_user(child, &ptrace32_view,
  845. REGSET_FP, 0,
  846. 33 * sizeof(u32),
  847. fps);
  848. break;
  849. case PTRACE_READTEXT:
  850. case PTRACE_READDATA:
  851. ret = ptrace_readdata(child, addr,
  852. (char __user *)addr2, data);
  853. if (ret == data)
  854. ret = 0;
  855. else if (ret >= 0)
  856. ret = -EIO;
  857. break;
  858. case PTRACE_WRITETEXT:
  859. case PTRACE_WRITEDATA:
  860. ret = ptrace_writedata(child, (char __user *) addr2,
  861. addr, data);
  862. if (ret == data)
  863. ret = 0;
  864. else if (ret >= 0)
  865. ret = -EIO;
  866. break;
  867. default:
  868. if (request == PTRACE_SPARC_DETACH)
  869. request = PTRACE_DETACH;
  870. ret = compat_ptrace_request(child, request, addr, data);
  871. break;
  872. }
  873. return ret;
  874. }
  875. #endif /* CONFIG_COMPAT */
  876. struct fps {
  877. unsigned int regs[64];
  878. unsigned long fsr;
  879. };
  880. long arch_ptrace(struct task_struct *child, long request,
  881. unsigned long addr, unsigned long data)
  882. {
  883. const struct user_regset_view *view = task_user_regset_view(current);
  884. unsigned long addr2 = task_pt_regs(current)->u_regs[UREG_I4];
  885. struct pt_regs __user *pregs;
  886. struct fps __user *fps;
  887. void __user *addr2p;
  888. int ret;
  889. pregs = (struct pt_regs __user *) addr;
  890. fps = (struct fps __user *) addr;
  891. addr2p = (void __user *) addr2;
  892. switch (request) {
  893. case PTRACE_PEEKUSR:
  894. ret = (addr != 0) ? -EIO : 0;
  895. break;
  896. case PTRACE_GETREGS64:
  897. ret = copy_regset_to_user(child, &ptrace64_view,
  898. REGSET_GENERAL, 0,
  899. 19 * sizeof(u64),
  900. pregs);
  901. break;
  902. case PTRACE_SETREGS64:
  903. ret = copy_regset_from_user(child, &ptrace64_view,
  904. REGSET_GENERAL, 0,
  905. 19 * sizeof(u64),
  906. pregs);
  907. break;
  908. case PTRACE_GETFPREGS64:
  909. ret = copy_regset_to_user(child, view, REGSET_FP,
  910. 0 * sizeof(u64),
  911. 33 * sizeof(u64),
  912. fps);
  913. break;
  914. case PTRACE_SETFPREGS64:
  915. ret = copy_regset_from_user(child, view, REGSET_FP,
  916. 0 * sizeof(u64),
  917. 33 * sizeof(u64),
  918. fps);
  919. break;
  920. case PTRACE_READTEXT:
  921. case PTRACE_READDATA:
  922. ret = ptrace_readdata(child, addr, addr2p, data);
  923. if (ret == data)
  924. ret = 0;
  925. else if (ret >= 0)
  926. ret = -EIO;
  927. break;
  928. case PTRACE_WRITETEXT:
  929. case PTRACE_WRITEDATA:
  930. ret = ptrace_writedata(child, addr2p, addr, data);
  931. if (ret == data)
  932. ret = 0;
  933. else if (ret >= 0)
  934. ret = -EIO;
  935. break;
  936. default:
  937. if (request == PTRACE_SPARC_DETACH)
  938. request = PTRACE_DETACH;
  939. ret = ptrace_request(child, request, addr, data);
  940. break;
  941. }
  942. return ret;
  943. }
  944. asmlinkage int syscall_trace_enter(struct pt_regs *regs)
  945. {
  946. int ret = 0;
  947. /* do the secure computing check first */
  948. secure_computing_strict(regs->u_regs[UREG_G1]);
  949. if (test_thread_flag(TIF_NOHZ))
  950. user_exit();
  951. if (test_thread_flag(TIF_SYSCALL_TRACE))
  952. ret = ptrace_report_syscall_entry(regs);
  953. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  954. trace_sys_enter(regs, regs->u_regs[UREG_G1]);
  955. audit_syscall_entry(regs->u_regs[UREG_G1], regs->u_regs[UREG_I0],
  956. regs->u_regs[UREG_I1], regs->u_regs[UREG_I2],
  957. regs->u_regs[UREG_I3]);
  958. return ret;
  959. }
  960. asmlinkage void syscall_trace_leave(struct pt_regs *regs)
  961. {
  962. if (test_thread_flag(TIF_NOHZ))
  963. user_exit();
  964. audit_syscall_exit(regs);
  965. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  966. trace_sys_exit(regs, regs->u_regs[UREG_I0]);
  967. if (test_thread_flag(TIF_SYSCALL_TRACE))
  968. ptrace_report_syscall_exit(regs, 0);
  969. if (test_thread_flag(TIF_NOHZ))
  970. user_enter();
  971. }
  972. /**
  973. * regs_query_register_offset() - query register offset from its name
  974. * @name: the name of a register
  975. *
  976. * regs_query_register_offset() returns the offset of a register in struct
  977. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  978. */
  979. int regs_query_register_offset(const char *name)
  980. {
  981. const struct pt_regs_offset *roff;
  982. for (roff = regoffset_table; roff->name != NULL; roff++)
  983. if (!strcmp(roff->name, name))
  984. return roff->offset;
  985. return -EINVAL;
  986. }
  987. /**
  988. * regs_within_kernel_stack() - check the address in the stack
  989. * @regs: pt_regs which contains kernel stack pointer.
  990. * @addr: address which is checked.
  991. *
  992. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  993. * If @addr is within the kernel stack, it returns true. If not, returns false.
  994. */
  995. static inline int regs_within_kernel_stack(struct pt_regs *regs,
  996. unsigned long addr)
  997. {
  998. unsigned long ksp = kernel_stack_pointer(regs) + STACK_BIAS;
  999. return ((addr & ~(THREAD_SIZE - 1)) ==
  1000. (ksp & ~(THREAD_SIZE - 1)));
  1001. }
  1002. /**
  1003. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  1004. * @regs: pt_regs which contains kernel stack pointer.
  1005. * @n: stack entry number.
  1006. *
  1007. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  1008. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  1009. * this returns 0.
  1010. */
  1011. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  1012. {
  1013. unsigned long ksp = kernel_stack_pointer(regs) + STACK_BIAS;
  1014. unsigned long *addr = (unsigned long *)ksp;
  1015. addr += n;
  1016. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  1017. return *addr;
  1018. else
  1019. return 0;
  1020. }