ptrace_64.c 28 KB

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