ptrace_64.c 25 KB

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